Issue 241910
Summary [PowerPC] wrong code at -O2: `xori` of a register known to be 0 with 65535 becomes `li -1` on big-endian powerpc64
Labels generated by fuzzer
Assignees
Reporter hazohelet
    Live Reproducer: https://godbolt.org/z/MsMqsM9z7

Compiling the following C code for `powerpc64` with `clang -O2` and running it prints the wrong result.

```c
#include <stdio.h>
static volatile int C = 1;
unsigned long long x;
_BitInt(81) r;
int main(void) {
  _BitInt(81) a = x, c = C ? a : 0;
  if (C) {
    x = 1;
    c = a;
  }
  if (C)
    r = ~c;
 printf("%lld\n", (long long)(r >> 64));
}
```

clang command:

```sh
clang --target=powerpc64-linux-gnu -static -O2 repro.c && qemu-ppc64 ./a.out
```

Output (wrong):

```text
65535
```

Expected (what -O0 prints, and what powerpc64le and x86-64 print):

```text
-1
```

`x` is 0, so `c` is 0, `r = ~c` is -1 and `r >> 64` is -1. The high word of `~c` is built with `xori 3, 3, 65535` and `xoris 3, 3, 1`, where `r3` is 0 (`li 3, 0` in the entry block). At -O2 the `xori` is replaced by `li 3, -1`, although `0 ^ 65535` is 65535:

```asm
 addis 5, 2, r@toc@ha
        li 3, -1
        not     4, 6
 addi 6, 5, r@toc@l
        xoris 3, 3, 1
```

With `-mllvm -ppc-convert-rr-to-ri=false` the same block has `xori 3, 3, 65535` in place of `li 3, -1`.

Notes:

* Correct at -O0 and -O1; -O2, -O3 and -Os are wrong, also with `-mcpu=pwr9`
* Correct for `powerpc64le`, and with `-mllvm -ppc-convert-rr-to-ri=false`

clang version:

```text
clang version 24.0.0git (https://github.com/llvm/llvm-project 7d25e4f9cd72686727beaf0b661ce3e910ee1349)
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to