Identifying loop variables

Hi everyone,
Are there any functions to identify the initiation,condition and iteration part of the loop?
Example: for(i=0;i<10;i++)
{
a[i]+=1;
}
I want to know the name of the variable used in the loop(i) , the initiation(i=0), condition(i<10) and the iteration(i++) part of the loop.
Thank you:)

Perhaps it's easier to use clang to achieve your goal?

Regards,
chenwj

Hi Adarsh,

Hi Adarsh,

the main question is where to do this analysis. You have two options

1. Do it in clang

As pointed out by chenwj, you can do this in clang. This will basically be a pattern matching approach or, if more advanced, some source code based induction variable recognition.

Working on clang has the advantage that your results directly relate to the C code. However, performing advanced induction variable analysis on C code is more difficult. I am not sure, if there already exists an induction variable analysis.

2. Do it in LLVM

Here you would work on LLVM-IR and use the LoopInfo and the ScalarEvolution analysis. These analysis perform advanced induction variable recognition and will give you good results for a wider range of code (including while loops, goto, pointer arithmetic). However, it will be hard to relate the results to the original C code. You can try to use debugging information, but I doubt this will work in all cases.

Cheers
Tobi