fold-const.c (fold_plusminus_mult_expr): Do not fold i * 4 + 2 to (i * 2 + 1) * 2.
authorRichard Guenther <rguenther@suse.de>
Wed, 1 Apr 2009 13:59:53 +0000 (13:59 +0000)
committerRichard Biener <rguenth@gcc.gnu.org>
Wed, 1 Apr 2009 13:59:53 +0000 (13:59 +0000)
2009-04-01  Richard Guenther  <rguenther@suse.de>

* fold-const.c (fold_plusminus_mult_expr): Do not fold
i * 4 + 2 to (i * 2 + 1) * 2.

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

From-SVN: r145403

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

index 54cad4c..f4b1e56 100644 (file)
@@ -1,3 +1,8 @@
+2009-04-01  Richard Guenther  <rguenther@suse.de>
+
+       * fold-const.c (fold_plusminus_mult_expr): Do not fold
+       i * 4 + 2 to (i * 2 + 1) * 2.
+
 2009-04-01  Jakub Jelinek  <jakub@redhat.com>
 
        PR c/37772
index ec06954..9c1a463 100644 (file)
@@ -7466,7 +7466,11 @@ fold_plusminus_mult_expr (enum tree_code code, tree type, tree arg0, tree arg1)
       else
        maybe_same = arg11;
 
-      if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0)
+      if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0
+         /* The remainder should not be a constant, otherwise we
+            end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
+            increased the number of multiplications necessary.  */
+         && TREE_CODE (arg10) != INTEGER_CST)
         {
          alt0 = fold_build2 (MULT_EXPR, TREE_TYPE (arg00), arg00,
                              build_int_cst (TREE_TYPE (arg00),
index 5aef381..5462bea 100644 (file)
@@ -1,3 +1,7 @@
+2009-04-01  Richard Guenther  <rguenther@suse.de>
+
+       * gcc.dg/fold-plusmult-2.c: New testcase.
+
 2009-04-01  Jakub Jelinek  <jakub@redhat.com>
 
        PR c/37772
diff --git a/gcc/testsuite/gcc.dg/fold-plusmult-2.c b/gcc/testsuite/gcc.dg/fold-plusmult-2.c
new file mode 100644 (file)
index 0000000..fd53762
--- /dev/null
@@ -0,0 +1,20 @@
+/* { dg-do compile } */
+/* { dg-options "-fdump-tree-original" } */
+
+int foo (int i)
+{
+  return 2 + i * 4;
+}
+
+/* We do _not_ want the above to be canonicalized to (i * 2 + 1) * 2.  */
+
+int bar (int i)
+{
+  return 4 + i * 2;
+}
+
+/* But eventually this to be canonicalized to (i + 2) * 2.  */
+
+/* { dg-final { scan-tree-dump "i \\\* 4 \\\+ 2" "original" } } */
+/* { dg-final { scan-tree-dump "\\\(i \\\+ 2\\\) \\\* 2" "original" } } */
+/* { dg-final { cleanup-tree-dump "original" } } */