Issue 234329
Summary [TypePromotion] Promoted `icmp` keeps `samesign`, so a defined compare becomes poison
Labels llvm:codegen, miscompilation, generated by fuzzer
Assignees
Reporter ChoKyuWon
    ### reproducer
godbolt: https://godbolt.org/z/G6Tss6MeY
alive: https://alive2.llvm.org/ce/z/nNirkN

```llvm
target triple = "aarch64-unknown-linux-gnu"

define i32 @f(i8 %a) {
entry:
  %s = sub i8 %a, 1
  %c = icmp samesign ule i8 %s, 200
  br i1 %c, label %t, label %e
t:
  ret i32 1
e:
  ret i32 2
}
```

```console
$ opt -S -passes=typepromotion repro.ll
define i32 @f(i8 %a) {
entry:
  %0 = zext i8 %a to i32
  %s = sub i32 %0, 1
  %c = icmp samesign ule i32 %s, 200
 br i1 %c, label %t, label %e
...

$ alive-tv --disable-undef-input --disable-poison-input -passes=typepromotion repro.ll
ERROR: Source is more defined than target

Example:
i8 %a = #x00 (0)

Source:
i8 %s = #xff (255, -1)
i1 %c = #x0 (0)
  >> Jump to %e

Target:
i32 %#0 = #x00000000 (0)
i32 %s = #xffffffff (4294967295, -1)
i1 %c = poison
UB triggered on br
```

### suggested fix
TypePromotion zero-extends the sources of an unsigned `icmp` and `IRPromoter::PromoteTree` remaps safe-wrap compare constants to `-zext(-C)`, which can change the operands' sign bits, but the promoted `icmp` keeps `samesign`.
Drop it there, as it already drops `nsw` from promoted `trunc ... to i1`: `cast<ICmpInst>(I)->setSameSign(false)`.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to