WEDNESDAY, JULY 29, 2026
optimize Performance Win
match: Fold remainder comparison with its dividend.
Optimization to simplify X % Y == X comparisons by checking X < Y.
The match optimization pass now simplifies expressions of the form X % Y == X. For non-negative operands, this condition is true if and only if the dividend X is less than the divisor Y. This avoids an unnecessary division operation. The optimization is similar to the existing rule for (X / Y) == 0 and preserves exceptions for potential zero divisors.
In Details
The match.pd optimization pass now recognizes the pattern (X % Y) == X. For unsigned integers, this condition simplifies to X < Y. This transformation is analogous to the existing (X / Y) == 0 simplification. Care is taken to preserve potential zero-divisor exceptions, especially when non-call exceptions are enabled.
For Context
- match.pd
- The pattern-matching optimization pass in GCC, responsible for recognizing and transforming specific code patterns into more efficient forms.
- remainder operation
- An arithmetic operation that yields the remainder after division of one number by another. In C/C++, this is the '%' operator.
- zero-divisor exception
- An exception raised when an attempt is made to divide by zero. Compilers may need to preserve this behavior or handle it specifically.