https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70309
Bug ID: 70309 Summary: wrong code at -O2 and -O3 in both 32-bit and 64-bit modes on x86_64-linux-gnu Product: gcc Version: 6.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: chengniansun at gmail dot com Target Milestone: --- The following code is miscompiled by the trunk at -O2 and -O3 in both 32-bit and 64-bit modes on x86_64-linux-gnu. This bug also affects gcc-4.8 and later versions. gcc-4.7 is okay. $: gcc-trunk -v Using built-in specs. COLLECT_GCC=gcc-trunk COLLECT_LTO_WRAPPER=/usr/local/gcc-trunk/libexec/gcc/x86_64-pc-linux-gnu/6.0.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: ../gcc-source-trunk/configure --enable-languages=c,c++,lto --prefix=/usr/local/gcc-trunk --disable-bootstrap Thread model: posix gcc version 6.0.0 20160319 (experimental) [trunk revision 234350] (GCC) $: $: gcc-trunk -O2 small.c ; ./a.out ; echo $? 1 $: gcc-trunk -O1 small.c ; ./a.out ; echo $? 0 $: gcc-4.8 -O3 small.c ; ./a.out ; echo $? 1 $: cat small.c typedef struct { unsigned b0:1; unsigned b1:1; unsigned b2:1; unsigned b3:1; unsigned b4:1; unsigned b5:1; unsigned b6:1; unsigned b7:1; unsigned b8:1; unsigned b9:1; unsigned b10:1; unsigned b11:1; unsigned b12:1; unsigned b13:1; unsigned b14:1; unsigned b15:1; } BitField; BitField BA; unsigned short *pA = (unsigned short *) &BA; BitField BB; unsigned short *pB = (unsigned short *) &BB; char * fn(void) { if (BA.b11) { if (BB.b11) return "F"; else return "S"; } else return "F"; } int main (void) { *pA = 0xedcb; *pB = 0x1234; return fn ()[0] == 'F'; } $: