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

Tree-optimization: Avoid UB negating ratio in IVOPTS cost compute

Fixes undefined behavior in loop invariant code motion by negating ratios in unsigned integer types.

This commit addresses undefined behavior in the loop invariant code motion (IVOPTS) optimization pass within GCC’s tree-ssa. Specifically, it resolves an issue where negating a ratio could lead to undefined behavior when performed on signed integers. The fix ensures that the negation is performed using unsigned types, maintaining correctness and preventing potential runtime errors or unexpected optimizations.

In Details

The tree-ssa-loop-ivopts.cc file's get_computation_cost function calculates the cost of operations within loops for optimization purposes. This commit corrects an overflow/underflow issue in the cost calculation when dealing with ratios by performing the negation in unsigned arithmetic, avoiding undefined behavior on signed integer negation. This change affects the cost heuristics used by the IVOPTS pass.

For Context
tree-optimization
A phase in GCC's compilation pipeline where the intermediate representation (the 'tree') is analyzed and transformed to improve code efficiency.
IVOPTS
Loop Invariant Code Motion Optimization. This pass identifies computations within a loop that produce the same result across all iterations and moves them outside the loop to reduce redundant work.
tree-ssa
Static Single Assignment form applied to GCC's intermediate representation tree. SSA helps in simplifying many optimizations by ensuring each variable is assigned a value only once.
Undefined Behavior (UB)
Behavior that results from an erroneous program condition for which the C and C++ standards impose no requirements. Compilers may take any action, including returning an incorrect result, or crashing.
Filed Under: optimizationcompilerGCCUB