| Issue |
234049
|
| Summary |
[IndVarSimplify] LFTR switches the exit test to a dynamically dead IV and keeps `nuw` that wraps on its first increment
|
| Labels |
miscompilation,
llvm:optimizations,
generated by fuzzer
|
| Assignees |
|
| Reporter |
ChoKyuWon
|
### reproducer
godbolt: https://godbolt.org/z/8sh39f5nx
alive: https://alive2.llvm.org/ce/z/nxZzft
```llvm
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
define i32 @test(ptr %p, i1 %always_false, i32 %limit) {
entry:
br label %loop
loop:
%iv = phi i32 [ 1, %entry ], [ %iv.inc, %always_taken ]
%iv2 = phi i64 [ -1, %entry ], [ %iv2.inc, %always_taken ]
br i1 %always_false, label %never_taken, label %always_taken
never_taken:
store i64 %iv2, ptr %p
br label %always_taken
always_taken:
%iv.inc = add nsw i32 %iv, 1
%iv2.inc = add nuw nsw i64 %iv2, 1
%cmp = icmp slt i32 %iv, %limit
br i1 %cmp, label %loop, label %exit
exit:
ret i32 %iv
}
```
```console
$ opt -S -passes=indvars repro.ll
always_taken:
%iv2.inc = add nuw nsw i64 %iv2, 1
%exitcond = icmp ne i64 %iv2.inc, %wide.trip.count
br i1 %exitcond, label %loop, label %exit
$ alive-tv --disable-undef-input --disable-poison-input --src-unroll=2 --tgt-unroll=2 -passes=indvars repro.ll
ERROR: Source is more defined than target
i1 %always_false = #x0 (0)
i32 %limit = #x00000000 (0)
UB triggered on br
# repro-main.ll = repro.ll + main() calling @test(ptr %slot, i1 false, i32 0)
$ llubi repro-main.ll; echo $?
1
$ opt -S -passes=indvars repro-main.ll | llubi -
Immediate UB detected: Branch on poison condition.
```
### suggested fix
`linearFunctionTestReplace` resets the increment's flags from the post-inc AddRec `{0,+,1}`, which does not cover the first increment `-1 +nuw 1` of an IV that was dynamically dead (the TODO there describes this case).
When LFTR switches to an IV the old exit test was not based on, drop `nuw`/`nsw` on `IncVar` unless `mustExecuteUBIfPoisonOnPathTo(IncVar, ExitingBB->getTerminator(), DT)` holds, as `FindLoopCounter` and the `SafeToPostInc` check in `linearFunctionTestReplace` already do for pointer IVs.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs