When experimenting with more complex flow for the assume pass, I found a case where we were not as precise as we should be, but it was because range-ops hadn't been taught that if the LHS of a bitwise OR was positive, that the 2 RHS operands must also be positive.

The testcase is simple a tweaked version of an existing testcase which turned

if (a_1 < 0 && b_2 < 0) goto bb3; else goto bb4;

into
_4 = a_1 | b_2
if (_4 < 0) goto bb3; else goto bb4;

on the branch to bb4, the condition is _4 >= 0 and we were not getting positive values for a_1 and b_2 on that edge resulting in ranges that included [-INF, -1] that shouldn't have been there.

This patch fixes that by implementing this functionality in op1_range in operator_bitwise_or, and adds the testcase.

Bootstrapped on x86_64-pc-linux-gnu with no regressions.  Pushed.

Andrew

From 9283b1b3593f708a4fc11fb6dfcf208ab218a5d9 Mon Sep 17 00:00:00 2001
From: Andrew MacLeod <amacl...@redhat.com>
Date: Thu, 31 Oct 2024 14:07:00 -0400
Subject: [PATCH 3/3] Update bitwise_or op_range.

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                              | 13 +++++++
 gcc/testsuite/g++.dg/cpp23/attr-assume-opt.C | 37 +++++++++++++++++++-
 2 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/gcc/range-op.cc b/gcc/range-op.cc
index 58734cddd2f..5a1eb59f3cd 100644
--- a/gcc/range-op.cc
+++ b/gcc/range-op.cc
@@ -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;
 }
diff --git a/gcc/testsuite/g++.dg/cpp23/attr-assume-opt.C b/gcc/testsuite/g++.dg/cpp23/attr-assume-opt.C
index 88d5e78dbba..e61ba7a27e0 100644
--- a/gcc/testsuite/g++.dg/cpp23/attr-assume-opt.C
+++ b/gcc/testsuite/g++.dg/cpp23/attr-assume-opt.C
@@ -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" } } */
-- 
2.45.0

Reply via email to