LLVM API Documentation

LoopUnroll.cpp
Go to the documentation of this file.
00001 //===-- UnrollLoop.cpp - Loop unrolling utilities -------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements some loop unrolling utilities. It does not define any
00011 // actual pass or policy, but provides a single function to perform loop
00012 // unrolling.
00013 //
00014 // The process of unrolling can produce extraneous basic blocks linked with
00015 // unconditional branches.  This will be corrected in the future.
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #include "llvm/Transforms/Utils/UnrollLoop.h"
00020 #include "llvm/ADT/SmallPtrSet.h"
00021 #include "llvm/ADT/Statistic.h"
00022 #include "llvm/Analysis/AssumptionTracker.h"
00023 #include "llvm/Analysis/InstructionSimplify.h"
00024 #include "llvm/Analysis/LoopIterator.h"
00025 #include "llvm/Analysis/LoopPass.h"
00026 #include "llvm/Analysis/ScalarEvolution.h"
00027 #include "llvm/IR/BasicBlock.h"
00028 #include "llvm/IR/DataLayout.h"
00029 #include "llvm/IR/Dominators.h"
00030 #include "llvm/IR/DiagnosticInfo.h"
00031 #include "llvm/IR/LLVMContext.h"
00032 #include "llvm/Support/Debug.h"
00033 #include "llvm/Support/raw_ostream.h"
00034 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
00035 #include "llvm/Transforms/Utils/Cloning.h"
00036 #include "llvm/Transforms/Utils/Local.h"
00037 #include "llvm/Transforms/Utils/LoopUtils.h"
00038 #include "llvm/Transforms/Utils/SimplifyIndVar.h"
00039 using namespace llvm;
00040 
00041 #define DEBUG_TYPE "loop-unroll"
00042 
00043 // TODO: Should these be here or in LoopUnroll?
00044 STATISTIC(NumCompletelyUnrolled, "Number of loops completely unrolled");
00045 STATISTIC(NumUnrolled, "Number of loops unrolled (completely or otherwise)");
00046 
00047 /// RemapInstruction - Convert the instruction operands from referencing the
00048 /// current values into those specified by VMap.
00049 static inline void RemapInstruction(Instruction *I,
00050                                     ValueToValueMapTy &VMap) {
00051   for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
00052     Value *Op = I->getOperand(op);
00053     ValueToValueMapTy::iterator It = VMap.find(Op);
00054     if (It != VMap.end())
00055       I->setOperand(op, It->second);
00056   }
00057 
00058   if (PHINode *PN = dyn_cast<PHINode>(I)) {
00059     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
00060       ValueToValueMapTy::iterator It = VMap.find(PN->getIncomingBlock(i));
00061       if (It != VMap.end())
00062         PN->setIncomingBlock(i, cast<BasicBlock>(It->second));
00063     }
00064   }
00065 }
00066 
00067 /// FoldBlockIntoPredecessor - Folds a basic block into its predecessor if it
00068 /// only has one predecessor, and that predecessor only has one successor.
00069 /// The LoopInfo Analysis that is passed will be kept consistent.  If folding is
00070 /// successful references to the containing loop must be removed from
00071 /// ScalarEvolution by calling ScalarEvolution::forgetLoop because SE may have
00072 /// references to the eliminated BB.  The argument ForgottenLoops contains a set
00073 /// of loops that have already been forgotten to prevent redundant, expensive
00074 /// calls to ScalarEvolution::forgetLoop.  Returns the new combined block.
00075 static BasicBlock *
00076 FoldBlockIntoPredecessor(BasicBlock *BB, LoopInfo* LI, LPPassManager *LPM,
00077                          SmallPtrSetImpl<Loop *> &ForgottenLoops) {
00078   // Merge basic blocks into their predecessor if there is only one distinct
00079   // pred, and if there is only one distinct successor of the predecessor, and
00080   // if there are no PHI nodes.
00081   BasicBlock *OnlyPred = BB->getSinglePredecessor();
00082   if (!OnlyPred) return nullptr;
00083 
00084   if (OnlyPred->getTerminator()->getNumSuccessors() != 1)
00085     return nullptr;
00086 
00087   DEBUG(dbgs() << "Merging: " << *BB << "into: " << *OnlyPred);
00088 
00089   // Resolve any PHI nodes at the start of the block.  They are all
00090   // guaranteed to have exactly one entry if they exist, unless there are
00091   // multiple duplicate (but guaranteed to be equal) entries for the
00092   // incoming edges.  This occurs when there are multiple edges from
00093   // OnlyPred to OnlySucc.
00094   FoldSingleEntryPHINodes(BB);
00095 
00096   // Delete the unconditional branch from the predecessor...
00097   OnlyPred->getInstList().pop_back();
00098 
00099   // Make all PHI nodes that referred to BB now refer to Pred as their
00100   // source...
00101   BB->replaceAllUsesWith(OnlyPred);
00102 
00103   // Move all definitions in the successor to the predecessor...
00104   OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList());
00105 
00106   // OldName will be valid until erased.
00107   StringRef OldName = BB->getName();
00108 
00109   // Erase basic block from the function...
00110 
00111   // ScalarEvolution holds references to loop exit blocks.
00112   if (LPM) {
00113     if (ScalarEvolution *SE = LPM->getAnalysisIfAvailable<ScalarEvolution>()) {
00114       if (Loop *L = LI->getLoopFor(BB)) {
00115         if (ForgottenLoops.insert(L))
00116           SE->forgetLoop(L);
00117       }
00118     }
00119   }
00120   LI->removeBlock(BB);
00121 
00122   // Inherit predecessor's name if it exists...
00123   if (!OldName.empty() && !OnlyPred->hasName())
00124     OnlyPred->setName(OldName);
00125 
00126   BB->eraseFromParent();
00127 
00128   return OnlyPred;
00129 }
00130 
00131 /// Unroll the given loop by Count. The loop must be in LCSSA form. Returns true
00132 /// if unrolling was successful, or false if the loop was unmodified. Unrolling
00133 /// can only fail when the loop's latch block is not terminated by a conditional
00134 /// branch instruction. However, if the trip count (and multiple) are not known,
00135 /// loop unrolling will mostly produce more code that is no faster.
00136 ///
00137 /// TripCount is generally defined as the number of times the loop header
00138 /// executes. UnrollLoop relaxes the definition to permit early exits: here
00139 /// TripCount is the iteration on which control exits LatchBlock if no early
00140 /// exits were taken. Note that UnrollLoop assumes that the loop counter test
00141 /// terminates LatchBlock in order to remove unnecesssary instances of the
00142 /// test. In other words, control may exit the loop prior to TripCount
00143 /// iterations via an early branch, but control may not exit the loop from the
00144 /// LatchBlock's terminator prior to TripCount iterations.
00145 ///
00146 /// Similarly, TripMultiple divides the number of times that the LatchBlock may
00147 /// execute without exiting the loop.
00148 ///
00149 /// The LoopInfo Analysis that is passed will be kept consistent.
00150 ///
00151 /// If a LoopPassManager is passed in, and the loop is fully removed, it will be
00152 /// removed from the LoopPassManager as well. LPM can also be NULL.
00153 ///
00154 /// This utility preserves LoopInfo. If DominatorTree or ScalarEvolution are
00155 /// available from the Pass it must also preserve those analyses.
00156 bool llvm::UnrollLoop(Loop *L, unsigned Count, unsigned TripCount,
00157                       bool AllowRuntime, unsigned TripMultiple,
00158                       LoopInfo *LI, Pass *PP, LPPassManager *LPM,
00159                       AssumptionTracker *AT) {
00160   BasicBlock *Preheader = L->getLoopPreheader();
00161   if (!Preheader) {
00162     DEBUG(dbgs() << "  Can't unroll; loop preheader-insertion failed.\n");
00163     return false;
00164   }
00165 
00166   BasicBlock *LatchBlock = L->getLoopLatch();
00167   if (!LatchBlock) {
00168     DEBUG(dbgs() << "  Can't unroll; loop exit-block-insertion failed.\n");
00169     return false;
00170   }
00171 
00172   // Loops with indirectbr cannot be cloned.
00173   if (!L->isSafeToClone()) {
00174     DEBUG(dbgs() << "  Can't unroll; Loop body cannot be cloned.\n");
00175     return false;
00176   }
00177 
00178   BasicBlock *Header = L->getHeader();
00179   BranchInst *BI = dyn_cast<BranchInst>(LatchBlock->getTerminator());
00180 
00181   if (!BI || BI->isUnconditional()) {
00182     // The loop-rotate pass can be helpful to avoid this in many cases.
00183     DEBUG(dbgs() <<
00184              "  Can't unroll; loop not terminated by a conditional branch.\n");
00185     return false;
00186   }
00187 
00188   if (Header->hasAddressTaken()) {
00189     // The loop-rotate pass can be helpful to avoid this in many cases.
00190     DEBUG(dbgs() <<
00191           "  Won't unroll loop: address of header block is taken.\n");
00192     return false;
00193   }
00194 
00195   if (TripCount != 0)
00196     DEBUG(dbgs() << "  Trip Count = " << TripCount << "\n");
00197   if (TripMultiple != 1)
00198     DEBUG(dbgs() << "  Trip Multiple = " << TripMultiple << "\n");
00199 
00200   // Effectively "DCE" unrolled iterations that are beyond the tripcount
00201   // and will never be executed.
00202   if (TripCount != 0 && Count > TripCount)
00203     Count = TripCount;
00204 
00205   // Don't enter the unroll code if there is nothing to do. This way we don't
00206   // need to support "partial unrolling by 1".
00207   if (TripCount == 0 && Count < 2)
00208     return false;
00209 
00210   assert(Count > 0);
00211   assert(TripMultiple > 0);
00212   assert(TripCount == 0 || TripCount % TripMultiple == 0);
00213 
00214   // Are we eliminating the loop control altogether?
00215   bool CompletelyUnroll = Count == TripCount;
00216 
00217   // We assume a run-time trip count if the compiler cannot
00218   // figure out the loop trip count and the unroll-runtime
00219   // flag is specified.
00220   bool RuntimeTripCount = (TripCount == 0 && Count > 0 && AllowRuntime);
00221 
00222   if (RuntimeTripCount && !UnrollRuntimeLoopProlog(L, Count, LI, LPM))
00223     return false;
00224 
00225   // Notify ScalarEvolution that the loop will be substantially changed,
00226   // if not outright eliminated.
00227   ScalarEvolution *SE =
00228       PP ? PP->getAnalysisIfAvailable<ScalarEvolution>() : nullptr;
00229   if (SE)
00230     SE->forgetLoop(L);
00231 
00232   // If we know the trip count, we know the multiple...
00233   unsigned BreakoutTrip = 0;
00234   if (TripCount != 0) {
00235     BreakoutTrip = TripCount % Count;
00236     TripMultiple = 0;
00237   } else {
00238     // Figure out what multiple to use.
00239     BreakoutTrip = TripMultiple =
00240       (unsigned)GreatestCommonDivisor64(Count, TripMultiple);
00241   }
00242 
00243   // Report the unrolling decision.
00244   DebugLoc LoopLoc = L->getStartLoc();
00245   Function *F = Header->getParent();
00246   LLVMContext &Ctx = F->getContext();
00247 
00248   if (CompletelyUnroll) {
00249     DEBUG(dbgs() << "COMPLETELY UNROLLING loop %" << Header->getName()
00250           << " with trip count " << TripCount << "!\n");
00251     emitOptimizationRemark(Ctx, DEBUG_TYPE, *F, LoopLoc,
00252                            Twine("completely unrolled loop with ") +
00253                                Twine(TripCount) + " iterations");
00254   } else {
00255     auto EmitDiag = [&](const Twine &T) {
00256       emitOptimizationRemark(Ctx, DEBUG_TYPE, *F, LoopLoc,
00257                              "unrolled loop by a factor of " + Twine(Count) +
00258                                  T);
00259     };
00260 
00261     DEBUG(dbgs() << "UNROLLING loop %" << Header->getName()
00262           << " by " << Count);
00263     if (TripMultiple == 0 || BreakoutTrip != TripMultiple) {
00264       DEBUG(dbgs() << " with a breakout at trip " << BreakoutTrip);
00265       EmitDiag(" with a breakout at trip " + Twine(BreakoutTrip));
00266     } else if (TripMultiple != 1) {
00267       DEBUG(dbgs() << " with " << TripMultiple << " trips per branch");
00268       EmitDiag(" with " + Twine(TripMultiple) + " trips per branch");
00269     } else if (RuntimeTripCount) {
00270       DEBUG(dbgs() << " with run-time trip count");
00271       EmitDiag(" with run-time trip count");
00272     }
00273     DEBUG(dbgs() << "!\n");
00274   }
00275 
00276   bool ContinueOnTrue = L->contains(BI->getSuccessor(0));
00277   BasicBlock *LoopExit = BI->getSuccessor(ContinueOnTrue);
00278 
00279   // For the first iteration of the loop, we should use the precloned values for
00280   // PHI nodes.  Insert associations now.
00281   ValueToValueMapTy LastValueMap;
00282   std::vector<PHINode*> OrigPHINode;
00283   for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
00284     OrigPHINode.push_back(cast<PHINode>(I));
00285   }
00286 
00287   std::vector<BasicBlock*> Headers;
00288   std::vector<BasicBlock*> Latches;
00289   Headers.push_back(Header);
00290   Latches.push_back(LatchBlock);
00291 
00292   // The current on-the-fly SSA update requires blocks to be processed in
00293   // reverse postorder so that LastValueMap contains the correct value at each
00294   // exit.
00295   LoopBlocksDFS DFS(L);
00296   DFS.perform(LI);
00297 
00298   // Stash the DFS iterators before adding blocks to the loop.
00299   LoopBlocksDFS::RPOIterator BlockBegin = DFS.beginRPO();
00300   LoopBlocksDFS::RPOIterator BlockEnd = DFS.endRPO();
00301 
00302   for (unsigned It = 1; It != Count; ++It) {
00303     std::vector<BasicBlock*> NewBlocks;
00304     SmallDenseMap<const Loop *, Loop *, 4> NewLoops;
00305     NewLoops[L] = L;
00306 
00307     for (LoopBlocksDFS::RPOIterator BB = BlockBegin; BB != BlockEnd; ++BB) {
00308       ValueToValueMapTy VMap;
00309       BasicBlock *New = CloneBasicBlock(*BB, VMap, "." + Twine(It));
00310       Header->getParent()->getBasicBlockList().push_back(New);
00311 
00312       // Tell LI about New.
00313       if (*BB == Header) {
00314         assert(LI->getLoopFor(*BB) == L && "Header should not be in a sub-loop");
00315         L->addBasicBlockToLoop(New, LI->getBase());
00316       } else {
00317         // Figure out which loop New is in.
00318         const Loop *OldLoop = LI->getLoopFor(*BB);
00319         assert(OldLoop && "Should (at least) be in the loop being unrolled!");
00320 
00321         Loop *&NewLoop = NewLoops[OldLoop];
00322         if (!NewLoop) {
00323           // Found a new sub-loop.
00324           assert(*BB == OldLoop->getHeader() &&
00325                  "Header should be first in RPO");
00326 
00327           Loop *NewLoopParent = NewLoops.lookup(OldLoop->getParentLoop());
00328           assert(NewLoopParent &&
00329                  "Expected parent loop before sub-loop in RPO");
00330           NewLoop = new Loop;
00331           NewLoopParent->addChildLoop(NewLoop);
00332 
00333           // Forget the old loop, since its inputs may have changed.
00334           if (SE)
00335             SE->forgetLoop(OldLoop);
00336         }
00337         NewLoop->addBasicBlockToLoop(New, LI->getBase());
00338       }
00339 
00340       if (*BB == Header)
00341         // Loop over all of the PHI nodes in the block, changing them to use
00342         // the incoming values from the previous block.
00343         for (unsigned i = 0, e = OrigPHINode.size(); i != e; ++i) {
00344           PHINode *NewPHI = cast<PHINode>(VMap[OrigPHINode[i]]);
00345           Value *InVal = NewPHI->getIncomingValueForBlock(LatchBlock);
00346           if (Instruction *InValI = dyn_cast<Instruction>(InVal))
00347             if (It > 1 && L->contains(InValI))
00348               InVal = LastValueMap[InValI];
00349           VMap[OrigPHINode[i]] = InVal;
00350           New->getInstList().erase(NewPHI);
00351         }
00352 
00353       // Update our running map of newest clones
00354       LastValueMap[*BB] = New;
00355       for (ValueToValueMapTy::iterator VI = VMap.begin(), VE = VMap.end();
00356            VI != VE; ++VI)
00357         LastValueMap[VI->first] = VI->second;
00358 
00359       // Add phi entries for newly created values to all exit blocks.
00360       for (succ_iterator SI = succ_begin(*BB), SE = succ_end(*BB);
00361            SI != SE; ++SI) {
00362         if (L->contains(*SI))
00363           continue;
00364         for (BasicBlock::iterator BBI = (*SI)->begin();
00365              PHINode *phi = dyn_cast<PHINode>(BBI); ++BBI) {
00366           Value *Incoming = phi->getIncomingValueForBlock(*BB);
00367           ValueToValueMapTy::iterator It = LastValueMap.find(Incoming);
00368           if (It != LastValueMap.end())
00369             Incoming = It->second;
00370           phi->addIncoming(Incoming, New);
00371         }
00372       }
00373       // Keep track of new headers and latches as we create them, so that
00374       // we can insert the proper branches later.
00375       if (*BB == Header)
00376         Headers.push_back(New);
00377       if (*BB == LatchBlock)
00378         Latches.push_back(New);
00379 
00380       NewBlocks.push_back(New);
00381     }
00382 
00383     // Remap all instructions in the most recent iteration
00384     for (unsigned i = 0; i < NewBlocks.size(); ++i)
00385       for (BasicBlock::iterator I = NewBlocks[i]->begin(),
00386            E = NewBlocks[i]->end(); I != E; ++I)
00387         ::RemapInstruction(I, LastValueMap);
00388   }
00389 
00390   // Loop over the PHI nodes in the original block, setting incoming values.
00391   for (unsigned i = 0, e = OrigPHINode.size(); i != e; ++i) {
00392     PHINode *PN = OrigPHINode[i];
00393     if (CompletelyUnroll) {
00394       PN->replaceAllUsesWith(PN->getIncomingValueForBlock(Preheader));
00395       Header->getInstList().erase(PN);
00396     }
00397     else if (Count > 1) {
00398       Value *InVal = PN->removeIncomingValue(LatchBlock, false);
00399       // If this value was defined in the loop, take the value defined by the
00400       // last iteration of the loop.
00401       if (Instruction *InValI = dyn_cast<Instruction>(InVal)) {
00402         if (L->contains(InValI))
00403           InVal = LastValueMap[InVal];
00404       }
00405       assert(Latches.back() == LastValueMap[LatchBlock] && "bad last latch");
00406       PN->addIncoming(InVal, Latches.back());
00407     }
00408   }
00409 
00410   // Now that all the basic blocks for the unrolled iterations are in place,
00411   // set up the branches to connect them.
00412   for (unsigned i = 0, e = Latches.size(); i != e; ++i) {
00413     // The original branch was replicated in each unrolled iteration.
00414     BranchInst *Term = cast<BranchInst>(Latches[i]->getTerminator());
00415 
00416     // The branch destination.
00417     unsigned j = (i + 1) % e;
00418     BasicBlock *Dest = Headers[j];
00419     bool NeedConditional = true;
00420 
00421     if (RuntimeTripCount && j != 0) {
00422       NeedConditional = false;
00423     }
00424 
00425     // For a complete unroll, make the last iteration end with a branch
00426     // to the exit block.
00427     if (CompletelyUnroll && j == 0) {
00428       Dest = LoopExit;
00429       NeedConditional = false;
00430     }
00431 
00432     // If we know the trip count or a multiple of it, we can safely use an
00433     // unconditional branch for some iterations.
00434     if (j != BreakoutTrip && (TripMultiple == 0 || j % TripMultiple != 0)) {
00435       NeedConditional = false;
00436     }
00437 
00438     if (NeedConditional) {
00439       // Update the conditional branch's successor for the following
00440       // iteration.
00441       Term->setSuccessor(!ContinueOnTrue, Dest);
00442     } else {
00443       // Remove phi operands at this loop exit
00444       if (Dest != LoopExit) {
00445         BasicBlock *BB = Latches[i];
00446         for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
00447              SI != SE; ++SI) {
00448           if (*SI == Headers[i])
00449             continue;
00450           for (BasicBlock::iterator BBI = (*SI)->begin();
00451                PHINode *Phi = dyn_cast<PHINode>(BBI); ++BBI) {
00452             Phi->removeIncomingValue(BB, false);
00453           }
00454         }
00455       }
00456       // Replace the conditional branch with an unconditional one.
00457       BranchInst::Create(Dest, Term);
00458       Term->eraseFromParent();
00459     }
00460   }
00461 
00462   // Merge adjacent basic blocks, if possible.
00463   SmallPtrSet<Loop *, 4> ForgottenLoops;
00464   for (unsigned i = 0, e = Latches.size(); i != e; ++i) {
00465     BranchInst *Term = cast<BranchInst>(Latches[i]->getTerminator());
00466     if (Term->isUnconditional()) {
00467       BasicBlock *Dest = Term->getSuccessor(0);
00468       if (BasicBlock *Fold = FoldBlockIntoPredecessor(Dest, LI, LPM,
00469                                                       ForgottenLoops))
00470         std::replace(Latches.begin(), Latches.end(), Dest, Fold);
00471     }
00472   }
00473 
00474   // FIXME: We could register any cloned assumptions instead of clearing the
00475   // whole function's cache.
00476   AT->forgetCachedAssumptions(F);
00477 
00478   DominatorTree *DT = nullptr;
00479   if (PP) {
00480     // FIXME: Reconstruct dom info, because it is not preserved properly.
00481     // Incrementally updating domtree after loop unrolling would be easy.
00482     if (DominatorTreeWrapperPass *DTWP =
00483             PP->getAnalysisIfAvailable<DominatorTreeWrapperPass>()) {
00484       DT = &DTWP->getDomTree();
00485       DT->recalculate(*L->getHeader()->getParent());
00486     }
00487 
00488     // Simplify any new induction variables in the partially unrolled loop.
00489     if (SE && !CompletelyUnroll) {
00490       SmallVector<WeakVH, 16> DeadInsts;
00491       simplifyLoopIVs(L, SE, LPM, DeadInsts);
00492 
00493       // Aggressively clean up dead instructions that simplifyLoopIVs already
00494       // identified. Any remaining should be cleaned up below.
00495       while (!DeadInsts.empty())
00496         if (Instruction *Inst =
00497             dyn_cast_or_null<Instruction>(&*DeadInsts.pop_back_val()))
00498           RecursivelyDeleteTriviallyDeadInstructions(Inst);
00499     }
00500   }
00501   // At this point, the code is well formed.  We now do a quick sweep over the
00502   // inserted code, doing constant propagation and dead code elimination as we
00503   // go.
00504   const std::vector<BasicBlock*> &NewLoopBlocks = L->getBlocks();
00505   for (std::vector<BasicBlock*>::const_iterator BB = NewLoopBlocks.begin(),
00506        BBE = NewLoopBlocks.end(); BB != BBE; ++BB)
00507     for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); I != E; ) {
00508       Instruction *Inst = I++;
00509 
00510       if (isInstructionTriviallyDead(Inst))
00511         (*BB)->getInstList().erase(Inst);
00512       else if (Value *V = SimplifyInstruction(Inst))
00513         if (LI->replacementPreservesLCSSAForm(Inst, V)) {
00514           Inst->replaceAllUsesWith(V);
00515           (*BB)->getInstList().erase(Inst);
00516         }
00517     }
00518 
00519   NumCompletelyUnrolled += CompletelyUnroll;
00520   ++NumUnrolled;
00521 
00522   Loop *OuterL = L->getParentLoop();
00523   // Remove the loop from the LoopPassManager if it's completely removed.
00524   if (CompletelyUnroll && LPM != nullptr)
00525     LPM->deleteLoopFromQueue(L);
00526 
00527   // If we have a pass and a DominatorTree we should re-simplify impacted loops
00528   // to ensure subsequent analyses can rely on this form. We want to simplify
00529   // at least one layer outside of the loop that was unrolled so that any
00530   // changes to the parent loop exposed by the unrolling are considered.
00531   if (PP && DT) {
00532     if (!OuterL && !CompletelyUnroll)
00533       OuterL = L;
00534     if (OuterL) {
00535       DataLayoutPass *DLP = PP->getAnalysisIfAvailable<DataLayoutPass>();
00536       const DataLayout *DL = DLP ? &DLP->getDataLayout() : nullptr;
00537       simplifyLoop(OuterL, DT, LI, PP, /*AliasAnalysis*/ nullptr, SE, DL, AT);
00538 
00539       // LCSSA must be performed on the outermost affected loop. The unrolled
00540       // loop's last loop latch is guaranteed to be in the outermost loop after
00541       // deleteLoopFromQueue updates LoopInfo.
00542       Loop *LatchLoop = LI->getLoopFor(Latches.back());
00543       if (!OuterL->contains(LatchLoop))
00544         while (OuterL->getParentLoop() != LatchLoop)
00545           OuterL = OuterL->getParentLoop();
00546 
00547       formLCSSARecursively(*OuterL, *DT, SE);
00548     }
00549   }
00550 
00551   return true;
00552 }