[LLVMdev] The nsw story

Eli Friedman eli.friedman at gmail.com
Mon Dec 5 18:23:21 PST 2011


On Mon, Dec 5, 2011 at 5:50 PM, me22 <me22.ca at gmail.com> wrote:
> On Thu, Dec 1, 2011 at 09:23, Dan Gohman <gohman at apple.com> wrote:
>>
>> int a = INT_MAX, b = 1;
>> long c = (long)(a + b);
>>
>> What is the value of c, on an LP64 target?
>>
>> If a and b are promoted to 64-bit, c is 0x0000000080000000.
>>
>> In a world where signed add overflow returns undef, c could be any of
>>  0x0000000000000000
>>  ...
>>  0xffffffffffffffff
>> however it can't ever be 0x0000000080000000, because there's no 32-bit value
>> the undef could take which sign-extends into that 64-bit value.
>>
>
> But since the add nsw returns undef, then isn't c "sext i32 undef to
> i64"

Yes.

> and thus also undef?

No: it's some value between INT32_MIN and INT32_MAX.  Think of undef
as a read from an arbitrary register: you can't predict what it will
contain, but you know it contains some representable value.

-Eli




More information about the llvm-dev mailing list