https://gcc.gnu.org/g:1f9326da83eddebddb22b7b55e9171c916d7b536

commit r17-6232-g1f9326da83eddebddb22b7b55e9171c916d7b536
Author: Andrea Pinski <[email protected]>
Date:   Sun Sep 27 15:34:37 2026 -0700

    Add relationships for (a - b) < 0 if there a and b are signed
    
    For `(a - b)`, if a and b are signed types and there was no overflow,
    you can infer a relationship if you know the result of the minus is
    non-negative (`a >= b`) or is non-positive (`a <= b`). You can also
    remove the `==` part if you know the range does not contain 0.
    
    This allows us to optimize stuff like this:
    ```
    int f(int a, int b)
    {
      int t = a - b;
      if (t < 0)
      {
          return a < b;
      }
      return 0;
    }
    ```
    without needing to optimize `(a - b) < 0` into `a < b`.
    
    The main thing that needed to be changed to support is passing
    the ranges to op1_op2_relation inside register_outgoing_edges.
    
    Bootstrapped and tested on x86_64-linux-gnu.
    
    Changes since v1:
    * v2: the operands don't need to be non-negative.
    
    gcc/ChangeLog:
    
            * gimple-range-fold.cc (fur_source::register_outgoing_edges): Use
            the range of the rhs1/rsh2 on that edge rather than varry.
            * range-op.cc (operator_minus::op1_op2_relation): Add
            case for signed types.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/tree-ssa/vrp-cmp-sub-4.c: New test.
            * gcc.dg/tree-ssa/vrp-cmp-sub-5.c: New test.
    
    Signed-off-by: Andrea Pinski <[email protected]>

Diff:
---
 gcc/gimple-range-fold.cc                      | 12 +++++++++
 gcc/range-op.cc                               | 21 +++++++++++++---
 gcc/testsuite/gcc.dg/tree-ssa/vrp-cmp-sub-4.c | 36 +++++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/vrp-cmp-sub-5.c | 30 ++++++++++++++++++++++
 4 files changed, 96 insertions(+), 3 deletions(-)

