John McCall | d1e40d5 | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 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 |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2 | |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 3 | struct NSFastEnumerationState; |
| 4 | @interface NSArray |
| 5 | - (unsigned long) countByEnumeratingWithState: (struct NSFastEnumerationState*) state |
| 6 | objects: (id*) buffer |
| 7 | count: (unsigned long) bufferSize; |
| 8 | @end; |
| 9 | NSArray *nsarray() { return 0; } |
| 10 | // CHECK: define [[NSARRAY:%.*]]* @_Z7nsarrayv() |
| 11 | |
| 12 | void use(id); |
| 13 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 14 | // rdar://problem/9315552 |
| 15 | // The analogous ObjC testcase test46 in arr.m. |
| 16 | void 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() |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 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]]) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 29 | id x = *wp = test0_helper(); |
| 30 | |
| 31 | // CHECK: [[T0:%.*]] = call i8* @_Z12test0_helperv() |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 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]]) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 38 | id y = *wvp = test0_helper(); |
| 39 | } |
| 40 | |
| 41 | // rdar://problem/9320648 |
| 42 | struct 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 | |
| 51 | void 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* |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 64 | // CHECK-NEXT: [[CONDCLEANUPSAVE:%.*]] = alloca i8* |
| 65 | // CHECK-NEXT: [[CONDCLEANUP:%.*]] = alloca i1 |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 66 | // CHECK-NEXT: store i32 |
| 67 | // CHECK-NEXT: store i8* null, i8** [[STRONG]] |
| 68 | // CHECK-NEXT: call i8* @objc_initWeak(i8** [[WEAK]], i8* null) |
| 69 | |
| 70 | // CHECK-NEXT: [[T0:%.*]] = load i32* [[COND]] |
| 71 | // CHECK-NEXT: [[T1:%.*]] = icmp ne i32 [[T0]], 0 |
| 72 | // CHECK: [[ARG:%.*]] = phi i8** |
| 73 | // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null |
| 74 | // CHECK-NEXT: [[T1:%.*]] = select i1 [[T0]], i8** null, i8** [[TEMP1]] |
| 75 | // CHECK-NEXT: br i1 [[T0]], |
| 76 | // CHECK: [[T0:%.*]] = load i8** [[ARG]] |
| 77 | // CHECK-NEXT: store i8* [[T0]], i8** [[TEMP1]] |
| 78 | // CHECK-NEXT: br label |
John McCall | b6a6079 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 79 | // CHECK: [[W0:%.*]] = phi i8* [ [[T0]], {{%.*}} ], [ undef, {{%.*}} ] |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 80 | // CHECK: call void @_Z11test34_sinkPU15__autoreleasingP11objc_object(i8** [[T1]]) |
| 81 | // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null |
| 82 | // CHECK-NEXT: br i1 [[T0]], |
| 83 | // CHECK: [[T0:%.*]] = load i8** [[TEMP1]] |
| 84 | // CHECK-NEXT: [[T1:%.*]] = call i8* @objc_retain(i8* [[T0]]) |
John McCall | b6a6079 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 85 | // CHECK-NEXT: call void (...)* @clang.arc.use(i8* [[W0]]) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 86 | // CHECK-NEXT: [[T2:%.*]] = load i8** [[ARG]] |
| 87 | // CHECK-NEXT: store i8* [[T1]], i8** [[ARG]] |
| 88 | // CHECK-NEXT: call void @objc_release(i8* [[T2]]) |
| 89 | // CHECK-NEXT: br label |
| 90 | |
| 91 | // CHECK: [[T0:%.*]] = load i32* [[COND]] |
| 92 | // CHECK-NEXT: [[T1:%.*]] = icmp ne i32 [[T0]], 0 |
| 93 | // CHECK: [[ARG:%.*]] = phi i8** |
| 94 | // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null |
| 95 | // CHECK-NEXT: [[T1:%.*]] = select i1 [[T0]], i8** null, i8** [[TEMP2]] |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 96 | // CHECK-NEXT: store i1 false, i1* [[CONDCLEANUP]] |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 97 | // CHECK-NEXT: br i1 [[T0]], |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 98 | // CHECK: [[T0:%.*]] = call i8* @objc_loadWeakRetained(i8** [[ARG]]) |
| 99 | // CHECK-NEXT: store i8* [[T0]], i8** [[CONDCLEANUPSAVE]] |
| 100 | // CHECK-NEXT: store i1 true, i1* [[CONDCLEANUP]] |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 101 | // CHECK-NEXT: store i8* [[T0]], i8** [[TEMP2]] |
| 102 | // CHECK-NEXT: br label |
| 103 | // CHECK: call void @_Z11test34_sinkPU15__autoreleasingP11objc_object(i8** [[T1]]) |
| 104 | // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null |
| 105 | // CHECK-NEXT: br i1 [[T0]], |
| 106 | // CHECK: [[T0:%.*]] = load i8** [[TEMP2]] |
| 107 | // CHECK-NEXT: call i8* @objc_storeWeak(i8** [[ARG]], i8* [[T0]]) |
| 108 | // CHECK-NEXT: br label |
| 109 | |
| 110 | // CHECK: call void @objc_destroyWeak(i8** [[WEAK]]) |
| 111 | // CHECK: ret void |
| 112 | } |
| 113 | |
| 114 | struct Test35_Helper { |
| 115 | static id makeObject1() __attribute__((ns_returns_retained)); |
| 116 | id makeObject2() __attribute__((ns_returns_retained)); |
| 117 | static id makeObject3(); |
| 118 | id makeObject4(); |
| 119 | }; |
| 120 | |
| 121 | // CHECK: define void @_Z6test3513Test35_HelperPS_ |
| 122 | void test35(Test35_Helper x0, Test35_Helper *x0p) { |
| 123 | // CHECK: call i8* @_ZN13Test35_Helper11makeObject1Ev |
| 124 | // CHECK-NOT: call i8* @objc_retain |
| 125 | id obj1 = Test35_Helper::makeObject1(); |
| 126 | // CHECK: call i8* @_ZN13Test35_Helper11makeObject2Ev |
| 127 | // CHECK-NOT: call i8* @objc_retain |
| 128 | id obj2 = x0.makeObject2(); |
| 129 | // CHECK: call i8* @_ZN13Test35_Helper11makeObject2Ev |
| 130 | // CHECK-NOT: call i8* @objc_retain |
| 131 | id obj3 = x0p->makeObject2(); |
| 132 | id (Test35_Helper::*pmf)() __attribute__((ns_returns_retained)) |
| 133 | = &Test35_Helper::makeObject2; |
| 134 | // CHECK: call i8* % |
| 135 | // CHECK-NOT: call i8* @objc_retain |
| 136 | id obj4 = (x0.*pmf)(); |
| 137 | // CHECK: call i8* % |
| 138 | // CHECK-NOT: call i8* @objc_retain |
| 139 | id obj5 = (x0p->*pmf)(); |
| 140 | |
| 141 | // CHECK: call void @objc_release |
| 142 | // CHECK: call void @objc_release |
| 143 | // CHECK: call void @objc_release |
| 144 | // CHECK: call void @objc_release |
| 145 | // CHECK: call void @objc_release |
| 146 | // CHECK-NEXT: ret void |
| 147 | } |
| 148 | |
| 149 | // CHECK: define void @_Z7test35b13Test35_HelperPS_ |
| 150 | void test35b(Test35_Helper x0, Test35_Helper *x0p) { |
| 151 | // CHECK: call i8* @_ZN13Test35_Helper11makeObject3Ev |
| 152 | // CHECK: call i8* @objc_retain |
| 153 | id obj1 = Test35_Helper::makeObject3(); |
| 154 | // CHECK: call i8* @_ZN13Test35_Helper11makeObject4Ev |
| 155 | // CHECK: call i8* @objc_retain |
| 156 | id obj2 = x0.makeObject4(); |
| 157 | // CHECK: call i8* @_ZN13Test35_Helper11makeObject4Ev |
| 158 | // CHECK: call i8* @objc_retain |
| 159 | id obj3 = x0p->makeObject4(); |
| 160 | id (Test35_Helper::*pmf)() = &Test35_Helper::makeObject4; |
| 161 | // CHECK: call i8* % |
| 162 | // CHECK: call i8* @objc_retain |
| 163 | id obj4 = (x0.*pmf)(); |
| 164 | // CHECK: call i8* % |
| 165 | // CHECK: call i8* @objc_retain |
| 166 | id obj5 = (x0p->*pmf)(); |
| 167 | |
| 168 | // CHECK: call void @objc_release |
| 169 | // CHECK: call void @objc_release |
| 170 | // CHECK: call void @objc_release |
| 171 | // CHECK: call void @objc_release |
| 172 | // CHECK: call void @objc_release |
| 173 | // CHECK-NEXT: ret void |
| 174 | } |
John McCall | 856d379 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 175 | |
| 176 | // rdar://problem/9603128 |
| 177 | // CHECK: define i8* @_Z6test36P11objc_object( |
| 178 | id test36(id z) { |
| 179 | // CHECK: objc_retain |
| 180 | // CHECK: objc_retain |
| 181 | // CHECK: objc_release |
| 182 | // CHECK: objc_autoreleaseReturnValue |
| 183 | return z; |
| 184 | } |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 185 | |
| 186 | // Template instantiation side of rdar://problem/9817306 |
| 187 | @interface Test37 |
Douglas Gregor | 9aab9c4 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 188 | + alloc; |
| 189 | - init; |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 190 | - (NSArray *) array; |
| 191 | @end |
| 192 | template <class T> void test37(T *a) { |
| 193 | for (id x in a.array) { |
| 194 | use(x); |
| 195 | } |
| 196 | } |
| 197 | extern template void test37<Test37>(Test37 *a); |
| 198 | template void test37<Test37>(Test37 *a); |
| 199 | // CHECK: define weak_odr void @_Z6test37I6Test37EvPT_( |
Douglas Gregor | d937c21 | 2011-12-10 00:28:18 +0000 | [diff] [blame] | 200 | // CHECK: [[T0:%.*]] = call [[NSARRAY]]* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to [[NSARRAY]]* (i8*, i8*)*)( |
| 201 | // CHECK-NEXT: [[T1:%.*]] = bitcast [[NSARRAY]]* [[T0]] to i8* |
| 202 | // CHECK-NEXT: [[T2:%.*]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[T1]]) |
| 203 | // CHECK-NEXT: [[COLL:%.*]] = bitcast i8* [[T2]] to [[NSARRAY]]* |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 204 | |
| 205 | // Make sure it's not immediately released before starting the iteration. |
Douglas Gregor | d937c21 | 2011-12-10 00:28:18 +0000 | [diff] [blame] | 206 | // CHECK-NEXT: load i8** @"\01L_OBJC_SELECTOR_REFERENCES_ |
| 207 | // CHECK-NEXT: [[T0:%.*]] = bitcast [[NSARRAY]]* [[COLL]] to i8* |
| 208 | // CHECK-NEXT: @objc_msgSend |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 209 | |
| 210 | // This bitcast is for the mutation check. |
Douglas Gregor | d937c21 | 2011-12-10 00:28:18 +0000 | [diff] [blame] | 211 | // CHECK: [[T0:%.*]] = bitcast [[NSARRAY]]* [[COLL]] to i8* |
| 212 | // CHECK-NEXT: @objc_enumerationMutation |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 213 | |
| 214 | // This bitcast is for the 'next' message send. |
Douglas Gregor | d937c21 | 2011-12-10 00:28:18 +0000 | [diff] [blame] | 215 | // CHECK: [[T0:%.*]] = bitcast [[NSARRAY]]* [[COLL]] to i8* |
| 216 | // CHECK-NEXT: @objc_msgSend |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 217 | |
| 218 | // This bitcast is for the final release. |
Douglas Gregor | d937c21 | 2011-12-10 00:28:18 +0000 | [diff] [blame] | 219 | // CHECK: [[T0:%.*]] = bitcast [[NSARRAY]]* [[COLL]] to i8* |
| 220 | // CHECK-NEXT: call void @objc_release(i8* [[T0]]) |
Douglas Gregor | 92be2a5 | 2011-12-10 00:23:21 +0000 | [diff] [blame] | 221 | |
| 222 | template<typename T> |
| 223 | void send_release() { |
| 224 | [Test37 array]; |
| 225 | } |
| 226 | |
| 227 | // CHECK: define weak_odr void @_Z12send_releaseIiEvv( |
| 228 | // CHECK: call %0* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend |
| 229 | // CHECK-NEXT: bitcast |
| 230 | // CHECK-NEXT: call i8* @objc_retainAutoreleasedReturnValue |
| 231 | // CHECK-NEXT: bitcast |
| 232 | // CHECK-NEXT: bitcast |
| 233 | // CHECK-NEXT: call void @objc_release |
| 234 | // CHECK-NEXT: ret void |
| 235 | template void send_release<int>(); |
Douglas Gregor | 9aab9c4 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 236 | |
| 237 | template<typename T> |
| 238 | Test37 *instantiate_init() { |
| 239 | Test37 *result = [[Test37 alloc] init]; |
| 240 | return result; |
| 241 | } |
| 242 | |
| 243 | // CHECK: define weak_odr %2* @_Z16instantiate_initIiEP6Test37v |
| 244 | // CHECK: call i8* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend |
| 245 | // CHECK: call i8* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend |
| 246 | // CHECK: call i8* @objc_retain |
| 247 | // CHECK: call void @objc_release |
| 248 | // CHECK: call i8* @objc_autoreleaseReturnValue |
| 249 | template Test37* instantiate_init<int>(); |
| 250 | |
John McCall | 7f39d51 | 2012-04-06 18:20:53 +0000 | [diff] [blame] | 251 | // Just make sure that the AST invariants hold properly here, |
| 252 | // i.e. that we don't crash. |
| 253 | // The block should get bound in the full-expression outside |
| 254 | // the statement-expression. |
| 255 | template <class T> class Test38 { |
| 256 | void test(T x) { |
| 257 | ^{ (void) x; }, ({ x; }); |
| 258 | } |
| 259 | }; |
| 260 | // CHECK: define weak_odr void @_ZN6Test38IiE4testEi( |
| 261 | template class Test38<int>; |
John McCall | bd9b65a | 2012-07-31 00:33:55 +0000 | [diff] [blame] | 262 | |
| 263 | // rdar://problem/11964832 |
| 264 | class Test39_base1 { |
| 265 | virtual void foo(); |
| 266 | }; |
| 267 | class Test39_base2 { |
| 268 | virtual id bar(); |
| 269 | }; |
| 270 | class Test39 : Test39_base1, Test39_base2 { // base2 is at non-zero offset |
| 271 | virtual id bar(); |
| 272 | }; |
| 273 | id Test39::bar() { return 0; } |
| 274 | // Note lack of autorelease. |
| 275 | // CHECK: define i8* @_ZThn8_N6Test393barEv( |
| 276 | // CHECK: call i8* @_ZN6Test393barEv( |
| 277 | // CHECK-NEXT: ret i8* |
John McCall | 93b6457 | 2013-04-11 02:14:26 +0000 | [diff] [blame] | 278 | |
| 279 | // rdar://13617051 |
| 280 | // Just a basic sanity-check that IR-gen still works after instantiating |
| 281 | // a non-dependent message send that requires writeback. |
| 282 | @interface Test40 |
| 283 | + (void) foo:(id *)errorPtr; |
| 284 | @end |
| 285 | template <class T> void test40_helper() { |
| 286 | id x; |
| 287 | [Test40 foo: &x]; |
| 288 | }; |
| 289 | template void test40_helper<int>(); |
| 290 | // CHECK: define weak_odr void @_Z13test40_helperIiEvv() |
| 291 | // CHECK: [[X:%.*]] = alloca i8* |
| 292 | // CHECK-NEXT: [[TEMP:%.*]] = alloca i8* |
| 293 | // CHECK-NEXT: store i8* null, i8** [[X]] |
| 294 | // CHECK: [[T0:%.*]] = load i8** [[X]] |
| 295 | // CHECK-NEXT: store i8* [[T0]], i8** [[TEMP]] |
| 296 | // CHECK: @objc_msgSend |
| 297 | // CHECK-NEXT: [[T0:%.*]] = load i8** [[TEMP]] |
| 298 | // CHECK-NEXT: call i8* @objc_retain(i8* [[T0]]) |
| 299 | |