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