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

            Bug ID: 128997
           Summary: [15/16/17 Regression] riscv64: wrong code at -Os:
                    (int)(g >> 16) is 0 for an __int128 g of 1 << 16
           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: riscv64-linux-gnu

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

$ cat t1.c
#include <stdio.h>
int j, m;
__int128 g;
signed char v;
int main(void) {
  short x = 0;
  if (!j)
    g = 1 << 16;
  for (; j; j++) {
    v = m || x;
    if (g == v)
      x = v;
  }
  printf("%d\n", (int)(g >> 16));
}

j is 0, so g is set to 1 << 16 and the loop does not run.  g >> 16 is 1, so the
program should print 1.

$ riscv64-linux-gnu-gcc -O2 -static t1.c -o t1 && qemu-riscv64 ./t1
1                    (correct)
$ riscv64-linux-gnu-gcc -Os -static t1.c -o t1 && qemu-riscv64 ./t1
0                    (wrong)

  riscv64 trunk (2f817b3ea9e9)   wrong
  riscv64 16.2.0, 15.3.0         wrong
  riscv64 14.4.0                 correct

-Os is wrong, but -O0, -O1, -O2, -O3 and -Og are correct.
-march=rv64gcv, -march=rv64gc_zba and -march=rv64gc_zbb are wrong too.
-fno-cse-follow-jumps, -fno-move-loop-invariants, -fdisable-rtl-cse1 or
-fdisable-rtl-combine gives 1 correctly.
x86_64, powerpc64le and powerpc64 print 1 correctly.

Reply via email to