[gcc r17-3283] match.pd: Fold umin(a, 1) | umin(b, 1) into umin(a | b, 1) [PR126646]

Venkataramanan Kumar via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:b190388bad47b1d3507abd52dc2a07f39a899984

commit r17-3283-gb190388bad47b1d3507abd52dc2a07f39a899984
Author: Reshma Roy <[email protected]>
Date:   Tue Aug 11 11:48:05 2026 +0530

    match.pd: Fold umin(a, 1) | umin(b, 1) into umin(a | b, 1) [PR126646]
    
    gcc/ChangeLog:
    
            PR tree-optimization/126646
            * match.pd: Fold umin(a, 1) | umin(b, 1) into umin(a | b, 1).
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/tree-ssa/pr126646-1.c: New test.
            * gcc.dg/tree-ssa/pr126646-2.c: New test.

Diff:
---
 gcc/match.pd                               |  5 +++++
 gcc/testsuite/gcc.dg/tree-ssa/pr126646-1.c | 15 +++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr126646-2.c | 13 +++++++++++++
 3 files changed, 33 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index 7fa15845e7a5..26cb2c9e155b 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -4901,6 +4901,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (bit_not (minmax:cs (bit_not @0) @1))
   (maxmin @0 (bit_not @1))))
 
+/* umin (a, 1) | umin (b, 1) -> umin (a | b, 1).  */
+ (simplify
+  (bit_ior (min @0 integer_onep@2) (min @1 @2))
+    (if (tree_expr_nonnegative_p (@0) && tree_expr_nonnegative_p (@1))
+     (min (bit_ior @0 @1) @2)))
 /* MIN (X, Y) == X -> X <= Y  */
 /* MIN (X, Y) < X -> X > Y  */
 /* MIN (X, Y) >= X -> X <= Y  */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr126646-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr126646-1.c
new file mode 100644
index 000000000000..447a1739b5f9
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr126646-1.c
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+/* The test case should produce only one min expr.  */
+/* umin(a,1) | umin(b,1) -> umin(a|b, 1).  */
+
+unsigned min_or (unsigned a, unsigned b)
+{
+  unsigned t = 1;
+  a = a < t ? a : t;
+  b = b < t ? b : t;
+  return a | b;
+}
+
+/* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr126646-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr126646-2.c
new file mode 100644
index 000000000000..3b47eee96565
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr126646-2.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int f (int a, int b)
+{
+  int x = __builtin_abs (a);
+  int y = __builtin_abs (b);
+  int p = x < 1 ? x : 1;
+  int q = y < 1 ? y : 1;
+  return p | q;
+}
+
+/* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "optimized" } } */
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.