[PATCH] Fix canonicalization of addresses

Richard Guenther richard.guenther@gmail.com
Mon Dec 29 17:07:00 GMT 2008

On Tue, Dec 23, 2008 at 12:11 PM, Eric Botcazou <ebotcazou@adacore.com> wrote:
> Hi,
> this comes from the discussion started yesterday on the main list:
>  http://gcc.gnu.org/ml/gcc/2008-12/msg00329.html
> Canonicalization of addresses with small offsets at the tree level has been de
> facto modified 3 years ago: it used to be for example
>  base + 2*ind - 2
> now it's
>  base + (1-ind) * -2
> and you may even end up with BIT_NOT_EXPRs in address calculations when the
> folder is subsequently called on things like (-1-ind).  This pessimizes the
> initial RTL and, while on x86/x86-64 CSE and FWPROP can generally mitigate
> the problem, this can have an impact e.g. on PowerPC for code with numerous
> contiguous array accesses.
> Interestingly, if you undo the whole change at the tree level, you get wrong
> code out of the vectorizer for some tests of the testsuite.
> We use the attached patch locally (in a 4.3-based compiler) to undo the most
> visible pessimizations.  Tested on i586-suse-linux, OK for mainline?

So after the patch we would canonicalize the above to base + (ind - 1) * 2?
To even catch the case where the user writes (1-ind)*-2 can you instead extend
fold_binary at

    case MULT_EXPR:
      /* (-A) * (-B) -> A * B  */
      if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
        return fold_build2 (MULT_EXPR, type,
                            fold_convert (type, TREE_OPERAND (arg0, 0)),
                            fold_convert (type, negate_expr (arg1)));
      if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
        return fold_build2 (MULT_EXPR, type,
                            fold_convert (type, negate_expr (arg0)),
                            fold_convert (type, TREE_OPERAND (arg1, 0)));

to also handle negative constants instead of just NEGATE_EXPR?

IMHO this would be the better approach.  I consider the expr.c hunk a
hack - what
fixes this up at -O2 vs. -Os?  Can we enable that at -Os as well instead?


> 2008-12-23  Eric Botcazou  <ebotcazou@adacore.com>
>        * fold-const.c (extract_muldiv): Remove obsolete comment.
>        (fold_plusminus_mult_expr): Use only positive power-of-two factors.
>        * expr.c (get_inner_reference): Canonicalize offset.
> --
> Eric Botcazou

More information about the Gcc-patches mailing list