MetallicPriest
MetallicPriest

Reputation: 30765

How to get FunctionType from CallInst when call is indirect in LLVM

If a function call is direct, you can get the Function type through the following code.

Function  * fun  = callInst->getCalledFunction();
Function  * funType = fun->getFunctionType();

However, if the call is indirect, that is, through a function pointer, the getCalledFunction returns NULL. So my question is how to get the Function type when a function is called through a function pointer.

Upvotes: 10

Views: 3429

Answers (2)

Hari
Hari

Reputation: 1803

Nice and informative answer by Oak.

As per recent versions of LLVM, getFunctionType of the CallBase class can be used.

Upvotes: 0

Oak
Oak

Reputation: 26868

To get the type from an indirect call, use getCalledValue instead of getCalledFunction, like so:

Type* t = callInst->getCalledValue()->getType();

That would get you the type of the pointer passed to the call instruction; to get the actual function type, continue with:

FunctionType* ft = cast<FunctionType>(cast<PointerType>(t)->getElementType());

Upvotes: 12

Related Questions