]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Update bitwise_or op_range.
authorAndrew MacLeod <amacleod@redhat.com>
Thu, 31 Oct 2024 18:07:00 +0000 (14:07 -0400)
committerAndrew MacLeod <amacleod@redhat.com>
Fri, 1 Nov 2024 19:24:10 +0000 (15:24 -0400)
If the LHS of a bitwise OR is positive, then so are both operands when
using op1_range or op2_range.

gcc/
* range-op.cc (operator_bitwise_or::op1_range): If LHS is signed
positive, so are both operands.

gcc/testsuite
* g++.dg/cpp23/attr-assume-opt.C (f2b): Alternate flow test.

gcc/range-op.cc
gcc/testsuite/g++.dg/cpp23/attr-assume-opt.C

index 58734cddd2fd53a9c3863dfa6c36985ba3d3112b..5a1eb59f3cd40da9262b722a32288ff559473fae 100644 (file)
@@ -3822,6 +3822,19 @@ operator_bitwise_or::op1_range (irange &r, tree type,
       r.set_zero (type);
       return true;
     }
+
+  //   if (A < 0 && B < 0)
+  // Sometimes gets translated to
+  //   _1 = A | B
+  //   if (_1 < 0))
+  // It is useful for ranger to recognize a positive LHS means the RHS
+  // operands are also positive when dealing with the ELSE range..
+  if (TYPE_SIGN (type) == SIGNED && wi::ge_p (lhs.lower_bound (), 0, SIGNED))
+    {
+      unsigned prec = TYPE_PRECISION (type);
+      r.set (type, wi::zero (prec), wi::max_value (prec, SIGNED));
+      return true;
+    }
   r.set_varying (type);
   return true;
 }
index 88d5e78dbbaedd72a1c43a34c9def163733b5e04..e61ba7a27e0fca04b74310183df9d571b1ae692d 100644 (file)
@@ -38,5 +38,40 @@ f3 (int x, int y, int z)
   return 1;
 }
 
+// This is the same as f2, except there is more complicated flow and 
+// required a range-op update to bitwise or.
+
+void barn(int x);
+// assume (x+12 == 14 && y >= 0 && y + 10 < 13 && z + 4 >= 4 && z - 2 < 18)
+// in different order and form with function calls to cause branches.
+bool assume_func (int x, int y, int z)
+{
+  if (z - 2 >= 18)
+    return false;
+  if (x+12 != 14)
+    return false;
+  barn (x);
+  if (y < 0)
+    return false;
+  if (z + 4 < 4)
+    return false;
+  barn (y);
+  if (y + 10 >= 13)
+    return false;
+  barn (z);
+  return true;
+}
+
+int
+f2b (int x, int y, int z)
+{
+  [[assume (assume_func (x, y, z))]];
+  unsigned q = x + y + z;
+  if (q*2 > 46)
+    return 0;
+  return 1;
+}
+
+
 /* { dg-final { scan-tree-dump-times "return 0" 0 "vrp2" } } */
-/* { dg-final { scan-tree-dump-times "return 1" 3 "vrp2" } } */
+/* { dg-final { scan-tree-dump-times "return 1" 4 "vrp2" } } */