Add relation to unsigned right shift.
authorAndrew MacLeod <amacleod@redhat.com>
Wed, 12 Jan 2022 18:28:55 +0000 (13:28 -0500)
committerAndrew MacLeod <amacleod@redhat.com>
Thu, 13 Jan 2022 18:51:30 +0000 (13:51 -0500)
If the first operand and the shift value of a right shift operation are both
>= 0, then we know the LHS of the operation is <= the first operand.

PR tree-optimization/96707
gcc/
* range-op.cc (operator_rshift::lhs_op1_relation): New.

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

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

index a4f6e9e..19bdf30 100644 (file)
@@ -1941,9 +1941,25 @@ public:
                          const irange &lhs,
                          const irange &op2,
                          relation_kind rel = VREL_NONE) const;
+  virtual enum tree_code lhs_op1_relation (const irange &lhs,
+                                          const irange &op1,
+                                          const irange &op2) const;
 } op_rshift;
 
 
+enum tree_code
+operator_rshift::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
+                                  const irange &op1,
+                                  const irange &op2) const
+{
+  // If both operands range are >= 0, then the LHS <= op1.
+  if (!op1.undefined_p () && !op2.undefined_p ()
+      && wi::ge_p (op1.lower_bound (), 0, TYPE_SIGN (op1.type ()))
+      && wi::ge_p (op2.lower_bound (), 0, TYPE_SIGN (op2.type ())))
+    return LE_EXPR;
+  return VREL_NONE;
+}
+
 bool
 operator_lshift::fold_range (irange &r, tree type,
                             const irange &op1,
diff --git a/gcc/testsuite/g++.dg/pr96707.C b/gcc/testsuite/g++.dg/pr96707.C
new file mode 100644 (file)
index 0000000..2653fe3
--- /dev/null
@@ -0,0 +1,10 @@
+/* { dg-do compile} */
+/* { dg-options "-O2 -fdump-tree-evrp" } */
+
+bool f(unsigned x, unsigned y)
+{
+    return (x >> y) <= x;
+}
+
+/* { dg-final { scan-tree-dump "return 1" "evrp" } }  */
+