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

Simplify (T)(x) == (T)(y) to (T)(x ^ y) == 0 in match.pd

The compiler now simplifies equality comparisons between narrowed integral types by converting them into XOR operations against zero.

The compiler can now transform equality comparisons of the form (T)(x) == (T)(y) into (T)(x ^ y) == 0, where T is an integral type with precision less than that of x and y. This optimization applies to both equality and inequality comparisons. The change improves code generation for cases where boolean or short values are compared after implicit conversion.

In Details

The match.pd file contains pattern-matching rules used during GCC's tree-ssa optimization phase. This commit adds a rule that simplifies integral equality comparisons where the compared values have been narrowed (cast) to a lower precision. Specifically, it transforms (T)(x) == (T)(y) into (T)(x ^ y) == 0 and (T)(x) != (T)(y) into (T)(x ^ y) != 0. This can help simplify code generated from boolean operations.

For Context

The compiler's optimization phase aims to transform code into a more efficient form without changing its behavior. One part of this is pattern matching, where the compiler looks for specific code patterns and replaces them with simpler equivalents. This commit adds a new pattern-matching rule for comparisons between integers. When two integers are compared for equality/inequality after both have been converted to a smaller integer type, the comparison internally becomes an XOR operation against zero which can be faster to compute.

Filed Under: optimizationpattern-matchingtree-ssaintegral