GCC Newspaper
JULY 29, 2026
Date
/
Architectures
Components
Topics
News & Policy
match Performance Win

match: Simplify `(a CMP1 b) AND/IOR (a CMP2 b)` [PR126042]

Compiler now simplifies combinations of comparisons using bitwise AND/IOR, improving optimization for floating-point and other comparisons.

This commit enhances GCC’s optimization capabilities by introducing a new pattern in match.pd to simplify expressions of the form (a CMP1 b) AND/IOR (a CMP2 b). It reuses the combine_comparisons logic from fold-cost.cc, enabling optimizations for various comparisons, including floating-point ones, especially when -fno-trapping-math is used. This change leads to more efficient code generation for complex conditional logic.

In Details

A new pattern in match.pd is introduced to recognize and simplify (a CMP1 b) BIT_AND_EXPR (a CMP2 b) and (a CMP1 b) BIT_IOR_EXPR (a CMP2 b). The logic is based on combine_comparisons in fold-cost.cc, which is split into two versions to handle these bitwise operations. This allows for simplification of combined comparison results, including floating-point comparisons and <=> operator when -fno-trapping-math is enabled, addressing PR126042 and related issues.

For Context
match.pd
A file containing pattern definitions for the GCC 'match' system, used for recognizing and optimizing specific instruction sequences or expression patterns.
fold-cost.cc
A GCC source file containing functions related to expression folding and cost analysis, which helps the optimizer decide which transformations to apply.
combine_comparisons
A function or logic within GCC that merges or simplifies multiple comparison operations into a more efficient form.
-fno-trapping-math
A GCC compiler flag that disables traditional IEEE 754 trapping for floating-point exceptions, allowing for more aggressive optimizations by assuming exceptions do not occur.
Filed Under: optimizationmatchfloating-pointcomparisons