From: Andrew Trick Date: Tue, 7 Jan 2014 01:02:55 +0000 (+0000) Subject: indvars: sink truncates outside the loop. X-Git-Tag: android-x86-7.1-r4~66319 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=ced88c5918851fb6b34c247471a7d2b62f806a01;p=android-x86%2Fexternal-llvm.git indvars: sink truncates outside the loop. This is a follow up of the r198338 commit that added truncates for lcssa phi nodes. Sinking the truncates below the phis cleans up the loop and simplifies subsequent analysis within the indvars pass. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@198654 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Scalar/IndVarSimplify.cpp b/lib/Transforms/Scalar/IndVarSimplify.cpp index 1076e50bc28..08df9533f16 100644 --- a/lib/Transforms/Scalar/IndVarSimplify.cpp +++ b/lib/Transforms/Scalar/IndVarSimplify.cpp @@ -883,6 +883,8 @@ const SCEVAddRecExpr *WidenIV::GetWideRecurrence(Instruction *NarrowUse) { /// This IV user cannot be widen. Replace this use of the original narrow IV /// with a truncation of the new wide IV to isolate and eliminate the narrow IV. static void truncateIVUse(NarrowIVDefUse DU, DominatorTree *DT) { + DEBUG(dbgs() << "INDVARS: Truncate IV " << *DU.WideDef + << " for user " << *DU.NarrowUse << "\n"); IRBuilder<> Builder(getInsertPointForUses(DU.NarrowUse, DU.NarrowDef, DT)); Value *Trunc = Builder.CreateTrunc(DU.WideDef, DU.NarrowDef->getType()); DU.NarrowUse->replaceUsesOfWith(DU.NarrowDef, Trunc); @@ -893,10 +895,27 @@ static void truncateIVUse(NarrowIVDefUse DU, DominatorTree *DT) { Instruction *WidenIV::WidenIVUse(NarrowIVDefUse DU, SCEVExpander &Rewriter) { // Stop traversing the def-use chain at inner-loop phis or post-loop phis. - if (isa(DU.NarrowUse) && - LI->getLoopFor(DU.NarrowUse->getParent()) != L) { - truncateIVUse(DU, DT); - return 0; + if (PHINode *UsePhi = dyn_cast(DU.NarrowUse)) { + if (LI->getLoopFor(UsePhi->getParent()) != L) { + // For LCSSA phis, sink the truncate outside the loop. + // After SimplifyCFG most loop exit targets have a single predecessor. + // Otherwise fall back to a truncate within the loop. + if (UsePhi->getNumOperands() != 1) + truncateIVUse(DU, DT); + else { + PHINode *WidePhi = + PHINode::Create(DU.WideDef->getType(), 1, UsePhi->getName() + ".wide", + UsePhi); + WidePhi->addIncoming(DU.WideDef, UsePhi->getIncomingBlock(0)); + IRBuilder<> Builder(WidePhi->getParent()->getFirstInsertionPt()); + Value *Trunc = Builder.CreateTrunc(WidePhi, DU.NarrowDef->getType()); + UsePhi->replaceAllUsesWith(Trunc); + DeadInsts.push_back(UsePhi); + DEBUG(dbgs() << "INDVARS: Widen lcssa phi " << *UsePhi + << " to " << *WidePhi << "\n"); + } + return 0; + } } // Our raison d'etre! Eliminate sign and zero extension. if (IsSigned ? isa(DU.NarrowUse) : isa(DU.NarrowUse)) { diff --git a/test/Transforms/IndVarSimplify/iv-widen.ll b/test/Transforms/IndVarSimplify/iv-widen.ll index 12067ab6fe8..c899e2f0a58 100644 --- a/test/Transforms/IndVarSimplify/iv-widen.ll +++ b/test/Transforms/IndVarSimplify/iv-widen.ll @@ -7,10 +7,11 @@ target triple = "x86_64-apple-darwin" ; Only one phi now. ; CHECK: phi ; CHECK-NOT: phi -; We now get 2 trunc, one for the gep and one for the lcssa phi. +; One trunc for the gep. ; CHECK: trunc i64 %indvars.iv to i32 -; CHECK: trunc i64 %indvars.iv to i32 -; CHECK-LABEL: B24: +; One trunc for the dummy() call. +; CHECK-LABEL: exit24: +; CHECK: trunc i64 {{.*}}lcssa.wide to i32 define void @sloop(i32* %a) { Prologue: br i1 undef, label %B18, label %B6