[LLVMdev] Question about ExprConstant optimization of IR stage

Haishan hndxvon at 163.com
Fri Nov 29 05:26:16 PST 2013


Hi Tim,
  Firstly, thanks for your reply.
I agree on your idea about integer overflow in this test.
But, in fact, the content of this test is the same to gcc\testsuite\gcc.c-torture\execute\920612-1.c.
gcc can handle it correctly.
Moreover, when I compile this test using clang with optlevel O0 instead of O2.
Its execution result is the same to gcc.
That to say, for this test, clang with optlevel O2 execution result is different from O0.
Is it reasonable?
Thanks a lot.
-Haishan



At 2013-11-28 22:02:51,"Tim Northover" <t.p.northover at gmail.com> wrote:
>Hi Haishan,
>
>>   int foo(int j) {
>>     return ++j > 0;
>>   }
>>   int main() {
>>     if (foo(((~0U)>>1)))
>>       abort();
>>     exit(0)
>>   }
>
>This test contains undefined behaviour, and you can never rely on the
>compiler doing anything predictable with that. Specifically, the
>result of casting (~0U) >> 1 to an int is almost certainly INT_MAX and
>when foo increments it integer overflow occurs, which is undefined.
>
>Cheers.
>
>Tim.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20131129/8b5cd85f/attachment.html>


More information about the llvm-dev mailing list