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

            Bug ID: 128999
           Summary: [14/15/16/17 Regression] ICE in as_a, at
                    machmode.h:391 (emit_cstore) at -O2: phiopt4 turns a
                    PHI <0, -1> of unsigned _BitInt(256) into -(T)cond
                    after bitintlower
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: ice-on-valid-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

typedef unsigned _BitInt(256) T;

T f(T a, T b) {
  return ((b & 1 ? a : b) ? a : 0) != 0 ? 1
         : ~(a < ~b ? a : b & 1 ? a : b);
}

T g(T h, unsigned n) {
  T r = 1;
  for (unsigned d = 0; d < n; d++) {
    r += h;
    h = f(f(h, d), h);
  }
  return r;
}

static T i(T h, T j) { return h ? f(j, h) : j; }

T k(T b, unsigned n) { return g(i(b, 0), n); }


$ gcc -O2 -c repro-small.c
during RTL pass: expand
repro-small.c: In function 'k':
repro-small.c:19:31: internal compiler error: in as_a, at machmode.h:391
   19 | T k(T b, unsigned n) { return g(i(b, 0), n); }
      |                               ^~~~~~~~~~~~~
0xbafa79 fancy_abort(char const*, int, char const*)
0x906038 scalar_int_mode as_a<scalar_int_mode>(machine_mode)
        ../../gcc/machmode.h:391
0x90637e emit_cstore(rtx_def*, insn_code, rtx_code, machine_mode, machine_mode,
int, rtx_def*, rtx_def*, int, machine_mode)
0xede95b emit_store_flag_1
0xedef6b emit_store_flag(...)
0xee09fc emit_store_flag_force(...)
0xef0353 do_store_flag
0xef1a79 expand_expr_real_2(...)
```

After inlining, `i (b, 0)` folds to `b != 0 ? -1 : 0`, and bitintlower leaves
the PHI alone. Before phiopt4 (`-fdump-tree-sink2`), `k` is:

```
  _41 = _26 | _32;                 // b != 0, computed on the limbs by
bitintlower
  if (_41 != 0) goto <bb 4>; else goto <bb 3>;
  <bb 3>:
  <bb 4>:
  # prephitmp_4 = PHI <0(3), 0xffff...ffff(2)>
  b = g (prephitmp_4, n_5(D)); [return slot optimization]
```

phiopt4 runs after bitintlower. It replaces the PHI with large _BitInt
arithmetic (`-fdump-tree-phiopt4`):

```
  _25 = _41 != 0;
  _28 = (unsigned _BitInt(256)) _25;
  _37 = -_28;
  b = g (_37, n_5(D)); [return slot optimization]
```

Nothing lowers these statements anymore. Expand then tries to emit the
bool-to-_BitInt(256) store-flag in BLKmode and hits the as_a<scalar_int_mode>
assertion in emit_cstore. `-fdisable-tree-phiopt4` makes it compile. -O0, -O1,
-O3 and -Os compile it. With `_BitInt(255)` it also ICEs; with `_BitInt(128)`
or `_BitInt(512)` it compiles.


Reproduced in Godbolt (gcc trunk, -O2 -c): https://godbolt.org/z/16YojGj67

## Checked versions
- gcc trunk 1ed04a18ef6 (2026-11-23, --enable-checking=yes): ICE in as_a, at
machmode.h:391 (emit_cstore)
- gcc 16.0.1 20260315 (Ubuntu): ICE in convert_move, at expr.cc:226
- gcc 15.2.0 (Ubuntu): ICE in convert_move, at expr.cc:222
- gcc 14.1 (Ubuntu): ICE in convert_move, at expr.cc:222
- clang: compiles

Thanks.

Reply via email to