Home | History | Annotate | Download | only in Analysis
      1 //===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator -------*- C++ -*-===//
      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.  A natural loop
     12 // has exactly one entry-point, which is called the header. Note that natural
     13 // loops may actually be several loops that share the same header node.
     14 //
     15 // This analysis calculates the nesting structure of loops in a function.  For
     16 // each natural loop identified, this analysis identifies natural loops
     17 // contained entirely within the loop and the basic blocks the make up the loop.
     18 //
     19 // It can calculate on the fly various bits of information, for example:
     20 //
     21 //  * whether there is a preheader for the loop
     22 //  * the number of back edges to the header
     23 //  * whether or not a particular block branches out of the loop
     24 //  * the successor blocks of the loop
     25 //  * the loop depth
     26 //  * etc...
     27 //
     28 //===----------------------------------------------------------------------===//
     29 
     30 #ifndef LLVM_ANALYSIS_LOOP_INFO_H
     31 #define LLVM_ANALYSIS_LOOP_INFO_H
     32 
     33 #include "llvm/Pass.h"
     34 #include "llvm/ADT/DenseMap.h"
     35 #include "llvm/ADT/DenseSet.h"
     36 #include "llvm/ADT/DepthFirstIterator.h"
     37 #include "llvm/ADT/GraphTraits.h"
     38 #include "llvm/ADT/SmallVector.h"
     39 #include "llvm/ADT/STLExtras.h"
     40 #include "llvm/Analysis/Dominators.h"
     41 #include "llvm/Support/CFG.h"
     42 #include "llvm/Support/raw_ostream.h"
     43 #include <algorithm>
     44 #include <map>
     45 
     46 namespace llvm {
     47 
     48 template<typename T>
     49 inline void RemoveFromVector(std::vector<T*> &V, T *N) {
     50   typename std::vector<T*>::iterator I = std::find(V.begin(), V.end(), N);
     51   assert(I != V.end() && "N is not in this list!");
     52   V.erase(I);
     53 }
     54 
     55 class DominatorTree;
     56 class LoopInfo;
     57 class Loop;
     58 class PHINode;
     59 template<class N, class M> class LoopInfoBase;
     60 template<class N, class M> class LoopBase;
     61 
     62 //===----------------------------------------------------------------------===//
     63 /// LoopBase class - Instances of this class are used to represent loops that
     64 /// are detected in the flow graph
     65 ///
     66 template<class BlockT, class LoopT>
     67 class LoopBase {
     68   LoopT *ParentLoop;
     69   // SubLoops - Loops contained entirely within this one.
     70   std::vector<LoopT *> SubLoops;
     71 
     72   // Blocks - The list of blocks in this loop.  First entry is the header node.
     73   std::vector<BlockT*> Blocks;
     74 
     75   // DO NOT IMPLEMENT
     76   LoopBase(const LoopBase<BlockT, LoopT> &);
     77   // DO NOT IMPLEMENT
     78   const LoopBase<BlockT, LoopT>&operator=(const LoopBase<BlockT, LoopT> &);
     79 public:
     80   /// Loop ctor - This creates an empty loop.
     81   LoopBase() : ParentLoop(0) {}
     82   ~LoopBase() {
     83     for (size_t i = 0, e = SubLoops.size(); i != e; ++i)
     84       delete SubLoops[i];
     85   }
     86 
     87   /// getLoopDepth - Return the nesting level of this loop.  An outer-most
     88   /// loop has depth 1, for consistency with loop depth values used for basic
     89   /// blocks, where depth 0 is used for blocks not inside any loops.
     90   unsigned getLoopDepth() const {
     91     unsigned D = 1;
     92     for (const LoopT *CurLoop = ParentLoop; CurLoop;
     93          CurLoop = CurLoop->ParentLoop)
     94       ++D;
     95     return D;
     96   }
     97   BlockT *getHeader() const { return Blocks.front(); }
     98   LoopT *getParentLoop() const { return ParentLoop; }
     99 
    100   /// setParentLoop is a raw interface for bypassing addChildLoop.
    101   void setParentLoop(LoopT *L) { ParentLoop = L; }
    102 
    103   /// contains - Return true if the specified loop is contained within in
    104   /// this loop.
    105   ///
    106   bool contains(const LoopT *L) const {
    107     if (L == this) return true;
    108     if (L == 0)    return false;
    109     return contains(L->getParentLoop());
    110   }
    111 
    112   /// contains - Return true if the specified basic block is in this loop.
    113   ///
    114   bool contains(const BlockT *BB) const {
    115     return std::find(block_begin(), block_end(), BB) != block_end();
    116   }
    117 
    118   /// contains - Return true if the specified instruction is in this loop.
    119   ///
    120   template<class InstT>
    121   bool contains(const InstT *Inst) const {
    122     return contains(Inst->getParent());
    123   }
    124 
    125   /// iterator/begin/end - Return the loops contained entirely within this loop.
    126   ///
    127   const std::vector<LoopT *> &getSubLoops() const { return SubLoops; }
    128   std::vector<LoopT *> &getSubLoopsVector() { return SubLoops; }
    129   typedef typename std::vector<LoopT *>::const_iterator iterator;
    130   typedef typename std::vector<LoopT *>::const_reverse_iterator
    131     reverse_iterator;
    132   iterator begin() const { return SubLoops.begin(); }
    133   iterator end() const { return SubLoops.end(); }
    134   reverse_iterator rbegin() const { return SubLoops.rbegin(); }
    135   reverse_iterator rend() const { return SubLoops.rend(); }
    136   bool empty() const { return SubLoops.empty(); }
    137 
    138   /// getBlocks - Get a list of the basic blocks which make up this loop.
    139   ///
    140   const std::vector<BlockT*> &getBlocks() const { return Blocks; }
    141   std::vector<BlockT*> &getBlocksVector() { return Blocks; }
    142   typedef typename std::vector<BlockT*>::const_iterator block_iterator;
    143   block_iterator block_begin() const { return Blocks.begin(); }
    144   block_iterator block_end() const { return Blocks.end(); }
    145 
    146   /// getNumBlocks - Get the number of blocks in this loop in constant time.
    147   unsigned getNumBlocks() const {
    148     return Blocks.size();
    149   }
    150 
    151   /// isLoopExiting - True if terminator in the block can branch to another
    152   /// block that is outside of the current loop.
    153   ///
    154   bool isLoopExiting(const BlockT *BB) const {
    155     typedef GraphTraits<BlockT*> BlockTraits;
    156     for (typename BlockTraits::ChildIteratorType SI =
    157          BlockTraits::child_begin(const_cast<BlockT*>(BB)),
    158          SE = BlockTraits::child_end(const_cast<BlockT*>(BB)); SI != SE; ++SI) {
    159       if (!contains(*SI))
    160         return true;
    161     }
    162     return false;
    163   }
    164 
    165   /// getNumBackEdges - Calculate the number of back edges to the loop header
    166   ///
    167   unsigned getNumBackEdges() const {
    168     unsigned NumBackEdges = 0;
    169     BlockT *H = getHeader();
    170 
    171     typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
    172     for (typename InvBlockTraits::ChildIteratorType I =
    173          InvBlockTraits::child_begin(const_cast<BlockT*>(H)),
    174          E = InvBlockTraits::child_end(const_cast<BlockT*>(H)); I != E; ++I)
    175       if (contains(*I))
    176         ++NumBackEdges;
    177 
    178     return NumBackEdges;
    179   }
    180 
    181   //===--------------------------------------------------------------------===//
    182   // APIs for simple analysis of the loop.
    183   //
    184   // Note that all of these methods can fail on general loops (ie, there may not
    185   // be a preheader, etc).  For best success, the loop simplification and
    186   // induction variable canonicalization pass should be used to normalize loops
    187   // for easy analysis.  These methods assume canonical loops.
    188 
    189   /// getExitingBlocks - Return all blocks inside the loop that have successors
    190   /// outside of the loop.  These are the blocks _inside of the current loop_
    191   /// which branch out.  The returned list is always unique.
    192   ///
    193   void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const;
    194 
    195   /// getExitingBlock - If getExitingBlocks would return exactly one block,
    196   /// return that block. Otherwise return null.
    197   BlockT *getExitingBlock() const;
    198 
    199   /// getExitBlocks - Return all of the successor blocks of this loop.  These
    200   /// are the blocks _outside of the current loop_ which are branched to.
    201   ///
    202   void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const;
    203 
    204   /// getExitBlock - If getExitBlocks would return exactly one block,
    205   /// return that block. Otherwise return null.
    206   BlockT *getExitBlock() const;
    207 
    208   /// Edge type.
    209   typedef std::pair<const BlockT*, const BlockT*> Edge;
    210 
    211   /// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
    212   void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const;
    213 
    214   /// getLoopPreheader - If there is a preheader for this loop, return it.  A
    215   /// loop has a preheader if there is only one edge to the header of the loop
    216   /// from outside of the loop.  If this is the case, the block branching to the
    217   /// header of the loop is the preheader node.
    218   ///
    219   /// This method returns null if there is no preheader for the loop.
    220   ///
    221   BlockT *getLoopPreheader() const;
    222 
    223   /// getLoopPredecessor - If the given loop's header has exactly one unique
    224   /// predecessor outside the loop, return it. Otherwise return null.
    225   /// This is less strict that the loop "preheader" concept, which requires
    226   /// the predecessor to have exactly one successor.
    227   ///
    228   BlockT *getLoopPredecessor() const;
    229 
    230   /// getLoopLatch - If there is a single latch block for this loop, return it.
    231   /// A latch block is a block that contains a branch back to the header.
    232   BlockT *getLoopLatch() const;
    233 
    234   //===--------------------------------------------------------------------===//
    235   // APIs for updating loop information after changing the CFG
    236   //
    237 
    238   /// addBasicBlockToLoop - This method is used by other analyses to update loop
    239   /// information.  NewBB is set to be a new member of the current loop.
    240   /// Because of this, it is added as a member of all parent loops, and is added
    241   /// to the specified LoopInfo object as being in the current basic block.  It
    242   /// is not valid to replace the loop header with this method.
    243   ///
    244   void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
    245 
    246   /// replaceChildLoopWith - This is used when splitting loops up.  It replaces
    247   /// the OldChild entry in our children list with NewChild, and updates the
    248   /// parent pointer of OldChild to be null and the NewChild to be this loop.
    249   /// This updates the loop depth of the new child.
    250   void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild);
    251 
    252   /// addChildLoop - Add the specified loop to be a child of this loop.  This
    253   /// updates the loop depth of the new child.
    254   ///
    255   void addChildLoop(LoopT *NewChild) {
    256     assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
    257     NewChild->ParentLoop = static_cast<LoopT *>(this);
    258     SubLoops.push_back(NewChild);
    259   }
    260 
    261   /// removeChildLoop - This removes the specified child from being a subloop of
    262   /// this loop.  The loop is not deleted, as it will presumably be inserted
    263   /// into another loop.
    264   LoopT *removeChildLoop(iterator I) {
    265     assert(I != SubLoops.end() && "Cannot remove end iterator!");
    266     LoopT *Child = *I;
    267     assert(Child->ParentLoop == this && "Child is not a child of this loop!");
    268     SubLoops.erase(SubLoops.begin()+(I-begin()));
    269     Child->ParentLoop = 0;
    270     return Child;
    271   }
    272 
    273   /// addBlockEntry - This adds a basic block directly to the basic block list.
    274   /// This should only be used by transformations that create new loops.  Other
    275   /// transformations should use addBasicBlockToLoop.
    276   void addBlockEntry(BlockT *BB) {
    277     Blocks.push_back(BB);
    278   }
    279 
    280   /// moveToHeader - This method is used to move BB (which must be part of this
    281   /// loop) to be the loop header of the loop (the block that dominates all
    282   /// others).
    283   void moveToHeader(BlockT *BB) {
    284     if (Blocks[0] == BB) return;
    285     for (unsigned i = 0; ; ++i) {
    286       assert(i != Blocks.size() && "Loop does not contain BB!");
    287       if (Blocks[i] == BB) {
    288         Blocks[i] = Blocks[0];
    289         Blocks[0] = BB;
    290         return;
    291       }
    292     }
    293   }
    294 
    295   /// removeBlockFromLoop - This removes the specified basic block from the
    296   /// current loop, updating the Blocks as appropriate.  This does not update
    297   /// the mapping in the LoopInfo class.
    298   void removeBlockFromLoop(BlockT *BB) {
    299     RemoveFromVector(Blocks, BB);
    300   }
    301 
    302   /// verifyLoop - Verify loop structure
    303   void verifyLoop() const;
    304 
    305   /// verifyLoop - Verify loop structure of this loop and all nested loops.
    306   void verifyLoopNest(DenseSet<const LoopT*> *Loops) const;
    307 
    308   void print(raw_ostream &OS, unsigned Depth = 0) const;
    309 
    310 protected:
    311   friend class LoopInfoBase<BlockT, LoopT>;
    312   explicit LoopBase(BlockT *BB) : ParentLoop(0) {
    313     Blocks.push_back(BB);
    314   }
    315 };
    316 
    317 template<class BlockT, class LoopT>
    318 raw_ostream& operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
    319   Loop.print(OS);
    320   return OS;
    321 }
    322 
    323 // Implementation in LoopInfoImpl.h
    324 #ifdef __GNUC__
    325 __extension__ extern template class LoopBase<BasicBlock, Loop>;
    326 #endif
    327 
    328 class Loop : public LoopBase<BasicBlock, Loop> {
    329 public:
    330   Loop() {}
    331 
    332   /// isLoopInvariant - Return true if the specified value is loop invariant
    333   ///
    334   bool isLoopInvariant(Value *V) const;
    335 
    336   /// hasLoopInvariantOperands - Return true if all the operands of the
    337   /// specified instruction are loop invariant.
    338   bool hasLoopInvariantOperands(Instruction *I) const;
    339 
    340   /// makeLoopInvariant - If the given value is an instruction inside of the
    341   /// loop and it can be hoisted, do so to make it trivially loop-invariant.
    342   /// Return true if the value after any hoisting is loop invariant. This
    343   /// function can be used as a slightly more aggressive replacement for
    344   /// isLoopInvariant.
    345   ///
    346   /// If InsertPt is specified, it is the point to hoist instructions to.
    347   /// If null, the terminator of the loop preheader is used.
    348   ///
    349   bool makeLoopInvariant(Value *V, bool &Changed,
    350                          Instruction *InsertPt = 0) const;
    351 
    352   /// makeLoopInvariant - If the given instruction is inside of the
    353   /// loop and it can be hoisted, do so to make it trivially loop-invariant.
    354   /// Return true if the instruction after any hoisting is loop invariant. This
    355   /// function can be used as a slightly more aggressive replacement for
    356   /// isLoopInvariant.
    357   ///
    358   /// If InsertPt is specified, it is the point to hoist instructions to.
    359   /// If null, the terminator of the loop preheader is used.
    360   ///
    361   bool makeLoopInvariant(Instruction *I, bool &Changed,
    362                          Instruction *InsertPt = 0) const;
    363 
    364   /// getCanonicalInductionVariable - Check to see if the loop has a canonical
    365   /// induction variable: an integer recurrence that starts at 0 and increments
    366   /// by one each time through the loop.  If so, return the phi node that
    367   /// corresponds to it.
    368   ///
    369   /// The IndVarSimplify pass transforms loops to have a canonical induction
    370   /// variable.
    371   ///
    372   PHINode *getCanonicalInductionVariable() const;
    373 
    374   /// isLCSSAForm - Return true if the Loop is in LCSSA form
    375   bool isLCSSAForm(DominatorTree &DT) const;
    376 
    377   /// isLoopSimplifyForm - Return true if the Loop is in the form that
    378   /// the LoopSimplify form transforms loops to, which is sometimes called
    379   /// normal form.
    380   bool isLoopSimplifyForm() const;
    381 
    382   /// isSafeToClone - Return true if the loop body is safe to clone in practice.
    383   bool isSafeToClone() const;
    384 
    385   /// hasDedicatedExits - Return true if no exit block for the loop
    386   /// has a predecessor that is outside the loop.
    387   bool hasDedicatedExits() const;
    388 
    389   /// getUniqueExitBlocks - Return all unique successor blocks of this loop.
    390   /// These are the blocks _outside of the current loop_ which are branched to.
    391   /// This assumes that loop exits are in canonical form.
    392   ///
    393   void getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const;
    394 
    395   /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one
    396   /// block, return that block. Otherwise return null.
    397   BasicBlock *getUniqueExitBlock() const;
    398 
    399   void dump() const;
    400 
    401 private:
    402   friend class LoopInfoBase<BasicBlock, Loop>;
    403   explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
    404 };
    405 
    406 //===----------------------------------------------------------------------===//
    407 /// LoopInfo - This class builds and contains all of the top level loop
    408 /// structures in the specified function.
    409 ///
    410 
    411 template<class BlockT, class LoopT>
    412 class LoopInfoBase {
    413   // BBMap - Mapping of basic blocks to the inner most loop they occur in
    414   DenseMap<BlockT *, LoopT *> BBMap;
    415   std::vector<LoopT *> TopLevelLoops;
    416   friend class LoopBase<BlockT, LoopT>;
    417   friend class LoopInfo;
    418 
    419   void operator=(const LoopInfoBase &); // do not implement
    420   LoopInfoBase(const LoopInfo &);       // do not implement
    421 public:
    422   LoopInfoBase() { }
    423   ~LoopInfoBase() { releaseMemory(); }
    424 
    425   void releaseMemory() {
    426     for (typename std::vector<LoopT *>::iterator I =
    427          TopLevelLoops.begin(), E = TopLevelLoops.end(); I != E; ++I)
    428       delete *I;   // Delete all of the loops...
    429 
    430     BBMap.clear();                           // Reset internal state of analysis
    431     TopLevelLoops.clear();
    432   }
    433 
    434   /// iterator/begin/end - The interface to the top-level loops in the current
    435   /// function.
    436   ///
    437   typedef typename std::vector<LoopT *>::const_iterator iterator;
    438   typedef typename std::vector<LoopT *>::const_reverse_iterator
    439     reverse_iterator;
    440   iterator begin() const { return TopLevelLoops.begin(); }
    441   iterator end() const { return TopLevelLoops.end(); }
    442   reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); }
    443   reverse_iterator rend() const { return TopLevelLoops.rend(); }
    444   bool empty() const { return TopLevelLoops.empty(); }
    445 
    446   /// getLoopFor - Return the inner most loop that BB lives in.  If a basic
    447   /// block is in no loop (for example the entry node), null is returned.
    448   ///
    449   LoopT *getLoopFor(const BlockT *BB) const {
    450     return BBMap.lookup(const_cast<BlockT*>(BB));
    451   }
    452 
    453   /// operator[] - same as getLoopFor...
    454   ///
    455   const LoopT *operator[](const BlockT *BB) const {
    456     return getLoopFor(BB);
    457   }
    458 
    459   /// getLoopDepth - Return the loop nesting level of the specified block.  A
    460   /// depth of 0 means the block is not inside any loop.
    461   ///
    462   unsigned getLoopDepth(const BlockT *BB) const {
    463     const LoopT *L = getLoopFor(BB);
    464     return L ? L->getLoopDepth() : 0;
    465   }
    466 
    467   // isLoopHeader - True if the block is a loop header node
    468   bool isLoopHeader(BlockT *BB) const {
    469     const LoopT *L = getLoopFor(BB);
    470     return L && L->getHeader() == BB;
    471   }
    472 
    473   /// removeLoop - This removes the specified top-level loop from this loop info
    474   /// object.  The loop is not deleted, as it will presumably be inserted into
    475   /// another loop.
    476   LoopT *removeLoop(iterator I) {
    477     assert(I != end() && "Cannot remove end iterator!");
    478     LoopT *L = *I;
    479     assert(L->getParentLoop() == 0 && "Not a top-level loop!");
    480     TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
    481     return L;
    482   }
    483 
    484   /// changeLoopFor - Change the top-level loop that contains BB to the
    485   /// specified loop.  This should be used by transformations that restructure
    486   /// the loop hierarchy tree.
    487   void changeLoopFor(BlockT *BB, LoopT *L) {
    488     if (!L) {
    489       BBMap.erase(BB);
    490       return;
    491     }
    492     BBMap[BB] = L;
    493   }
    494 
    495   /// changeTopLevelLoop - Replace the specified loop in the top-level loops
    496   /// list with the indicated loop.
    497   void changeTopLevelLoop(LoopT *OldLoop,
    498                           LoopT *NewLoop) {
    499     typename std::vector<LoopT *>::iterator I =
    500                  std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop);
    501     assert(I != TopLevelLoops.end() && "Old loop not at top level!");
    502     *I = NewLoop;
    503     assert(NewLoop->ParentLoop == 0 && OldLoop->ParentLoop == 0 &&
    504            "Loops already embedded into a subloop!");
    505   }
    506 
    507   /// addTopLevelLoop - This adds the specified loop to the collection of
    508   /// top-level loops.
    509   void addTopLevelLoop(LoopT *New) {
    510     assert(New->getParentLoop() == 0 && "Loop already in subloop!");
    511     TopLevelLoops.push_back(New);
    512   }
    513 
    514   /// removeBlock - This method completely removes BB from all data structures,
    515   /// including all of the Loop objects it is nested in and our mapping from
    516   /// BasicBlocks to loops.
    517   void removeBlock(BlockT *BB) {
    518     typename DenseMap<BlockT *, LoopT *>::iterator I = BBMap.find(BB);
    519     if (I != BBMap.end()) {
    520       for (LoopT *L = I->second; L; L = L->getParentLoop())
    521         L->removeBlockFromLoop(BB);
    522 
    523       BBMap.erase(I);
    524     }
    525   }
    526 
    527   // Internals
    528 
    529   static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
    530                                       const LoopT *ParentLoop) {
    531     if (SubLoop == 0) return true;
    532     if (SubLoop == ParentLoop) return false;
    533     return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
    534   }
    535 
    536   /// Create the loop forest using a stable algorithm.
    537   void Analyze(DominatorTreeBase<BlockT> &DomTree);
    538 
    539   // Debugging
    540 
    541   void print(raw_ostream &OS) const;
    542 };
    543 
    544 // Implementation in LoopInfoImpl.h
    545 #ifdef __GNUC__
    546 __extension__ extern template class LoopInfoBase<BasicBlock, Loop>;
    547 #endif
    548 
    549 class LoopInfo : public FunctionPass {
    550   LoopInfoBase<BasicBlock, Loop> LI;
    551   friend class LoopBase<BasicBlock, Loop>;
    552 
    553   void operator=(const LoopInfo &); // do not implement
    554   LoopInfo(const LoopInfo &);       // do not implement
    555 public:
    556   static char ID; // Pass identification, replacement for typeid
    557 
    558   LoopInfo() : FunctionPass(ID) {
    559     initializeLoopInfoPass(*PassRegistry::getPassRegistry());
    560   }
    561 
    562   LoopInfoBase<BasicBlock, Loop>& getBase() { return LI; }
    563 
    564   /// iterator/begin/end - The interface to the top-level loops in the current
    565   /// function.
    566   ///
    567   typedef LoopInfoBase<BasicBlock, Loop>::iterator iterator;
    568   typedef LoopInfoBase<BasicBlock, Loop>::reverse_iterator reverse_iterator;
    569   inline iterator begin() const { return LI.begin(); }
    570   inline iterator end() const { return LI.end(); }
    571   inline reverse_iterator rbegin() const { return LI.rbegin(); }
    572   inline reverse_iterator rend() const { return LI.rend(); }
    573   bool empty() const { return LI.empty(); }
    574 
    575   /// getLoopFor - Return the inner most loop that BB lives in.  If a basic
    576   /// block is in no loop (for example the entry node), null is returned.
    577   ///
    578   inline Loop *getLoopFor(const BasicBlock *BB) const {
    579     return LI.getLoopFor(BB);
    580   }
    581 
    582   /// operator[] - same as getLoopFor...
    583   ///
    584   inline const Loop *operator[](const BasicBlock *BB) const {
    585     return LI.getLoopFor(BB);
    586   }
    587 
    588   /// getLoopDepth - Return the loop nesting level of the specified block.  A
    589   /// depth of 0 means the block is not inside any loop.
    590   ///
    591   inline unsigned getLoopDepth(const BasicBlock *BB) const {
    592     return LI.getLoopDepth(BB);
    593   }
    594 
    595   // isLoopHeader - True if the block is a loop header node
    596   inline bool isLoopHeader(BasicBlock *BB) const {
    597     return LI.isLoopHeader(BB);
    598   }
    599 
    600   /// runOnFunction - Calculate the natural loop information.
    601   ///
    602   virtual bool runOnFunction(Function &F);
    603 
    604   virtual void verifyAnalysis() const;
    605 
    606   virtual void releaseMemory() { LI.releaseMemory(); }
    607 
    608   virtual void print(raw_ostream &O, const Module* M = 0) const;
    609 
    610   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
    611 
    612   /// removeLoop - This removes the specified top-level loop from this loop info
    613   /// object.  The loop is not deleted, as it will presumably be inserted into
    614   /// another loop.
    615   inline Loop *removeLoop(iterator I) { return LI.removeLoop(I); }
    616 
    617   /// changeLoopFor - Change the top-level loop that contains BB to the
    618   /// specified loop.  This should be used by transformations that restructure
    619   /// the loop hierarchy tree.
    620   inline void changeLoopFor(BasicBlock *BB, Loop *L) {
    621     LI.changeLoopFor(BB, L);
    622   }
    623 
    624   /// changeTopLevelLoop - Replace the specified loop in the top-level loops
    625   /// list with the indicated loop.
    626   inline void changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop) {
    627     LI.changeTopLevelLoop(OldLoop, NewLoop);
    628   }
    629 
    630   /// addTopLevelLoop - This adds the specified loop to the collection of
    631   /// top-level loops.
    632   inline void addTopLevelLoop(Loop *New) {
    633     LI.addTopLevelLoop(New);
    634   }
    635 
    636   /// removeBlock - This method completely removes BB from all data structures,
    637   /// including all of the Loop objects it is nested in and our mapping from
    638   /// BasicBlocks to loops.
    639   void removeBlock(BasicBlock *BB) {
    640     LI.removeBlock(BB);
    641   }
    642 
    643   /// updateUnloop - Update LoopInfo after removing the last backedge from a
    644   /// loop--now the "unloop". This updates the loop forest and parent loops for
    645   /// each block so that Unloop is no longer referenced, but the caller must
    646   /// actually delete the Unloop object.
    647   void updateUnloop(Loop *Unloop);
    648 
    649   /// replacementPreservesLCSSAForm - Returns true if replacing From with To
    650   /// everywhere is guaranteed to preserve LCSSA form.
    651   bool replacementPreservesLCSSAForm(Instruction *From, Value *To) {
    652     // Preserving LCSSA form is only problematic if the replacing value is an
    653     // instruction.
    654     Instruction *I = dyn_cast<Instruction>(To);
    655     if (!I) return true;
    656     // If both instructions are defined in the same basic block then replacement
    657     // cannot break LCSSA form.
    658     if (I->getParent() == From->getParent())
    659       return true;
    660     // If the instruction is not defined in a loop then it can safely replace
    661     // anything.
    662     Loop *ToLoop = getLoopFor(I->getParent());
    663     if (!ToLoop) return true;
    664     // If the replacing instruction is defined in the same loop as the original
    665     // instruction, or in a loop that contains it as an inner loop, then using
    666     // it as a replacement will not break LCSSA form.
    667     return ToLoop->contains(getLoopFor(From->getParent()));
    668   }
    669 };
    670 
    671 
    672 // Allow clients to walk the list of nested loops...
    673 template <> struct GraphTraits<const Loop*> {
    674   typedef const Loop NodeType;
    675   typedef LoopInfo::iterator ChildIteratorType;
    676 
    677   static NodeType *getEntryNode(const Loop *L) { return L; }
    678   static inline ChildIteratorType child_begin(NodeType *N) {
    679     return N->begin();
    680   }
    681   static inline ChildIteratorType child_end(NodeType *N) {
    682     return N->end();
    683   }
    684 };
    685 
    686 template <> struct GraphTraits<Loop*> {
    687   typedef Loop NodeType;
    688   typedef LoopInfo::iterator ChildIteratorType;
    689 
    690   static NodeType *getEntryNode(Loop *L) { return L; }
    691   static inline ChildIteratorType child_begin(NodeType *N) {
    692     return N->begin();
    693   }
    694   static inline ChildIteratorType child_end(NodeType *N) {
    695     return N->end();
    696   }
    697 };
    698 
    699 } // End llvm namespace
    700 
    701 #endif
    702