From 165ba2e9c7a6a86dbb12182486359edefba1dd9d Mon Sep 17 00:00:00 2001
From: Jakub Jelinek <jakub@redhat.com>
Date: Wed, 4 Jan 2017 10:07:33 +0100
Subject: [PATCH] re PR tree-optimization/71563 (Regression in GCC-7.0.0's
 optimizer.)

	PR tree-optimization/71563
	* match.pd: Simplify X << Y into X if Y is known to be 0 or
	out of range value - has low bits known to be zero.

	* gcc.dg/tree-ssa/pr71563.c: New test.

From-SVN: r244050
---
 gcc/ChangeLog                           |  6 ++++++
 gcc/match.pd                            | 15 +++++++++++++++
 gcc/testsuite/ChangeLog                 |  5 +++++
 gcc/testsuite/gcc.dg/tree-ssa/pr71563.c | 23 +++++++++++++++++++++++
 4 files changed, 49 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr71563.c

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 06021eed5def..864c30b9ccd2 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2017-01-04  Jakub Jelinek  <jakub@redhat.com>
+
+	PR tree-optimization/71563
+	* match.pd: Simplify X << Y into X if Y is known to be 0 or
+	out of range value - has low bits known to be zero.
+
 2017-01-04  Alan Modra  <amodra@gmail.com>
 
 	* Makefile.in (aclocal_deps): Update and order as per aclocal.m4.
diff --git a/gcc/match.pd b/gcc/match.pd
index 8791e57ea212..7b9680094730 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1515,6 +1515,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
     (if (tem)
      (shiftrotate @0 { tem; }))))))
 
+/* Simplify X << Y where Y's low width bits are 0 to X, as only valid
+   Y is 0.  Similarly for X >> Y.  */
+#if GIMPLE
+(for shift (lshift rshift)
+ (simplify
+  (shift @0 SSA_NAME@1)
+   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
+    (with {
+      int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
+      int prec = TYPE_PRECISION (TREE_TYPE (@1));
+     }
+     (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
+      @0)))))
+#endif
+
 /* Rewrite an LROTATE_EXPR by a constant into an
    RROTATE_EXPR by a new constant.  */
 (simplify
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 4a1caa7bb04f..9e7cae414195 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2017-01-04  Jakub Jelinek  <jakub@redhat.com>
+
+	PR tree-optimization/71563
+	* gcc.dg/tree-ssa/pr71563.c: New test.
+
 2017-01-04  Janne Blomqvist  <jb@gcc.gnu.org>
 
 	PR fortran/78534
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr71563.c b/gcc/testsuite/gcc.dg/tree-ssa/pr71563.c
new file mode 100644
index 000000000000..f01291d25dca
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr71563.c
@@ -0,0 +1,23 @@
+/* PR tree-optimization/71563 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+void link_error (void);
+
+void
+foo (int k)
+{
+  int t = 1 << ((1 / k) << 8);
+  if (t != 1)
+    link_error ();
+}
+
+void
+bar (int k, int l)
+{
+  int t = l << (k << 8);
+  if (t != l)
+    link_error ();
+}
+
+/* { dg-final { scan-tree-dump-not "link_error" "optimized" } } */
-- 
GitLab