public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] tree-optimization/96707 - Add relation to unsigned right shift.
@ 2022-01-13 13:57 Andrew MacLeod
  2022-01-13 15:11 ` Richard Biener
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew MacLeod @ 2022-01-13 13:57 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 261 bytes --]

A quick addition to range ops for

LHS = OP1 >> OP2

if OP1 and OP2 are both >= 0,   then we can register the relation  LHS 
<= OP1   and all the expected good things happen.

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

OK for trunk?

Andrew

[-- Attachment #2: 0001-Add-relation-to-unsigned-right-shift.patch --]
[-- Type: text/x-patch, Size: 2021 bytes --]

From c34dab537d6f54b66b430f5980cde278fa033904 Mon Sep 17 00:00:00 2001
From: Andrew MacLeod <amacleod@redhat.com>
Date: Wed, 12 Jan 2022 13:28:55 -0500
Subject: [PATCH 1/2] Add relation to unsigned right shift.

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.c (operator_rshift::lhs_op1_relation): New.
	gcc/testtsuite/
	* g++.dg/pr96707.C: New.
---
 gcc/range-op.cc                | 16 ++++++++++++++++
 gcc/testsuite/g++.dg/pr96707.C | 10 ++++++++++
 2 files changed, 26 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/pr96707.C

diff --git a/gcc/range-op.cc b/gcc/range-op.cc
index a4f6e9eba29..19bdf30911a 100644
--- a/gcc/range-op.cc
+++ b/gcc/range-op.cc
@@ -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
index 00000000000..2653fe3d043
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pr96707.C
@@ -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" } }  */
+
-- 
2.17.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] tree-optimization/96707 - Add relation to unsigned right shift.
  2022-01-13 13:57 [PATCH] tree-optimization/96707 - Add relation to unsigned right shift Andrew MacLeod
@ 2022-01-13 15:11 ` Richard Biener
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2022-01-13 15:11 UTC (permalink / raw)
  To: Andrew MacLeod; +Cc: gcc-patches

On Thu, Jan 13, 2022 at 2:58 PM Andrew MacLeod via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> A quick addition to range ops for
>
> LHS = OP1 >> OP2
>
> if OP1 and OP2 are both >= 0,   then we can register the relation  LHS
> <= OP1   and all the expected good things happen.
>
> Bootstrapped on x86_64-pc-linux-gnu with no regressions.
>
> OK for trunk?

OK.

>
> Andrew

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-01-13 15:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-13 13:57 [PATCH] tree-optimization/96707 - Add relation to unsigned right shift Andrew MacLeod
2022-01-13 15:11 ` Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).