diff --git a/gcc/gimple-range-fold.cc b/gcc/gimple-range-fold.cc
index 8a08c10cf905..2f552628c56f 100644
--- a/gcc/gimple-range-fold.cc
+++ b/gcc/gimple-range-fold.cc
@@ -2000,6 +2000,12 @@ fur_source::register_outgoing_edges (gcond *s, irange 
&lhs_range,
              && !r.undefined_p ()
              && !r.varying_p ())
            {
+             if (!gori ()->edge_range_p (r1, e0, ssa1, *m_query)
+                 || r1.undefined_p ())
+               r1.set_varying (TREE_TYPE (ssa1));
+             if (!gori ()->edge_range_p (r2, e0, ssa2, *m_query)
+                 || r2.undefined_p ())
+               r2.set_varying (TREE_TYPE (ssa2));
              relation_kind relation = handler.op1_op2_relation (r, r1, r2);
              if (relation != VREL_VARYING)
                register_relation (e0, relation, ssa1, ssa2);
@@ -2008,6 +2014,12 @@ fur_source::register_outgoing_edges (gcond *s, irange 
&lhs_range,
              && !r.undefined_p ()
              && !r.varying_p ())
            {
+             if (!gori ()->edge_range_p (r1, e1, ssa1, *m_query)
+                 || r1.undefined_p ())
+               r1.set_varying (TREE_TYPE (ssa1));
+             if (!gori ()->edge_range_p (r2, e1, ssa2, *m_query)
+                 || r2.undefined_p ())
+               r2.set_varying (TREE_TYPE (ssa2));
              relation_kind relation = handler.op1_op2_relation (r, r1, r2);
              if (relation != VREL_VARYING)
                register_relation (e1, relation, ssa1, ssa2);
diff --git a/gcc/range-op.cc b/gcc/range-op.cc
index c80d611f3919..765b5146eba0 100644
--- a/gcc/range-op.cc
+++ b/gcc/range-op.cc
@@ -2195,10 +2195,12 @@ minus_op1_op2_relation_effect (irange &lhs_range, tree 
type,
 
 // Check if the LHS range indicates a relation between OP1 and OP2.
 // OP1 - OP2 == 0 implies OP1 == OP2 even with wrapping arithmetic.
+// For signed types, when both OP1 and OP2 are both positive, infer
+// a relationship if there were it is overflow free.
 
 relation_kind
-operator_minus::op1_op2_relation (const irange &lhs, const irange &,
-                                 const irange &) const
+operator_minus::op1_op2_relation (const irange &lhs, const irange &rhs1,
+                                 const irange &rhs2) const
 {
   if (lhs.undefined_p ())
     return VREL_UNDEFINED;
@@ -2206,7 +2208,20 @@ operator_minus::op1_op2_relation (const irange &lhs, 
const irange &,
   if (lhs.zero_p ())
     return VREL_EQ;
 
-  if (!lhs.contains_zero_p ())
+  bool contains_z = lhs.contains_zero_p ();
+  // When a - b does not overflow and signed,
+  // we can figure out the relationship between
+  // a and b if the result is non-negative or non-positive.
+  if (!TYPE_UNSIGNED (lhs.type ())
+      && overflow_free_p (rhs1, rhs2))
+    {
+      if (lhs.nonnegative_p ())
+       return contains_z ? VREL_GE : VREL_GT;
+      if (lhs.nonpositive_p ())
+       return contains_z ? VREL_LE : VREL_LT;
+    }
+
+  if (!contains_z)
     return VREL_NE;
 
   return VREL_VARYING;
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp-cmp-sub-4.c 
b/gcc/testsuite/gcc.dg/tree-ssa/vrp-cmp-sub-4.c
new file mode 100644
index 000000000000..775a7f79de3d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp-cmp-sub-4.c
@@ -0,0 +1,36 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int f(int a, int b)
+{
+  a&=0xff;
+  b&=0xff;
+  int t = a - b;
+  if (t < 0)
+    return a < b;
+  return 0;
+}
+
+int g();
+
+int f1(int a, int b)
+{
+  a&=0xff;
+  b&=0xff;
+  int t = a - b;
+  if (t < 0)
+   {
+     if (a < b)
+       return g();
+   }
+  return 0;
+}
+
+
+/* { dg-final { scan-tree-dump-times " < " 2 "optimized" } } */
+/* Only 1 if left for f1 */
+/* { dg-final { scan-tree-dump-times "if " 1 "optimized" } } */
+/* 4 &, 2 `& 255` for each functions. */
+/* { dg-final { scan-tree-dump-times " & " 4 "optimized" } } */
+/* { dg-final { scan-tree-dump-not " != " "optimized" } } */
+/* { dg-final { scan-tree-dump-not " == " "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp-cmp-sub-5.c 
b/gcc/testsuite/gcc.dg/tree-ssa/vrp-cmp-sub-5.c
new file mode 100644
index 000000000000..c7d04ae4dd65
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp-cmp-sub-5.c
@@ -0,0 +1,30 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int f(int a, int b)
+{
+  int t = a - b;
+  if (t < 0)
+    return a < b;
+  return 0;
+}
+
+int g();
+
+int f1(int a, int b)
+{
+  int t = a - b;
+  if (t < 0)
+   {
+     if (a < b)
+       return g();
+   }
+  return 0;
+}
+
+
+/* { dg-final { scan-tree-dump-times " < " 2 "optimized" } } */
+/* Only 1 if left for f1 */
+/* { dg-final { scan-tree-dump-times "if " 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-not " != " "optimized" } } */
+/* { dg-final { scan-tree-dump-not " == " "optimized" } } */

Reply via email to