[LLVMdev] How to get the total number of variables in a funciton?

John Criswell criswell at uiuc.edu
Mon Dec 15 07:53:19 PST 2008


Mingxing Tan wrote:
>
> Hi, all;
>
> How can I get the number of variables in a function/module (both the 
> number of variables in original source program and the number of 
> temporal variables for ssa-form)?
>
Do you want the SSA values *created* by a function, those used by a 
function, or both?

For SSA values, simply iterate over all of the instructions within the 
function and count those that generate SSA values.  Most instructions 
(like add, sub, cast) do, but a few (like store or call when calling a 
function returning void) do not.  If you want all SSA variables, you 
will also need to count the number of operands to each instruction (even 
those like store that do not create new SSA values).

If you're working on a whole module, you will also need to count he SSA 
values for all globals and functions.

Counting original source program variables is more difficult.  One thing 
you could try is to compile the program with debug information and use 
the information in the LLVM debugging intrinsics to find the original 
source program variables.  You might want to try doing this before the 
mem2reg pass is run (mem2reg will take stack allocated variables and 
change them to SSA form; this may make correlation between the LLVM 
level and source level a bit more tricky).

Another solution to finding the number of source-level variables is to 
use clang instead of llvm-gcc.  I believe you can, with relative ease, 
modify clang to record the number of source-level variables.
>
> I’m dealing with some dataflow analysis problem and need the 
> information of variables. Appreciate any help.
>
Just out of curiosity, what dataflow analysis problem are you trying to 
solve?

-- John T.

>  
>
> Thanks.
>
>  
>
> Star.Tan
>
>  
>




More information about the llvm-dev mailing list