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

            Bug ID: 128783
           Summary: [16/17 Regression] riscv64: wrong code at -O2:
                    __builtin_mul_overflow of two signed chars narrowed
                    from an __int128 never reports overflow
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: middle-end
          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/1xnhxscba

$ cat t1.c
#include <stdio.h>
__attribute__((noipa)) int f(int x, _Bool b) {
  __int128 p;
  __builtin_mul_overflow(x, b, &p);
  signed char t = p;
  return __builtin_mul_overflow_p(t, t, (signed char)0);
}
int main(void) { printf("%d\n", f(-128, 1)); }

p is -128, so t is -128, and t * t = 16384 does not fit in a signed char, so
the program should print 1.

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

  riscv64 trunk (c548a927b535)   wrong
  riscv64 16.2.0                 wrong
  riscv64 15.3.0                 correct
  riscv64 14.4.0                 correct

-O2, -O3 and -Os are wrong. -O0, -Og and -O1 are correct. -march=rv64gcv is
wrong likewise.
-fno-expensive-optimizations or -fdisable-tree-widening_mul gives the correct
value.
x86-64, aarch64, loongarch64, powerpc64le and powerpc64 print 1 (which's
correct).
  • [Bug middle-end/128783] New: [16... shimizu2486 at gmail dot com via Gcc-bugs

Reply via email to