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

forwprop: Reset MOD result range when X is negative

Fixes incorrect constant folding of the MOD operator when the dividend can be negative.

The forward propagation pass in GCC incorrectly preserved the range of a MOD result even when the dividend was negative. This led to incorrect folding of the result to zero in specific cases. This change resets the flow-sensitive information of the MOD result when its value is rewritten under these conditions, ensuring accurate constant propagation.

In Details

The Value Range Propagation (VRP) pass relies on flow-sensitive information to determine the possible range of values for variables. In forwprop.cc, the simplify_phi_result_movdiv function sometimes rewrites M = X % PHI into M = X & PHI. If X can be negative, this rewrite preserves the zero-ness of M but not its precise value range. This commit corrects VRP by resetting the flow-sensitive info of M when such a rewrite occurs, preventing incorrect folds of M to 0.

Filed Under: optimizationcompiler