Bug 15255 - [tree-ssa] a * 2 + a * 2 is not converted to a * 4
Summary: [tree-ssa] a * 2 + a * 2 is not converted to a * 4
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: tree-ssa
: P2 enhancement
Target Milestone: 4.4.0
Assignee: Richard Biener
URL:
Keywords: missed-optimization
Depends on: 15459
Blocks: 19987 32120
  Show dependency treegraph
 
Reported: 2004-05-03 07:13 UTC by Kazu Hirata
Modified: 2023-12-30 01:15 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2008-04-28 20:19:51


Attachments
Do the required folding (605 bytes, patch)
2005-04-18 02:14 UTC, James A. Morrison
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Kazu Hirata 2004-05-03 07:13:59 UTC
int
foo (int a)
{
  return a * 2 + a * 2;
}

The last SSA form looks like:

foo (a)
{
  int T.0;

<bb 0>:
  T.0_2 = a_1 + a_1;
  return T.0_2 * 2;

}

Why not return a_1 * 4?

Interestingly, a * 3 + a * 5 is converted to a * 8, but if I compute like so

int
foo (int a)
{
  int b = a * 3;
  int c = a * 5;
  return b + c;
}

I get:

  b_2 = a_1 * 3;
  c_3 = a_1 * 5;
  return b_2 + c_3;
Comment 1 Andrew Pinski 2004-05-03 11:24:24 UTC
Confirmed.
Comment 2 Andrew Pinski 2004-05-24 01:26:21 UTC
Mine as this will get fixed when 15459 gets fixed.
Comment 3 Andrew Pinski 2004-05-24 15:57:38 UTC
The first one is because fold converted it to be (a + a) * 2 but fold should be doing this conversion.  PR 
15459 fixes the other problem in the PR, I will submitting a new patch today with more comments and 
more like the RTL combiner. 
Comment 4 Andrew Pinski 2004-06-21 05:13:26 UTC
I will submit my patch tree combine patch tonight which fixes the second case but not the first as the 
first is a problem in fold.
Comment 5 Andrew Pinski 2005-02-15 22:22:31 UTC
As I said before I was not going to fix a fold problem (for the first testcase).
Comment 6 James A. Morrison 2005-04-18 02:14:18 UTC
Created attachment 8671 [details]
Do the required folding

 This also allows something like:
int
foo (int a)
{
	  return a * 2 + a * 2 + a * 3 + a;
}

to be simplified into a*8.
Comment 7 Richard Biener 2008-04-28 20:19:51 UTC
Mine.
Comment 8 Richard Biener 2008-04-29 14:17:27 UTC
We already handle most of the foldings in fold_plusminus_mult_expr, just the
A + A -> 2 * A folding is not done (for a reason).

We also miss (A + A) * Cst -> A * 2 * Cst, which is what I am going to implement
in addition to extending the tree re-association pass. 
Comment 9 Richard Biener 2008-04-29 16:00:55 UTC
Subject: Bug 15255

Author: rguenth
Date: Tue Apr 29 15:59:43 2008
New Revision: 134798

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=134798
Log:
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.

Added:
    trunk/gcc/testsuite/gcc.dg/fold-plusmult.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/fold-const.c
    trunk/gcc/testsuite/ChangeLog

Comment 10 Richard Biener 2008-04-29 16:03:44 UTC
The fold missed optimizations are fixed.
Comment 11 Richard Biener 2008-08-13 08:58:48 UTC
Subject: Bug 15255

Author: rguenth
Date: Wed Aug 13 08:57:20 2008
New Revision: 139048

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=139048
Log:
2008-08-13  Richard Guenther  <rguenther@suse.de>

	PR tree-optimization/15255
	* tree-ssa-reassoc.c (linearize_expr_tree): Declare.
	(struct oecount_s): New struct and VEC types.
	(cvec): New global.
	(oecount_hash): New function.
	(oecount_eq): Likewise.
	(oecount_cmp): Likewise.
	(zero_one_operation): New function.
	(build_and_add_sum): Likewise.
	(undistribute_ops_list): Perform un-distribution of multiplication
	and division on the chain of summands.
	(should_break_up_subtract): Also break up subtracts for factors.
	(reassociate_bb): Delete dead visited statements.
	Call undistribute_ops_list.  Re-sort and optimize if it did something.
	* passes.c (init_optimization_passes): Move DSE before
	reassociation.
	* tree-ssa-loop-niter.c (stmt_dominates_stmt_p): Correctly handle
	PHI nodes.

	* gcc.dg/tree-ssa/reassoc-14.c: New testcase.
	* gcc.dg/tree-ssa/reassoc-15.c: Likewise.
	* gcc.dg/tree-ssa/reassoc-16.c: Likewise.
	* gcc.dg/torture/reassoc-1.c: Likewise.
	* gcc.dg/tree-ssa/recip-2.c: Adjust.
	* gcc.dg/tree-ssa/recip-6.c: Likewise.
	* gcc.dg/tree-ssa/recip-7.c: Likewise.
	* gfortran.dg/reassoc_4.f: Likewise.

Added:
    trunk/gcc/testsuite/gcc.dg/torture/reassoc-1.c
    trunk/gcc/testsuite/gcc.dg/tree-ssa/reassoc-14.c
    trunk/gcc/testsuite/gcc.dg/tree-ssa/reassoc-15.c
    trunk/gcc/testsuite/gcc.dg/tree-ssa/reassoc-16.c
    trunk/gcc/testsuite/gcc.dg/tree-ssa/reassoc-17.c
    trunk/gcc/testsuite/gcc.dg/tree-ssa/reassoc-18.c
    trunk/gcc/testsuite/gfortran.dg/reassoc_4.f
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/passes.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gcc.dg/tree-ssa/recip-2.c
    trunk/gcc/testsuite/gcc.dg/tree-ssa/recip-6.c
    trunk/gcc/testsuite/gcc.dg/tree-ssa/recip-7.c
    trunk/gcc/tree-ssa-loop-niter.c
    trunk/gcc/tree-ssa-reassoc.c

Comment 12 Richard Biener 2008-08-13 09:00:36 UTC
Fixed.