<div dir="ltr"><div dir="ltr"><div dir="ltr">Hi Congzhe,<div><br></div><div>Could you clarify the first question? Whose correctness is in question? backedgeTakenCount() is supposed to always be correct of course. That is different from whether it can always compute the backedge taken count.</div><div>Maybe not and it'll tell you.</div><div><br></div><div>For the second question: There's no special distinction about induction variables. The same logic is for them or other variables and the basic question is "Does your value change because of the loop?"<br></div><div>Now, if you want more resources on understanding the general concepts of scalar evolution, then I recommend these two:</div><div>1) <a href="http://users.uoa.gr/~sdi1600105/compilers/introduction-to-scalar-evolution.html">http://users.uoa.gr/~sdi1600105/compilers/introduction-to-scalar-evolution.html</a></div><div>2) <a href="https://www.youtube.com/watch?v=AmjliNp0_00">https://www.youtube.com/watch?v=AmjliNp0_00</a></div><div><br></div><div>Disclaimer: The first resource is an article of mine. The reason I recommend it is because I created it because I think it is a very quick and very intuitive introduction, that uses a different intuition than I usually see in tutorials (although this intuition is the one it seems that almost all experienced devs use in their minds).</div><div>That said, I would definitely recommend watching 2) either you read 1) or not.</div><div><br></div><div>Best,</div><div>Stefanos</div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Στις Τετ, 3 Φεβ 2021 στις 10:16 μ.μ., ο/η congzhe cao via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> έγραψε:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hi Michael,</div><div><br></div>Thanks for the reply! As you've seen, my purpose is indeed to use getSCEVAtScope(V, L) with V being an instruction/value in the inner loop and L being the outer loop. <div><br></div><div>- I can somewhat see that if V is the induction variable for the inner loop, then getSCEVAtScope(V, L) tries to derive the backedgeTakenCount of the inner loop. Is it always correct?</div><div><br></div><div>- I'm also wondering if V is not the induction variable for the inner loop but some other value inside the inner loop (so this is a more general situation), what would be the expected behavior of getSCEVAtScope(V, L)? It would be ideal if you could let me know the logic in SCEV regarding this situation.</div><div><br></div><div>Thanks again,</div><div>Congzhe</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Feb 3, 2021 at 12:54 PM Michael Kruse <<a href="mailto:llvmdev@meinersbur.de" target="_blank">llvmdev@meinersbur.de</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">%j.018 is variant in the innermost loop (for.body4), but after<br>
existing that loop, it will have the value before leaving the loop. If<br>
you intent to use %j.018 in the innermost loop, you need to call<br>
getSCEVAtScope() with the innermost loop as the scope. getSCEVAtScope<br>
with a scope outside the loop (or NULL) will try to derive the exit<br>
value.<br>
<br>
Michael<br>
<br>
<br>
<br>
Am Mi., 3. Feb. 2021 um 11:11 Uhr schrieb congzhe cao via llvm-dev<br>
<<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>>:<br>
><br>
> Dear all,<br>
><br>
><br>
><br>
> For the following IR which is essentially a doubly nested loop, if we get the SCEV expression for the inner loop induction variable, i.e., %j.018, at the scope of the outer loop using getSCEVAtScope(), the result is: 9. That is a constant, or loop-invariant.<br>
><br>
><br>
> However, %j.018 does keep changing within the scope of the outer loop since it is the induction variable of the inner loop, so it is not straightforward to me why %j.018 is considered a loop-invariant. Something like “{0,+,1}<nuw><nsw><%for.body4>” would make more sense to me.<br>
><br>
><br>
><br>
> I’m wondering if I can get any comments on that?<br>
><br>
><br>
><br>
> Best regards,<br>
><br>
> Congzhe<br>
><br>
><br>
><br>
> ***************************************************************<br>
><br>
> define dso_local i32 @main() {<br>
><br>
> entry:<br>
><br>
>   br label %for.cond1.preheader<br>
><br>
><br>
><br>
> for.cond1.preheader:                              ; preds = %for.cond.cleanup3, %entry<br>
><br>
>   %i.020 = phi i32 [ 0, %entry ], [ %inc8, %for.cond.cleanup3 ]<br>
><br>
>   %x.019 = phi i32 [ 17, %entry ], [ %add, %for.cond.cleanup3 ]<br>
><br>
>   br label %for.body4<br>
><br>
><br>
><br>
> for.cond.cleanup:                                 ; preds = %for.cond.cleanup3<br>
><br>
>   ret i32 %add<br>
><br>
><br>
><br>
> for.cond.cleanup3:                                ; preds = %for.body4<br>
><br>
>   %inc8 = add nuw nsw i32 %i.020, 1<br>
><br>
>   %exitcond21 = icmp eq i32 %inc8, 10<br>
><br>
>   br i1 %exitcond21, label %for.cond.cleanup, label %for.cond1.preheader<br>
><br>
><br>
><br>
> for.body4:                                        ; preds = %for.body4, %for.cond1.preheader<br>
><br>
>   %j.018 = phi i64 [ 0, %for.cond1.preheader ], [ %inc, %for.body4 ]<br>
><br>
>   %x.117 = phi i32 [ %x.019, %for.cond1.preheader ], [ %add, %for.body4 ]<br>
><br>
>   %cmp5 = icmp eq i64 %j.018, 9<br>
><br>
>   %conv = zext i1 %cmp5 to i32<br>
><br>
>   %add = add nsw i32 %x.117, %conv<br>
><br>
>   call void @_Z3foov()<br>
><br>
>   %inc = add nuw nsw i64 %j.018, 1<br>
><br>
>   %exitcond = icmp eq i64 %inc, 10<br>
><br>
>   br i1 %exitcond, label %for.cond.cleanup3, label %for.body4<br>
><br>
> }<br>
><br>
><br>
><br>
> declare dso_local void @_Z3foov() local_unnamed_addr #1<br>
><br>
> _______________________________________________<br>
> LLVM Developers mailing list<br>
> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>