https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128910
Bug ID: 128910
Summary: [17 Regression] riscv64: wrong code at -O2: x ==
(unsigned) x being true for x = 0x100000001
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: riscv64-linux-gnu
Live reproducer: https://godbolt.org/z/1nfxvjnro
$ cat t1.c
#include <stdio.h>
__attribute__((noipa)) int f(unsigned long long x, int b) {
int t = 0, r = 0;
unsigned lo = x;
for (int i = 0; i < 3 && (unsigned)x; i++) {
if (b || t)
r = x == lo;
t = x = i != 0;
}
return r;
}
int main(void) { printf("%d\n", f(0x100000001, 1)); }
x is 0x100000001, so lo is 1 and x == lo is false. The first iteration sets r
to 0 and x to 0 (i is 0), which ends
the loop, so f returns 0 and the program should print 0.
$ riscv64-linux-gnu-gcc -O1 -static t1.c -o t1 && qemu-riscv64 ./t1
0 (correct)
$ riscv64-linux-gnu-gcc -O2 -static t1.c -o t1 && qemu-riscv64 ./t1
1 (wrong)
riscv64 trunk (9b667b8158e7) wrong
riscv64 16.2.0, 15.3.0, 14.4.0 correct
-O2 is wrong. -O0, -O1, -O3, -Os and -Og are correct.
With Zba (-march=rv64gc_zba) it is correct.
-fno-move-loop-invariants gives 0 correctly.
x86_64, arm, powerpc64le and powerpc64 print 0 correctly.