https://gcc.gnu.org/g:5d14cb6417055829bfc1ff12e4ce935e844e9b38
commit r17-1573-g5d14cb6417055829bfc1ff12e4ce935e844e9b38 Author: Kael Andrew Franco <[email protected]> Date: Mon Jun 15 11:18:32 2026 -0600 [PATCH] match: For nonnegative X and Y, relax condition on X % Y < Y to true [PR125737] From ae75421fd6c7d50e5b1e9aafea2ae3cbcd4ebc1c Mon Sep 17 00:00:00 2001 From: Kael Andrew Alonzo Franco <[email protected]> Date: Sun, 14 Jun 2026 06:28:01 -0400 Subject: [PATCH] match: For nonnegative X and Y, relax condition on X % Y < Y to true [PR125737] tree_expr_nonnegative_p covers TYPE_UNSIGNED (type) or when X and Y are known to be nonnegative. Bootstrapped and tested on x86_64-pc-linux-gnu PR tree-optimization/125737 gcc/ChangeLog: PR tree-optimization/125737 * match.pd: Use tree_expr_nonnegative_p for X % Y < Y to true. gcc/testsuite/ChangeLog: PR tree-optimization/125737 * gcc.dg/pr125737.c: New test. Diff: --- gcc/match.pd | 5 ++--- gcc/testsuite/gcc.dg/pr125737.c | 12 ++++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/gcc/match.pd b/gcc/match.pd index f2503b35dca0..21b0945943b3 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1715,9 +1715,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* X % Y is smaller than Y. */ (for cmp (lt ge) (simplify - (cmp:c (trunc_mod @0 @1) @1) - (if (TYPE_UNSIGNED (TREE_TYPE (@0))) - { constant_boolean_node (cmp == LT_EXPR, type); }))) + (cmp:c (trunc_mod tree_expr_nonnegative_p@0 tree_expr_nonnegative_p@1) @1) + { constant_boolean_node (cmp == LT_EXPR, type); })) /* x | ~0 -> ~0 */ (simplify diff --git a/gcc/testsuite/gcc.dg/pr125737.c b/gcc/testsuite/gcc.dg/pr125737.c new file mode 100644 index 000000000000..93d0fcd215af --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr125737.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +int +f (unsigned short a, unsigned short b) { + int aa = a; + int bb = b; + int c = aa % bb; + return c < bb; +} + +/* { dg-final { scan-tree-dump-times "return 1;" 1 "optimized" } } */
