https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114999

--- Comment #15 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Jennifer Schmitz from comment #12)
> Created attachment 60149 [details]
> Proposed patch for detecting abs diff for signed integers

Note the change for `-(A - B) -> B - A` with the addition of a nop_convert can
introduce a signed integer overflow if the cast is from signed to unsigned.

That is:

`-(unsigned)(int1 = int2)` is not the same as `(unsigned)(int2 - int1)` if int1
is INT_MIN and int2 is 0.  The subtraction has to be done in unsigned type to
be valid transformation.

The other way around is a problem too:
`-(signed)(unsigned1 - unsigned2)`
that is take unsigned1 == 1 and unsigned2 = (unsigned)INT_MIN. INT_MIN-1 would
have an overflow; but the original (1-(unsigned)INT_MIN) is well defined (for
32bit it is -2147483647) and taking negative of that also would be well defined
in signed integer.

Reply via email to