Hi,<br><br>Printf could call main. If it did so, demotion to a local would be illegal. <br><br>You can either compile in c++ mode (which mandates that main is not recursive) or add '-force-attribute=main:norecurse' to get this behaviour. <br><br>James<br><div class="gmail_quote"><div dir="ltr">On Wed, 31 Aug 2016 at 20:52, Mehdi Amini via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><blockquote type="cite"><div>On Aug 30, 2016, at 2:57 PM, Ryan Taylor <<a href="mailto:ryta1203@gmail.com" target="_blank">ryta1203@gmail.com</a>> wrote:</div><br></blockquote></div></div><div style="word-wrap:break-word"><div><blockquote type="cite"><div><div dir="ltr"><div>Yes, the full test case is:</div><div><br></div><div>static int x = 100;</div><div>int y = whatever;</div><div><br></div><div>int main() {</div><div>   x = -101;</div><div>   y = whatever that's not whatever above;</div><div>   printf("%d\n", y);</div><div>   printf("%d\n", x);</div><div>  return 0;</div><div>}</div><div><br></div></div></div></blockquote></div></div><div style="word-wrap:break-word"><div><blockquote type="cite"><div><div dir="ltr"><div>You are correct, in the above test case the globalopt does not make the transformation…. </div></div></div></blockquote><div><br></div><div>I’m more annoyed that it does not turn x into an alloca. With this code:</div><div><br></div><div>$ cat /tmp/test.c</div><div></div></div></div><div style="word-wrap:break-word"><div><div><div>static int x = 100;</div></div></div></div><div style="word-wrap:break-word"><div><div><div>int y = 2;</div></div></div></div><div style="word-wrap:break-word"><div><div><div>int main() {</div><div>   x = -101;</div></div></div></div><div style="word-wrap:break-word"><div><div><div>   y = 1; </div></div></div></div><div style="word-wrap:break-word"><div><div><div>   printf("%d\n", y);</div><div>   printf("%d\n", x);</div><div>  return 0;</div><div>}</div></div></div></div><div style="word-wrap:break-word"><div><div><div>$ clang -O3 -S -emit-llvm -o - /tmp/test.c </div><div>; ModuleID = '/tmp/test.c'<br>source_filename = "/tmp/test.c"<br>target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"<br>target triple = "x86_64-apple-macosx10.12.0"<br><br>@y = global i32 2, align 4<br>@x = internal unnamed_addr global i1 false, align 4<br>@.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1<br><br>; Function Attrs: nounwind ssp uwtable<br>define i32 @main() #0 {<br>  store i1 true, i1* @x, align 4<br>  store i32 1, i32* @y, align 4, !tbaa !2<br>  %1 = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 1)<br>  %2 = load i1, i1* @x, align 4<br>  %3 = select i1 %2, i32 -101, i32 100<br>  %4 = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %3)<br>  ret i32 0<br>}<br><br></div></div></div></div><div style="word-wrap:break-word"><div><br><blockquote type="cite"><div><div dir="ltr"><div>however, I think the original issue still stands, it really shouldn't be doing it here either.</div></div></div></blockquote><div><br></div></div></div><div style="word-wrap:break-word"><div><div>Yes.</div><div><br></div><div>— </div></div></div><div style="word-wrap:break-word"><div><div>Mehdi</div></div></div><div style="word-wrap:break-word"><div><div><br></div><div><br></div><br><blockquote type="cite"><div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Aug 30, 2016 at 5:52 PM, Mehdi Amini <span dir="ltr"><<a href="mailto:mehdi.amini@apple.com" target="_blank">mehdi.amini@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span><br>
> On Aug 30, 2016, at 1:01 PM, Ryan Taylor via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
><br>
</span><span>> Given some code:<br>
><br>
> static int x = 100;<br>
><br>
> int main() {<br>
>    x = 101;<br>
>    printf("%d\n", x);<br>
> }<br>
><br>
> results in:<br>
><br>
> %0 = load i1<br>
> %1 = select %0, 101, 100<br>
<br>
</span>If x is only touched in one function, I’d expect globlaopt to turn it into an alloca instead of leaving it global and shrinking it.<br>
Did you oversimplify the real case where you see this for the sake of the example here?<br>
<br>
—<br>
Mehdi<br>
<span><br>
<br>
> ...<br>
> ...<br>
><br>
> 1) What architecture(s) does this benefit?<br>
> 2) What's the best way to circumvent this in the backend (currently I am just not allowing this opt function to run)? I have tried a few setOperationAction methods to no result. Rather not just comment out core code because it makes porting more difficult.<br>
><br>
> -Ryan<br>
</span>> _______________________________________________<br>
> LLVM Developers mailing list<br>
> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">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/mailman/listinfo/llvm-dev</a><br>
<br>
</blockquote></div><br></div>
</div></blockquote></div></div>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">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/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>