1 // RUN: %clang_cc1 -verify -fopenmp=libiomp5 -x c++ -triple x86_64-apple-darwin10 -emit-llvm %s -o - | FileCheck %s
2 // RUN: %clang_cc1 -fopenmp=libiomp5 -x c++ -std=c++11 -triple x86_64-apple-darwin10 -emit-pch -o %t %s
3 // RUN: %clang_cc1 -fopenmp=libiomp5 -x c++ -triple x86_64-apple-darwin10 -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s
4 // RUN: %clang_cc1 -verify -fopenmp=libiomp5 -x c++ -std=c++11 -DLAMBDA -triple x86_64-apple-darwin10 -emit-llvm %s -o - | FileCheck -check-prefix=LAMBDA %s
5 // RUN: %clang_cc1 -verify -fopenmp=libiomp5 -x c++ -fblocks -DBLOCKS -triple x86_64-apple-darwin10 -emit-llvm %s -o - | FileCheck -check-prefix=BLOCKS %s
6 // expected-no-diagnostics
7 #ifndef HEADER
8 #define HEADER
9 
10 struct St {
11   int a, b;
StSt12   St() : a(0), b(0) {}
StSt13   St(const St &st) : a(st.a + st.b), b(0) {}
~StSt14   ~St() {}
15 };
16 
17 volatile int g = 1212;
18 
19 template <class T>
20 struct S {
21   T f;
SS22   S(T a) : f(a + g) {}
SS23   S() : f(g) {}
SS24   S(const S &s, St t = St()) : f(s.f + t.a) {}
operator TS25   operator T() { return T(); }
~SS26   ~S() {}
27 };
28 
29 // CHECK-DAG: [[S_FLOAT_TY:%.+]] = type { float }
30 // CHECK-DAG: [[S_INT_TY:%.+]] = type { i{{[0-9]+}} }
31 // CHECK-DAG: [[ST_TY:%.+]] = type { i{{[0-9]+}}, i{{[0-9]+}} }
32 // CHECK-DAG: [[CAP_TMAIN_TY:%.+]] = type { i{{[0-9]+}}*, [2 x i{{[0-9]+}}]*, [2 x [[S_INT_TY]]]*, [[S_INT_TY]]* }
33 
34 template <typename T>
tmain()35 T tmain() {
36   S<T> test;
37   T t_var = T();
38   T vec[] = {1, 2};
39   S<T> s_arr[] = {1, 2};
40   S<T> var(3);
41 #pragma omp parallel
42 #pragma omp for firstprivate(t_var, vec, s_arr, var)
43   for (int i = 0; i < 0; ++i) {
44     vec[i] = t_var;
45     s_arr[i] = var;
46   }
47   return T();
48 }
49 
50 // CHECK: [[TEST:@.+]] = global [[S_FLOAT_TY]] zeroinitializer,
51 S<float> test;
52 // CHECK-DAG: [[T_VAR:@.+]] = global i{{[0-9]+}} 333,
53 int t_var = 333;
54 // CHECK-DAG: [[VEC:@.+]] = global [2 x i{{[0-9]+}}] [i{{[0-9]+}} 1, i{{[0-9]+}} 2],
55 int vec[] = {1, 2};
56 // CHECK-DAG: [[S_ARR:@.+]] = global [2 x [[S_FLOAT_TY]]] zeroinitializer,
57 S<float> s_arr[] = {1, 2};
58 // CHECK-DAG: [[VAR:@.+]] = global [[S_FLOAT_TY]] zeroinitializer,
59 S<float> var(3);
60 // CHECK-DAG: [[IMPLICIT_BARRIER_LOC:@.+]] = private unnamed_addr constant %{{.+}} { i32 0, i32 66, i32 0, i32 0, i8*
61 
62 // CHECK: call {{.*}} [[S_FLOAT_TY_DEF_CONSTR:@.+]]([[S_FLOAT_TY]]* [[TEST]])
63 // CHECK: ([[S_FLOAT_TY]]*)* [[S_FLOAT_TY_DESTR:@[^ ]+]] {{[^,]+}}, {{.+}}([[S_FLOAT_TY]]* [[TEST]]
main()64 int main() {
65 #ifdef LAMBDA
66   // LAMBDA: [[G:@.+]] = global i{{[0-9]+}} 1212,
67   // LAMBDA-LABEL: @main
68   // LAMBDA: call void [[OUTER_LAMBDA:@.+]](
69   [&]() {
70 // LAMBDA: define{{.*}} internal{{.*}} void [[OUTER_LAMBDA]](
71 // LAMBDA: call void {{.+}} @__kmpc_fork_call({{.+}}, i32 1, {{.+}}* [[OMP_REGION:@.+]] to {{.+}}, i8* %{{.+}})
72 #pragma omp parallel
73 #pragma omp for firstprivate(g)
74   for (int i = 0; i < 2; ++i) {
75     // LAMBDA: define{{.*}} internal{{.*}} void [[OMP_REGION]](i32* %{{.+}}, i32* %{{.+}}, %{{.+}}* [[ARG:%.+]])
76     // Skip temp vars for loop
77     // LAMBDA: alloca i{{[0-9]+}},
78     // LAMBDA: alloca i{{[0-9]+}},
79     // LAMBDA: alloca i{{[0-9]+}},
80     // LAMBDA: alloca i{{[0-9]+}},
81     // LAMBDA: alloca i{{[0-9]+}},
82     // LAMBDA: [[G_PRIVATE_ADDR:%.+]] = alloca i{{[0-9]+}},
83     // LAMBDA: [[G_VAL:%.+]] = load volatile i{{[0-9]+}}, i{{[0-9]+}}* [[G]]
84     // LAMBDA: store i{{[0-9]+}} [[G_VAL]], i{{[0-9]+}}* [[G_PRIVATE_ADDR]]
85     // LAMBDA: call i32 @__kmpc_cancel_barrier(
86     g = 1;
87     // LAMBDA: call void @__kmpc_for_static_init_4(
88     // LAMBDA: store volatile i{{[0-9]+}} 1, i{{[0-9]+}}* [[G_PRIVATE_ADDR]],
89     // LAMBDA: [[G_PRIVATE_ADDR_REF:%.+]] = getelementptr inbounds %{{.+}}, %{{.+}}* [[ARG:%.+]], i{{[0-9]+}} 0, i{{[0-9]+}} 0
90     // LAMBDA: store i{{[0-9]+}}* [[G_PRIVATE_ADDR]], i{{[0-9]+}}** [[G_PRIVATE_ADDR_REF]]
91     // LAMBDA: call void [[INNER_LAMBDA:@.+]](%{{.+}}* [[ARG]])
92     // LAMBDA: call void @__kmpc_for_static_fini(
93     // LAMBDA: call i32 @__kmpc_cancel_barrier(
94     [&]() {
95       // LAMBDA: define {{.+}} void [[INNER_LAMBDA]](%{{.+}}* [[ARG_PTR:%.+]])
96       // LAMBDA: store %{{.+}}* [[ARG_PTR]], %{{.+}}** [[ARG_PTR_REF:%.+]],
97       g = 2;
98       // LAMBDA: [[ARG_PTR:%.+]] = load %{{.+}}*, %{{.+}}** [[ARG_PTR_REF]]
99       // LAMBDA: [[G_PTR_REF:%.+]] = getelementptr inbounds %{{.+}}, %{{.+}}* [[ARG_PTR]], i{{[0-9]+}} 0, i{{[0-9]+}} 0
100       // LAMBDA: [[G_REF:%.+]] = load i{{[0-9]+}}*, i{{[0-9]+}}** [[G_PTR_REF]]
101       // LAMBDA: store volatile i{{[0-9]+}} 2, i{{[0-9]+}}* [[G_REF]]
102     }();
103   }
104   }();
105   return 0;
106 #elif defined(BLOCKS)
107   // BLOCKS: [[G:@.+]] = global i{{[0-9]+}} 1212,
108   // BLOCKS-LABEL: @main
109   // BLOCKS: call void {{%.+}}(i8
110   ^{
111 // BLOCKS: define{{.*}} internal{{.*}} void {{.+}}(i8*
112 // BLOCKS: call void {{.+}} @__kmpc_fork_call({{.+}}, i32 1, {{.+}}* [[OMP_REGION:@.+]] to {{.+}}, i8* %{{.+}})
113 #pragma omp parallel
114 #pragma omp for firstprivate(g)
115   for (int i = 0; i < 2; ++i) {
116     // BLOCKS: define{{.*}} internal{{.*}} void [[OMP_REGION]](i32* %{{.+}}, i32* %{{.+}}, %{{.+}}* [[ARG:%.+]])
117     // Skip temp vars for loop
118     // BLOCKS: alloca i{{[0-9]+}},
119     // BLOCKS: alloca i{{[0-9]+}},
120     // BLOCKS: alloca i{{[0-9]+}},
121     // BLOCKS: alloca i{{[0-9]+}},
122     // BLOCKS: alloca i{{[0-9]+}},
123     // BLOCKS: [[G_PRIVATE_ADDR:%.+]] = alloca i{{[0-9]+}},
124     // BLOCKS: [[G_VAL:%.+]] = load volatile i{{[0-9]+}}, i{{[0-9]+}}* [[G]]
125     // BLOCKS: store i{{[0-9]+}} [[G_VAL]], i{{[0-9]+}}* [[G_PRIVATE_ADDR]]
126     // BLOCKS: call i32 @__kmpc_cancel_barrier(
127     g = 1;
128     // BLOCKS: call void @__kmpc_for_static_init_4(
129     // BLOCKS: store volatile i{{[0-9]+}} 1, i{{[0-9]+}}* [[G_PRIVATE_ADDR]],
130     // BLOCKS-NOT: [[G]]{{[[^:word:]]}}
131     // BLOCKS: i{{[0-9]+}}* [[G_PRIVATE_ADDR]]
132     // BLOCKS-NOT: [[G]]{{[[^:word:]]}}
133     // BLOCKS: call void {{%.+}}(i8
134     // BLOCKS: call void @__kmpc_for_static_fini(
135     // BLOCKS: call i32 @__kmpc_cancel_barrier(
136     ^{
137       // BLOCKS: define {{.+}} void {{@.+}}(i8*
138       g = 2;
139       // BLOCKS-NOT: [[G]]{{[[^:word:]]}}
140       // BLOCKS: store volatile i{{[0-9]+}} 2, i{{[0-9]+}}*
141       // BLOCKS-NOT: [[G]]{{[[^:word:]]}}
142       // BLOCKS: ret
143     }();
144   }
145   }();
146   return 0;
147 #else
148 #pragma omp for firstprivate(t_var, vec, s_arr, var)
149   for (int i = 0; i < 0; ++i) {
150     vec[i] = t_var;
151     s_arr[i] = var;
152   }
153   return tmain<int>();
154 #endif
155 }
156 
157 // CHECK: define {{.*}}i{{[0-9]+}} @main()
158 // CHECK: alloca i{{[0-9]+}},
159 // Skip temp vars for loop
160 // CHECK: alloca i{{[0-9]+}},
161 // CHECK: alloca i{{[0-9]+}},
162 // CHECK: alloca i{{[0-9]+}},
163 // CHECK: alloca i{{[0-9]+}},
164 // CHECK: alloca i{{[0-9]+}},
165 // CHECK: [[T_VAR_PRIV:%.+]] = alloca i{{[0-9]+}},
166 // CHECK: [[VEC_PRIV:%.+]] = alloca [2 x i{{[0-9]+}}],
167 // CHECK: [[S_ARR_PRIV:%.+]] = alloca [2 x [[S_FLOAT_TY]]],
168 // CHECK: [[VAR_PRIV:%.+]] = alloca [[S_FLOAT_TY]],
169 // CHECK: [[GTID:%.+]] = call i32 @__kmpc_global_thread_num(
170 
171 // firstprivate t_var(t_var)
172 // CHECK: [[T_VAR_VAL:%.+]] = load i{{[0-9]+}}, i{{[0-9]+}}* [[T_VAR]],
173 // CHECK: store i{{[0-9]+}} [[T_VAR_VAL]], i{{[0-9]+}}* [[T_VAR_PRIV]],
174 
175 // firstprivate vec(vec)
176 // CHECK: [[VEC_DEST:%.+]] = bitcast [2 x i{{[0-9]+}}]* [[VEC_PRIV]] to i8*
177 // CHECK: call void @llvm.memcpy.{{.+}}(i8* [[VEC_DEST]], i8* bitcast ([2 x i{{[0-9]+}}]* [[VEC]] to i8*),
178 
179 // firstprivate s_arr(s_arr)
180 // CHECK: [[S_ARR_PRIV_BEGIN:%.+]] = getelementptr inbounds [2 x [[S_FLOAT_TY]]], [2 x [[S_FLOAT_TY]]]* [[S_ARR_PRIV]], i{{[0-9]+}} 0, i{{[0-9]+}} 0
181 // CHECK: [[S_ARR_PRIV_END:%.+]] = getelementptr [[S_FLOAT_TY]], [[S_FLOAT_TY]]* [[S_ARR_PRIV_BEGIN]], i{{[0-9]+}} 2
182 // CHECK: [[IS_EMPTY:%.+]] = icmp eq [[S_FLOAT_TY]]* [[S_ARR_PRIV_BEGIN]], [[S_ARR_PRIV_END]]
183 // CHECK: br i1 [[IS_EMPTY]], label %[[S_ARR_BODY_DONE:.+]], label %[[S_ARR_BODY:.+]]
184 // CHECK: [[S_ARR_BODY]]
185 // CHECK: getelementptr inbounds ([2 x [[S_FLOAT_TY]]], [2 x [[S_FLOAT_TY]]]* [[S_ARR]], i{{[0-9]+}} 0, i{{[0-9]+}} 0)
186 // CHECK: call {{.*}} [[ST_TY_DEFAULT_CONSTR:@.+]]([[ST_TY]]* [[ST_TY_TEMP:%.+]])
187 // CHECK: call {{.*}} [[S_FLOAT_TY_COPY_CONSTR:@.+]]([[S_FLOAT_TY]]* {{.+}}, [[S_FLOAT_TY]]* {{.+}}, [[ST_TY]]* [[ST_TY_TEMP]])
188 // CHECK: call {{.*}} [[ST_TY_DESTR:@.+]]([[ST_TY]]* [[ST_TY_TEMP]])
189 // CHECK: br i1 {{.+}}, label %{{.+}}, label %[[S_ARR_BODY]]
190 
191 // firstprivate var(var)
192 // CHECK: call {{.*}} [[ST_TY_DEFAULT_CONSTR]]([[ST_TY]]* [[ST_TY_TEMP:%.+]])
193 // CHECK: call {{.*}} [[S_FLOAT_TY_COPY_CONSTR]]([[S_FLOAT_TY]]* [[VAR_PRIV]], [[S_FLOAT_TY]]* {{.*}} [[VAR]], [[ST_TY]]* [[ST_TY_TEMP]])
194 // CHECK: call {{.*}} [[ST_TY_DESTR]]([[ST_TY]]* [[ST_TY_TEMP]])
195 
196 // Synchronization for initialization.
197 // CHECK: call i32 @__kmpc_cancel_barrier(%{{.+}}* [[IMPLICIT_BARRIER_LOC]], i{{[0-9]+}} [[GTID]])
198 
199 // CHECK: call void @__kmpc_for_static_init_4(
200 // CHECK: call void @__kmpc_for_static_fini(
201 
202 // ~(firstprivate var), ~(firstprivate s_arr)
203 // CHECK-DAG: call {{.*}} [[S_FLOAT_TY_DESTR]]([[S_FLOAT_TY]]* [[VAR_PRIV]])
204 // CHECK-DAG: call {{.*}} [[S_FLOAT_TY_DESTR]]([[S_FLOAT_TY]]*
205 // CHECK: call i32 @__kmpc_cancel_barrier(%{{.+}}* [[IMPLICIT_BARRIER_LOC]], i{{[0-9]+}} [[GTID]])
206 
207 // CHECK: = call {{.*}}i{{.+}} [[TMAIN_INT:@.+]]()
208 
209 // CHECK: ret void
210 
211 // CHECK: define {{.*}} i{{[0-9]+}} [[TMAIN_INT]]()
212 // CHECK: [[TEST:%.+]] = alloca [[S_INT_TY]],
213 // CHECK: call {{.*}} [[S_INT_TY_DEF_CONSTR:@.+]]([[S_INT_TY]]* [[TEST]])
214 // CHECK: call void (%{{.+}}*, i{{[0-9]+}}, void (i{{[0-9]+}}*, i{{[0-9]+}}*, ...)*, ...) @__kmpc_fork_call(%{{.+}}* @{{.+}}, i{{[0-9]+}} 1, void (i{{[0-9]+}}*, i{{[0-9]+}}*, ...)* bitcast (void (i{{[0-9]+}}*, i{{[0-9]+}}*, [[CAP_TMAIN_TY]]*)* [[TMAIN_MICROTASK:@.+]] to void
215 // CHECK: call {{.*}} [[S_INT_TY_DESTR:@.+]]([[S_INT_TY]]*
216 // CHECK: ret
217 //
218 // CHECK: define internal void [[TMAIN_MICROTASK]](i{{[0-9]+}}* [[GTID_ADDR:%.+]], i{{[0-9]+}}* %{{.+}}, [[CAP_TMAIN_TY]]* %{{.+}})
219 // Skip temp vars for loop
220 // CHECK: alloca i{{[0-9]+}},
221 // CHECK: alloca i{{[0-9]+}},
222 // CHECK: alloca i{{[0-9]+}},
223 // CHECK: alloca i{{[0-9]+}},
224 // CHECK: alloca i{{[0-9]+}},
225 // CHECK: [[T_VAR_PRIV:%.+]] = alloca i{{[0-9]+}},
226 // CHECK: [[VEC_PRIV:%.+]] = alloca [2 x i{{[0-9]+}}],
227 // CHECK: [[S_ARR_PRIV:%.+]] = alloca [2 x [[S_INT_TY]]],
228 // CHECK: [[VAR_PRIV:%.+]] = alloca [[S_INT_TY]],
229 // CHECK: store i{{[0-9]+}}* [[GTID_ADDR]], i{{[0-9]+}}** [[GTID_ADDR_ADDR:%.+]],
230 
231 // firstprivate t_var(t_var)
232 // CHECK: [[T_VAR_PTR_REF:%.+]] = getelementptr inbounds [[CAP_TMAIN_TY]], [[CAP_TMAIN_TY]]* %{{.+}}, i{{[0-9]+}} 0, i{{[0-9]+}} 0
233 // CHECK: [[T_VAR_REF:%.+]] = load i{{[0-9]+}}*, i{{[0-9]+}}** [[T_VAR_PTR_REF]],
234 // CHECK: [[T_VAR_VAL:%.+]] = load i{{[0-9]+}}, i{{[0-9]+}}* [[T_VAR_REF]],
235 // CHECK: store i{{[0-9]+}} [[T_VAR_VAL]], i{{[0-9]+}}* [[T_VAR_PRIV]],
236 
237 // firstprivate vec(vec)
238 // CHECK: [[VEC_PTR_REF:%.+]] = getelementptr inbounds [[CAP_TMAIN_TY]], [[CAP_TMAIN_TY]]* %{{.+}}, i{{[0-9]+}} 0, i{{[0-9]+}} 1
239 // CHECK: [[VEC_REF:%.+]] = load [2 x i{{[0-9]+}}]*, [2 x i{{[0-9]+}}]** [[VEC_PTR_REF:%.+]],
240 // CHECK: [[VEC_DEST:%.+]] = bitcast [2 x i{{[0-9]+}}]* [[VEC_PRIV]] to i8*
241 // CHECK: [[VEC_SRC:%.+]] = bitcast [2 x i{{[0-9]+}}]* [[VEC_REF]] to i8*
242 // CHECK: call void @llvm.memcpy.{{.+}}(i8* [[VEC_DEST]], i8* [[VEC_SRC]],
243 
244 // firstprivate s_arr(s_arr)
245 // CHECK: [[S_ARR_REF:%.+]] = getelementptr inbounds [[CAP_TMAIN_TY]], [[CAP_TMAIN_TY]]* %{{.+}}, i{{[0-9]+}} 0, i{{[0-9]+}} 2
246 // CHECK: [[S_ARR:%.+]] = load [2 x [[S_INT_TY]]]*, [2 x [[S_INT_TY]]]** [[S_ARR_REF]],
247 // CHECK: [[S_ARR_PRIV_BEGIN:%.+]] = getelementptr inbounds [2 x [[S_INT_TY]]], [2 x [[S_INT_TY]]]* [[S_ARR_PRIV]], i{{[0-9]+}} 0, i{{[0-9]+}} 0
248 // CHECK: [[S_ARR_PRIV_END:%.+]] = getelementptr [[S_INT_TY]], [[S_INT_TY]]* [[S_ARR_PRIV_BEGIN]], i{{[0-9]+}} 2
249 // CHECK: [[IS_EMPTY:%.+]] = icmp eq [[S_INT_TY]]* [[S_ARR_PRIV_BEGIN]], [[S_ARR_PRIV_END]]
250 // CHECK: br i1 [[IS_EMPTY]], label %[[S_ARR_BODY_DONE:.+]], label %[[S_ARR_BODY:.+]]
251 // CHECK: [[S_ARR_BODY]]
252 // CHECK: call {{.*}} [[ST_TY_DEFAULT_CONSTR:@.+]]([[ST_TY]]* [[ST_TY_TEMP:%.+]])
253 // CHECK: call {{.*}} [[S_INT_TY_COPY_CONSTR:@.+]]([[S_INT_TY]]* {{.+}}, [[S_INT_TY]]* {{.+}}, [[ST_TY]]* [[ST_TY_TEMP]])
254 // CHECK: call {{.*}} [[ST_TY_DESTR:@.+]]([[ST_TY]]* [[ST_TY_TEMP]])
255 // CHECK: br i1 {{.+}}, label %{{.+}}, label %[[S_ARR_BODY]]
256 
257 // firstprivate var(var)
258 // CHECK: [[VAR_REF_PTR:%.+]] = getelementptr inbounds [[CAP_TMAIN_TY]], [[CAP_TMAIN_TY]]* %{{.+}}, i{{[0-9]+}} 0, i{{[0-9]+}} 3
259 // CHECK: [[VAR_REF:%.+]] = load [[S_INT_TY]]*, [[S_INT_TY]]** [[VAR_REF_PTR]],
260 // CHECK: call {{.*}} [[ST_TY_DEFAULT_CONSTR]]([[ST_TY]]* [[ST_TY_TEMP:%.+]])
261 // CHECK: call {{.*}} [[S_INT_TY_COPY_CONSTR]]([[S_INT_TY]]* [[VAR_PRIV]], [[S_INT_TY]]* {{.*}} [[VAR_REF]], [[ST_TY]]* [[ST_TY_TEMP]])
262 // CHECK: call {{.*}} [[ST_TY_DESTR]]([[ST_TY]]* [[ST_TY_TEMP]])
263 
264 // Synchronization for initialization.
265 // CHECK: [[GTID_REF:%.+]] = load i{{[0-9]+}}*, i{{[0-9]+}}** [[GTID_ADDR_ADDR]]
266 // CHECK: [[GTID:%.+]] = load i{{[0-9]+}}, i{{[0-9]+}}* [[GTID_REF]]
267 // CHECK: call i32 @__kmpc_cancel_barrier(%{{.+}}* [[IMPLICIT_BARRIER_LOC]], i{{[0-9]+}} [[GTID]])
268 
269 // CHECK: call void @__kmpc_for_static_init_4(
270 // CHECK: call void @__kmpc_for_static_fini(
271 
272 // ~(firstprivate var), ~(firstprivate s_arr)
273 // CHECK-DAG: call {{.*}} [[S_INT_TY_DESTR]]([[S_INT_TY]]* [[VAR_PRIV]])
274 // CHECK-DAG: call {{.*}} [[S_INT_TY_DESTR]]([[S_INT_TY]]*
275 // CHECK: [[GTID_REF:%.+]] = load i{{[0-9]+}}*, i{{[0-9]+}}** [[GTID_ADDR_ADDR]]
276 // CHECK: [[GTID:%.+]] = load i{{[0-9]+}}, i{{[0-9]+}}* [[GTID_REF]]
277 // CHECK: call i32 @__kmpc_cancel_barrier(%{{.+}}* [[IMPLICIT_BARRIER_LOC]], i{{[0-9]+}} [[GTID]])
278 // CHECK: ret void
279 #endif
280 
281