<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Thanks for confirming that it seems to be a bug.</div><div dir="ltr">I was studying the code you mentioned and X86FastISel::fastLowerCall() calls getRegForValue() for each argument. Then the second iteration over the arguments (after it called CCInfo.AnalyzeCallOperands()) may decide to call X86FastEmitStore() if not VA.isRegLoc() and ArgVal is a constant (ignoring the register, but that was already marked used I guess by getRegForValue).</div><div dir="ltr"><a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86FastISel.cpp?view=markup">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86FastISel.cpp?view=markup</a><br></div><div>Could we maybe delay the call to getRegForValue() after the analyze so that we don't call it when not needed?</div></div></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Fri, Sep 14, 2018 at 9:26 PM Reid Kleckner <<a href="mailto:rnk@google.com">rnk@google.com</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">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" target="_blank">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>
</blockquote></div>