<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 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:"\@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* 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;}
span.EmailStyle17
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.25in 1.0in 1.25in;}
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'>Thanks, 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><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'>Yes, I made a mistake, I will revert and recommit.<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><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'>Regards<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><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'>Lawrence Hu<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><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 Blaikie [mailto:dblaikie@gmail.com] <br><b>Sent:</b> Monday, January 25, 2016 11:14 AM<br><b>To:</b> Lawrence Hu<br><b>Cc:</b> llvm-commits<br><b>Subject:</b> Re: [llvm] r258700 - Differential Revision: http://reviews.llvm.org/D13151<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 Mon, Jan 25, 2016 at 10:53 AM, Lawrence Hu 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-right:0in'><p class=MsoNormal>Author: lawrence<br>Date: Mon Jan 25 12:53:39 2016<br>New Revision: 258700<br><br>URL: <a href="http://llvm.org/viewvc/llvm-project?rev=258700&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=258700&view=rev</a><br>Log:<o:p></o:p></p></blockquote><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>This could've used a commit message ^<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-right:0in'><p class=MsoNormal><br>Differential Revision: <a href="http://reviews.llvm.org/D13151" target="_blank">http://reviews.llvm.org/D13151</a><br><br>Added:<br> llvm/trunk/test/Transforms/LoopReroll/ptrindvar.ll<br>Modified:<br> llvm/trunk/lib/Transforms/Scalar/LoopRerollPass.cpp<br><br>Modified: llvm/trunk/lib/Transforms/Scalar/LoopRerollPass.cpp<br>URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopRerollPass.cpp?rev=258700&r1=258699&r2=258700&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopRerollPass.cpp?rev=258700&r1=258699&r2=258700&view=diff</a><br>==============================================================================<br>--- llvm/trunk/lib/Transforms/Scalar/LoopRerollPass.cpp (original)<br>+++ llvm/trunk/lib/Transforms/Scalar/LoopRerollPass.cpp Mon Jan 25 12:53:39 2016<br>@@ -396,6 +396,7 @@ namespace {<br> bool instrDependsOn(Instruction *I,<br> UsesTy::iterator Start,<br> UsesTy::iterator End);<br>+ void replaceIV(Instruction *Inst, Instruction *IV, const SCEV *IterCount);<br><br> LoopReroll *Parent;<br><br>@@ -460,6 +461,48 @@ static bool hasUsesOutsideLoop(Instructi<br> return false;<br> }<br><br>+static const SCEVConstant *getIncrmentFactorSCEV(ScalarEvolution *SE,<br>+ const SCEV *SCEVExpr,<br>+ Instruction &IV) {<br>+ const SCEVMulExpr *MulSCEV = dyn_cast<SCEVMulExpr>(SCEVExpr);<br>+<br>+ // If StepRecurrence of a SCEVExpr is a constant (c1 * c2, c2 = sizeof(ptr)),<br>+ // Return c1.<br>+ if (!MulSCEV && IV.getType()->isPointerTy())<br>+ if (const SCEVConstant *IncSCEV = dyn_cast<SCEVConstant>(SCEVExpr)) {<br>+ const PointerType *PTy = cast<PointerType>(IV.getType());<br>+ Type *ElTy = PTy->getElementType();<br>+ const SCEV *SizeOfExpr =<br>+ SE->getSizeOfExpr(SE->getEffectiveSCEVType(IV.getType()), ElTy);<br>+ if (IncSCEV->getValue()->getValue().isNegative()) {<br>+ const SCEV *NewSCEV =<br>+ SE->getUDivExpr(SE->getNegativeSCEV(SCEVExpr), SizeOfExpr);<br>+ return dyn_cast<SCEVConstant>(SE->getNegativeSCEV(NewSCEV));<br>+ } else {<br>+ return dyn_cast<SCEVConstant>(SE->getUDivExpr(SCEVExpr, SizeOfExpr));<br>+ }<br>+ }<br>+<br>+ if (!MulSCEV)<br>+ return nullptr;<br>+<br>+ // If StepRecurrence of a SCEVExpr is a c * sizeof(x), where c is constant,<br>+ // Return c.<br>+ const SCEVConstant *CIncSCEV = nullptr;<br>+ for (const SCEV *Operand : MulSCEV->operands()) {<br>+ if (const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Operand)) {<br>+ CIncSCEV = Constant;<br>+ } else if (const SCEVUnknown *Unknown = dyn_cast<SCEVUnknown>(Operand)) {<br>+ Type *AllocTy;<br>+ if (!Unknown->isSizeOf(AllocTy))<br>+ break;<br>+ } else {<br>+ return nullptr;<br>+ }<br>+ }<br>+ return CIncSCEV;<br>+}<br>+<br> // Collect the list of loop induction variables with respect to which it might<br> // be possible to reroll the loop.<br> void LoopReroll::collectPossibleIVs(Loop *L,<br>@@ -469,7 +512,7 @@ void LoopReroll::collectPossibleIVs(Loop<br> IE = Header->getFirstInsertionPt(); I != IE; ++I) {<br> if (!isa<PHINode>(I))<br> continue;<br>- if (!I->getType()->isIntegerTy())<br>+ if (!I->getType()->isIntegerTy() && !I->getType()->isPointerTy())<br> continue;<br><br> if (const SCEVAddRecExpr *PHISCEV =<br>@@ -478,9 +521,14 @@ void LoopReroll::collectPossibleIVs(Loop<br> continue;<br> if (!PHISCEV->isAffine())<br> continue;<br>- if (const SCEVConstant *IncSCEV =<br>- dyn_cast<SCEVConstant>(PHISCEV->getStepRecurrence(*SE))) {<br>- const APInt &AInt = IncSCEV->getAPInt().abs();<br>+ const SCEVConstant *IncSCEV = nullptr;<br>+ if (I->getType()->isPointerTy())<br>+ IncSCEV =<br>+ getIncrmentFactorSCEV(SE, PHISCEV->getStepRecurrence(*SE), *I);<br>+ else<br>+ IncSCEV = dyn_cast<SCEVConstant>(PHISCEV->getStepRecurrence(*SE));<br>+ if (IncSCEV) {<br>+ const APInt &AInt = IncSCEV->getValue()->getValue().abs();<br> if (IncSCEV->getValue()->isZero() || AInt.uge(MaxInc))<br> continue;<br> IVToIncMap[&*I] = IncSCEV->getValue()->getSExtValue();<br>@@ -651,10 +699,12 @@ static bool isSimpleArithmeticOp(User *I<br><br> static bool isLoopIncrement(User *U, Instruction *IV) {<br> BinaryOperator *BO = dyn_cast<BinaryOperator>(U);<br>- if (!BO || BO->getOpcode() != Instruction::Add)<br>+<br>+ if ((BO && BO->getOpcode() != Instruction::Add) ||<br>+ (!BO && !isa<GetElementPtrInst>(U)))<br> return false;<br><br>- for (auto *UU : BO->users()) {<br>+ for (auto *UU : U->users()) {<br> PHINode *PN = dyn_cast<PHINode>(UU);<br> if (PN && PN == IV)<br> return true;<br>@@ -1272,61 +1322,84 @@ void LoopReroll::DAGRootTracker::replace<br><br> ++J;<br> }<br>- bool Negative = IVToIncMap[IV] < 0;<br>- const DataLayout &DL = Header->getModule()->getDataLayout();<br><br> // We need to create a new induction variable for each different BaseInst.<br>- for (auto &DRS : RootSets) {<br>+ for (auto &DRS : RootSets)<br> // Insert the new induction variable.<br>- const SCEVAddRecExpr *RealIVSCEV =<br>- cast<SCEVAddRecExpr>(SE->getSCEV(DRS.BaseInst));<br>- const SCEV *Start = RealIVSCEV->getStart();<br>- const SCEVAddRecExpr *H = cast<SCEVAddRecExpr>(SE->getAddRecExpr(<br>- Start, SE->getConstant(RealIVSCEV->getType(), Negative ? -1 : 1), L,<br>- SCEV::FlagAnyWrap));<br>- { // Limit the lifetime of SCEVExpander.<br>- SCEVExpander Expander(*SE, DL, "reroll");<br>- Value *NewIV = Expander.expandCodeFor(H, IV->getType(), &Header->front());<br>-<br>- for (auto &KV : Uses) {<br>- if (KV.second.find_first() == 0)<br>- KV.first->replaceUsesOfWith(DRS.BaseInst, NewIV);<br>- }<br>-<br>- if (BranchInst *BI = dyn_cast<BranchInst>(Header->getTerminator())) {<br>- // FIXME: Why do we need this check?<br>- if (Uses[BI].find_first() == IL_All) {<br>- const SCEV *ICSCEV = RealIVSCEV->evaluateAtIteration(IterCount, *SE);<br>-<br>- // Iteration count SCEV minus 1<br>- const SCEV *ICMinus1SCEV = SE->getMinusSCEV(<br>- ICSCEV, SE->getConstant(ICSCEV->getType(), Negative ? -1 : 1));<br>-<br>- Value *ICMinus1; // Iteration count minus 1<br>- if (isa<SCEVConstant>(ICMinus1SCEV)) {<br>- ICMinus1 = Expander.expandCodeFor(ICMinus1SCEV, NewIV->getType(), BI);<br>- } else {<br>- BasicBlock *Preheader = L->getLoopPreheader();<br>- if (!Preheader)<br>- Preheader = InsertPreheaderForLoop(L, DT, LI, PreserveLCSSA);<br>+ replaceIV(DRS.BaseInst, IV, IterCount);<br><br>- ICMinus1 = Expander.expandCodeFor(ICMinus1SCEV, NewIV->getType(),<br>- Preheader->getTerminator());<br>- }<br>+ SimplifyInstructionsInBlock(Header, TLI);<br>+ DeleteDeadPHIs(Header, TLI);<br>+}<br><br>- Value *Cond =<br>- new ICmpInst(BI, CmpInst::ICMP_EQ, NewIV, ICMinus1, "exitcond");<br>- BI->setCondition(Cond);<br>+void LoopReroll::DAGRootTracker::replaceIV(Instruction *Inst,<br>+ Instruction *InstIV,<br>+ const SCEV *IterCount) {<br>+ BasicBlock *Header = L->getHeader();<br>+ int64_t Inc = IVToIncMap[InstIV];<br>+ bool Negative = Inc < 0;<br>+<br>+ const SCEVAddRecExpr *RealIVSCEV = cast<SCEVAddRecExpr>(SE->getSCEV(Inst));<br>+ const SCEV *Start = RealIVSCEV->getStart();<br><br>- if (BI->getSuccessor(1) != Header)<br>- BI->swapSuccessors();<br>+ const SCEV *SizeOfExpr = nullptr;<br>+ const SCEV *IncrExpr =<br>+ SE->getConstant(RealIVSCEV->getType(), Negative ? -1 : 1);<br>+ if (auto *PTy = dyn_cast<PointerType>(Inst->getType())) {<br>+ Type *ElTy = PTy->getElementType();<br>+ SizeOfExpr =<br>+ SE->getSizeOfExpr(SE->getEffectiveSCEVType(Inst->getType()), ElTy);<br>+ IncrExpr = SE->getMulExpr(IncrExpr, SizeOfExpr);<br>+ }<br>+ const SCEV *NewIVSCEV =<br>+ SE->getAddRecExpr(Start, IncrExpr, L, SCEV::FlagAnyWrap);<br>+<br>+ { // Limit the lifetime of SCEVExpander.<br>+ const DataLayout &DL = Header->getModule()->getDataLayout();<br>+ SCEVExpander Expander(*SE, DL, "reroll");<br>+ Value *NewIV =<br>+ Expander.expandCodeFor(NewIVSCEV, InstIV->getType(), &Header->front());<br>+<br>+ for (auto &KV : Uses)<br>+ if (KV.second.find_first() == 0)<br>+ KV.first->replaceUsesOfWith(Inst, NewIV);<br>+<br>+ if (BranchInst *BI = dyn_cast<BranchInst>(Header->getTerminator())) {<br>+ // FIXME: Why do we need this check?<br>+ if (Uses[BI].find_first() == IL_All) {<br>+ const SCEV *ICSCEV = RealIVSCEV->evaluateAtIteration(IterCount, *SE);<br>+<br>+ // Iteration count SCEV minus or plus 1<br>+ const SCEV *MinusPlus1SCEV =<br>+ SE->getConstant(ICSCEV->getType(), Negative ? -1 : 1);<br>+ if (Inst->getType()->isPointerTy()) {<br>+ assert(SizeOfExpr && "SizeOfExpr is not initialized");<br>+ MinusPlus1SCEV = SE->getMulExpr(MinusPlus1SCEV, SizeOfExpr);<br> }<br>+<br>+ const SCEV *ICMinusPlus1SCEV = SE->getMinusSCEV(ICSCEV, MinusPlus1SCEV);<br>+ // Iteration count minus 1<br>+ Value *ICMinusPlus1 = nullptr;<br>+ if (isa<SCEVConstant>(ICMinusPlus1SCEV)) {<br>+ ICMinusPlus1 =<br>+ Expander.expandCodeFor(ICMinusPlus1SCEV, NewIV->getType(), BI);<br>+ } else {<br>+ BasicBlock *Preheader = L->getLoopPreheader();<br>+ if (!Preheader)<br>+ Preheader = InsertPreheaderForLoop(L, DT, LI, PreserveLCSSA);<br>+ ICMinusPlus1 = Expander.expandCodeFor(<br>+ ICMinusPlus1SCEV, NewIV->getType(), Preheader->getTerminator());<br>+ }<br>+<br>+ Value *Cond =<br>+ new ICmpInst(BI, CmpInst::ICMP_EQ, NewIV, ICMinusPlus1, "exitcond");<br>+ BI->setCondition(Cond);<br>+<br>+ if (BI->getSuccessor(1) != Header)<br>+ BI->swapSuccessors();<br> }<br> }<br> }<br>-<br>- SimplifyInstructionsInBlock(Header, TLI);<br>- DeleteDeadPHIs(Header, TLI);<br> }<br><br> // Validate the selected reductions. All iterations must have an isomorphic<br><br>Added: llvm/trunk/test/Transforms/LoopReroll/ptrindvar.ll<br>URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LoopReroll/ptrindvar.ll?rev=258700&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LoopReroll/ptrindvar.ll?rev=258700&view=auto</a><br>==============================================================================<br>--- llvm/trunk/test/Transforms/LoopReroll/ptrindvar.ll (added)<br>+++ llvm/trunk/test/Transforms/LoopReroll/ptrindvar.ll Mon Jan 25 12:53:39 2016<br>@@ -0,0 +1,81 @@<br>+; RUN: opt -S -loop-reroll %s | FileCheck %s<br>+target triple = "aarch64--linux-gnu"<br>+<br>+define i32 @test(i32* readonly %buf, i32* readnone %end) #0 {<br>+entry:<br>+ %cmp.9 = icmp eq i32* %buf, %end<br>+ br i1 %cmp.9, label %while.end, label %while.body.preheader<br>+<br>+while.body.preheader:<br>+ br label %while.body<br>+<br>+while.body:<br>+;CHECK-LABEL: while.body:<br>+;CHECK-NEXT: %indvar = phi i64 [ %indvar.next, %while.body ], [ 0, %while.body.preheader ]<br>+;CHECK-NEXT: %S.011 = phi i32 [ %add, %while.body ], [ undef, %while.body.preheader ]<br>+;CHECK-NEXT: %scevgep = getelementptr i32, i32* %buf, i64 %indvar<br>+;CHECK-NEXT: %4 = load i32, i32* %scevgep, align 4<br>+;CHECK-NEXT: %add = add nsw i32 %4, %S.011<br>+;CHECK-NEXT: %indvar.next = add i64 %indvar, 1<br>+;CHECK-NEXT: %exitcond = icmp eq i32* %scevgep, %scevgep5<br>+;CHECK-NEXT: br i1 %exitcond, label %while.end.loopexit, label %while.body<br>+<br>+ %S.011 = phi i32 [ %add2, %while.body ], [ undef, %while.body.preheader ]<br>+ %buf.addr.010 = phi i32* [ %add.ptr, %while.body ], [ %buf, %while.body.preheader ]<br>+ %0 = load i32, i32* %buf.addr.010, align 4<br>+ %add = add nsw i32 %0, %S.011<br>+ %arrayidx1 = getelementptr inbounds i32, i32* %buf.addr.010, i64 1<br>+ %1 = load i32, i32* %arrayidx1, align 4<br>+ %add2 = add nsw i32 %add, %1<br>+ %add.ptr = getelementptr inbounds i32, i32* %buf.addr.010, i64 2<br>+ %cmp = icmp eq i32* %add.ptr, %end<br>+ br i1 %cmp, label %while.end.loopexit, label %while.body<br>+<br>+while.end.loopexit:<br>+ %add2.lcssa = phi i32 [ %add2, %while.body ]<br>+ br label %while.end<br>+<br>+while.end:<br>+ %S.0.lcssa = phi i32 [ undef, %entry ], [ %add2.lcssa, %while.end.loopexit ]<br>+ ret i32 %S.0.lcssa<br>+}<br>+<br>+define i32 @test2(i32* readonly %buf, i32* readnone %end) #0 {<br>+entry:<br>+ %cmp.9 = icmp eq i32* %buf, %end<br>+ br i1 %cmp.9, label %while.end, label %while.body.preheader<br>+<br>+while.body.preheader:<br>+ br label %while.body<br>+<br>+while.body:<br>+;CHECK-LABEL: while.body:<br>+;CHECK-NEXT: %indvar = phi i64 [ %indvar.next, %while.body ], [ 0, %while.body.preheader ]<br>+;CHECK-NEXT: %S.011 = phi i32 [ %add, %while.body ], [ undef, %while.body.preheader ]<br>+;CHECK-NEXT: %4 = mul i64 %indvar, -1<br>+;CHECK-NEXT: %scevgep = getelementptr i32, i32* %buf, i64 %4<br>+;CHECK-NEXT: %5 = load i32, i32* %scevgep, align 4<br>+;CHECK-NEXT: %add = add nsw i32 %5, %S.011<br>+;CHECK-NEXT: %indvar.next = add i64 %indvar, 1<br>+;CHECK-NEXT: %exitcond = icmp eq i32* %scevgep, %scevgep5<br>+;CHECK-NEXT: br i1 %exitcond, label %while.end.loopexit, label %while.body<br>+<br>+ %S.011 = phi i32 [ %add2, %while.body ], [ undef, %while.body.preheader ]<br>+ %buf.addr.010 = phi i32* [ %add.ptr, %while.body ], [ %buf, %while.body.preheader ]<br>+ %0 = load i32, i32* %buf.addr.010, align 4<br>+ %add = add nsw i32 %0, %S.011<br>+ %arrayidx1 = getelementptr inbounds i32, i32* %buf.addr.010, i64 -1<br>+ %1 = load i32, i32* %arrayidx1, align 4<br>+ %add2 = add nsw i32 %add, %1<br>+ %add.ptr = getelementptr inbounds i32, i32* %buf.addr.010, i64 -2<br>+ %cmp = icmp eq i32* %add.ptr, %end<br>+ br i1 %cmp, label %while.end.loopexit, label %while.body<br>+<br>+while.end.loopexit:<br>+ %add2.lcssa = phi i32 [ %add2, %while.body ]<br>+ br label %while.end<br>+<br>+while.end:<br>+ %S.0.lcssa = phi i32 [ undef, %entry ], [ %add2.lcssa, %while.end.loopexit ]<br>+ ret i32 %S.0.lcssa<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>