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

Loop iteration analysis corrected for bit count loops

GCC's loop analysis for bit counting operations now correctly identifies the loop's recurrence variable, preventing miscompilation in certain cases.

This commit fixes an issue in GCC’s loop iteration analysis for bit counting operations (popcount, cltz, cltz_complement). Previously, the analysis incorrectly assumed that the loop’s exit condition always tested the same induction variable (IV) that was being modified within the loop. This could lead to incorrect iteration counts and miscompilation, as demonstrated with an example where the loop body modified one IV while the exit condition checked another. The fix ensures that the analysis validates that the exit test operates on the loop’s recurrence PHI, improving correctness.

In Details

The number_of_iterations_popcount, number_of_iterations_cltz, and number_of_iterations_cltz_complement analyses have been updated to verify that the loop's exit test condition operates on the same header PHI that represents the loop's recurrence variable. Currently, these analyses accept an exit test on a header PHI whose latch value is the modified IV, but they take the start value from a different PHI of the IV. This commit adds a check to ensure these two PHIs are identical. Without this check, miscompilation can occur, as seen in GCC 11 with the popcount form, and demonstrated by a…

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.
popcount
Population Count. An operation that counts the number of set bits (1s) in an integer.
cltz
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.
PHI node
In SSA (Static Single Assignment) form, a PHI node is used to merge values from different control flow paths. In loops, it's often used to represent the value of an induction variable at the start of each iteration.
header PHI
A PHI node at the beginning of a loop (the loop header) that represents the value of a loop-variant variable at the start of each iteration.
latch value
The value of a variable on the 'back edge' of a loop, which is the value after it has been updated within the loop body and before the next iteration begins.
SSA form
Static Single Assignment form. An intermediate representation where every variable is assigned a value exactly once. PHI nodes are used to handle control flow.
Filed Under: optimizationloop-analysisbugfix