1 // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -fobjc-runtime-has-weak -fblocks -fobjc-arc -O2 -disable-llvm-passes -o - %s | FileCheck %s
3 struct NSFastEnumerationState;
5 - (unsigned long) countByEnumeratingWithState: (struct NSFastEnumerationState*) state
7 count: (unsigned long) bufferSize;
9 NSArray *nsarray() { return 0; }
10 // CHECK: define{{.*}} ptr @_Z7nsarrayv()
14 // The analogous ObjC testcase test46 in arr.m.
15 void test0(__weak id *wp, __weak volatile id *wvp) {
16 extern id test0_helper(void);
18 // TODO: this is sub-optimal, we should retain at the actual call site.
19 // TODO: in the non-volatile case, we do not need to be reloading.
21 // CHECK: [[T1:%.*]] = call noundef ptr @_Z12test0_helperv() [ "clang.arc.attachedcall"(ptr @llvm.objc.retainAutoreleasedReturnValue) ]
22 // CHECK-NEXT: call void (...) @llvm.objc.clang.arc.noop.use(ptr [[T1]])
23 // CHECK-NEXT: [[T2:%.*]] = load ptr, ptr {{%.*}}, align 8
24 // CHECK-NEXT: [[T3:%.*]] = call ptr @llvm.objc.storeWeak(ptr [[T2]], ptr [[T1]])
25 // CHECK-NEXT: [[T4:%.*]] = call ptr @llvm.objc.retain(ptr [[T3]])
26 // CHECK-NEXT: store ptr [[T4]], ptr
27 // CHECK-NEXT: call void @llvm.objc.release(ptr [[T1]])
28 id x = *wp = test0_helper();
30 // CHECK: [[T1:%.*]] = call noundef ptr @_Z12test0_helperv() [ "clang.arc.attachedcall"(ptr @llvm.objc.retainAutoreleasedReturnValue) ]
31 // CHECK-NEXT: call void (...) @llvm.objc.clang.arc.noop.use(ptr [[T1]])
32 // CHECK-NEXT: [[T2:%.*]] = load ptr, ptr {{%.*}}, align 8
33 // CHECK-NEXT: [[T3:%.*]] = call ptr @llvm.objc.storeWeak(ptr [[T2]], ptr [[T1]])
34 // CHECK-NEXT: [[T4:%.*]] = call ptr @llvm.objc.loadWeakRetained(ptr [[T2]])
35 // CHECK-NEXT: store ptr [[T4]], ptr
36 // CHECK-NEXT: call void @llvm.objc.release(ptr [[T1]])
37 id y = *wvp = test0_helper();
40 struct Test1_helper { Test1_helper(); };
42 @implementation Test1 { Test1_helper x; } @end
43 // CHECK: define internal noundef ptr @"\01-[Test1 .cxx_construct]"(
44 // CHECK: call void @_ZN12Test1_helperC1Ev(
46 // CHECK-NEXT: ret ptr
48 void test34(int cond) {
51 extern void test34_sink(id *);
52 test34_sink(cond ? &strong : 0);
53 test34_sink(cond ? &weak : 0);
55 // CHECK-LABEL: define{{.*}} void @_Z6test34i(
56 // CHECK: [[COND:%.*]] = alloca i32
57 // CHECK-NEXT: [[STRONG:%.*]] = alloca ptr
58 // CHECK-NEXT: [[WEAK:%.*]] = alloca ptr
59 // CHECK-NEXT: [[TEMP1:%.*]] = alloca ptr
60 // CHECK-NEXT: [[TEMP2:%.*]] = alloca ptr
61 // CHECK-NEXT: [[CONDCLEANUPSAVE:%.*]] = alloca ptr
62 // CHECK-NEXT: [[CONDCLEANUP:%.*]] = alloca i1
63 // CHECK-NEXT: store i32
64 // CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 8, ptr [[STRONG]])
65 // CHECK-NEXT: store ptr null, ptr [[STRONG]]
66 // CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 8, ptr [[WEAK]])
67 // CHECK-NEXT: call ptr @llvm.objc.initWeak(ptr [[WEAK]], ptr null)
69 // CHECK-NEXT: [[T0:%.*]] = load i32, ptr [[COND]]
70 // CHECK-NEXT: [[T1:%.*]] = icmp ne i32 [[T0]], 0
71 // CHECK: [[ARG:%.*]] = phi ptr
72 // CHECK-NEXT: [[T0:%.*]] = icmp eq ptr [[ARG]], null
73 // CHECK-NEXT: [[T1:%.*]] = select i1 [[T0]], ptr null, ptr [[TEMP1]]
74 // CHECK-NEXT: br i1 [[T0]],
75 // CHECK: [[T0:%.*]] = load ptr, ptr [[ARG]]
76 // CHECK-NEXT: store ptr [[T0]], ptr [[TEMP1]]
77 // CHECK-NEXT: br label
78 // CHECK: [[W0:%.*]] = phi ptr [ [[T0]], {{%.*}} ], [ undef, {{%.*}} ]
79 // CHECK: call void @_Z11test34_sinkPU15__autoreleasingP11objc_object(ptr noundef [[T1]])
80 // CHECK-NEXT: [[T0:%.*]] = icmp eq ptr [[ARG]], null
81 // CHECK-NEXT: br i1 [[T0]],
82 // CHECK: [[T0:%.*]] = load ptr, ptr [[TEMP1]]
83 // CHECK-NEXT: [[T1:%.*]] = call ptr @llvm.objc.retain(ptr [[T0]])
84 // CHECK-NEXT: call void (...) @llvm.objc.clang.arc.use(ptr [[W0]])
85 // CHECK-NEXT: [[T2:%.*]] = load ptr, ptr [[ARG]]
86 // CHECK-NEXT: store ptr [[T1]], ptr [[ARG]]
87 // CHECK-NEXT: call void @llvm.objc.release(ptr [[T2]])
88 // CHECK-NEXT: br label
90 // CHECK: [[T0:%.*]] = load i32, ptr [[COND]]
91 // CHECK-NEXT: [[T1:%.*]] = icmp ne i32 [[T0]], 0
92 // CHECK: [[ARG:%.*]] = phi ptr
93 // CHECK-NEXT: [[T0:%.*]] = icmp eq ptr [[ARG]], null
94 // CHECK-NEXT: [[T1:%.*]] = select i1 [[T0]], ptr null, ptr [[TEMP2]]
95 // CHECK-NEXT: store i1 false, ptr [[CONDCLEANUP]]
96 // CHECK-NEXT: br i1 [[T0]],
97 // CHECK: [[T0:%.*]] = call ptr @llvm.objc.loadWeakRetained(ptr [[ARG]])
98 // CHECK-NEXT: store ptr [[T0]], ptr [[CONDCLEANUPSAVE]]
99 // CHECK-NEXT: store i1 true, ptr [[CONDCLEANUP]]
100 // CHECK-NEXT: store ptr [[T0]], ptr [[TEMP2]]
101 // CHECK-NEXT: br label
102 // CHECK: call void @_Z11test34_sinkPU15__autoreleasingP11objc_object(ptr noundef [[T1]])
103 // CHECK-NEXT: [[T0:%.*]] = icmp eq ptr [[ARG]], null
104 // CHECK-NEXT: br i1 [[T0]],
105 // CHECK: [[T0:%.*]] = load ptr, ptr [[TEMP2]]
106 // CHECK-NEXT: call ptr @llvm.objc.storeWeak(ptr [[ARG]], ptr [[T0]])
107 // CHECK-NEXT: br label
109 // CHECK: call void @llvm.objc.destroyWeak(ptr [[WEAK]])
113 struct Test35_Helper {
114 static id makeObject1() __attribute__((ns_returns_retained));
115 id makeObject2() __attribute__((ns_returns_retained));
116 static id makeObject3();
120 // CHECK-LABEL: define{{.*}} void @_Z6test3513Test35_HelperPS_
121 void test35(Test35_Helper x0, Test35_Helper *x0p) {
122 // CHECK: call void @llvm.lifetime.start
123 // CHECK: call noundef ptr @_ZN13Test35_Helper11makeObject1Ev
124 // CHECK-NOT: call ptr @llvm.objc.retain
125 id obj1 = Test35_Helper::makeObject1();
126 // CHECK: call void @llvm.lifetime.start
127 // CHECK: call noundef ptr @_ZN13Test35_Helper11makeObject2Ev
128 // CHECK-NOT: call ptr @llvm.objc.retain
129 id obj2 = x0.makeObject2();
130 // CHECK: call void @llvm.lifetime.start
131 // CHECK: call noundef ptr @_ZN13Test35_Helper11makeObject2Ev
132 // CHECK-NOT: call ptr @llvm.objc.retain
133 id obj3 = x0p->makeObject2();
134 id (Test35_Helper::*pmf)() __attribute__((ns_returns_retained))
135 = &Test35_Helper::makeObject2;
136 // CHECK: call void @llvm.lifetime.start
137 // CHECK: call noundef ptr %
138 // CHECK-NOT: call ptr @llvm.objc.retain
139 id obj4 = (x0.*pmf)();
140 // CHECK: call void @llvm.lifetime.start
141 // CHECK: call noundef ptr %
142 // CHECK-NOT: call ptr @llvm.objc.retain
143 id obj5 = (x0p->*pmf)();
145 // CHECK: call void @llvm.objc.release
146 // CHECK: call void @llvm.lifetime.end
147 // CHECK: call void @llvm.objc.release
148 // CHECK: call void @llvm.lifetime.end
149 // CHECK: call void @llvm.objc.release
150 // CHECK: call void @llvm.lifetime.end
151 // CHECK: call void @llvm.objc.release
152 // CHECK: call void @llvm.lifetime.end
153 // CHECK: call void @llvm.objc.release
154 // CHECK: call void @llvm.lifetime.end
155 // CHECK-NEXT: ret void
158 // CHECK-LABEL: define{{.*}} void @_Z7test35b13Test35_HelperPS_
159 void test35b(Test35_Helper x0, Test35_Helper *x0p) {
160 // CHECK: call void @llvm.lifetime.start
161 // CHECK: call noundef ptr @_ZN13Test35_Helper11makeObject3Ev{{.*}} [ "clang.arc.attachedcall"(ptr @llvm.objc.retainAutoreleasedReturnValue) ]
162 id obj1 = Test35_Helper::makeObject3();
163 // CHECK: call void @llvm.lifetime.start
164 // CHECK: call noundef ptr @_ZN13Test35_Helper11makeObject4Ev{{.*}} [ "clang.arc.attachedcall"(ptr @llvm.objc.retainAutoreleasedReturnValue) ]
165 id obj2 = x0.makeObject4();
166 // CHECK: call void @llvm.lifetime.start
167 // CHECK: call noundef ptr @_ZN13Test35_Helper11makeObject4Ev{{.*}} [ "clang.arc.attachedcall"(ptr @llvm.objc.retainAutoreleasedReturnValue) ]
168 id obj3 = x0p->makeObject4();
169 id (Test35_Helper::*pmf)() = &Test35_Helper::makeObject4;
170 // CHECK: call void @llvm.lifetime.start
171 // CHECK: call noundef ptr %{{.*}} [ "clang.arc.attachedcall"(ptr @llvm.objc.retainAutoreleasedReturnValue) ]
172 id obj4 = (x0.*pmf)();
173 // CHECK: call void @llvm.lifetime.start
174 // CHECK: call noundef ptr %{{.*}} [ "clang.arc.attachedcall"(ptr @llvm.objc.retainAutoreleasedReturnValue) ]
175 id obj5 = (x0p->*pmf)();
177 // CHECK: call void @llvm.objc.release
178 // CHECK: call void @llvm.lifetime.end
179 // CHECK: call void @llvm.objc.release
180 // CHECK: call void @llvm.lifetime.end
181 // CHECK: call void @llvm.lifetime.end
182 // CHECK: call void @llvm.objc.release
183 // CHECK: call void @llvm.lifetime.end
184 // CHECK: call void @llvm.objc.release
185 // CHECK: call void @llvm.lifetime.end
186 // CHECK: call void @llvm.objc.release
187 // CHECK: call void @llvm.lifetime.end
188 // CHECK-NEXT: ret void
191 // CHECK-LABEL: define{{.*}} ptr @_Z6test36P11objc_object(
193 // CHECK: llvm.objc.retain
194 // CHECK: llvm.objc.retain
195 // CHECK: llvm.objc.release
196 // CHECK: llvm.objc.autoreleaseReturnValue
205 template <class T> void test37(T *a) {
206 for (id x in a.array) {
210 extern template void test37<Test37>(Test37 *a);
211 template void test37<Test37>(Test37 *a);
212 // CHECK-LABEL: define weak_odr void @_Z6test37I6Test37EvPT_(
213 // CHECK: [[T2:%.*]] = call noundef ptr @objc_msgSend({{.*}} [ "clang.arc.attachedcall"(ptr @llvm.objc.retainAutoreleasedReturnValue) ]
214 // CHECK-NEXT: call void (...) @llvm.objc.clang.arc.noop.use({{.*}} [[T2]])
216 // Make sure it's not immediately released before starting the iteration.
217 // CHECK-NEXT: load ptr, ptr @OBJC_SELECTOR_REFERENCES_
218 // CHECK-NEXT: @objc_msgSend
220 // This bitcast is for the mutation check.
221 // CHECK: @objc_enumerationMutation
223 // This bitcast is for the 'next' message send.
224 // CHECK: load ptr, ptr @OBJC_SELECTOR_REFERENCES_
225 // CHECK-NEXT: @objc_msgSend
227 // This bitcast is for the final release.
228 // CHECK: call void @llvm.objc.release(ptr [[T2]])
231 void send_release() {
235 // CHECK-LABEL: define weak_odr void @_Z12send_releaseIiEvv(
236 // CHECK: call noundef ptr @objc_msgSend{{.*}} [ "clang.arc.attachedcall"(ptr @llvm.objc.retainAutoreleasedReturnValue) ]
237 // CHECK-NEXT: call void (...) @llvm.objc.clang.arc.noop.use(
238 // CHECK-NEXT: call void @llvm.objc.release
239 // CHECK-NEXT: ret void
240 template void send_release<int>();
243 Test37 *instantiate_init() {
244 Test37 *result = [[Test37 alloc] init];
248 // CHECK-LABEL: define weak_odr noundef ptr @_Z16instantiate_initIiEP6Test37v
249 // CHECK: call noundef ptr @objc_msgSend
250 // CHECK: call noundef ptr @objc_msgSend
251 // CHECK: call ptr @llvm.objc.retain
252 // CHECK: call void @llvm.objc.release
253 // CHECK: call ptr @llvm.objc.autoreleaseReturnValue
254 template Test37* instantiate_init<int>();
256 // Just make sure that the AST invariants hold properly here,
257 // i.e. that we don't crash.
258 // The block should get bound in the full-expression outside
259 // the statement-expression.
260 template <class T> class Test38 {
262 ^{ (void) x; }, ({ x; });
265 // CHECK-LABEL: define weak_odr void @_ZN6Test38IiE4testEi(
266 template class Test38<int>;
274 class Test39 : Test39_base1, Test39_base2 { // base2 is at non-zero offset
277 id Test39::bar() { return 0; }
278 // Note lack of autorelease.
279 // CHECK-LABEL: define{{.*}} ptr @_ZThn8_N6Test393barEv(
280 // CHECK: call noundef ptr @_ZN6Test393barEv(
281 // CHECK-NEXT: ret ptr
283 // Just a basic correctness check that IR-gen still works after instantiating
284 // a non-dependent message send that requires writeback.
286 + (void) foo:(id *)errorPtr;
288 template <class T> void test40_helper() {
292 template void test40_helper<int>();
293 // CHECK-LABEL: define weak_odr void @_Z13test40_helperIiEvv()
294 // CHECK: [[X:%.*]] = alloca ptr
295 // CHECK-NEXT: [[TEMP:%.*]] = alloca ptr
296 // CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 8, ptr [[X]])
297 // CHECK-NEXT: store ptr null, ptr [[X]]
298 // CHECK: [[T0:%.*]] = load ptr, ptr [[X]]
299 // CHECK-NEXT: store ptr [[T0]], ptr [[TEMP]]
300 // CHECK: @objc_msgSend
301 // CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[TEMP]]
302 // CHECK-NEXT: call ptr @llvm.objc.retain(ptr [[T0]])
304 // Check that moves out of __weak variables are compiled to use objc_moveWeak.
305 void test41(__weak id &&x) {
306 __weak id y = static_cast<__weak id &&>(x);
308 // CHECK-LABEL: define{{.*}} void @_Z6test41OU6__weakP11objc_object
309 // CHECK: [[X:%.*]] = alloca ptr
310 // CHECK: [[Y:%.*]] = alloca ptr
311 // CHECK: [[T0:%.*]] = load ptr, ptr [[X]]
312 // CHECK-NEXT: call void @llvm.objc.moveWeak(ptr [[Y]], ptr [[T0]])
313 // CHECK-NEXT: call void @llvm.objc.destroyWeak(ptr [[Y]])
316 __attribute__((ns_returns_retained)) id test42_0();
318 void test42_2(id &&);
319 test42_2(test42_1(test42_0()));
322 // Check that the pointer returned by test42_0 is released after the full expression.
324 // CHECK-LABEL: define void @_Z6test42v()
325 // CHECK: %[[CALL:.*]] = call noundef ptr @_Z8test42_0v()
326 // CHECK: call void @_Z8test42_2OU15__autoreleasingP11objc_object(
327 // CHECK: call void @llvm.objc.release(ptr %[[CALL]])