<div dir="ltr">I think this is a bug in fastisel. It sometimes generates unnecessary materializations like this. It's unclear why they end up getting spilled, though.</div><br><div class="gmail_quote"><div dir="ltr">On Fri, Sep 14, 2018 at 8:16 AM palpar 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 dir="ltr">Hi everyone,<div><br></div><div>I found that LLVM generates redundant code when calling functions with constant parameters, with optimizations disabled.</div><div><br></div><div><div>Consider the following C code snippet:</div><div><br></div><div>int foo(int x, int y);</div><div><br></div><div><div>void bar()</div><div>{</div><div><span style="white-space:pre-wrap">       </span>foo(1, 2);</div><div><span style="white-space:pre-wrap">       </span>foo(3, 4);</div><div>}</div></div><div><br></div><div>Clang/LLVM 6.0 generates the following assembly code:</div><div>_bar:</div><div><span style="white-space:pre-wrap">  </span>subl<span style="white-space:pre-wrap">    </span>$32, %esp</div><div><span style="white-space:pre-wrap">        </span>movl<span style="white-space:pre-wrap">    </span>$1, %eax</div><div><span style="white-space:pre-wrap"> </span>movl<span style="white-space:pre-wrap">    </span>$2, %ecx</div><div><span style="white-space:pre-wrap"> </span>movl<span style="white-space:pre-wrap">    </span>$1, (%esp)</div><div><span style="white-space:pre-wrap">       </span>movl<span style="white-space:pre-wrap">    </span>$2, 4(%esp)</div><div><span style="white-space:pre-wrap">      </span>movl<span style="white-space:pre-wrap">    </span>%eax, 28(%esp)</div><div><span style="white-space:pre-wrap">   </span>movl<span style="white-space:pre-wrap">    </span>%ecx, 24(%esp)</div><div><span style="white-space:pre-wrap">   </span>calll<span style="white-space:pre-wrap">   </span>_foo</div><div><span style="white-space:pre-wrap">     </span>movl<span style="white-space:pre-wrap">    </span>$3, %ecx</div><div><span style="white-space:pre-wrap"> </span>movl<span style="white-space:pre-wrap">    </span>$4, %edx</div><div><span style="white-space:pre-wrap"> </span>movl<span style="white-space:pre-wrap">    </span>$3, (%esp)</div><div><span style="white-space:pre-wrap">       </span>movl<span style="white-space:pre-wrap">    </span>$4, 4(%esp)</div><div><span style="white-space:pre-wrap">      </span>movl<span style="white-space:pre-wrap">    </span>%eax, 20(%esp)</div><div><span style="white-space:pre-wrap">   </span>movl<span style="white-space:pre-wrap">    </span>%ecx, 16(%esp)</div><div><span style="white-space:pre-wrap">   </span>movl<span style="white-space:pre-wrap">    </span>%edx, 12(%esp)</div><div><span style="white-space:pre-wrap">   </span>calll<span style="white-space:pre-wrap">   </span>_foo</div><div><span style="white-space:pre-wrap">     </span>movl<span style="white-space:pre-wrap">    </span>%eax, 8(%esp)</div><div><span style="white-space:pre-wrap">    </span>addl<span style="white-space:pre-wrap">    </span>$32, %esp</div><div><span style="white-space:pre-wrap">        </span>retl</div><div><span style="white-space:pre-wrap">     </span><br></div><div>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>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>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><br></div><div>(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><div>define void @bar() #0 {</div><div>  %call = call i32 @foo(i32 1, i32 2)</div><div>  %call1 = call i32 @foo(i32 3, i32 4)</div><div>  ret void</div><div>}</div></div><div>)</div></div><div><br></div><div>Thanks,</div><div>Alpar</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>