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