<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jul 5, 2016, at 6:01 PM, Chawla, Pankaj 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 class="">Hi Sanjoy,<br class=""><br class="">The following trivial change in howManyLessThans() seems to resolve the problem with the original loop-<br class=""><br class="">   // Avoid negative or zero stride values<br class="">-  if (!isKnownPositive(Stride))<br class="">+  if (!NoWrap && !isKnownPositive(Stride))<br class="">     return getCouldNotCompute();<br class=""><br class="">However, I was experimenting with a few variants of the loop I posted and they seem to have different issues which may require more involved fixes. I am listing them here-<br class=""><br class="">1) Changing the loop control condition from '<' to '<='.<br class=""><br class="">The canonical form of this loop is something like this-<br class=""><br class="">if (0 < N) {<br class="">  i = 0;<br class="">  do {<br class="">    a[i]++;<br class="">    i += s; // NSW add<br class="">  } while (! (i > N));  // sgt compare<br class="">}<br class=""><br class="">The 'sgt' compare is inverted to 'sle' for analysis. ScalarEvolution isn't really expecting 'sle' in canonicalized loops so it reverts to brute force exit count computation using computeExitCountExhaustively() which doesn't work. This looks like a canonicalization issue.<br class=""><br class="">2) Variants with '>' and '>='. For example-<br class=""><br class="">  for(i=n; i>=0; i-=s) {<br class="">    A[i]++;<br class="">  }<br class=""><br class="">In this case the SCEV form of IV does not have 'nsw' flag- <br class="">{%n,+,(-1 * %s)}<%for.body><br class=""><br class=""><br class="">For now, I can submit a patch which fixes the issue with the original loop.  <br class="">Please let me know how to proceed.<br class=""></div></div></blockquote><div><br class=""></div><div>If you’re asking about how to submit a patch to LLVM, this may help: <a href="http://llvm.org/docs/DeveloperPolicy.html#making-and-submitting-a-patch" class="">http://llvm.org/docs/DeveloperPolicy.html#making-and-submitting-a-patch</a></div><div>(And possibly this: <a href="http://llvm.org/docs/Phabricator.html" class="">http://llvm.org/docs/Phabricator.html</a> )</div><div><br class=""></div><div>— </div><div>Mehdi</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class="">Thanks,<br class="">Pankaj<br class=""><br class="">-----Original Message-----<br class="">From: Chawla, Pankaj <br class="">Sent: Thursday, June 30, 2016 12:13 PM<br class="">To: 'Sanjoy Das'<br class="">Cc: <a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a><br class="">Subject: RE: [llvm-dev] Regarding ScalarEvolution's loop backedge computation<br class=""><br class="">Hi Sanjoy,<br class=""><br class="">Thank you for the clarification!<br class="">I will give it a try and put up the changes for review.<br class=""><br class="">-Pankaj<br class=""><br class="">-----Original Message-----<br class="">From: Sanjoy Das [<a href="mailto:sanjoy@playingwithpointers.com" class="">mailto:sanjoy@playingwithpointers.com</a>] <br class="">Sent: Wednesday, June 29, 2016 5:02 PM<br class="">To: Chawla, Pankaj<br class="">Cc: <a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a><br class="">Subject: Re: [llvm-dev] Regarding ScalarEvolution's loop backedge computation<br class=""><br class="">Hi Pankaj,<br class=""><br class="">Chawla, Pankaj via llvm-dev wrote:<br class=""><blockquote type="cite" class="">It looks like ScalarEvolution bails out of loop backedge computation if  > it cannot prove the IV stride as either positive or negative (based on  > loop control condition). I think this logic can be refined for signed IVs.<br class=""><br class="">Consider this simple loop-<br class=""><br class="">void foo(int *A, int n, int s) {<br class=""><br class="">int i;<br class=""><br class="">for(i=0; i<n; i += s) {<br class=""><br class="">A[i]++;<br class=""><br class="">}<br class=""><br class="">}<br class=""><br class="">The IV of this loop has this SCEV form-  >  > {0,+,%s}<nsw><%for.body><br class=""></blockquote><br class="">This looks valid -- we already do things like this for<br class=""><br class="">for (i = A; i != B; i += 5)<br class="">   ...<br class=""><br class="">and compute the backedge taken count as "(B - A) / 5" (roughly :) ) since if (B - A) is not divisible by 5 then we have UB due to overflow.  We just have to be careful around cases like:<br class=""><br class="">   for(i = 0; i < 60; i += s) {<br class="">     may_exit();<br class="">   }<br class=""><br class="">"s" can be (say) -3 and the loop can take 160 backedges and then "exit(0)", avoiding the undefined behavior due to underflow.  "s" can also be zero, in which case the loop can potentially take an infinite number of backedges.<br class=""><br class="">However, in the example you gave (written in LLVM's canonical rotated<br class="">form):<br class=""><br class="">   if (0 < N) {<br class="">     i = 0;<br class="">     do {<br class="">       a[i]++;<br class="">       i += s; // NSW add<br class="">     } while (i < N);<br class="">   }<br class=""><br class="">For any s <= 0 we have undefined behavior, so it is sound to assume s  > 0.<br class=""><br class="">Do you want to take a crack at fixing this?  I'm traveling till the 10th of July, but I can review your change once I'm back.<br class=""><br class="">-- Sanjoy<br class="">_______________________________________________<br class="">LLVM Developers mailing list<br class=""><a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a><br class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev<br class=""></div></div></blockquote></div><br class=""></body></html>