MachineBasicBlock.h revision 0aef12a7a96968a80c38144dfc0a7ae6a9152db9
1//===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// Collect the sequence of machine instructions for a basic block. 11// 12//===----------------------------------------------------------------------===// 13 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 67public: 68 MachineBasicBlock(const BasicBlock *bb = 0) : Prev(0), Next(0), BB(bb) { 69 Insts.parent = this; 70 } 71 ~MachineBasicBlock() {} 72 73 /// getBasicBlock - Return the LLVM basic block that this instance 74 /// corresponded to originally. 75 /// 76 const BasicBlock *getBasicBlock() const { return BB; } 77 78 /// getParent - Return the MachineFunction containing this basic block. 79 /// 80 const MachineFunction *getParent() const; 81 82 typedef ilist<MachineInstr>::iterator iterator; 83 typedef ilist<MachineInstr>::const_iterator const_iterator; 84 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 85 typedef std::reverse_iterator<iterator> reverse_iterator; 86 87 unsigned size() const { return Insts.size(); } 88 bool empty() const { return Insts.empty(); } 89 90 MachineInstr& front() { return Insts.front(); } 91 MachineInstr& back() { return Insts.back(); } 92 93 iterator begin() { return Insts.begin(); } 94 const_iterator begin() const { return Insts.begin(); } 95 iterator end() { return Insts.end(); } 96 const_iterator end() const { return Insts.end(); } 97 reverse_iterator rbegin() { return Insts.rbegin(); } 98 const_reverse_iterator rbegin() const { return Insts.rbegin(); } 99 reverse_iterator rend () { return Insts.rend(); } 100 const_reverse_iterator rend () const { return Insts.rend(); } 101 102 // Machine-CFG iterators 103 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator; 104 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator; 105 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator; 106 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator; 107 108 pred_iterator pred_begin() { return Predecessors.begin (); } 109 const_pred_iterator pred_begin() const { return Predecessors.begin (); } 110 pred_iterator pred_end() { return Predecessors.end (); } 111 const_pred_iterator pred_end() const { return Predecessors.end (); } 112 unsigned pred_size() const { return Predecessors.size (); } 113 succ_iterator succ_begin() { return Successors.begin (); } 114 const_succ_iterator succ_begin() const { return Successors.begin (); } 115 succ_iterator succ_end() { return Successors.end (); } 116 const_succ_iterator succ_end() const { return Successors.end (); } 117 unsigned succ_size() const { return Successors.size (); } 118 119 // Machine-CFG mutators 120 121 /// addSuccessor - Add succ as a successor of this MachineBasicBlock. 122 /// The Predecessors list of succ is automatically updated. 123 /// 124 void addSuccessor (MachineBasicBlock *succ) { 125 assert (std::find (Successors.begin (), Successors.end (), succ) 126 == Successors.end () 127 && "Trying to addSuccessor a MBB which is already my successor"); 128 Successors.push_back (succ); 129 succ->addPredecessor (this); 130 } 131 132 /// removeSuccessor - Remove succ from the successors list of this 133 /// MachineBasicBlock. The Predecessors list of succ is automatically updated. 134 /// 135 void removeSuccessor (MachineBasicBlock *succ) { 136 succ->removePredecessor (this); 137 std::vector<MachineBasicBlock *>::iterator goner = 138 std::find (Successors.begin(), Successors.end (), succ); 139 assert (goner != Successors.end () 140 && "Trying to removeSuccessor a MBB which isn't my successor"); 141 Successors.erase (goner); 142 } 143 144 /// getFirstTerminator - returns an iterator to the first terminator 145 /// instruction of this basic block. If a terminator does not exist, 146 /// it returns end() 147 iterator getFirstTerminator(); 148 149 void push_back(MachineInstr *MI) { Insts.push_back(MI); } 150 template<typename IT> 151 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); } 152 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); } 153 154 // erase - Remove the specified element or range from the instruction list. 155 // These functions delete any instructions removed. 156 // 157 iterator erase(iterator I) { return Insts.erase(I); } 158 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); } 159 MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); } 160 void clear() { Insts.clear(); } 161 162 // Debugging methods. 163 void dump() const; 164 void print(std::ostream &OS) const; 165 166private: // Methods used to maintain doubly linked list of blocks... 167 friend class ilist_traits<MachineBasicBlock>; 168 169 MachineBasicBlock *getPrev() const { return Prev; } 170 MachineBasicBlock *getNext() const { return Next; } 171 void setPrev(MachineBasicBlock *P) { Prev = P; } 172 void setNext(MachineBasicBlock *N) { Next = N; } 173 174 // Machine-CFG mutators 175 176 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock. 177 /// Don't do this unless you know what you're doing, because it doesn't 178 /// update pred's successors list. Use pred->addSuccessor instead. 179 /// 180 void addPredecessor (MachineBasicBlock *pred) { 181 assert(std::find (Predecessors.begin (), Predecessors.end (), pred) 182 == Predecessors.end () 183 && "Trying to addPredecessor a MBB which is already my predecessor"); 184 Predecessors.push_back (pred); 185 } 186 187 /// removePredecessor - Remove pred as a predecessor of this 188 /// MachineBasicBlock. Don't do this unless you know what you're 189 /// doing, because it doesn't update pred's successors list. Use 190 /// pred->removeSuccessor instead. 191 /// 192 void removePredecessor (MachineBasicBlock *pred) { 193 std::vector<MachineBasicBlock *>::iterator goner = 194 std::find (Predecessors.begin(), Predecessors.end (), pred); 195 assert (goner != Predecessors.end () 196 && "Trying to removePredecessor a MBB which isn't my predecessor"); 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