<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Hi Haoran,<br>
</p>
<div class="moz-cite-prefix">On 9/25/20 11:33 AM, Haoran Xu via
llvm-dev wrote:<br>
</div>
<blockquote type="cite"
cite="mid:CA+jENmLubOwZxfbRBAPugqLyAKvnx9cVd90hjq=PFZcDAxJQTQ@mail.gmail.com">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<div dir="ltr">
<div>Hi friendly LLVM Devs,</div>
<div><br>
</div>
<div>I'm trying to understand the technical details of tail call
optimization, but unfortunately I hit some issues that I
couldn't figure out myself.</div>
<div><br>
</div>
<div>So I tried the following two really simple functions:</div>
<div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px
0.8ex;border-left:1px solid
rgb(204,204,204);padding-left:1ex">
<div><span style="color:rgb(0,0,255)">extern</span><span
style="color:rgb(0,0,0)"> </span><span
style="color:rgb(0,0,255)">void</span><span
style="color:rgb(0,0,0)"> g(</span><span
style="color:rgb(0,0,255)">int</span><span
style="color:rgb(0,0,0)">*);</span></div>
<div><span style="color:rgb(0,0,255)">void</span><span
style="color:rgb(0,0,0)"> f1() {</span></div>
<div><span style="color:rgb(0,0,0)"> </span><span
style="color:rgb(0,0,255)">int</span><span
style="color:rgb(0,0,0)"> x;</span></div>
<div><span style="color:rgb(0,0,0)"> g(&x);</span></div>
<div><span style="color:rgb(0,0,0)">}</span></div>
<div><span style="color:rgb(0,0,255)">void</span><span
style="color:rgb(0,0,0)"> f2(</span><span
style="color:rgb(0,0,255)">int</span><span
style="color:rgb(0,0,0)">* x) {</span></div>
<div><span style="color:rgb(0,0,0)"> g(x);</span></div>
<div><span style="color:rgb(0,0,0)">}</span></div>
</blockquote>
</div>
<div>It turns out that 'f1' does not have tail call
optimization, but 'f2' does. <br>
</div>
<div>My current really naive understanding is that a call
happening as the last statement of the function can be
optimized as a tail call. However, why cannot 'f1' do this? <br>
</div>
</div>
</blockquote>
<p>There are some more restrictions to tail calls, namely it needs
to be possible to restructure the function in such a way that the
function epilogue has already been run. Local variables are scoped
and go out of scope at the end of the function. The function
epilogue is responsible for restoring the stack and (in C++)
running the destructors of stack variables.</p>
<p><tt>x however can't be converted into a register variable as it
can in f2, as the & operator requires the variable to be
placed in memory.</tt><br>
</p>
<blockquote type="cite"
cite="mid:CA+jENmLubOwZxfbRBAPugqLyAKvnx9cVd90hjq=PFZcDAxJQTQ@mail.gmail.com">
<div dir="ltr">
<div>Is there any recommended articles that I should read if I
were to understand the internals of tail call optimization in
LLVM, or more generally, performance optimization on reducing
function calling overhead? I know there are a bunch of
different calling conventions, optimized for different use
cases, but I don't know any of the details. Can someone
suggest a good introductory material that I should read to
understand what are the pros and cons of each calling
convention, and what should be the best calling convention for
a given use case?</div>
</div>
</blockquote>
non-cdecl calling conventions are highly architecture dependent. You
are likely better off instructing LLVM to inline functions by
choosing a higher optimization level (-O3) and Link Time
Optimizations (-flto=thin)<br>
<blockquote type="cite"
cite="mid:CA+jENmLubOwZxfbRBAPugqLyAKvnx9cVd90hjq=PFZcDAxJQTQ@mail.gmail.com">
<div dir="ltr">
<div><br>
</div>
<div>Thanks!</div>
<div><br>
</div>
<div>Best,</div>
<div>Haoran<br>
</div>
<div><br>
</div>
<div>
<div
style="color:rgb(0,0,0);background-color:rgb(255,255,254);font-family:"Consolas,
"">
<div><span style="color:rgb(0,0,0)"><br>
</span></div>
<br>
<br>
</div>
</div>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
LLVM Developers mailing list
<a class="moz-txt-link-abbreviated" href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>
<a class="moz-txt-link-freetext" href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a>
</pre>
</blockquote>
</body>
</html>