This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Fix fold_plusminus_mult_expr somewhat
- From: Richard Guenther <rguenther at suse dot de>
- To: gcc-patches at gcc dot gnu dot org
- Date: Wed, 1 Apr 2009 15:55:47 +0200 (CEST)
- Subject: [PATCH] Fix fold_plusminus_mult_expr somewhat
As intermediate step to eventually not canonicalize i * 2 + 4
to (i + 2) * 2 the following patch disables the clearly non-profitable
canonicalization of i * 4 + 2 to (i * 2 + 1) * 2.
Bootstrapped and tested on x86_64-unknown-linux-gnu, applied to trunk.
Richard.
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.
Index: gcc/fold-const.c
===================================================================
*** gcc/fold-const.c (revision 145396)
--- gcc/fold-const.c (working copy)
*************** fold_plusminus_mult_expr (enum tree_code
*** 7466,7472 ****
else
maybe_same = arg11;
! if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0)
{
alt0 = fold_build2 (MULT_EXPR, TREE_TYPE (arg00), arg00,
build_int_cst (TREE_TYPE (arg00),
--- 7466,7476 ----
else
maybe_same = arg11;
! 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: gcc/testsuite/gcc.dg/fold-plusmult-2.c
===================================================================
*** gcc/testsuite/gcc.dg/fold-plusmult-2.c (revision 0)
--- gcc/testsuite/gcc.dg/fold-plusmult-2.c (revision 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" } } */