[llvm-dev] How to efficiently extract the calledFunction from a complex CallInst?

David Blaikie via llvm-dev llvm-dev at lists.llvm.org
Thu Nov 12 19:48:00 PST 2015


On Thu, Nov 12, 2015 at 7:27 PM, Shen Liu via llvm-dev <
llvm-dev at lists.llvm.org> wrote:

> Hi all,
>
> Usually if we want to get the called Function we can directly use
> CallInst->getCalledFunction(), however, today i encounter an unusual
> CallInst as follows:
>
>  %call11 = call double (...)* bitcast (double ()* @quantum_frand to double
> (...)*)()
>
> the original C source involve type cast:
>
> float u,v;
> extern double quantum_frand();
>    u = 2 * quantum_frand() - 1;
>    v = 2 * quantum_frand() - 1;
>
> In this case, CallInst->getCalledFunction() returns a nullptr unusually, I
> printed out the getOperand(0) and found the operand is the whole thing of
>  "double (...)* bitcast (double ()* @quantum_frand to double (...)*)()".
> Any member function calling on that fails so i don't know whether there is
> an efficient way to exactly get the called function @quantum_frand(...)
> here?
>

What you have is a BitCastInst, so you'd have to downcast the Value* to
that, then look through the bitcast.

But realize you may never actually find a function in the end:

void func(void (*f)(void)) {
  f();
}

the f call is a call to a function pointer, so there is no statically
knowable called function here. Your code will probably need to handle the
case where this arises.

- David


> Thanks!
>
>
> Best regards,
>
> Shen
>
>
>
>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://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/20151112/cca8920b/attachment.html>


More information about the llvm-dev mailing list