I have the followed code for which I am writing a loop pass.

int main() {
int i = 0;
for (i=0; i<20; i++) {
printf (“hello world %d\n”, i);
return 0;

In the function runOnLoop, I have the following instruction
PHINode *indv = NULL;
indv = L->getCanonicalInductionVariable();

However, when I check indv is always set to NULL.
Since the code has a canonical induction variable, I was expecting the endv to start pointing to the induction var phi node.

Please let me what I’m missing here



What does your LLVM IR look like. Are you sure the loop hasnt been optimized away?


Also, make sure you're running the necessary transform passes as well,
I've run into this testing my analysis on unoptimized code.

There are various loop/induction variable passes, and offhand I think
things like -mem2reg help too.

Good luck!


Are you running IndVarSimplify prior to when you're asking for the
inductive variable? The comment for that method (and for other
inductive-like queries in LoopInfo), hints that you should run
IndVarSimplify (-indvars).

Forwarding to list :slight_smile: