https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128702

            Bug ID: 128702
           Summary: arm: wrong code at -O1 in ARM mode: the sign of a long
                    long right shift by 32 or more is lost
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: shimizu2486 at gmail dot com
  Target Milestone: ---
            Target: arm-linux-gnueabihf

Live reproducer: https://godbolt.org/z/Mqr9Yf8v7

$ cat t1.c
#include <stdio.h>
__attribute__((noinline)) long long zero(void) { return 0; }
static volatile int X = -1, Y = 32;
int main(void) {
  if (!Y)
    return 0;
  unsigned _BitInt(65) m = zero() - (unsigned _BitInt(65))((long long)X >> Y);
  printf("%d\n", (int)(m >> 64));
}

(long long)-1 >> 32 is -1, so m = 0 - (unsigned _BitInt(65))-1 = 1 and the
program prints m >> 64 = 0.

$ arm-linux-gnueabihf-gcc -marm -O0 -static t1.c -o t1 && qemu-arm ./t1
0                    (correct)
$ arm-linux-gnueabihf-gcc -marm -O1 -static t1.c -o t1 && qemu-arm ./t1
1                    (wrong)

  arm trunk (842e09f03b64)   wrong
  arm 16.2.0                 wrong

-O1 is wrong; -O0, -O2, -O3, -Os and -Og are correct, and so is -O1 with
-mthumb. With Y = 31 the program is correct; with Y = 33 or 63 it is wrong too.
 x86-64, riscv64 are correct.

In the -O1 code, bit 64 of m is computed with "add r3, r3, ip, lsr r2", where
ip s the high word of (long long)X and r2 the shift count, where the sign of
"ip, asr r2" is needed; a register lsr by 32 or more gives 0.

Reply via email to