LLVM API Documentation
00001 //===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator -------*- C++ -*-===// 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 natural 00012 // loops may actually be several loops that share the same header node. 00013 // 00014 // This analysis calculates the nesting structure of loops in a function. For 00015 // each natural loop identified, this analysis identifies natural loops 00016 // contained entirely within the loop and the basic blocks the make up the loop. 00017 // 00018 // It can calculate on the fly various bits of information, for example: 00019 // 00020 // * whether there is a preheader for the loop 00021 // * the number of back edges to the header 00022 // * whether or not a particular block branches out of the loop 00023 // * the successor blocks of the loop 00024 // * the loop depth 00025 // * the trip count 00026 // * etc... 00027 // 00028 //===----------------------------------------------------------------------===// 00029 00030 #ifndef LLVM_ANALYSIS_LOOP_INFO_H 00031 #define LLVM_ANALYSIS_LOOP_INFO_H 00032 00033 #include "llvm/Pass.h" 00034 #include "llvm/ADT/GraphTraits.h" 00035 00036 namespace llvm { 00037 00038 struct DominatorSet; 00039 class LoopInfo; 00040 class PHINode; 00041 class Instruction; 00042 00043 //===----------------------------------------------------------------------===// 00044 /// Loop class - Instances of this class are used to represent loops that are 00045 /// detected in the flow graph 00046 /// 00047 class Loop { 00048 Loop *ParentLoop; 00049 std::vector<Loop*> SubLoops; // Loops contained entirely within this one 00050 std::vector<BasicBlock*> Blocks; // First entry is the header node 00051 00052 Loop(const Loop &); // DO NOT IMPLEMENT 00053 const Loop &operator=(const Loop &); // DO NOT IMPLEMENT 00054 public: 00055 /// Loop ctor - This creates an empty loop. 00056 Loop() : ParentLoop(0) {} 00057 ~Loop() { 00058 for (unsigned i = 0, e = SubLoops.size(); i != e; ++i) 00059 delete SubLoops[i]; 00060 } 00061 00062 unsigned getLoopDepth() const { 00063 unsigned D = 0; 00064 for (const Loop *CurLoop = this; CurLoop; CurLoop = CurLoop->ParentLoop) 00065 ++D; 00066 return D; 00067 } 00068 BasicBlock *getHeader() const { return Blocks.front(); } 00069 Loop *getParentLoop() const { return ParentLoop; } 00070 00071 /// contains - Return true of the specified basic block is in this loop 00072 /// 00073 bool contains(const BasicBlock *BB) const; 00074 00075 /// iterator/begin/end - Return the loops contained entirely within this loop. 00076 /// 00077 typedef std::vector<Loop*>::const_iterator iterator; 00078 iterator begin() const { return SubLoops.begin(); } 00079 iterator end() const { return SubLoops.end(); } 00080 00081 /// getBlocks - Get a list of the basic blocks which make up this loop. 00082 /// 00083 const std::vector<BasicBlock*> &getBlocks() const { return Blocks; } 00084 typedef std::vector<BasicBlock*>::const_iterator block_iterator; 00085 block_iterator block_begin() const { return Blocks.begin(); } 00086 block_iterator block_end() const { return Blocks.end(); } 00087 00088 /// isLoopExit - True if terminator in the block can branch to another block 00089 /// that is outside of the current loop. 00090 /// 00091 bool isLoopExit(const BasicBlock *BB) const; 00092 00093 /// getNumBackEdges - Calculate the number of back edges to the loop header 00094 /// 00095 unsigned getNumBackEdges() const; 00096 00097 /// isLoopInvariant - Return true if the specified value is loop invariant 00098 /// 00099 bool isLoopInvariant(Value *V) const; 00100 00101 //===--------------------------------------------------------------------===// 00102 // APIs for simple analysis of the loop. 00103 // 00104 // Note that all of these methods can fail on general loops (ie, there may not 00105 // be a preheader, etc). For best success, the loop simplification and 00106 // induction variable canonicalization pass should be used to normalize loops 00107 // for easy analysis. These methods assume canonical loops. 00108 00109 /// getExitBlocks - Return all of the successor blocks of this loop. These 00110 /// are the blocks _outside of the current loop_ which are branched to. 00111 /// 00112 void getExitBlocks(std::vector<BasicBlock*> &Blocks) const; 00113 00114 /// getLoopPreheader - If there is a preheader for this loop, return it. A 00115 /// loop has a preheader if there is only one edge to the header of the loop 00116 /// from outside of the loop. If this is the case, the block branching to the 00117 /// header of the loop is the preheader node. 00118 /// 00119 /// This method returns null if there is no preheader for the loop. 00120 /// 00121 BasicBlock *getLoopPreheader() const; 00122 00123 /// getCanonicalInductionVariable - Check to see if the loop has a canonical 00124 /// induction variable: an integer recurrence that starts at 0 and increments 00125 /// by one each time through the loop. If so, return the phi node that 00126 /// corresponds to it. 00127 /// 00128 PHINode *getCanonicalInductionVariable() const; 00129 00130 /// getCanonicalInductionVariableIncrement - Return the LLVM value that holds 00131 /// the canonical induction variable value for the "next" iteration of the 00132 /// loop. This always succeeds if getCanonicalInductionVariable succeeds. 00133 /// 00134 Instruction *getCanonicalInductionVariableIncrement() const; 00135 00136 /// getTripCount - Return a loop-invariant LLVM value indicating the number of 00137 /// times the loop will be executed. Note that this means that the backedge 00138 /// of the loop executes N-1 times. If the trip-count cannot be determined, 00139 /// this returns null. 00140 /// 00141 Value *getTripCount() const; 00142 00143 //===--------------------------------------------------------------------===// 00144 // APIs for updating loop information after changing the CFG 00145 // 00146 00147 /// addBasicBlockToLoop - This method is used by other analyses to update loop 00148 /// information. NewBB is set to be a new member of the current loop. 00149 /// Because of this, it is added as a member of all parent loops, and is added 00150 /// to the specified LoopInfo object as being in the current basic block. It 00151 /// is not valid to replace the loop header with this method. 00152 /// 00153 void addBasicBlockToLoop(BasicBlock *NewBB, LoopInfo &LI); 00154 00155 /// replaceChildLoopWith - This is used when splitting loops up. It replaces 00156 /// the OldChild entry in our children list with NewChild, and updates the 00157 /// parent pointer of OldChild to be null and the NewChild to be this loop. 00158 /// This updates the loop depth of the new child. 00159 void replaceChildLoopWith(Loop *OldChild, Loop *NewChild); 00160 00161 /// addChildLoop - Add the specified loop to be a child of this loop. This 00162 /// updates the loop depth of the new child. 00163 /// 00164 void addChildLoop(Loop *NewChild); 00165 00166 /// removeChildLoop - This removes the specified child from being a subloop of 00167 /// this loop. The loop is not deleted, as it will presumably be inserted 00168 /// into another loop. 00169 Loop *removeChildLoop(iterator OldChild); 00170 00171 /// addBlockEntry - This adds a basic block directly to the basic block list. 00172 /// This should only be used by transformations that create new loops. Other 00173 /// transformations should use addBasicBlockToLoop. 00174 void addBlockEntry(BasicBlock *BB) { 00175 Blocks.push_back(BB); 00176 } 00177 00178 /// removeBlockFromLoop - This removes the specified basic block from the 00179 /// current loop, updating the Blocks as appropriate. This does not update 00180 /// the mapping in the LoopInfo class. 00181 void removeBlockFromLoop(BasicBlock *BB); 00182 00183 void print(std::ostream &O, unsigned Depth = 0) const; 00184 void dump() const; 00185 private: 00186 friend class LoopInfo; 00187 Loop(BasicBlock *BB) : ParentLoop(0) { 00188 Blocks.push_back(BB); 00189 } 00190 }; 00191 00192 00193 00194 //===----------------------------------------------------------------------===// 00195 /// LoopInfo - This class builds and contains all of the top level loop 00196 /// structures in the specified function. 00197 /// 00198 class LoopInfo : public FunctionPass { 00199 // BBMap - Mapping of basic blocks to the inner most loop they occur in 00200 std::map<BasicBlock*, Loop*> BBMap; 00201 std::vector<Loop*> TopLevelLoops; 00202 friend class Loop; 00203 public: 00204 ~LoopInfo() { releaseMemory(); } 00205 00206 /// iterator/begin/end - The interface to the top-level loops in the current 00207 /// function. 00208 /// 00209 typedef std::vector<Loop*>::const_iterator iterator; 00210 iterator begin() const { return TopLevelLoops.begin(); } 00211 iterator end() const { return TopLevelLoops.end(); } 00212 00213 /// getLoopFor - Return the inner most loop that BB lives in. If a basic 00214 /// block is in no loop (for example the entry node), null is returned. 00215 /// 00216 Loop *getLoopFor(const BasicBlock *BB) const { 00217 std::map<BasicBlock *, Loop*>::const_iterator I=BBMap.find((BasicBlock*)BB); 00218 return I != BBMap.end() ? I->second : 0; 00219 } 00220 00221 /// operator[] - same as getLoopFor... 00222 /// 00223 const Loop *operator[](const BasicBlock *BB) const { 00224 return getLoopFor(BB); 00225 } 00226 00227 /// getLoopDepth - Return the loop nesting level of the specified block... 00228 /// 00229 unsigned getLoopDepth(const BasicBlock *BB) const { 00230 const Loop *L = getLoopFor(BB); 00231 return L ? L->getLoopDepth() : 0; 00232 } 00233 00234 // isLoopHeader - True if the block is a loop header node 00235 bool isLoopHeader(BasicBlock *BB) const { 00236 return getLoopFor(BB)->getHeader() == BB; 00237 } 00238 00239 /// runOnFunction - Calculate the natural loop information. 00240 /// 00241 virtual bool runOnFunction(Function &F); 00242 00243 virtual void releaseMemory(); 00244 void print(std::ostream &O) const; 00245 00246 /// getAnalysisUsage - Requires dominator sets 00247 /// 00248 virtual void getAnalysisUsage(AnalysisUsage &AU) const; 00249 00250 /// removeLoop - This removes the specified top-level loop from this loop info 00251 /// object. The loop is not deleted, as it will presumably be inserted into 00252 /// another loop. 00253 Loop *removeLoop(iterator I); 00254 00255 /// changeLoopFor - Change the top-level loop that contains BB to the 00256 /// specified loop. This should be used by transformations that restructure 00257 /// the loop hierarchy tree. 00258 void changeLoopFor(BasicBlock *BB, Loop *L); 00259 00260 /// changeTopLevelLoop - Replace the specified loop in the top-level loops 00261 /// list with the indicated loop. 00262 void changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop); 00263 00264 /// addTopLevelLoop - This adds the specified loop to the collection of 00265 /// top-level loops. 00266 void addTopLevelLoop(Loop *New) { 00267 assert(New->getParentLoop() == 0 && "Loop already in subloop!"); 00268 TopLevelLoops.push_back(New); 00269 } 00270 00271 /// removeBlock - This method completely removes BB from all data structures, 00272 /// including all of the Loop objects it is nested in and our mapping from 00273 /// BasicBlocks to loops. 00274 void removeBlock(BasicBlock *BB); 00275 00276 static void stub(); // Noop 00277 private: 00278 void Calculate(const DominatorSet &DS); 00279 Loop *ConsiderForLoop(BasicBlock *BB, const DominatorSet &DS); 00280 void MoveSiblingLoopInto(Loop *NewChild, Loop *NewParent); 00281 void InsertLoopInto(Loop *L, Loop *Parent); 00282 }; 00283 00284 00285 // Make sure that any clients of this file link in LoopInfo.cpp 00286 static IncludeFile 00287 LOOP_INFO_INCLUDE_FILE((void*)&LoopInfo::stub); 00288 00289 // Allow clients to walk the list of nested loops... 00290 template <> struct GraphTraits<const Loop*> { 00291 typedef const Loop NodeType; 00292 typedef std::vector<Loop*>::const_iterator ChildIteratorType; 00293 00294 static NodeType *getEntryNode(const Loop *L) { return L; } 00295 static inline ChildIteratorType child_begin(NodeType *N) { 00296 return N->begin(); 00297 } 00298 static inline ChildIteratorType child_end(NodeType *N) { 00299 return N->end(); 00300 } 00301 }; 00302 00303 template <> struct GraphTraits<Loop*> { 00304 typedef Loop NodeType; 00305 typedef std::vector<Loop*>::const_iterator ChildIteratorType; 00306 00307 static NodeType *getEntryNode(Loop *L) { return L; } 00308 static inline ChildIteratorType child_begin(NodeType *N) { 00309 return N->begin(); 00310 } 00311 static inline ChildIteratorType child_end(NodeType *N) { 00312 return N->end(); 00313 } 00314 }; 00315 00316 } // End llvm namespace 00317 00318 #endif