loop induction variables at IR level

Hi,

I was looking at trying to get loop induction variable at IR level. LLVM documentation mentioned indvars pass and getCanonicalInductionVariable() to get them, I tried running the indvars pass and then a custom pass which iterates through loops and uses the function to obtain variable for a simple loop program. But the API returns null. I also read in similar posts that the indvars pass is not available in the current version but documentation still mentions them.

Is there something I am missing in my method , or is there an alternative method to get the induction variable in the latest version.

Thanks in advance

Regards

Hi, Archa,

What do you want to do with the induction variable? For most uses cases, you can use the ScalarEvolution analysis (and, if necessary, the ScalarEvolutionExpander).

-Hal

Hi,

My understanding of induction variable is that it drives the loop with fixed increment and by comparing it based on a condition (Hope I understood it right). I want to extract and print this variable and what it is compared to.

I have been trying to understand the ScalarEvolution and SCEVexpander through llvm documentation are there any examples on how it is used, because I have not been able to understand its usage properly.

Thanks and Regards
Archa

Hi Archa,

My understanding of induction variable is that it drives the loop with fixed
increment and by comparing it based on a condition (Hope I understood it

It's a bit more general than that, but overall you have the right idea.

right). I want to extract and print this variable and what it is compared
to.

I have been trying to understand the ScalarEvolution and SCEVexpander

To only detect and analyze induction variables, using ScalarEvolution
is sufficient (you don't need SCEVExpander). You have to get hold of
a ScalarEvolution object (see the implementation of
IndVarSimplifyLegacyPass or IndVarSimplifyPass on how to do that) and
use the ScalarEvolution::getSCEV method to translate an llvm::Value*
to an llvm::SCEV*. If the llvm::SCEV* you get back is an
llvm::SCEVAddRecExpr* (you can check this by dyn_cast<> or isa<>) then
the llvm::Value* is an induction variable.

Thanks!
-- Sanjoy