1 // RUN: %clang_cc1 -triple i386-apple-darwin9 -fobjc-runtime=macosx-fragile-10.5 -emit-llvm -fblocks -Wno-strict-prototypes -o - %s | FileCheck %s
3 // Check that there is only one capture (20o) in the copy/dispose function
6 // CHECK: @[[BLOCK_DESCRIPTOR0:.*]] = 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 @{{.*}}, i32 512 },
16 - (int)foo: (T* (^)(T*)) x;
27 // CHECK: define internal i32 @"__8-[A im0]_block_invoke"(
30 (void) ^{ return 1; }();
37 ^(void) { [self im0]; }();
43 ^{ ^{[super im0];}(); }();
47 // In-depth test for the initialization of a __weak __block variable.
48 @interface Test2 -(void) destroy; @end
49 void test2(Test2 *x) {
50 extern void test2_helper(void (^)(void));
51 // CHECK-LABEL: define{{.*}} void @test2(
52 // CHECK: [[X:%.*]] = alloca ptr,
53 // CHECK-NEXT: [[WEAKX:%.*]] = alloca [[WEAK_T:%.*]],
54 // CHECK-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]],
55 // CHECK-NEXT: store ptr
57 // isa=1 for weak byrefs.
58 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[WEAK_T]], ptr [[WEAKX]], i32 0, i32 0
59 // CHECK-NEXT: store ptr inttoptr (i32 1 to ptr), ptr [[T0]]
62 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [[WEAK_T]], ptr [[WEAKX]], i32 0, i32 1
63 // CHECK-NEXT: store ptr [[WEAKX]], ptr [[T1]]
65 // Flags. This is just BLOCK_HAS_COPY_DISPOSE BLOCK_BYREF_LAYOUT_UNRETAINED
66 // CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds [[WEAK_T]], ptr [[WEAKX]], i32 0, i32 2
67 // CHECK-NEXT: store i32 1375731712, ptr [[T2]]
70 // CHECK-NEXT: [[T3:%.*]] = getelementptr inbounds [[WEAK_T]], ptr [[WEAKX]], i32 0, i32 3
71 // CHECK-NEXT: store i32 28, ptr [[T3]]
73 // Copy and dispose helpers.
74 // CHECK-NEXT: [[T4:%.*]] = getelementptr inbounds [[WEAK_T]], ptr [[WEAKX]], i32 0, i32 4
75 // CHECK-NEXT: store ptr @__Block_byref_object_copy_{{.*}}, ptr [[T4]]
76 // CHECK-NEXT: [[T5:%.*]] = getelementptr inbounds [[WEAK_T]], ptr [[WEAKX]], i32 0, i32 5
77 // CHECK-NEXT: store ptr @__Block_byref_object_dispose_{{.*}}, ptr [[T5]]
79 // Actually capture the value.
80 // CHECK-NEXT: [[T6:%.*]] = getelementptr inbounds [[WEAK_T]], ptr [[WEAKX]], i32 0, i32 6
81 // CHECK-NEXT: [[CAPTURE:%.*]] = load ptr, ptr [[X]]
82 // CHECK-NEXT: store ptr [[CAPTURE]], ptr [[T6]]
84 // Then we initialize the block, blah blah blah.
85 // CHECK: call void @test2_helper(
87 // Finally, kill the variable with BLOCK_FIELD_IS_BYREF.
88 // CHECK: call void @_Block_object_dispose(ptr [[WEAKX]], i32 24)
90 __attribute__((objc_gc(weak))) __block Test2 *weakX = x;
91 test2_helper(^{ [weakX destroy]; });
94 // In the test above, check that the use in the invocation function
95 // doesn't require a read barrier.
96 // CHECK-LABEL: define internal void @__test2_block_invoke
97 // CHECK: [[T0:%.*]] = getelementptr inbounds [[BLOCK_T]], ptr {{%.*}}, i32 0, i32 5
98 // CHECK-NEXT: [[T1:%.*]] = load ptr, ptr [[T0]]
99 // CHECK-NEXT: [[T3:%.*]] = getelementptr inbounds [[WEAK_T]]{{.*}}, ptr [[T1]], i32 0, i32 1
100 // CHECK-NEXT: [[T4:%.*]] = load ptr, ptr [[T3]]
101 // CHECK-NEXT: [[WEAKX:%.*]] = getelementptr inbounds [[WEAK_T]]{{.*}}, ptr [[T4]], i32 0, i32 6
102 // CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[WEAKX]], align 4
104 // Make sure that ... is appropriately positioned in a block call.
105 void test3(void (^block)(int, ...)) {
108 // CHECK-LABEL: define{{.*}} void @test3(
109 // CHECK: [[BLOCK:%.*]] = alloca ptr, align 4
110 // CHECK-NEXT: store ptr
111 // CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[BLOCK]], align 4
112 // CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds [[BLOCK_T:%.*]], ptr [[T0]], i32 0, i32 3
113 // CHECK-NEXT: [[T4:%.*]] = load ptr, ptr [[T2]]
114 // CHECK-NEXT: call void (ptr, i32, ...) [[T4]](ptr noundef [[T0]], i32 noundef 0, i32 noundef 1, i32 noundef 2, i32 noundef 3)
115 // CHECK-NEXT: ret void
117 void test4(void (^block)()) {
120 // CHECK-LABEL: define{{.*}} void @test4(
121 // CHECK: [[BLOCK:%.*]] = alloca ptr, align 4
122 // CHECK-NEXT: store ptr
123 // CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[BLOCK]], align 4
124 // CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds [[BLOCK_T:%.*]], ptr [[T0]], i32 0, i32 3
125 // CHECK-NEXT: [[T4:%.*]] = load ptr, ptr [[T2]]
126 // CHECK-NEXT: call void [[T4]](ptr noundef [[T0]], i32 noundef 0, i32 noundef 1, i32 noundef 2, i32 noundef 3)
127 // CHECK-NEXT: ret void
130 __unsafe_unretained A *t = a;
131 gb0 = ^{ (void)a; (void)t; };
134 // CHECK-LABEL: define void @test5(
135 // CHECK: %[[V0:.*]] = getelementptr inbounds <{ ptr, i32, i32, ptr, ptr, ptr, ptr }>, ptr %{{.*}}, i32 0, i32 4
136 // CHECK: store ptr @[[BLOCK_DESCRIPTOR0]], ptr %[[V0]],
138 void test6(id a, long long b) {
139 void (^block)() = ^{ (void)b; (void)a; };
142 // Check that the block literal doesn't have two fields for capture 'a'.
144 // CHECK-LABEL: define void @test6(
145 // CHECK: alloca <{ ptr, i32, i32, ptr, ptr, ptr, i64 }>,