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;
Confirmed.
Mine as this will get fixed when 15459 gets fixed.
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.
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.
As I said before I was not going to fix a fold problem (for the first testcase).
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.
Mine.
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.
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
The fold missed optimizations are fixed.
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
Fixed.