1 //===- LoopInfo.cpp - Natural Loop Calculator -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the LoopInfo class that is used to identify natural loops
11 // and determine the loop depth of various nodes of the CFG. Note that the
12 // loops identified may actually be several natural loops that share the same
13 // header node... not just a single natural loop.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/Constants.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Analysis/Dominators.h"
21 #include "llvm/Analysis/LoopIterator.h"
22 #include "llvm/Assembly/Writer.h"
23 #include "llvm/Support/CFG.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/ADT/DepthFirstIterator.h"
27 #include "llvm/ADT/SmallPtrSet.h"
31 // Always verify loopinfo if expensive checking is enabled.
33 static bool VerifyLoopInfo = true;
35 static bool VerifyLoopInfo = false;
37 static cl::opt<bool,true>
38 VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo),
39 cl::desc("Verify loop info (time consuming)"));
41 char LoopInfo::ID = 0;
42 INITIALIZE_PASS_BEGIN(LoopInfo, "loops", "Natural Loop Information", true, true)
43 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
44 INITIALIZE_PASS_END(LoopInfo, "loops", "Natural Loop Information", true, true)
46 //===----------------------------------------------------------------------===//
47 // Loop implementation
50 /// isLoopInvariant - Return true if the specified value is loop invariant
52 bool Loop::isLoopInvariant(Value *V) const {
53 if (Instruction *I = dyn_cast<Instruction>(V))
55 return true; // All non-instructions are loop invariant
58 /// hasLoopInvariantOperands - Return true if all the operands of the
59 /// specified instruction are loop invariant.
60 bool Loop::hasLoopInvariantOperands(Instruction *I) const {
61 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
62 if (!isLoopInvariant(I->getOperand(i)))
68 /// makeLoopInvariant - If the given value is an instruciton inside of the
69 /// loop and it can be hoisted, do so to make it trivially loop-invariant.
70 /// Return true if the value after any hoisting is loop invariant. This
71 /// function can be used as a slightly more aggressive replacement for
74 /// If InsertPt is specified, it is the point to hoist instructions to.
75 /// If null, the terminator of the loop preheader is used.
77 bool Loop::makeLoopInvariant(Value *V, bool &Changed,
78 Instruction *InsertPt) const {
79 if (Instruction *I = dyn_cast<Instruction>(V))
80 return makeLoopInvariant(I, Changed, InsertPt);
81 return true; // All non-instructions are loop-invariant.
84 /// makeLoopInvariant - If the given instruction is inside of the
85 /// loop and it can be hoisted, do so to make it trivially loop-invariant.
86 /// Return true if the instruction after any hoisting is loop invariant. This
87 /// function can be used as a slightly more aggressive replacement for
90 /// If InsertPt is specified, it is the point to hoist instructions to.
91 /// If null, the terminator of the loop preheader is used.
93 bool Loop::makeLoopInvariant(Instruction *I, bool &Changed,
94 Instruction *InsertPt) const {
95 // Test if the value is already loop-invariant.
96 if (isLoopInvariant(I))
98 if (!I->isSafeToSpeculativelyExecute())
100 if (I->mayReadFromMemory())
102 // Determine the insertion point, unless one was given.
104 BasicBlock *Preheader = getLoopPreheader();
105 // Without a preheader, hoisting is not feasible.
108 InsertPt = Preheader->getTerminator();
110 // Don't hoist instructions with loop-variant operands.
111 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
112 if (!makeLoopInvariant(I->getOperand(i), Changed, InsertPt))
116 I->moveBefore(InsertPt);
121 /// getCanonicalInductionVariable - Check to see if the loop has a canonical
122 /// induction variable: an integer recurrence that starts at 0 and increments
123 /// by one each time through the loop. If so, return the phi node that
124 /// corresponds to it.
126 /// The IndVarSimplify pass transforms loops to have a canonical induction
129 PHINode *Loop::getCanonicalInductionVariable() const {
130 BasicBlock *H = getHeader();
132 BasicBlock *Incoming = 0, *Backedge = 0;
133 pred_iterator PI = pred_begin(H);
134 assert(PI != pred_end(H) &&
135 "Loop must have at least one backedge!");
137 if (PI == pred_end(H)) return 0; // dead loop
139 if (PI != pred_end(H)) return 0; // multiple backedges?
141 if (contains(Incoming)) {
142 if (contains(Backedge))
144 std::swap(Incoming, Backedge);
145 } else if (!contains(Backedge))
148 // Loop over all of the PHI nodes, looking for a canonical indvar.
149 for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) {
150 PHINode *PN = cast<PHINode>(I);
151 if (ConstantInt *CI =
152 dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming)))
153 if (CI->isNullValue())
154 if (Instruction *Inc =
155 dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge)))
156 if (Inc->getOpcode() == Instruction::Add &&
157 Inc->getOperand(0) == PN)
158 if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
159 if (CI->equalsInt(1))
165 /// getTripCount - Return a loop-invariant LLVM value indicating the number of
166 /// times the loop will be executed. Note that this means that the backedge
167 /// of the loop executes N-1 times. If the trip-count cannot be determined,
168 /// this returns null.
170 /// The IndVarSimplify pass transforms loops to have a form that this
171 /// function easily understands.
173 Value *Loop::getTripCount() const {
174 // Canonical loops will end with a 'cmp ne I, V', where I is the incremented
175 // canonical induction variable and V is the trip count of the loop.
176 PHINode *IV = getCanonicalInductionVariable();
177 if (IV == 0 || IV->getNumIncomingValues() != 2) return 0;
179 bool P0InLoop = contains(IV->getIncomingBlock(0));
180 Value *Inc = IV->getIncomingValue(!P0InLoop);
181 BasicBlock *BackedgeBlock = IV->getIncomingBlock(!P0InLoop);
183 if (BranchInst *BI = dyn_cast<BranchInst>(BackedgeBlock->getTerminator()))
184 if (BI->isConditional()) {
185 if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) {
186 if (ICI->getOperand(0) == Inc) {
187 if (BI->getSuccessor(0) == getHeader()) {
188 if (ICI->getPredicate() == ICmpInst::ICMP_NE)
189 return ICI->getOperand(1);
190 } else if (ICI->getPredicate() == ICmpInst::ICMP_EQ) {
191 return ICI->getOperand(1);
200 /// getSmallConstantTripCount - Returns the trip count of this loop as a
201 /// normal unsigned value, if possible. Returns 0 if the trip count is unknown
202 /// or not constant. Will also return 0 if the trip count is very large
204 unsigned Loop::getSmallConstantTripCount() const {
205 Value* TripCount = this->getTripCount();
207 if (ConstantInt *TripCountC = dyn_cast<ConstantInt>(TripCount)) {
208 // Guard against huge trip counts.
209 if (TripCountC->getValue().getActiveBits() <= 32) {
210 return (unsigned)TripCountC->getZExtValue();
217 /// getSmallConstantTripMultiple - Returns the largest constant divisor of the
218 /// trip count of this loop as a normal unsigned value, if possible. This
219 /// means that the actual trip count is always a multiple of the returned
220 /// value (don't forget the trip count could very well be zero as well!).
222 /// Returns 1 if the trip count is unknown or not guaranteed to be the
223 /// multiple of a constant (which is also the case if the trip count is simply
224 /// constant, use getSmallConstantTripCount for that case), Will also return 1
225 /// if the trip count is very large (>= 2^32).
226 unsigned Loop::getSmallConstantTripMultiple() const {
227 Value* TripCount = this->getTripCount();
228 // This will hold the ConstantInt result, if any
229 ConstantInt *Result = NULL;
231 // See if the trip count is constant itself
232 Result = dyn_cast<ConstantInt>(TripCount);
233 // if not, see if it is a multiplication
235 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TripCount)) {
236 switch (BO->getOpcode()) {
237 case BinaryOperator::Mul:
238 Result = dyn_cast<ConstantInt>(BO->getOperand(1));
240 case BinaryOperator::Shl:
241 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1)))
242 if (CI->getValue().getActiveBits() <= 5)
243 return 1u << CI->getZExtValue();
250 // Guard against huge trip counts.
251 if (Result && Result->getValue().getActiveBits() <= 32) {
252 return (unsigned)Result->getZExtValue();
258 /// isLCSSAForm - Return true if the Loop is in LCSSA form
259 bool Loop::isLCSSAForm(DominatorTree &DT) const {
260 // Sort the blocks vector so that we can use binary search to do quick
262 SmallPtrSet<BasicBlock*, 16> LoopBBs(block_begin(), block_end());
264 for (block_iterator BI = block_begin(), E = block_end(); BI != E; ++BI) {
265 BasicBlock *BB = *BI;
266 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;++I)
267 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;
270 BasicBlock *UserBB = cast<Instruction>(U)->getParent();
271 if (PHINode *P = dyn_cast<PHINode>(U))
272 UserBB = P->getIncomingBlock(UI);
274 // Check the current block, as a fast-path, before checking whether
275 // the use is anywhere in the loop. Most values are used in the same
276 // block they are defined in. Also, blocks not reachable from the
277 // entry are special; uses in them don't need to go through PHIs.
279 !LoopBBs.count(UserBB) &&
280 DT.isReachableFromEntry(UserBB))
288 /// isLoopSimplifyForm - Return true if the Loop is in the form that
289 /// the LoopSimplify form transforms loops to, which is sometimes called
291 bool Loop::isLoopSimplifyForm() const {
292 // Normal-form loops have a preheader, a single backedge, and all of their
293 // exits have all their predecessors inside the loop.
294 return getLoopPreheader() && getLoopLatch() && hasDedicatedExits();
297 /// hasDedicatedExits - Return true if no exit block for the loop
298 /// has a predecessor that is outside the loop.
299 bool Loop::hasDedicatedExits() const {
300 // Sort the blocks vector so that we can use binary search to do quick
302 SmallPtrSet<BasicBlock *, 16> LoopBBs(block_begin(), block_end());
303 // Each predecessor of each exit block of a normal loop is contained
305 SmallVector<BasicBlock *, 4> ExitBlocks;
306 getExitBlocks(ExitBlocks);
307 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
308 for (pred_iterator PI = pred_begin(ExitBlocks[i]),
309 PE = pred_end(ExitBlocks[i]); PI != PE; ++PI)
310 if (!LoopBBs.count(*PI))
312 // All the requirements are met.
316 /// getUniqueExitBlocks - Return all unique successor blocks of this loop.
317 /// These are the blocks _outside of the current loop_ which are branched to.
318 /// This assumes that loop exits are in canonical form.
321 Loop::getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const {
322 assert(hasDedicatedExits() &&
323 "getUniqueExitBlocks assumes the loop has canonical form exits!");
325 // Sort the blocks vector so that we can use binary search to do quick
327 SmallVector<BasicBlock *, 128> LoopBBs(block_begin(), block_end());
328 std::sort(LoopBBs.begin(), LoopBBs.end());
330 SmallVector<BasicBlock *, 32> switchExitBlocks;
332 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI) {
334 BasicBlock *current = *BI;
335 switchExitBlocks.clear();
337 for (succ_iterator I = succ_begin(*BI), E = succ_end(*BI); I != E; ++I) {
338 // If block is inside the loop then it is not a exit block.
339 if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
342 pred_iterator PI = pred_begin(*I);
343 BasicBlock *firstPred = *PI;
345 // If current basic block is this exit block's first predecessor
346 // then only insert exit block in to the output ExitBlocks vector.
347 // This ensures that same exit block is not inserted twice into
348 // ExitBlocks vector.
349 if (current != firstPred)
352 // If a terminator has more then two successors, for example SwitchInst,
353 // then it is possible that there are multiple edges from current block
354 // to one exit block.
355 if (std::distance(succ_begin(current), succ_end(current)) <= 2) {
356 ExitBlocks.push_back(*I);
360 // In case of multiple edges from current block to exit block, collect
361 // only one edge in ExitBlocks. Use switchExitBlocks to keep track of
363 if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), *I)
364 == switchExitBlocks.end()) {
365 switchExitBlocks.push_back(*I);
366 ExitBlocks.push_back(*I);
372 /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one
373 /// block, return that block. Otherwise return null.
374 BasicBlock *Loop::getUniqueExitBlock() const {
375 SmallVector<BasicBlock *, 8> UniqueExitBlocks;
376 getUniqueExitBlocks(UniqueExitBlocks);
377 if (UniqueExitBlocks.size() == 1)
378 return UniqueExitBlocks[0];
382 void Loop::dump() const {
386 //===----------------------------------------------------------------------===//
387 // UnloopUpdater implementation
390 /// Find the new parent loop for all blocks within the "unloop" whose last
391 /// backedges has just been removed.
392 class UnloopUpdater {
398 // Map unloop's immediate subloops to their nearest reachable parents. Nested
399 // loops within these subloops will not change parents. However, an immediate
400 // subloop's new parent will be the nearest loop reachable from either its own
401 // exits *or* any of its nested loop's exits.
402 DenseMap<Loop*, Loop*> SubloopParents;
404 // Flag the presence of an irreducible backedge whose destination is a block
405 // directly contained by the original unloop.
409 UnloopUpdater(Loop *UL, LoopInfo *LInfo) :
410 Unloop(UL), LI(LInfo), DFS(UL), FoundIB(false) {}
412 void updateBlockParents();
414 void updateSubloopParents();
417 Loop *getNearestLoop(BasicBlock *BB, Loop *BBLoop);
420 /// updateBlockParents - Update the parent loop for all blocks that are directly
421 /// contained within the original "unloop".
422 void UnloopUpdater::updateBlockParents() {
423 if (Unloop->getNumBlocks()) {
424 // Perform a post order CFG traversal of all blocks within this loop,
425 // propagating the nearest loop from sucessors to predecessors.
426 LoopBlocksTraversal Traversal(DFS, LI);
427 for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(),
428 POE = Traversal.end(); POI != POE; ++POI) {
430 Loop *L = LI->getLoopFor(*POI);
431 Loop *NL = getNearestLoop(*POI, L);
434 // For reducible loops, NL is now an ancestor of Unloop.
435 assert((NL != Unloop && (!NL || NL->contains(Unloop))) &&
436 "uninitialized successor");
437 LI->changeLoopFor(*POI, NL);
440 // Or the current block is part of a subloop, in which case its parent
442 assert((FoundIB || Unloop->contains(L)) && "uninitialized successor");
446 // Each irreducible loop within the unloop induces a round of iteration using
447 // the DFS result cached by Traversal.
448 bool Changed = FoundIB;
449 for (unsigned NIters = 0; Changed; ++NIters) {
450 assert(NIters < Unloop->getNumBlocks() && "runaway iterative algorithm");
452 // Iterate over the postorder list of blocks, propagating the nearest loop
453 // from successors to predecessors as before.
455 for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(),
456 POE = DFS.endPostorder(); POI != POE; ++POI) {
458 Loop *L = LI->getLoopFor(*POI);
459 Loop *NL = getNearestLoop(*POI, L);
461 assert(NL != Unloop && (!NL || NL->contains(Unloop)) &&
462 "uninitialized successor");
463 LI->changeLoopFor(*POI, NL);
470 /// updateSubloopParents - Update the parent loop for all subloops directly
471 /// nested within unloop.
472 void UnloopUpdater::updateSubloopParents() {
473 while (!Unloop->empty()) {
474 Loop *Subloop = *(Unloop->end()-1);
475 Unloop->removeChildLoop(Unloop->end()-1);
477 assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop");
478 if (SubloopParents[Subloop])
479 SubloopParents[Subloop]->addChildLoop(Subloop);
483 /// getNearestLoop - Return the nearest parent loop among this block's
484 /// successors. If a successor is a subloop header, consider its parent to be
485 /// the nearest parent of the subloop's exits.
487 /// For subloop blocks, simply update SubloopParents and return NULL.
488 Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) {
490 // Initialy for blocks directly contained by Unloop, NearLoop == Unloop and is
491 // considered uninitialized.
492 Loop *NearLoop = BBLoop;
495 if (NearLoop != Unloop && Unloop->contains(NearLoop)) {
497 // Find the subloop ancestor that is directly contained within Unloop.
498 while (Subloop->getParentLoop() != Unloop) {
499 Subloop = Subloop->getParentLoop();
500 assert(Subloop && "subloop is not an ancestor of the original loop");
502 // Get the current nearest parent of the Subloop exits, initially Unloop.
503 if (!SubloopParents.count(Subloop))
504 SubloopParents[Subloop] = Unloop;
505 NearLoop = SubloopParents[Subloop];
508 succ_iterator I = succ_begin(BB), E = succ_end(BB);
510 assert(!Subloop && "subloop blocks must have a successor");
511 NearLoop = 0; // unloop blocks may now exit the function.
513 for (; I != E; ++I) {
515 continue; // self loops are uninteresting
517 Loop *L = LI->getLoopFor(*I);
519 // This successor has not been processed. This path must lead to an
520 // irreducible backedge.
521 assert((FoundIB || !DFS.hasPostorder(*I)) && "should have seen IB");
524 if (L != Unloop && Unloop->contains(L)) {
525 // Successor is in a subloop.
527 continue; // Branching within subloops. Ignore it.
529 // BB branches from the original into a subloop header.
530 assert(L->getParentLoop() == Unloop && "cannot skip into nested loops");
532 // Get the current nearest parent of the Subloop's exits.
533 L = SubloopParents[L];
534 // L could be Unloop if the only exit was an irreducible backedge.
539 // Handle critical edges from Unloop into a sibling loop.
540 if (L && !L->contains(Unloop)) {
541 L = L->getParentLoop();
543 // Remember the nearest parent loop among successors or subloop exits.
544 if (NearLoop == Unloop || !NearLoop || NearLoop->contains(L))
548 SubloopParents[Subloop] = NearLoop;
554 //===----------------------------------------------------------------------===//
555 // LoopInfo implementation
557 bool LoopInfo::runOnFunction(Function &) {
559 LI.Calculate(getAnalysis<DominatorTree>().getBase()); // Update
563 /// updateUnloop - The last backedge has been removed from a loop--now the
564 /// "unloop". Find a new parent for the blocks contained within unloop and
565 /// update the loop tree. We don't necessarilly have valid dominators at this
566 /// point, but LoopInfo is still valid except for the removal of this loop.
568 /// Note that Unloop may now be an empty loop. Calling Loop::getHeader without
569 /// checking first is illegal.
570 void LoopInfo::updateUnloop(Loop *Unloop) {
572 // First handle the special case of no parent loop to simplify the algorithm.
573 if (!Unloop->getParentLoop()) {
574 // Since BBLoop had no parent, Unloop blocks are no longer in a loop.
575 for (Loop::block_iterator I = Unloop->block_begin(),
576 E = Unloop->block_end(); I != E; ++I) {
578 // Don't reparent blocks in subloops.
579 if (getLoopFor(*I) != Unloop)
582 // Blocks no longer have a parent but are still referenced by Unloop until
583 // the Unloop object is deleted.
584 LI.changeLoopFor(*I, 0);
587 // Remove the loop from the top-level LoopInfo object.
588 for (LoopInfo::iterator I = LI.begin(), E = LI.end();; ++I) {
589 assert(I != E && "Couldn't find loop");
596 // Move all of the subloops to the top-level.
597 while (!Unloop->empty())
598 LI.addTopLevelLoop(Unloop->removeChildLoop(Unloop->end()-1));
603 // Update the parent loop for all blocks within the loop. Blocks within
604 // subloops will not change parents.
605 UnloopUpdater Updater(Unloop, this);
606 Updater.updateBlockParents();
608 // Remove unloop's blocks from all ancestors below their new parents.
609 for (Loop::block_iterator BI = Unloop->block_begin(),
610 BE = Unloop->block_end(); BI != BE; ++BI) {
611 Loop *NewParent = getLoopFor(*BI);
612 // If this block is in a subloop, skip it.
613 if (Unloop->contains(NewParent))
616 // Remove blocks from former Ancestors except Unloop itself which will be
618 for (Loop *OldParent = Unloop->getParentLoop(); OldParent != NewParent;
619 OldParent = OldParent->getParentLoop()) {
620 assert(OldParent && "new loop is not an ancestor of the original");
621 OldParent->removeBlockFromLoop(*BI);
625 // Add direct subloops as children in their new parent loop.
626 Updater.updateSubloopParents();
628 // Remove unloop from its parent loop.
629 Loop *ParentLoop = Unloop->getParentLoop();
630 for (Loop::iterator I = ParentLoop->begin(), E = ParentLoop->end();; ++I) {
631 assert(I != E && "Couldn't find loop");
633 ParentLoop->removeChildLoop(I);
639 void LoopInfo::verifyAnalysis() const {
640 // LoopInfo is a FunctionPass, but verifying every loop in the function
641 // each time verifyAnalysis is called is very expensive. The
642 // -verify-loop-info option can enable this. In order to perform some
643 // checking by default, LoopPass has been taught to call verifyLoop
644 // manually during loop pass sequences.
646 if (!VerifyLoopInfo) return;
648 for (iterator I = begin(), E = end(); I != E; ++I) {
649 assert(!(*I)->getParentLoop() && "Top-level loop has a parent!");
650 (*I)->verifyLoopNest();
653 // TODO: check BBMap consistency.
656 void LoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
657 AU.setPreservesAll();
658 AU.addRequired<DominatorTree>();
661 void LoopInfo::print(raw_ostream &OS, const Module*) const {
665 //===----------------------------------------------------------------------===//
666 // LoopBlocksDFS implementation
669 /// Traverse the loop blocks and store the DFS result.
670 /// Useful for clients that just want the final DFS result and don't need to
671 /// visit blocks during the initial traversal.
672 void LoopBlocksDFS::perform(LoopInfo *LI) {
673 LoopBlocksTraversal Traversal(*this, LI);
674 for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(),
675 POE = Traversal.end(); POI != POE; ++POI) ;