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

Loop invariant code motion honors -fwrapv-pointer

Loop invariant analysis now correctly considers pointer wrapping behavior when `-fwrapv-pointer` is enabled.

The nowrap_type_p function, used by loop invariant optimization (IVOPTs) and loop iteration analysis, incorrectly assumed pointer types never wrap. This commit updates tree-ssa-loop-niter.cc to properly honor the -fwrapv-pointer flag, ensuring that pointer arithmetic is analyzed with the correct wrapping semantics. Two test cases are added to verify this correction.

In Details

The nowrap_type_p predicate, crucial for several loop optimizations like IVOPTs and loop iteration counting, failed to account for the -fwrapv-pointer flag. It incorrectly treated all POINTER_TYPE_P as non-wrapping. This change modifies the predicate to respect the flag, ensuring accurate analysis when pointer arithmetic might wrap around.

For Context
-fwrapv-pointer
A GCC flag that instructs the compiler to assume that signed integer arithmetic, including pointer arithmetic, wraps around on overflow, matching the behavior of two's complement arithmetic.
IVOPTs
Loop Invariant Code Motion optimizations. These passes identify expressions within loops whose values do not change with each iteration and move them outside the loop to reduce redundant computations.
niter analysis
Analysis performed by the compiler to determine the number of iterations a loop is expected to execute. This information is used to guide other optimizations.
Filed Under: loop-optimizationoptimizationpointer-arithmetic