| Issue |
233686
|
| Summary |
[DAGCombiner] `fmul C, (uitofp (trunc (shl 1, n)))` becomes an exponent add although the truncated value can be 0
|
| Labels |
miscompilation,
llvm:SelectionDAG,
generated by fuzzer
|
| Assignees |
|
| Reporter |
ChoKyuWon
|
### reproducer
godbolt: https://godbolt.org/z/noMWqs8dr
```llvm
source_filename = "repro.ll"
target triple = "x86_64-pc-linux-gnu"
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"
@fmt = private unnamed_addr constant [4 x i8] c"%d\0A\00"
declare i32 @printf(ptr, ...)
define double @calc(i64 %cnt) noinline {
entry:
%wide = shl nuw i64 1, %cnt
%narrow = trunc i64 %wide to i8
%u = uitofp i8 %narrow to double
%r = fmul double 9.000000e+00, %u
ret double %r
}
define i32 @main() {
entry:
%v = call double @calc(i64 8)
%iszero = fcmp oeq double %v, 0.000000e+00
%selected = select i1 %iszero, i32 1, i32 0
%p = call i32 (ptr, ...) @printf(ptr @fmt, i32 %selected)
ret i32 0
}
```
```console
$ llc -O3 -mtriple=x86_64-unknown-linux-gnu repro.ll -o -
calc:
movzbl %dil, %eax
shlq $52, %rax
movabsq $4621256167635550208, %rcx # imm = 0x4022000000000000
addq %rax, %rcx
movq %rcx, %xmm0
retq
$ llubi repro.ll
1
$ clang -O0 repro.ll && ./a.out
1
$ clang -O3 -Xclang -disable-llvm-passes repro.ll && ./a.out
0
```
### suggested fix
`takeInexpensiveLog2` (DAGCombiner.cpp) begins with `Op = PeekThroughCastsAndTrunc(Op)`, so for `trunc (shl 1, n)` it matches `log2(1 << n) = n` on the wide shift, and `combineFMulOrFDivWithIntPow2` folds the `fmul` into an exponent add.
After the `trunc` to i8 the value is 0 for `n >= 8`, which is not a power of two. Only look through a `TRUNCATE` when the narrow value is known non-zero (e.g. the shift amount is known to be less than the narrow width), or stop looking through `TRUNCATE` here.
> 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