https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128728
Bug ID: 128728
Summary: [17 Regression] wrong code at -O2 for upper bits of a
product of two 3-bit bit-fields
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: shimizu2486 at gmail dot com
Target Milestone: ---
Target: aarch64-linux-gnu loongarch64-linux-gnu
riscv64-linux-gnu
Live reproducer: https://godbolt.org/z/n1rbf1nE8
>From -O2 up, p >> 4 of an unsigned char product of two 3-bit bit-fields comes
out one too small.
$ cat t1.c
#include <stdio.h>
struct S { int v : 3; };
static volatile int A = -3, B = 2;
int main(void) {
struct S a = { A }, b = { B };
unsigned char p = b.v * (unsigned)a.v;
printf("%d\n", (struct S){ p >> 4 }.v);
}
a.v is -3 and b.v is 2, so p is 250 and p >> 4 is 15, which is -1 as a 3-bit
bit-field. The program should print -1.
$ aarch64-linux-gnu-gcc -O0 -static t1.c -o t1 && qemu-aarch64 ./t1
-1 (correct)
$ aarch64-linux-gnu-gcc -O2 -static t1.c -o t1 && qemu-aarch64 ./t1
-2 (wrong)
aarch64 trunk (372c32f911fa) wrong
aarch64 16.2.0 correct
aarch64 15.3.0 correct
aarch64 14.4.0 correct
-O0 and -O1 are correct. -O2, -O3 and -Os are wrong.
-fno-expensive-optimizations makes -O2 print -1.
loongarch64, riscv64 trunk are wrong too. x86_64 trunk prints -1.