GCC Newspaper
OCTOBER 6, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
expmed Performance Win

Reset division constant cache between functions

Fixes a bug where division/modulo optimization state incorrectly carried over between functions.

The expand_divmod function maintained a cache of the constant divisor from the last division it processed, intending to optimize subsequent modulo operations with the same constant. However, this cache was a function-scope static variable that was never reset, causing its state to persist across function boundaries. This commit resets the cache at the start of each function expansion, ensuring that a function’s optimization is determined solely by its own code and not influenced by previously compiled functions in the same translation unit. This change makes the heuristic deterministic and can lead to code size improvements.

In Details

The expand_divmod function in GCC's experimental medium-range optimizer (expmed) employs a heuristic: it caches the constant divisor of the last division expanded. This allows a subsequent modulo operation with the same constant to reuse the quotient, potentially reducing to fewer instructions. Previously, this cache (last_div_const) was a static variable within the function scope and was not reset between calls to expand_divmod for different functions. This commit moves the reset of last_div_const to prepare_function_start, mirroring the reset of bitint_extended. This ensures the…

For Context
division/modulo optimization
Compiler optimizations that target division and modulo operations, often by recognizing specific patterns or constants to generate more efficient code.
translation unit
A single source code file after all preprocessing directives (like #include) have been processed.
heuristic
A practical approach or rule of thumb used in computer science to solve a problem that may not be optimal or perfect, but is sufficient for the immediate goals.
libcall
A call to a function in a shared or static library, often used when the compiler cannot generate inline code for an operation.
Filed Under: optimizationcompiler internalscode generation