<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On 20 July 2018 at 13:21, Umesh Kalappa via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Edited the Subject.<br>
<br>
On Fri, Jul 20, 2018 at 5:50 PM, Umesh Kalappa <<a href="mailto:umesh.kalappa0@gmail.com">umesh.kalappa0@gmail.com</a>> wrote:<br>
> Hi All ,<br>
><br>
> We are looking at the C sample i.e<br>
><br>
> extern int i,j;<br>
><br>
> int test()<br>
> {<br>
> while(1)<br>
> {       i++;<br>
>         j=20;<br>
> }<br>
> return 0;<br>
> }<br>
><br>
> command used :(clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)<br>
> )<br>
> clang -S test.c -O2<br>
><br>
> the generated asm for x86<br>
><br>
> .L2:<br>
>         jmp     .L2<br>
><br>
> we understand that,the infinite loop is not  deterministic ,compiler<br>
> is free to treat as that as UB and do aggressive optimization ,but we<br>
> need keep the side effects like j=20 untouched by optimization .<br>
><br>
> Please note that using the volatile qualifier for i and j  or empty<br>
> asm("") in the while loop,will stop the optimizer ,but we don't want<br>
> do  that.<br>
><br>
> Anyone from the community ,please share their insights why above<br>
> transformation is right ?<br>
><br>
> and without using volatile or memory barrier ,how we can stop the<br>
> above transformation .<br></blockquote><div><br></div><div>What are you actually trying to achieve? The sample code does not actually DO anything useful, even if it's technically possible to find ways to observe the value of `i` and `j` outside of `main`.<br></div><div>What do you expect the compiler to do with this?<br></div><div><br></div><div>If nothing else is using `i` and `j`, what's the purpose of changing their value?</div><div><br></div><div>If you ARE using those values elsewhere, the using `volatile` is indeed the right thing to do. Or calling some functions inside `main`, that will cause the compiler to understand that "this does something more than just update some variables".</div><div><br></div><div>--</div><div>Mats<br></div><div></div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
><br>
><br>
> Thank you in advance.<br>
> ~Umesh<br>
______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
</blockquote></div><br></div></div>