| Issue |
235297
|
| Summary |
[InstCombine] foldSelectCttzCtlz flips cttz/ctlz isZeroPoison false→true without guarding undef true-value — miscompile on select(icmp x,0,K,cttz(x,false))
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
leidian977
|
## Summary
`InstCombine::foldSelectCttzCtlz` flips the `isZeroPoison` flag of
`llvm.cttz`/`llvm.ctlz` from `false` to `true` when folding
`select (icmp eq X, 0), K, c[t]tz(X, false)` into `c[t]tz(X, true)`.
The fold is unsound when the value feeding the intrinsic is `undef`:
with `isZeroPoison=true`, `ctlz(undef)` becomes `poison`, whereas the
original `select` form yields a defined value. This is a silent
miscompile.
This is the `undef` half of the case fixed for `poison` by
`700febc04ca` ("Fix incorrect is_zero_poison when fold"); the `undef`
side is still unguarded.
## Reduced test case (llvm-reduce'd, 4 lines)
```llvm
define i8 @h8(i8 %a0) {
%z1 = icmp eq i8 %a0, 0
%t1 = call i8 @llvm.ctlz.i8(i8 %a0, i1 false)
%v1 = select i1 %z1, i8 0, i8 %t1
ret i8 %v1
}
declare i8 @llvm.ctlz.i8(i8, i1 immarg)
```
## Reproduction
$ opt -S -passes=instcombine f1.ll
define i8 @h8(i8 %a0) {
%z1 = icmp eq i8 %a0, 0
%t1 = call range(i8 0, 9) i8 @llvm.ctlz.i8(i8 %a0, i1 true) ; false -> true
%v1 = select i1 %z1, i8 0, i8 %t1
ret i8 %v1
}
## Verification (alive-tv counterexample)
$ alive-tv f1.ll optimized.ll
Transformation doesn't verify!
ERROR: Target is more poisonous than source
Example:
i8 %a0 = undef
Source:
i1 %z1 = #x0 (0) [based on undef]
i8 %t1 = #x06 (6) [based on undef]
i8 %v1 = #x06 (6) [based on undef]
Target:
i1 %z1 = #x0 (0)
i8 %t1 = poison
i8 %#range_0_%t1 = poison
i8 %v1 = poison
Source value: #x06 (6) [based on undef]
Target value: poison
With `%a0 = undef`, the source yields a defined value (6) while the
optimized version yields `poison` — the transformation is not
semantics-preserving.
## Root cause
`llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp:1600`
(`foldSelectCttzCtlz`, called at :2545). The guard does not consider
that flipping `i1 false -> true` is invalid when the select's true-value
operand feeding the intrinsic can be `undef` (the `poison` case was
fixed by `700febc04ca`; the `undef` case remains).
## Suggested fix
Before flipping `isZeroPoison`, add an `isUndef`-conservative check:
only allow `false -> true` when the select's true value is provably not
`undef`.
## Environment
- LLVM `24.0.0git`, built from git HEAD `cc2acc33a5b`
- Reproduced with `opt -S -passes=instcombine` and `alive-tv`
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs