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*** {{%.*}}, 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*** {{%.*}}, 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: 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: store i32 65 // CHECK-NEXT: store i8* null, i8** [[STRONG]] 66 // CHECK-NEXT: call i8* @objc_initWeak(i8** [[WEAK]], i8* null) 67 68 // CHECK-NEXT: [[T0:%.*]] = load i32* [[COND]] 69 // CHECK-NEXT: [[T1:%.*]] = icmp ne i32 [[T0]], 0 70 // CHECK: [[ARG:%.*]] = phi i8** 71 // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null 72 // CHECK-NEXT: [[T1:%.*]] = select i1 [[T0]], i8** null, i8** [[TEMP1]] 73 // CHECK-NEXT: br i1 [[T0]], 74 // CHECK: [[T0:%.*]] = load i8** [[ARG]] 75 // CHECK-NEXT: store i8* [[T0]], i8** [[TEMP1]] 76 // CHECK-NEXT: br label 77 // CHECK: call void @_Z11test34_sinkPU15__autoreleasingP11objc_object(i8** [[T1]]) 78 // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null 79 // CHECK-NEXT: br i1 [[T0]], 80 // CHECK: [[T0:%.*]] = load i8** [[TEMP1]] 81 // CHECK-NEXT: [[T1:%.*]] = call i8* @objc_retain(i8* [[T0]]) 82 // CHECK-NEXT: [[T2:%.*]] = load i8** [[ARG]] 83 // CHECK-NEXT: store i8* [[T1]], i8** [[ARG]] 84 // CHECK-NEXT: call void @objc_release(i8* [[T2]]) 85 // CHECK-NEXT: br label 86 87 // CHECK: [[T0:%.*]] = load i32* [[COND]] 88 // CHECK-NEXT: [[T1:%.*]] = icmp ne i32 [[T0]], 0 89 // CHECK: [[ARG:%.*]] = phi i8** 90 // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null 91 // CHECK-NEXT: [[T1:%.*]] = select i1 [[T0]], i8** null, i8** [[TEMP2]] 92 // CHECK-NEXT: br i1 [[T0]], 93 // CHECK: [[T0:%.*]] = call i8* @objc_loadWeak(i8** [[ARG]]) 94 // CHECK-NEXT: store i8* [[T0]], i8** [[TEMP2]] 95 // CHECK-NEXT: br label 96 // CHECK: call void @_Z11test34_sinkPU15__autoreleasingP11objc_object(i8** [[T1]]) 97 // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null 98 // CHECK-NEXT: br i1 [[T0]], 99 // CHECK: [[T0:%.*]] = load i8** [[TEMP2]] 100 // CHECK-NEXT: call i8* @objc_storeWeak(i8** [[ARG]], i8* [[T0]]) 101 // CHECK-NEXT: br label 102 103 // CHECK: call void @objc_destroyWeak(i8** [[WEAK]]) 104 // CHECK: ret void 105} 106 107struct Test35_Helper { 108 static id makeObject1() __attribute__((ns_returns_retained)); 109 id makeObject2() __attribute__((ns_returns_retained)); 110 static id makeObject3(); 111 id makeObject4(); 112}; 113 114// CHECK: define void @_Z6test3513Test35_HelperPS_ 115void test35(Test35_Helper x0, Test35_Helper *x0p) { 116 // CHECK: call i8* @_ZN13Test35_Helper11makeObject1Ev 117 // CHECK-NOT: call i8* @objc_retain 118 id obj1 = Test35_Helper::makeObject1(); 119 // CHECK: call i8* @_ZN13Test35_Helper11makeObject2Ev 120 // CHECK-NOT: call i8* @objc_retain 121 id obj2 = x0.makeObject2(); 122 // CHECK: call i8* @_ZN13Test35_Helper11makeObject2Ev 123 // CHECK-NOT: call i8* @objc_retain 124 id obj3 = x0p->makeObject2(); 125 id (Test35_Helper::*pmf)() __attribute__((ns_returns_retained)) 126 = &Test35_Helper::makeObject2; 127 // CHECK: call i8* % 128 // CHECK-NOT: call i8* @objc_retain 129 id obj4 = (x0.*pmf)(); 130 // CHECK: call i8* % 131 // CHECK-NOT: call i8* @objc_retain 132 id obj5 = (x0p->*pmf)(); 133 134 // CHECK: call void @objc_release 135 // CHECK: call void @objc_release 136 // CHECK: call void @objc_release 137 // CHECK: call void @objc_release 138 // CHECK: call void @objc_release 139 // CHECK-NEXT: ret void 140} 141 142// CHECK: define void @_Z7test35b13Test35_HelperPS_ 143void test35b(Test35_Helper x0, Test35_Helper *x0p) { 144 // CHECK: call i8* @_ZN13Test35_Helper11makeObject3Ev 145 // CHECK: call i8* @objc_retain 146 id obj1 = Test35_Helper::makeObject3(); 147 // CHECK: call i8* @_ZN13Test35_Helper11makeObject4Ev 148 // CHECK: call i8* @objc_retain 149 id obj2 = x0.makeObject4(); 150 // CHECK: call i8* @_ZN13Test35_Helper11makeObject4Ev 151 // CHECK: call i8* @objc_retain 152 id obj3 = x0p->makeObject4(); 153 id (Test35_Helper::*pmf)() = &Test35_Helper::makeObject4; 154 // CHECK: call i8* % 155 // CHECK: call i8* @objc_retain 156 id obj4 = (x0.*pmf)(); 157 // CHECK: call i8* % 158 // CHECK: call i8* @objc_retain 159 id obj5 = (x0p->*pmf)(); 160 161 // CHECK: call void @objc_release 162 // CHECK: call void @objc_release 163 // CHECK: call void @objc_release 164 // CHECK: call void @objc_release 165 // CHECK: call void @objc_release 166 // CHECK-NEXT: ret void 167} 168 169// rdar://problem/9603128 170// CHECK: define i8* @_Z6test36P11objc_object( 171id test36(id z) { 172 // CHECK: objc_retain 173 // CHECK: objc_retain 174 // CHECK: objc_release 175 // CHECK: objc_autoreleaseReturnValue 176 return z; 177} 178 179// Template instantiation side of rdar://problem/9817306 180@interface Test37 181+ alloc; 182- init; 183- (NSArray *) array; 184@end 185template <class T> void test37(T *a) { 186 for (id x in a.array) { 187 use(x); 188 } 189} 190extern template void test37<Test37>(Test37 *a); 191template void test37<Test37>(Test37 *a); 192// CHECK: define weak_odr void @_Z6test37I6Test37EvPT_( 193// CHECK: [[T0:%.*]] = call [[NSARRAY]]* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to [[NSARRAY]]* (i8*, i8*)*)( 194// CHECK-NEXT: [[T1:%.*]] = bitcast [[NSARRAY]]* [[T0]] to i8* 195// CHECK-NEXT: [[T2:%.*]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[T1]]) 196// CHECK-NEXT: [[COLL:%.*]] = bitcast i8* [[T2]] to [[NSARRAY]]* 197 198// Make sure it's not immediately released before starting the iteration. 199// CHECK-NEXT: load i8** @"\01L_OBJC_SELECTOR_REFERENCES_ 200// CHECK-NEXT: [[T0:%.*]] = bitcast [[NSARRAY]]* [[COLL]] to i8* 201// CHECK-NEXT: @objc_msgSend 202 203// This bitcast is for the mutation check. 204// CHECK: [[T0:%.*]] = bitcast [[NSARRAY]]* [[COLL]] to i8* 205// CHECK-NEXT: @objc_enumerationMutation 206 207// This bitcast is for the 'next' message send. 208// CHECK: [[T0:%.*]] = bitcast [[NSARRAY]]* [[COLL]] to i8* 209// CHECK-NEXT: @objc_msgSend 210 211// This bitcast is for the final release. 212// CHECK: [[T0:%.*]] = bitcast [[NSARRAY]]* [[COLL]] to i8* 213// CHECK-NEXT: call void @objc_release(i8* [[T0]]) 214 215template<typename T> 216void send_release() { 217 [Test37 array]; 218} 219 220// CHECK: define weak_odr void @_Z12send_releaseIiEvv( 221// CHECK: call %0* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend 222// CHECK-NEXT: bitcast 223// CHECK-NEXT: call i8* @objc_retainAutoreleasedReturnValue 224// CHECK-NEXT: bitcast 225// CHECK-NEXT: bitcast 226// CHECK-NEXT: call void @objc_release 227// CHECK-NEXT: ret void 228template void send_release<int>(); 229 230template<typename T> 231Test37 *instantiate_init() { 232 Test37 *result = [[Test37 alloc] init]; 233 return result; 234} 235 236// CHECK: define weak_odr %2* @_Z16instantiate_initIiEP6Test37v 237// CHECK: call i8* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend 238// CHECK: call i8* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend 239// CHECK: call i8* @objc_retain 240// CHECK: call void @objc_release 241// CHECK: call i8* @objc_autoreleaseReturnValue 242template Test37* instantiate_init<int>(); 243 244// Just make sure that the AST invariants hold properly here, 245// i.e. that we don't crash. 246// The block should get bound in the full-expression outside 247// the statement-expression. 248template <class T> class Test38 { 249 void test(T x) { 250 ^{ (void) x; }, ({ x; }); 251 } 252}; 253// CHECK: define weak_odr void @_ZN6Test38IiE4testEi( 254template class Test38<int>; 255 256// rdar://problem/11964832 257class Test39_base1 { 258 virtual void foo(); 259}; 260class Test39_base2 { 261 virtual id bar(); 262}; 263class Test39 : Test39_base1, Test39_base2 { // base2 is at non-zero offset 264 virtual id bar(); 265}; 266id Test39::bar() { return 0; } 267// Note lack of autorelease. 268// CHECK: define i8* @_ZThn8_N6Test393barEv( 269// CHECK: call i8* @_ZN6Test393barEv( 270// CHECK-NEXT: ret i8* 271