LLVM API Documentation

LoopInfo.cpp

Go to the documentation of this file.
00001 //===- LoopInfo.cpp - Natural Loop Calculator -----------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the LoopInfo class that is used to identify natural loops
00011 // and determine the loop depth of various nodes of the CFG.  Note that the
00012 // loops identified may actually be several natural loops that share the same
00013 // header node... not just a single natural loop.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #include "llvm/Analysis/LoopInfo.h"
00018 #include "llvm/Constants.h"
00019 #include "llvm/Instructions.h"
00020 #include "llvm/Analysis/Dominators.h"
00021 #include "llvm/Assembly/Writer.h"
00022 #include "llvm/Support/CFG.h"
00023 #include "llvm/ADT/DepthFirstIterator.h"
00024 #include <algorithm>
00025 #include <iostream>
00026 using namespace llvm;
00027 
00028 static RegisterAnalysis<LoopInfo>
00029 X("loops", "Natural Loop Construction", true);
00030 
00031 //===----------------------------------------------------------------------===//
00032 // Loop implementation
00033 //
00034 bool Loop::contains(const BasicBlock *BB) const {
00035   return std::find(Blocks.begin(), Blocks.end(), BB) != Blocks.end();
00036 }
00037 
00038 bool Loop::isLoopExit(const BasicBlock *BB) const {
00039   for (succ_const_iterator SI = succ_begin(BB), SE = succ_end(BB);
00040        SI != SE; ++SI) {
00041     if (!contains(*SI))
00042       return true;
00043   }
00044   return false;
00045 }
00046 
00047 /// getNumBackEdges - Calculate the number of back edges to the loop header.
00048 ///
00049 unsigned Loop::getNumBackEdges() const {
00050   unsigned NumBackEdges = 0;
00051   BasicBlock *H = getHeader();
00052 
00053   for (pred_iterator I = pred_begin(H), E = pred_end(H); I != E; ++I)
00054     if (contains(*I))
00055       ++NumBackEdges;
00056 
00057   return NumBackEdges;
00058 }
00059 
00060 /// isLoopInvariant - Return true if the specified value is loop invariant
00061 ///
00062 bool Loop::isLoopInvariant(Value *V) const {
00063   if (Instruction *I = dyn_cast<Instruction>(V))
00064     return !contains(I->getParent());
00065   return true;  // All non-instructions are loop invariant
00066 }
00067 
00068 void Loop::print(std::ostream &OS, unsigned Depth) const {
00069   OS << std::string(Depth*2, ' ') << "Loop Containing: ";
00070 
00071   for (unsigned i = 0; i < getBlocks().size(); ++i) {
00072     if (i) OS << ",";
00073     WriteAsOperand(OS, getBlocks()[i], false);
00074   }
00075   OS << "\n";
00076 
00077   for (iterator I = begin(), E = end(); I != E; ++I)
00078     (*I)->print(OS, Depth+2);
00079 }
00080 
00081 void Loop::dump() const {
00082   print(std::cerr);
00083 }
00084 
00085 
00086 //===----------------------------------------------------------------------===//
00087 // LoopInfo implementation
00088 //
00089 bool LoopInfo::runOnFunction(Function &) {
00090   releaseMemory();
00091   Calculate(getAnalysis<ETForest>());    // Update
00092   return false;
00093 }
00094 
00095 void LoopInfo::releaseMemory() {
00096   for (std::vector<Loop*>::iterator I = TopLevelLoops.begin(),
00097          E = TopLevelLoops.end(); I != E; ++I)
00098     delete *I;   // Delete all of the loops...
00099 
00100   BBMap.clear();                             // Reset internal state of analysis
00101   TopLevelLoops.clear();
00102 }
00103 
00104 
00105 void LoopInfo::Calculate(ETForest &EF) {
00106   BasicBlock *RootNode = EF.getRoot();
00107 
00108   for (df_iterator<BasicBlock*> NI = df_begin(RootNode),
00109          NE = df_end(RootNode); NI != NE; ++NI)
00110     if (Loop *L = ConsiderForLoop(*NI, EF))
00111       TopLevelLoops.push_back(L);
00112 }
00113 
00114 void LoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
00115   AU.setPreservesAll();
00116   AU.addRequired<ETForest>();
00117 }
00118 
00119 void LoopInfo::print(std::ostream &OS, const Module* ) const {
00120   for (unsigned i = 0; i < TopLevelLoops.size(); ++i)
00121     TopLevelLoops[i]->print(OS);
00122 #if 0
00123   for (std::map<BasicBlock*, Loop*>::const_iterator I = BBMap.begin(),
00124          E = BBMap.end(); I != E; ++I)
00125     OS << "BB '" << I->first->getName() << "' level = "
00126        << I->second->getLoopDepth() << "\n";
00127 #endif
00128 }
00129 
00130 static bool isNotAlreadyContainedIn(Loop *SubLoop, Loop *ParentLoop) {
00131   if (SubLoop == 0) return true;
00132   if (SubLoop == ParentLoop) return false;
00133   return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
00134 }
00135 
00136 Loop *LoopInfo::ConsiderForLoop(BasicBlock *BB, ETForest &EF) {
00137   if (BBMap.find(BB) != BBMap.end()) return 0;   // Haven't processed this node?
00138 
00139   std::vector<BasicBlock *> TodoStack;
00140 
00141   // Scan the predecessors of BB, checking to see if BB dominates any of
00142   // them.  This identifies backedges which target this node...
00143   for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I)
00144     if (EF.dominates(BB, *I))   // If BB dominates it's predecessor...
00145       TodoStack.push_back(*I);
00146 
00147   if (TodoStack.empty()) return 0;  // No backedges to this block...
00148 
00149   // Create a new loop to represent this basic block...
00150   Loop *L = new Loop(BB);
00151   BBMap[BB] = L;
00152 
00153   BasicBlock *EntryBlock = &BB->getParent()->getEntryBlock();
00154 
00155   while (!TodoStack.empty()) {  // Process all the nodes in the loop
00156     BasicBlock *X = TodoStack.back();
00157     TodoStack.pop_back();
00158 
00159     if (!L->contains(X) &&         // As of yet unprocessed??
00160         EF.dominates(EntryBlock, X)) {   // X is reachable from entry block?
00161       // Check to see if this block already belongs to a loop.  If this occurs
00162       // then we have a case where a loop that is supposed to be a child of the
00163       // current loop was processed before the current loop.  When this occurs,
00164       // this child loop gets added to a part of the current loop, making it a
00165       // sibling to the current loop.  We have to reparent this loop.
00166       if (Loop *SubLoop = const_cast<Loop*>(getLoopFor(X)))
00167         if (SubLoop->getHeader() == X && isNotAlreadyContainedIn(SubLoop, L)) {
00168           // Remove the subloop from it's current parent...
00169           assert(SubLoop->ParentLoop && SubLoop->ParentLoop != L);
00170           Loop *SLP = SubLoop->ParentLoop;  // SubLoopParent
00171           std::vector<Loop*>::iterator I =
00172             std::find(SLP->SubLoops.begin(), SLP->SubLoops.end(), SubLoop);
00173           assert(I != SLP->SubLoops.end() && "SubLoop not a child of parent?");
00174           SLP->SubLoops.erase(I);   // Remove from parent...
00175 
00176           // Add the subloop to THIS loop...
00177           SubLoop->ParentLoop = L;
00178           L->SubLoops.push_back(SubLoop);
00179         }
00180 
00181       // Normal case, add the block to our loop...
00182       L->Blocks.push_back(X);
00183 
00184       // Add all of the predecessors of X to the end of the work stack...
00185       TodoStack.insert(TodoStack.end(), pred_begin(X), pred_end(X));
00186     }
00187   }
00188 
00189   // If there are any loops nested within this loop, create them now!
00190   for (std::vector<BasicBlock*>::iterator I = L->Blocks.begin(),
00191          E = L->Blocks.end(); I != E; ++I)
00192     if (Loop *NewLoop = ConsiderForLoop(*I, EF)) {
00193       L->SubLoops.push_back(NewLoop);
00194       NewLoop->ParentLoop = L;
00195     }
00196 
00197   // Add the basic blocks that comprise this loop to the BBMap so that this
00198   // loop can be found for them.
00199   //
00200   for (std::vector<BasicBlock*>::iterator I = L->Blocks.begin(),
00201          E = L->Blocks.end(); I != E; ++I) {
00202     std::map<BasicBlock*, Loop*>::iterator BBMI = BBMap.lower_bound(*I);
00203     if (BBMI == BBMap.end() || BBMI->first != *I)  // Not in map yet...
00204       BBMap.insert(BBMI, std::make_pair(*I, L));   // Must be at this level
00205   }
00206 
00207   // Now that we have a list of all of the child loops of this loop, check to
00208   // see if any of them should actually be nested inside of each other.  We can
00209   // accidentally pull loops our of their parents, so we must make sure to
00210   // organize the loop nests correctly now.
00211   {
00212     std::map<BasicBlock*, Loop*> ContainingLoops;
00213     for (unsigned i = 0; i != L->SubLoops.size(); ++i) {
00214       Loop *Child = L->SubLoops[i];
00215       assert(Child->getParentLoop() == L && "Not proper child loop?");
00216 
00217       if (Loop *ContainingLoop = ContainingLoops[Child->getHeader()]) {
00218         // If there is already a loop which contains this loop, move this loop
00219         // into the containing loop.
00220         MoveSiblingLoopInto(Child, ContainingLoop);
00221         --i;  // The loop got removed from the SubLoops list.
00222       } else {
00223         // This is currently considered to be a top-level loop.  Check to see if
00224         // any of the contained blocks are loop headers for subloops we have
00225         // already processed.
00226         for (unsigned b = 0, e = Child->Blocks.size(); b != e; ++b) {
00227           Loop *&BlockLoop = ContainingLoops[Child->Blocks[b]];
00228           if (BlockLoop == 0) {   // Child block not processed yet...
00229             BlockLoop = Child;
00230           } else if (BlockLoop != Child) {
00231             Loop *SubLoop = BlockLoop;
00232             // Reparent all of the blocks which used to belong to BlockLoops
00233             for (unsigned j = 0, e = SubLoop->Blocks.size(); j != e; ++j)
00234               ContainingLoops[SubLoop->Blocks[j]] = Child;
00235 
00236             // There is already a loop which contains this block, that means
00237             // that we should reparent the loop which the block is currently
00238             // considered to belong to to be a child of this loop.
00239             MoveSiblingLoopInto(SubLoop, Child);
00240             --i;  // We just shrunk the SubLoops list.
00241           }
00242         }
00243       }
00244     }
00245   }
00246 
00247   return L;
00248 }
00249 
00250 /// MoveSiblingLoopInto - This method moves the NewChild loop to live inside of
00251 /// the NewParent Loop, instead of being a sibling of it.
00252 void LoopInfo::MoveSiblingLoopInto(Loop *NewChild, Loop *NewParent) {
00253   Loop *OldParent = NewChild->getParentLoop();
00254   assert(OldParent && OldParent == NewParent->getParentLoop() &&
00255          NewChild != NewParent && "Not sibling loops!");
00256 
00257   // Remove NewChild from being a child of OldParent
00258   std::vector<Loop*>::iterator I =
00259     std::find(OldParent->SubLoops.begin(), OldParent->SubLoops.end(), NewChild);
00260   assert(I != OldParent->SubLoops.end() && "Parent fields incorrect??");
00261   OldParent->SubLoops.erase(I);   // Remove from parent's subloops list
00262   NewChild->ParentLoop = 0;
00263 
00264   InsertLoopInto(NewChild, NewParent);
00265 }
00266 
00267 /// InsertLoopInto - This inserts loop L into the specified parent loop.  If the
00268 /// parent loop contains a loop which should contain L, the loop gets inserted
00269 /// into L instead.
00270 void LoopInfo::InsertLoopInto(Loop *L, Loop *Parent) {
00271   BasicBlock *LHeader = L->getHeader();
00272   assert(Parent->contains(LHeader) && "This loop should not be inserted here!");
00273 
00274   // Check to see if it belongs in a child loop...
00275   for (unsigned i = 0, e = Parent->SubLoops.size(); i != e; ++i)
00276     if (Parent->SubLoops[i]->contains(LHeader)) {
00277       InsertLoopInto(L, Parent->SubLoops[i]);
00278       return;
00279     }
00280 
00281   // If not, insert it here!
00282   Parent->SubLoops.push_back(L);
00283   L->ParentLoop = Parent;
00284 }
00285 
00286 /// changeLoopFor - Change the top-level loop that contains BB to the
00287 /// specified loop.  This should be used by transformations that restructure
00288 /// the loop hierarchy tree.
00289 void LoopInfo::changeLoopFor(BasicBlock *BB, Loop *L) {
00290   Loop *&OldLoop = BBMap[BB];
00291   assert(OldLoop && "Block not in a loop yet!");
00292   OldLoop = L;
00293 }
00294 
00295 /// changeTopLevelLoop - Replace the specified loop in the top-level loops
00296 /// list with the indicated loop.
00297 void LoopInfo::changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop) {
00298   std::vector<Loop*>::iterator I = std::find(TopLevelLoops.begin(),
00299                                              TopLevelLoops.end(), OldLoop);
00300   assert(I != TopLevelLoops.end() && "Old loop not at top level!");
00301   *I = NewLoop;
00302   assert(NewLoop->ParentLoop == 0 && OldLoop->ParentLoop == 0 &&
00303          "Loops already embedded into a subloop!");
00304 }
00305 
00306 /// removeLoop - This removes the specified top-level loop from this loop info
00307 /// object.  The loop is not deleted, as it will presumably be inserted into
00308 /// another loop.
00309 Loop *LoopInfo::removeLoop(iterator I) {
00310   assert(I != end() && "Cannot remove end iterator!");
00311   Loop *L = *I;
00312   assert(L->getParentLoop() == 0 && "Not a top-level loop!");
00313   TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
00314   return L;
00315 }
00316 
00317 /// removeBlock - This method completely removes BB from all data structures,
00318 /// including all of the Loop objects it is nested in and our mapping from
00319 /// BasicBlocks to loops.
00320 void LoopInfo::removeBlock(BasicBlock *BB) {
00321   std::map<BasicBlock *, Loop*>::iterator I = BBMap.find(BB);
00322   if (I != BBMap.end()) {
00323     for (Loop *L = I->second; L; L = L->getParentLoop())
00324       L->removeBlockFromLoop(BB);
00325 
00326     BBMap.erase(I);
00327   }
00328 }
00329 
00330 
00331 //===----------------------------------------------------------------------===//
00332 // APIs for simple analysis of the loop.
00333 //
00334 
00335 /// getExitBlocks - Return all of the successor blocks of this loop.  These
00336 /// are the blocks _outside of the current loop_ which are branched to.
00337 ///
00338 void Loop::getExitBlocks(std::vector<BasicBlock*> &ExitBlocks) const {
00339   for (std::vector<BasicBlock*>::const_iterator BI = Blocks.begin(),
00340          BE = Blocks.end(); BI != BE; ++BI)
00341     for (succ_iterator I = succ_begin(*BI), E = succ_end(*BI); I != E; ++I)
00342       if (!contains(*I))               // Not in current loop?
00343         ExitBlocks.push_back(*I);          // It must be an exit block...
00344 }
00345 
00346 
00347 /// getLoopPreheader - If there is a preheader for this loop, return it.  A
00348 /// loop has a preheader if there is only one edge to the header of the loop
00349 /// from outside of the loop.  If this is the case, the block branching to the
00350 /// header of the loop is the preheader node.
00351 ///
00352 /// This method returns null if there is no preheader for the loop.
00353 ///
00354 BasicBlock *Loop::getLoopPreheader() const {
00355   // Keep track of nodes outside the loop branching to the header...
00356   BasicBlock *Out = 0;
00357 
00358   // Loop over the predecessors of the header node...
00359   BasicBlock *Header = getHeader();
00360   for (pred_iterator PI = pred_begin(Header), PE = pred_end(Header);
00361        PI != PE; ++PI)
00362     if (!contains(*PI)) {     // If the block is not in the loop...
00363       if (Out && Out != *PI)
00364         return 0;             // Multiple predecessors outside the loop
00365       Out = *PI;
00366     }
00367 
00368   // Make sure there is only one exit out of the preheader.
00369   assert(Out && "Header of loop has no predecessors from outside loop?");
00370   succ_iterator SI = succ_begin(Out);
00371   ++SI;
00372   if (SI != succ_end(Out))
00373     return 0;  // Multiple exits from the block, must not be a preheader.
00374 
00375   // If there is exactly one preheader, return it.  If there was zero, then Out
00376   // is still null.
00377   return Out;
00378 }
00379 
00380 /// getLoopLatch - If there is a latch block for this loop, return it.  A
00381 /// latch block is the canonical backedge for a loop.  A loop header in normal
00382 /// form has two edges into it: one from a preheader and one from a latch
00383 /// block.
00384 BasicBlock *Loop::getLoopLatch() const {
00385   BasicBlock *Header = getHeader();
00386   pred_iterator PI = pred_begin(Header), PE = pred_end(Header);
00387   if (PI == PE) return 0;  // no preds?
00388   
00389   BasicBlock *Latch = 0;
00390   if (contains(*PI))
00391     Latch = *PI;
00392   ++PI;
00393   if (PI == PE) return 0;  // only one pred?
00394   
00395   if (contains(*PI)) {
00396     if (Latch) return 0;  // multiple backedges
00397     Latch = *PI;
00398   }
00399   ++PI;
00400   if (PI != PE) return 0;  // more than two preds
00401   
00402   return Latch;  
00403 }
00404 
00405 /// getCanonicalInductionVariable - Check to see if the loop has a canonical
00406 /// induction variable: an integer recurrence that starts at 0 and increments by
00407 /// one each time through the loop.  If so, return the phi node that corresponds
00408 /// to it.
00409 ///
00410 PHINode *Loop::getCanonicalInductionVariable() const {
00411   BasicBlock *H = getHeader();
00412 
00413   BasicBlock *Incoming = 0, *Backedge = 0;
00414   pred_iterator PI = pred_begin(H);
00415   assert(PI != pred_end(H) && "Loop must have at least one backedge!");
00416   Backedge = *PI++;
00417   if (PI == pred_end(H)) return 0;  // dead loop
00418   Incoming = *PI++;
00419   if (PI != pred_end(H)) return 0;  // multiple backedges?
00420 
00421   if (contains(Incoming)) {
00422     if (contains(Backedge))
00423       return 0;
00424     std::swap(Incoming, Backedge);
00425   } else if (!contains(Backedge))
00426     return 0;
00427 
00428   // Loop over all of the PHI nodes, looking for a canonical indvar.
00429   for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) {
00430     PHINode *PN = cast<PHINode>(I);
00431     if (Instruction *Inc =
00432         dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge)))
00433       if (Inc->getOpcode() == Instruction::Add && Inc->getOperand(0) == PN)
00434         if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
00435           if (CI->equalsInt(1))
00436             return PN;
00437   }
00438   return 0;
00439 }
00440 
00441 /// getCanonicalInductionVariableIncrement - Return the LLVM value that holds
00442 /// the canonical induction variable value for the "next" iteration of the loop.
00443 /// This always succeeds if getCanonicalInductionVariable succeeds.
00444 ///
00445 Instruction *Loop::getCanonicalInductionVariableIncrement() const {
00446   if (PHINode *PN = getCanonicalInductionVariable()) {
00447     bool P1InLoop = contains(PN->getIncomingBlock(1));
00448     return cast<Instruction>(PN->getIncomingValue(P1InLoop));
00449   }
00450   return 0;
00451 }
00452 
00453 /// getTripCount - Return a loop-invariant LLVM value indicating the number of
00454 /// times the loop will be executed.  Note that this means that the backedge of
00455 /// the loop executes N-1 times.  If the trip-count cannot be determined, this
00456 /// returns null.
00457 ///
00458 Value *Loop::getTripCount() const {
00459   // Canonical loops will end with a 'setne I, V', where I is the incremented
00460   // canonical induction variable and V is the trip count of the loop.
00461   Instruction *Inc = getCanonicalInductionVariableIncrement();
00462   if (Inc == 0) return 0;
00463   PHINode *IV = cast<PHINode>(Inc->getOperand(0));
00464 
00465   BasicBlock *BackedgeBlock =
00466     IV->getIncomingBlock(contains(IV->getIncomingBlock(1)));
00467 
00468   if (BranchInst *BI = dyn_cast<BranchInst>(BackedgeBlock->getTerminator()))
00469     if (BI->isConditional())
00470       if (SetCondInst *SCI = dyn_cast<SetCondInst>(BI->getCondition()))
00471         if (SCI->getOperand(0) == Inc)
00472           if (BI->getSuccessor(0) == getHeader()) {
00473             if (SCI->getOpcode() == Instruction::SetNE)
00474               return SCI->getOperand(1);
00475           } else if (SCI->getOpcode() == Instruction::SetEQ) {
00476             return SCI->getOperand(1);
00477           }
00478 
00479   return 0;
00480 }
00481 
00482 /// isLCSSAForm - Return true if the Loop is in LCSSA form
00483 bool Loop::isLCSSAForm() const {  
00484   for (Loop::block_iterator BB = block_begin(), E = block_end();
00485        BB != E; ++BB) {
00486     for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); I != E; ++I)
00487       for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;
00488            ++UI) {
00489         BasicBlock *UserBB = cast<Instruction>(*UI)->getParent();
00490         if (PHINode* p = dyn_cast<PHINode>(*UI)) {
00491           unsigned OperandNo = UI.getOperandNo();
00492           UserBB = p->getIncomingBlock(OperandNo/2);
00493         }
00494         
00495         if (!contains(UserBB)) {
00496           return false;
00497         }
00498       }
00499   }
00500   
00501   return true;
00502 }
00503 
00504 //===-------------------------------------------------------------------===//
00505 // APIs for updating loop information after changing the CFG
00506 //
00507 
00508 /// addBasicBlockToLoop - This function is used by other analyses to update loop
00509 /// information.  NewBB is set to be a new member of the current loop.  Because
00510 /// of this, it is added as a member of all parent loops, and is added to the
00511 /// specified LoopInfo object as being in the current basic block.  It is not
00512 /// valid to replace the loop header with this method.
00513 ///
00514 void Loop::addBasicBlockToLoop(BasicBlock *NewBB, LoopInfo &LI) {
00515   assert((Blocks.empty() || LI[getHeader()] == this) &&
00516          "Incorrect LI specified for this loop!");
00517   assert(NewBB && "Cannot add a null basic block to the loop!");
00518   assert(LI[NewBB] == 0 && "BasicBlock already in the loop!");
00519 
00520   // Add the loop mapping to the LoopInfo object...
00521   LI.BBMap[NewBB] = this;
00522 
00523   // Add the basic block to this loop and all parent loops...
00524   Loop *L = this;
00525   while (L) {
00526     L->Blocks.push_back(NewBB);
00527     L = L->getParentLoop();
00528   }
00529 }
00530 
00531 /// replaceChildLoopWith - This is used when splitting loops up.  It replaces
00532 /// the OldChild entry in our children list with NewChild, and updates the
00533 /// parent pointers of the two loops as appropriate.
00534 void Loop::replaceChildLoopWith(Loop *OldChild, Loop *NewChild) {
00535   assert(OldChild->ParentLoop == this && "This loop is already broken!");
00536   assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
00537   std::vector<Loop*>::iterator I = std::find(SubLoops.begin(), SubLoops.end(),
00538                                              OldChild);
00539   assert(I != SubLoops.end() && "OldChild not in loop!");
00540   *I = NewChild;
00541   OldChild->ParentLoop = 0;
00542   NewChild->ParentLoop = this;
00543 }
00544 
00545 /// addChildLoop - Add the specified loop to be a child of this loop.
00546 ///
00547 void Loop::addChildLoop(Loop *NewChild) {
00548   assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
00549   NewChild->ParentLoop = this;
00550   SubLoops.push_back(NewChild);
00551 }
00552 
00553 template<typename T>
00554 static void RemoveFromVector(std::vector<T*> &V, T *N) {
00555   typename std::vector<T*>::iterator I = std::find(V.begin(), V.end(), N);
00556   assert(I != V.end() && "N is not in this list!");
00557   V.erase(I);
00558 }
00559 
00560 /// removeChildLoop - This removes the specified child from being a subloop of
00561 /// this loop.  The loop is not deleted, as it will presumably be inserted
00562 /// into another loop.
00563 Loop *Loop::removeChildLoop(iterator I) {
00564   assert(I != SubLoops.end() && "Cannot remove end iterator!");
00565   Loop *Child = *I;
00566   assert(Child->ParentLoop == this && "Child is not a child of this loop!");
00567   SubLoops.erase(SubLoops.begin()+(I-begin()));
00568   Child->ParentLoop = 0;
00569   return Child;
00570 }
00571 
00572 
00573 /// removeBlockFromLoop - This removes the specified basic block from the
00574 /// current loop, updating the Blocks and ExitBlocks lists as appropriate.  This
00575 /// does not update the mapping in the LoopInfo class.
00576 void Loop::removeBlockFromLoop(BasicBlock *BB) {
00577   RemoveFromVector(Blocks, BB);
00578 }
00579 
00580 // Ensure this file gets linked when LoopInfo.h is used.
00581 DEFINING_FILE_FOR(LoopInfo)