>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/125737.c: New test.
Signed-off-by: Kael Franco <[email protected]>
---
gcc/match.pd | 5 ++---
gcc/testsuite/gcc.dg/pr125737.c | 12 ++++++++++++
2 files changed, 14 insertions(+), 3 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/pr125737.c
diff --git a/gcc/match.pd b/gcc/match.pd
index 92607189619..d93d55bb94f 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 00000000000..93d0fcd215a
--- /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" } } */
--
2.54.0