MachineBasicBlock.h revision 792699c46ef9bfc47dd459bbfa7e71bcb2cee29a
154e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton//===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- C++ -*-===// 254e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton// 354e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton// The LLVM Compiler Infrastructure 454e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton// 554e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton// This file was developed by the LLVM research group and is distributed under 654e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton// the University of Illinois Open Source License. See LICENSE.TXT for details. 754e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton// 854e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton//===----------------------------------------------------------------------===// 954e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton// 1054e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton// Collect the sequence of machine instructions for a basic block. 1127cf23214479177c83f115b4dbac92b2dba2b3b2Greg Clayton// 1254e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton//===----------------------------------------------------------------------===// 1354e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton 14#ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H 15#define LLVM_CODEGEN_MACHINEBASICBLOCK_H 16 17#include "llvm/CodeGen/MachineInstr.h" 18#include "Support/GraphTraits.h" 19#include "Support/ilist" 20#include <iosfwd> 21 22namespace llvm { 23 class MachineFunction; 24 25// ilist_traits 26template <> 27class ilist_traits<MachineInstr> { 28 // this is only set by the MachineBasicBlock owning the ilist 29 friend class MachineBasicBlock; 30 MachineBasicBlock* parent; 31 32public: 33 ilist_traits<MachineInstr>() : parent(0) { } 34 35 static MachineInstr* getPrev(MachineInstr* N) { return N->prev; } 36 static MachineInstr* getNext(MachineInstr* N) { return N->next; } 37 38 static const MachineInstr* 39 getPrev(const MachineInstr* N) { return N->prev; } 40 41 static const MachineInstr* 42 getNext(const MachineInstr* N) { return N->next; } 43 44 static void setPrev(MachineInstr* N, MachineInstr* prev) { N->prev = prev; } 45 static void setNext(MachineInstr* N, MachineInstr* next) { N->next = next; } 46 47 static MachineInstr* createNode(); 48 void addNodeToList(MachineInstr* N); 49 void removeNodeFromList(MachineInstr* N); 50 void transferNodesFromList( 51 iplist<MachineInstr, ilist_traits<MachineInstr> >& toList, 52 ilist_iterator<MachineInstr> first, 53 ilist_iterator<MachineInstr> last); 54}; 55 56class BasicBlock; 57 58class MachineBasicBlock { 59public: 60 typedef ilist<MachineInstr> Instructions; 61 Instructions Insts; 62 MachineBasicBlock *Prev, *Next; 63 const BasicBlock *BB; 64 std::vector<MachineBasicBlock *> Predecessors; 65 std::vector<MachineBasicBlock *> Successors; 66 int Number; 67 MachineFunction *Parent; 68 69public: 70 MachineBasicBlock(const BasicBlock *bb = 0) : Prev(0), Next(0), BB(bb), 71 Number(-1), Parent(0) { 72 Insts.parent = this; 73 } 74 ~MachineBasicBlock() {} 75 76 /// getBasicBlock - Return the LLVM basic block that this instance 77 /// corresponded to originally. 78 /// 79 const BasicBlock *getBasicBlock() const { return BB; } 80 81 /// getParent - Return the MachineFunction containing this basic block. 82 /// 83 const MachineFunction *getParent() const { return Parent; } 84 MachineFunction *getParent() { return Parent; } 85 86 typedef ilist<MachineInstr>::iterator iterator; 87 typedef ilist<MachineInstr>::const_iterator const_iterator; 88 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 89 typedef std::reverse_iterator<iterator> reverse_iterator; 90 91 unsigned size() const { return Insts.size(); } 92 bool empty() const { return Insts.empty(); } 93 94 MachineInstr& front() { return Insts.front(); } 95 MachineInstr& back() { return Insts.back(); } 96 97 iterator begin() { return Insts.begin(); } 98 const_iterator begin() const { return Insts.begin(); } 99 iterator end() { return Insts.end(); } 100 const_iterator end() const { return Insts.end(); } 101 reverse_iterator rbegin() { return Insts.rbegin(); } 102 const_reverse_iterator rbegin() const { return Insts.rbegin(); } 103 reverse_iterator rend () { return Insts.rend(); } 104 const_reverse_iterator rend () const { return Insts.rend(); } 105 106 // Machine-CFG iterators 107 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator; 108 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator; 109 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator; 110 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator; 111 112 pred_iterator pred_begin() { return Predecessors.begin (); } 113 const_pred_iterator pred_begin() const { return Predecessors.begin (); } 114 pred_iterator pred_end() { return Predecessors.end (); } 115 const_pred_iterator pred_end() const { return Predecessors.end (); } 116 unsigned pred_size() const { return Predecessors.size (); } 117 succ_iterator succ_begin() { return Successors.begin (); } 118 const_succ_iterator succ_begin() const { return Successors.begin (); } 119 succ_iterator succ_end() { return Successors.end (); } 120 const_succ_iterator succ_end() const { return Successors.end (); } 121 unsigned succ_size() const { return Successors.size (); } 122 123 // Machine-CFG mutators 124 125 /// addSuccessor - Add succ as a successor of this MachineBasicBlock. 126 /// The Predecessors list of succ is automatically updated. 127 /// 128 void addSuccessor (MachineBasicBlock *succ) { 129 Successors.push_back (succ); 130 succ->addPredecessor (this); 131 } 132 133 /// removeSuccessor - Remove succ from the successors list of this 134 /// MachineBasicBlock. The Predecessors list of succ is automatically updated. 135 /// 136 void removeSuccessor (MachineBasicBlock *succ) { 137 succ->removePredecessor (this); 138 std::vector<MachineBasicBlock *>::iterator goner = 139 std::find (Successors.begin(), Successors.end (), succ); 140 Successors.erase (goner); 141 } 142 143 /// getFirstTerminator - returns an iterator to the first terminator 144 /// instruction of this basic block. If a terminator does not exist, 145 /// it returns end() 146 iterator getFirstTerminator(); 147 148 void push_back(MachineInstr *MI) { Insts.push_back(MI); } 149 template<typename IT> 150 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); } 151 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); } 152 153 // erase - Remove the specified element or range from the instruction list. 154 // These functions delete any instructions removed. 155 // 156 iterator erase(iterator I) { return Insts.erase(I); } 157 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); } 158 MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); } 159 void clear() { Insts.clear(); } 160 161 // Debugging methods. 162 void dump() const; 163 void print(std::ostream &OS) const; 164 165 /// getNumber - MachineBasicBlocks are uniquely numbered at the function 166 /// level, unless they're not in a MachineFunction yet, in which case this 167 /// will return -1. 168 /// 169 int getNumber() const { return Number; } 170 171private: // Methods used to maintain doubly linked list of blocks... 172 friend class ilist_traits<MachineBasicBlock>; 173 174 MachineBasicBlock *getPrev() const { return Prev; } 175 MachineBasicBlock *getNext() const { return Next; } 176 void setPrev(MachineBasicBlock *P) { Prev = P; } 177 void setNext(MachineBasicBlock *N) { Next = N; } 178 179 // Machine-CFG mutators 180 181 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock. 182 /// Don't do this unless you know what you're doing, because it doesn't 183 /// update pred's successors list. Use pred->addSuccessor instead. 184 /// 185 void addPredecessor (MachineBasicBlock *pred) { 186 Predecessors.push_back (pred); 187 } 188 189 /// removePredecessor - Remove pred as a predecessor of this 190 /// MachineBasicBlock. Don't do this unless you know what you're 191 /// doing, because it doesn't update pred's successors list. Use 192 /// pred->removeSuccessor instead. 193 /// 194 void removePredecessor (MachineBasicBlock *pred) { 195 std::vector<MachineBasicBlock *>::iterator goner = 196 std::find (Predecessors.begin(), Predecessors.end (), pred); 197 Predecessors.erase (goner); 198 } 199}; 200 201 202//===--------------------------------------------------------------------===// 203// GraphTraits specializations for machine basic block graphs (machine-CFGs) 204//===--------------------------------------------------------------------===// 205 206// Provide specializations of GraphTraits to be able to treat a 207// MachineFunction as a graph of MachineBasicBlocks... 208// 209 210template <> struct GraphTraits<MachineBasicBlock *> { 211 typedef MachineBasicBlock NodeType; 212 typedef MachineBasicBlock::succ_iterator ChildIteratorType; 213 214 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; } 215 static inline ChildIteratorType child_begin(NodeType *N) { 216 return N->succ_begin(); 217 } 218 static inline ChildIteratorType child_end(NodeType *N) { 219 return N->succ_end(); 220 } 221}; 222 223template <> struct GraphTraits<const MachineBasicBlock *> { 224 typedef const MachineBasicBlock NodeType; 225 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType; 226 227 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; } 228 static inline ChildIteratorType child_begin(NodeType *N) { 229 return N->succ_begin(); 230 } 231 static inline ChildIteratorType child_end(NodeType *N) { 232 return N->succ_end(); 233 } 234}; 235 236// Provide specializations of GraphTraits to be able to treat a 237// MachineFunction as a graph of MachineBasicBlocks... and to walk it 238// in inverse order. Inverse order for a function is considered 239// to be when traversing the predecessor edges of a MBB 240// instead of the successor edges. 241// 242template <> struct GraphTraits<Inverse<MachineBasicBlock*> > { 243 typedef MachineBasicBlock NodeType; 244 typedef MachineBasicBlock::pred_iterator ChildIteratorType; 245 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) { 246 return G.Graph; 247 } 248 static inline ChildIteratorType child_begin(NodeType *N) { 249 return N->pred_begin(); 250 } 251 static inline ChildIteratorType child_end(NodeType *N) { 252 return N->pred_end(); 253 } 254}; 255 256template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > { 257 typedef const MachineBasicBlock NodeType; 258 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType; 259 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) { 260 return G.Graph; 261 } 262 static inline ChildIteratorType child_begin(NodeType *N) { 263 return N->pred_begin(); 264 } 265 static inline ChildIteratorType child_end(NodeType *N) { 266 return N->pred_end(); 267 } 268}; 269 270} // End llvm namespace 271 272#endif 273