[gcc r17-3434] ranger: Fix operator_div with VREL_LT [PR126934]

Andrea Pinski via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:ea41a5efe4b4a715f66ebfcad35cfd3b22e94690

commit r17-3434-gea41a5efe4b4a715f66ebfcad35cfd3b22e94690
Author: Andrea Pinski <[email protected]>
Date:   Tue Aug 18 21:33:46 2026 -0700

    ranger: Fix operator_div with VREL_LT [PR126934]
    
    The problem here (which I missed during the review) is
    that when we don't set the rel_range to 0, the rel_range
    will be undefined (rather than varying) so when we do
    an intersection with that the lhs becomes undefined.
    That is wrong.  This fixes the problem by instead
    doing an early return if either of the ops are not
    nonnegative.
    
    Pushed as obvious after bootstrap/test on x86_64-linux-gnu.
    
            PR tree-optimization/126934
    
    gcc/ChangeLog:
    
            * range-op.cc (operator_div::op1_op2_relation_effect): Fix
            for ops range being negative with VREL_LT.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/torture/pr126934-1.c: New test.
    
    Signed-off-by: Andrea Pinski <[email protected]>

Diff:
---
 gcc/range-op.cc                           |  8 ++++----
 gcc/testsuite/gcc.dg/torture/pr126934-1.c | 23 +++++++++++++++++++++++
 2 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/gcc/range-op.cc b/gcc/range-op.cc
index bb0556238728..1918ef5f9b14 100644
--- a/gcc/range-op.cc
+++ b/gcc/range-op.cc
@@ -2647,10 +2647,10 @@ operator_div::op1_op2_relation_effect (irange &lhs_range,
     /* op1/op2 = 0 if op1 < op2 and both op1 and op2
        are known positives.  */
     case VREL_LT:
-      if (TYPE_UNSIGNED (type)
-	  || (wi::ge_p (op1_range.lower_bound (), 0, SIGNED)
-	      && wi::ge_p (op2_range.lower_bound (), 0, SIGNED)))
-	rel_range.set_zero (type);
+      if (!op1_range.nonnegative_p ()
+	   || !op2_range.nonnegative_p ())
+	return false;
+      rel_range.set_zero (type);
       break;
     default:
       return false;
diff --git a/gcc/testsuite/gcc.dg/torture/pr126934-1.c b/gcc/testsuite/gcc.dg/torture/pr126934-1.c
new file mode 100644
index 000000000000..f4404dbecd1d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr126934-1.c
@@ -0,0 +1,23 @@
+/* { dg-do run } */
+/* PR tree-optimization/126934 */
+
+__attribute__((noinline))
+int ff(int a, signed char c)
+{
+  int t = c;
+  int t1 = c+60;
+  if (a)
+    return t/t1;
+  return 1000;
+}
+
+int main ()
+{
+  if (ff(1,-127) != 1)
+    __builtin_abort ();
+  if (ff(1,1) != 0)
+    __builtin_abort ();
+  if (ff(1,4) != 0)
+    __builtin_abort ();
+}
+
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.