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

match: Use tree_expr_nonnegative_p for (X / Y) (==, !=) 0 -> X (<,>=) Y [PR125738]

Improves optimization by using tree_expr_nonnegative_p for division-related condition simplification.

This commit enhances GCC’s optimization capabilities by using tree_expr_nonnegative_p in match.pd to correctly simplify expressions involving division. Specifically, it enables the transformation of (X / Y) (==, !=) 0 to X (<,>=) Y when X and Y are known to be non-negative. This change relaxes the conditions for this optimization, addressing issues with unsigned types and ensuring more aggressive and correct simplification of conditional expressions.

In Details

This patch updates gcc/match.pd to use tree_expr_nonnegative_p for a division simplification rule: (X / Y) (==, !=) 0 transforms to X (<,>=) Y. Previously, the TYPE_UNSIGNED check was insufficient for non-negative operands. By using tree_expr_nonnegative_p, the condition is relaxed, allowing the optimization to apply correctly even when X and Y are large unsigned types whose division result can be zero or non-zero. This resolves PR125738 and affects PR64130 by enabling this optimization earlier in the pipeline, requiring removal of some scan-tree-dump calls.

For Context
tree_expr_nonnegative_p
A GCC internal function that checks if an expression is guaranteed to evaluate to a non-negative value. This is crucial for mathematical optimizations where the sign of operands affects the validity of transformations.
match.pd
A file containing pattern matching definitions used by GCC's internal representation (RTL) to optimize code. These patterns describe common instruction sequences and allow the compiler to replace them with more efficient ones.
GIMPLE
GCC's internal representation (IR) used between the front-end and the RTL optimizer. It's a three-address code form designed for easier optimization analysis.
optimization pass
A stage in the compiler's optimization pipeline where specific transformations are applied to the intermediate representation of the code to improve performance, reduce code size, or enhance other metrics.
PR
Problem Report. Refers to a bug or issue filed in the GCC bug tracking system.
Filed Under: optimizationtree-ssamatch.pdunsigned types