LLVM API Documentation
00001 //===- llvm/CodeGen/MachineLoopInfo.h - Natural Loop Calculator -*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the MachineLoopInfo class that is used to identify natural 00011 // loops and determine the loop depth of various nodes of the CFG. Note that 00012 // natural 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_CODEGEN_MACHINELOOPINFO_H 00031 #define LLVM_CODEGEN_MACHINELOOPINFO_H 00032 00033 #include "llvm/Analysis/LoopInfo.h" 00034 #include "llvm/CodeGen/MachineBasicBlock.h" 00035 #include "llvm/CodeGen/MachineFunctionPass.h" 00036 00037 namespace llvm { 00038 00039 // Implementation in LoopInfoImpl.h 00040 #ifdef __GNUC__ 00041 class MachineLoop; 00042 __extension__ extern template class LoopBase<MachineBasicBlock, MachineLoop>; 00043 #endif 00044 00045 class MachineLoop : public LoopBase<MachineBasicBlock, MachineLoop> { 00046 public: 00047 MachineLoop(); 00048 00049 /// getTopBlock - Return the "top" block in the loop, which is the first 00050 /// block in the linear layout, ignoring any parts of the loop not 00051 /// contiguous with the part the contains the header. 00052 MachineBasicBlock *getTopBlock(); 00053 00054 /// getBottomBlock - Return the "bottom" block in the loop, which is the last 00055 /// block in the linear layout, ignoring any parts of the loop not 00056 /// contiguous with the part the contains the header. 00057 MachineBasicBlock *getBottomBlock(); 00058 00059 void dump() const; 00060 00061 private: 00062 friend class LoopInfoBase<MachineBasicBlock, MachineLoop>; 00063 explicit MachineLoop(MachineBasicBlock *MBB) 00064 : LoopBase<MachineBasicBlock, MachineLoop>(MBB) {} 00065 }; 00066 00067 // Implementation in LoopInfoImpl.h 00068 #ifdef __GNUC__ 00069 __extension__ extern template 00070 class LoopInfoBase<MachineBasicBlock, MachineLoop>; 00071 #endif 00072 00073 class MachineLoopInfo : public MachineFunctionPass { 00074 LoopInfoBase<MachineBasicBlock, MachineLoop> LI; 00075 friend class LoopBase<MachineBasicBlock, MachineLoop>; 00076 00077 void operator=(const MachineLoopInfo &) LLVM_DELETED_FUNCTION; 00078 MachineLoopInfo(const MachineLoopInfo &) LLVM_DELETED_FUNCTION; 00079 00080 public: 00081 static char ID; // Pass identification, replacement for typeid 00082 00083 MachineLoopInfo() : MachineFunctionPass(ID) { 00084 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry()); 00085 } 00086 00087 LoopInfoBase<MachineBasicBlock, MachineLoop>& getBase() { return LI; } 00088 00089 /// iterator/begin/end - The interface to the top-level loops in the current 00090 /// function. 00091 /// 00092 typedef LoopInfoBase<MachineBasicBlock, MachineLoop>::iterator iterator; 00093 inline iterator begin() const { return LI.begin(); } 00094 inline iterator end() const { return LI.end(); } 00095 bool empty() const { return LI.empty(); } 00096 00097 /// getLoopFor - Return the inner most loop that BB lives in. If a basic 00098 /// block is in no loop (for example the entry node), null is returned. 00099 /// 00100 inline MachineLoop *getLoopFor(const MachineBasicBlock *BB) const { 00101 return LI.getLoopFor(BB); 00102 } 00103 00104 /// operator[] - same as getLoopFor... 00105 /// 00106 inline const MachineLoop *operator[](const MachineBasicBlock *BB) const { 00107 return LI.getLoopFor(BB); 00108 } 00109 00110 /// getLoopDepth - Return the loop nesting level of the specified block... 00111 /// 00112 inline unsigned getLoopDepth(const MachineBasicBlock *BB) const { 00113 return LI.getLoopDepth(BB); 00114 } 00115 00116 // isLoopHeader - True if the block is a loop header node 00117 inline bool isLoopHeader(MachineBasicBlock *BB) const { 00118 return LI.isLoopHeader(BB); 00119 } 00120 00121 /// runOnFunction - Calculate the natural loop information. 00122 /// 00123 bool runOnMachineFunction(MachineFunction &F) override; 00124 00125 void releaseMemory() override { LI.releaseMemory(); } 00126 00127 void getAnalysisUsage(AnalysisUsage &AU) const override; 00128 00129 /// removeLoop - This removes the specified top-level loop from this loop info 00130 /// object. The loop is not deleted, as it will presumably be inserted into 00131 /// another loop. 00132 inline MachineLoop *removeLoop(iterator I) { return LI.removeLoop(I); } 00133 00134 /// changeLoopFor - Change the top-level loop that contains BB to the 00135 /// specified loop. This should be used by transformations that restructure 00136 /// the loop hierarchy tree. 00137 inline void changeLoopFor(MachineBasicBlock *BB, MachineLoop *L) { 00138 LI.changeLoopFor(BB, L); 00139 } 00140 00141 /// changeTopLevelLoop - Replace the specified loop in the top-level loops 00142 /// list with the indicated loop. 00143 inline void changeTopLevelLoop(MachineLoop *OldLoop, MachineLoop *NewLoop) { 00144 LI.changeTopLevelLoop(OldLoop, NewLoop); 00145 } 00146 00147 /// addTopLevelLoop - This adds the specified loop to the collection of 00148 /// top-level loops. 00149 inline void addTopLevelLoop(MachineLoop *New) { 00150 LI.addTopLevelLoop(New); 00151 } 00152 00153 /// removeBlock - This method completely removes BB from all data structures, 00154 /// including all of the Loop objects it is nested in and our mapping from 00155 /// MachineBasicBlocks to loops. 00156 void removeBlock(MachineBasicBlock *BB) { 00157 LI.removeBlock(BB); 00158 } 00159 }; 00160 00161 00162 // Allow clients to walk the list of nested loops... 00163 template <> struct GraphTraits<const MachineLoop*> { 00164 typedef const MachineLoop NodeType; 00165 typedef MachineLoopInfo::iterator ChildIteratorType; 00166 00167 static NodeType *getEntryNode(const MachineLoop *L) { return L; } 00168 static inline ChildIteratorType child_begin(NodeType *N) { 00169 return N->begin(); 00170 } 00171 static inline ChildIteratorType child_end(NodeType *N) { 00172 return N->end(); 00173 } 00174 }; 00175 00176 template <> struct GraphTraits<MachineLoop*> { 00177 typedef MachineLoop NodeType; 00178 typedef MachineLoopInfo::iterator ChildIteratorType; 00179 00180 static NodeType *getEntryNode(MachineLoop *L) { return L; } 00181 static inline ChildIteratorType child_begin(NodeType *N) { 00182 return N->begin(); 00183 } 00184 static inline ChildIteratorType child_end(NodeType *N) { 00185 return N->end(); 00186 } 00187 }; 00188 00189 } // End llvm namespace 00190 00191 #endif