<div class="gmail_quote">On Fri, Jun 3, 2011 at 9:55 AM, Chris Lattner <span dir="ltr"><<a href="mailto:clattner@apple.com">clattner@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div id=":4zx">Hi Chandler,<br>
<br>
Does this trap on:<br>
<br>
int x = 1;<br>
x = x << 31;<br></div></blockquote><div><br></div><div>Yep.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div id=":4zx">While this is technically a signed overflow, I believe that we define it as valid.</div>
</blockquote></div><br><div>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.</div>
<div><br></div><div>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.</div>
<div><br></div><div>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?)</div>