regarding function pointer pass

Hi,
I am a Mtech student at IIT,Bombay and presently studying llvm as a part
of my Seminar work.Currently,i am writing a simple pass that would detect
function pointer call,use and declarations in the src program.
I could detect function pointer calls using the getCalledFunction().

However,i am stuck in identifying function pointer declaration and use.
is there a way one can distinguish between data and function pointers
in llvm?

I tried the following,
    I->getType()->getElementType()->getTypeID()

but this always returns me 12 i.e pointer type id (for pointers)
irrespective if the pointer is a data or a function pointer.

Kindly let me know where i am going wrong.

Regards,
Netra
Mtech,CSE,IIT BOMBAY

Hi,
I am a Mtech student at IIT,Bombay and presently studying llvm as a part
of my Seminar work.Currently,i am writing a simple pass that would detect
function pointer call,use and declarations in the src program.
I could detect function pointer calls using the getCalledFunction().

However,i am stuck in identifying function pointer declaration and use.
is there a way one can distinguish between data and function pointers
in llvm?

I tried the following,
     I->getType()->getElementType()->getTypeID()

but this always returns me 12 i.e pointer type id (for pointers)
irrespective if the pointer is a data or a function pointer.

What you want to do is first determine if the value is of pointer type and then, if it is, determine if it is pointing to a function.

You can use dyn_cast<Type> and isa<Type> to determine if a particular type is of the class PointerType. You can then use PointerType's getElementType() method to determine if it is pointing to a function type.

To get you started, here's how to determine if Type * Ty is a Pointer type:

Type * Ty = I->getType();
if (PointerType * PT = dyn_cast<PointerType>(Ty)) {
     ...
}

Inside the block, you can now use PT's getElementType() method to see what the pointer is pointing to.

You should read up on dyn_cast<> and isa<> in the Programmer's Manual and use the doxygen information on the web site to understand the class hierarchy of llvm::Type and its subclasses.

One final note: the LLVM type system is not type safe in that it allows arbitrary type casts like C does. For example, I can take a function pointer, cast it to an int, store it in memory, and then have some other part of the program load the integer from memory, cast it back to a function pointer, and use it. The fact that an LLVM value has an integer type doesn't mean that it's holding an integer.

For some applications, this is not a problem. For others, it can be, and you'll need an analysis like DSA to give you more accurate (but more conservative) information.

-- John T.

Hello

I tried the following,
I->getType()->getElementType()->getTypeID()

but this always returns me 12 i.e pointer type id (for pointers)
irrespective if the pointer is a data or a function pointer.

This is correct. Both Function and GlobalVariable are the instances of
GlobalValue, which type is basically a pointer type (function and
global variables *are* pointers to the data / code). So, given the
Value you'd just need to figure out whether this is GlobalVariable or
Function.

http://llvm.org/docs/ProgrammersManual.html#isa will help.