1// RUN: %clang_cc1 -triple i386-apple-darwin9 -fobjc-runtime=macosx-fragile-10.5 -emit-llvm -fblocks -o - %s | FileCheck %s 2 3// test1. All of this is somehow testing rdar://6676764 4struct S { 5 void (^F)(struct S*); 6} P; 7 8 9@interface T 10 - (int)foo: (T* (^)(T*)) x; 11@end 12 13void foo(T *P) { 14 [P foo: 0]; 15} 16 17@interface A 18-(void) im0; 19@end 20 21// CHECK: define internal i32 @"__8-[A im0]_block_invoke"( 22@implementation A 23-(void) im0 { 24 (void) ^{ return 1; }(); 25} 26@end 27 28@interface B : A @end 29@implementation B 30-(void) im1 { 31 ^(void) { [self im0]; }(); 32} 33-(void) im2 { 34 ^{ [super im0]; }(); 35} 36-(void) im3 { 37 ^{ ^{[super im0];}(); }(); 38} 39@end 40 41// rdar://problem/9006315 42// In-depth test for the initialization of a __weak __block variable. 43@interface Test2 -(void) destroy; @end 44void test2(Test2 *x) { 45 extern void test2_helper(void (^)(void)); 46 // CHECK-LABEL: define void @test2( 47 // CHECK: [[X:%.*]] = alloca [[TEST2:%.*]]*, 48 // CHECK-NEXT: [[WEAKX:%.*]] = alloca [[WEAK_T:%.*]], 49 // CHECK-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]], 50 // CHECK-NEXT: store [[TEST2]]* 51 52 // isa=1 for weak byrefs. 53 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[WEAK_T]], [[WEAK_T]]* [[WEAKX]], i32 0, i32 0 54 // CHECK-NEXT: store i8* inttoptr (i32 1 to i8*), i8** [[T0]] 55 56 // Forwarding. 57 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [[WEAK_T]], [[WEAK_T]]* [[WEAKX]], i32 0, i32 1 58 // CHECK-NEXT: store [[WEAK_T]]* [[WEAKX]], [[WEAK_T]]** [[T1]] 59 60 // Flags. This is just BLOCK_HAS_COPY_DISPOSE BLOCK_BYREF_LAYOUT_UNRETAINED 61 // CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds [[WEAK_T]], [[WEAK_T]]* [[WEAKX]], i32 0, i32 2 62 // CHECK-NEXT: store i32 1375731712, i32* [[T2]] 63 64 // Size. 65 // CHECK-NEXT: [[T3:%.*]] = getelementptr inbounds [[WEAK_T]], [[WEAK_T]]* [[WEAKX]], i32 0, i32 3 66 // CHECK-NEXT: store i32 28, i32* [[T3]] 67 68 // Copy and dispose helpers. 69 // CHECK-NEXT: [[T4:%.*]] = getelementptr inbounds [[WEAK_T]], [[WEAK_T]]* [[WEAKX]], i32 0, i32 4 70 // CHECK-NEXT: store i8* bitcast (void (i8*, i8*)* @__Block_byref_object_copy_{{.*}} to i8*), i8** [[T4]] 71 // CHECK-NEXT: [[T5:%.*]] = getelementptr inbounds [[WEAK_T]], [[WEAK_T]]* [[WEAKX]], i32 0, i32 5 72 // CHECK-NEXT: store i8* bitcast (void (i8*)* @__Block_byref_object_dispose_{{.*}} to i8*), i8** [[T5]] 73 74 // Actually capture the value. 75 // CHECK-NEXT: [[T6:%.*]] = getelementptr inbounds [[WEAK_T]], [[WEAK_T]]* [[WEAKX]], i32 0, i32 6 76 // CHECK-NEXT: [[CAPTURE:%.*]] = load [[TEST2]]*, [[TEST2]]** [[X]] 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. 83 // CHECK: [[T0:%.*]] = bitcast [[WEAK_T]]* [[WEAKX]] to i8* 84 // CHECK: call void @_Block_object_dispose(i8* [[T0]], i32 24) 85 86 __attribute__((objc_gc(weak))) __block Test2 *weakX = x; 87 test2_helper(^{ [weakX destroy]; }); 88} 89 90// rdar://problem/9124263 91// In the test above, check that the use in the invocation function 92// doesn't require a read barrier. 93// CHECK-LABEL: define internal void @__test2_block_invoke 94// CHECK: [[BLOCK:%.*]] = bitcast i8* {{%.*}} to [[BLOCK_T]]* 95// CHECK-NOT: bitcast 96// CHECK: [[T0:%.*]] = getelementptr inbounds [[BLOCK_T]], [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5 97// CHECK-NEXT: [[T1:%.*]] = load i8*, i8** [[T0]] 98// CHECK-NEXT: [[T2:%.*]] = bitcast i8* [[T1]] to [[WEAK_T]]{{.*}}* 99// CHECK-NEXT: [[T3:%.*]] = getelementptr inbounds [[WEAK_T]]{{.*}}, [[WEAK_T]]{{.*}}* [[T2]], i32 0, i32 1 100// CHECK-NEXT: [[T4:%.*]] = load [[WEAK_T]]{{.*}}*, [[WEAK_T]]{{.*}}** [[T3]] 101// CHECK-NEXT: [[WEAKX:%.*]] = getelementptr inbounds [[WEAK_T]]{{.*}}, [[WEAK_T]]{{.*}}* [[T4]], i32 0, i32 6 102// CHECK-NEXT: [[T0:%.*]] = load [[TEST2]]*, [[TEST2]]** [[WEAKX]], align 4 103 104// rdar://problem/12722954 105// Make sure that ... is appropriately positioned in a block call. 106void test3(void (^block)(int, ...)) { 107 block(0, 1, 2, 3); 108} 109// CHECK-LABEL: define void @test3( 110// CHECK: [[BLOCK:%.*]] = alloca void (i32, ...)*, align 4 111// CHECK-NEXT: store void (i32, ...)* 112// CHECK-NEXT: [[T0:%.*]] = load void (i32, ...)*, void (i32, ...)** [[BLOCK]], align 4 113// CHECK-NEXT: [[T1:%.*]] = bitcast void (i32, ...)* [[T0]] to [[BLOCK_T:%.*]]* 114// CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds [[BLOCK_T]], [[BLOCK_T]]* [[T1]], i32 0, i32 3 115// CHECK-NEXT: [[T3:%.*]] = bitcast [[BLOCK_T]]* [[T1]] to i8* 116// CHECK-NEXT: [[T4:%.*]] = load i8*, i8** [[T2]] 117// CHECK-NEXT: [[T5:%.*]] = bitcast i8* [[T4]] to void (i8*, i32, ...)* 118// CHECK-NEXT: call void (i8*, i32, ...) [[T5]](i8* [[T3]], i32 0, i32 1, i32 2, i32 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 void (...)*, align 4 126// CHECK-NEXT: store void (...)* 127// CHECK-NEXT: [[T0:%.*]] = load void (...)*, void (...)** [[BLOCK]], align 4 128// CHECK-NEXT: [[T1:%.*]] = bitcast void (...)* [[T0]] to [[BLOCK_T:%.*]]* 129// CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds [[BLOCK_T]], [[BLOCK_T]]* [[T1]], i32 0, i32 3 130// CHECK-NEXT: [[T3:%.*]] = bitcast [[BLOCK_T]]* [[T1]] to i8* 131// CHECK-NEXT: [[T4:%.*]] = load i8*, i8** [[T2]] 132// CHECK-NEXT: [[T5:%.*]] = bitcast i8* [[T4]] to void (i8*, i32, i32, i32, i32)* 133// CHECK-NEXT: call void [[T5]](i8* [[T3]], i32 0, i32 1, i32 2, i32 3) 134// CHECK-NEXT: ret void 135