This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] Constant fold -A - B as -B - A
- From: Roger Sayle <roger at www dot eyesopen dot com>
- To: "Joseph S. Myers" <jsm28 at cam dot ac dot uk>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Wed, 15 Jan 2003 06:37:56 -0700 (MST)
- Subject: Re: [PATCH] Constant fold -A - B as -B - A
On Wed, 15 Jan 2003, Joseph S. Myers wrote:
> On Tue, 14 Jan 2003, Roger Sayle wrote:
> > Not only is it also safe for integer constants, but also for
> > other forms of expressions that are cheaply negated. The obvious
> > case is where B is a negation, i.e. -C, would transform -A - -C
> > into C - A, but this is already handled by constant folding.
> > A more interesting case is where B is a subtraction D-E, then
> > -A - (D-E) becomes (E-D) - A, which saves a negation.
>
> But this isn't safe if D-E is the most negative integer. Various code
> in GCC knows that signed integer overflow is undefined and this
> transformation could run into such code, whether present now or added
> later.
Hopefully, the code that believes that signed integer addition is
undefined and takes advantage of it is confined to the language
front-ends. I believe GCC supports languages that define (or
expect) the behaviour of signed integer overflow.
Indeed there is a large amount of code in my patch to disable this
optimization if "B" is the most negative integer constant. Not
because the transformation is unsafe on GCC's supported targets,
but because we'd fall foul of C/C++'s undefined overflow semantics.
Roger
--