diff mbox

move a * (1 << b) -> a << b pattern from fold-const.c to match.pd

Message ID CAAgBjM=Q5NMk39JTCPKiD8MPMxS5yqxwzmSa_SLbTvNVLQ-qXQ@mail.gmail.com
State New
Headers show

Commit Message

Prathamesh Kulkarni July 6, 2015, 8:28 p.m. UTC
Hi,
The attached patch moves pattern a * (1 << b) -> a << b.
Bootstrapped on x86_64-unknown-linux-gnu, testing in progress.
OK for trunk if testing passes ?

Thank you,
Prathamesh
2015-07-06  Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>

	* fold-const.c (fold_binary_loc): Remove pattern a * 1 << b -> a << b.
	* match.pd (a * 1 << b -> a << b): New pattern.

testsuite/
	* gcc.dg/tree-ssa/fold-shiftmult.c: New test-case.
diff mbox

Patch

Index: fold-const.c
===================================================================
--- fold-const.c	(revision 225473)
+++ fold-const.c	(working copy)
@@ -10175,16 +10175,6 @@ 
 						  negate_expr (arg0)),
 				tem);
 
-	  /* (a * (1 << b)) is (a << b)  */
-	  if (TREE_CODE (arg1) == LSHIFT_EXPR
-	      && integer_onep (TREE_OPERAND (arg1, 0)))
-	    return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
-				TREE_OPERAND (arg1, 1));
-	  if (TREE_CODE (arg0) == LSHIFT_EXPR
-	      && integer_onep (TREE_OPERAND (arg0, 0)))
-	    return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
-				TREE_OPERAND (arg0, 1));
-
 	  /* (A + A) * C -> A * 2 * C  */
 	  if (TREE_CODE (arg0) == PLUS_EXPR
 	      && TREE_CODE (arg1) == INTEGER_CST
Index: match.pd
===================================================================
--- match.pd	(revision 225473)
+++ match.pd	(working copy)
@@ -854,6 +854,12 @@ 
       && tree_expr_nonnegative_p (@1))
   @0))
 
+/* a * (1 << b) -> a << b */
+(simplify
+  (mult:c @a (lshift integer_onep @b))
+  (if (!FLOAT_TYPE_P (type))
+    (lshift @a @b)))
+
 (for shiftrotate (lrotate rrotate lshift rshift)
  (simplify
   (shiftrotate @0 integer_zerop)
Index: testsuite/gcc.dg/tree-ssa/fold-shiftmult.c
===================================================================
--- testsuite/gcc.dg/tree-ssa/fold-shiftmult.c	(revision 0)
+++ testsuite/gcc.dg/tree-ssa/fold-shiftmult.c	(working copy)
@@ -0,0 +1,19 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-forwprop-details" } */
+
+int f1(int a, int b)
+{
+  int t1 = 1 << b;
+  int f1_val = a * t1;
+  return f1_val;
+}
+/* { dg-final { scan-tree-dump "gimple_simplified to f1_val_\\d\+ = a_\\d\+\\(D\\) << b_\\d\+\\(D\\)" "forwprop1" } } */
+
+int f2(int a, int b)
+{
+  int t1 = 1 << b;
+  int f2_val = t1 * a;
+  return f2_val;
+}
+
+/* { dg-final { scan-tree-dump "gimple_simplified to f2_val_\\d\+ = a_\\d\+\\(D\\) << b_\\d\+\\(D\\)" "forwprop1" } } */