[cfe-commits] PATCH: Enhance -fcatch-undefined-behavior for the '<<' operator.

Chandler Carruth chandlerc at google.com
Fri Jun 3 10:33:23 PDT 2011


On Fri, Jun 3, 2011 at 9:55 AM, Chris Lattner <clattner at apple.com> wrote:

> Hi Chandler,
>
> Does this trap on:
>
> int x = 1;
> x = x << 31;
>

Yep.


> While this is technically a signed overflow, I believe that we define it as
> valid.
>

Certain Clang and GCC both define this as valid, but I'm a bit leery of not
trapping on it with -fcatch-undefined-behavior. The standard says its flat
out undefined, not just unspecified. It doesn't even distinguish between the
case of 1 << 31 and 2 << 31. It seems strange for Clang to do so. Anyways, I
see this as an open question. I'll let the debate carry on. I'm happy to
adjust the patch in either direction. Currently, I'm leaning toward a
conservative interpretation of the standard by -fcatch-undefined-behavior as
it seems a flag intended to help improve portability / conformance.

>From another perspective, do you want LLVM and Clang's source code to rely
on 1 << 31 not being UB? Currently there are many places in the code that do
this. I was preparing patches for them, but I'll hold off until the
discussion is resolved.

Finally, are there any optimizations that marking the shift left that
results from this as "nsw" enables which would not be enabled by marking it
as "nuw"? (Am I even correct in my thinking that making 1 << 31 be well
defined would move the possible "nsw" flag to an "nuw" to model the allowed
semantics correctly?)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20110603/45c2e21e/attachment.html>


More information about the cfe-commits mailing list