GCC Newspaper
OCTOBER 6, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
tree-optimization

Avoid undefined behavior in CHREC optimization for signed overflow

Ensures peeled CHREC optimization correctly guards against signed integer overflow to prevent undefined behavior.

An optimization for handling peeled CHRECs (CHange RECords) has been modified to prevent undefined behavior (UB) when dealing with signed integer overflow. Previously, this optimization could incorrectly infer loop bounds, leading to wrong iteration counts. The change guards the optimization when non-wrapping overflow is detected, ensuring correctness and preventing regressions in loop analysis and related optimizations.

In Details

The simplify_peeled_chrec function in tree-scalar-evolution.cc is modified to guard the optimization when analyzing a CHREC with non-wrapping signed integer overflow. This prevents UB that could manifest in infer_loop_bounds_from_signedness and lead to incorrect niter analysis. A regression on gomp/static-chunk-size-one.c is adjusted.

For Context
CHREC
Change Record, a data structure used in GCC's scalar evolution analysis to represent the evolution of a loop induction variable.
peeled CHREC
A CHREC that has been processed or modified in a way that separates the first iteration of a loop from the rest, often to simplify analysis or enable further optimizations.
scalar evolution
A compiler analysis pass that determines how variables change throughout a program, particularly within loops. It's used to find loop-invariant computations and optimize induction variables.
undefined behavior (UB)
Program behavior that results from an operation that is not specified by the C or C++ standards. Compilers can make assumptions about UB, which can lead to unexpected results or optimizations.
loop bounds analysis
The process of determining the starting and ending conditions, and the number of iterations, for loops in a program.
induction variable
A variable in a loop whose value changes in a predictable way with each iteration, often used as a loop counter or to access array elements.
Filed Under: optimizationscalar evolutionUBloops