| Issue |
233432
|
| Summary |
[RISCV][GlobalISel] RV64 legalizer should not preserve UB flags on any-extended 32-bit instructions
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
nchappe
|
The widening logic of the custom RISC-V 64 legalizer of GlobalISel should drop UB flags when operating on any-extended values.
For instance, when adding two scalars that have been any-extended from 32 to 64 bits, the 32 higher bits have unspecified values, so nuw-adding them could trigger an overflow (e.g. if both have their highest bit set) and thus UB.
The two following functions exhibit UB after legalization at `-O0` because of the nuw flag that is erroneously kept (though there is currently no end-to-end miscompilation here):
```llvm
define i64 @f() {
%1 = trunc i64 u0x8000000000000000 to i32
%2 = add nuw i32 %1, %1
%r = zext i32 %2 to i64
ret i64 %r
}
define i64 @g() {
%1 = trunc i64 u0x8000000000000000 to i32
%2 = shl nuw i32 %1, 1
%r = zext i32 %2 to i64
ret i64 %r
}
```
After legalization, this becomes:
```mir
---
name: f
body: |
bb.1 (%ir-block.0):
%0:_(s64) = G_CONSTANT i64 -9223372036854775808
%6:_(s64) = nuw G_ADD %0, %0
%7:_(s64) = G_SEXT_INREG %6, 32
%8:_(s64) = G_CONSTANT i64 4294967295
%3:_(s64) = G_AND %7, %8
$x10 = COPY %3(s64)
PseudoRET implicit $x10
---
name: g
body: |
bb.1 (%ir-block.0):
%0:_(s64) = G_CONSTANT i64 -9223372036854775808
%8:_(s64) = G_CONSTANT i64 1
%7:_(s64) = nuw G_SHL %0, %8(s64)
$x10 = COPY %7(s64)
PseudoRET implicit $x10
```
Both the `nuw G_ADD` and the `nuw G_SHL` overflow here.
See: https://godbolt.org/z/vMv1nbj8Y
This issue affects the nuw and nsw flags for G_ADD, G_SUB, G_SHL in `legalizeCustom` in https://github.com/llvm/llvm-project/blob/main/llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp#L1671.
I did not find any other affected instruction, nor any other affected target.
Related: #192675.
Cc: @regehr
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs