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

John Criswell via llvm-dev llvm-dev at lists.llvm.org
Fri Nov 13 05:44:59 PST 2015


On 11/12/15 10:27 PM, Shen Liu via llvm-dev 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 (...)*)()

As others have noted, Function::getCalledFunction() is returning null 
because the function that is being called is first casted.  For some 
reason, Function::getCalledFunction() does not strip off pointer casts 
on its first argument.

However, you can do that easily.  Just use:

Function * calledFunc = 
dyn_cast<Function>(CallInst->getCalledValue()->stripPointerCasts())

If calledFunc is non-zero, it's a pointer to the called function. 
Otherwise, you have an indirect call.

As an aside, note that indirect calls are sometimes the result of a 
select instruction that picks between one of two functions.  It's 
therefore worth checking to see if the called value is a SelectInst and, 
if it is, see if both operands to the SelectInst are Functions.

Regards,

John Criswell

>
> 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? Thanks!
>
>
> Best regards,
>
> Shen
>
>
>
>
>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev


-- 
John Criswell
Assistant Professor
Department of Computer Science, University of Rochester
http://www.cs.rochester.edu/u/criswell

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20151113/4f0688e3/attachment.html>


More information about the llvm-dev mailing list