Issue 240163
Summary [ARM] wrong code at -O1: 64-bit shift by an amount that is always 0 sets the low word to -1
Labels generated by fuzzer
Assignees
Reporter hazohelet
    Live Reproducer: https://godbolt.org/z/EGTzfhcon

Compiling the following C code for `arm-linux-gnueabihf` with `clang -O1` and running it prints the wrong result.

```c
#include <stdio.h>
static volatile long long X = 1;
unsigned g;
int main(void) {
  long long x = X;
  unsigned n = -(unsigned)(x & 63);
  g = n;
  printf("%lld\n", x >> ((n + x) & 63));
}
```

clang command:

```sh
clang --target=arm-linux-gnueabihf -O1 repro.c && qemu-arm ./a.out
```

Output (wrong):

```text
4294967295
```

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

```text
1
```

`n` is `-(1 & 63)` = 0xFFFFFFFF, so `n + x` is 0x100000000 and the shift amount `(n + x) & 63` is 0 (for any `x`). The program prints `x >> 0` = 1. The generated code looks like it's instead setting the low word of the result to -1:

```asm
        ldrd    r4, r5, [r0]     @ x
        ...
        mvn     r2, #31          @ r2 = -32
 cmp     r2, #0
        mvnmi   r2, #0           @ low word = -1, should be r4
        asrpl   r5, r5, #31
        ...
        mov     r3, r5
 bl      printf
```

Notes:

* Correct at -O0 and -Oz
* Also wrong at -O2, -O3 and -Os
* Compiling the -O1 IR with `clang -O0` prints the same wrong value, so I assume it's a backend bug

clang version:

```text
clang version 24.0.0git (https://github.com/llvm/llvm-project 3a34e810f3844371c6fa1f164741cf22c3c513ff)
```

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

Reply via email to