https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129090
Bug ID: 129090
Summary: [14/15/16/17 Regression] hang at -O1 and above:
infinite loop in match_uaddc_usubc (widening_mul) when
both __imag__ operands come from the same
.SUB_OVERFLOW call
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: compile-time-hog
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
unsigned long f(unsigned long acc, unsigned long y, unsigned long z, unsigned
long a)
{
for (int i = 0; i < 3; i++) {
unsigned long o;
_Bool c = __builtin_sub_overflow(y, z, &o);
a += i;
acc -= o + c + a;
}
return acc;
}
$ gcc -O2 -c repro.c # never finishes (killed after 600 seconds)
$ gcc -O1 -c repro.c # ok
$ gcc -O2 -fdisable-tree-widening_mul -c repro.c # ok
repro-gcc14.c beelow (a variant with `if (i & 1) a += i;`, nine iterations and
a constant minuend) hangs at -O3 on GCC 14.1 and above:
```
unsigned long f(unsigned long acc, unsigned long z, unsigned long a)
{
for (int i = 0; i < 9; i++) {
if (i & 1)
a += i;
unsigned long o;
_Bool c = __builtin_sub_overflow(5ul, z, &o);
acc = acc - (o + c + a);
}
return acc;
}
```
Reproduced in Godbolt (gcc trunk, -O2 -c; "Killed - processing time exceeded"):
https://godbolt.org/z/oz77b979M
## Checked versions
- gcc trunk 4eca40b54dd (2026-11-25, checking): repro.c hangs at -O2/-O3/-Os,
-O1 ok; repro-gcc14.c hangs at -O3. Godbolt trunk: same.
- gcc 16.0.1 20260315 (local) and 16.2 (Godbolt cg162): repro.c hangs at -O2.
- gcc 15.3, 14.4: repro.c compiles (different unrolling/reassociation, no such
chain), but repro-gcc14.c hangs at -O3 on 14.1, 14.2, 14.4, 15.2, 15.3, 16.1,
16.2.
- gcc 13/12/11 (local): compile both (no match_uaddc_usubc).
Thanks.