4
votes

I am trying to obtain the canonical induction variable for a loop in a loop pass, given its reference L*, using L->getCanonicalInductionVariable().

But many of the loops I encounter are not in canonical form. For ex:

for (int i = 10; i < 20 ; i++) {
    ....
}

According to llvm documentation http://llvm.org/docs/Passes.html#indvars-canonicalize-induction-variables) , using the "indvars" pass in "opt" should do the trick by converting the loops induction variable into canonical form. I have tried running:

opt -mem2reg -indvars < test.bc > optTest.bc

where "test.bc" is a bit code format of the for-loop above. But the indvars pass seems to have no effect ( I have omitted the assembly code here, but I have checked it).

Later, using L->getCanonicalInductionVariable() returns null. I have also tried using "indvars" with other passes like "loops", "loop-simplify" but to no avail.

Any idea on how to get this to work?

1

1 Answers

4
votes

If you are using an LLVM version earlier than r153260, you can enable induction variable rewrite by adding the -enable-iv-rewrite command-line option, and it should generate the result you expect to see.

This option has been deprecated since 3.0 and altogether removed after the above-mentioned version; and without it, the pass does not behave according to its documentation. If you want to do it now, then, I think you have to add a pass to implement it yourself - though you can of course use the old (removed) code as reference. I believe that it was removed because (1) more passes were changed to be able to handle loops without a canonical induction variable and (2) the canonization led to other performance impacts; so I guess it would be wise to think twice before adding a similar pass.