This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Optimize X << Y with low bits of Y known to be 0 (PR tree-optimization/71563, take 2)
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Richard Biener <rguenther at suse dot de>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Thu, 29 Dec 2016 22:19:53 +0100
- Subject: [PATCH] Optimize X << Y with low bits of Y known to be 0 (PR tree-optimization/71563, take 2)
- Authentication-results: sourceware.org; auth=none
- References: <20161220190933.GO21933@tucnak> <alpine.DEB.2.20.1612202116420.2423@laptop-mg.saclay.inria.fr> <20161220204503.GP21933@tucnak>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
Hi!
On Tue, Dec 20, 2016 at 09:45:03PM +0100, Jakub Jelinek wrote:
> > Note that you can write (shift @0 SSA_NAME@1) in the pattern instead of a
> > separate test.
>
> That is what I tried first, but there is some bug in genmatch.c that
> prevents it. The:
> (for vec (VECTOR_CST CONSTRUCTOR)
> (simplify
> (shiftrotate @0 vec@1)
> results in case SSA_NAME: being added to a switch:
> case SSA_NAME:
> if (do_valueize (valueize, op1) != NULL_TREE)
> {
> gimple *def_stmt = SSA_NAME_DEF_STMT (op1);
> if (gassign *def = dyn_cast <gassign *> (def_stmt))
> switch (gimple_assign_rhs_code (def))
> {
> case CONSTRUCTOR:
> and the SSA_NAME@1 in another simplification resulted in another
> case SSA_NAME:
> into the same switch (rather than appending to the case SSA_NAME).
And here is the corresponding updated version of the patch:
2016-12-29 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.
* gcc.dg/tree-ssa/pr71563.c: New test.
--- gcc/match.pd.jj 2016-12-21 10:00:10.809244456 +0100
+++ gcc/match.pd 2016-12-29 21:56:56.891858831 +0100
@@ -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
--- gcc/testsuite/gcc.dg/tree-ssa/pr71563.c.jj 2016-12-29 21:56:12.668414342 +0100
+++ gcc/testsuite/gcc.dg/tree-ssa/pr71563.c 2016-12-29 21:56:12.668414342 +0100
@@ -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" } } */
Jakub