GCC Newspaper
JUNE 15, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
match Performance Win

GCC now handles `X != INT_MIN ? -X : INT_MIN` without bailing out.

GCC optimizes a conditional negation involving `INT_MIN` by using an unsigned negation to avoid undefined behavior.

GCC can now optimize expressions of the form X != INT_MIN ? -X : INT_MIN even when the signed negation of INT_MIN is undefined behavior. The compiler now emits an unsigned negate (equivalent to (signed)(-(unsigned)X)) in these cases, mirroring the existing handling of the abs pattern for the same edge case. This ensures the expression is well-defined and allows for further optimization.

In Details

The match.pd file in GCC defines patterns for tree-based optimizations. This commit addresses a specific case where the pattern X != C1 ? -X : C2 bails out when C1 is INT_MIN due to the undefined behavior of negating INT_MIN. The fix involves emitting an unsigned negation ((signed)(-(unsigned)X)) instead, which is well-defined. The change primarily affects the wi::only_sign_bit_p case within match.pd.

For Context

Compilers often perform optimizations by recognising common code patterns and replacing them with equivalent, but more efficient, code. This commit improves GCC's ability to optimize a conditional negation pattern involving INT_MIN. By using an unsigned negation, the compiler avoids a potential issue with undefined behavior, while still achieving the desired result. This can lead to faster and more reliable code execution.

Filed Under: gccoptimization