GCC Newspaper
OCTOBER 6, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
gcc/match-sat-alu.pd Performance Win

match-sat-alu.pd: Recognize more unsigned saturating subtraction forms

Improves recognition of unsigned saturating subtractions with swapped operands or reversed conditional arms.

This commit enhances the recognition of unsigned saturating subtraction operations in GCC. It now correctly identifies forms where the comparison operands or conditional arms are reversed, such as ‘B < A ? A - B : 0’ or ‘A < B ? 0 : A - B’. This allows the compiler to apply more efficient instructions, like AArch64’s UQSUB, replacing sequences of comparisons, subtractions, and conditional selects, leading to better code generation.

In Details

The match-sat-alu.pd file contains patterns for recognizing and optimizing arithmetic operations, particularly saturating ones. This patch adds recognition for unsigned saturating subtraction (unsigned_integer_sat_sub) when the comparison operands are commuted (e.g., a < b vs b < a) or when the conditional branches are reversed. This is achieved by modifying the pattern definitions to accept these variations, enabling the backend to generate more efficient code by utilizing instructions like AArch64's subs and csel or UQSUB.

For Context
match-sat-alu.pd
A GCC pattern file used to define patterns for recognizing and optimizing saturating arithmetic operations on ALUs (Arithmetic Logic Units).
unsigned saturating subtraction
An arithmetic operation that subtracts two unsigned integers. If the result is less than zero, it clamps to zero; otherwise, it performs the subtraction. This prevents underflow.
commuted operands
When the order of operands in an operation or comparison is swapped (e.g., a - b becomes b - a, or a < b becomes b < a).
conditional select (CSEL)
An instruction that selects between two source operands based on a condition code and writes the result to a destination register.
UQSUB
An AArch64 instruction for unsigned saturating subtraction.
Filed Under: gccoptimizationaarch64pattern matching