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_MACHINE_LOOP_INFO_H 00031 #define LLVM_CODEGEN_MACHINE_LOOP_INFO_H 00032 00033 #include "llvm/CodeGen/MachineFunctionPass.h" 00034 #include "llvm/Analysis/LoopInfo.h" 00035 00036 namespace llvm { 00037 00038 // Provide overrides for Loop methods that don't make sense for machine loops. 00039 template<> inline 00040 PHINode *LoopBase<MachineBasicBlock>::getCanonicalInductionVariable() const { 00041 assert(0 && "getCanonicalInductionVariable not supported for machine loops!"); 00042 return 0; 00043 } 00044 00045 template<> inline Instruction* 00046 LoopBase<MachineBasicBlock>::getCanonicalInductionVariableIncrement() const { 00047 assert(0 && 00048 "getCanonicalInductionVariableIncrement not supported for machine loops!"); 00049 return 0; 00050 } 00051 00052 template<> 00053 inline bool LoopBase<MachineBasicBlock>::isLoopInvariant(Value *V) const { 00054 assert(0 && "isLoopInvariant not supported for machine loops!"); 00055 return false; 00056 } 00057 00058 template<> 00059 inline Value *LoopBase<MachineBasicBlock>::getTripCount() const { 00060 assert(0 && "getTripCount not supported for machine loops!"); 00061 return 0; 00062 } 00063 00064 template<> 00065 inline bool LoopBase<MachineBasicBlock>::isLCSSAForm() const { 00066 assert(0 && "isLCSSAForm not supported for machine loops"); 00067 return false; 00068 } 00069 00070 typedef LoopBase<MachineBasicBlock> MachineLoop; 00071 00072 class MachineLoopInfo : public MachineFunctionPass { 00073 LoopInfoBase<MachineBasicBlock>* LI; 00074 friend class LoopBase<MachineBasicBlock>; 00075 00076 LoopInfoBase<MachineBasicBlock>& getBase() { return *LI; } 00077 public: 00078 static char ID; // Pass identification, replacement for typeid 00079 00080 MachineLoopInfo() : MachineFunctionPass(intptr_t(&ID)) { 00081 LI = new LoopInfoBase<MachineBasicBlock>(); 00082 } 00083 00084 ~MachineLoopInfo() { delete LI; } 00085 00086 /// iterator/begin/end - The interface to the top-level loops in the current 00087 /// function. 00088 /// 00089 typedef std::vector<MachineLoop*>::const_iterator iterator; 00090 inline iterator begin() const { return LI->begin(); } 00091 inline iterator end() const { return LI->end(); } 00092 bool empty() const { return LI->empty(); } 00093 00094 /// getLoopFor - Return the inner most loop that BB lives in. If a basic 00095 /// block is in no loop (for example the entry node), null is returned. 00096 /// 00097 inline MachineLoop *getLoopFor(const MachineBasicBlock *BB) const { 00098 return LI->getLoopFor(BB); 00099 } 00100 00101 /// operator[] - same as getLoopFor... 00102 /// 00103 inline const MachineLoop *operator[](const MachineBasicBlock *BB) const { 00104 return LI->getLoopFor(BB); 00105 } 00106 00107 /// getLoopDepth - Return the loop nesting level of the specified block... 00108 /// 00109 inline unsigned getLoopDepth(const MachineBasicBlock *BB) const { 00110 return LI->getLoopDepth(BB); 00111 } 00112 00113 // isLoopHeader - True if the block is a loop header node 00114 inline bool isLoopHeader(MachineBasicBlock *BB) const { 00115 return LI->isLoopHeader(BB); 00116 } 00117 00118 /// runOnFunction - Calculate the natural loop information. 00119 /// 00120 virtual bool runOnMachineFunction(MachineFunction &F); 00121 00122 virtual void releaseMemory() { LI->releaseMemory(); } 00123 00124 virtual void getAnalysisUsage(AnalysisUsage &AU) const; 00125 00126 /// removeLoop - This removes the specified top-level loop from this loop info 00127 /// object. The loop is not deleted, as it will presumably be inserted into 00128 /// another loop. 00129 inline MachineLoop *removeLoop(iterator I) { return LI->removeLoop(I); } 00130 00131 /// changeLoopFor - Change the top-level loop that contains BB to the 00132 /// specified loop. This should be used by transformations that restructure 00133 /// the loop hierarchy tree. 00134 inline void changeLoopFor(MachineBasicBlock *BB, MachineLoop *L) { 00135 LI->changeLoopFor(BB, L); 00136 } 00137 00138 /// changeTopLevelLoop - Replace the specified loop in the top-level loops 00139 /// list with the indicated loop. 00140 inline void changeTopLevelLoop(MachineLoop *OldLoop, MachineLoop *NewLoop) { 00141 LI->changeTopLevelLoop(OldLoop, NewLoop); 00142 } 00143 00144 /// addTopLevelLoop - This adds the specified loop to the collection of 00145 /// top-level loops. 00146 inline void addTopLevelLoop(MachineLoop *New) { 00147 LI->addTopLevelLoop(New); 00148 } 00149 00150 /// removeBlock - This method completely removes BB from all data structures, 00151 /// including all of the Loop objects it is nested in and our mapping from 00152 /// MachineBasicBlocks to loops. 00153 void removeBlock(MachineBasicBlock *BB) { 00154 LI->removeBlock(BB); 00155 } 00156 }; 00157 00158 00159 // Allow clients to walk the list of nested loops... 00160 template <> struct GraphTraits<const MachineLoop*> { 00161 typedef const MachineLoop NodeType; 00162 typedef std::vector<MachineLoop*>::const_iterator ChildIteratorType; 00163 00164 static NodeType *getEntryNode(const MachineLoop *L) { return L; } 00165 static inline ChildIteratorType child_begin(NodeType *N) { 00166 return N->begin(); 00167 } 00168 static inline ChildIteratorType child_end(NodeType *N) { 00169 return N->end(); 00170 } 00171 }; 00172 00173 template <> struct GraphTraits<MachineLoop*> { 00174 typedef MachineLoop NodeType; 00175 typedef std::vector<MachineLoop*>::const_iterator ChildIteratorType; 00176 00177 static NodeType *getEntryNode(MachineLoop *L) { return L; } 00178 static inline ChildIteratorType child_begin(NodeType *N) { 00179 return N->begin(); 00180 } 00181 static inline ChildIteratorType child_end(NodeType *N) { 00182 return N->end(); 00183 } 00184 }; 00185 00186 } // End llvm namespace 00187 00188 #endif