<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="">- Questions like this are better suited for <a href="http://bugs.llvm.org" class="">bugs.llvm.org</a><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 class=""><br class=""></div><div class="">- Matthias<br class=""><div><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" class="">llvm-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-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" class="">llvm-dev@lists.llvm.org</a><br class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev<br class=""></div></blockquote></div><br class=""></div></body></html>