PTR-PLUS merge into the mainline

Richard Guenther rguenther@suse.de
Thu Jul 5 12:10:00 GMT 2007


On Thu, 5 Jul 2007, Roman Zippel wrote:

> Hi,
> 
> On Thu, 5 Jul 2007, Richard Guenther wrote:
> 
> > > > The code to fold_binary was added by:
> > > > r107218 | rguenth | 2005-11-19 03:29:10 -0800 (Sat, 19 Nov 2005) | 9 lines
> > > > 
> > > > 2005-11-19  Richard Guenther  <rguenther@suse.de>
> > > > 
> > > >        PR middle-end/23294
> > > >        * fold-const.c (fold_plusminus_mult_expr): New function.
> > > >        (fold_binary): Use to canonicalize PLUS_EXPR and MINUS_EXPR
> > > >        cases, remove now unnecessary code.
> > > > 
> > > >        * gcc.dg/tree-ssa/pr23294.c: New testcase.
> > > > 
> > > > And it looks like it was doing this transformation this way on purpose.
> > > > Now as I mentioned before the way this should be done is using PRE/FRE
> > > > to catch the redudent multiplication.
> > > 
> > > I have to agree, IMHO it looks this optimization is done to early.
> > > Richard, it would be great if you comment on this.
> > > Thanks.
> > 
> > No, the fold code just does canonicalization.  If you remove that you
> > just switch the cases that are not handled.
> 
> What makes "(i + 1) * 4" the canonical form of "(i * 4) + 4" compared to 
> other expressions like "(i * 4) + 8"?

It's an arbitrary decision by fold.  For (i + 2) * 4 the canonical form
is (i * 4) + 8.  For (i * j) + j the canonical form is (i + 1) * j,
for (i * j) + 2 * j it is (i + 2) * j.  Now we can easily make constants
special here.

> >  The real fix is in the
> > value numberer that should value number both kinds the same.
> 
> Sorry, I have no idea what this means.

This means that without context, neither (i + 1) * 4 nor (i * 4) + 4
is better or cheaper.  With context that allows simplification this
simplification should be possible regardless of what form the
expression is in.  Remember that fold only does the transformation
if it sees the full expression, so writing

  tmp1 = i + 1;
  tmp1 = tmp1 * 4;
  tmp2 = i * 4;
  tmp2 = tmp2 + 4;
  if (tmp1 == tmp2)
    ...

should be optimized as well, but fold does nothing about that.

Richard.



More information about the Gcc mailing list