LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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