1 ; RUN: opt < %s -S -analyze -scalar-evolution | FileCheck %s
3 ; Positive and negative tests for inferring flags like nsw from
4 ; reasoning about how a poison value from overflow would trigger
11 ; Example where an add should get the nsw flag, so that a sext can be
12 ; distributed over the add.
13 define void @test-add-nsw(float* %input, i32 %offset, i32 %numIterations) {
14 ; CHECK-LABEL: @test-add-nsw
18 %i = phi i32 [ %nexti, %loop ], [ 0, %entry ]
21 ; CHECK: --> {%offset,+,1}<nsw>
22 %index32 = add nsw i32 %i, %offset
25 ; CHECK: --> {(sext i32 %offset to i64),+,1}<nsw>
26 %index64 = sext i32 %index32 to i64
28 %ptr = getelementptr inbounds float, float* %input, i64 %index64
29 %nexti = add nsw i32 %i, 1
30 %f = load float, float* %ptr, align 4
32 %exitcond = icmp eq i32 %nexti, %numIterations
33 br i1 %exitcond, label %exit, label %loop
38 ; Example where an add should get the nuw flag.
39 define void @test-add-nuw(float* %input, i32 %offset, i32 %numIterations) {
40 ; CHECK-LABEL: @test-add-nuw
44 %i = phi i32 [ %nexti, %loop ], [ 0, %entry ]
47 ; CHECK: --> {%offset,+,1}<nuw>
48 %index32 = add nuw i32 %i, %offset
50 %ptr = getelementptr inbounds float, float* %input, i32 %index32
51 %nexti = add nuw i32 %i, 1
52 %f = load float, float* %ptr, align 4
53 %exitcond = icmp eq i32 %nexti, %numIterations
54 br i1 %exitcond, label %exit, label %loop
60 ; With no load to trigger UB from poison, we cannot infer nsw.
61 define void @test-add-no-load(float* %input, i32 %offset, i32 %numIterations) {
62 ; CHECK-LABEL: @test-add-no-load
66 %i = phi i32 [ %nexti, %loop ], [ 0, %entry ]
69 ; CHECK: --> {%offset,+,1}<nw>
70 %index32 = add nsw i32 %i, %offset
72 %ptr = getelementptr inbounds float, float* %input, i32 %index32
73 %nexti = add nuw i32 %i, 1
74 %exitcond = icmp eq i32 %nexti, %numIterations
75 br i1 %exitcond, label %exit, label %loop
81 ; The current code is only supposed to look at the loop header, so
82 ; it should not infer nsw in this case, as that would require looking
83 ; outside the loop header.
84 define void @test-add-not-header(float* %input, i32 %offset, i32 %numIterations) {
85 ; CHECK-LABEL: @test-add-not-header
89 %i = phi i32 [ %nexti, %loop2 ], [ 0, %entry ]
94 ; CHECK: --> {%offset,+,1}<nw>
95 %index32 = add nsw i32 %i, %offset
97 %ptr = getelementptr inbounds float, float* %input, i32 %index32
98 %nexti = add nsw i32 %i, 1
99 %f = load float, float* %ptr, align 4
100 %exitcond = icmp eq i32 %nexti, %numIterations
101 br i1 %exitcond, label %exit, label %loop
106 ; Same thing as test-add-not-header, but in this case only the load
107 ; instruction is outside the loop header.
108 define void @test-add-not-header2(float* %input, i32 %offset, i32 %numIterations) {
109 ; CHECK-LABEL: @test-add-not-header2
113 %i = phi i32 [ %nexti, %loop2 ], [ 0, %entry ]
116 ; CHECK: --> {%offset,+,1}<nw>
117 %index32 = add nsw i32 %i, %offset
119 %ptr = getelementptr inbounds float, float* %input, i32 %index32
120 %nexti = add nsw i32 %i, 1
123 %f = load float, float* %ptr, align 4
124 %exitcond = icmp eq i32 %nexti, %numIterations
125 br i1 %exitcond, label %exit, label %loop
130 ; The call instruction makes it not guaranteed that the add will be
131 ; executed, since it could run forever or throw an exception, so we
132 ; cannot assume that the UB is realized.
133 define void @test-add-call(float* %input, i32 %offset, i32 %numIterations) {
134 ; CHECK-LABEL: @test-add-call
138 %i = phi i32 [ %nexti, %loop ], [ 0, %entry ]
141 ; CHECK: --> {%offset,+,1}<nw>
143 %index32 = add nsw i32 %i, %offset
145 %ptr = getelementptr inbounds float, float* %input, i32 %index32
146 %nexti = add nsw i32 %i, 1
147 %f = load float, float* %ptr, align 4
148 %exitcond = icmp eq i32 %nexti, %numIterations
149 br i1 %exitcond, label %exit, label %loop
154 ; Same issue as test-add-call, but this time the call is between the
155 ; producer of poison and the load that consumes it.
156 define void @test-add-call2(float* %input, i32 %offset, i32 %numIterations) {
157 ; CHECK-LABEL: @test-add-call2
161 %i = phi i32 [ %nexti, %loop ], [ 0, %entry ]
164 ; CHECK: --> {%offset,+,1}<nw>
165 %index32 = add nsw i32 %i, %offset
167 %ptr = getelementptr inbounds float, float* %input, i32 %index32
168 %nexti = add nsw i32 %i, 1
170 %f = load float, float* %ptr, align 4
171 %exitcond = icmp eq i32 %nexti, %numIterations
172 br i1 %exitcond, label %exit, label %loop
177 ; Without inbounds, GEP does not propagate poison in the very
178 ; conservative approach used here.
179 define void @test-add-no-inbounds(float* %input, i32 %offset, i32 %numIterations) {
180 ; CHECK-LABEL: @test-add-no-inbounds
184 %i = phi i32 [ %nexti, %loop ], [ 0, %entry ]
187 ; CHECK: --> {%offset,+,1}<nw>
188 %index32 = add nsw i32 %i, %offset
190 %ptr = getelementptr float, float* %input, i32 %index32
191 %nexti = add nsw i32 %i, 1
192 %f = load float, float* %ptr, align 4
193 %exitcond = icmp eq i32 %nexti, %numIterations
194 br i1 %exitcond, label %exit, label %loop
199 ; Multiplication by a non-zero constant propagates poison if there is
200 ; a nuw or nsw flag on the multiplication.
201 define void @test-add-mul-propagates(float* %input, i32 %offset, i32 %numIterations) {
202 ; CHECK-LABEL: @test-add-mul-propagates
206 %i = phi i32 [ %nexti, %loop ], [ 0, %entry ]
209 ; CHECK: --> {%offset,+,1}<nsw>
210 %index32 = add nsw i32 %i, %offset
212 %indexmul = mul nuw i32 %index32, 2
213 %ptr = getelementptr inbounds float, float* %input, i32 %indexmul
214 %nexti = add nsw i32 %i, 1
215 %f = load float, float* %ptr, align 4
216 %exitcond = icmp eq i32 %nexti, %numIterations
217 br i1 %exitcond, label %exit, label %loop
222 ; Multiplication by a non-constant should not propagate poison in the
223 ; very conservative approach used here.
224 define void @test-add-mul-no-propagation(float* %input, i32 %offset, i32 %numIterations) {
225 ; CHECK-LABEL: @test-add-mul-no-propagation
229 %i = phi i32 [ %nexti, %loop ], [ 0, %entry ]
232 ; CHECK: --> {%offset,+,1}<nw>
233 %index32 = add nsw i32 %i, %offset
235 %indexmul = mul nsw i32 %index32, %offset
236 %ptr = getelementptr inbounds float, float* %input, i32 %indexmul
237 %nexti = add nsw i32 %i, 1
238 %f = load float, float* %ptr, align 4
239 %exitcond = icmp eq i32 %nexti, %numIterations
240 br i1 %exitcond, label %exit, label %loop
245 ; Multiplication by a non-zero constant does not propagate poison
246 ; without a no-wrap flag.
247 define void @test-add-mul-no-propagation2(float* %input, i32 %offset, i32 %numIterations) {
248 ; CHECK-LABEL: @test-add-mul-no-propagation2
252 %i = phi i32 [ %nexti, %loop ], [ 0, %entry ]
255 ; CHECK: --> {%offset,+,1}<nw>
256 %index32 = add nsw i32 %i, %offset
258 %indexmul = mul i32 %index32, 2
259 %ptr = getelementptr inbounds float, float* %input, i32 %indexmul
260 %nexti = add nsw i32 %i, 1
261 %f = load float, float* %ptr, align 4
262 %exitcond = icmp eq i32 %nexti, %numIterations
263 br i1 %exitcond, label %exit, label %loop
268 ; Division by poison triggers UB.
269 define void @test-add-div(float* %input, i32 %offset, i32 %numIterations) {
270 ; CHECK-LABEL: @test-add-div
274 %i = phi i32 [ %nexti, %loop ], [ 0, %entry ]
277 ; CHECK: --> {%offset,+,1}<nsw>
278 %j = add nsw i32 %i, %offset
280 %q = sdiv i32 %numIterations, %j
281 %nexti = add nsw i32 %i, 1
282 %exitcond = icmp eq i32 %nexti, %numIterations
283 br i1 %exitcond, label %exit, label %loop
288 ; Remainder of poison by non-poison divisor does not trigger UB.
289 define void @test-add-div2(float* %input, i32 %offset, i32 %numIterations) {
290 ; CHECK-LABEL: @test-add-div2
294 %i = phi i32 [ %nexti, %loop ], [ 0, %entry ]
297 ; CHECK: --> {%offset,+,1}<nw>
298 %j = add nsw i32 %i, %offset
300 %q = sdiv i32 %j, %numIterations
301 %nexti = add nsw i32 %i, 1
302 %exitcond = icmp eq i32 %nexti, %numIterations
303 br i1 %exitcond, label %exit, label %loop
308 ; Store to poison address triggers UB.
309 define void @test-add-store(float* %input, i32 %offset, i32 %numIterations) {
310 ; CHECK-LABEL: @test-add-store
314 %i = phi i32 [ %nexti, %loop ], [ 0, %entry ]
317 ; CHECK: --> {%offset,+,1}<nsw>
318 %index32 = add nsw i32 %i, %offset
320 %ptr = getelementptr inbounds float, float* %input, i32 %index32
321 %nexti = add nsw i32 %i, 1
322 store float 1.0, float* %ptr, align 4
323 %exitcond = icmp eq i32 %nexti, %numIterations
324 br i1 %exitcond, label %exit, label %loop
329 ; Three sequential adds where the middle add should have nsw. There is
330 ; a special case for sequential adds and this test covers that. We have to
331 ; put the final add first in the program since otherwise the special case
332 ; is not triggered, hence the strange basic block ordering.
333 define void @test-add-twice(float* %input, i32 %offset, i32 %numIterations) {
334 ; CHECK-LABEL: @test-add-twice
339 ; CHECK: --> {(2 + %offset),+,1}<nw>
340 %seq = add nsw nuw i32 %index32, 1
341 %exitcond = icmp eq i32 %nexti, %numIterations
342 br i1 %exitcond, label %exit, label %loop
345 %i = phi i32 [ %nexti, %loop2 ], [ 0, %entry ]
347 %j = add nsw i32 %i, 1
349 ; CHECK: --> {(1 + %offset),+,1}<nsw>
350 %index32 = add nsw i32 %j, %offset
352 %ptr = getelementptr inbounds float, float* %input, i32 %index32
353 %nexti = add nsw i32 %i, 1
354 store float 1.0, float* %ptr, align 4
360 ; Example where a mul should get the nsw flag, so that a sext can be
361 ; distributed over the mul.
362 define void @test-mul-nsw(float* %input, i32 %stride, i32 %numIterations) {
363 ; CHECK-LABEL: @test-mul-nsw
367 %i = phi i32 [ %nexti, %loop ], [ 0, %entry ]
370 ; CHECK: --> {0,+,%stride}<nsw>
371 %index32 = mul nsw i32 %i, %stride
374 ; CHECK: --> {0,+,(sext i32 %stride to i64)}<nsw>
375 %index64 = sext i32 %index32 to i64
377 %ptr = getelementptr inbounds float, float* %input, i64 %index64
378 %nexti = add nsw i32 %i, 1
379 %f = load float, float* %ptr, align 4
380 %exitcond = icmp eq i32 %nexti, %numIterations
381 br i1 %exitcond, label %exit, label %loop
386 ; Example where a mul should get the nuw flag.
387 define void @test-mul-nuw(float* %input, i32 %stride, i32 %numIterations) {
388 ; CHECK-LABEL: @test-mul-nuw
392 %i = phi i32 [ %nexti, %loop ], [ 0, %entry ]
395 ; CHECK: --> {0,+,%stride}<nuw>
396 %index32 = mul nuw i32 %i, %stride
398 %ptr = getelementptr inbounds float, float* %input, i32 %index32
399 %nexti = add nuw i32 %i, 1
400 %f = load float, float* %ptr, align 4
401 %exitcond = icmp eq i32 %nexti, %numIterations
402 br i1 %exitcond, label %exit, label %loop
408 ; Example where a shl should get the nsw flag, so that a sext can be
409 ; distributed over the shl.
410 define void @test-shl-nsw(float* %input, i32 %start, i32 %numIterations) {
411 ; CHECK-LABEL: @test-shl-nsw
415 %i = phi i32 [ %nexti, %loop ], [ %start, %entry ]
418 ; CHECK: --> {(256 * %start),+,256}<nsw>
419 %index32 = shl nsw i32 %i, 8
422 ; CHECK: --> {(sext i32 (256 * %start) to i64),+,256}<nsw>
423 %index64 = sext i32 %index32 to i64
425 %ptr = getelementptr inbounds float, float* %input, i64 %index64
426 %nexti = add nsw i32 %i, 1
427 %f = load float, float* %ptr, align 4
428 %exitcond = icmp eq i32 %nexti, %numIterations
429 br i1 %exitcond, label %exit, label %loop
434 ; Example where a shl should get the nuw flag.
435 define void @test-shl-nuw(float* %input, i32 %numIterations) {
436 ; CHECK-LABEL: @test-shl-nuw
440 %i = phi i32 [ %nexti, %loop ], [ 0, %entry ]
443 ; CHECK: --> {0,+,512}<nuw>
444 %index32 = shl nuw i32 %i, 9
446 %ptr = getelementptr inbounds float, float* %input, i32 %index32
447 %nexti = add nuw i32 %i, 1
448 %f = load float, float* %ptr, align 4
449 %exitcond = icmp eq i32 %nexti, %numIterations
450 br i1 %exitcond, label %exit, label %loop
456 ; Example where a sub should *not* get the nsw flag, because of how
457 ; scalar evolution represents A - B as A + (-B) and -B can wrap even
458 ; in cases where A - B does not.
459 define void @test-sub-no-nsw(float* %input, i32 %start, i32 %sub, i32 %numIterations) {
460 ; CHECK-LABEL: @test-sub-no-nsw
464 %i = phi i32 [ %nexti, %loop ], [ %start, %entry ]
467 ; CHECK: --> {((-1 * %sub) + %start),+,1}<nw>
468 %index32 = sub nsw i32 %i, %sub
469 %index64 = sext i32 %index32 to i64
471 %ptr = getelementptr inbounds float, float* %input, i64 %index64
472 %nexti = add nsw i32 %i, 1
473 %f = load float, float* %ptr, align 4
474 %exitcond = icmp eq i32 %nexti, %numIterations
475 br i1 %exitcond, label %exit, label %loop
480 ; Example where a sub should get the nsw flag as the RHS cannot be the
481 ; minimal signed value.
482 define void @test-sub-nsw(float* %input, i32 %start, i32 %sub, i32 %numIterations) {
483 ; CHECK-LABEL: @test-sub-nsw
485 %halfsub = ashr i32 %sub, 1
488 %i = phi i32 [ %nexti, %loop ], [ %start, %entry ]
491 ; CHECK: --> {((-1 * %halfsub)<nsw> + %start),+,1}<nsw>
492 %index32 = sub nsw i32 %i, %halfsub
493 %index64 = sext i32 %index32 to i64
495 %ptr = getelementptr inbounds float, float* %input, i64 %index64
496 %nexti = add nsw i32 %i, 1
497 %f = load float, float* %ptr, align 4
498 %exitcond = icmp eq i32 %nexti, %numIterations
499 br i1 %exitcond, label %exit, label %loop
504 ; Example where a sub should get the nsw flag, since the LHS is non-negative,
505 ; which implies that the RHS cannot be the minimal signed value.
506 define void @test-sub-nsw-lhs-non-negative(float* %input, i32 %sub, i32 %numIterations) {
507 ; CHECK-LABEL: @test-sub-nsw-lhs-non-negative
511 %i = phi i32 [ %nexti, %loop ], [ 0, %entry ]
514 ; CHECK: --> {(-1 * %sub),+,1}<nsw>
515 %index32 = sub nsw i32 %i, %sub
518 ; CHECK: --> {(sext i32 (-1 * %sub) to i64),+,1}<nsw>
519 %index64 = sext i32 %index32 to i64
521 %ptr = getelementptr inbounds float, float* %input, i64 %index64
522 %nexti = add nsw i32 %i, 1
523 %f = load float, float* %ptr, align 4
524 %exitcond = icmp eq i32 %nexti, %numIterations
525 br i1 %exitcond, label %exit, label %loop
530 ; Two adds with a sub in the middle and the sub should have nsw. There is
531 ; a special case for sequential adds/subs and this test covers that. We have to
532 ; put the final add first in the program since otherwise the special case
533 ; is not triggered, hence the strange basic block ordering.
534 define void @test-sub-with-add(float* %input, i32 %offset, i32 %numIterations) {
535 ; CHECK-LABEL: @test-sub-with-add
540 ; CHECK: --> {(2 + (-1 * %offset)),+,1}<nw>
541 %seq = add nsw nuw i32 %index32, 1
542 %exitcond = icmp eq i32 %nexti, %numIterations
543 br i1 %exitcond, label %exit, label %loop
546 %i = phi i32 [ %nexti, %loop2 ], [ 0, %entry ]
548 %j = add nsw i32 %i, 1
550 ; CHECK: --> {(1 + (-1 * %offset)),+,1}<nsw>
551 %index32 = sub nsw i32 %j, %offset
553 %ptr = getelementptr inbounds float, float* %input, i32 %index32
554 %nexti = add nsw i32 %i, 1
555 store float 1.0, float* %ptr, align 4
562 ; Subtraction of two recurrences. The addition in the SCEV that this
563 ; maps to is NSW, but the negation of the RHS does not since that
564 ; recurrence could be the most negative representable value.
565 define void @subrecurrences(i32 %outer_l, i32 %inner_l, i32 %val) {
566 ; CHECK-LABEL: @subrecurrences
571 %o_idx = phi i32 [ 0, %entry ], [ %o_idx.inc, %outer.be ]
572 %o_idx.inc = add nsw i32 %o_idx, 1
573 %cond = icmp eq i32 %o_idx, %val
574 br i1 %cond, label %inner, label %outer.be
577 %i_idx = phi i32 [ 0, %outer ], [ %i_idx.inc, %inner ]
578 %i_idx.inc = add nsw i32 %i_idx, 1
580 ; CHECK-NEXT: --> {{[{][{]}}-1,+,-1}<nw><%outer>,+,1}<nsw><%inner>
581 %v = sub nsw i32 %i_idx, %o_idx.inc
582 %forub = udiv i32 1, %v
583 %cond2 = icmp eq i32 %i_idx, %inner_l
584 br i1 %cond2, label %outer.be, label %inner
587 %cond3 = icmp eq i32 %o_idx, %outer_l
588 br i1 %cond3, label %exit, label %outer