1 // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -fblocks -fobjc-arc -fobjc-runtime-has-weak -O2 -disable-llvm-passes -o - %s | FileCheck %s
5 void test0(_Bool cond) {
6 id test0_helper(void) __attribute__((ns_returns_retained));
8 // CHECK-LABEL: define{{.*}} void @test0(
9 // CHECK: [[COND:%.*]] = alloca i8,
10 // CHECK-NEXT: [[X:%.*]] = alloca ptr,
11 // CHECK-NEXT: [[RELVAL:%.*]] = alloca ptr
12 // CHECK-NEXT: [[RELCOND:%.*]] = alloca i1
15 // CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 8, ptr [[X]])
16 // CHECK-NEXT: [[T0:%.*]] = load i8, ptr [[COND]]
17 // CHECK-NEXT: [[T1:%.*]] = trunc i8 [[T0]] to i1
18 // CHECK-NEXT: store i1 false, ptr [[RELCOND]]
19 // CHECK-NEXT: br i1 [[T1]],
21 // CHECK: [[CALL:%.*]] = call ptr @test0_helper()
22 // CHECK-NEXT: store ptr [[CALL]], ptr [[RELVAL]]
23 // CHECK-NEXT: store i1 true, ptr [[RELCOND]]
24 // CHECK-NEXT: br label
25 // CHECK: [[T0:%.*]] = phi ptr [ null, {{%.*}} ], [ [[CALL]], {{%.*}} ]
26 // CHECK-NEXT: [[T1:%.*]] = call ptr @llvm.objc.retain(ptr [[T0]]) [[NUW:#[0-9]+]]
27 // CHECK-NEXT: store ptr [[T1]], ptr [[X]],
28 // CHECK-NEXT: [[REL:%.*]] = load i1, ptr [[RELCOND]]
29 // CHECK-NEXT: br i1 [[REL]],
30 // CHECK: [[T0:%.*]] = load ptr, ptr [[RELVAL]]
31 // CHECK-NEXT: call void @llvm.objc.release(ptr [[T0]]) [[NUW]]
32 // CHECK-NEXT: br label
33 // CHECK: [[T0:%.*]] = load ptr, ptr [[X]]
34 // CHECK-NEXT: call void @llvm.objc.release(ptr [[T0]]) [[NUW]]
35 // CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 8, ptr [[X]])
36 // CHECK-NEXT: ret void
37 id x = (cond ? 0 : test0_helper());
40 void test1(int cond) {
43 extern void test1_sink(id *);
44 test1_sink(cond ? &strong : 0);
45 test1_sink(cond ? &weak : 0);
47 // CHECK-LABEL: define{{.*}} void @test1(
48 // CHECK: [[COND:%.*]] = alloca i32
49 // CHECK-NEXT: [[STRONG:%.*]] = alloca ptr
50 // CHECK-NEXT: [[WEAK:%.*]] = alloca ptr
51 // CHECK-NEXT: [[TEMP1:%.*]] = alloca ptr
52 // CHECK-NEXT: [[TEMP2:%.*]] = alloca ptr
53 // CHECK-NEXT: [[CONDCLEANUPSAVE:%.*]] = alloca ptr
54 // CHECK-NEXT: [[CONDCLEANUP:%.*]] = alloca i1
55 // CHECK-NEXT: store i32
56 // CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 8, ptr [[STRONG]])
57 // CHECK-NEXT: store ptr null, ptr [[STRONG]]
58 // CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 8, ptr [[WEAK]])
59 // CHECK-NEXT: call ptr @llvm.objc.initWeak(ptr [[WEAK]], ptr null)
61 // CHECK-NEXT: [[T0:%.*]] = load i32, ptr [[COND]]
62 // CHECK-NEXT: [[T1:%.*]] = icmp ne i32 [[T0]], 0
63 // CHECK: [[ARG:%.*]] = phi ptr
64 // CHECK-NEXT: [[T0:%.*]] = icmp eq ptr [[ARG]], null
65 // CHECK-NEXT: [[T1:%.*]] = select i1 [[T0]], ptr null, ptr [[TEMP1]]
66 // CHECK-NEXT: br i1 [[T0]],
67 // CHECK: [[T0:%.*]] = load ptr, ptr [[ARG]]
68 // CHECK-NEXT: store ptr [[T0]], ptr [[TEMP1]]
69 // CHECK-NEXT: br label
70 // CHECK: [[W:%.*]] = phi ptr [ [[T0]], {{%.*}} ], [ undef, {{%.*}} ]
71 // CHECK-NEXT: call void @test1_sink(ptr noundef [[T1]])
72 // CHECK-NEXT: [[T0:%.*]] = icmp eq ptr [[ARG]], null
73 // CHECK-NEXT: br i1 [[T0]],
74 // CHECK: [[T0:%.*]] = load ptr, ptr [[TEMP1]]
75 // CHECK-NEXT: [[T1:%.*]] = call ptr @llvm.objc.retain(ptr [[T0]])
76 // CHECK-NEXT: call void (...) @llvm.objc.clang.arc.use(ptr [[W]]) [[NUW]]
77 // CHECK-NEXT: [[T2:%.*]] = load ptr, ptr [[ARG]]
78 // CHECK-NEXT: store ptr [[T1]], ptr [[ARG]]
79 // CHECK-NEXT: call void @llvm.objc.release(ptr [[T2]])
80 // CHECK-NEXT: br label
82 // CHECK: [[T0:%.*]] = load i32, ptr [[COND]]
83 // CHECK-NEXT: [[T1:%.*]] = icmp ne i32 [[T0]], 0
84 // CHECK: [[ARG:%.*]] = phi ptr
85 // CHECK-NEXT: [[T0:%.*]] = icmp eq ptr [[ARG]], null
86 // CHECK-NEXT: [[T1:%.*]] = select i1 [[T0]], ptr null, ptr [[TEMP2]]
87 // CHECK-NEXT: store i1 false, ptr [[CONDCLEANUP]]
88 // CHECK-NEXT: br i1 [[T0]],
89 // CHECK: [[T0:%.*]] = call ptr @llvm.objc.loadWeakRetained(ptr [[ARG]])
90 // CHECK-NEXT: store ptr [[T0]], ptr [[CONDCLEANUPSAVE]]
91 // CHECK-NEXT: store i1 true, ptr [[CONDCLEANUP]]
92 // CHECK-NEXT: store ptr [[T0]], ptr [[TEMP2]]
93 // CHECK-NEXT: br label
94 // CHECK: call void @test1_sink(ptr noundef [[T1]])
95 // CHECK-NEXT: [[T0:%.*]] = icmp eq ptr [[ARG]], null
96 // CHECK-NEXT: br i1 [[T0]],
97 // CHECK: [[T0:%.*]] = load ptr, ptr [[TEMP2]]
98 // CHECK-NEXT: call ptr @llvm.objc.storeWeak(ptr [[ARG]], ptr [[T0]])
99 // CHECK-NEXT: br label
101 // CHECK: call void @llvm.objc.destroyWeak(ptr [[WEAK]])
102 // CHECK: call void @llvm.lifetime.end.p0(i64 8, ptr [[WEAK]])
103 // CHECK: call void @llvm.lifetime.end.p0(i64 8, ptr [[STRONG]])
107 // Test that, when emitting an expression at +1 that we can't peephole,
108 // we emit the retain inside the full-expression. If we ever peephole
109 // +1s of conditional expressions (which we probably ought to), we'll
110 // need to find another example of something we need to do this for.
111 void test2(int cond) {
112 extern id test2_producer(void);
113 for (id obj in cond ? test2_producer() : (void*) 0) {
116 // CHECK-LABEL: define{{.*}} void @test2(
117 // CHECK: [[COND:%.*]] = alloca i32,
119 // CHECK: [[CLEANUP_SAVE:%.*]] = alloca ptr
120 // CHECK: [[RUN_CLEANUP:%.*]] = alloca i1
121 // Evaluate condition; cleanup disabled by default.
122 // CHECK: [[T0:%.*]] = load i32, ptr [[COND]],
123 // CHECK-NEXT: icmp ne i32 [[T0]], 0
124 // CHECK-NEXT: store i1 false, ptr [[RUN_CLEANUP]]
126 // Within true branch, cleanup enabled.
127 // CHECK: [[T1:%.*]] = call ptr @test2_producer() [ "clang.arc.attachedcall"(ptr @llvm.objc.retainAutoreleasedReturnValue) ]
128 // CHECK-NEXT: call void (...) @llvm.objc.clang.arc.noop.use(ptr [[T1]])
129 // CHECK-NEXT: store ptr [[T1]], ptr [[CLEANUP_SAVE]]
130 // CHECK-NEXT: store i1 true, ptr [[RUN_CLEANUP]]
131 // CHECK-NEXT: br label
132 // Join point for conditional operator; retain immediately.
133 // CHECK: [[T0:%.*]] = phi ptr [ [[T1]], {{%.*}} ], [ null, {{%.*}} ]
134 // CHECK-NEXT: [[RESULT:%.*]] = call ptr @llvm.objc.retain(ptr [[T0]])
135 // Leaving full-expression; run conditional cleanup.
136 // CHECK-NEXT: [[T0:%.*]] = load i1, ptr [[RUN_CLEANUP]]
137 // CHECK-NEXT: br i1 [[T0]]
138 // CHECK: [[T0:%.*]] = load ptr, ptr [[CLEANUP_SAVE]]
139 // CHECK-NEXT: call void @llvm.objc.release(ptr [[T0]])
140 // CHECK-NEXT: br label
141 // And way down at the end of the loop:
142 // CHECK: call void @llvm.objc.release(ptr [[RESULT]])
145 void test3(int cond) {
146 __strong id *p = cond ? (__strong id[]){g0, g1} : (__strong id[]){g1, g0};
149 // CHECK: define{{.*}} void @test3(
150 // CHECK: %[[P:.*]] = alloca ptr, align 8
151 // CHECK: %[[_COMPOUNDLITERAL:.*]] = alloca [2 x ptr], align 8
152 // CHECK: %[[CLEANUP_COND:.*]] = alloca i1, align 1
153 // CHECK: %[[_COMPOUNDLITERAL1:.*]] = alloca [2 x ptr], align 8
154 // CHECK: %[[CLEANUP_COND4:.*]] = alloca i1, align 1
156 // CHECK: %[[V2:.*]] = load ptr, ptr @g0, align 8
157 // CHECK: %[[V3:.*]] = call ptr @llvm.objc.retain(ptr %[[V2]])
158 // CHECK: store ptr %[[V3]], ptr %[[_COMPOUNDLITERAL]], align 8
159 // CHECK: %[[ARRAYINIT_ELEMENT:.*]] = getelementptr inbounds ptr, ptr %[[_COMPOUNDLITERAL]], i64 1
160 // CHECK: %[[V4:.*]] = load ptr, ptr @g1, align 8
161 // CHECK: %[[V5:.*]] = call ptr @llvm.objc.retain(ptr %[[V4]])
162 // CHECK: store ptr %[[V5]], ptr %[[ARRAYINIT_ELEMENT]], align 8
163 // CHECK: store i1 true, ptr %[[CLEANUP_COND]], align 1
164 // CHECK: %[[ARRAYDECAY:.*]] = getelementptr inbounds [2 x ptr], ptr %[[_COMPOUNDLITERAL]], i64 0, i64 0
166 // CHECK: %[[V6:.*]] = load ptr, ptr @g1, align 8
167 // CHECK: %[[V7:.*]] = call ptr @llvm.objc.retain(ptr %[[V6]])
168 // CHECK: store ptr %[[V7]], ptr %[[_COMPOUNDLITERAL1]], align 8
169 // CHECK: %[[ARRAYINIT_ELEMENT3:.*]] = getelementptr inbounds ptr, ptr %[[_COMPOUNDLITERAL1]], i64 1
170 // CHECK: %[[V8:.*]] = load ptr, ptr @g0, align 8
171 // CHECK: %[[V9:.*]] = call ptr @llvm.objc.retain(ptr %[[V8]])
172 // CHECK: store ptr %[[V9]], ptr %[[ARRAYINIT_ELEMENT3]], align 8
173 // CHECK: store i1 true, ptr %[[CLEANUP_COND4]], align 1
174 // CHECK: %[[ARRAYDECAY5:.*]] = getelementptr inbounds [2 x ptr], ptr %[[_COMPOUNDLITERAL1]], i64 0, i64 0
176 // CHECK: %[[COND6:.*]] = phi ptr [ %[[ARRAYDECAY]], %{{.*}} ], [ %[[ARRAYDECAY5]], %{{.*}} ]
177 // CHECK: store ptr %[[COND6]], ptr %[[P]], align 8
178 // CHECK: call void @test2(
180 // CHECK: %[[ARRAY_BEGIN:.*]] = getelementptr inbounds [2 x ptr], ptr %[[_COMPOUNDLITERAL1]], i32 0, i32 0
181 // CHECK: %[[V11:.*]] = getelementptr inbounds ptr, ptr %[[ARRAY_BEGIN]], i64 2
183 // CHECK: %[[ARRAYDESTROY_ELEMENTPAST:.*]] = phi ptr [ %[[V11]], %{{.*}} ], [ %[[ARRAYDESTROY_ELEMENT:.*]], %{{.*}} ]
184 // CHECK: %[[ARRAYDESTROY_ELEMENT]] = getelementptr inbounds ptr, ptr %[[ARRAYDESTROY_ELEMENTPAST]], i64 -1
185 // CHECK: %[[V12:.*]] = load ptr, ptr %[[ARRAYDESTROY_ELEMENT]], align 8
186 // CHECK: call void @llvm.objc.release(ptr %[[V12]])
188 // CHECK: %[[ARRAY_BEGIN10:.*]] = getelementptr inbounds [2 x ptr], ptr %[[_COMPOUNDLITERAL]], i32 0, i32 0
189 // CHECK: %[[V13:.*]] = getelementptr inbounds ptr, ptr %[[ARRAY_BEGIN10]], i64 2
191 // CHECK: %[[ARRAYDESTROY_ELEMENTPAST12:.*]] = phi ptr [ %[[V13]], %{{.*}} ], [ %[[ARRAYDESTROY_ELEMENT13:.*]], %{{.*}} ]
192 // CHECK: %[[ARRAYDESTROY_ELEMENT13]] = getelementptr inbounds ptr, ptr %[[ARRAYDESTROY_ELEMENTPAST12]], i64 -1
193 // CHECK: %[[V14:.*]] = load ptr, ptr %[[ARRAYDESTROY_ELEMENT13]], align 8
194 // CHECK: call void @llvm.objc.release(ptr %[[V14]])
197 // CHECK: attributes [[NUW]] = { nounwind }