[LLVMdev] Destination register needs to be valid after callee saved register restore when tail calling

Anton Korobeynikov asl at math.spbu.ru
Wed Aug 8 12:12:47 PDT 2007


Hello, Arnold.

> with the sentence i tried to express the question whether there is a  
> way to persuade the code generator to use another register to load (or
> move) the function pointer to (right before the callee saved register
> restore) but thinking a little further that's nonsense.
Why don't define some special op for callee address and custom lower it?
I really suggest you to look into eh_return. It's used in some pretty
tricky situtation inside eh runtime: it it used to return from some eh
runtime code. We already know, how much we should unwind the stack, and
what is the handler (sounds similar, right?). Also %eax and %edx are
used to return eh data and should be preserved in such function. So, in
general, code for eh_return looks like (intel notation here):

         mov ecx, ebp
         add ecx, offset
         mov [ecx], handler
         .... (just usual epilogue)
         mov esp, ecx
         ret

(in real we have slightly different code, because we need to compensate
stack adjustment due to ebp save in epilogue, but idea is the same)

We cannot just jump to handler because we need to change the %esp here.
Also, offset and handler are calculated by code of routine, that's why
such code looks like to be the most painless way to do the things. The
tail call emission has many similar bits, but everything (both handler
and offset) is known during codegen time, that's why we can simplify the
code much.

> TOT means trunk of today (funny because in german, my native language  
> it means death)?
TOT = Top of Tree. But yes, many abbrevations has very funny meaning in
different languages (TOT = "that" in russian :) )

> So what i will be trying then is to emit a copytoreg from the virtual  
> register holding the
> function pointer to ecx before the tailcall node.
This seems to be the right way to do the things.

> the downside here is that ECX is no longer free for passing function  
> arguments. (i am using the x86_fastcall semantics at the moment with  
> first two arguments stored in ecx,edx)
This is the most problematic stuff here. Some notes:
1. You cannot emit a tail call to something returning a struct.
2. It can be tricky to emit a call to function having arguments in
registers (emitting something target-dependent for register holding
function pointer seems to be the right way to do the things: you can
always check, what are livein registers for function and try to emit
something safe).

I'd suggest to start from plain functions with C calling convention.

> and sorry if i am bothering you with questions whose answer should be  
> obvious. i am really a total newbie greenhorn :)
You're welcome :) Many of such questions are not so easy to answer.

--
WBR, Anton Korobeynikov 



More information about the llvm-dev mailing list