https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129207
Bug ID: 129207
Summary: wrong code at -O2: (int) of an unsigned _BitInt(7)
array element held in a register is not masked
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: jeffchen006 at gmail dot com
Target Milestone: ---
Target: x86_64-pc-linux-gnu
int printf(const char *, ...);
int main(void)
{
unsigned _BitInt(7) x[16];
for (int i = 0; i < 16; i++)
x[i] = 0;
for (int i = 0; i < 16; i++)
((unsigned char *)x)[i] |= 128;
printf("%d\n", (int)x[0]);
return 0;
}
$ gcc -std=c23 -O1 t.c && ./a.out
0
$ gcc -std=c23 -O2 t.c && ./a.out
128
Wrong at -O2, -O3. -fno-tree-loop-vectorize gives 0.
Bisected to Wrong since _BitInt support: r14-3754-gf76ae4369cb ("C _BitInt
incremental fixes"), the first commit that compiles this test, already fails.
After the byte stores are vectorized, x is no longer addressable and lives in a
TImode register. expand reads x[0] as zero_extend (subreg:QI) without masking
to 7 bits, so the padding bit set through unsigned char shows up in the value
(128). The x86-64 psABI leaves _BitInt padding bits unspecified, so the read
has to mask them; in memory (-O1) it does.
Clean with -fsanitize=address,undefined (gcc and clang).
gcc version 17.0.0 20261201 (experimental), r17-6987-gdeb313acd22,
x86_64-pc-linux-gnu
Configured with: --enable-languages=c,c++ --disable-multilib
--disable-bootstrap --enable-checking=release