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

            Bug ID: 129122
           Summary: [15/16/17 Regression] ICE (segfault after unbounded
                    memory growth) at -O1 in
                    simplify_gimple_switch_label_vec: switch on a 1-bit
                    signed bit-field, edge loop never advances since
                    r15-3190
           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.c

struct S { int b : 1; };

int f(int y)
{
  struct S s;
  s.b = y ? -1 : 0;
  switch (s.b) {
  case -1: return 1;
  case 0: return 2;
  default: return 3;
  }
}

$ gcc -O1 -c repro.c
during GIMPLE pass: forwprop
repro.c: In function 'f':
repro.c:3:5: internal compiler error: Segmentation fault
    3 | int f(int y)
      |     ^
0x2c5291f internal_error(char const*, ...)
0x13f538f crash_signal
0x1578fdb vec<std::pair<int, int>, va_heap,
vl_embed>::quick_push(std::pair<int, int> const&)
0x1578fdb vec<std::pair<int, int>, va_heap, vl_ptr>::safe_push(std::pair<int,
int> const&)
0x1578fdb simplify_gimple_switch_label_vec
0x1578fdb simplify_gimple_switch
0x158a064 execute

## Some analysis

Before the segfault cc1 runs for ~20 s and grows to ~17 GB (`/usr/bin/time -v`:
maximum resident set size 16936724 kB). On Compiler Explorer the compiler is
killed instead ("Killed signal terminated program cc1"). -O0, -O2, -O3, -Os and
`-O1 -fno-tree-forwprop` compile it.

The loop that collects the dead switch edges in
simplify_gimple_switch_label_vec (tree-ssa-forwprop.cc) never advances:

```
      for (ei = ei_start (gimple_bb (stmt)->succs); (e = ei_safe_edge (ei)); )
        {
          if (! bitmap_bit_p (target_blocks, e->dest->index))
            edges_to_remove.safe_push (std::make_pair (e->src->index,
                                                       e->dest->index));
          else
            ei_next (&ei);
        }
```

Before r15-3190 (03b802e14f4, "Delay edge removal in forwprop", 2024-08-26) the
first branch was `remove_edge (e)`, which deleted `e` from the successor
vector, so `ei_safe_edge (ei)` returned the next edge. Now the edge stays; the
same edge is pushed again on every iteration, and `edges_to_remove` grows until
the allocation fails. The branch is reached when
preprocess_case_label_vec_for_gimple drops a case label as out of range for the
index type: here the 1-bit field has the range [-1, 0], so `default` has no
reachable value and its edge becomes dead. A plain `ei_next (&ei);` after the
push should fix it.

Reproduced in Godbolt (gcc trunk, -O1 -c; cc1 is killed for exceeding the
memory limit): https://godbolt.org/z/WGW1excrv

## Checked versions
- gcc trunk c3129ba5e3e (2026-11-27, --enable-checking=yes): ICE at -O1
(segfault after ~17 GB); -O0/-O2/-O3/-Os ok
- gcc 16.0.1 and 15.2 (local): ICE at -O1
- Godbolt: gcc 15.1, 15.3, 16.2, trunk: cc1 killed at -O1; gcc 12.2, 13.2,
13.4, 14.1, 14.4: ok


Thanks.

Reply via email to