https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128727
Bug ID: 128727
Summary: [17 Regression] aarch64: truncation of a sign-extended
_BitInt(9) to unsigned short is dropped
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: aarch64-linux-gnu
Live reproducer: https://godbolt.org/z/YvqhG79e9
>From -O2 up, the conversion to unsigned short of a value sign-extended from
_BitInt(9) is lost, and the 32-bit value is used instead.
$ cat t1.c
#include <stdio.h>
static volatile int X = 511;
static volatile unsigned long long Y = 0;
static int f(int a) { return a > 255 ? a : 0; }
int main(void) {
int g = X;
unsigned long long m = (_BitInt(9))g ? (_BitInt(9))f(g) : 0;
unsigned short v = m;
printf("%llu\n", Y * 1003 + v);
}
X is 511, so (_BitInt(9))g is -1, f(g) is 511 and m is (unsigned long long)-1.
v is 65535, and so is the printed sum.
$ aarch64-linux-gnu-gcc -O1 -static t1.c -o t1 && qemu-aarch64 ./t1
65535 (correct)
$ aarch64-linux-gnu-gcc -O2 -static t1.c -o t1 && qemu-aarch64 ./t1
4294967295 (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. -mno-narrow-gp-writes
makes -O2 print 65535, and so do -fno-ext-dce and -fno-tree-pre. x86_64,
riscv64 and loongarch64 trunk print 65535.
At -O2 the `sxth x0, w1` and `and x0, x0, 65535` that 16.1.0 emits before the
madd are missing:
csel w0, w1, wzr, gt
.L2:
...
madd x1, x2, x1, x0