[llvm-dev] Understanding tail call

Charlotte Delenk via llvm-dev llvm-dev at lists.llvm.org
Fri Sep 25 06:05:38 PDT 2020


Hi Haoran,

On 9/25/20 11:33 AM, Haoran Xu via llvm-dev wrote:
> Hi friendly LLVM Devs,
>
> 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.
>
> So I tried the following two really simple functions:
>
>     externvoidg(int*);
>     voidf1() {
>     intx;
>        g(&x);
>     }
>     voidf2(int* x) {
>        g(x);
>     }
>
> It turns out that 'f1' does not have tail call optimization, but 'f2' 
> does.
> 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?

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.

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.

> 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?
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)
>
> Thanks!
>
> Best,
> Haoran
>
>
>
>
>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200925/180df047/attachment.html>


More information about the llvm-dev mailing list