John McCall | d1e40d5 | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fblocks -fobjc-arc -fobjc-runtime-has-weak -triple x86_64-apple-darwin -O0 -emit-llvm %s -o %t-64.s |
Tim Northover | 931a4fe | 2013-08-12 12:51:05 +0000 | [diff] [blame] | 2 | // RUN: FileCheck -check-prefix CHECK-LP64 --input-file=%t-64.s %s |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3 | // rdar://9503326 |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 4 | // rdar://9606600 |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6 | extern void use(id); |
| 7 | extern void use_block(void (^)(void)); |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 8 | |
| 9 | struct NSFastEnumerationState; |
| 10 | @interface NSArray |
| 11 | - (unsigned long) countByEnumeratingWithState: (struct NSFastEnumerationState*) state |
| 12 | objects: (id*) buffer |
| 13 | count: (unsigned long) bufferSize; |
| 14 | @end; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 15 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 16 | void test0(NSArray *array) { |
| 17 | // 'x' should be initialized without a retain. |
| 18 | // We should actually do a non-constant capture, and that |
| 19 | // capture should require a retain. |
| 20 | for (id x in array) { |
| 21 | use_block(^{ use(x); }); |
| 22 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 23 | } |
| 24 | |
Stephen Lin | 93ab6bf | 2013-08-15 06:47:53 +0000 | [diff] [blame] | 25 | // CHECK-LP64-LABEL: define void @test0( |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 26 | // CHECK-LP64: [[ARRAY:%.*]] = alloca [[ARRAY_T:%.*]]*, |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 27 | // CHECK-LP64-NEXT: [[X:%.*]] = alloca i8*, |
| 28 | // CHECK-LP64-NEXT: [[STATE:%.*]] = alloca [[STATE_T:%.*]], |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 29 | // CHECK-LP64-NEXT: [[BUFFER:%.*]] = alloca [16 x i8*], align 8 |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 30 | // CHECK-LP64-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]], |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 31 | |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 32 | // Initialize 'array'. |
Fariborz Jahanian | 41a6a3e | 2013-02-21 00:40:10 +0000 | [diff] [blame] | 33 | // CHECK-LP64-NEXT: store [[ARRAY_T]]* null, [[ARRAY_T]]** [[ARRAY]] |
| 34 | // CHECK-LP64-NEXT: [[ZERO:%.*]] = bitcast [[ARRAY_T]]** [[ARRAY]] to i8** |
| 35 | // CHECK-LP64-NEXT: [[ONE:%.*]] = bitcast [[ARRAY_T]]* {{%.*}} to i8* |
Bill Wendling | 4e1125f | 2013-02-22 09:10:20 +0000 | [diff] [blame] | 36 | // CHECK-LP64-NEXT: call void @objc_storeStrong(i8** [[ZERO]], i8* [[ONE]]) [[NUW:#[0-9]+]] |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 37 | |
| 38 | // Initialize the fast enumaration state. |
| 39 | // CHECK-LP64-NEXT: [[T0:%.*]] = bitcast [[STATE_T]]* [[STATE]] to i8* |
| 40 | // CHECK-LP64-NEXT: call void @llvm.memset.p0i8.i64(i8* [[T0]], i8 0, i64 64, i32 8, i1 false) |
| 41 | |
| 42 | // Evaluate the collection expression and retain. |
| 43 | // CHECK-LP64-NEXT: [[T0:%.*]] = load [[ARRAY_T]]** [[ARRAY]], align 8 |
| 44 | // CHECK-LP64-NEXT: [[T1:%.*]] = bitcast [[ARRAY_T]]* [[T0]] to i8* |
| 45 | // CHECK-LP64-NEXT: [[T2:%.*]] = call i8* @objc_retain(i8* [[T1]]) |
| 46 | // CHECK-LP64-NEXT: [[SAVED_ARRAY:%.*]] = bitcast i8* [[T2]] to [[ARRAY_T]]* |
| 47 | |
| 48 | // Call the enumeration method. |
| 49 | // CHECK-LP64-NEXT: [[T0:%.*]] = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_ |
| 50 | // CHECK-LP64-NEXT: [[T1:%.*]] = bitcast [[ARRAY_T]]* [[SAVED_ARRAY]] to i8* |
| 51 | // CHECK-LP64-NEXT: [[SIZE:%.*]] = call i64 bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to i64 (i8*, i8*, [[STATE_T]]*, [16 x i8*]*, i64)*)(i8* [[T1]], i8* [[T0]], [[STATE_T]]* [[STATE]], [16 x i8*]* [[BUFFER]], i64 16) |
| 52 | |
| 53 | // Check for a nonzero result. |
| 54 | // CHECK-LP64-NEXT: [[T0:%.*]] = icmp eq i64 [[SIZE]], 0 |
| 55 | // CHECK-LP64-NEXT: br i1 [[T0]] |
| 56 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 57 | // CHECK-LP64: [[T0:%.*]] = getelementptr inbounds [[STATE_T]]* [[STATE]], i32 0, i32 1 |
| 58 | // CHECK-LP64-NEXT: [[T1:%.*]] = load i8*** [[T0]] |
| 59 | // CHECK-LP64-NEXT: [[T2:%.*]] = getelementptr i8** [[T1]], i64 |
| 60 | // CHECK-LP64-NEXT: [[T3:%.*]] = load i8** [[T2]] |
| 61 | // CHECK-LP64-NEXT: store i8* [[T3]], i8** [[X]] |
| 62 | |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 63 | // CHECK-LP64: [[D0:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5 |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 64 | // CHECK-LP64: [[T0:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5 |
| 65 | // CHECK-LP64-NEXT: [[T1:%.*]] = load i8** [[X]] |
| 66 | // CHECK-LP64-NEXT: [[T2:%.*]] = call i8* @objc_retain(i8* [[T1]]) |
| 67 | // CHECK-LP64-NEXT: store i8* [[T2]], i8** [[T0]] |
Chris Lattner | f0a8679 | 2011-07-10 03:47:27 +0000 | [diff] [blame] | 68 | // CHECK-LP64-NEXT: [[T1:%.*]] = bitcast [[BLOCK_T]]* [[BLOCK]] |
Chris Lattner | 57eb23f | 2011-07-10 05:39:13 +0000 | [diff] [blame] | 69 | // CHECK-LP64: call void @use_block( |
John McCall | 015f33b | 2012-10-17 02:28:37 +0000 | [diff] [blame] | 70 | // CHECK-LP64-NEXT: call void @objc_storeStrong(i8** [[D0]], i8* null) |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 71 | |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 72 | // CHECK-LP64: [[T0:%.*]] = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_ |
| 73 | // CHECK-LP64-NEXT: [[T1:%.*]] = bitcast [[ARRAY_T]]* [[SAVED_ARRAY]] to i8* |
| 74 | // CHECK-LP64-NEXT: [[SIZE:%.*]] = call i64 bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to i64 (i8*, i8*, [[STATE_T]]*, [16 x i8*]*, i64)*)(i8* [[T1]], i8* [[T0]], [[STATE_T]]* [[STATE]], [16 x i8*]* [[BUFFER]], i64 16) |
| 75 | |
| 76 | // Release the array. |
| 77 | // CHECK-LP64: [[T0:%.*]] = bitcast [[ARRAY_T]]* [[SAVED_ARRAY]] to i8* |
| 78 | // CHECK-LP64-NEXT: call void @objc_release(i8* [[T0]]) |
| 79 | |
| 80 | // Destroy 'array'. |
John McCall | 015f33b | 2012-10-17 02:28:37 +0000 | [diff] [blame] | 81 | // CHECK-LP64: [[T0:%.*]] = bitcast [[ARRAY_T]]** [[ARRAY]] to i8** |
| 82 | // CHECK-LP64-NEXT: call void @objc_storeStrong(i8** [[T0]], i8* null) |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 83 | // CHECK-LP64-NEXT: ret void |
| 84 | |
Stephen Lin | 93ab6bf | 2013-08-15 06:47:53 +0000 | [diff] [blame] | 85 | // CHECK-LP64-LABEL: define internal void @__test0_block_invoke |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 86 | // CHECK-LP64: [[BLOCK:%.*]] = bitcast i8* {{%.*}} to [[BLOCK_T]]* |
Adrian Prantl | 9b97adf | 2013-03-29 19:20:35 +0000 | [diff] [blame] | 87 | // CHECK-LP64-NOT: ret |
| 88 | // CHECK-LP64: [[T0:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5 |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 89 | // CHECK-LP64-NEXT: [[T2:%.*]] = load i8** [[T0]], align 8 |
| 90 | // CHECK-LP64-NEXT: call void @use(i8* [[T2]]) |
| 91 | |
| 92 | void test1(NSArray *array) { |
| 93 | for (__weak id x in array) { |
| 94 | use_block(^{ use(x); }); |
| 95 | } |
| 96 | } |
| 97 | |
Stephen Lin | 93ab6bf | 2013-08-15 06:47:53 +0000 | [diff] [blame] | 98 | // CHECK-LP64-LABEL: define void @test1( |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 99 | // CHECK-LP64: alloca [[ARRAY_T:%.*]]*, |
| 100 | // CHECK-LP64-NEXT: [[X:%.*]] = alloca i8*, |
| 101 | // CHECK-LP64-NEXT: [[STATE:%.*]] = alloca [[STATE_T:%.*]], |
| 102 | // CHECK-LP64-NEXT: alloca [16 x i8*], align 8 |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 103 | // CHECK-LP64-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]], |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 104 | |
| 105 | // CHECK-LP64: [[T0:%.*]] = getelementptr inbounds [[STATE_T]]* [[STATE]], i32 0, i32 1 |
| 106 | // CHECK-LP64-NEXT: [[T1:%.*]] = load i8*** [[T0]] |
| 107 | // CHECK-LP64-NEXT: [[T2:%.*]] = getelementptr i8** [[T1]], i64 |
| 108 | // CHECK-LP64-NEXT: [[T3:%.*]] = load i8** [[T2]] |
| 109 | // CHECK-LP64-NEXT: call i8* @objc_initWeak(i8** [[X]], i8* [[T3]]) |
| 110 | |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 111 | // CHECK-LP64: [[D0:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5 |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 112 | // CHECK-LP64: [[T0:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5 |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 113 | // CHECK-LP64-NEXT: [[T1:%.*]] = call i8* @objc_loadWeakRetained(i8** [[X]]) |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 114 | // CHECK-LP64-NEXT: call i8* @objc_initWeak(i8** [[T0]], i8* [[T1]]) |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 115 | // CHECK-LP64-NEXT: call void @objc_release(i8* [[T1]]) |
Chris Lattner | f0a8679 | 2011-07-10 03:47:27 +0000 | [diff] [blame] | 116 | // CHECK-LP64-NEXT: [[T1:%.*]] = bitcast [[BLOCK_T]]* [[BLOCK]] to |
Chris Lattner | 57eb23f | 2011-07-10 05:39:13 +0000 | [diff] [blame] | 117 | // CHECK-LP64: call void @use_block |
John McCall | b99785b | 2011-11-10 09:22:44 +0000 | [diff] [blame] | 118 | // CHECK-LP64-NEXT: call void @objc_destroyWeak(i8** [[D0]]) |
| 119 | // CHECK-LP64-NEXT: call void @objc_destroyWeak(i8** [[X]]) |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 120 | |
| 121 | // rdar://problem/9817306 |
| 122 | @interface Test2 |
| 123 | - (NSArray *) array; |
| 124 | @end |
| 125 | void test2(Test2 *a) { |
| 126 | for (id x in a.array) { |
| 127 | use(x); |
| 128 | } |
| 129 | } |
| 130 | |
Stephen Lin | 93ab6bf | 2013-08-15 06:47:53 +0000 | [diff] [blame] | 131 | // CHECK-LP64-LABEL: define void @test2( |
John McCall | 990567c | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 132 | // CHECK-LP64: [[T0:%.*]] = call [[ARRAY_T]]* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to [[ARRAY_T]]* (i8*, i8*)*)( |
| 133 | // CHECK-LP64-NEXT: [[T1:%.*]] = bitcast [[ARRAY_T]]* [[T0]] to i8* |
| 134 | // CHECK-LP64-NEXT: [[T2:%.*]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[T1]]) |
| 135 | // CHECK-LP64-NEXT: [[COLL:%.*]] = bitcast i8* [[T2]] to [[ARRAY_T]]* |
| 136 | |
| 137 | // Make sure it's not immediately released before starting the iteration. |
| 138 | // CHECK-LP64-NEXT: load i8** @"\01L_OBJC_SELECTOR_REFERENCES_ |
| 139 | // CHECK-LP64-NEXT: [[T0:%.*]] = bitcast [[ARRAY_T]]* [[COLL]] to i8* |
| 140 | // CHECK-LP64-NEXT: @objc_msgSend |
| 141 | |
| 142 | // This bitcast is for the mutation check. |
| 143 | // CHECK-LP64: [[T0:%.*]] = bitcast [[ARRAY_T]]* [[COLL]] to i8* |
| 144 | // CHECK-LP64-NEXT: @objc_enumerationMutation |
| 145 | |
| 146 | // This bitcast is for the 'next' message send. |
| 147 | // CHECK-LP64: [[T0:%.*]] = bitcast [[ARRAY_T]]* [[COLL]] to i8* |
| 148 | // CHECK-LP64-NEXT: @objc_msgSend |
| 149 | |
| 150 | // This bitcast is for the final release. |
| 151 | // CHECK-LP64: [[T0:%.*]] = bitcast [[ARRAY_T]]* [[COLL]] to i8* |
| 152 | // CHECK-LP64-NEXT: call void @objc_release(i8* [[T0]]) |
John McCall | 4b302d3 | 2011-08-05 00:14:38 +0000 | [diff] [blame] | 153 | |
| 154 | |
| 155 | // Check that the 'continue' label is positioned appropriately |
| 156 | // relative to the collection clenaup. |
| 157 | void test3(NSArray *array) { |
| 158 | for (id x in array) { |
| 159 | if (!x) continue; |
| 160 | use(x); |
| 161 | } |
| 162 | |
Stephen Lin | 93ab6bf | 2013-08-15 06:47:53 +0000 | [diff] [blame] | 163 | // CHECK-LP64-LABEL: define void @test3( |
John McCall | 4b302d3 | 2011-08-05 00:14:38 +0000 | [diff] [blame] | 164 | // CHECK-LP64: [[ARRAY:%.*]] = alloca [[ARRAY_T]]*, align 8 |
| 165 | // CHECK-LP64-NEXT: [[X:%.*]] = alloca i8*, align 8 |
| 166 | // CHECK-LP64: [[T0:%.*]] = load i8** [[X]], align 8 |
| 167 | // CHECK-LP64-NEXT: [[T1:%.*]] = icmp ne i8* [[T0]], null |
| 168 | // CHECK-LP64-NEXT: br i1 [[T1]], |
| 169 | // CHECK-LP64: br label [[L:%[^ ]+]] |
| 170 | // CHECK-LP64: [[T0:%.*]] = load i8** [[X]], align 8 |
| 171 | // CHECK-LP64-NEXT: call void @use(i8* [[T0]]) |
| 172 | // CHECK-LP64-NEXT: br label [[L]] |
| 173 | } |
Bill Wendling | 4e1125f | 2013-02-22 09:10:20 +0000 | [diff] [blame] | 174 | |
| 175 | // CHECK: attributes [[NUW]] = { nounwind } |