<html><head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Sep 17, 2018, at 1:30 PM, David Blaikie <<a href="mailto:dblaikie@gmail.com" class="">dblaikie@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><br class=""><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Mon, Sep 17, 2018 at 1:27 PM Matthias Braun via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a>> wrote:<br class=""></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;line-break:after-white-space" class="">- Questions like this are better suited for <a href="http://bugs.llvm.org/" target="_blank" class="">bugs.llvm.org</a></div></blockquote><div class=""><br class="">Not sure - I think it's pretty fine for discussion here, when someone's not sure it's a bug/intended, etc.<br class=""> </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;line-break:after-white-space" class=""><div class="">- Please provide the full clang invocation. In this case I suspect your problem will go away if you use `-O2` or similar.</div></div></blockquote><div class=""><br class="">Right, I think the original post calls this out as specifically being about -O0.<br class=""></div></div></div></div></blockquote><div>Ah sorry, missed that bit.</div><div><br class=""></div><div>It's a bit harder to argue about unoptimize/-O0 code quality. If you can improve the generated code without making the compiler slower (and without increasing complexity) then that may be a change we want to make, but generally we rather want fast/simple code for the -O0 case.</div><div>Concrete case here does indeed come out with dead values out of fast-isel. Deciding whether this can be improved inside fast-isel or whether adding a dead code elimination pass will affect compile time would need someone to investigate/benchmark...</div><div><br class=""></div><div>- Matthias</div><div><br class=""></div><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_quote"><div class=""><br class="">- Dave<br class=""> </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;line-break:after-white-space" class=""><div class=""><br class=""></div><div class=""></div></div><div style="word-wrap:break-word;line-break:after-white-space" class=""><div class="">- Matthias</div></div><div style="word-wrap:break-word;line-break:after-white-space" class=""><div class=""><br class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On Sep 14, 2018, at 8:16 AM, palpar via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a>> wrote:</div><br class="m_6059422875266927923Apple-interchange-newline"><div class=""><div dir="ltr" class="">Hi everyone,<div class=""><br class=""></div><div class="">I found that LLVM generates redundant code when calling functions with constant parameters, with optimizations disabled.</div><div class=""><br class=""></div><div class=""><div class="">Consider the following C code snippet:</div><div class=""><br class=""></div><div class="">int foo(int x, int y);</div><div class=""><br class=""></div><div class=""><div class="">void bar()</div><div class="">{</div><div class=""><span style="white-space:pre-wrap" class="">        </span>foo(1, 2);</div><div class=""><span style="white-space:pre-wrap" class=""> </span>foo(3, 4);</div><div class="">}</div></div><div class=""><br class=""></div><div class="">Clang/LLVM 6.0 generates the following assembly code:</div><div class="">_bar:</div><div class=""><span style="white-space:pre-wrap" class="">     </span>subl<span style="white-space:pre-wrap" class=""> </span>$32, %esp</div><div class=""><span style="white-space:pre-wrap" class="">  </span>movl<span style="white-space:pre-wrap" class=""> </span>$1, %eax</div><div class=""><span style="white-space:pre-wrap" class="">   </span>movl<span style="white-space:pre-wrap" class=""> </span>$2, %ecx</div><div class=""><span style="white-space:pre-wrap" class="">   </span>movl<span style="white-space:pre-wrap" class=""> </span>$1, (%esp)</div><div class=""><span style="white-space:pre-wrap" class=""> </span>movl<span style="white-space:pre-wrap" class=""> </span>$2, 4(%esp)</div><div class=""><span style="white-space:pre-wrap" class="">        </span>movl<span style="white-space:pre-wrap" class=""> </span>%eax, 28(%esp)</div><div class=""><span style="white-space:pre-wrap" class="">     </span>movl<span style="white-space:pre-wrap" class=""> </span>%ecx, 24(%esp)</div><div class=""><span style="white-space:pre-wrap" class="">     </span>calll<span style="white-space:pre-wrap" class="">        </span>_foo</div><div class=""><span style="white-space:pre-wrap" class="">       </span>movl<span style="white-space:pre-wrap" class=""> </span>$3, %ecx</div><div class=""><span style="white-space:pre-wrap" class="">   </span>movl<span style="white-space:pre-wrap" class=""> </span>$4, %edx</div><div class=""><span style="white-space:pre-wrap" class="">   </span>movl<span style="white-space:pre-wrap" class=""> </span>$3, (%esp)</div><div class=""><span style="white-space:pre-wrap" class=""> </span>movl<span style="white-space:pre-wrap" class=""> </span>$4, 4(%esp)</div><div class=""><span style="white-space:pre-wrap" class="">        </span>movl<span style="white-space:pre-wrap" class=""> </span>%eax, 20(%esp)</div><div class=""><span style="white-space:pre-wrap" class="">     </span>movl<span style="white-space:pre-wrap" class=""> </span>%ecx, 16(%esp)</div><div class=""><span style="white-space:pre-wrap" class="">     </span>movl<span style="white-space:pre-wrap" class=""> </span>%edx, 12(%esp)</div><div class=""><span style="white-space:pre-wrap" class="">     </span>calll<span style="white-space:pre-wrap" class="">        </span>_foo</div><div class=""><span style="white-space:pre-wrap" class="">       </span>movl<span style="white-space:pre-wrap" class=""> </span>%eax, 8(%esp)</div><div class=""><span style="white-space:pre-wrap" class="">      </span>addl<span style="white-space:pre-wrap" class=""> </span>$32, %esp</div><div class=""><span style="white-space:pre-wrap" class="">  </span>retl</div><div class=""><span style="white-space:pre-wrap" class="">       </span><br class=""></div><div class="">Note how the constants are stored in registers but when saving the parameters on the stack for the call the immediate values are used. The registers are still stored on the stack probably because it's the caller's responsibility once they were used (which seems expected).</div><div class="">I think the problem comes from the fact that LLVM unconditionally allocates a register for each parameter value regardless if it's used later or not.</div><div class="">If the stack space of the program is sufficiently large this is probably not a problem, but otherwise if there is a large number of such calls, despite not recursive, it can lead to stack overflow. Do you think I should create a bug report for this?</div><div class=""><br class=""></div><div class="">(Similarly, the return value of the function could be not saved but the LLVM IR code that Clang generates has the call with assignment so at this point LLVM couldn't possibly know.</div><div class=""><div class="">define void @bar() #0 {</div><div class="">  %call = call i32 @foo(i32 1, i32 2)</div><div class="">  %call1 = call i32 @foo(i32 3, i32 4)</div><div class="">  ret void</div><div class="">}</div></div><div class="">)</div></div><div class=""><br class=""></div><div class="">Thanks,</div><div class="">Alpar</div></div>
_______________________________________________<br class="">LLVM Developers mailing list<br class=""><a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a><br class=""><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank" class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br class=""></div></blockquote></div><br class=""></div></div>_______________________________________________<br class="">
LLVM Developers mailing list<br class="">
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a><br class="">
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank" class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br class="">
</blockquote></div></div>
</div></blockquote></div><br class=""></body></html>