https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127345

            Bug ID: 127345
           Summary: [16/17 Regression] wrong code storing a signed
                    _BitInt(N) struct
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: shimizu2486 at gmail dot com
  Target Milestone: ---

Live Reproducer: https://godbolt.org/z/Wx378zEnT
Storing a signed _BitInt(N) into a struct member, where N does not fill its
container, gives the wrong value from -O2 up.

$ cat t1.c
#include <stdio.h>
int a1_0;
long in6;
typedef struct { _BitInt(3) f1; } S0;
int main(void) {
  long v0 = 0;
  S0 s0;
  if (!in6) {
    for (; a1_0; v0 = 1)
      ;
    s0.f1 = v0 - 1;
  }
  printf("%llu\n", (unsigned long long)(long long)s0.f1);
}

a1_0 and in6 are zero, so the loop never runs, v0 stays 0, and s0.f1 is -1.

$ gcc -O0 t1.c -o t1 && ./t1
18446744073709551615                (correct)
$ gcc -O2 t1.c -o t1 && ./t1
0                                   (wrong)

  15.3                  correct
  16.2                  wrong
  trunk (b5a11752d) wrong

The program does not trigger UB.

Reply via email to