1 // RUN: %clang_cc1 -triple i386-unknown-unknown %s -emit-llvm -o - -fblocks | FileCheck %s
2
3 // CHECK: %[[STRUCT_BLOCK_DESCRIPTOR:.*]] = type { i32, i32 }
4
5 // CHECK: @{{.*}} = internal constant { i32, i32, i8*, i8*, i8*, i8* } { i32 0, i32 24, i8* bitcast (void (i8*, i8*)* @__copy_helper_block_4_20r to i8*), i8* bitcast (void (i8*)* @__destroy_helper_block_4_20r to i8*), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @{{.*}}, i32 0, i32 0), i8* null }, align 4
6 // CHECK: @[[BLOCK_DESCRIPTOR_TMP21:.*]] = internal constant { i32, i32, i8*, i8*, i8*, i8* } { i32 0, i32 24, i8* bitcast (void (i8*, i8*)* @__copy_helper_block_4_20r to i8*), i8* bitcast (void (i8*)* @__destroy_helper_block_4_20r to i8*), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @{{.*}}, i32 0, i32 0), i8* null }, align 4
7
8 void (^f)(void) = ^{};
9
10 // rdar://6768379
11 int f0(int (^a0)()) {
12 return a0(1, 2, 3);
13 }
14
15 // Verify that attributes on blocks are set correctly.
16 typedef struct s0 T;
17 struct s0 {
18 int a[64];
19 };
20
21 // CHECK: define internal void @__f2_block_invoke(%struct.s0* noalias sret(%struct.s0) align 4 {{%.*}}, i8* {{%.*}}, %struct.s0* byval(%struct.s0) align 4 {{.*}})
f2(struct s0 a0)22 struct s0 f2(struct s0 a0) {
23 return ^(struct s0 a1){ return a1; }(a0);
24 }
25
26 // This should not crash: rdar://6808051
27 void *P = ^{
28 void *Q = __func__;
29 };
30
31 void (^test1)(void) = ^(void) {
32 __block int i;
33 ^ { i = 1; }();
34 };
35
36 // CHECK-LABEL: define linkonce_odr hidden void @__copy_helper_block_4_20r(i8* %0, i8* %1) unnamed_addr
37 // CHECK: %[[_ADDR:.*]] = alloca i8*, align 4
38 // CHECK-NEXT: %[[_ADDR1:.*]] = alloca i8*, align 4
39 // CHECK-NEXT: store i8* %0, i8** %[[_ADDR]], align 4
40 // CHECK-NEXT: store i8* %1, i8** %[[_ADDR1]], align 4
41 // CHECK-NEXT: %[[V2:.*]] = load i8*, i8** %[[_ADDR1]], align 4
42 // CHECK-NEXT: %[[BLOCK_SOURCE:.*]] = bitcast i8* %[[V2]] to <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8* }>*
43 // CHECK-NEXT: %[[V3:.*]] = load i8*, i8** %[[_ADDR]], align 4
44 // CHECK-NEXT: %[[BLOCK_DEST:.*]] = bitcast i8* %[[V3]] to <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8* }>*
45 // CHECK-NEXT: %[[V4:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8* }>, <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8* }>* %[[BLOCK_SOURCE]], i32 0, i32 5
46 // CHECK-NEXT: %[[V5:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8* }>, <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8* }>* %[[BLOCK_DEST]], i32 0, i32 5
47 // CHECK-NEXT: %[[BLOCKCOPY_SRC:.*]] = load i8*, i8** %[[V4]], align 4
48 // CHECK-NEXT: %[[V6:.*]] = bitcast i8** %[[V5]] to i8*
49 // CHECK-NEXT: call void @_Block_object_assign(i8* %[[V6]], i8* %[[BLOCKCOPY_SRC]], i32 8)
50 // CHECK-NEXT: ret void
51
52 // CHECK-LABEL: define linkonce_odr hidden void @__destroy_helper_block_4_20r(i8* %0) unnamed_addr
53 // CHECK: %[[_ADDR:.*]] = alloca i8*, align 4
54 // CHECK-NEXT: store i8* %0, i8** %[[_ADDR]], align 4
55 // CHECK-NEXT: %[[V1:.*]] = load i8*, i8** %[[_ADDR]], align 4
56 // CHECK-NEXT: %[[BLOCK:.*]] = bitcast i8* %[[V1]] to <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8* }>*
57 // CHECK-NEXT: %[[V2:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8* }>, <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8* }>* %[[BLOCK]], i32 0, i32 5
58 // CHECK-NEXT: %[[V3:.*]] = load i8*, i8** %[[V2]], align 4
59 // CHECK-NEXT: call void @_Block_object_dispose(i8* %[[V3]], i32 8)
60 // CHECK-NEXT: ret void
61
62 typedef double ftype(double);
63 // It's not clear that we *should* support this syntax, but until that decision
64 // is made, we should support it properly and not crash.
65 ftype ^test2 = ^ftype {
66 return 0;
67 };
68
69 // rdar://problem/8605032
70 void f3_helper(void (^)(void));
f3()71 void f3() {
72 _Bool b = 0;
73 f3_helper(^{ if (b) {} });
74 }
75
76 // rdar://problem/11322251
77 // The bool can fill in between the header and the long long.
78 // Add the appropriate amount of padding between them.
79 void f4_helper(long long (^)(void));
80 // CHECK-LABEL: define void @f4()
f4(void)81 void f4(void) {
82 _Bool b = 0;
83 long long ll = 0;
84 // CHECK: alloca <{ i8*, i32, i32, i8*, {{%.*}}*, i8, [3 x i8], i64 }>, align 8
85 f4_helper(^{ if (b) return ll; return 0LL; });
86 }
87
88 // rdar://problem/11354538
89 // The alignment after rounding up to the align of F5 is actually
90 // greater than the required alignment. Don't assert.
91 struct F5 {
92 char buffer[32] __attribute((aligned));
93 };
94 void f5_helper(void (^)(struct F5 *));
95 // CHECK-LABEL: define void @f5()
f5(void)96 void f5(void) {
97 struct F5 value;
98 // CHECK: alloca <{ i8*, i32, i32, i8*, {{%.*}}*, [12 x i8], [[F5:%.*]] }>, align 16
99 f5_helper(^(struct F5 *slot) { *slot = value; });
100 }
101
102 // rdar://14085217
103 void (^b)() = ^{};
main()104 int main() {
105 (b?: ^{})();
106 }
107 // CHECK: [[ZERO:%.*]] = load void (...)*, void (...)** @b
108 // CHECK-NEXT: [[TB:%.*]] = icmp ne void (...)* [[ZERO]], null
109 // CHECK-NEXT: br i1 [[TB]], label [[CT:%.*]], label [[CF:%.*]]
110 // CHECK: [[ONE:%.*]] = bitcast void (...)* [[ZERO]] to void ()*
111 // CHECK-NEXT: br label [[CE:%.*]]
112
113 // Ensure that we don't emit helper code in copy/dispose routines for variables
114 // that are const-captured.
testConstCaptureInCopyAndDestroyHelpers()115 void testConstCaptureInCopyAndDestroyHelpers() {
116 const int x = 0;
117 __block int i;
118 (^ { i = x; })();
119 }
120 // CHECK-LABEL: define void @testConstCaptureInCopyAndDestroyHelpers(
121 // CHECK: %[[BLOCK_DESCRIPTOR:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8* }>, <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8* }>* %{{.*}}, i32 0, i32 4
122 // CHECK: store %[[STRUCT_BLOCK_DESCRIPTOR]]* bitcast ({ i32, i32, i8*, i8*, i8*, i8* }* @[[BLOCK_DESCRIPTOR_TMP21]] to %[[STRUCT_BLOCK_DESCRIPTOR]]*), %[[STRUCT_BLOCK_DESCRIPTOR]]** %[[BLOCK_DESCRIPTOR]], align 4
123
124 // CHECK-LABEL: define internal void @__testConstCaptureInCopyAndDestroyHelpers_block_invoke
125