<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
        {font-family:Verdana;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.MsoPlainText, li.MsoPlainText, div.MsoPlainText
        {mso-style-priority:99;
        mso-style-link:"Plain Text Char";
        margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
        {mso-style-priority:99;
        mso-style-link:"Balloon Text Char";
        margin:0in;
        margin-bottom:.0001pt;
        font-size:8.0pt;
        font-family:"Tahoma","sans-serif";}
span.EmailStyle17
        {mso-style-type:personal;
        font-family:"Verdana","sans-serif";
        color:windowtext;
        font-weight:normal;
        font-style:normal;
        text-decoration:none none;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
span.BalloonTextChar
        {mso-style-name:"Balloon Text Char";
        mso-style-priority:99;
        mso-style-link:"Balloon Text";
        font-family:"Tahoma","sans-serif";}
span.PlainTextChar
        {mso-style-name:"Plain Text Char";
        mso-style-priority:99;
        mso-style-link:"Plain Text";
        font-family:"Calibri","sans-serif";}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Hi David,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><i><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">>></span> A loop which stores to a global variable has no side effects?<o:p></o:p></i></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><a name="_MailEndCompose"></a><span style="font-size:10.0pt;font-family:"Verdana","sans-serif";color:#1F497D">This seems to be an oversight.
<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Verdana","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Verdana","sans-serif";color:#1F497D">How about we check that the pointer operand of the store is not a GLobalValue?<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Verdana","sans-serif";color:#1F497D">Does this seem reasonable?<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Verdana","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Verdana","sans-serif";color:#1F497D">Thanks,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Verdana","sans-serif";color:#1F497D">Pankaj<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><a name="_____replyseparator"></a><b><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"">From:</span></b><span style="font-size:11.0pt;font-family:"Calibri","sans-serif""> David Majnemer [<a href="mailto:david.majnemer@gmail.com">mailto:david.majnemer@gmail.com</a>]
<br>
<b>Sent:</b> Friday, September 16, 2016 6:58 PM<br>
<b>To:</b> Kreitzer, David L <<a href="mailto:david.l.kreitzer@intel.com">david.l.kreitzer@intel.com</a>><br>
<b>Cc:</b> llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>><br>
<b>Subject:</b> Re: [llvm] r281732 - Reapplying r278731 after fixing the problem that caused it to be reverted.<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">On Fri, Sep 16, 2016 at 7:38 AM, David L Kreitzer via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>> wrote:<o:p></o:p></p>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-top:5.0pt;margin-right:0in;margin-bottom:5.0pt">
<p class="MsoNormal">Author: dlkreitz<br>
Date: Fri Sep 16 09:38:13 2016<br>
New Revision: 281732<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=281732&view=rev" target="_blank">
http://llvm.org/viewvc/llvm-project?rev=281732&view=rev</a><br>
Log:<br>
Reapplying r278731 after fixing the problem that caused it to be reverted.<br>
<br>
Enhance SCEV to compute the trip count for some loops with unknown stride.<br>
<br>
Patch by Pankaj Chawla<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D22377" target="_blank">
https://reviews.llvm.org/D22377</a><br>
<br>
Added:<br>
    llvm/trunk/test/Analysis/ScalarEvolution/trip-count-unknown-stride.ll<br>
Modified:<br>
    llvm/trunk/include/llvm/Analysis/ScalarEvolution.h<br>
    llvm/trunk/lib/Analysis/ScalarEvolution.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/Analysis/ScalarEvolution.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/ScalarEvolution.h?rev=281732&r1=281731&r2=281732&view=diff" target="_blank">
http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/ScalarEvolution.h?rev=281732&r1=281731&r2=281732&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Analysis/ScalarEvolution.h (original)<br>
+++ llvm/trunk/include/llvm/Analysis/ScalarEvolution.h Fri Sep 16 09:38:13 2016<br>
@@ -803,6 +803,13 @@ namespace llvm {<br>
     /// Cache for \c loopHasNoAbnormalExits.<br>
     DenseMap<const Loop *, bool> LoopHasNoAbnormalExits;<br>
<br>
+    /// Cache for \c loopHasNoSideEffects.<br>
+    DenseMap<const Loop *, bool> LoopHasNoSideEffects;<br>
+<br>
+    /// Returns true if \p L contains no instruction that can have side effects<br>
+    /// (i.e. via throwing an exception, volatile or atomic access).<br>
+    bool loopHasNoSideEffects(const Loop *L);<br>
+<br>
     /// Returns true if \p L contains no instruction that can abnormally exit<br>
     /// the loop (i.e. via throwing an exception, by terminating the thread<br>
     /// cleanly or by infinite looping in a called function).  Strictly<br>
<br>
Modified: llvm/trunk/lib/Analysis/ScalarEvolution.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ScalarEvolution.cpp?rev=281732&r1=281731&r2=281732&view=diff" target="_blank">
http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ScalarEvolution.cpp?rev=281732&r1=281731&r2=281732&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Analysis/ScalarEvolution.cpp (original)<br>
+++ llvm/trunk/lib/Analysis/ScalarEvolution.cpp Fri Sep 16 09:38:13 2016<br>
@@ -4953,6 +4953,28 @@ bool ScalarEvolution::isAddRecNeverPoiso<br>
   return LatchControlDependentOnPoison && loopHasNoAbnormalExits(L);<br>
 }<br>
<br>
+bool ScalarEvolution::loopHasNoSideEffects(const Loop *L) {<br>
+  auto Itr = LoopHasNoSideEffects.find(L);<br>
+  if (Itr == LoopHasNoSideEffects.end()) {<br>
+    auto NoSideEffectsInBB = [&](BasicBlock *BB) {<br>
+      return all_of(*BB, [](Instruction &I) {<br>
+        // Non-atomic, non-volatile stores are ok.<br>
+        if (auto *SI = dyn_cast<StoreInst>(&I))<br>
+          return SI->isSimple();<span style="color:#1F497D"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
</blockquote>
<div>
<p class="MsoPlainText">A loop which stores to a global variable has no side effects?<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-top:5.0pt;margin-right:0in;margin-bottom:5.0pt">
<p class="MsoNormal">+<br>
+        return !I.mayHaveSideEffects();<br>
+      });<br>
+    };<br>
+<br>
+    auto InsertPair = LoopHasNoSideEffects.insert(<br>
+        {L, all_of(L->getBlocks(), NoSideEffectsInBB)});<br>
+    assert(InsertPair.second && "We just checked!");<br>
+    Itr = InsertPair.first;<br>
+  }<br>
+<br>
+  return Itr->second;<br>
+}<br>
+<br>
 bool ScalarEvolution::loopHasNoAbnormalExits(const Loop *L) {<br>
   auto Itr = LoopHasNoAbnormalExits.find(L);<br>
   if (Itr == LoopHasNoAbnormalExits.end()) {<br>
@@ -5540,6 +5562,7 @@ void ScalarEvolution::forgetLoop(const L<br>
     forgetLoop(I);<br>
<br>
   LoopHasNoAbnormalExits.erase(L);<br>
+  LoopHasNoSideEffects.erase(L);<br>
 }<br>
<br>
 void ScalarEvolution::forgetValue(Value *V) {<br>
@@ -8614,6 +8637,8 @@ bool ScalarEvolution::isImpliedCondOpera<br>
<br>
 bool ScalarEvolution::doesIVOverflowOnLT(const SCEV *RHS, const SCEV *Stride,<br>
                                          bool IsSigned, bool NoWrap) {<br>
+  assert(isKnownPositive(Stride) && "Positive stride expected!");<br>
+<br>
   if (NoWrap) return false;<br>
<br>
   unsigned BitWidth = getTypeSizeInBits(RHS->getType());<br>
@@ -8682,11 +8707,15 @@ ScalarEvolution::howManyLessThans(const<br>
     return getCouldNotCompute();<br>
<br>
   const SCEVAddRecExpr *IV = dyn_cast<SCEVAddRecExpr>(LHS);<br>
-  if (!IV && AllowPredicates)<br>
+  bool PredicatedIV = false;<br>
+<br>
+  if (!IV && AllowPredicates) {<br>
     // Try to make this an AddRec using runtime tests, in the first X<br>
     // iterations of this loop, where X is the SCEV expression found by the<br>
     // algorithm below.<br>
     IV = convertSCEVToAddRecWithPredicates(LHS, L, P);<br>
+    PredicatedIV = true;<br>
+  }<br>
<br>
   // Avoid weird loops<br>
   if (!IV || IV->getLoop() != L || !IV->isAffine())<br>
@@ -8697,15 +8726,62 @@ ScalarEvolution::howManyLessThans(const<br>
<br>
   const SCEV *Stride = IV->getStepRecurrence(*this);<br>
<br>
-  // Avoid negative or zero stride values<br>
-  if (!isKnownPositive(Stride))<br>
-    return getCouldNotCompute();<br>
+  bool PositiveStride = isKnownPositive(Stride);<br>
<br>
-  // Avoid proven overflow cases: this will ensure that the backedge taken count<br>
-  // will not generate any unsigned overflow. Relaxed no-overflow conditions<br>
-  // exploit NoWrapFlags, allowing to optimize in presence of undefined<br>
-  // behaviors like the case of C language.<br>
-  if (!Stride->isOne() && doesIVOverflowOnLT(RHS, Stride, IsSigned, NoWrap))<br>
+  // Avoid negative or zero stride values.<br>
+  if (!PositiveStride) {<br>
+    // We can compute the correct backedge taken count for loops with unknown<br>
+    // strides if we can prove that the loop is not an infinite loop with side<br>
+    // effects. Here's the loop structure we are trying to handle -<br>
+    //<br>
+    // i = start<br>
+    // do {<br>
+    //   A[i] = i;<br>
+    //   i += s;<br>
+    // } while (i < end);<br>
+    //<br>
+    // The backedge taken count for such loops is evaluated as -<br>
+    // (max(end, start + stride) - start - 1) /u stride<br>
+    //<br>
+    // The additional preconditions that we need to check to prove correctness<br>
+    // of the above formula is as follows -<br>
+    //<br>
+    // a) IV is either nuw or nsw depending upon signedness (indicated by the<br>
+    //    NoWrap flag).<br>
+    // b) loop is single exit with no side effects.<br>
+    //<br>
+    //<br>
+    // Precondition a) implies that if the stride is negative, this is a single<br>
+    // trip loop. The backedge taken count formula reduces to zero in this case.<br>
+    //<br>
+    // Precondition b) implies that the unknown stride cannot be zero otherwise<br>
+    // we have UB.<br>
+    //<br>
+    // The positive stride case is the same as isKnownPositive(Stride) returning<br>
+    // true (original behavior of the function).<br>
+    //<br>
+    // We want to make sure that the stride is truly unknown as there are edge<br>
+    // cases where ScalarEvolution propagates no wrap flags to the<br>
+    // post-increment/decrement IV even though the increment/decrement operation<br>
+    // itself is wrapping. The computed backedge taken count may be wrong in<br>
+    // such cases. This is prevented by checking that the stride is not known to<br>
+    // be either positive or non-positive. For example, no wrap flags are<br>
+    // propagated to the post-increment IV of this loop with a trip count of 2 -<br>
+    //<br>
+    // unsigned char i;<br>
+    // for(i=127; i<128; i+=129)<br>
+    //   A[i] = i;<br>
+    //<br>
+    if (PredicatedIV || !NoWrap || isKnownNonPositive(Stride) ||<br>
+        !loopHasNoSideEffects(L))<br>
+      return getCouldNotCompute();<br>
+<br>
+  } else if (!Stride->isOne() &&<br>
+             doesIVOverflowOnLT(RHS, Stride, IsSigned, NoWrap))<br>
+    // Avoid proven overflow cases: this will ensure that the backedge taken<br>
+    // count will not generate any unsigned overflow. Relaxed no-overflow<br>
+    // conditions exploit NoWrapFlags, allowing to optimize in presence of<br>
+    // undefined behaviors like the case of C language.<br>
     return getCouldNotCompute();<br>
<br>
   ICmpInst::Predicate Cond = IsSigned ? ICmpInst::ICMP_SLT<br>
@@ -8720,12 +8796,21 @@ ScalarEvolution::howManyLessThans(const<br>
   APInt MinStart = IsSigned ? getSignedRange(Start).getSignedMin()<br>
                             : getUnsignedRange(Start).getUnsignedMin();<br>
<br>
-  APInt MinStride = IsSigned ? getSignedRange(Stride).getSignedMin()<br>
-                             : getUnsignedRange(Stride).getUnsignedMin();<br>
-<br>
   unsigned BitWidth = getTypeSizeInBits(LHS->getType());<br>
-  APInt Limit = IsSigned ? APInt::getSignedMaxValue(BitWidth) - (MinStride - 1)<br>
-                         : APInt::getMaxValue(BitWidth) - (MinStride - 1);<br>
+<br>
+  APInt StrideForMaxBECount;<br>
+<br>
+  if (PositiveStride)<br>
+    StrideForMaxBECount = IsSigned ? getSignedRange(Stride).getSignedMin()<br>
+                                   : getUnsignedRange(Stride).getUnsignedMin();<br>
+  else<br>
+    // Using a stride of 1 is safe when computing max backedge taken count for<br>
+    // a loop with unknown stride.<br>
+    StrideForMaxBECount = APInt(BitWidth, 1, IsSigned);<br>
+<br>
+  APInt Limit =<br>
+      IsSigned ? APInt::getSignedMaxValue(BitWidth) - (StrideForMaxBECount - 1)<br>
+               : APInt::getMaxValue(BitWidth) - (StrideForMaxBECount - 1);<br>
<br>
   // Although End can be a MAX expression we estimate MaxEnd considering only<br>
   // the case End = RHS. This is safe because in the other case (End - Start)<br>
@@ -8739,7 +8824,7 @@ ScalarEvolution::howManyLessThans(const<br>
     MaxBECount = BECount;<br>
   else<br>
     MaxBECount = computeBECount(getConstant(MaxEnd - MinStart),<br>
-                                getConstant(MinStride), false);<br>
+                                getConstant(StrideForMaxBECount), false);<br>
<br>
   if (isa<SCEVCouldNotCompute>(MaxBECount))<br>
     MaxBECount = BECount;<br>
<br>
Added: llvm/trunk/test/Analysis/ScalarEvolution/trip-count-unknown-stride.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/ScalarEvolution/trip-count-unknown-stride.ll?rev=281732&view=auto" target="_blank">
http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/ScalarEvolution/trip-count-unknown-stride.ll?rev=281732&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Analysis/ScalarEvolution/trip-count-unknown-stride.ll (added)<br>
+++ llvm/trunk/test/Analysis/ScalarEvolution/trip-count-unknown-stride.ll Fri Sep 16 09:38:13 2016<br>
@@ -0,0 +1,62 @@<br>
+; RUN: opt < %s -analyze -scalar-evolution | FileCheck %s<br>
+<br>
+; ScalarEvolution should be able to compute trip count of the loop by proving<br>
+; that this is not an infinite loop with side effects.<br>
+<br>
+; CHECK: Determining loop execution counts for: @foo1<br>
+; CHECK: backedge-taken count is ((-1 + %n) /u %s)<br>
+<br>
+; We should have a conservative estimate for the max backedge taken count for<br>
+; loops with unknown stride.<br>
+; CHECK: max backedge-taken count is -1<br>
+<br>
+target datalayout = "e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128"<br>
+<br>
+; Function Attrs: norecurse nounwind<br>
+define void @foo1(i32* nocapture %A, i32 %n, i32 %s) #0 {<br>
+entry:<br>
+  %cmp4 = icmp sgt i32 %n, 0<br>
+  br i1 %cmp4, label %for.body, label %for.end<br>
+<br>
+for.body:                                         ; preds = %entry, %for.body<br>
+  %i.05 = phi i32 [ %add, %for.body ], [ 0, %entry ]<br>
+  %arrayidx = getelementptr inbounds i32, i32* %A, i32 %i.05<br>
+  %0 = load i32, i32* %arrayidx, align 4<br>
+  %inc = add nsw i32 %0, 1<br>
+  store i32 %inc, i32* %arrayidx, align 4<br>
+  %add = add nsw i32 %i.05, %s<br>
+  %cmp = icmp slt i32 %add, %n<br>
+  br i1 %cmp, label %for.body, label %for.end<br>
+<br>
+for.end:                                          ; preds = %for.body, %entry<br>
+  ret void<br>
+}<br>
+<br>
+<br>
+; Check that we are able to compute trip count of a loop without an entry guard.<br>
+; CHECK: Determining loop execution counts for: @foo2<br>
+; CHECK: backedge-taken count is ((-1 + (%n smax %s)) /u %s)<br>
+<br>
+; We should have a conservative estimate for the max backedge taken count for<br>
+; loops with unknown stride.<br>
+; CHECK: max backedge-taken count is -1<br>
+<br>
+; Function Attrs: norecurse nounwind<br>
+define void @foo2(i32* nocapture %A, i32 %n, i32 %s) #0 {<br>
+entry:<br>
+  br label %for.body<br>
+<br>
+for.body:                                         ; preds = %entry, %for.body<br>
+  %i.05 = phi i32 [ %add, %for.body ], [ 0, %entry ]<br>
+  %arrayidx = getelementptr inbounds i32, i32* %A, i32 %i.05<br>
+  %0 = load i32, i32* %arrayidx, align 4<br>
+  %inc = add nsw i32 %0, 1<br>
+  store i32 %inc, i32* %arrayidx, align 4<br>
+  %add = add nsw i32 %i.05, %s<br>
+  %cmp = icmp slt i32 %add, %n<br>
+  br i1 %cmp, label %for.body, label %for.end<br>
+<br>
+for.end:                                          ; preds = %for.body, %entry<br>
+  ret void<br>
+}<br>
+<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><o:p></o:p></p>
</blockquote>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</div>
</body>
</html>