John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple i386-apple-darwin9 -fobjc-runtime=macosx-fragile-10.5 -emit-llvm -fblocks -o - %s | FileCheck %s |
Chris Lattner | a66b4df | 2009-03-13 22:00:25 +0000 | [diff] [blame] | 2 | |
John McCall | 0d3c985 | 2011-02-18 02:58:31 +0000 | [diff] [blame] | 3 | // test1. All of this is somehow testing rdar://6676764 |
Chris Lattner | a66b4df | 2009-03-13 22:00:25 +0000 | [diff] [blame] | 4 | struct S { |
| 5 | void (^F)(struct S*); |
| 6 | } P; |
| 7 | |
| 8 | |
| 9 | @interface T |
Eli Friedman | ddb5a39 | 2013-06-14 21:14:10 +0000 | [diff] [blame] | 10 | - (int)foo: (T* (^)(T*)) x; |
Chris Lattner | a66b4df | 2009-03-13 22:00:25 +0000 | [diff] [blame] | 11 | @end |
| 12 | |
| 13 | void foo(T *P) { |
| 14 | [P foo: 0]; |
| 15 | } |
| 16 | |
Mike Stump | 6cc88f7 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 17 | @interface A |
| 18 | -(void) im0; |
| 19 | @end |
| 20 | |
Fariborz Jahanian | 4904bf4 | 2012-06-26 16:06:38 +0000 | [diff] [blame] | 21 | // CHECK: define internal i32 @"__8-[A im0]_block_invoke"( |
Daniel Dunbar | 49f59ec | 2009-05-14 16:42:16 +0000 | [diff] [blame] | 22 | @implementation A |
| 23 | -(void) im0 { |
| 24 | (void) ^{ return 1; }(); |
| 25 | } |
| 26 | @end |
| 27 | |
Mike Stump | 6cc88f7 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 28 | @interface B : A @end |
| 29 | @implementation B |
| 30 | -(void) im1 { |
Chris Lattner | 00e2c63 | 2010-04-12 05:43:31 +0000 | [diff] [blame] | 31 | ^(void) { [self im0]; }(); |
Mike Stump | 6cc88f7 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 32 | } |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 33 | -(void) im2 { |
| 34 | ^{ [super im0]; }(); |
| 35 | } |
| 36 | -(void) im3 { |
| 37 | ^{ ^{[super im0];}(); }(); |
| 38 | } |
Mike Stump | 6cc88f7 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 39 | @end |
Daniel Dunbar | 49f59ec | 2009-05-14 16:42:16 +0000 | [diff] [blame] | 40 | |
John McCall | 0d3c985 | 2011-02-18 02:58:31 +0000 | [diff] [blame] | 41 | // rdar://problem/9006315 |
| 42 | // In-depth test for the initialization of a __weak __block variable. |
| 43 | @interface Test2 -(void) destroy; @end |
| 44 | void test2(Test2 *x) { |
| 45 | extern void test2_helper(void (^)(void)); |
Stephen Lin | 93ab6bf | 2013-08-15 06:47:53 +0000 | [diff] [blame] | 46 | // CHECK-LABEL: define void @test2( |
John McCall | 0d3c985 | 2011-02-18 02:58:31 +0000 | [diff] [blame] | 47 | // CHECK: [[X:%.*]] = alloca [[TEST2:%.*]]*, |
| 48 | // CHECK-NEXT: [[WEAKX:%.*]] = alloca [[WEAK_T:%.*]], |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 49 | // CHECK-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]], |
John McCall | 0d3c985 | 2011-02-18 02:58:31 +0000 | [diff] [blame] | 50 | // CHECK-NEXT: store [[TEST2]]* |
| 51 | |
| 52 | // isa=1 for weak byrefs. |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 53 | // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[WEAK_T]], [[WEAK_T]]* [[WEAKX]], i32 0, i32 0 |
John McCall | 0d3c985 | 2011-02-18 02:58:31 +0000 | [diff] [blame] | 54 | // CHECK-NEXT: store i8* inttoptr (i32 1 to i8*), i8** [[T0]] |
| 55 | |
| 56 | // Forwarding. |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 57 | // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [[WEAK_T]], [[WEAK_T]]* [[WEAKX]], i32 0, i32 1 |
John McCall | 0d3c985 | 2011-02-18 02:58:31 +0000 | [diff] [blame] | 58 | // CHECK-NEXT: store [[WEAK_T]]* [[WEAKX]], [[WEAK_T]]** [[T1]] |
| 59 | |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 60 | // Flags. This is just BLOCK_HAS_COPY_DISPOSE BLOCK_BYREF_LAYOUT_UNRETAINED |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 61 | // CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds [[WEAK_T]], [[WEAK_T]]* [[WEAKX]], i32 0, i32 2 |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 62 | // CHECK-NEXT: store i32 1375731712, i32* [[T2]] |
John McCall | 0d3c985 | 2011-02-18 02:58:31 +0000 | [diff] [blame] | 63 | |
| 64 | // Size. |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 65 | // CHECK-NEXT: [[T3:%.*]] = getelementptr inbounds [[WEAK_T]], [[WEAK_T]]* [[WEAKX]], i32 0, i32 3 |
John McCall | 0d3c985 | 2011-02-18 02:58:31 +0000 | [diff] [blame] | 66 | // CHECK-NEXT: store i32 28, i32* [[T3]] |
| 67 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 68 | // Copy and dispose helpers. |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 69 | // CHECK-NEXT: [[T4:%.*]] = getelementptr inbounds [[WEAK_T]], [[WEAK_T]]* [[WEAKX]], i32 0, i32 4 |
John McCall | 0d3c985 | 2011-02-18 02:58:31 +0000 | [diff] [blame] | 70 | // CHECK-NEXT: store i8* bitcast (void (i8*, i8*)* @__Block_byref_object_copy_{{.*}} to i8*), i8** [[T4]] |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 71 | // CHECK-NEXT: [[T5:%.*]] = getelementptr inbounds [[WEAK_T]], [[WEAK_T]]* [[WEAKX]], i32 0, i32 5 |
John McCall | 0d3c985 | 2011-02-18 02:58:31 +0000 | [diff] [blame] | 72 | // CHECK-NEXT: store i8* bitcast (void (i8*)* @__Block_byref_object_dispose_{{.*}} to i8*), i8** [[T5]] |
| 73 | |
| 74 | // Actually capture the value. |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 75 | // CHECK-NEXT: [[T6:%.*]] = getelementptr inbounds [[WEAK_T]], [[WEAK_T]]* [[WEAKX]], i32 0, i32 6 |
| 76 | // CHECK-NEXT: [[CAPTURE:%.*]] = load [[TEST2]]*, [[TEST2]]** [[X]] |
John McCall | 0d3c985 | 2011-02-18 02:58:31 +0000 | [diff] [blame] | 77 | // CHECK-NEXT: store [[TEST2]]* [[CAPTURE]], [[TEST2]]** [[T6]] |
| 78 | |
| 79 | // Then we initialize the block, blah blah blah. |
| 80 | // CHECK: call void @test2_helper( |
| 81 | |
| 82 | // Finally, kill the variable with BLOCK_FIELD_IS_BYREF. We're not |
| 83 | // supposed to pass BLOCK_FIELD_IS_WEAK here. |
| 84 | // CHECK: [[T0:%.*]] = bitcast [[WEAK_T]]* [[WEAKX]] to i8* |
| 85 | // CHECK: call void @_Block_object_dispose(i8* [[T0]], i32 8) |
| 86 | |
| 87 | __weak __block Test2 *weakX = x; |
| 88 | test2_helper(^{ [weakX destroy]; }); |
| 89 | } |
John McCall | a5bcb8f | 2011-03-16 02:53:38 +0000 | [diff] [blame] | 90 | |
| 91 | // rdar://problem/9124263 |
| 92 | // In the test above, check that the use in the invocation function |
| 93 | // doesn't require a read barrier. |
Stephen Lin | 93ab6bf | 2013-08-15 06:47:53 +0000 | [diff] [blame] | 94 | // CHECK-LABEL: define internal void @__test2_block_invoke |
John McCall | a5bcb8f | 2011-03-16 02:53:38 +0000 | [diff] [blame] | 95 | // CHECK: [[BLOCK:%.*]] = bitcast i8* {{%.*}} to [[BLOCK_T]]* |
Adrian Prantl | 9b97adf | 2013-03-29 19:20:35 +0000 | [diff] [blame] | 96 | // CHECK-NOT: bitcast |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 97 | // CHECK: [[T0:%.*]] = getelementptr inbounds [[BLOCK_T]], [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5 |
| 98 | // CHECK-NEXT: [[T1:%.*]] = load i8*, i8** [[T0]] |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 99 | // CHECK-NEXT: [[T2:%.*]] = bitcast i8* [[T1]] to [[WEAK_T]]{{.*}}* |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 100 | // CHECK-NEXT: [[T3:%.*]] = getelementptr inbounds [[WEAK_T]]{{.*}}, [[WEAK_T]]{{.*}}* [[T2]], i32 0, i32 1 |
| 101 | // CHECK-NEXT: [[T4:%.*]] = load [[WEAK_T]]{{.*}}*, [[WEAK_T]]{{.*}}** [[T3]] |
| 102 | // CHECK-NEXT: [[WEAKX:%.*]] = getelementptr inbounds [[WEAK_T]]{{.*}}, [[WEAK_T]]{{.*}}* [[T4]], i32 0, i32 6 |
| 103 | // CHECK-NEXT: [[T0:%.*]] = load [[TEST2]]*, [[TEST2]]** [[WEAKX]], align 4 |
John McCall | e56bb36 | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 104 | |
| 105 | // rdar://problem/12722954 |
| 106 | // Make sure that ... is appropriately positioned in a block call. |
| 107 | void test3(void (^block)(int, ...)) { |
| 108 | block(0, 1, 2, 3); |
| 109 | } |
Stephen Lin | 93ab6bf | 2013-08-15 06:47:53 +0000 | [diff] [blame] | 110 | // CHECK-LABEL: define void @test3( |
John McCall | e56bb36 | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 111 | // CHECK: [[BLOCK:%.*]] = alloca void (i32, ...)*, align 4 |
| 112 | // CHECK-NEXT: store void (i32, ...)* |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 113 | // CHECK-NEXT: [[T0:%.*]] = load void (i32, ...)*, void (i32, ...)** [[BLOCK]], align 4 |
John McCall | e56bb36 | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 114 | // CHECK-NEXT: [[T1:%.*]] = bitcast void (i32, ...)* [[T0]] to [[BLOCK_T:%.*]]* |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 115 | // CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds [[BLOCK_T]], [[BLOCK_T]]* [[T1]], i32 0, i32 3 |
John McCall | e56bb36 | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 116 | // CHECK-NEXT: [[T3:%.*]] = bitcast [[BLOCK_T]]* [[T1]] to i8* |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 117 | // CHECK-NEXT: [[T4:%.*]] = load i8*, i8** [[T2]] |
John McCall | e56bb36 | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 118 | // CHECK-NEXT: [[T5:%.*]] = bitcast i8* [[T4]] to void (i8*, i32, ...)* |
Pirama Arumuga Nainar | 33337ca | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 119 | // CHECK-NEXT: call void (i8*, i32, ...) [[T5]](i8* [[T3]], i32 0, i32 1, i32 2, i32 3) |
John McCall | e56bb36 | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 120 | // CHECK-NEXT: ret void |
| 121 | |
| 122 | void test4(void (^block)()) { |
| 123 | block(0, 1, 2, 3); |
| 124 | } |
Stephen Lin | 93ab6bf | 2013-08-15 06:47:53 +0000 | [diff] [blame] | 125 | // CHECK-LABEL: define void @test4( |
John McCall | e56bb36 | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 126 | // CHECK: [[BLOCK:%.*]] = alloca void (...)*, align 4 |
| 127 | // CHECK-NEXT: store void (...)* |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 128 | // CHECK-NEXT: [[T0:%.*]] = load void (...)*, void (...)** [[BLOCK]], align 4 |
John McCall | e56bb36 | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 129 | // CHECK-NEXT: [[T1:%.*]] = bitcast void (...)* [[T0]] to [[BLOCK_T:%.*]]* |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 130 | // CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds [[BLOCK_T]], [[BLOCK_T]]* [[T1]], i32 0, i32 3 |
John McCall | e56bb36 | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 131 | // CHECK-NEXT: [[T3:%.*]] = bitcast [[BLOCK_T]]* [[T1]] to i8* |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 132 | // CHECK-NEXT: [[T4:%.*]] = load i8*, i8** [[T2]] |
John McCall | e56bb36 | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 133 | // CHECK-NEXT: [[T5:%.*]] = bitcast i8* [[T4]] to void (i8*, i32, i32, i32, i32)* |
| 134 | // CHECK-NEXT: call void [[T5]](i8* [[T3]], i32 0, i32 1, i32 2, i32 3) |
| 135 | // CHECK-NEXT: ret void |