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

tree-optimization: Fix loop iteration count with STRING_CST

Corrects loop iteration analysis by simulating string constant comparisons in uint8_t.

This commit fixes a sign-extension issue in GCC’s loop iteration analysis, specifically within the loop_niter_by_eval function. The problem occurred when simulating iterations over a STRING_CST. The fix ensures that the comparison of the STRING_CST element with the terminating constant is performed using uint8_t type, preventing incorrect sign extensions and ensuring accurate loop iteration counts, resolving PR127966.

In Details

The loop_niter_by_eval function estimates loop iteration counts. This change addresses a bug where simulating iteration over a STRING_CST (e.g., in a loop that terminates based on string length or content) caused a sign-extension issue. The fix modifies the function to perform the comparison of the STRING_CST element against the loop's terminating constant in uint8_t type, resolving the sign issue and ensuring correct iteration count estimation for such loops.

For Context
tree-optimization
A phase in GCC's compilation pipeline that optimizes the intermediate representation (tree) of the code.
loop_niter_by_eval
A function within GCC's loop optimization passes that attempts to evaluate and determine the number of iterations a loop will execute.
STRING_CST
GCC's internal representation for a string constant.
sign-extension
The process of preserving the sign of a number when converting it to a larger data type by padding with copies of the most significant bit.
uint8_t
An unsigned integer type that occupies exactly 8 bits.
Filed Under: optimizationloop optimizationSTRING_CST