1// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -fobjc-runtime-has-weak -fblocks -fobjc-arc -O2 -disable-llvm-optzns -o - %s | FileCheck %s
2
3struct NSFastEnumerationState;
4@interface NSArray
5- (unsigned long) countByEnumeratingWithState: (struct NSFastEnumerationState*) state
6                  objects: (id*) buffer
7                  count: (unsigned long) bufferSize;
8@end;
9NSArray *nsarray() { return 0; }
10// CHECK: define [[NSARRAY:%.*]]* @_Z7nsarrayv()
11
12void use(id);
13
14// rdar://problem/9315552
15// The analogous ObjC testcase test46 in arr.m.
16void test0(__weak id *wp, __weak volatile id *wvp) {
17  extern id test0_helper(void);
18
19  // TODO: this is sub-optimal, we should retain at the actual call site.
20  // TODO: in the non-volatile case, we do not need to be reloading.
21
22  // CHECK:      [[T0:%.*]] = call i8* @_Z12test0_helperv()
23  // CHECK-NEXT: [[T1:%.*]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[T0]])
24  // CHECK-NEXT: [[T2:%.*]] = load i8**, i8*** {{%.*}}, align 8
25  // CHECK-NEXT: [[T3:%.*]] = call i8* @objc_storeWeak(i8** [[T2]], i8* [[T1]])
26  // CHECK-NEXT: [[T4:%.*]] = call i8* @objc_retain(i8* [[T3]])
27  // CHECK-NEXT: store i8* [[T4]], i8**
28  // CHECK-NEXT: call void @objc_release(i8* [[T1]])
29  id x = *wp = test0_helper();
30
31  // CHECK:      [[T0:%.*]] = call i8* @_Z12test0_helperv()
32  // CHECK-NEXT: [[T1:%.*]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[T0]])
33  // CHECK-NEXT: [[T2:%.*]] = load i8**, i8*** {{%.*}}, align 8
34  // CHECK-NEXT: [[T3:%.*]] = call i8* @objc_storeWeak(i8** [[T2]], i8* [[T1]])
35  // CHECK-NEXT: [[T4:%.*]] = call i8* @objc_loadWeakRetained(i8** [[T2]])
36  // CHECK-NEXT: store i8* [[T4]], i8**
37  // CHECK-NEXT: call void @objc_release(i8* [[T1]])
38  id y = *wvp = test0_helper();
39}
40
41// rdar://problem/9320648
42struct Test1_helper { Test1_helper(); };
43@interface Test1 @end
44@implementation Test1 { Test1_helper x; } @end
45// CHECK: define internal i8* @"\01-[Test1 .cxx_construct]"(
46// CHECK:      call void @_ZN12Test1_helperC1Ev(
47// CHECK-NEXT: load
48// CHECK-NEXT: bitcast
49// CHECK-NEXT: ret i8*
50
51void test34(int cond) {
52  __strong id strong;
53  __weak id weak;
54  extern void test34_sink(id *);
55  test34_sink(cond ? &strong : 0);
56  test34_sink(cond ? &weak : 0);
57
58  // CHECK-LABEL:    define void @_Z6test34i(
59  // CHECK:      [[COND:%.*]] = alloca i32
60  // CHECK-NEXT: [[STRONG:%.*]] = alloca i8*
61  // CHECK-NEXT: [[WEAK:%.*]] = alloca i8*
62  // CHECK-NEXT: [[TEMP1:%.*]] = alloca i8*
63  // CHECK-NEXT: [[TEMP2:%.*]] = alloca i8*
64  // CHECK-NEXT: [[CONDCLEANUPSAVE:%.*]] = alloca i8*
65  // CHECK-NEXT: [[CONDCLEANUP:%.*]] = alloca i1
66  // CHECK-NEXT: store i32
67  // CHECK-NEXT: [[STRONGP:%.*]] = bitcast i8** [[STRONG]] to i8*
68  // CHECK-NEXT: call void @llvm.lifetime.start(i64 8, i8* [[STRONGP]])
69  // CHECK-NEXT: store i8* null, i8** [[STRONG]]
70  // CHECK-NEXT: [[WEAKP:%.*]] = bitcast i8** [[WEAK]] to i8*
71  // CHECK-NEXT: call void @llvm.lifetime.start(i64 8, i8* [[WEAKP]])
72  // CHECK-NEXT: call i8* @objc_initWeak(i8** [[WEAK]], i8* null)
73
74  // CHECK-NEXT: [[T0:%.*]] = load i32, i32* [[COND]]
75  // CHECK-NEXT: [[T1:%.*]] = icmp ne i32 [[T0]], 0
76  // CHECK:      [[ARG:%.*]] = phi i8**
77  // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null
78  // CHECK-NEXT: [[T1:%.*]] = select i1 [[T0]], i8** null, i8** [[TEMP1]]
79  // CHECK-NEXT: br i1 [[T0]],
80  // CHECK:      [[T0:%.*]] = load i8*, i8** [[ARG]]
81  // CHECK-NEXT: store i8* [[T0]], i8** [[TEMP1]]
82  // CHECK-NEXT: br label
83  // CHECK:      [[W0:%.*]] = phi i8* [ [[T0]], {{%.*}} ], [ undef, {{%.*}} ]
84  // CHECK:      call void @_Z11test34_sinkPU15__autoreleasingP11objc_object(i8** [[T1]])
85  // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null
86  // CHECK-NEXT: br i1 [[T0]],
87  // CHECK:      [[T0:%.*]] = load i8*, i8** [[TEMP1]]
88  // CHECK-NEXT: [[T1:%.*]] = call i8* @objc_retain(i8* [[T0]])
89  // CHECK-NEXT: call void (...) @clang.arc.use(i8* [[W0]])
90  // CHECK-NEXT: [[T2:%.*]] = load i8*, i8** [[ARG]]
91  // CHECK-NEXT: store i8* [[T1]], i8** [[ARG]]
92  // CHECK-NEXT: call void @objc_release(i8* [[T2]])
93  // CHECK-NEXT: br label
94
95  // CHECK:      [[T0:%.*]] = load i32, i32* [[COND]]
96  // CHECK-NEXT: [[T1:%.*]] = icmp ne i32 [[T0]], 0
97  // CHECK:      [[ARG:%.*]] = phi i8**
98  // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null
99  // CHECK-NEXT: [[T1:%.*]] = select i1 [[T0]], i8** null, i8** [[TEMP2]]
100  // CHECK-NEXT: store i1 false, i1* [[CONDCLEANUP]]
101  // CHECK-NEXT: br i1 [[T0]],
102  // CHECK:      [[T0:%.*]] = call i8* @objc_loadWeakRetained(i8** [[ARG]])
103  // CHECK-NEXT: store i8* [[T0]], i8** [[CONDCLEANUPSAVE]]
104  // CHECK-NEXT: store i1 true, i1* [[CONDCLEANUP]]
105  // CHECK-NEXT: store i8* [[T0]], i8** [[TEMP2]]
106  // CHECK-NEXT: br label
107  // CHECK:      call void @_Z11test34_sinkPU15__autoreleasingP11objc_object(i8** [[T1]])
108  // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null
109  // CHECK-NEXT: br i1 [[T0]],
110  // CHECK:      [[T0:%.*]] = load i8*, i8** [[TEMP2]]
111  // CHECK-NEXT: call i8* @objc_storeWeak(i8** [[ARG]], i8* [[T0]])
112  // CHECK-NEXT: br label
113
114  // CHECK:      call void @objc_destroyWeak(i8** [[WEAK]])
115  // CHECK:      ret void
116}
117
118struct Test35_Helper {
119  static id makeObject1() __attribute__((ns_returns_retained));
120  id makeObject2() __attribute__((ns_returns_retained));
121  static id makeObject3();
122  id makeObject4();
123};
124
125// CHECK-LABEL: define void @_Z6test3513Test35_HelperPS_
126void test35(Test35_Helper x0, Test35_Helper *x0p) {
127  // CHECK: call void @llvm.lifetime.start
128  // CHECK: call i8* @_ZN13Test35_Helper11makeObject1Ev
129  // CHECK-NOT: call i8* @objc_retain
130  id obj1 = Test35_Helper::makeObject1();
131  // CHECK: call void @llvm.lifetime.start
132  // CHECK: call i8* @_ZN13Test35_Helper11makeObject2Ev
133  // CHECK-NOT: call i8* @objc_retain
134  id obj2 = x0.makeObject2();
135  // CHECK: call void @llvm.lifetime.start
136  // CHECK: call i8* @_ZN13Test35_Helper11makeObject2Ev
137  // CHECK-NOT: call i8* @objc_retain
138  id obj3 = x0p->makeObject2();
139  id (Test35_Helper::*pmf)() __attribute__((ns_returns_retained))
140    = &Test35_Helper::makeObject2;
141  // CHECK: call void @llvm.lifetime.start
142  // CHECK: call i8* %
143  // CHECK-NOT: call i8* @objc_retain
144  id obj4 = (x0.*pmf)();
145  // CHECK: call void @llvm.lifetime.start
146  // CHECK: call i8* %
147  // CHECK-NOT: call i8* @objc_retain
148  id obj5 = (x0p->*pmf)();
149
150  // CHECK: call void @objc_release
151  // CHECK: call void @llvm.lifetime.end
152  // CHECK: call void @objc_release
153  // CHECK: call void @llvm.lifetime.end
154  // CHECK: call void @objc_release
155  // CHECK: call void @llvm.lifetime.end
156  // CHECK: call void @objc_release
157  // CHECK: call void @llvm.lifetime.end
158  // CHECK: call void @objc_release
159  // CHECK: call void @llvm.lifetime.end
160  // CHECK-NEXT: ret void
161}
162
163// CHECK-LABEL: define void @_Z7test35b13Test35_HelperPS_
164void test35b(Test35_Helper x0, Test35_Helper *x0p) {
165  // CHECK: call void @llvm.lifetime.start
166  // CHECK: call i8* @_ZN13Test35_Helper11makeObject3Ev
167  // CHECK: call i8* @objc_retain
168  id obj1 = Test35_Helper::makeObject3();
169  // CHECK: call void @llvm.lifetime.start
170  // CHECK: call i8* @_ZN13Test35_Helper11makeObject4Ev
171  // CHECK: call i8* @objc_retain
172  id obj2 = x0.makeObject4();
173  // CHECK: call void @llvm.lifetime.start
174  // CHECK: call i8* @_ZN13Test35_Helper11makeObject4Ev
175  // CHECK: call i8* @objc_retain
176  id obj3 = x0p->makeObject4();
177  id (Test35_Helper::*pmf)() = &Test35_Helper::makeObject4;
178  // CHECK: call void @llvm.lifetime.start
179  // CHECK: call i8* %
180  // CHECK: call i8* @objc_retain
181  id obj4 = (x0.*pmf)();
182  // CHECK: call void @llvm.lifetime.start
183  // CHECK: call i8* %
184  // CHECK: call i8* @objc_retain
185  id obj5 = (x0p->*pmf)();
186
187  // CHECK: call void @objc_release
188  // CHECK: call void @llvm.lifetime.end
189  // CHECK: call void @objc_release
190  // CHECK: call void @llvm.lifetime.end
191  // CHECK: call void @llvm.lifetime.end
192  // CHECK: call void @objc_release
193  // CHECK: call void @llvm.lifetime.end
194  // CHECK: call void @objc_release
195  // CHECK: call void @llvm.lifetime.end
196  // CHECK: call void @objc_release
197  // CHECK: call void @llvm.lifetime.end
198  // CHECK-NEXT: ret void
199}
200
201// rdar://problem/9603128
202// CHECK-LABEL: define i8* @_Z6test36P11objc_object(
203id test36(id z) {
204  // CHECK: objc_retain
205  // CHECK: objc_retain
206  // CHECK: objc_release
207  // CHECK: objc_autoreleaseReturnValue
208  return z;
209}
210
211// Template instantiation side of rdar://problem/9817306
212@interface Test37
213+ alloc;
214- init;
215- (NSArray *) array;
216@end
217template <class T> void test37(T *a) {
218  for (id x in a.array) {
219    use(x);
220  }
221}
222extern template void test37<Test37>(Test37 *a);
223template void test37<Test37>(Test37 *a);
224// CHECK-LABEL: define weak_odr void @_Z6test37I6Test37EvPT_(
225// CHECK:      [[T0:%.*]] = call [[NSARRAY]]* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to [[NSARRAY]]* (i8*, i8*)*)(
226// CHECK-NEXT: [[T1:%.*]] = bitcast [[NSARRAY]]* [[T0]] to i8*
227// CHECK-NEXT: [[T2:%.*]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[T1]])
228// CHECK-NEXT: [[COLL:%.*]] = bitcast i8* [[T2]] to [[NSARRAY]]*
229
230// Make sure it's not immediately released before starting the iteration.
231// CHECK-NEXT: load i8*, i8** @OBJC_SELECTOR_REFERENCES_
232// CHECK-NEXT: [[T0:%.*]] = bitcast [[NSARRAY]]* [[COLL]] to i8*
233// CHECK-NEXT: @objc_msgSend
234
235// This bitcast is for the mutation check.
236// CHECK:      [[T0:%.*]] = bitcast [[NSARRAY]]* [[COLL]] to i8*
237// CHECK-NEXT: @objc_enumerationMutation
238
239// This bitcast is for the 'next' message send.
240// CHECK:      [[T0:%.*]] = bitcast [[NSARRAY]]* [[COLL]] to i8*
241// CHECK-NEXT: @objc_msgSend
242
243// This bitcast is for the final release.
244// CHECK:      [[T0:%.*]] = bitcast [[NSARRAY]]* [[COLL]] to i8*
245// CHECK-NEXT: call void @objc_release(i8* [[T0]])
246
247template<typename T>
248void send_release() {
249  [Test37 array];
250}
251
252// CHECK-LABEL: define weak_odr void @_Z12send_releaseIiEvv(
253// CHECK: call %0* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend
254// CHECK-NEXT: bitcast
255// CHECK-NEXT: call i8* @objc_retainAutoreleasedReturnValue
256// CHECK-NEXT: bitcast
257// CHECK-NEXT: bitcast
258// CHECK-NEXT: call void @objc_release
259// CHECK-NEXT: ret void
260template void send_release<int>();
261
262template<typename T>
263Test37 *instantiate_init() {
264  Test37 *result = [[Test37 alloc] init];
265  return result;
266}
267
268// CHECK-LABEL: define weak_odr %2* @_Z16instantiate_initIiEP6Test37v
269// CHECK: call i8* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend
270// CHECK: call i8* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend
271// CHECK: call i8* @objc_retain
272// CHECK: call void @objc_release
273// CHECK: call i8* @objc_autoreleaseReturnValue
274template Test37* instantiate_init<int>();
275
276// Just make sure that the AST invariants hold properly here,
277// i.e. that we don't crash.
278// The block should get bound in the full-expression outside
279// the statement-expression.
280template <class T> class Test38 {
281  void test(T x) {
282    ^{ (void) x; }, ({ x; });
283  }
284};
285// CHECK-LABEL: define weak_odr void @_ZN6Test38IiE4testEi(
286template class Test38<int>;
287
288// rdar://problem/11964832
289class Test39_base1 {
290  virtual void foo();
291};
292class Test39_base2 {
293  virtual id bar();
294};
295class Test39 : Test39_base1, Test39_base2 { // base2 is at non-zero offset
296  virtual id bar();
297};
298id Test39::bar() { return 0; }
299// Note lack of autorelease.
300// CHECK-LABEL:    define i8* @_ZThn8_N6Test393barEv(
301// CHECK:      call i8* @_ZN6Test393barEv(
302// CHECK-NEXT: ret i8*
303
304// rdar://13617051
305// Just a basic sanity-check that IR-gen still works after instantiating
306// a non-dependent message send that requires writeback.
307@interface Test40
308+ (void) foo:(id *)errorPtr;
309@end
310template <class T> void test40_helper() {
311  id x;
312  [Test40 foo: &x];
313};
314template void test40_helper<int>();
315// CHECK-LABEL:    define weak_odr void @_Z13test40_helperIiEvv()
316// CHECK:      [[X:%.*]] = alloca i8*
317// CHECK-NEXT: [[TEMP:%.*]] = alloca i8*
318// CHECK-NEXT: [[XP:%.*]] = bitcast i8** [[X]] to i8*
319// CHECK-NEXT: call void @llvm.lifetime.start(i64 8, i8* [[XP]])
320// CHECK-NEXT: store i8* null, i8** [[X]]
321// CHECK:      [[T0:%.*]] = load i8*, i8** [[X]]
322// CHECK-NEXT: store i8* [[T0]], i8** [[TEMP]]
323// CHECK:      @objc_msgSend
324// CHECK-NEXT: [[T0:%.*]] = load i8*, i8** [[TEMP]]
325// CHECK-NEXT: call i8* @objc_retain(i8* [[T0]])
326
327// Check that moves out of __weak variables are compiled to use objc_moveWeak.
328void test41(__weak id &&x) {
329  __weak id y = static_cast<__weak id &&>(x);
330}
331// CHECK-LABEL: define void @_Z6test41OU6__weakP11objc_object
332// CHECK:      [[X:%.*]] = alloca i8**
333// CHECK:      [[Y:%.*]] = alloca i8*
334// CHECK:      [[T0:%.*]] = load i8**, i8*** [[X]]
335// CHECK-NEXT: call void @objc_moveWeak(i8** [[Y]], i8** [[T0]])
336// CHECK-NEXT: call void @objc_destroyWeak(i8** [[Y]])
337