<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Feb 29, 2016 at 10:50 AM, Sanjoy Das <span dir="ltr"><<a href="mailto:sanjoy@playingwithpointers.com" target="_blank">sanjoy@playingwithpointers.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Mon, Feb 29, 2016 at 10:42 AM, Mehdi Amini <<a href="mailto:mehdi.amini@apple.com">mehdi.amini@apple.com</a>> wrote:<br>
><br>
> On Feb 29, 2016, at 10:38 AM, Xinliang David Li via llvm-dev<br>
> <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br>
><br>
> ok thanks. A more reduced test case can show different behavior between O2<br>
> and O0.<br>
><br>
> Say we have<br>
><br>
> unsigned maybe_divide (unsigned *ptr) {<br>
> int flag = false;<br>
> unsigned val = 500/ptr[0];<br>
> if (flag)<br>
> return val;<br>
> return (unsigned)(intptr_t)ptr);<br>
> }<br>
><br>
> int main() {<br>
> unsigned g = 0;<br>
> return maybe_divide(&g);<br>
> }<br>
><br>
><br>
> At O2, it runs fine, but at O0 it core dumps.<br>
><br>
><br>
> I believe this program has a divide by zero and is not correct.<br>
> By luck the optimization removes the faulty instruction, which does not mean<br>
> the program is well formed.<br>
><br>
> IMO this is different from Sanjoy's example, where a wrong optimization<br>
> introduces the error.<br>
<br>
</span>Yup; in my example there actually is no place where the program<br>
divides by zero. Both the call sites that call maybe_divide store a<br>
non-zero value to ptr[0] before calling maybe_divide.<br></blockquote><div><br></div><div>yes -- it is UB if user writes code like this. What if the g=0 is speculatively moved above the call of maybe_divide at O2? My point is that such runtime behavior difference may not specific to multi-TU definition scenario.</div><div><br></div><div>David</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<span class="HOEnZb"><font color="#888888"><br>
-- Sanjoy<br>
</font></span></blockquote></div><br></div></div>