1; RUN: opt -objc-arc -S < %s | FileCheck %s
2; rdar://11229925
3
4target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
5
6%struct.__block_byref_weakLogNTimes = type { i8*, %struct.__block_byref_weakLogNTimes*, i32, i32, i8*, i8*, void (...)* }
7%struct.__block_descriptor = type { i64, i64 }
8
9; Don't optimize away the retainBlock, because the object's address "escapes"
10; with the objc_storeWeak call.
11
12; CHECK-LABEL: define void @test0(
13; CHECK: %tmp7 = call i8* @objc_retainBlock(i8* %tmp6) [[NUW:#[0-9]+]], !clang.arc.copy_on_escape !0
14; CHECK: call void @objc_release(i8* %tmp7) [[NUW]], !clang.imprecise_release !0
15; CHECK: }
16define void @test0() nounwind {
17entry:
18  %weakLogNTimes = alloca %struct.__block_byref_weakLogNTimes, align 8
19  %block = alloca <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>, align 8
20  %byref.isa = getelementptr inbounds %struct.__block_byref_weakLogNTimes, %struct.__block_byref_weakLogNTimes* %weakLogNTimes, i64 0, i32 0
21  store i8* null, i8** %byref.isa, align 8
22  %byref.forwarding = getelementptr inbounds %struct.__block_byref_weakLogNTimes, %struct.__block_byref_weakLogNTimes* %weakLogNTimes, i64 0, i32 1
23  store %struct.__block_byref_weakLogNTimes* %weakLogNTimes, %struct.__block_byref_weakLogNTimes** %byref.forwarding, align 8
24  %byref.flags = getelementptr inbounds %struct.__block_byref_weakLogNTimes, %struct.__block_byref_weakLogNTimes* %weakLogNTimes, i64 0, i32 2
25  store i32 33554432, i32* %byref.flags, align 8
26  %byref.size = getelementptr inbounds %struct.__block_byref_weakLogNTimes, %struct.__block_byref_weakLogNTimes* %weakLogNTimes, i64 0, i32 3
27  store i32 48, i32* %byref.size, align 4
28  %tmp1 = getelementptr inbounds %struct.__block_byref_weakLogNTimes, %struct.__block_byref_weakLogNTimes* %weakLogNTimes, i64 0, i32 4
29  store i8* bitcast (void (i8*, i8*)* @__Block_byref_object_copy_ to i8*), i8** %tmp1, align 8
30  %tmp2 = getelementptr inbounds %struct.__block_byref_weakLogNTimes, %struct.__block_byref_weakLogNTimes* %weakLogNTimes, i64 0, i32 5
31  store i8* bitcast (void (i8*)* @__Block_byref_object_dispose_ to i8*), i8** %tmp2, align 8
32  %weakLogNTimes1 = getelementptr inbounds %struct.__block_byref_weakLogNTimes, %struct.__block_byref_weakLogNTimes* %weakLogNTimes, i64 0, i32 6
33  %tmp3 = bitcast void (...)** %weakLogNTimes1 to i8**
34  %tmp4 = call i8* @objc_initWeak(i8** %tmp3, i8* null) nounwind
35  %block.isa = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>, <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>* %block, i64 0, i32 0
36  store i8* null, i8** %block.isa, align 8
37  %block.flags = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>, <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>* %block, i64 0, i32 1
38  store i32 1107296256, i32* %block.flags, align 8
39  %block.reserved = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>, <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>* %block, i64 0, i32 2
40  store i32 0, i32* %block.reserved, align 4
41  %block.invoke = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>, <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>* %block, i64 0, i32 3
42  store i8* bitcast (void (i8*, i32)* @__main_block_invoke_0 to i8*), i8** %block.invoke, align 8
43  %block.descriptor = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>, <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>* %block, i64 0, i32 4
44  store %struct.__block_descriptor* null, %struct.__block_descriptor** %block.descriptor, align 8
45  %block.captured = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>, <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>* %block, i64 0, i32 5
46  %tmp5 = bitcast %struct.__block_byref_weakLogNTimes* %weakLogNTimes to i8*
47  store i8* %tmp5, i8** %block.captured, align 8
48  %tmp6 = bitcast <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>* %block to i8*
49  %tmp7 = call i8* @objc_retainBlock(i8* %tmp6) nounwind, !clang.arc.copy_on_escape !0
50  %tmp8 = load %struct.__block_byref_weakLogNTimes*, %struct.__block_byref_weakLogNTimes** %byref.forwarding, align 8
51  %weakLogNTimes3 = getelementptr inbounds %struct.__block_byref_weakLogNTimes, %struct.__block_byref_weakLogNTimes* %tmp8, i64 0, i32 6
52  %tmp9 = bitcast void (...)** %weakLogNTimes3 to i8**
53  %tmp10 = call i8* @objc_storeWeak(i8** %tmp9, i8* %tmp7) nounwind
54  %tmp11 = getelementptr inbounds i8, i8* %tmp7, i64 16
55  %tmp12 = bitcast i8* %tmp11 to i8**
56  %tmp13 = load i8*, i8** %tmp12, align 8
57  %tmp14 = bitcast i8* %tmp13 to void (i8*, i32)*
58  call void %tmp14(i8* %tmp7, i32 10) nounwind, !clang.arc.no_objc_arc_exceptions !0
59  call void @objc_release(i8* %tmp7) nounwind, !clang.imprecise_release !0
60  call void @_Block_object_dispose(i8* %tmp5, i32 8) nounwind
61  call void @objc_destroyWeak(i8** %tmp3) nounwind
62  ret void
63}
64
65; Like test0, but it makes a regular call instead of a storeWeak call,
66; so the optimization is valid.
67
68; CHECK-LABEL: define void @test1(
69; CHECK-NOT: @objc_retainBlock
70; CHECK: }
71define void @test1() nounwind {
72entry:
73  %weakLogNTimes = alloca %struct.__block_byref_weakLogNTimes, align 8
74  %block = alloca <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>, align 8
75  %byref.isa = getelementptr inbounds %struct.__block_byref_weakLogNTimes, %struct.__block_byref_weakLogNTimes* %weakLogNTimes, i64 0, i32 0
76  store i8* null, i8** %byref.isa, align 8
77  %byref.forwarding = getelementptr inbounds %struct.__block_byref_weakLogNTimes, %struct.__block_byref_weakLogNTimes* %weakLogNTimes, i64 0, i32 1
78  store %struct.__block_byref_weakLogNTimes* %weakLogNTimes, %struct.__block_byref_weakLogNTimes** %byref.forwarding, align 8
79  %byref.flags = getelementptr inbounds %struct.__block_byref_weakLogNTimes, %struct.__block_byref_weakLogNTimes* %weakLogNTimes, i64 0, i32 2
80  store i32 33554432, i32* %byref.flags, align 8
81  %byref.size = getelementptr inbounds %struct.__block_byref_weakLogNTimes, %struct.__block_byref_weakLogNTimes* %weakLogNTimes, i64 0, i32 3
82  store i32 48, i32* %byref.size, align 4
83  %tmp1 = getelementptr inbounds %struct.__block_byref_weakLogNTimes, %struct.__block_byref_weakLogNTimes* %weakLogNTimes, i64 0, i32 4
84  store i8* bitcast (void (i8*, i8*)* @__Block_byref_object_copy_ to i8*), i8** %tmp1, align 8
85  %tmp2 = getelementptr inbounds %struct.__block_byref_weakLogNTimes, %struct.__block_byref_weakLogNTimes* %weakLogNTimes, i64 0, i32 5
86  store i8* bitcast (void (i8*)* @__Block_byref_object_dispose_ to i8*), i8** %tmp2, align 8
87  %weakLogNTimes1 = getelementptr inbounds %struct.__block_byref_weakLogNTimes, %struct.__block_byref_weakLogNTimes* %weakLogNTimes, i64 0, i32 6
88  %tmp3 = bitcast void (...)** %weakLogNTimes1 to i8**
89  %tmp4 = call i8* @objc_initWeak(i8** %tmp3, i8* null) nounwind
90  %block.isa = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>, <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>* %block, i64 0, i32 0
91  store i8* null, i8** %block.isa, align 8
92  %block.flags = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>, <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>* %block, i64 0, i32 1
93  store i32 1107296256, i32* %block.flags, align 8
94  %block.reserved = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>, <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>* %block, i64 0, i32 2
95  store i32 0, i32* %block.reserved, align 4
96  %block.invoke = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>, <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>* %block, i64 0, i32 3
97  store i8* bitcast (void (i8*, i32)* @__main_block_invoke_0 to i8*), i8** %block.invoke, align 8
98  %block.descriptor = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>, <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>* %block, i64 0, i32 4
99  store %struct.__block_descriptor* null, %struct.__block_descriptor** %block.descriptor, align 8
100  %block.captured = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>, <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>* %block, i64 0, i32 5
101  %tmp5 = bitcast %struct.__block_byref_weakLogNTimes* %weakLogNTimes to i8*
102  store i8* %tmp5, i8** %block.captured, align 8
103  %tmp6 = bitcast <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i8* }>* %block to i8*
104  %tmp7 = call i8* @objc_retainBlock(i8* %tmp6) nounwind, !clang.arc.copy_on_escape !0
105  %tmp8 = load %struct.__block_byref_weakLogNTimes*, %struct.__block_byref_weakLogNTimes** %byref.forwarding, align 8
106  %weakLogNTimes3 = getelementptr inbounds %struct.__block_byref_weakLogNTimes, %struct.__block_byref_weakLogNTimes* %tmp8, i64 0, i32 6
107  %tmp9 = bitcast void (...)** %weakLogNTimes3 to i8**
108  %tmp10 = call i8* @not_really_objc_storeWeak(i8** %tmp9, i8* %tmp7) nounwind
109  %tmp11 = getelementptr inbounds i8, i8* %tmp7, i64 16
110  %tmp12 = bitcast i8* %tmp11 to i8**
111  %tmp13 = load i8*, i8** %tmp12, align 8
112  %tmp14 = bitcast i8* %tmp13 to void (i8*, i32)*
113  call void %tmp14(i8* %tmp7, i32 10) nounwind, !clang.arc.no_objc_arc_exceptions !0
114  call void @objc_release(i8* %tmp7) nounwind, !clang.imprecise_release !0
115  call void @_Block_object_dispose(i8* %tmp5, i32 8) nounwind
116  call void @objc_destroyWeak(i8** %tmp3) nounwind
117  ret void
118}
119
120declare void @__Block_byref_object_copy_(i8*, i8*) nounwind
121declare void @__Block_byref_object_dispose_(i8*) nounwind
122declare void @objc_destroyWeak(i8**)
123declare i8* @objc_initWeak(i8**, i8*)
124declare void @__main_block_invoke_0(i8* nocapture, i32) nounwind ssp
125declare void @_Block_object_dispose(i8*, i32)
126declare i8* @objc_retainBlock(i8*)
127declare i8* @objc_storeWeak(i8**, i8*)
128declare i8* @not_really_objc_storeWeak(i8**, i8*)
129declare void @objc_release(i8*)
130
131!0 = !{}
132
133; CHECK: attributes [[NUW]] = { nounwind }
134; CHECK: attributes #1 = { nounwind ssp }
135