LLVM API Documentation
00001 //===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- 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 // Collect the sequence of machine instructions for a basic block. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H 00015 #define LLVM_CODEGEN_MACHINEBASICBLOCK_H 00016 00017 #include "llvm/CodeGen/MachineInstr.h" 00018 #include "llvm/ADT/GraphTraits.h" 00019 #include "llvm/Support/Streams.h" 00020 00021 namespace llvm { 00022 00023 class BasicBlock; 00024 class MachineFunction; 00025 00026 template <> 00027 struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> { 00028 private: 00029 mutable MachineInstr Sentinel; 00030 00031 // this is only set by the MachineBasicBlock owning the LiveList 00032 friend class MachineBasicBlock; 00033 MachineBasicBlock* Parent; 00034 00035 public: 00036 MachineInstr *createSentinel() const { return &Sentinel; } 00037 void destroySentinel(MachineInstr *) const {} 00038 00039 void addNodeToList(MachineInstr* N); 00040 void removeNodeFromList(MachineInstr* N); 00041 void transferNodesFromList(ilist_traits &SrcTraits, 00042 ilist_iterator<MachineInstr> first, 00043 ilist_iterator<MachineInstr> last); 00044 void deleteNode(MachineInstr *N); 00045 private: 00046 void createNode(const MachineInstr &); 00047 }; 00048 00049 class MachineBasicBlock : public ilist_node<MachineBasicBlock> { 00050 typedef ilist<MachineInstr> Instructions; 00051 Instructions Insts; 00052 const BasicBlock *BB; 00053 int Number; 00054 MachineFunction *xParent; 00055 00056 /// Predecessors/Successors - Keep track of the predecessor / successor 00057 /// basicblocks. 00058 std::vector<MachineBasicBlock *> Predecessors; 00059 std::vector<MachineBasicBlock *> Successors; 00060 00061 /// LiveIns - Keep track of the physical registers that are livein of 00062 /// the basicblock. 00063 std::vector<unsigned> LiveIns; 00064 00065 /// Alignment - Alignment of the basic block. Zero if the basic block does 00066 /// not need to be aligned. 00067 unsigned Alignment; 00068 00069 /// IsLandingPad - Indicate that this basic block is entered via an 00070 /// exception handler. 00071 bool IsLandingPad; 00072 00073 // Intrusive list support 00074 friend struct ilist_sentinel_traits<MachineBasicBlock>; 00075 MachineBasicBlock() {} 00076 00077 explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb); 00078 00079 ~MachineBasicBlock(); 00080 00081 // MachineBasicBlocks are allocated and owned by MachineFunction. 00082 friend class MachineFunction; 00083 00084 public: 00085 /// getBasicBlock - Return the LLVM basic block that this instance 00086 /// corresponded to originally. 00087 /// 00088 const BasicBlock *getBasicBlock() const { return BB; } 00089 00090 /// getParent - Return the MachineFunction containing this basic block. 00091 /// 00092 const MachineFunction *getParent() const { return xParent; } 00093 MachineFunction *getParent() { return xParent; } 00094 00095 typedef Instructions::iterator iterator; 00096 typedef Instructions::const_iterator const_iterator; 00097 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00098 typedef std::reverse_iterator<iterator> reverse_iterator; 00099 00100 unsigned size() const { return (unsigned)Insts.size(); } 00101 bool empty() const { return Insts.empty(); } 00102 00103 MachineInstr& front() { return Insts.front(); } 00104 MachineInstr& back() { return Insts.back(); } 00105 const MachineInstr& front() const { return Insts.front(); } 00106 const MachineInstr& back() const { return Insts.back(); } 00107 00108 iterator begin() { return Insts.begin(); } 00109 const_iterator begin() const { return Insts.begin(); } 00110 iterator end() { return Insts.end(); } 00111 const_iterator end() const { return Insts.end(); } 00112 reverse_iterator rbegin() { return Insts.rbegin(); } 00113 const_reverse_iterator rbegin() const { return Insts.rbegin(); } 00114 reverse_iterator rend () { return Insts.rend(); } 00115 const_reverse_iterator rend () const { return Insts.rend(); } 00116 00117 // Machine-CFG iterators 00118 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator; 00119 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator; 00120 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator; 00121 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator; 00122 typedef std::vector<MachineBasicBlock *>::reverse_iterator 00123 pred_reverse_iterator; 00124 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator 00125 const_pred_reverse_iterator; 00126 typedef std::vector<MachineBasicBlock *>::reverse_iterator 00127 succ_reverse_iterator; 00128 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator 00129 const_succ_reverse_iterator; 00130 00131 pred_iterator pred_begin() { return Predecessors.begin(); } 00132 const_pred_iterator pred_begin() const { return Predecessors.begin(); } 00133 pred_iterator pred_end() { return Predecessors.end(); } 00134 const_pred_iterator pred_end() const { return Predecessors.end(); } 00135 pred_reverse_iterator pred_rbegin() 00136 { return Predecessors.rbegin();} 00137 const_pred_reverse_iterator pred_rbegin() const 00138 { return Predecessors.rbegin();} 00139 pred_reverse_iterator pred_rend() 00140 { return Predecessors.rend(); } 00141 const_pred_reverse_iterator pred_rend() const 00142 { return Predecessors.rend(); } 00143 unsigned pred_size() const { 00144 return (unsigned)Predecessors.size(); 00145 } 00146 bool pred_empty() const { return Predecessors.empty(); } 00147 succ_iterator succ_begin() { return Successors.begin(); } 00148 const_succ_iterator succ_begin() const { return Successors.begin(); } 00149 succ_iterator succ_end() { return Successors.end(); } 00150 const_succ_iterator succ_end() const { return Successors.end(); } 00151 succ_reverse_iterator succ_rbegin() 00152 { return Successors.rbegin(); } 00153 const_succ_reverse_iterator succ_rbegin() const 00154 { return Successors.rbegin(); } 00155 succ_reverse_iterator succ_rend() 00156 { return Successors.rend(); } 00157 const_succ_reverse_iterator succ_rend() const 00158 { return Successors.rend(); } 00159 unsigned succ_size() const { 00160 return (unsigned)Successors.size(); 00161 } 00162 bool succ_empty() const { return Successors.empty(); } 00163 00164 // LiveIn management methods. 00165 00166 /// addLiveIn - Add the specified register as a live in. Note that it 00167 /// is an error to add the same register to the same set more than once. 00168 void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); } 00169 00170 /// removeLiveIn - Remove the specified register from the live in set. 00171 /// 00172 void removeLiveIn(unsigned Reg); 00173 00174 /// isLiveIn - Return true if the specified register is in the live in set. 00175 /// 00176 bool isLiveIn(unsigned Reg) const; 00177 00178 // Iteration support for live in sets. These sets are kept in sorted 00179 // order by their register number. 00180 typedef std::vector<unsigned>::iterator livein_iterator; 00181 typedef std::vector<unsigned>::const_iterator const_livein_iterator; 00182 livein_iterator livein_begin() { return LiveIns.begin(); } 00183 const_livein_iterator livein_begin() const { return LiveIns.begin(); } 00184 livein_iterator livein_end() { return LiveIns.end(); } 00185 const_livein_iterator livein_end() const { return LiveIns.end(); } 00186 bool livein_empty() const { return LiveIns.empty(); } 00187 00188 /// getAlignment - Return alignment of the basic block. 00189 /// 00190 unsigned getAlignment() const { return Alignment; } 00191 00192 /// setAlignment - Set alignment of the basic block. 00193 /// 00194 void setAlignment(unsigned Align) { Alignment = Align; } 00195 00196 /// isLandingPad - Returns true if the block is a landing pad. That is 00197 /// this basic block is entered via an exception handler. 00198 bool isLandingPad() const { return IsLandingPad; } 00199 00200 /// setIsLandingPad - Indicates the block is a landing pad. That is 00201 /// this basic block is entered via an exception handler. 00202 void setIsLandingPad() { IsLandingPad = true; } 00203 00204 // Code Layout methods. 00205 00206 /// moveBefore/moveAfter - move 'this' block before or after the specified 00207 /// block. This only moves the block, it does not modify the CFG or adjust 00208 /// potential fall-throughs at the end of the block. 00209 void moveBefore(MachineBasicBlock *NewAfter); 00210 void moveAfter(MachineBasicBlock *NewBefore); 00211 00212 // Machine-CFG mutators 00213 00214 /// addSuccessor - Add succ as a successor of this MachineBasicBlock. 00215 /// The Predecessors list of succ is automatically updated. 00216 /// 00217 void addSuccessor(MachineBasicBlock *succ); 00218 00219 /// removeSuccessor - Remove successor from the successors list of this 00220 /// MachineBasicBlock. The Predecessors list of succ is automatically updated. 00221 /// 00222 void removeSuccessor(MachineBasicBlock *succ); 00223 00224 /// removeSuccessor - Remove specified successor from the successors list of 00225 /// this MachineBasicBlock. The Predecessors list of succ is automatically 00226 /// updated. Return the iterator to the element after the one removed. 00227 /// 00228 succ_iterator removeSuccessor(succ_iterator I); 00229 00230 /// transferSuccessors - Transfers all the successors from MBB to this 00231 /// machine basic block (i.e., copies all the successors fromMBB and 00232 /// remove all the successors fromBB). 00233 void transferSuccessors(MachineBasicBlock *fromMBB); 00234 00235 /// isSuccessor - Return true if the specified MBB is a successor of this 00236 /// block. 00237 bool isSuccessor(MachineBasicBlock *MBB) const; 00238 00239 /// isLayoutSuccessor - Return true if the specified MBB will be emitted 00240 /// immediately after this block, such that if this block exits by 00241 /// falling through, control will transfer to the specified MBB. Note 00242 /// that MBB need not be a successor at all, for example if this block 00243 /// ends with an unconditional branch to some other block. 00244 bool isLayoutSuccessor(MachineBasicBlock *MBB) const; 00245 00246 /// getFirstTerminator - returns an iterator to the first terminator 00247 /// instruction of this basic block. If a terminator does not exist, 00248 /// it returns end() 00249 iterator getFirstTerminator(); 00250 00251 void pop_front() { Insts.pop_front(); } 00252 void pop_back() { Insts.pop_back(); } 00253 void push_back(MachineInstr *MI) { Insts.push_back(MI); } 00254 template<typename IT> 00255 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); } 00256 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); } 00257 00258 // erase - Remove the specified element or range from the instruction list. 00259 // These functions delete any instructions removed. 00260 // 00261 iterator erase(iterator I) { return Insts.erase(I); } 00262 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); } 00263 MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); } 00264 void clear() { Insts.clear(); } 00265 00266 /// splice - Take an instruction from MBB 'Other' at the position From, 00267 /// and insert it into this MBB right before 'where'. 00268 void splice(iterator where, MachineBasicBlock *Other, iterator From) { 00269 Insts.splice(where, Other->Insts, From); 00270 } 00271 00272 /// splice - Take a block of instructions from MBB 'Other' in the range [From, 00273 /// To), and insert them into this MBB right before 'where'. 00274 void splice(iterator where, MachineBasicBlock *Other, iterator From, 00275 iterator To) { 00276 Insts.splice(where, Other->Insts, From, To); 00277 } 00278 00279 /// removeFromParent - This method unlinks 'this' from the containing 00280 /// function, and returns it, but does not delete it. 00281 MachineBasicBlock *removeFromParent(); 00282 00283 /// eraseFromParent - This method unlinks 'this' from the containing 00284 /// function and deletes it. 00285 void eraseFromParent(); 00286 00287 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to 00288 /// 'Old', change the code and CFG so that it branches to 'New' instead. 00289 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New); 00290 00291 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in 00292 /// the CFG to be inserted. If we have proven that MBB can only branch to 00293 /// DestA and DestB, remove any other MBB successors from the CFG. DestA and 00294 /// DestB can be null. Besides DestA and DestB, retain other edges leading 00295 /// to LandingPads (currently there can be only one; we don't check or require 00296 /// that here). Note it is possible that DestA and/or DestB are LandingPads. 00297 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA, 00298 MachineBasicBlock *DestB, 00299 bool isCond); 00300 00301 // Debugging methods. 00302 void dump() const; 00303 void print(std::ostream &OS) const; 00304 void print(std::ostream *OS) const { if (OS) print(*OS); } 00305 00306 /// getNumber - MachineBasicBlocks are uniquely numbered at the function 00307 /// level, unless they're not in a MachineFunction yet, in which case this 00308 /// will return -1. 00309 /// 00310 int getNumber() const { return Number; } 00311 void setNumber(int N) { Number = N; } 00312 00313 private: // Methods used to maintain doubly linked list of blocks... 00314 friend struct ilist_traits<MachineBasicBlock>; 00315 00316 // Machine-CFG mutators 00317 00318 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock. 00319 /// Don't do this unless you know what you're doing, because it doesn't 00320 /// update pred's successors list. Use pred->addSuccessor instead. 00321 /// 00322 void addPredecessor(MachineBasicBlock *pred); 00323 00324 /// removePredecessor - Remove pred as a predecessor of this 00325 /// MachineBasicBlock. Don't do this unless you know what you're 00326 /// doing, because it doesn't update pred's successors list. Use 00327 /// pred->removeSuccessor instead. 00328 /// 00329 void removePredecessor(MachineBasicBlock *pred); 00330 }; 00331 00332 std::ostream& operator<<(std::ostream &OS, const MachineBasicBlock &MBB); 00333 00334 //===--------------------------------------------------------------------===// 00335 // GraphTraits specializations for machine basic block graphs (machine-CFGs) 00336 //===--------------------------------------------------------------------===// 00337 00338 // Provide specializations of GraphTraits to be able to treat a 00339 // MachineFunction as a graph of MachineBasicBlocks... 00340 // 00341 00342 template <> struct GraphTraits<MachineBasicBlock *> { 00343 typedef MachineBasicBlock NodeType; 00344 typedef MachineBasicBlock::succ_iterator ChildIteratorType; 00345 00346 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; } 00347 static inline ChildIteratorType child_begin(NodeType *N) { 00348 return N->succ_begin(); 00349 } 00350 static inline ChildIteratorType child_end(NodeType *N) { 00351 return N->succ_end(); 00352 } 00353 }; 00354 00355 template <> struct GraphTraits<const MachineBasicBlock *> { 00356 typedef const MachineBasicBlock NodeType; 00357 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType; 00358 00359 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; } 00360 static inline ChildIteratorType child_begin(NodeType *N) { 00361 return N->succ_begin(); 00362 } 00363 static inline ChildIteratorType child_end(NodeType *N) { 00364 return N->succ_end(); 00365 } 00366 }; 00367 00368 // Provide specializations of GraphTraits to be able to treat a 00369 // MachineFunction as a graph of MachineBasicBlocks... and to walk it 00370 // in inverse order. Inverse order for a function is considered 00371 // to be when traversing the predecessor edges of a MBB 00372 // instead of the successor edges. 00373 // 00374 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > { 00375 typedef MachineBasicBlock NodeType; 00376 typedef MachineBasicBlock::pred_iterator ChildIteratorType; 00377 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) { 00378 return G.Graph; 00379 } 00380 static inline ChildIteratorType child_begin(NodeType *N) { 00381 return N->pred_begin(); 00382 } 00383 static inline ChildIteratorType child_end(NodeType *N) { 00384 return N->pred_end(); 00385 } 00386 }; 00387 00388 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > { 00389 typedef const MachineBasicBlock NodeType; 00390 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType; 00391 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) { 00392 return G.Graph; 00393 } 00394 static inline ChildIteratorType child_begin(NodeType *N) { 00395 return N->pred_begin(); 00396 } 00397 static inline ChildIteratorType child_end(NodeType *N) { 00398 return N->pred_end(); 00399 } 00400 }; 00401 00402 } // End llvm namespace 00403 00404 #endif