[LLVMdev] Understanding tail calls

Duncan Sands baldrick at free.fr
Thu Mar 18 02:11:42 PDT 2010


Hi Scott,

> So if I am looking at a CallInst, and want to know if the callee is
> going to be instrumented by my pass, I can check F->isWeakForLinker(),
> where F is the called function. But this just tells me when the
> function definition *may* be replaced. I want to know precisely when
> the CallInst is a call to a function that I am going to instrument as
> part of my pass (ignore indirect invocations for the moment).
> Obviously I could do an initial pass and build an explicit list of
> such functions. But is there an easier way by just looking at the
> Function object?
>
> I guess I am looking for something like
> F->IsGoingToGetReplacedAtLinkTime(). Actually, that seems like it is
> probably impossible to resolve during the pass.

I don't really understand what you are asking.  In general a function with
weak linkage may or may not be replaced by another at link time, but you
can't tell before link time.  For example, suppose a function called F (I
will refer to this as F1) has weak linkage.  If at link time you link with
another object file that also defines a function called F (I will refer to
this as F2) but not with weak linkage, then F1 will be discarded and F2 is
what will end up in the executable.  On the other hand, suppose F2 also has
weak linkage, then I think one of F1/F2 is chosen essentially randomly.
Finally, if at link time no other object file defines a function called F
then F1 will be used.  So what happens is determined by what the user passes
to the linker, and as such is not known at compile time.

A philosophical remark: if you could tell at compile time which function
bodies will be in the final executable, then there would be no point in
having weak linkage: if you can tell that the function body is not going
to be used then you might as well throw it away (i.e. replace with a
declaration) and not bother optimizing it; on the other hand, if you are
sure that it is going to be used then you might as well give it strong
linkage.

Ciao,

Duncan.



More information about the llvm-dev mailing list