1 // RUN: fir-opt --split-input-file --cfg-conversion %s | FileCheck %s
2 // RUN: fir-opt --split-input-file --cfg-conversion="set-nsw=false" %s | FileCheck %s --check-prefix=NO-NSW
4 // NO-NSW-NOT: overflow<nsw>
6 func.func @x(%lb : index, %ub : index, %step : index, %b : i1, %addr : !fir.ref<index>) {
7 fir.do_loop %iv = %lb to %ub step %step unordered {
8 // expect following conditional blocks to get fused
10 fir.store %iv to %addr : !fir.ref<index>
12 %zero = arith.constant 0 : index
13 fir.store %zero to %addr : !fir.ref<index>
19 func.func private @f2() -> i1
21 // CHECK: func @x(%[[VAL_0:.*]]: index, %[[VAL_1:.*]]: index, %[[VAL_2:.*]]: index, %[[VAL_3:.*]]: i1, %[[VAL_4:.*]]: !fir.ref<index>) {
22 // CHECK: %[[VAL_5:.*]] = arith.subi %[[VAL_1]], %[[VAL_0]] : index
23 // CHECK: %[[VAL_6:.*]] = arith.addi %[[VAL_5]], %[[VAL_2]] : index
24 // CHECK: %[[VAL_7:.*]] = arith.divsi %[[VAL_6]], %[[VAL_2]] : index
25 // CHECK: br ^bb1(%[[VAL_0]], %[[VAL_7]] : index, index)
26 // CHECK: ^bb1(%[[VAL_8:.*]]: index, %[[VAL_9:.*]]: index):
27 // CHECK: %[[VAL_10:.*]] = arith.constant 0 : index
28 // CHECK: %[[VAL_11:.*]] = arith.cmpi sgt, %[[VAL_9]], %[[VAL_10]] : index
29 // CHECK: cond_br %[[VAL_11]], ^bb2, ^bb6
31 // CHECK: cond_br %[[VAL_3]], ^bb3, ^bb4
33 // CHECK: fir.store %[[VAL_8]] to %[[VAL_4]] : !fir.ref<index>
36 // CHECK: %[[VAL_12:.*]] = arith.constant 0 : index
37 // CHECK: fir.store %[[VAL_12]] to %[[VAL_4]] : !fir.ref<index>
40 // CHECK: %[[VAL_13:.*]] = arith.addi %[[VAL_8]], %[[VAL_2]] overflow<nsw> : index
41 // CHECK: %[[VAL_14:.*]] = arith.constant 1 : index
42 // CHECK: %[[VAL_15:.*]] = arith.subi %[[VAL_9]], %[[VAL_14]] : index
43 // CHECK: br ^bb1(%[[VAL_13]], %[[VAL_15]] : index, index)
47 // CHECK: func private @f2() -> i1
51 func.func @x2(%lo : index, %up : index, %ok : i1) {
52 %c1 = arith.constant 1 : index
53 %unused = fir.iterate_while (%i = %lo to %up step %c1) and (%ok1 = %ok) {
54 %ok2 = fir.call @f2() : () -> i1
60 func.func private @f3(i16)
62 // CHECK: func @x2(%[[VAL_0:.*]]: index, %[[VAL_1:.*]]: index, %[[VAL_2:.*]]: i1) {
63 // CHECK: %[[VAL_3:.*]] = arith.constant 1 : index
64 // CHECK: br ^bb1(%[[VAL_0]], %[[VAL_2]] : index, i1)
65 // CHECK: ^bb1(%[[VAL_4:.*]]: index, %[[VAL_5:.*]]: i1):
66 // CHECK: %[[VAL_6:.*]] = arith.constant 0 : index
67 // CHECK: %[[VAL_7:.*]] = arith.cmpi slt, %[[VAL_6]], %[[VAL_3]] : index
68 // CHECK: %[[VAL_8:.*]] = arith.cmpi sle, %[[VAL_4]], %[[VAL_1]] : index
69 // CHECK: %[[VAL_9:.*]] = arith.cmpi slt, %[[VAL_3]], %[[VAL_6]] : index
70 // CHECK: %[[VAL_10:.*]] = arith.cmpi sle, %[[VAL_1]], %[[VAL_4]] : index
71 // CHECK: %[[VAL_11:.*]] = arith.andi %[[VAL_7]], %[[VAL_8]] : i1
72 // CHECK: %[[VAL_12:.*]] = arith.andi %[[VAL_9]], %[[VAL_10]] : i1
73 // CHECK: %[[VAL_13:.*]] = arith.ori %[[VAL_11]], %[[VAL_12]] : i1
74 // CHECK: %[[VAL_14:.*]] = arith.andi %[[VAL_5]], %[[VAL_13]] : i1
75 // CHECK: cond_br %[[VAL_14]], ^bb2, ^bb3
77 // CHECK: %[[VAL_15:.*]] = fir.call @f2() : () -> i1
78 // CHECK: %[[VAL_16:.*]] = arith.addi %[[VAL_4]], %[[VAL_3]] overflow<nsw> : index
79 // CHECK: br ^bb1(%[[VAL_16]], %[[VAL_15]] : index, i1)
83 // CHECK: func private @f3(i16)
87 // do_loop with an extra loop-carried value
88 func.func @x3(%lo : index, %up : index) -> i1 {
89 %c1 = arith.constant 1 : index
90 %ok1 = arith.constant true
91 %ok2 = fir.do_loop %i = %lo to %up step %c1 iter_args(%j = %ok1) -> i1 {
92 %ok = fir.call @f2() : () -> i1
98 // CHECK-LABEL: func @x3(
99 // CHECK-SAME: %[[VAL_0:.*]]: index,
100 // CHECK-SAME: %[[VAL_1:.*]]: index) -> i1 {
101 // CHECK: %[[VAL_2:.*]] = arith.constant 1 : index
102 // CHECK: %[[VAL_3:.*]] = arith.constant true
103 // CHECK: %[[VAL_4:.*]] = arith.subi %[[VAL_1]], %[[VAL_0]] : index
104 // CHECK: %[[VAL_5:.*]] = arith.addi %[[VAL_4]], %[[VAL_2]] : index
105 // CHECK: %[[VAL_6:.*]] = arith.divsi %[[VAL_5]], %[[VAL_2]] : index
106 // CHECK: br ^bb1(%[[VAL_0]], %[[VAL_3]], %[[VAL_6]] : index, i1, index)
107 // CHECK: ^bb1(%[[VAL_7:.*]]: index, %[[VAL_8:.*]]: i1, %[[VAL_9:.*]]: index):
108 // CHECK: %[[VAL_10:.*]] = arith.constant 0 : index
109 // CHECK: %[[VAL_11:.*]] = arith.cmpi sgt, %[[VAL_9]], %[[VAL_10]] : index
110 // CHECK: cond_br %[[VAL_11]], ^bb2, ^bb3
112 // CHECK: %[[VAL_12:.*]] = fir.call @f2() : () -> i1
113 // CHECK: %[[VAL_13:.*]] = arith.addi %[[VAL_7]], %[[VAL_2]] overflow<nsw> : index
114 // CHECK: %[[VAL_14:.*]] = arith.constant 1 : index
115 // CHECK: %[[VAL_15:.*]] = arith.subi %[[VAL_9]], %[[VAL_14]] : index
116 // CHECK: br ^bb1(%[[VAL_13]], %[[VAL_12]], %[[VAL_15]] : index, i1, index)
118 // CHECK: return %[[VAL_8]] : i1
123 // iterate_while with an extra loop-carried value
124 func.func @y3(%lo : index, %up : index) -> i1 {
125 %c1 = arith.constant 1 : index
126 %ok1 = arith.constant true
127 %ok4 = fir.call @f2() : () -> i1
128 %ok2:2 = fir.iterate_while (%i = %lo to %up step %c1) and (%ok3 = %ok1) iter_args(%j = %ok4) -> i1 {
129 %ok = fir.call @f2() : () -> i1
130 fir.result %ok3, %ok : i1, i1
132 %andok = arith.andi %ok2#0, %ok2#1 : i1
136 func.func private @f4(i32) -> i1
138 // CHECK-LABEL: func @y3(
139 // CHECK-SAME: %[[VAL_0:.*]]: index,
140 // CHECK-SAME: %[[VAL_1:.*]]: index) -> i1 {
141 // CHECK: %[[VAL_2:.*]] = arith.constant 1 : index
142 // CHECK: %[[VAL_3:.*]] = arith.constant true
143 // CHECK: %[[VAL_4:.*]] = fir.call @f2() : () -> i1
144 // CHECK: br ^bb1(%[[VAL_0]], %[[VAL_3]], %[[VAL_4]] : index, i1, i1)
145 // CHECK: ^bb1(%[[VAL_5:.*]]: index, %[[VAL_6:.*]]: i1, %[[VAL_7:.*]]: i1):
146 // CHECK: %[[VAL_8:.*]] = arith.constant 0 : index
147 // CHECK: %[[VAL_9:.*]] = arith.cmpi slt, %[[VAL_8]], %[[VAL_2]] : index
148 // CHECK: %[[VAL_10:.*]] = arith.cmpi sle, %[[VAL_5]], %[[VAL_1]] : index
149 // CHECK: %[[VAL_11:.*]] = arith.cmpi slt, %[[VAL_2]], %[[VAL_8]] : index
150 // CHECK: %[[VAL_12:.*]] = arith.cmpi sle, %[[VAL_1]], %[[VAL_5]] : index
151 // CHECK: %[[VAL_13:.*]] = arith.andi %[[VAL_9]], %[[VAL_10]] : i1
152 // CHECK: %[[VAL_14:.*]] = arith.andi %[[VAL_11]], %[[VAL_12]] : i1
153 // CHECK: %[[VAL_15:.*]] = arith.ori %[[VAL_13]], %[[VAL_14]] : i1
154 // CHECK: %[[VAL_16:.*]] = arith.andi %[[VAL_6]], %[[VAL_15]] : i1
155 // CHECK: cond_br %[[VAL_16]], ^bb2, ^bb3
157 // CHECK: %[[VAL_17:.*]] = fir.call @f2() : () -> i1
158 // CHECK: %[[VAL_18:.*]] = arith.addi %[[VAL_5]], %[[VAL_2]] overflow<nsw> : index
159 // CHECK: br ^bb1(%[[VAL_18]], %[[VAL_6]], %[[VAL_17]] : index, i1, i1)
161 // CHECK: %[[VAL_19:.*]] = arith.andi %[[VAL_6]], %[[VAL_7]] : i1
162 // CHECK: return %[[VAL_19]] : i1
164 // CHECK: func private @f4(i32) -> i1
168 // do_loop that returns the final value of the induction
169 func.func @x4(%lo : index, %up : index) -> index {
170 %c1 = arith.constant 1 : index
171 %v = fir.do_loop %i = %lo to %up step %c1 -> index {
172 %i1 = fir.convert %i : (index) -> i32
173 %ok = fir.call @f4(%i1) : (i32) -> i1
174 fir.result %i : index
179 // CHECK-LABEL: func @x4(
180 // CHECK-SAME: %[[VAL_0:.*]]: index,
181 // CHECK-SAME: %[[VAL_1:.*]]: index) -> index {
182 // CHECK: %[[VAL_2:.*]] = arith.constant 1 : index
183 // CHECK: %[[VAL_3:.*]] = arith.subi %[[VAL_1]], %[[VAL_0]] : index
184 // CHECK: %[[VAL_4:.*]] = arith.addi %[[VAL_3]], %[[VAL_2]] : index
185 // CHECK: %[[VAL_5:.*]] = arith.divsi %[[VAL_4]], %[[VAL_2]] : index
186 // CHECK: br ^bb1(%[[VAL_0]], %[[VAL_5]] : index, index)
187 // CHECK: ^bb1(%[[VAL_6:.*]]: index, %[[VAL_7:.*]]: index):
188 // CHECK: %[[VAL_8:.*]] = arith.constant 0 : index
189 // CHECK: %[[VAL_9:.*]] = arith.cmpi sgt, %[[VAL_7]], %[[VAL_8]] : index
190 // CHECK: cond_br %[[VAL_9]], ^bb2, ^bb3
192 // CHECK: %[[VAL_10:.*]] = fir.convert %[[VAL_6]] : (index) -> i32
193 // CHECK: %[[VAL_11:.*]] = fir.call @f4(%[[VAL_10]]) : (i32) -> i1
194 // CHECK: %[[VAL_12:.*]] = arith.addi %[[VAL_6]], %[[VAL_2]] overflow<nsw> : index
195 // CHECK: %[[VAL_13:.*]] = arith.constant 1 : index
196 // CHECK: %[[VAL_14:.*]] = arith.subi %[[VAL_7]], %[[VAL_13]] : index
197 // CHECK: br ^bb1(%[[VAL_12]], %[[VAL_14]] : index, index)
199 // CHECK: return %[[VAL_6]] : index
204 // iterate_while that returns the final value of both inductions
205 func.func @y4(%lo : index, %up : index) -> index {
206 %c1 = arith.constant 1 : index
207 %ok1 = arith.constant true
208 %v:2 = fir.iterate_while (%i = %lo to %up step %c1) and (%ok2 = %ok1) -> (index, i1) {
209 %i1 = fir.convert %i : (index) -> i32
210 %ok = fir.call @f4(%i1) : (i32) -> i1
211 fir.result %i, %ok : index, i1
216 // CHECK-LABEL: func @y4(
217 // CHECK-SAME: %[[VAL_0:.*]]: index,
218 // CHECK-SAME: %[[VAL_1:.*]]: index) -> index {
219 // CHECK: %[[VAL_2:.*]] = arith.constant 1 : index
220 // CHECK: %[[VAL_3:.*]] = arith.constant true
221 // CHECK: br ^bb1(%[[VAL_0]], %[[VAL_3]] : index, i1)
222 // CHECK: ^bb1(%[[VAL_4:.*]]: index, %[[VAL_5:.*]]: i1):
223 // CHECK: %[[VAL_6:.*]] = arith.constant 0 : index
224 // CHECK: %[[VAL_7:.*]] = arith.cmpi slt, %[[VAL_6]], %[[VAL_2]] : index
225 // CHECK: %[[VAL_8:.*]] = arith.cmpi sle, %[[VAL_4]], %[[VAL_1]] : index
226 // CHECK: %[[VAL_9:.*]] = arith.cmpi slt, %[[VAL_2]], %[[VAL_6]] : index
227 // CHECK: %[[VAL_10:.*]] = arith.cmpi sle, %[[VAL_1]], %[[VAL_4]] : index
228 // CHECK: %[[VAL_11:.*]] = arith.andi %[[VAL_7]], %[[VAL_8]] : i1
229 // CHECK: %[[VAL_12:.*]] = arith.andi %[[VAL_9]], %[[VAL_10]] : i1
230 // CHECK: %[[VAL_13:.*]] = arith.ori %[[VAL_11]], %[[VAL_12]] : i1
231 // CHECK: %[[VAL_14:.*]] = arith.andi %[[VAL_5]], %[[VAL_13]] : i1
232 // CHECK: cond_br %[[VAL_14]], ^bb2, ^bb3
234 // CHECK: %[[VAL_15:.*]] = fir.convert %[[VAL_4]] : (index) -> i32
235 // CHECK: %[[VAL_16:.*]] = fir.call @f4(%[[VAL_15]]) : (i32) -> i1
236 // CHECK: %[[VAL_17:.*]] = arith.addi %[[VAL_4]], %[[VAL_2]] overflow<nsw> : index
237 // CHECK: br ^bb1(%[[VAL_17]], %[[VAL_16]] : index, i1)
239 // CHECK: return %[[VAL_4]] : index
244 // do_loop that returns the final induction value
245 // and an extra loop-carried value
246 func.func @x5(%lo : index, %up : index) -> index {
247 %c1 = arith.constant 1 : index
248 %s1 = arith.constant 42 : i16
249 %v:2 = fir.do_loop %i = %lo to %up step %c1 iter_args(%s = %s1) -> (index, i16) {
250 %ok = fir.call @f2() : () -> i1
251 %s2 = fir.convert %ok : (i1) -> i16
252 fir.result %i, %s2 : index, i16
254 fir.call @f3(%v#1) : (i16) -> ()
258 // CHECK-LABEL: func @x5(
259 // CHECK-SAME: %[[VAL_0:.*]]: index,
260 // CHECK-SAME: %[[VAL_1:.*]]: index) -> index {
261 // CHECK: %[[VAL_2:.*]] = arith.constant 1 : index
262 // CHECK: %[[VAL_3:.*]] = arith.constant 42 : i16
263 // CHECK: %[[VAL_4:.*]] = arith.subi %[[VAL_1]], %[[VAL_0]] : index
264 // CHECK: %[[VAL_5:.*]] = arith.addi %[[VAL_4]], %[[VAL_2]] : index
265 // CHECK: %[[VAL_6:.*]] = arith.divsi %[[VAL_5]], %[[VAL_2]] : index
266 // CHECK: br ^bb1(%[[VAL_0]], %[[VAL_3]], %[[VAL_6]] : index, i16, index)
267 // CHECK: ^bb1(%[[VAL_7:.*]]: index, %[[VAL_8:.*]]: i16, %[[VAL_9:.*]]: index):
268 // CHECK: %[[VAL_10:.*]] = arith.constant 0 : index
269 // CHECK: %[[VAL_11:.*]] = arith.cmpi sgt, %[[VAL_9]], %[[VAL_10]] : index
270 // CHECK: cond_br %[[VAL_11]], ^bb2, ^bb3
272 // CHECK: %[[VAL_12:.*]] = fir.call @f2() : () -> i1
273 // CHECK: %[[VAL_13:.*]] = fir.convert %[[VAL_12]] : (i1) -> i16
274 // CHECK: %[[VAL_14:.*]] = arith.addi %[[VAL_7]], %[[VAL_2]] overflow<nsw> : index
275 // CHECK: %[[VAL_15:.*]] = arith.constant 1 : index
276 // CHECK: %[[VAL_16:.*]] = arith.subi %[[VAL_9]], %[[VAL_15]] : index
277 // CHECK: br ^bb1(%[[VAL_14]], %[[VAL_13]], %[[VAL_16]] : index, i16, index)
279 // CHECK: fir.call @f3(%[[VAL_8]]) : (i16) -> ()
280 // CHECK: return %[[VAL_7]] : index
285 // iterate_while that returns the both induction values
286 // and an extra loop-carried value
287 func.func @y5(%lo : index, %up : index) -> index {
288 %c1 = arith.constant 1 : index
289 %s1 = arith.constant 42 : i16
290 %ok1 = arith.constant true
291 %v:3 = fir.iterate_while (%i = %lo to %up step %c1) and (%ok2 = %ok1) iter_args(%s = %s1) -> (index, i1, i16) {
292 %ok = fir.call @f2() : () -> i1
293 %s2 = fir.convert %ok : (i1) -> i16
294 fir.result %i, %ok, %s2 : index, i1, i16
297 %arg = arith.constant 0 : i32
298 %ok4 = fir.call @f4(%arg) : (i32) -> i1
300 fir.call @f3(%v#2) : (i16) -> ()
304 // CHECK-LABEL: func @y5(
305 // CHECK-SAME: %[[VAL_0:.*]]: index,
306 // CHECK-SAME: %[[VAL_1:.*]]: index) -> index {
307 // CHECK: %[[VAL_2:.*]] = arith.constant 1 : index
308 // CHECK: %[[VAL_3:.*]] = arith.constant 42 : i16
309 // CHECK: %[[VAL_4:.*]] = arith.constant true
310 // CHECK: br ^bb1(%[[VAL_0]], %[[VAL_4]], %[[VAL_3]] : index, i1, i16)
311 // CHECK: ^bb1(%[[VAL_5:.*]]: index, %[[VAL_6:.*]]: i1, %[[VAL_7:.*]]: i16):
312 // CHECK: %[[VAL_8:.*]] = arith.constant 0 : index
313 // CHECK: %[[VAL_9:.*]] = arith.cmpi slt, %[[VAL_8]], %[[VAL_2]] : index
314 // CHECK: %[[VAL_10:.*]] = arith.cmpi sle, %[[VAL_5]], %[[VAL_1]] : index
315 // CHECK: %[[VAL_11:.*]] = arith.cmpi slt, %[[VAL_2]], %[[VAL_8]] : index
316 // CHECK: %[[VAL_12:.*]] = arith.cmpi sle, %[[VAL_1]], %[[VAL_5]] : index
317 // CHECK: %[[VAL_13:.*]] = arith.andi %[[VAL_9]], %[[VAL_10]] : i1
318 // CHECK: %[[VAL_14:.*]] = arith.andi %[[VAL_11]], %[[VAL_12]] : i1
319 // CHECK: %[[VAL_15:.*]] = arith.ori %[[VAL_13]], %[[VAL_14]] : i1
320 // CHECK: %[[VAL_16:.*]] = arith.andi %[[VAL_6]], %[[VAL_15]] : i1
321 // CHECK: cond_br %[[VAL_16]], ^bb2, ^bb3
323 // CHECK: %[[VAL_17:.*]] = fir.call @f2() : () -> i1
324 // CHECK: %[[VAL_18:.*]] = fir.convert %[[VAL_17]] : (i1) -> i16
325 // CHECK: %[[VAL_19:.*]] = arith.addi %[[VAL_5]], %[[VAL_2]] overflow<nsw> : index
326 // CHECK: br ^bb1(%[[VAL_19]], %[[VAL_17]], %[[VAL_18]] : index, i1, i16)
328 // CHECK: cond_br %[[VAL_6]], ^bb4, ^bb5
330 // CHECK: %[[VAL_20:.*]] = arith.constant 0 : i32
331 // CHECK: %[[VAL_21:.*]] = fir.call @f4(%[[VAL_20]]) : (i32) -> i1
334 // CHECK: fir.call @f3(%[[VAL_7]]) : (i16) -> ()
335 // CHECK: return %[[VAL_5]] : index