[PATCH] Ignore shifts larger than precision in operator_rshift::op1_range.

Aldy Hernandez aldyh@redhat.com
Mon Oct 12 09:51:17 GMT 2020


Pushed as obvious.

gcc/ChangeLog:

	PR tree-optimization/97371
	* range-op.cc (operator_rshift::op1_range): Ignore shifts larger than
	or equal to type precision.

gcc/testsuite/ChangeLog:

	* gcc.dg/pr97371.c: New test.
---
 gcc/range-op.cc                | 7 +++++++
 gcc/testsuite/gcc.dg/pr97371.c | 8 ++++++++
 2 files changed, 15 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/pr97371.c

diff --git a/gcc/range-op.cc b/gcc/range-op.cc
index d1a11b34894..ce6ae2de20c 100644
--- a/gcc/range-op.cc
+++ b/gcc/range-op.cc
@@ -1626,6 +1626,13 @@ operator_rshift::op1_range (irange &r,
   tree shift;
   if (op2.singleton_p (&shift))
     {
+      // Ignore nonsensical shifts.
+      unsigned prec = TYPE_PRECISION (type);
+      if (wi::ge_p (wi::to_wide (shift),
+		    wi::uhwi (prec, TYPE_PRECISION (TREE_TYPE (shift))),
+		    UNSIGNED))
+	return false;
+
       // Folding the original operation may discard some impossible
       // ranges from the LHS.
       int_range_max lhs_refined;
diff --git a/gcc/testsuite/gcc.dg/pr97371.c b/gcc/testsuite/gcc.dg/pr97371.c
new file mode 100644
index 00000000000..ffefad0287e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr97371.c
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -w" } */
+
+int a, b;
+void c() {
+  if (b >> 38)
+    a = b;
+}
-- 
2.26.2



More information about the Gcc-patches mailing list