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

Handle undefined ranges in CLZ loop optimization.

Fixes loop optimization for CLZ by correctly handling undefined ranges.

The optimization that converts signed right-shift loops into Count Leading Zeros (CLZ) operations previously mishandled undefined ranges. When determining the number of loop iterations, the Ranger analysis could return an undefined range, which was then incorrectly treated as a valid non-negative range. This change adds explicit checks for undefined ranges, preventing incorrect optimization.

In Details

In tree-ssa-loop-niter.cc, the number_of_iterations_cltz and number_of_iterations_cltz_complement functions optimize signed right-shift loops into CLZ operations. These functions use Ranger on the loop preheader to check for non-negativity. However, range_on_edge returning true only confirms Ranger support, not that the range is defined. This commit adds a check for undefined_p() before applying the non-negativity test, preventing misoptimizations when the range is not fully determined.

For Context
CLZ
Count Leading Zeros, an instruction that counts the number of zero bits from the most significant bit to the first set bit.
loop optimization
Compiler transformations applied to loops to improve their execution speed, such as reducing the number of iterations or simplifying loop-invariant computations.
Ranger
A GCC analysis pass that determines the range of values a variable can take during program execution.
loop preheader
A basic block that unconditionally jumps to the beginning of a loop, used to facilitate loop optimizations by providing a single entry point.
Filed Under: tree-optimizationCLZloop optimization