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

            Bug ID: 129011
           Summary: wrong code at -O1 and -Os: a signed _BitInt(3) struct
                    member copied through SRA reads back without sign
                    extension (5 instead of -3)
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: haoxintu at gmail dot com
  Target Milestone: ---

Hi,

$cat repro-small.c

struct S { _BitInt(3) e; };

__attribute__((noipa)) long f(long g, long i) {
  struct S a = {}, b;
  long acc = b.e = g + i;          /* 5 wraps to -3 in _BitInt(3) */
  struct S c = i ? a : b;
  a = c;
  return acc ^ a.e * 3 ^ b.e * 3;  /* -3 ^ -9 ^ -9 = -3 */
}

int main(void) {
  if (f(5, 0) != -3)
    __builtin_abort();
  return 0;
}

$ gcc -O0 repro-small.c && ./a.out        # ok (also -O2, -O3)
$ gcc -O1 repro-small.c && ./a.out        # also -Os
Aborted
$ gcc -O1 -fno-tree-sra repro-small.c && ./a.out       # ok (also
-fno-tree-forwprop)


Reproduced in Godbolt (gcc trunk, -O1 vs -O0): https://godbolt.org/z/PxMKPcaYW

## Checked versions
- gcc trunk 1ed04a18ef6 (2026-11-23, --enable-checking=yes): -O1, -Os abort;
-O0, -O2, -O3 ok
- gcc 16.0.1/gcc 15.2.0/gcc-14.1: -O1 aborts
- clang 21: ok

Thanks.

Reply via email to