Return the correct relation
authorAndrew MacLeod <amacleod@redhat.com>
Mon, 22 Aug 2022 19:40:48 +0000 (15:40 -0400)
committerAndrew MacLeod <amacleod@redhat.com>
Tue, 23 Aug 2022 01:04:44 +0000 (21:04 -0400)
With an input condition of op1 > op2, and evaluating the unsigned expression:
LHS = op1 - op2
range-ops was returning LHS < op1 , which is incorrect as op2 coould be
zero.  This patch adjusts it to return LHS <= op1.

PR tree-optimization/106687
gcc/
* range-op.cc (operator_minus::lhs_op1_relation): Return VREL_LE
for the VREL_GT case as well.

gcc/testsuite/
* g++.dg/pr106687.C: New.

gcc/range-op.cc
gcc/testsuite/g++.dg/pr106687.C [new file with mode: 0644]

index dfdd971..806edf1 100644 (file)
@@ -1378,7 +1378,6 @@ operator_minus::lhs_op1_relation (const irange &, const irange &op1,
     switch (rel)
       {
       case VREL_GT:
-       return VREL_LT;
       case VREL_GE:
        return VREL_LE;
       default:
diff --git a/gcc/testsuite/g++.dg/pr106687.C b/gcc/testsuite/g++.dg/pr106687.C
new file mode 100644 (file)
index 0000000..75ac81c
--- /dev/null
@@ -0,0 +1,22 @@
+// { dg-do run }
+// { dg-options "-O2"  }
+
+bool var_0 = (bool)0;
+unsigned int var_7 = 42;
+char var_215;
+
+int main() {
+    unsigned b = var_0;
+    unsigned p2 = var_7;
+    unsigned *tp;
+    if (b < p2)
+      tp = &p2;
+    else
+      tp = &b;
+    unsigned tt = *tp;
+    unsigned t = tt ^ (var_7 - var_0);
+    var_215 = t ? t : 42;
+    if (var_215 != 42)
+      __builtin_abort();
+    return 0;
+}