https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127401
Bug ID: 127401
Summary: wrong code at -O2/-O3/-Os/-Oz
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: kyuwoncho18 at gmail dot com
Target Milestone: ---
### Reproducer
#include <stdio.h>
struct S { _Bool a; _Bool b; };
union U { struct S s; unsigned char c[2]; };
union U u;
volatile unsigned char va = 1;
volatile unsigned char vb = 2; /* s.b's representation is neither 0 nor 1 */
volatile int vc = 1;
volatile int vd = 1;
__attribute__((noinline)) static int
f (struct S *s, int c, int d)
{
_Bool r, q;
if (c) r = s->a; else r = s->b; /* adjacent _Bool fields ->
hoist_adjacent_loads */
if (d) q = 1; else q = s->b; /* then folds to q = d | s->b
unconditionally */
return (int) r * 16 + (int) q;
}
int
main (void)
{
u.c[0] = va;
u.c[1] = vb;
printf ("%d\n", f (&u.s, vc, vd));
return 0;
}
With -O0/-O1, it prints 17, but with higher optimization, it prints 19.
Reproduced: 14.1 to trunk