<div dir="auto"><br><div class="gmail_extra" dir="auto"><div class="gmail_quote"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto">It was accepted by one reviewer which is why it was landed as per policy.<div dir="auto"><br></div><div dir="auto">We can do a post commit review also as per developer's policy.</div><div dir="auto"><br></div><div dir="auto">Do let me know what comments you have and it could be incorporated.</div><div dir="auto"><br></div></div><div class="elided-text"><div class="gmail_extra"><br><div class="gmail_quote">On 18 Oct 2017 10:22, "Sanjoy Das" <<a href="mailto:sanjoy@playingwithpointers.com" target="_blank">sanjoy@playingwithpointers.<wbr>com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div dir="auto">Hi Jatin,</div><div dir="auto"><br></div><div dir="auto">This hasn't been LGTMed. Please revert it for now while we continue the review. </div><div dir="auto"><br></div><div dir="auto">-- Sanjoy</div><br><div class="gmail_quote"><div>On Tue, Oct 17, 2017 at 6:36 PM Jatin Bhateja via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: jbhateja<br>
Date: Tue Oct 17 18:36:16 2017<br>
New Revision: 316054<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=316054&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-pr<wbr>oject?rev=316054&view=rev</a><br>
Log:<br>
[ScalarEvolution] Handling for ICmp occuring in the evolution chain.<br>
<br>
Summary:<br>
If a compare instruction is same or inverse of the compare in the<br>
branch of the loop latch, then return a constant evolution node.<br>
Currently scope of evaluation is limited to SCEV computation for<br>
PHI nodes.<br>
<br>
This shall facilitate computations of loop exit counts in cases<br>
where compare appears in the evolution chain of induction variables.<br>
<br>
Will fix PR 34538<br>
Reviewers: sanjoy, hfinkel, junryoungju<br>
<br>
Reviewed By: junryoungju<br>
<br>
Subscribers: javed.absar, llvm-commits<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D38494" rel="noreferrer" target="_blank">https://reviews.llvm.org/D3849<wbr>4</a><br>
<br>
Added:<br>
llvm/trunk/test/Analysis/Scala<wbr>rEvolution/pr34538.ll<br>
Modified:<br>
llvm/trunk/include/llvm/Analys<wbr>is/ScalarEvolution.h<br>
llvm/trunk/lib/Analysis/Scalar<wbr>Evolution.cpp<br>
llvm/trunk/lib/Transforms/Scal<wbr>ar/LoopStrengthReduce.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/Analys<wbr>is/ScalarEvolution.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/ScalarEvolution.h?rev=316054&r1=316053&r2=316054&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-pr<wbr>oject/llvm/trunk/include/llvm/<wbr>Analysis/ScalarEvolution.h?<wbr>rev=316054&r1=316053&r2=316054<wbr>&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/include/llvm/Analys<wbr>is/ScalarEvolution.h (original)<br>
+++ llvm/trunk/include/llvm/Analys<wbr>is/ScalarEvolution.h Tue Oct 17 18:36:16 2017<br>
@@ -1378,6 +1378,9 @@ private:<br>
/// Helper function called from createNodeForPHI.<br>
const SCEV *createAddRecFromPHI(PHINode *PN);<br>
<br>
+ /// Evaluate ICmpInst to a constant node for special patterns.<br>
+ const SCEV *evaluateForICmp(ICmpInst *IC);<br>
+<br>
/// A helper function for createAddRecFromPHI to handle simple cases.<br>
const SCEV *createSimpleAffineAddRec(PHIN<wbr>ode *PN, Value *BEValueV,<br>
Value *StartValueV);<br>
<br>
Modified: llvm/trunk/lib/Analysis/Scalar<wbr>Evolution.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ScalarEvolution.cpp?rev=316054&r1=316053&r2=316054&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-pr<wbr>oject/llvm/trunk/lib/Analysis/<wbr>ScalarEvolution.cpp?rev=<wbr>316054&r1=316053&r2=316054&<wbr>view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/lib/Analysis/Scalar<wbr>Evolution.cpp (original)<br>
+++ llvm/trunk/lib/Analysis/Scalar<wbr>Evolution.cpp Tue Oct 17 18:36:16 2017<br>
@@ -4756,11 +4756,26 @@ const SCEV *ScalarEvolution::createAddRe<br>
Ops.push_back(Add->getOperand<wbr>(i));<br>
const SCEV *Accum = getAddExpr(Ops);<br>
<br>
+ bool InvariantF = isLoopInvariant(Accum, L);<br>
+<br>
+ if (!InvariantF && Accum->getSCEVType() == scZeroExtend) {<br>
+ const SCEV *Op = dyn_cast<SCEVZeroExtendExpr>(A<wbr>ccum)->getOperand();<br>
+ const SCEVUnknown *Un = dyn_cast<SCEVUnknown>(Op);<br>
+ if (Un && Un->getValue() && isa<Instruction>(Un->getValue(<wbr>)) &&<br>
+ dyn_cast<Instruction>(Un->getV<wbr>alue())->getOpcode() ==<br>
+ Instruction::ICmp) {<br>
+ const SCEV *ICmpSC = evaluateForICmp(cast<ICmpInst><wbr>(Un->getValue()));<br>
+ bool IsConstSC = ICmpSC->getSCEVType() == scConstant;<br>
+ Accum =<br>
+ IsConstSC ? getZeroExtendExpr(ICmpSC, Accum->getType()) : Accum;<br>
+ InvariantF = IsConstSC ? true : false;<br>
+ }<br>
+ }<br>
+<br>
// This is not a valid addrec if the step amount is varying each<br>
// loop iteration, but is not itself an addrec in this loop.<br>
- if (isLoopInvariant(Accum, L) ||<br>
- (isa<SCEVAddRecExpr>(Accum) &&<br>
- cast<SCEVAddRecExpr>(Accum)-><wbr>getLoop() == L)) {<br>
+ if (InvariantF || (isa<SCEVAddRecExpr>(Accum) &&<br>
+ cast<SCEVAddRecExpr>(Accum)-><wbr>getLoop() == L)) {<br>
SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;<br>
<br>
if (auto BO = MatchBinaryOp(BEValueV, DT)) {<br>
@@ -6443,6 +6458,30 @@ void ScalarEvolution::forgetLoop(co<wbr>nst L<br>
}<br>
}<br>
<br>
+<br>
+const SCEV *ScalarEvolution::evaluateForI<wbr>Cmp(ICmpInst *IC) {<br>
+ BasicBlock *Latch = nullptr;<br>
+ const Loop *L = LI.getLoopFor(IC->getParent())<wbr>;<br>
+<br>
+ // If compare instruction is same or inverse of the compare in the<br>
+ // branch of the loop latch, then return a constant evolution<br>
+ // node. This shall facilitate computations of loop exit counts<br>
+ // in cases where compare appears in the evolution chain of induction<br>
+ // variables.<br>
+ if (L && (Latch = L->getLoopLatch())) {<br>
+ BranchInst *BI = dyn_cast<BranchInst>(Latch->ge<wbr>tTerminator());<br>
+ if (BI && BI->isConditional() && BI->getCondition() == IC) {<br>
+ if (BI->getSuccessor(0) != L->getHeader())<br>
+ return getZero(Type::getInt1Ty(getCon<wbr>text()));<br>
+ else<br>
+ return getOne(Type::getInt1Ty(getCont<wbr>ext()));<br>
+ }<br>
+ }<br>
+<br>
+ return getUnknown(IC);<br>
+}<br>
+<br>
+<br>
void ScalarEvolution::forgetValue(V<wbr>alue *V) {<br>
Instruction *I = dyn_cast<Instruction>(V);<br>
if (!I) return;<br>
<br>
Modified: llvm/trunk/lib/Transforms/Scal<wbr>ar/LoopStrengthReduce.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopStrengthReduce.cpp?rev=316054&r1=316053&r2=316054&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-pr<wbr>oject/llvm/trunk/lib/Transform<wbr>s/Scalar/LoopStrengthReduce.<wbr>cpp?rev=316054&r1=316053&r2=<wbr>316054&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/lib/Transforms/Scal<wbr>ar/LoopStrengthReduce.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/Scal<wbr>ar/LoopStrengthReduce.cpp Tue Oct 17 18:36:16 2017<br>
@@ -2973,8 +2973,11 @@ void LSRInstance::CollectChains() {<br>
// Ignore users that are part of a SCEV expression. This way we only<br>
// consider leaf IV Users. This effectively rediscovers a portion of<br>
// IVUsers analysis but in program order this time.<br>
- if (SE.isSCEVable(I.getType()) && !isa<SCEVUnknown>(SE.getSCEV(&<wbr>I)))<br>
- continue;<br>
+ if (SE.isSCEVable(I.getType())) {<br>
+ const SCEV *SI = SE.getSCEV(&I);<br>
+ if (!isa<SCEVUnknown>(SI) && !isa<SCEVConstant>(SI))<br>
+ continue;<br>
+ }<br>
<br>
// Remove this instruction from any NearUsers set it may be in.<br>
for (unsigned ChainIdx = 0, NChains = IVChainVec.size();<br>
<br>
Added: llvm/trunk/test/Analysis/Scala<wbr>rEvolution/pr34538.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/ScalarEvolution/pr34538.ll?rev=316054&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-pr<wbr>oject/llvm/trunk/test/Analysis<wbr>/ScalarEvolution/pr34538.ll?<wbr>rev=316054&view=auto</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/test/Analysis/Scala<wbr>rEvolution/pr34538.ll (added)<br>
+++ llvm/trunk/test/Analysis/Scala<wbr>rEvolution/pr34538.ll Tue Oct 17 18:36:16 2017<br>
@@ -0,0 +1,19 @@<br>
+; RUN: opt -S -scalar-evolution -loop-deletion -simplifycfg -analyze < %s | FileCheck %s --check-prefix=CHECK-ANALYSIS<br>
+<br>
+define i32 @foo() local_unnamed_addr #0 {<br>
+; CHECK-ANALYSIS: Loop %do.body: backedge-taken count is 10000<br>
+; CHECK-ANALYSIS: Loop %do.body: max backedge-taken count is 10000<br>
+; CHECK-ANALYSIS: Loop %do.body: Predicated backedge-taken count is 10000<br>
+entry:<br>
+ br label %do.body<br>
+<br>
+do.body: ; preds = %do.body, %entry<br>
+ %start.0 = phi i32 [ 0, %entry ], [ %inc.start.0, %do.body ]<br>
+ %cmp = icmp slt i32 %start.0, 10000<br>
+ %inc = zext i1 %cmp to i32<br>
+ %inc.start.0 = add nsw i32 %start.0, %inc<br>
+ br i1 %cmp, label %do.body, label %do.end<br>
+<br>
+do.end: ; preds = %do.body<br>
+ ret i32 0<br>
+}<br>
<br>
<br>
______________________________<wbr>_________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-commits</a><br>
</blockquote></div></div>
</blockquote></div></div>
</div></blockquote></div><br></div></div>