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

match: Fix signed integer overflow in pattern for `cmp + (-cmp ^ x)`

Corrects a GCC pattern that introduced unintended signed integer overflow with INT_MIN.

A recent addition to GCC’s pattern matching system inadvertently introduced signed integer overflow for specific inputs, particularly when dealing with INT_MIN. The pattern cmp + (-cmp ^ x) was incorrectly translated, causing overflow when x was INT_MIN and cmp was 0. This commit fixes the issue by using an unsigned type for the negation, preventing the overflow.

In Details

The match.pd file contains patterns for optimizations. A recent pattern for (A ^ -cmp) + cmp was intended to simplify expressions but introduced a signed integer overflow when x was INT_MIN and cmp was 0 (the expression 0 + (-0 ^ INT_MIN) would overflow). This commit corrects the pattern by casting to an unsigned type before negating, preventing the overflow and aligning with PR tree-optimization/126418.

For Context
Pattern matching
A technique used by compilers to recognize specific sequences of operations or data structures (patterns) in the intermediate representation, which can then be transformed into more efficient code.
Signed integer overflow
Occurs when the result of an arithmetic operation on signed integers is outside the range that can be represented by the data type. In C and C++, this results in undefined behavior.
INT_MIN
The smallest representable value for a signed integer type (e.g., -2147483648 for a 32-bit int).
Intermediate Representation (IR)
A data structure or code format used internally by a compiler to represent the program being compiled, between the source code and the target machine code.
Filed Under: optimizationbugfixinteger overflowpattern matching