Finding call points to a function

Hi,

I am writing an LLVM Pass and I would like to know how would I do the following:

  1. I have a function foo, and I need to get all the call points calling this function in each Function of the Module. The only way I can think of is to iterate over the BasicBlocks of each Function, look for all the Call Instructions and check if the called function is foo. I was wondering if there is any other better way to do this.

  2. Is it possible to distinguish a class method from a non class method while analyzing the emitted LLVM code for a C++ program? Because, I need to interpret the calls made to foo differently in a class method and in a non class method.

Thanks,
Rakesh

Rakesh Komuravelli wrote:

Hi,

I am writing an LLVM Pass and I would like to know how would I do the following:

1. I have a function foo, and I need to get all the call points calling this function in each Function of the Module. The only way I can think of is to iterate over the BasicBlocks of each Function, look for all the Call Instructions and check if the called function is foo. I was wondering if there is any other better way to do this.

Write a ModulePass and use CallGraph analysis in that.

Rakesh Komuravelli wrote:

Hi,

I am writing an LLVM Pass and I would like to know how would I do the following:

1. I have a function foo, and I need to get all the call points calling this function in each Function of the Module. The only way I can think of is to iterate over the BasicBlocks of each Function, look for all the Call Instructions and check if the called function is foo. I was wondering if there is any other better way to do this.

Iterate over @foo's use list. http://wiki.llvm.org/HowTo:_Find_all_call_sites_of_a_function

2. Is it possible to distinguish a class method from a non class method while analyzing the emitted LLVM code for a C++ program? Because, I need to interpret the calls made to foo differently in a class method and in a non class method.

No. Methods turn into functions that happen to take the class type pointer as first argument.

Nick

Nick Lewycky wrote:

Rakesh Komuravelli wrote:
  

Hi,

I am writing an LLVM Pass and I would like to know how would I do the
following:

1. I have a function foo, and I need to get all the call points calling
this function in each Function of the Module. The only way I can think
of is to iterate over the BasicBlocks of each Function, look for all the
Call Instructions and check if the called function is foo. I was
wondering if there is any other better way to do this.
    
Iterate over @foo's use list.
http://wiki.llvm.org/HowTo:_Find_all_call_sites_of_a_function
  

Note that you can have indirect calls to function foo. If foo is marked
with internal linkage, then the only way it can be used indirectly is if
it has a use that is either:

a) Not a call instruction
b) Is a call instruction, but foo is passed as an argument to the function

If foo is marked with external linkage, then, in theory, external
libraries linked into the program after code generation can call foo.

If you need to deal with indirect function calls, there are some
options. First, I believe the CallGraph analysis pass provides correct
(but very conservative) information on which functions are reachable by
which indirect call instructions. If you need more accurate results,
then you can use the DSA pointer analysis code in the poolalloc project.

2. Is it possible to distinguish a class method from a non class method
while analyzing the emitted LLVM code for a C++ program? Because, I need
to interpret the calls made to foo differently in a class method and in
a non class method.
    
No. Methods turn into functions that happen to take the class type
pointer as first argument.
  

There might be a hack'ish way to do this. The LLVM function name is (or
is close to) the mangled name of the original C++ function. If you're
unable to demangle the name, you should be able to tell whether the
function is a class method or a regular function.

-- John T.