[LLVMdev] RFC: Tail call optimization X86

Arnold Schwaighofer arnold.schwaighofer at gmail.com
Thu Oct 4 05:29:38 PDT 2007


> > ifeq ($(ARCH),x86)
> > LLCBETAOPTION := -regalloc=local -fast -tail-call-opt -tail-call-opt-
> > align-stack
okay i ll do another round of testing with
LLCBETAOPTION := -tail-call-opt -tail-call-opt-align-stack

> Please remove -regalloc=local -fast. We want to test this patch
> separately. Can you explain the advantages / disadvantages of -tail-
> call-opt-align-stack?

When you do tail call optimization just before calling/jmping to the
callee you sometimes have to adjust the stack pointer.
e.g

int calller(int arg1, int arg2)
{
  return callee(arg2, arg1, 2*arg3;
}

conceptually before jumping to callee the stackpointer has to be
adjusted (by -4 bytes in the example). Now this can cause the stack to
be misaligned (according to the target abi. In order to prevent this
when calculating the argument size (and when tail-call-opt-align-stack
is enabled) i make the resulting argument size a multiple of the
target alignment (minus the return address slot).
So in the example above the argument stack slot size would be 12bytes
for both functions.(on darwin-x86 which requires the stack to be
16byte aligned - which i found out by mistake - because dynamically
linked function calls would not work ;)
this results in a stack adjustment that is a multiple of the target
stack alignment.

Maybe the default should be to perform this stack alignment and turn
it off with a switch (tail-call-opt-disable-stack-alignment) when
required? which one would do if he knew that he never calls to
dynamically linked functions on darwin for example or only from a top
level function (one that is not called from others eg. main).
why didn't i made the switch tail-call-opt-disable-stack-alignment in
the first place? hmm stupidity comes to ones mind ;) no i had the llvm
as a backend for functional languages in mind which might not require
the stack to be aligned and only call printf form a top level
function.

in short:
the disadvantage of -tail-call-opt-align-stack is that the function
stack frame is bigger.
the advantage is that it won't blow up if for example the darwin
linker requires functions to be 16 byte aligned :)

> > What does turning on by default mean? Does that mean it is shown in
> > llc -help or that it is performed every time (without requesting via
> > the command line) when compiling?
>
> The later.
>
> >
> >
> > I would not do the latter since tail call optimization clears the
> > stack frame of the caller (sequence), possibly causing confusion
> > when debugging also resulting code is probably not faster (since
> > arguments are lowered to regular stack slot -where they would be if
> > the call was
>
> Running it as llcbeta will hopefully help us identify the performance
> issues so we can fix them before it's turned on by default.

Okay.

> Please add the potential optimizations (with concrete examples /
> assembly snippets) to README.txt under Target/X86. Thanks!
will do.

regards arnold



More information about the llvm-dev mailing list