An algorithm for incrementally updating LoopInfo within a
[oota-llvm.git] / lib / Analysis / LoopInfo.cpp
1 //===- LoopInfo.cpp - Natural Loop Calculator -----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
14 //
15 //===----------------------------------------------------------------------===//
16
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"
28 #include <algorithm>
29 using namespace llvm;
30
31 // Always verify loopinfo if expensive checking is enabled.
32 #ifdef XDEBUG
33 static bool VerifyLoopInfo = true;
34 #else
35 static bool VerifyLoopInfo = false;
36 #endif
37 static cl::opt<bool,true>
38 VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo),
39                 cl::desc("Verify loop info (time consuming)"));
40
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)
45
46 //===----------------------------------------------------------------------===//
47 // Loop implementation
48 //
49
50 /// isLoopInvariant - Return true if the specified value is loop invariant
51 ///
52 bool Loop::isLoopInvariant(Value *V) const {
53   if (Instruction *I = dyn_cast<Instruction>(V))
54     return !contains(I);
55   return true;  // All non-instructions are loop invariant
56 }
57
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)))
63       return false;
64
65   return true;
66 }
67
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
72 /// isLoopInvariant.
73 ///
74 /// If InsertPt is specified, it is the point to hoist instructions to.
75 /// If null, the terminator of the loop preheader is used.
76 ///
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.
82 }
83
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
88 /// isLoopInvariant.
89 ///
90 /// If InsertPt is specified, it is the point to hoist instructions to.
91 /// If null, the terminator of the loop preheader is used.
92 ///
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))
97     return true;
98   if (!I->isSafeToSpeculativelyExecute())
99     return false;
100   if (I->mayReadFromMemory())
101     return false;
102   // Determine the insertion point, unless one was given.
103   if (!InsertPt) {
104     BasicBlock *Preheader = getLoopPreheader();
105     // Without a preheader, hoisting is not feasible.
106     if (!Preheader)
107       return false;
108     InsertPt = Preheader->getTerminator();
109   }
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))
113       return false;
114
115   // Hoist.
116   I->moveBefore(InsertPt);
117   Changed = true;
118   return true;
119 }
120
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.
125 ///
126 /// The IndVarSimplify pass transforms loops to have a canonical induction
127 /// variable.
128 ///
129 PHINode *Loop::getCanonicalInductionVariable() const {
130   BasicBlock *H = getHeader();
131
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!");
136   Backedge = *PI++;
137   if (PI == pred_end(H)) return 0;  // dead loop
138   Incoming = *PI++;
139   if (PI != pred_end(H)) return 0;  // multiple backedges?
140
141   if (contains(Incoming)) {
142     if (contains(Backedge))
143       return 0;
144     std::swap(Incoming, Backedge);
145   } else if (!contains(Backedge))
146     return 0;
147
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))
160                 return PN;
161   }
162   return 0;
163 }
164
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.
169 ///
170 /// The IndVarSimplify pass transforms loops to have a form that this
171 /// function easily understands.
172 ///
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;
178
179   bool P0InLoop = contains(IV->getIncomingBlock(0));
180   Value *Inc = IV->getIncomingValue(!P0InLoop);
181   BasicBlock *BackedgeBlock = IV->getIncomingBlock(!P0InLoop);
182
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);
192           }
193         }
194       }
195     }
196
197   return 0;
198 }
199
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
203 /// (>= 2^32)
204 unsigned Loop::getSmallConstantTripCount() const {
205   Value* TripCount = this->getTripCount();
206   if (TripCount) {
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();
211       }
212     }
213   }
214   return 0;
215 }
216
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!).
221 ///
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;
230   if (TripCount) {
231     // See if the trip count is constant itself
232     Result = dyn_cast<ConstantInt>(TripCount);
233     // if not, see if it is a multiplication
234     if (!Result)
235       if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TripCount)) {
236         switch (BO->getOpcode()) {
237         case BinaryOperator::Mul:
238           Result = dyn_cast<ConstantInt>(BO->getOperand(1));
239           break;
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();
244           break;
245         default:
246           break;
247         }
248       }
249   }
250   // Guard against huge trip counts.
251   if (Result && Result->getValue().getActiveBits() <= 32) {
252     return (unsigned)Result->getZExtValue();
253   } else {
254     return 1;
255   }
256 }
257
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
261   // lookups.
262   SmallPtrSet<BasicBlock*, 16> LoopBBs(block_begin(), block_end());
263
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;
268            ++UI) {
269         User *U = *UI;
270         BasicBlock *UserBB = cast<Instruction>(U)->getParent();
271         if (PHINode *P = dyn_cast<PHINode>(U))
272           UserBB = P->getIncomingBlock(UI);
273
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.
278         if (UserBB != BB &&
279             !LoopBBs.count(UserBB) &&
280             DT.isReachableFromEntry(UserBB))
281           return false;
282       }
283   }
284
285   return true;
286 }
287
288 /// isLoopSimplifyForm - Return true if the Loop is in the form that
289 /// the LoopSimplify form transforms loops to, which is sometimes called
290 /// normal form.
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();
295 }
296
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
301   // lookups.
302   SmallPtrSet<BasicBlock *, 16> LoopBBs(block_begin(), block_end());
303   // Each predecessor of each exit block of a normal loop is contained
304   // within the loop.
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))
311         return false;
312   // All the requirements are met.
313   return true;
314 }
315
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.
319 ///
320 void
321 Loop::getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const {
322   assert(hasDedicatedExits() &&
323          "getUniqueExitBlocks assumes the loop has canonical form exits!");
324
325   // Sort the blocks vector so that we can use binary search to do quick
326   // lookups.
327   SmallVector<BasicBlock *, 128> LoopBBs(block_begin(), block_end());
328   std::sort(LoopBBs.begin(), LoopBBs.end());
329
330   SmallVector<BasicBlock *, 32> switchExitBlocks;
331
332   for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI) {
333
334     BasicBlock *current = *BI;
335     switchExitBlocks.clear();
336
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))
340         continue;
341
342       pred_iterator PI = pred_begin(*I);
343       BasicBlock *firstPred = *PI;
344
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)
350         continue;
351
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);
357         continue;
358       }
359
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
362       // duplicate edges.
363       if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), *I)
364           == switchExitBlocks.end()) {
365         switchExitBlocks.push_back(*I);
366         ExitBlocks.push_back(*I);
367       }
368     }
369   }
370 }
371
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];
379   return 0;
380 }
381
382 void Loop::dump() const {
383   print(dbgs());
384 }
385
386 //===----------------------------------------------------------------------===//
387 // UnloopUpdater implementation
388 //
389
390 /// Find the new parent loop for all blocks within the "unloop" whose last
391 /// backedges has just been removed.
392 class UnloopUpdater {
393   Loop *Unloop;
394   LoopInfo *LI;
395
396   LoopBlocksDFS DFS;
397
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;
403
404   // Flag the presence of an irreducible backedge whose destination is a block
405   // directly contained by the original unloop.
406   bool FoundIB;
407
408 public:
409   UnloopUpdater(Loop *UL, LoopInfo *LInfo) :
410     Unloop(UL), LI(LInfo), DFS(UL), FoundIB(false) {}
411
412   void updateBlockParents();
413
414   void updateSubloopParents();
415
416 protected:
417   Loop *getNearestLoop(BasicBlock *BB, Loop *BBLoop);
418 };
419
420 /// updateBlockParents - Update the parent loop for all blocks that are directly
421 /// contained within the original "unloop".
422 void UnloopUpdater::updateBlockParents() {
423   {
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) {
429
430       Loop *L = LI->getLoopFor(*POI);
431       Loop *NL = getNearestLoop(*POI, L);
432
433       if (NL != 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);
438       }
439       else {
440         // Or the current block is part of a subloop, in which case its parent
441         // is unchanged.
442         assert((FoundIB || Unloop->contains(L)) && "uninitialized successor");
443       }
444     }
445   }
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");
451
452     // Iterate over the postorder list of blocks, propagating the nearest loop
453     // from successors to predecessors as before.
454     Changed = false;
455     for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(),
456            POE = DFS.endPostorder(); POI != POE; ++POI) {
457
458       Loop *L = LI->getLoopFor(*POI);
459       Loop *NL = getNearestLoop(*POI, L);
460       if (NL != L) {
461         assert(NL != Unloop && (!NL || NL->contains(Unloop)) &&
462                "uninitialized successor");
463         LI->changeLoopFor(*POI, NL);
464         Changed = true;
465       }
466     }
467   }
468 }
469
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);
476
477     assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop");
478     if (SubloopParents[Subloop])
479       SubloopParents[Subloop]->addChildLoop(Subloop);
480   }
481 }
482
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.
486 ///
487 /// For subloop blocks, simply update SubloopParents and return NULL.
488 Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) {
489
490   // Initialy for blocks directly contained by Unloop, NearLoop == Unloop and is
491   // considered uninitialized.
492   Loop *NearLoop = BBLoop;
493
494   Loop *Subloop = 0;
495   if (NearLoop != Unloop && Unloop->contains(NearLoop)) {
496     Subloop = 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");
501     }
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];
506   }
507
508   succ_iterator I = succ_begin(BB), E = succ_end(BB);
509   if (I == E) {
510     assert(!Subloop && "subloop blocks must have a successor");
511     NearLoop = 0; // unloop blocks may now exit the function.
512   }
513   for (; I != E; ++I) {
514     if (*I == BB)
515       continue; // self loops are uninteresting
516
517     Loop *L = LI->getLoopFor(*I);
518     if (L == Unloop) {
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");
522       FoundIB = true;
523     }
524     if (L != Unloop && Unloop->contains(L)) {
525       // Successor is in a subloop.
526       if (Subloop)
527         continue; // Branching within subloops. Ignore it.
528
529       // BB branches from the original into a subloop header.
530       assert(L->getParentLoop() == Unloop && "cannot skip into nested loops");
531
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.
535     }
536     if (L == Unloop) {
537       continue;
538     }
539     // Handle critical edges from Unloop into a sibling loop.
540     if (L && !L->contains(Unloop)) {
541       L = L->getParentLoop();
542     }
543     // Remember the nearest parent loop among successors or subloop exits.
544     if (NearLoop == Unloop || !NearLoop || NearLoop->contains(L))
545       NearLoop = L;
546   }
547   if (Subloop) {
548     SubloopParents[Subloop] = NearLoop;
549     return BBLoop;
550   }
551   return NearLoop;
552 }
553
554 //===----------------------------------------------------------------------===//
555 // LoopInfo implementation
556 //
557 bool LoopInfo::runOnFunction(Function &) {
558   releaseMemory();
559   LI.Calculate(getAnalysis<DominatorTree>().getBase());    // Update
560   return false;
561 }
562
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.
567 void LoopInfo::updateUnloop(Loop *Unloop) {
568
569   // First handle the special case of no parent loop to simplify the algorithm.
570   if (!Unloop->getParentLoop()) {
571     // Since BBLoop had no parent, Unloop blocks are no longer in a loop.
572     for (Loop::block_iterator I = Unloop->block_begin(),
573          E = Unloop->block_end(); I != E; ++I) {
574
575       // Don't reparent blocks in subloops.
576       if (getLoopFor(*I) != Unloop)
577         continue;
578
579       // Blocks no longer have a parent but are still referenced by Unloop until
580       // the Unloop object is deleted.
581       LI.changeLoopFor(*I, 0);
582     }
583
584     // Remove the loop from the top-level LoopInfo object.
585     for (LoopInfo::iterator I = LI.begin(), E = LI.end();; ++I) {
586       assert(I != E && "Couldn't find loop");
587       if (*I == Unloop) {
588         LI.removeLoop(I);
589         break;
590       }
591     }
592
593     // Move all of the subloops to the top-level.
594     while (!Unloop->empty())
595       LI.addTopLevelLoop(Unloop->removeChildLoop(Unloop->end()-1));
596
597     return;
598   }
599
600   // Update the parent loop for all blocks within the loop. Blocks within
601   // subloops will not change parents.
602   UnloopUpdater Updater(Unloop, this);
603   Updater.updateBlockParents();
604
605   // Remove unloop's blocks from all ancestors below their new parents.
606   for (Loop::block_iterator BI = Unloop->block_begin(),
607          BE = Unloop->block_end(); BI != BE; ++BI) {
608     Loop *NewParent = getLoopFor(*BI);
609     // If this block is in a subloop, skip it.
610     if (Unloop->contains(NewParent))
611       continue;
612
613     // Remove blocks from former Ancestors except Unloop itself which will be
614     // deleted.
615     for (Loop *OldParent = Unloop->getParentLoop(); OldParent != NewParent;
616          OldParent = OldParent->getParentLoop()) {
617       assert(OldParent && "new loop is not an ancestor of the original");
618       OldParent->removeBlockFromLoop(*BI);
619     }
620   }
621
622   // Add direct subloops as children in their new parent loop.
623   Updater.updateSubloopParents();
624
625   // Remove unloop from its parent loop.
626   Loop *ParentLoop = Unloop->getParentLoop();
627   for (Loop::iterator I = ParentLoop->begin(), E = ParentLoop->end();; ++I) {
628     assert(I != E && "Couldn't find loop");
629     if (*I == Unloop) {
630       ParentLoop->removeChildLoop(I);
631       break;
632     }
633   }
634 }
635
636 void LoopInfo::verifyAnalysis() const {
637   // LoopInfo is a FunctionPass, but verifying every loop in the function
638   // each time verifyAnalysis is called is very expensive. The
639   // -verify-loop-info option can enable this. In order to perform some
640   // checking by default, LoopPass has been taught to call verifyLoop
641   // manually during loop pass sequences.
642
643   if (!VerifyLoopInfo) return;
644
645   for (iterator I = begin(), E = end(); I != E; ++I) {
646     assert(!(*I)->getParentLoop() && "Top-level loop has a parent!");
647     (*I)->verifyLoopNest();
648   }
649
650   // TODO: check BBMap consistency.
651 }
652
653 void LoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
654   AU.setPreservesAll();
655   AU.addRequired<DominatorTree>();
656 }
657
658 void LoopInfo::print(raw_ostream &OS, const Module*) const {
659   LI.print(OS);
660 }
661