[llvm-dev] Problem of array index manipulation collection of LLVM IR

Mehdi Amini via llvm-dev llvm-dev at lists.llvm.org
Thu Jul 21 15:38:32 PDT 2016

> On Jul 21, 2016, at 5:07 AM, Qingkun Meng via llvm-dev <llvm-dev at lists.llvm.org> wrote:
> Hi there,
> I am a newbie of llvm and here is my question situation. Assume that there is a function F which contains a loop named L, a array b[100]. I want to collect the statistical information of array index operation op(i) (take add and mul simply) of i in the loop L. Pseudocode lists below.
> void F(arg1, arg2){
>     int b[100];
>     for(int i=0; i<n; i++){
>         op1(i);
>         op2(i);
>         ......
>         b[op1(i)]=n1;
>         b[op2(i)]=n2;    // n1 and n2 are just common constants
> }
> }
> The code fragment is compiled to LLVM IR, I want to collect how many times are operations (like add and mul) put on i. However the operations are not easily obtained because there are many temp variables mix the variable trace. Does anyone have ideas to solve this or some open source project do this job?

In short: there is no reliable way in the absolute. The optimizer will make transformations that completely loses any relationship with the source-code. Also if you are interested about what gets actually *executed*, some of these computation will be folded in the addressing mode depending on the architecture.

Some people are doing these kind of analyses using debug info to map back to the source code, it may be enough if you don’t need precise results or results that are accurate with respect to the final optimized binary instruction stream.


More information about the llvm-dev mailing list