re PR tree-optimization/15255 ([tree-ssa] a * 2 + a * 2 is not converted to a * 4)
authorRichard Guenther <rguenther@suse.de>
Tue, 29 Apr 2008 15:59:43 +0000 (15:59 +0000)
committerRichard Biener <rguenth@gcc.gnu.org>
Tue, 29 Apr 2008 15:59:43 +0000 (15:59 +0000)
2008-04-29  Richard Guenther  <rguenther@suse.de>

PR middle-end/15255
* fold-const.c (fold_binary): Fold (A + A) * C to A * 2*C.

* gcc.dg/fold-plusmult.c: New testcase.

From-SVN: r134798

gcc/ChangeLog
gcc/fold-const.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/fold-plusmult.c [new file with mode: 0644]

index e261305f13f6b3cc5e42dce86417fa883e275972..b5ae15e141bc68eaff47cc16878bf6f9f2f4184c 100644 (file)
@@ -1,3 +1,8 @@
+2008-04-29  Richard Guenther  <rguenther@suse.de>
+
+       PR middle-end/15255
+       * fold-const.c (fold_binary): Fold (A + A) * C to A * 2*C.
+
 2008-04-29  Richard Guenther  <rguenther@suse.de>
 
        * tree-ssa-alias.c (finalize_ref_all_pointers): Remove.
index aae203777289cb5e39a4dc9799af674705a27714..b4645ca256e874bdcd3b920c3e5ac3d9afa98169 100644 (file)
@@ -10154,6 +10154,17 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1)
            return fold_build2 (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
+             && operand_equal_p (TREE_OPERAND (arg0, 0),
+                                 TREE_OPERAND (arg0, 1), 0))
+           return fold_build2 (MULT_EXPR, type,
+                               omit_one_operand (type, TREE_OPERAND (arg0, 0),
+                                                 TREE_OPERAND (arg0, 1)),
+                               fold_build2 (MULT_EXPR, type,
+                                            build_int_cst (type, 2) , arg1));
+
          strict_overflow_p = false;
          if (TREE_CODE (arg1) == INTEGER_CST
              && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
index f2b69e90afca2484067642d0d7f7291653c7d5bf..dd8b3aee01c2afd8e7acdbe25c2806efc6ac884d 100644 (file)
@@ -1,3 +1,8 @@
+2008-04-29  Richard Guenther  <rguenther@suse.de>
+
+       PR middle-end/15255
+       * gcc.dg/fold-plusmult.c: New testcase.
+
 2008-04-29  Richard Guenther  <rguenther@suse.de>
 
        PR middle-end/36077
diff --git a/gcc/testsuite/gcc.dg/fold-plusmult.c b/gcc/testsuite/gcc.dg/fold-plusmult.c
new file mode 100644 (file)
index 0000000..d584b95
--- /dev/null
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-options "-fdump-tree-original" } */
+
+int test1 (int a)
+{
+  return 2*a + 2*a;
+}
+
+int test2 (int a)
+{
+  return (a + a)*2;
+}
+
+/* { dg-final { scan-tree-dump-times "<a> \\\* 4" 2 "original" } } */
+/* { dg-final { cleanup-tree-dump "original" } } */