<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>If this particular API is too restrictive for your use case, you
can also consider the following two approaches:</p>
<ol>
<li>Walk the phi nodes in the header, and ask SCEV for the node
representing each. For any which is an AddRec within the
current loop, you've found an induction variable.</li>
<li>If you're using IVs to analyze loop behavior, consider using
the exit count APIs instead. They handle all the complexities
of multi exit loops which can be tedious to get right by hand.</li>
</ol>
<p>Philip</p>
<p>p.s. The point Florian made about rotated loops is also spot on.
<br>
</p>
<div class="moz-cite-prefix">On 10/23/2020 1:23 AM, ZIN MOON via
llvm-dev wrote:<br>
</div>
<blockquote type="cite"
cite="mid:CAFMxq3E2RmVUmGDLjiPufuroV39LG7BW_ct=1OpAX0NAeyKRJg@mail.gmail.com">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<div dir="ltr">
<div>Dear All</div>
<div><br>
</div>
<span lang="en"><span title="">I have a question about the
getInductionVariable API.</span><br>
<span title="">I was trying to get an induction variable
(indvar) in the code below using the getInductionVariable
API (LoopInfo class).</span></span>
<div><br>
</div>
<div><b>C file</b><br>
</div>
<div>void test(int val) {</div>
<div> int sum = 0, i = 0;<br>
</div>
<div> for (i = 0; i < val; i++) {</div>
<div> sum += i;<br>
</div>
<div> }<br>
</div>
<div>}</div>
<div><br>
</div>
<div><b>IR (with mem2reg pass)</b><br>
</div>
<div>; Function Attrs: nounwind<br>
define dso_local void @test(i32 %0) #0 {<br>
br label %2<br>
2: ; preds =
%6, %1<br>
%.01 = phi i32 [ 0, %1 ], [ %5, %6 ]<br>
%.0 = phi i32 [ 0, %1 ], [ %7, %6 ]<br>
%3 = icmp slt i32 %.0, %0<br>
br i1 %3, label %4, label %8 <- <span
style="color:rgb(204,204,204)"><span
style="background-color:rgb(0,0,255)"><b>Why don't check
this BranchInst ?</b></span></span><br>
4: ; preds = %2<br>
%5 = add nsw i32 %.01, %.0<br>
br label %6<br>
6: ; preds = %4<br>
%7 = add nsw i32 %.0, 1<br>
<b> br label %2 <- <span
style="background-color:rgb(255,0,0)">checked it whether
isCondition by getLatchCmpInst API(called by
getInductionVariable API)</span></b><br>
8: ; preds = %2<br>
ret void<br>
}</div>
<div><br>
</div>
Then I got null (no indvar) because BranchInst's number of
operands was less than 2.(red block).<br>
<br>
A CmpInst(compare with induction variable) is contained in the
header of the loop.<br>
But the getInductionVariable API checks only in a latch
block(contains a branch back to the header of the loop)<br>
is there a missing case that contains CmpInst in the header?<br>
<br>
And if I use the getCanonicalInductionVariable API, I can get
the variable 'i'(%. 01) as an indvar.<br>
<br>
I checked the code implementing getInductionVariable and
getCanonicalInductionVariable.<br>
It seems that indvar and canonical indvar differ only in the
starting number (0 or none).<br>
Why not use the same way to detect the indvar between
getInductionVariable and getCanonicalInductionVariable.<br>
<br>
BR<br>
ZIN<br>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
LLVM Developers mailing list
<a class="moz-txt-link-abbreviated" href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>
<a class="moz-txt-link-freetext" href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a>
</pre>
</blockquote>
</body>
</html>