GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
niter Performance Win

Loop iteration count calculation improved for bitwise operations

GCC's loop analysis for bitwise operations now correctly handles cases where loop variables might be zero, improving iteration count accuracy and performance.

This commit refines GCC’s number_of_iterations_cltz_complement analysis to more accurately calculate loop iterations, particularly for loops involving bitwise operations like clz (count leading zeros). It addresses a bug where the analysis assumed loop variables were non-zero, leading to incorrect results or inefficient code generation for certain inputs. The fix ensures that the analysis correctly provides a value when the loop variable is zero, improving code for functions like sqrtllu, resulting in fewer instructions on various architectures.

In Details

The number_of_iterations_cltz_complement analysis has been modified to ensure that the complement count always has a defined value, even when the initial value T of the induction variable (IV) is zero. Previously, build_cltz_expr would construct T != 0 ? .CLZ(T) : PREC, and subsequent gimplification would evaluate .CLZ(T) for T=0, leading to undefined results. The corrected analysis now uses build .CLZ(T, PREC) directly, avoiding the select, and simplify_using_initial_conditions is used upfront to remove the select if T is known to be non-zero. This improves generated code…

For Context
niter
Likely refers to the 'number of iterations' analysis pass in GCC's optimizer, which tries to determine how many times a loop will execute.
clz
Count Leading Zeros. An instruction or function that counts the number of zero bits from the most significant bit down to the first set bit.
induction variable (IV)
A variable in a loop whose value changes with each iteration, typically used for loop control or accumulating results.
GIMPLE
GCC's Internal Representation (IR) used in the middle-end of the compiler. It's a three-address code form designed for optimization.
sccp
Sparse Conditional Constant Propagation. A compiler optimization pass that determines constant values and propagates them through the code.
LZCNT
Leading Zero Count instruction. A processor instruction that performs the CLZ operation efficiently.
PREC
Precision. In the context of bitwise operations like CLZ, it refers to the number of bits in the data type being considered (e.g., 32 or 64 for integers).
Filed Under: optimizationloop-analysisbugfix