GCC Newspaper
OCTOBER 6, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
match

match: 1 / X -> X == 1 for positive X. [PR125735]

Correctly optimizes division by a variable in GCC's pattern matching to improve code generation.

This commit corrects a pattern matching rule in GCC’s tree-optimization passes. Previously, the rule for 1 / X when X was positive was not correctly identified as equivalent to X == 1. This change uses tree_expr_nonnegative_p to ensure signed positive values are handled, leading to more accurate optimization of division operations.

In Details

Fixes a bug in the match.pd pattern matching code where the transformation 1 / X -> X == 1 was incorrectly applied for positive signed types because TYPE_UNSIGNED(type) did not cover them. It now uses tree_expr_nonnegative_p() for a more robust check, ensuring the transformation is only applied when X is guaranteed to be positive. This is relevant for optimizers that rely on algebraic simplifications of arithmetic operations.

For Context
pattern matching
A technique used in compilers to identify and transform specific code structures. In GCC, pattern matching is used in optimization passes to find and replace common code sequences with more efficient ones.
tree-optimization
An optimization phase in GCC that operates on the Abstract Syntax Tree (AST) or its subsequent intermediate representations (like GENERIC or GIMPLE) to simplify and improve the code.
tree_expr_nonnegative_p
A GCC internal function that checks if a given expression is guaranteed to be non-negative. This is crucial for algebraic simplifications and range analysis in optimizations.
Filed Under: optimizationbugfix