This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [patch] convert.c: Fix PR tree-optimization/25125.
kazu@codesourcery.com (Kazu Hirata) wrote on 19.12.05 in <200512192242.jBJMgKJ6006698@sparrowhawk.codesourcery.com>:
> Consider
>
> unsigned short
> f (short a)
> {
> short b;
>
> if (a > 0)
> return 0;
> b = ((int) a) + - (int) 32768;
> return b;
> }
>
> Here is a sequent of events that are relevant to the bug.
>
> 1. convert.c:convert_to_integer changes the type of the addition in
> "b = ...;" from signed int to signed short. That is, we now have
> b = a + -32768, where '+' is done in signed short.
>
> 2. Since the result of overflow is undefined for signed operations, VRP
> assumes that b is in range [-INF, -32768], which is really -32768.
> Note that the return type is unsigned short, so
> "(unsigned short) -32768" comes out to be 32768. In short, for all
> a <= 0, this function returns 32768, which is wrong.
Hmm.
For a = 0, that seems to be the correct result.
For a = -1, the original wants to do b = -32769, which seems undefined,
and thus the result is correct. Similarly for even more negative a.
Where is the actual bug here?
MfG Kai