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

Avoid undefined behavior in CSE of -x / y.

GCC now correctly handles CSE of negated division/multiplication operands to avoid UB.

The common subexpression elimination (CSE) pass in GCC incorrectly handled cases like -x / y when trying to match it with x / y. This commit ensures that the pass checks for the literal availability of the negated operand and avoids issues with undefined behavior (UB) that can arise from negation, particularly with INT_MIN.

In Details

In tree-ssa-sccvn.cc, the visit_nary_op function performs common subexpression elimination. This patch modifies the handling of division and multiplication operations to correctly identify and match negated operands. It specifically checks if a negated operand is available before attempting to build or simplify it, especially when negation could lead to UB. This is crucial for correctly folding expressions involving negation and avoiding incorrect transformations when operands like INT_MIN are involved.

For Context
CSE
Common Subexpression Elimination, an optimization technique that identifies and replaces identical expressions with a single temporary variable.
undefined behavior (UB)
Behavior of a program that results from an operation or condition that violates a language or C standard, for which the standard imposes no requirements. Compilers assume UB does not happen, which can lead to unexpected optimizations.
INT_MIN
The smallest representable value for a signed integer type.
IL
Intermediate Language, a representation of a program between the source code and the machine code.
Filed Under: optimizationundefined-behaviorgcc