| Issue |
233317
|
| Summary |
[IndVarSimplify] Widening an IV expands its `udiv` step in the loop header, before the guard that protects it
|
| Labels |
miscompilation,
llvm:optimizations,
generated by fuzzer
|
| Assignees |
|
| Reporter |
ChoKyuWon
|
### reproducer
godbolt: https://godbolt.org/z/59nvc6cqo
alive: https://alive2.llvm.org/ce/z/ZGCU_j
```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"
target triple = "x86_64-unknown-linux-gnu"
define i64 @f(i32 %n) {
entry:
br label %loop
loop:
%iv = phi i32 [ 0, %entry ], [ %iv.next, %latch ]
%e = sext i32 %iv to i64
%acc.next = or i64 0, %e
%cmp = icmp slt i32 %iv, %n
br i1 %cmp, label %latch, label %exit
latch:
%step = udiv i32 1, %n
%iv.next = add nsw i32 %iv, %step
br label %loop
exit:
ret i64 0
}
define i32 @main() {
%r = call i64 @f(i32 0)
ret i32 0
}
```
```console
$ opt -S -passes=indvars repro.ll
define i64 @f(i32 %n) {
entry:
%0 = zext i32 %n to i64
%1 = sext i32 %n to i64
br label %loop
loop:
%indvars.iv = phi i64 [ %indvars.iv.next, %latch ], [ 0, %entry ]
%2 = udiv i64 1, %0
%cmp = icmp slt i64 %indvars.iv, %1
br i1 %cmp, label %latch, label %exit
latch:
%indvars.iv.next = add nsw i64 %indvars.iv, %2
br label %loop
exit:
ret i64 0
}
$ llubi repro.ll; echo $?
0
$ opt -S -passes=indvars repro.ll | llubi -
Stacktrace:
#0 %2 = udiv i64 1, %0 at @f <stdin>:14
#1 %r = call i64 @f(i32 0) at @main <stdin>:27
Immediate UB detected: Division by zero.
error: Execution of function 'main' failed.
```
### suggested fix
`WidenIV::createWideIV` (SimplifyIndVar.cpp) expands the widened AddRec at the header's first insertion point without checking that the expansion is safe there; with `%n = 0` the latch (and its `udiv`) never runs in the source.
Bail out (`return nullptr`) unless `Rewriter.isSafeToExpandAt(AddRec, InsertPt)`, as the other expansion sites in the same file already do; `isSafeToExpand` rejects a `SCEVUDivExpr` whose divisor is not known non-zero.
> Disclaimer: This test case was generated by a fuzzer, and the report was written with LLM assistance.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs