1a2223612SMichael Kruse // Check code generation
2532dc62bSNikita Popov // RUN: %clang_cc1 -no-opaque-pointers -verify -triple x86_64-pc-linux-gnu -fopenmp -fopenmp-version=51 -emit-llvm %s -o - | FileCheck %s --check-prefix=IR
3a2223612SMichael Kruse 
4a2223612SMichael Kruse // Check same results after serialization round-trip
5532dc62bSNikita Popov // RUN: %clang_cc1 -no-opaque-pointers -verify -triple x86_64-pc-linux-gnu -fopenmp -fopenmp-version=51 -emit-pch -o %t %s
6532dc62bSNikita Popov // RUN: %clang_cc1 -no-opaque-pointers -verify -triple x86_64-pc-linux-gnu -fopenmp -fopenmp-version=51 -include-pch %t -emit-llvm %s -o - | FileCheck %s --check-prefix=IR
7a2223612SMichael Kruse // expected-no-diagnostics
8a2223612SMichael Kruse 
9a2223612SMichael Kruse #ifndef HEADER
10a2223612SMichael Kruse #define HEADER
11a2223612SMichael Kruse 
12a2223612SMichael Kruse // placeholder for loop body code.
body(...)13a2223612SMichael Kruse extern "C" void body(...) {}
14a2223612SMichael Kruse 
15a2223612SMichael Kruse 
16a2223612SMichael Kruse // IR-LABEL: @func(
17a2223612SMichael Kruse // IR-NEXT:  [[ENTRY:.*]]:
18a2223612SMichael Kruse // IR-NEXT:    %[[START_ADDR:.+]] = alloca i32, align 4
19a2223612SMichael Kruse // IR-NEXT:    %[[END_ADDR:.+]] = alloca i32, align 4
20a2223612SMichael Kruse // IR-NEXT:    %[[STEP_ADDR:.+]] = alloca i32, align 4
21a2223612SMichael Kruse // IR-NEXT:    %[[I:.+]] = alloca i32, align 4
22a2223612SMichael Kruse // IR-NEXT:    %[[DOTOMP_IV:.+]] = alloca i32, align 4
23a2223612SMichael Kruse // IR-NEXT:    %[[TMP:.+]] = alloca i32, align 4
24a2223612SMichael Kruse // IR-NEXT:    %[[DOTCAPTURE_EXPR_:.+]] = alloca i32, align 4
25a2223612SMichael Kruse // IR-NEXT:    %[[DOTCAPTURE_EXPR_1:.+]] = alloca i32, align 4
26a2223612SMichael Kruse // IR-NEXT:    %[[DOTCAPTURE_EXPR_2:.+]] = alloca i32, align 4
27a2223612SMichael Kruse // IR-NEXT:    %[[DOTCAPTURE_EXPR_3:.+]] = alloca i32, align 4
28a2223612SMichael Kruse // IR-NEXT:    %[[DOTCAPTURE_EXPR_6:.+]] = alloca i32, align 4
29a2223612SMichael Kruse // IR-NEXT:    %[[DOTCAPTURE_EXPR_8:.+]] = alloca i32, align 4
30a2223612SMichael Kruse // IR-NEXT:    %[[DOTUNROLLED_IV_I:.+]] = alloca i32, align 4
31a2223612SMichael Kruse // IR-NEXT:    %[[DOTOMP_LB:.+]] = alloca i32, align 4
32a2223612SMichael Kruse // IR-NEXT:    %[[DOTOMP_UB:.+]] = alloca i32, align 4
33a2223612SMichael Kruse // IR-NEXT:    %[[DOTOMP_STRIDE:.+]] = alloca i32, align 4
34a2223612SMichael Kruse // IR-NEXT:    %[[DOTOMP_IS_LAST:.+]] = alloca i32, align 4
35a2223612SMichael Kruse // IR-NEXT:    %[[DOTUNROLLED_IV_I12:.+]] = alloca i32, align 4
36a2223612SMichael Kruse // IR-NEXT:    %[[DOTUNROLL_INNER_IV_I:.+]] = alloca i32, align 4
37a2223612SMichael Kruse // IR-NEXT:    %[[TMP0:.+]] = call i32 @__kmpc_global_thread_num(%struct.ident_t* @2)
38a2223612SMichael Kruse // IR-NEXT:    store i32 %[[START:.+]], i32* %[[START_ADDR]], align 4
39a2223612SMichael Kruse // IR-NEXT:    store i32 %[[END:.+]], i32* %[[END_ADDR]], align 4
40a2223612SMichael Kruse // IR-NEXT:    store i32 %[[STEP:.+]], i32* %[[STEP_ADDR]], align 4
41a2223612SMichael Kruse // IR-NEXT:    %[[TMP1:.+]] = load i32, i32* %[[START_ADDR]], align 4
42a2223612SMichael Kruse // IR-NEXT:    store i32 %[[TMP1]], i32* %[[DOTCAPTURE_EXPR_]], align 4
43a2223612SMichael Kruse // IR-NEXT:    %[[TMP2:.+]] = load i32, i32* %[[END_ADDR]], align 4
44a2223612SMichael Kruse // IR-NEXT:    store i32 %[[TMP2]], i32* %[[DOTCAPTURE_EXPR_1]], align 4
45a2223612SMichael Kruse // IR-NEXT:    %[[TMP3:.+]] = load i32, i32* %[[STEP_ADDR]], align 4
46a2223612SMichael Kruse // IR-NEXT:    store i32 %[[TMP3]], i32* %[[DOTCAPTURE_EXPR_2]], align 4
47a2223612SMichael Kruse // IR-NEXT:    %[[TMP4:.+]] = load i32, i32* %[[DOTCAPTURE_EXPR_1]], align 4
48a2223612SMichael Kruse // IR-NEXT:    %[[TMP5:.+]] = load i32, i32* %[[DOTCAPTURE_EXPR_]], align 4
49a2223612SMichael Kruse // IR-NEXT:    %[[SUB:.+]] = sub i32 %[[TMP4]], %[[TMP5]]
50a2223612SMichael Kruse // IR-NEXT:    %[[SUB4:.+]] = sub i32 %[[SUB]], 1
51a2223612SMichael Kruse // IR-NEXT:    %[[TMP6:.+]] = load i32, i32* %[[DOTCAPTURE_EXPR_2]], align 4
52a2223612SMichael Kruse // IR-NEXT:    %[[ADD:.+]] = add i32 %[[SUB4]], %[[TMP6]]
53a2223612SMichael Kruse // IR-NEXT:    %[[TMP7:.+]] = load i32, i32* %[[DOTCAPTURE_EXPR_2]], align 4
54a2223612SMichael Kruse // IR-NEXT:    %[[DIV:.+]] = udiv i32 %[[ADD]], %[[TMP7]]
55a2223612SMichael Kruse // IR-NEXT:    %[[SUB5:.+]] = sub i32 %[[DIV]], 1
56a2223612SMichael Kruse // IR-NEXT:    store i32 %[[SUB5]], i32* %[[DOTCAPTURE_EXPR_3]], align 4
57a2223612SMichael Kruse // IR-NEXT:    %[[TMP8:.+]] = load i32, i32* %[[DOTCAPTURE_EXPR_3]], align 4
58a2223612SMichael Kruse // IR-NEXT:    %[[ADD7:.+]] = add i32 %[[TMP8]], 1
59a2223612SMichael Kruse // IR-NEXT:    store i32 %[[ADD7]], i32* %[[DOTCAPTURE_EXPR_6]], align 4
60a2223612SMichael Kruse // IR-NEXT:    %[[TMP9:.+]] = load i32, i32* %[[DOTCAPTURE_EXPR_6]], align 4
61a2223612SMichael Kruse // IR-NEXT:    %[[SUB9:.+]] = sub i32 %[[TMP9]], -1
62a2223612SMichael Kruse // IR-NEXT:    %[[DIV10:.+]] = udiv i32 %[[SUB9]], 2
63a2223612SMichael Kruse // IR-NEXT:    %[[SUB11:.+]] = sub i32 %[[DIV10]], 1
64a2223612SMichael Kruse // IR-NEXT:    store i32 %[[SUB11]], i32* %[[DOTCAPTURE_EXPR_8]], align 4
65a2223612SMichael Kruse // IR-NEXT:    store i32 0, i32* %[[DOTUNROLLED_IV_I]], align 4
66a2223612SMichael Kruse // IR-NEXT:    %[[TMP10:.+]] = load i32, i32* %[[DOTCAPTURE_EXPR_6]], align 4
67a2223612SMichael Kruse // IR-NEXT:    %[[CMP:.+]] = icmp ult i32 0, %[[TMP10]]
68a2223612SMichael Kruse // IR-NEXT:    br i1 %[[CMP]], label %[[OMP_PRECOND_THEN:.+]], label %[[OMP_PRECOND_END:.+]]
69a2223612SMichael Kruse // IR-EMPTY:
70a2223612SMichael Kruse // IR-NEXT:  [[OMP_PRECOND_THEN]]:
71a2223612SMichael Kruse // IR-NEXT:    store i32 0, i32* %[[DOTOMP_LB]], align 4
72a2223612SMichael Kruse // IR-NEXT:    %[[TMP11:.+]] = load i32, i32* %[[DOTCAPTURE_EXPR_8]], align 4
73a2223612SMichael Kruse // IR-NEXT:    store i32 %[[TMP11]], i32* %[[DOTOMP_UB]], align 4
74a2223612SMichael Kruse // IR-NEXT:    store i32 1, i32* %[[DOTOMP_STRIDE]], align 4
75a2223612SMichael Kruse // IR-NEXT:    store i32 0, i32* %[[DOTOMP_IS_LAST]], align 4
76a2223612SMichael Kruse // IR-NEXT:    call void @__kmpc_for_static_init_4u(%struct.ident_t* @1, i32 %[[TMP0]], i32 34, i32* %[[DOTOMP_IS_LAST]], i32* %[[DOTOMP_LB]], i32* %[[DOTOMP_UB]], i32* %[[DOTOMP_STRIDE]], i32 1, i32 1)
77a2223612SMichael Kruse // IR-NEXT:    %[[TMP12:.+]] = load i32, i32* %[[DOTOMP_UB]], align 4
78a2223612SMichael Kruse // IR-NEXT:    %[[TMP13:.+]] = load i32, i32* %[[DOTCAPTURE_EXPR_8]], align 4
79a2223612SMichael Kruse // IR-NEXT:    %[[CMP13:.+]] = icmp ugt i32 %[[TMP12]], %[[TMP13]]
80a2223612SMichael Kruse // IR-NEXT:    br i1 %[[CMP13]], label %[[COND_TRUE:.+]], label %[[COND_FALSE:.+]]
81a2223612SMichael Kruse // IR-EMPTY:
82a2223612SMichael Kruse // IR-NEXT:  [[COND_TRUE]]:
83a2223612SMichael Kruse // IR-NEXT:    %[[TMP14:.+]] = load i32, i32* %[[DOTCAPTURE_EXPR_8]], align 4
84a2223612SMichael Kruse // IR-NEXT:    br label %[[COND_END:.+]]
85a2223612SMichael Kruse // IR-EMPTY:
86a2223612SMichael Kruse // IR-NEXT:  [[COND_FALSE]]:
87a2223612SMichael Kruse // IR-NEXT:    %[[TMP15:.+]] = load i32, i32* %[[DOTOMP_UB]], align 4
88a2223612SMichael Kruse // IR-NEXT:    br label %[[COND_END]]
89a2223612SMichael Kruse // IR-EMPTY:
90a2223612SMichael Kruse // IR-NEXT:  [[COND_END]]:
91a2223612SMichael Kruse // IR-NEXT:    %[[COND:.+]] = phi i32 [ %[[TMP14]], %[[COND_TRUE]] ], [ %[[TMP15]], %[[COND_FALSE]] ]
92a2223612SMichael Kruse // IR-NEXT:    store i32 %[[COND]], i32* %[[DOTOMP_UB]], align 4
93a2223612SMichael Kruse // IR-NEXT:    %[[TMP16:.+]] = load i32, i32* %[[DOTOMP_LB]], align 4
94a2223612SMichael Kruse // IR-NEXT:    store i32 %[[TMP16]], i32* %[[DOTOMP_IV]], align 4
95a2223612SMichael Kruse // IR-NEXT:    br label %[[OMP_INNER_FOR_COND:.+]]
96a2223612SMichael Kruse // IR-EMPTY:
97a2223612SMichael Kruse // IR-NEXT:  [[OMP_INNER_FOR_COND]]:
98a2223612SMichael Kruse // IR-NEXT:    %[[TMP17:.+]] = load i32, i32* %[[DOTOMP_IV]], align 4
99a2223612SMichael Kruse // IR-NEXT:    %[[TMP18:.+]] = load i32, i32* %[[DOTOMP_UB]], align 4
100a2223612SMichael Kruse // IR-NEXT:    %[[ADD14:.+]] = add i32 %[[TMP18]], 1
101a2223612SMichael Kruse // IR-NEXT:    %[[CMP15:.+]] = icmp ult i32 %[[TMP17]], %[[ADD14]]
102a2223612SMichael Kruse // IR-NEXT:    br i1 %[[CMP15]], label %[[OMP_INNER_FOR_BODY:.+]], label %[[OMP_INNER_FOR_END:.+]]
103a2223612SMichael Kruse // IR-EMPTY:
104a2223612SMichael Kruse // IR-NEXT:  [[OMP_INNER_FOR_BODY]]:
105a2223612SMichael Kruse // IR-NEXT:    %[[TMP19:.+]] = load i32, i32* %[[DOTOMP_IV]], align 4
106a2223612SMichael Kruse // IR-NEXT:    %[[MUL:.+]] = mul i32 %[[TMP19]], 2
107a2223612SMichael Kruse // IR-NEXT:    %[[ADD16:.+]] = add i32 0, %[[MUL]]
108a2223612SMichael Kruse // IR-NEXT:    store i32 %[[ADD16]], i32* %[[DOTUNROLLED_IV_I12]], align 4
109a2223612SMichael Kruse // IR-NEXT:    %[[TMP20:.+]] = load i32, i32* %[[DOTUNROLLED_IV_I12]], align 4
110a2223612SMichael Kruse // IR-NEXT:    store i32 %[[TMP20]], i32* %[[DOTUNROLL_INNER_IV_I]], align 4
111a2223612SMichael Kruse // IR-NEXT:    br label %[[FOR_COND:.+]]
112a2223612SMichael Kruse // IR-EMPTY:
113a2223612SMichael Kruse // IR-NEXT:  [[FOR_COND]]:
114a2223612SMichael Kruse // IR-NEXT:    %[[TMP21:.+]] = load i32, i32* %[[DOTUNROLL_INNER_IV_I]], align 4
115a2223612SMichael Kruse // IR-NEXT:    %[[TMP22:.+]] = load i32, i32* %[[DOTUNROLLED_IV_I12]], align 4
116a2223612SMichael Kruse // IR-NEXT:    %[[ADD17:.+]] = add i32 %[[TMP22]], 2
117*acec0700SMichael Kruse // IR-NEXT:    %[[CMP18:.+]] = icmp ult i32 %[[TMP21]], %[[ADD17]]
118a2223612SMichael Kruse // IR-NEXT:    br i1 %[[CMP18]], label %[[LAND_RHS:.+]], label %[[LAND_END:.+]]
119a2223612SMichael Kruse // IR-EMPTY:
120a2223612SMichael Kruse // IR-NEXT:  [[LAND_RHS]]:
121a2223612SMichael Kruse // IR-NEXT:    %[[TMP23:.+]] = load i32, i32* %[[DOTUNROLL_INNER_IV_I]], align 4
122a2223612SMichael Kruse // IR-NEXT:    %[[TMP24:.+]] = load i32, i32* %[[DOTCAPTURE_EXPR_3]], align 4
123a2223612SMichael Kruse // IR-NEXT:    %[[ADD19:.+]] = add i32 %[[TMP24]], 1
124*acec0700SMichael Kruse // IR-NEXT:    %[[CMP20:.+]] = icmp ult i32 %[[TMP23]], %[[ADD19]]
125a2223612SMichael Kruse // IR-NEXT:    br label %[[LAND_END]]
126a2223612SMichael Kruse // IR-EMPTY:
127a2223612SMichael Kruse // IR-NEXT:  [[LAND_END]]:
128a2223612SMichael Kruse // IR-NEXT:    %[[TMP25:.+]] = phi i1 [ false, %[[FOR_COND]] ], [ %[[CMP20]], %[[LAND_RHS]] ]
129a2223612SMichael Kruse // IR-NEXT:    br i1 %[[TMP25]], label %[[FOR_BODY:.+]], label %[[FOR_END:.+]]
130a2223612SMichael Kruse // IR-EMPTY:
131a2223612SMichael Kruse // IR-NEXT:  [[FOR_BODY]]:
132a2223612SMichael Kruse // IR-NEXT:    %[[TMP26:.+]] = load i32, i32* %[[DOTCAPTURE_EXPR_]], align 4
133a2223612SMichael Kruse // IR-NEXT:    %[[TMP27:.+]] = load i32, i32* %[[DOTUNROLL_INNER_IV_I]], align 4
134a2223612SMichael Kruse // IR-NEXT:    %[[TMP28:.+]] = load i32, i32* %[[DOTCAPTURE_EXPR_2]], align 4
135a2223612SMichael Kruse // IR-NEXT:    %[[MUL21:.+]] = mul i32 %[[TMP27]], %[[TMP28]]
136a2223612SMichael Kruse // IR-NEXT:    %[[ADD22:.+]] = add i32 %[[TMP26]], %[[MUL21]]
137a2223612SMichael Kruse // IR-NEXT:    store i32 %[[ADD22]], i32* %[[I]], align 4
138a2223612SMichael Kruse // IR-NEXT:    %[[TMP29:.+]] = load i32, i32* %[[START_ADDR]], align 4
139a2223612SMichael Kruse // IR-NEXT:    %[[TMP30:.+]] = load i32, i32* %[[END_ADDR]], align 4
140a2223612SMichael Kruse // IR-NEXT:    %[[TMP31:.+]] = load i32, i32* %[[STEP_ADDR]], align 4
141a2223612SMichael Kruse // IR-NEXT:    %[[TMP32:.+]] = load i32, i32* %[[I]], align 4
1421b1c8d83Shyeongyu kim // IR-NEXT:    call void (...) @body(i32 noundef %[[TMP29]], i32 noundef %[[TMP30]], i32 noundef %[[TMP31]], i32 noundef %[[TMP32]])
143a2223612SMichael Kruse // IR-NEXT:    br label %[[FOR_INC:.+]]
144a2223612SMichael Kruse // IR-EMPTY:
145a2223612SMichael Kruse // IR-NEXT:  [[FOR_INC]]:
146a2223612SMichael Kruse // IR-NEXT:    %[[TMP33:.+]] = load i32, i32* %[[DOTUNROLL_INNER_IV_I]], align 4
147a2223612SMichael Kruse // IR-NEXT:    %[[INC:.+]] = add i32 %[[TMP33]], 1
148a2223612SMichael Kruse // IR-NEXT:    store i32 %[[INC]], i32* %[[DOTUNROLL_INNER_IV_I]], align 4
149a2223612SMichael Kruse // IR-NEXT:    br label %[[FOR_COND]], !llvm.loop ![[LOOP2:[0-9]+]]
150a2223612SMichael Kruse // IR-EMPTY:
151a2223612SMichael Kruse // IR-NEXT:  [[FOR_END]]:
152a2223612SMichael Kruse // IR-NEXT:    br label %[[OMP_BODY_CONTINUE:.+]]
153a2223612SMichael Kruse // IR-EMPTY:
154a2223612SMichael Kruse // IR-NEXT:  [[OMP_BODY_CONTINUE]]:
155a2223612SMichael Kruse // IR-NEXT:    br label %[[OMP_INNER_FOR_INC:.+]]
156a2223612SMichael Kruse // IR-EMPTY:
157a2223612SMichael Kruse // IR-NEXT:  [[OMP_INNER_FOR_INC]]:
158a2223612SMichael Kruse // IR-NEXT:    %[[TMP34:.+]] = load i32, i32* %[[DOTOMP_IV]], align 4
159a2223612SMichael Kruse // IR-NEXT:    %[[ADD23:.+]] = add i32 %[[TMP34]], 1
160a2223612SMichael Kruse // IR-NEXT:    store i32 %[[ADD23]], i32* %[[DOTOMP_IV]], align 4
161a2223612SMichael Kruse // IR-NEXT:    br label %[[OMP_INNER_FOR_COND]]
162a2223612SMichael Kruse // IR-EMPTY:
163a2223612SMichael Kruse // IR-NEXT:  [[OMP_INNER_FOR_END]]:
164a2223612SMichael Kruse // IR-NEXT:    br label %[[OMP_LOOP_EXIT:.+]]
165a2223612SMichael Kruse // IR-EMPTY:
166a2223612SMichael Kruse // IR-NEXT:  [[OMP_LOOP_EXIT]]:
167a2223612SMichael Kruse // IR-NEXT:    call void @__kmpc_for_static_fini(%struct.ident_t* @1, i32 %[[TMP0]])
168a2223612SMichael Kruse // IR-NEXT:    br label %[[OMP_PRECOND_END]]
169a2223612SMichael Kruse // IR-EMPTY:
170a2223612SMichael Kruse // IR-NEXT:  [[OMP_PRECOND_END]]:
171a2223612SMichael Kruse // IR-NEXT:    call void @__kmpc_barrier(%struct.ident_t* @3, i32 %[[TMP0]])
172a2223612SMichael Kruse // IR-NEXT:    ret void
173a2223612SMichael Kruse // IR-NEXT:  }
func(int start,int end,int step)174a2223612SMichael Kruse extern "C" void func(int start, int end, int step) {
175a2223612SMichael Kruse   int i;
176a2223612SMichael Kruse   #pragma omp for
177a2223612SMichael Kruse   #pragma omp unroll partial
178a2223612SMichael Kruse   for (i = start; i < end; i+=step)
179a2223612SMichael Kruse     body(start, end, step, i);
180a2223612SMichael Kruse }
181a2223612SMichael Kruse 
182a2223612SMichael Kruse #endif /* HEADER */
183a2223612SMichael Kruse 
184a2223612SMichael Kruse 
185a2223612SMichael Kruse // IR: ![[LOOP2]] = distinct !{![[LOOP2]], ![[LOOPPROP3:[0-9]+]], ![[LOOPPROP4:[0-9]+]]}
186a2223612SMichael Kruse // IR: ![[LOOPPROP3]] = !{!"llvm.loop.mustprogress"}
187a2223612SMichael Kruse // IR: ![[LOOPPROP4]] = !{!"llvm.loop.unroll.count", i32 2}
188