| Issue |
244520
|
| Summary |
[SelectionDAG] Wrong code at every -O level: i65 `mul` + `add` stored with sext gets bit 64 set (`_BitInt(65)`)
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
haoxintu
|
## Description
```c
_BitInt(65) c;
__attribute__((noinline)) void
d (long e, long f)
{
c = (_BitInt(65)) e * 11400714819323198485wb + ((_BitInt(65)) f + (f < -1)) * 30;
}
int main (void)
{
d (1, 0);
/* 1 * 11400714819323198485 + (0 + 0) * 30 fits in signed _BitInt(65) (< 2^64): it must stay positive. */
if (c != 11400714819323198485wb)
__builtin_abort ();
return 0;
}
```
```
$ clang -O0 repro.c && ./a.out # Aborted (also -O1, -O2, -O3, -Os)
$ gcc -O2 repro.c && ./a.out # ok
```
The low 64 bits are right (`0x9e3779b97f4a7c15`), but bit 64, the sign bit of `_BitInt(65)`, is set. So `c` becomes negative.
`-fsanitize=address,undefined` is clean.
The bug is in the back end. `llc -O0` and `llc -O2` on this IR store bit 64 = 1 for `d(1, 0)`:
```llvm
define void @d(i64 %e, i64 %f) {
%conv = sext i64 %e to i65
%mul = mul i65 %conv, 11400714819323198485
%a = sext i64 %f to i65
%b = icmp slt i64 %f, -1
%z = zext i1 %b to i65
%c2 = add i65 %a, %z
%m3 = mul i65 %c2, 30
%a4 = add i65 %mul, %m3
%s = sext i65 %a4 to i128
store i128 %s, ptr @c, align 8
ret void
}
```
The `(sext f + zext (f < -1)) * 30` term is 0 here. Removing the icmp/zext, or the `* 30`, makes the result correct.
Reproduced in Godbolt (x86-64 clang assertions trunk, -O2): https://godbolt.org/z/h1hjd9xMP
Thanks.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs