1// RUN: %clang_cc1 -triple i386-apple-darwin9 -fobjc-runtime=macosx-fragile-10.5 -emit-llvm -fblocks -Wno-strict-prototypes -o - %s | FileCheck --check-prefix=CHECK --check-prefix=SIG_STR %s 2// RUN: %clang_cc1 -triple i386-apple-darwin9 -fobjc-runtime=macosx-fragile-10.5 -emit-llvm -fblocks -Wno-strict-prototypes -fdisable-block-signature-string -o - %s | FileCheck --check-prefix=CHECK --check-prefix=NO_SIG_STR %s 3 4// Check that there is only one capture (20o) in the copy/dispose function 5// names. 6 7// SIG_STR: @[[STR3:.*]] = private unnamed_addr constant [6 x i8] c"v4@?0\00", align 1 8// SIG_STR: @[[BLOCK_DESCRIPTOR0:"__block_descriptor_28_4_20o_e5_v4\\01\?0l"]] = linkonce_odr hidden unnamed_addr constant { i32, i32, ptr, ptr, ptr, i32 } { i32 0, i32 28, ptr @__copy_helper_block_4_20o, ptr @__destroy_helper_block_4_20o, ptr @[[STR3]], i32 512 }, 9// NO_SIG_STR: @[[BLOCK_DESCRIPTOR0:__block_descriptor_28_4_20o_e0_l]] = linkonce_odr hidden unnamed_addr constant { i32, i32, ptr, ptr, ptr, i32 } { i32 0, i32 28, ptr @__copy_helper_block_4_20o, ptr @__destroy_helper_block_4_20o, ptr null, i32 512 }, 10 11 12void (^gb0)(void); 13 14struct S { 15 void (^F)(struct S*); 16} P; 17 18 19@interface T 20 - (int)foo: (T* (^)(T*)) x; 21@end 22 23void foo(T *P) { 24 [P foo: 0]; 25} 26 27@interface A 28-(void) im0; 29@end 30 31// CHECK: define internal i32 @"__8-[A im0]_block_invoke"( 32@implementation A 33-(void) im0 { 34 (void) ^{ return 1; }(); 35} 36@end 37 38@interface B : A @end 39@implementation B 40-(void) im1 { 41 ^(void) { [self im0]; }(); 42} 43-(void) im2 { 44 ^{ [super im0]; }(); 45} 46-(void) im3 { 47 ^{ ^{[super im0];}(); }(); 48} 49@end 50 51// In-depth test for the initialization of a __weak __block variable. 52@interface Test2 -(void) destroy; @end 53void test2(Test2 *x) { 54 extern void test2_helper(void (^)(void)); 55 // CHECK-LABEL: define{{.*}} void @test2( 56 // CHECK: [[X:%.*]] = alloca ptr, 57 // CHECK-NEXT: [[WEAKX:%.*]] = alloca [[WEAK_T:%.*]], 58 // CHECK-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]], 59 // CHECK-NEXT: store ptr 60 61 // isa=1 for weak byrefs. 62 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds nuw [[WEAK_T]], ptr [[WEAKX]], i32 0, i32 0 63 // CHECK-NEXT: store ptr inttoptr (i32 1 to ptr), ptr [[T0]] 64 65 // Forwarding. 66 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds nuw [[WEAK_T]], ptr [[WEAKX]], i32 0, i32 1 67 // CHECK-NEXT: store ptr [[WEAKX]], ptr [[T1]] 68 69 // Flags. This is just BLOCK_HAS_COPY_DISPOSE BLOCK_BYREF_LAYOUT_UNRETAINED 70 // CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds nuw [[WEAK_T]], ptr [[WEAKX]], i32 0, i32 2 71 // CHECK-NEXT: store i32 1375731712, ptr [[T2]] 72 73 // Size. 74 // CHECK-NEXT: [[T3:%.*]] = getelementptr inbounds nuw [[WEAK_T]], ptr [[WEAKX]], i32 0, i32 3 75 // CHECK-NEXT: store i32 28, ptr [[T3]] 76 77 // Copy and dispose helpers. 78 // CHECK-NEXT: [[T4:%.*]] = getelementptr inbounds nuw [[WEAK_T]], ptr [[WEAKX]], i32 0, i32 4 79 // CHECK-NEXT: store ptr @__Block_byref_object_copy_{{.*}}, ptr [[T4]] 80 // CHECK-NEXT: [[T5:%.*]] = getelementptr inbounds nuw [[WEAK_T]], ptr [[WEAKX]], i32 0, i32 5 81 // CHECK-NEXT: store ptr @__Block_byref_object_dispose_{{.*}}, ptr [[T5]] 82 83 // Actually capture the value. 84 // CHECK-NEXT: [[T6:%.*]] = getelementptr inbounds nuw [[WEAK_T]], ptr [[WEAKX]], i32 0, i32 6 85 // CHECK-NEXT: [[CAPTURE:%.*]] = load ptr, ptr [[X]] 86 // CHECK-NEXT: store ptr [[CAPTURE]], ptr [[T6]] 87 88 // Then we initialize the block, blah blah blah. 89 // CHECK: call void @test2_helper( 90 91 // Finally, kill the variable with BLOCK_FIELD_IS_BYREF. 92 // CHECK: call void @_Block_object_dispose(ptr [[WEAKX]], i32 24) 93 94 __attribute__((objc_gc(weak))) __block Test2 *weakX = x; 95 test2_helper(^{ [weakX destroy]; }); 96} 97 98// In the test above, check that the use in the invocation function 99// doesn't require a read barrier. 100// CHECK-LABEL: define internal void @__test2_block_invoke 101// CHECK: [[T0:%.*]] = getelementptr inbounds nuw [[BLOCK_T]], ptr {{%.*}}, i32 0, i32 5 102// CHECK-NEXT: [[T1:%.*]] = load ptr, ptr [[T0]] 103// CHECK-NEXT: [[T3:%.*]] = getelementptr inbounds nuw [[WEAK_T]]{{.*}}, ptr [[T1]], i32 0, i32 1 104// CHECK-NEXT: [[T4:%.*]] = load ptr, ptr [[T3]] 105// CHECK-NEXT: [[WEAKX:%.*]] = getelementptr inbounds nuw [[WEAK_T]]{{.*}}, ptr [[T4]], i32 0, i32 6 106// CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[WEAKX]], align 4 107 108// Make sure that ... is appropriately positioned in a block call. 109void test3(void (^block)(int, ...)) { 110 block(0, 1, 2, 3); 111} 112// CHECK-LABEL: define{{.*}} void @test3( 113// CHECK: [[BLOCK:%.*]] = alloca ptr, align 4 114// CHECK-NEXT: store ptr 115// CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[BLOCK]], align 4 116// CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds nuw [[BLOCK_T:%.*]], ptr [[T0]], i32 0, i32 3 117// CHECK-NEXT: [[T4:%.*]] = load ptr, ptr [[T2]] 118// CHECK-NEXT: call void (ptr, i32, ...) [[T4]](ptr noundef [[T0]], i32 noundef 0, i32 noundef 1, i32 noundef 2, i32 noundef 3) 119// CHECK-NEXT: ret void 120 121void test4(void (^block)()) { 122 block(0, 1, 2, 3); 123} 124// CHECK-LABEL: define{{.*}} void @test4( 125// CHECK: [[BLOCK:%.*]] = alloca ptr, align 4 126// CHECK-NEXT: store ptr 127// CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[BLOCK]], align 4 128// CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds nuw [[BLOCK_T:%.*]], ptr [[T0]], i32 0, i32 3 129// CHECK-NEXT: [[T4:%.*]] = load ptr, ptr [[T2]] 130// CHECK-NEXT: call void [[T4]](ptr noundef [[T0]], i32 noundef 0, i32 noundef 1, i32 noundef 2, i32 noundef 3) 131// CHECK-NEXT: ret void 132 133void test5(A *a) { 134 __unsafe_unretained A *t = a; 135 gb0 = ^{ (void)a; (void)t; }; 136} 137 138// CHECK-LABEL: define void @test5( 139// CHECK: %[[FLAGS:.*]] = getelementptr inbounds nuw <{ ptr, i32, i32, ptr, ptr, ptr, ptr }>, ptr %{{.*}}, i32 0, i32 1 140// SIG_STR: store i32 -1040187392, ptr %[[FLAGS]], align 4 141// NO_SIG_STR: store i32 -2113929216, ptr %[[FLAGS]], align 4 142// CHECK: %[[V0:.*]] = getelementptr inbounds nuw <{ ptr, i32, i32, ptr, ptr, ptr, ptr }>, ptr %{{.*}}, i32 0, i32 4 143// CHECK: store ptr @[[BLOCK_DESCRIPTOR0]], ptr %[[V0]], 144 145void test6(id a, long long b) { 146 void (^block)() = ^{ (void)b; (void)a; }; 147} 148 149// Check that the block literal doesn't have two fields for capture 'a'. 150 151// CHECK-LABEL: define void @test6( 152// CHECK: alloca <{ ptr, i32, i32, ptr, ptr, ptr, i64 }>, 153