<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hi,<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Oct 23, 2020, at 09:23, ZIN MOON via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">Dear All</div><div class=""><br class=""></div><span lang="en" class=""><span title="" class="">I have a question about the getInductionVariable API.</span><br class=""><span title="" class="">I was trying to get an induction variable (indvar) in the code below using the getInductionVariable API (LoopInfo class).</span></span><div class=""></div><div class=""><br class=""></div><div class=""><b class="">C file</b><br class=""></div><div class="">void test(int val) {</div><div class="">  int sum = 0, i = 0;<br class=""></div><div class="">  for (i = 0; i < val; i++) {</div><div class="">      sum += i;<br class=""></div><div class="">  }<br class=""></div><div class="">}</div><div class=""><br class=""></div><div class=""><b class="">IR (with mem2reg pass)</b><br class=""></div><div class="">; Function Attrs: nounwind<br class="">define dso_local void @test(i32 %0) #0 {<br class="">  br label %2<br class="">2:                                                ; preds = %6, %1<br class="">  %.01 = phi i32 [ 0, %1 ], [ %5, %6 ]<br class="">  %.0 = phi i32 [ 0, %1 ], [ %7, %6 ]<br class="">  %3 = icmp slt i32 %.0, %0<br class="">  br i1 %3, label %4, label %8 <- <span style="color:rgb(204,204,204)" class=""><span style="background-color:rgb(0,0,255)" class=""><b class="">Why don't check this BranchInst ?</b></span></span><br class="">4:                                                ; preds = %2<br class="">  %5 = add nsw i32 %.01, %.0<br class="">  br label %6<br class="">6:                                                ; preds = %4<br class="">  %7 = add nsw i32 %.0, 1<br class=""> <b class=""> br label %2  <- <span style="background-color:rgb(255,0,0)" class="">checked it whether isCondition by getLatchCmpInst API(called by getInductionVariable API)</span></b><br class="">8:                                                ; preds = %2<br class="">  ret void<br class="">}</div><div class=""><br class=""></div><div class=""></div>Then I got null (no indvar) because BranchInst's number of operands was less than 2.(red block).<br class=""><br class="">A CmpInst(compare with induction variable) is contained in the header of the loop.<br class="">But the getInductionVariable API checks only in a latch block(contains a branch back to the header of the loop)<br class="">is there a missing case that contains CmpInst in the header?<br class=""><br class="">And if I use the getCanonicalInductionVariable API, I can get the variable 'i'(%. 01) as an indvar.<br class=""><br class="">I checked the code implementing getInductionVariable and getCanonicalInductionVariable.<br class="">It seems that indvar and canonical indvar differ only in the starting number (0 or none).<br class="">Why not use the same way to detect the indvar between getInductionVariable and getCanonicalInductionVariable.<br class=""></div></div></blockquote></div><br class=""><div class=""><br class=""></div><div class="">Most loop transformations and utilities expect to work on ‘rotated’ loops (<a href="https://www.llvm.org/docs/LoopTerminology.html#rotated-loops" class="">https://www.llvm.org/docs/LoopTerminology.html#rotated-loops</a>). It is probably best to also run `-loop-rotate` before transforming analyzing/transforming the loop.</div><div class=""><br class=""></div><div class="">Note that the documentation for getInductionVariable explicitly requires the induction variable to determine the condition of the branch in the loop latch (<a href="http://llvm.org/doxygen/classllvm_1_1Loop.html#ab05e97728516fbeeaa9426496257c800" class="">http://llvm.org/doxygen/classllvm_1_1Loop.html#ab05e97728516fbeeaa9426496257c800</a>) and uses InductionDescriptor::isInductionPHI to detect induction PHIs (plus some other checks). There’s also isAuxiliaryInductionVariable, which does not have the latch branch requirement or you can use InductionDescriptor directly.</div><div class=""><br class=""></div><div class="">Cheers,</div><div class="">Florian</div></body></html>