[LLVMdev] Question about ExprConstant optimization of IR stage

Tim Northover
Thu, 28 Nov 2013 14:02:51 -0000

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.