1 // RUN: %clang_cc1 -fblocks -emit-llvm %s -o %t
2 // RUN: FileCheck %s -input-file=%t -check-prefix=CHECK1
3 // RUN: FileCheck %s -input-file=%t -check-prefix=CHECK2
4
5 struct A {
6 int a;
7 float b;
8 char c;
9 };
10
test_nest_captured_stmt(int param,int size,int param_arr[size])11 void test_nest_captured_stmt(int param, int size, int param_arr[size]) {
12 int w;
13 int arr[param][size];
14 // CHECK1: %struct.anon{{.*}} = type { [[INT:i.+]]*, [[INT]]*, [[SIZE_TYPE:i.+]], [[INT]]**, [[INT]]*, [[SIZE_TYPE]], [[SIZE_TYPE]], [[INT]]* }
15 // CHECK1: %struct.anon{{.*}} = type { [[INT]]*, [[INT]]*, [[INT]]**, [[INT]]*, [[SIZE_TYPE]], [[INT]]**, [[INT]]*, [[SIZE_TYPE]], [[SIZE_TYPE]], [[INT]]* }
16 // CHECK1: [[T:%struct.anon.*]] = type { [[INT]]*, [[INT]]*, %struct.A*, [[INT]]**, [[INT]]*, [[SIZE_TYPE]], [[INT]]**, [[INT]]*, [[SIZE_TYPE]], [[SIZE_TYPE]], [[INT]]* }
17 #pragma clang __debug captured
18 {
19 int x;
20 int *y = &w;
21 #pragma clang __debug captured
22 {
23 struct A z;
24 #pragma clang __debug captured
25 {
26 w = x = z.a = 1;
27 *y = param;
28 z.b = 0.1f;
29 z.c = 'c';
30 param_arr[size - 1] = 2;
31 arr[10][z.a] = 12;
32
33 // CHECK1: define internal {{.*}}void @__captured_stmt{{.*}}([[T]]
34 // CHECK1: [[PARAM_ARR_SIZE_REF:%.+]] = getelementptr inbounds [[T]], [[T]]* {{.+}}, i{{[0-9]+}} 0, i{{[0-9]+}} 5
35 // CHECK1: [[PARAM_ARR_SIZE:%.+]] = load [[SIZE_TYPE]], [[SIZE_TYPE]]* [[PARAM_ARR_SIZE_REF]]
36 // CHECK1: [[ARR_SIZE1_REF:%.+]] = getelementptr inbounds [[T]], [[T]]* {{.+}}, i{{[0-9]+}} 0, i{{[0-9]+}} 8
37 // CHECK1: [[ARR_SIZE1:%.+]] = load [[SIZE_TYPE]], [[SIZE_TYPE]]* [[ARR_SIZE1_REF]]
38 // CHECK1: [[ARR_SIZE2_REF:%.+]] = getelementptr inbounds [[T]], [[T]]* {{.+}}, i{{[0-9]+}} 0, i{{[0-9]+}} 9
39 // CHECK1: [[ARR_SIZE2:%.+]] = load [[SIZE_TYPE]], [[SIZE_TYPE]]* [[ARR_SIZE2_REF]]
40 //
41 // CHECK1: getelementptr inbounds [[T]], [[T]]* {{.*}}, i{{[0-9]+}} 0, i{{[0-9]+}} 2
42 // CHECK1-NEXT: load %struct.A*, %struct.A**
43 // CHECK1-NEXT: getelementptr inbounds %struct.A, %struct.A*
44 // CHECK1-NEXT: store i{{.+}} 1
45 //
46 // CHECK1: getelementptr inbounds [[T]], [[T]]* {{.*}}, i{{[0-9]+}} 0, i{{[0-9]+}} 1
47 // CHECK1-NEXT: load i{{[0-9]+}}*, i{{[0-9]+}}**
48 // CHECK1-NEXT: store i{{[0-9]+}} 1
49 //
50 // CHECK1: getelementptr inbounds [[T]], [[T]]* {{.*}}, i{{[0-9]+}} 0, i{{[0-9]+}} 0
51 // CHECK1-NEXT: load i{{[0-9]+}}*, i{{[0-9]+}}**
52 // CHECK1-NEXT: store i{{[0-9]+}} 1
53 //
54 // CHECK1: getelementptr inbounds [[T]], [[T]]* {{.*}}, i{{[0-9]+}} 0, i{{[0-9]+}} 4
55 // CHECK1-NEXT: load i{{[0-9]+}}*, i{{[0-9]+}}**
56 // CHECK1-NEXT: load i{{[0-9]+}}, i{{[0-9]+}}*
57 // CHECK1-NEXT: getelementptr inbounds [[T]], [[T]]* {{.*}}, i{{[0-9]+}} 0, i{{[0-9]+}} 3
58 // CHECK1-NEXT: load i{{[0-9]+}}**, i{{[0-9]+}}***
59 // CHECK1-NEXT: load i{{[0-9]+}}*, i{{[0-9]+}}**
60 // CHECK1-NEXT: store i{{[0-9]+}}
61 //
62 // CHECK1: getelementptr inbounds [[T]], [[T]]* {{.*}}, i{{[0-9]+}} 0, i{{[0-9]+}} 2
63 // CHECK1-NEXT: load %struct.A*, %struct.A**
64 // CHECK1-NEXT: getelementptr inbounds %struct.A, %struct.A*
65 // CHECK1-NEXT: store float
66 //
67 // CHECK1: getelementptr inbounds [[T]], [[T]]* {{.*}}, i{{[0-9]+}} 0, i{{[0-9]+}} 2
68 // CHECK1-NEXT: load %struct.A*, %struct.A**
69 // CHECK1-NEXT: getelementptr inbounds %struct.A, %struct.A*
70 // CHECK1-NEXT: store i8 99
71 //
72 // CHECK1-DAG: [[SIZE_ADDR_REF:%.*]] = getelementptr inbounds [[T]], [[T]]* {{.*}}, i{{.+}} 0, i{{.+}} 7
73 // CHECK1-DAG: [[SIZE_ADDR:%.*]] = load i{{.+}}*, i{{.+}}** [[SIZE_ADDR_REF]]
74 // CHECK1-DAG: [[SIZE:%.*]] = load i{{.+}}, i{{.+}}* [[SIZE_ADDR]]
75 // CHECK1-DAG: [[PARAM_ARR_IDX:%.*]] = sub nsw i{{.+}} [[SIZE]], 1
76 // CHECK1-DAG: [[PARAM_ARR_ADDR_REF:%.*]] = getelementptr inbounds [[T]], [[T]]* {{.*}}, i{{.+}} 0, i{{.+}} 6
77 // CHECK1-DAG: [[PARAM_ARR_ADDR:%.*]] = load i{{.+}}**, i{{.+}}*** [[PARAM_ARR_ADDR_REF]]
78 // CHECK1-DAG: [[PARAM_ARR:%.*]] = load i{{.+}}*, i{{.+}}** [[PARAM_ARR_ADDR]]
79 // CHECK1-DAG: [[PARAM_ARR_SIZE_MINUS_1_ADDR:%.*]] = getelementptr inbounds i{{.+}}, i{{.+}}* [[PARAM_ARR]], i{{.*}}
80 // CHECK1: store i{{.+}} 2, i{{.+}}* [[PARAM_ARR_SIZE_MINUS_1_ADDR]]
81 //
82 // CHECK1-DAG: [[Z_ADDR_REF:%.*]] = getelementptr inbounds [[T]], [[T]]* {{.*}}, i{{.+}} 0, i{{.+}} 2
83 // CHECK1-DAG: [[Z_ADDR:%.*]] = load %struct.A*, %struct.A** [[Z_ADDR_REF]]
84 // CHECK1-DAG: [[Z_A_ADDR:%.*]] = getelementptr inbounds %struct.A, %struct.A* [[Z_ADDR]], i{{.+}} 0, i{{.+}} 0
85 // CHECK1-DAG: [[ARR_IDX_2:%.*]] = load i{{.+}}, i{{.+}}* [[Z_A_ADDR]]
86 // CHECK1-DAG: [[ARR_ADDR_REF:%.*]] = getelementptr inbounds [[T]], [[T]]* {{.*}}, i{{.+}} 0, i{{.+}} 10
87 // CHECK1-DAG: [[ARR_ADDR:%.*]] = load i{{.+}}*, i{{.+}}** [[ARR_ADDR_REF]]
88 // CHECK1-DAG: [[ARR_IDX_1:%.*]] = mul {{.*}} 10
89 // CHECK1-DAG: [[ARR_10_ADDR:%.*]] = getelementptr inbounds i{{.+}}, i{{.+}}* [[ARR_ADDR]], i{{.*}} [[ARR_IDX_1]]
90 // CHECK1-DAG: [[ARR_10_Z_A_ADDR:%.*]] = getelementptr inbounds i{{.+}}, i{{.+}}* [[ARR_10_ADDR]], i{{.*}}
91 // CHECK1: store i{{.+}} 12, i{{.+}}* [[ARR_10_Z_A_ADDR]]
92 }
93 }
94 }
95 }
96
test_nest_block()97 void test_nest_block() {
98 __block int x;
99 int y;
100 ^{
101 int z;
102 x = z;
103 #pragma clang __debug captured
104 {
105 z = y; // OK
106 }
107 }();
108
109 // CHECK2: define internal {{.*}}void @{{.*}}test_nest_block_block_invoke
110 //
111 // CHECK2: [[Z:%[0-9a-z_]*]] = alloca i{{[0-9]+}},
112 // CHECK2: alloca %struct.anon{{.*}}
113 //
114 // CHECK2: store i{{[0-9]+}}
115 // CHECK2: store i{{[0-9]+}}* [[Z]]
116 //
117 // CHECK2: getelementptr inbounds %struct.anon
118 // CHECK2-NEXT: getelementptr inbounds
119 // CHECK2-NEXT: store i{{[0-9]+}}*
120 //
121 // CHECK2: call void @__captured_stmt
122
123 int a;
124 #pragma clang __debug captured
125 {
126 __block int b;
127 int c;
128 __block int d;
129 ^{
130 b = a;
131 b = c;
132 b = d;
133 }();
134 }
135
136 // CHECK2: alloca %struct.__block_byref_b
137 // CHECK2-NEXT: [[C:%[0-9a-z_]*]] = alloca i{{[0-9]+}}
138 // CHECK2-NEXT: alloca %struct.__block_byref_d
139 //
140 // CHECK2: bitcast %struct.__block_byref_b*
141 // CHECK2-NEXT: store i8*
142 //
143 // CHECK2: [[CapA:%[0-9a-z_.]*]] = getelementptr inbounds {{.*}}, i{{[0-9]+}} 0, i{{[0-9]+}} 7
144 //
145 // CHECK2: getelementptr inbounds %struct.anon{{.*}}, i{{[0-9]+}} 0, i{{[0-9]+}} 0
146 // CHECK2: load i{{[0-9]+}}*, i{{[0-9]+}}**
147 // CHECK2: load i{{[0-9]+}}, i{{[0-9]+}}*
148 // CHECK2: store i{{[0-9]+}} {{.*}}, i{{[0-9]+}}* [[CapA]]
149 //
150 // CHECK2: [[CapC:%[0-9a-z_.]*]] = getelementptr inbounds {{.*}}, i{{[0-9]+}} 0, i{{[0-9]+}} 8
151 // CHECK2-NEXT: [[Val:%[0-9a-z_]*]] = load i{{[0-9]+}}, i{{[0-9]+}}* [[C]]
152 // CHECK2-NEXT: store i{{[0-9]+}} [[Val]], i{{[0-9]+}}* [[CapC]]
153 //
154 // CHECK2: bitcast %struct.__block_byref_d*
155 // CHECK2-NEXT: store i8*
156 }
157