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

            Bug ID: 128703
           Summary: arm: wrong code at -O2 in ARM mode with a variable
                    _BitInt(96) shift in a loop
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          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/eGsezeoE1

$ cat t1.c
#include <stdio.h>
_BitInt(96) a, s;
_BitInt(113) g;
_BitInt(33) i;
long z;
static volatile int A;
_BitInt(96) shl(_BitInt(96) x, _BitInt(6) y) {
  return x > (unsigned _BitInt(96))1 << 95 ? x : x << y;
}
int main(void) {
  _BitInt(3) x;
  short t = A;
  x = 1;
  if (A)
    z = x = 0;
  for (; i < 1; i++) {
    s = t & (unsigned _BitInt(65))shl(t, x);
    t = a = g ? g : 2;
  }
  long long c = g >> 4;
  for (int k = 0; k < 3; k++)
    c = s;
  printf("%llu\n", (unsigned long long)c);
}

A is 0, so t is 0, the if does nothing, and the loop runs once: s = 0 & (0 <<
1) = 0, so the program prints 0.

$ arm-linux-gnueabihf-gcc -marm -march=armv7-a+fp -mfpu=vfpv3-d16 -O0 -static
t1.c -o t1 && qemu-arm ./t1
0                    (correct)
$ arm-linux-gnueabihf-gcc -marm -march=armv7-a+fp -mfpu=vfpv3-d16 -O2 -static
t1.c -o t1 && qemu-arm ./t1
18446744069414584320 (wrong)

  arm trunk (842e09f03b64)   wrong
  arm 16.2.0                 wrong

-O2 is wrong; -O0, -O1, -O3 and -Os are correct, and so is -O2 with -mthumb. 
With NEON (-mfpu=neon, or -march=armv7-a+simd) the program is correct.
-fdisable-rtl-cse2 or -fdisable-rtl-combine each give the correct value.
x86-64, riscv64 are correct.

Reply via email to