MachineBasicBlock.h revision 2c3f7ae3843bdc9dcfe85393e178211976c1f9bd
1//===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// 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 "llvm/ADT/GraphTraits.h" 19#include "llvm/Support/Streams.h" 20 21namespace llvm { 22 class MachineFunction; 23 24template <> 25struct alist_traits<MachineInstr, MachineInstr> { 26protected: 27 // this is only set by the MachineBasicBlock owning the LiveList 28 friend class MachineBasicBlock; 29 MachineBasicBlock* Parent; 30 31 typedef alist_iterator<MachineInstr> iterator; 32 33public: 34 alist_traits<MachineInstr, MachineInstr>() : Parent(0) { } 35 36 void addNodeToList(MachineInstr* N); 37 void removeNodeFromList(MachineInstr* N); 38 void transferNodesFromList(alist_traits &, iterator, iterator); 39 void deleteNode(MachineInstr *N); 40}; 41 42class BasicBlock; 43 44class MachineBasicBlock { 45 typedef alist<MachineInstr> Instructions; 46 Instructions Insts; 47 const BasicBlock *BB; 48 int Number; 49 MachineFunction *xParent; 50 51 /// Predecessors/Successors - Keep track of the predecessor / successor 52 /// basicblocks. 53 std::vector<MachineBasicBlock *> Predecessors; 54 std::vector<MachineBasicBlock *> Successors; 55 56 /// LiveIns - Keep track of the physical registers that are livein of 57 /// the basicblock. 58 std::vector<unsigned> LiveIns; 59 60 /// Alignment - Alignment of the basic block. Zero if the basic block does 61 /// not need to be aligned. 62 unsigned Alignment; 63 64 /// IsLandingPad - Indicate that this basic block is entered via an 65 /// exception handler. 66 bool IsLandingPad; 67 68 explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb); 69 70 ~MachineBasicBlock(); 71 72 // MachineBasicBlocks are allocated and owned by MachineFunction. 73 friend class MachineFunction; 74 75public: 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 xParent; } 84 MachineFunction *getParent() { return xParent; } 85 86 typedef Instructions::iterator iterator; 87 typedef Instructions::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 (unsigned)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 typedef std::vector<MachineBasicBlock *>::reverse_iterator 112 pred_reverse_iterator; 113 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator 114 const_pred_reverse_iterator; 115 typedef std::vector<MachineBasicBlock *>::reverse_iterator 116 succ_reverse_iterator; 117 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator 118 const_succ_reverse_iterator; 119 120 pred_iterator pred_begin() { return Predecessors.begin(); } 121 const_pred_iterator pred_begin() const { return Predecessors.begin(); } 122 pred_iterator pred_end() { return Predecessors.end(); } 123 const_pred_iterator pred_end() const { return Predecessors.end(); } 124 pred_reverse_iterator pred_rbegin() 125 { return Predecessors.rbegin();} 126 const_pred_reverse_iterator pred_rbegin() const 127 { return Predecessors.rbegin();} 128 pred_reverse_iterator pred_rend() 129 { return Predecessors.rend(); } 130 const_pred_reverse_iterator pred_rend() const 131 { return Predecessors.rend(); } 132 unsigned pred_size() const { 133 return (unsigned)Predecessors.size(); 134 } 135 bool pred_empty() const { return Predecessors.empty(); } 136 succ_iterator succ_begin() { return Successors.begin(); } 137 const_succ_iterator succ_begin() const { return Successors.begin(); } 138 succ_iterator succ_end() { return Successors.end(); } 139 const_succ_iterator succ_end() const { return Successors.end(); } 140 succ_reverse_iterator succ_rbegin() 141 { return Successors.rbegin(); } 142 const_succ_reverse_iterator succ_rbegin() const 143 { return Successors.rbegin(); } 144 succ_reverse_iterator succ_rend() 145 { return Successors.rend(); } 146 const_succ_reverse_iterator succ_rend() const 147 { return Successors.rend(); } 148 unsigned succ_size() const { 149 return (unsigned)Successors.size(); 150 } 151 bool succ_empty() const { return Successors.empty(); } 152 153 // LiveIn management methods. 154 155 /// addLiveIn - Add the specified register as a live in. Note that it 156 /// is an error to add the same register to the same set more than once. 157 void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); } 158 159 /// removeLiveIn - Remove the specified register from the live in set. 160 /// 161 void removeLiveIn(unsigned Reg); 162 163 /// isLiveIn - Return true if the specified register is in the live in set. 164 /// 165 bool isLiveIn(unsigned Reg) const; 166 167 // Iteration support for live in sets. These sets are kept in sorted 168 // order by their register number. 169 typedef std::vector<unsigned>::iterator livein_iterator; 170 typedef std::vector<unsigned>::const_iterator const_livein_iterator; 171 livein_iterator livein_begin() { return LiveIns.begin(); } 172 const_livein_iterator livein_begin() const { return LiveIns.begin(); } 173 livein_iterator livein_end() { return LiveIns.end(); } 174 const_livein_iterator livein_end() const { return LiveIns.end(); } 175 bool livein_empty() const { return LiveIns.empty(); } 176 177 /// getAlignment - Return alignment of the basic block. 178 /// 179 unsigned getAlignment() const { return Alignment; } 180 181 /// setAlignment - Set alignment of the basic block. 182 /// 183 void setAlignment(unsigned Align) { Alignment = Align; } 184 185 /// isLandingPad - Returns true if the block is a landing pad. That is 186 /// this basic block is entered via an exception handler. 187 bool isLandingPad() const { return IsLandingPad; } 188 189 /// setIsLandingPad - Indicates the block is a landing pad. That is 190 /// this basic block is entered via an exception handler. 191 void setIsLandingPad() { IsLandingPad = true; } 192 193 // Code Layout methods. 194 195 /// moveBefore/moveAfter - move 'this' block before or after the specified 196 /// block. This only moves the block, it does not modify the CFG or adjust 197 /// potential fall-throughs at the end of the block. 198 void moveBefore(MachineBasicBlock *NewAfter); 199 void moveAfter(MachineBasicBlock *NewBefore); 200 201 // Machine-CFG mutators 202 203 /// addSuccessor - Add succ as a successor of this MachineBasicBlock. 204 /// The Predecessors list of succ is automatically updated. 205 /// 206 void addSuccessor(MachineBasicBlock *succ); 207 208 /// removeSuccessor - Remove successor from the successors list of this 209 /// MachineBasicBlock. The Predecessors list of succ is automatically updated. 210 /// 211 void removeSuccessor(MachineBasicBlock *succ); 212 213 /// removeSuccessor - Remove specified successor from the successors list of 214 /// this MachineBasicBlock. The Predecessors list of succ is automatically 215 /// updated. Return the iterator to the element after the one removed. 216 /// 217 succ_iterator removeSuccessor(succ_iterator I); 218 219 /// transferSuccessors - Transfers all the successors from MBB to this 220 /// machine basic block (i.e., copies all the successors fromMBB and 221 /// remove all the successors fromBB). 222 void transferSuccessors(MachineBasicBlock *fromMBB); 223 224 /// isSuccessor - Return true if the specified MBB is a successor of this 225 /// block. 226 bool isSuccessor(MachineBasicBlock *MBB) const; 227 228 /// getFirstTerminator - returns an iterator to the first terminator 229 /// instruction of this basic block. If a terminator does not exist, 230 /// it returns end() 231 iterator getFirstTerminator(); 232 233 void pop_front() { Insts.pop_front(); } 234 void pop_back() { Insts.pop_back(); } 235 void push_back(MachineInstr *MI) { Insts.push_back(MI); } 236 template<typename IT> 237 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); } 238 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); } 239 240 // erase - Remove the specified element or range from the instruction list. 241 // These functions delete any instructions removed. 242 // 243 iterator erase(iterator I) { return Insts.erase(I); } 244 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); } 245 MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); } 246 void clear() { Insts.clear(); } 247 248 /// splice - Take a block of instructions from MBB 'Other' in the range [From, 249 /// To), and insert them into this MBB right before 'where'. 250 void splice(iterator where, MachineBasicBlock *Other, iterator From, 251 iterator To) { 252 Insts.splice(where, Other->Insts, From, To); 253 } 254 255 /// removeFromParent - This method unlinks 'this' from the containing 256 /// function, and returns it, but does not delete it. 257 MachineBasicBlock *removeFromParent(); 258 259 /// eraseFromParent - This method unlinks 'this' from the containing 260 /// function and deletes it. 261 void eraseFromParent(); 262 263 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to 264 /// 'Old', change the code and CFG so that it branches to 'New' instead. 265 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New); 266 267 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in 268 /// the CFG to be inserted. If we have proven that MBB can only branch to 269 /// DestA and DestB, remove any other MBB successors from the CFG. DestA and 270 /// DestB can be null. Besides DestA and DestB, retain other edges leading 271 /// to LandingPads (currently there can be only one; we don't check or require 272 /// that here). Note it is possible that DestA and/or DestB are LandingPads. 273 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA, 274 MachineBasicBlock *DestB, 275 bool isCond); 276 277 // Debugging methods. 278 void dump() const; 279 void print(std::ostream &OS) const; 280 void print(std::ostream *OS) const { if (OS) print(*OS); } 281 282 /// getNumber - MachineBasicBlocks are uniquely numbered at the function 283 /// level, unless they're not in a MachineFunction yet, in which case this 284 /// will return -1. 285 /// 286 int getNumber() const { return Number; } 287 void setNumber(int N) { Number = N; } 288 289private: // Methods used to maintain doubly linked list of blocks... 290 friend struct alist_traits<MachineBasicBlock>; 291 292 // Machine-CFG mutators 293 294 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock. 295 /// Don't do this unless you know what you're doing, because it doesn't 296 /// update pred's successors list. Use pred->addSuccessor instead. 297 /// 298 void addPredecessor(MachineBasicBlock *pred); 299 300 /// removePredecessor - Remove pred as a predecessor of this 301 /// MachineBasicBlock. Don't do this unless you know what you're 302 /// doing, because it doesn't update pred's successors list. Use 303 /// pred->removeSuccessor instead. 304 /// 305 void removePredecessor(MachineBasicBlock *pred); 306}; 307 308std::ostream& operator<<(std::ostream &OS, const MachineBasicBlock &MBB); 309 310//===--------------------------------------------------------------------===// 311// GraphTraits specializations for machine basic block graphs (machine-CFGs) 312//===--------------------------------------------------------------------===// 313 314// Provide specializations of GraphTraits to be able to treat a 315// MachineFunction as a graph of MachineBasicBlocks... 316// 317 318template <> struct GraphTraits<MachineBasicBlock *> { 319 typedef MachineBasicBlock NodeType; 320 typedef MachineBasicBlock::succ_iterator ChildIteratorType; 321 322 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; } 323 static inline ChildIteratorType child_begin(NodeType *N) { 324 return N->succ_begin(); 325 } 326 static inline ChildIteratorType child_end(NodeType *N) { 327 return N->succ_end(); 328 } 329}; 330 331template <> struct GraphTraits<const MachineBasicBlock *> { 332 typedef const MachineBasicBlock NodeType; 333 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType; 334 335 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; } 336 static inline ChildIteratorType child_begin(NodeType *N) { 337 return N->succ_begin(); 338 } 339 static inline ChildIteratorType child_end(NodeType *N) { 340 return N->succ_end(); 341 } 342}; 343 344// Provide specializations of GraphTraits to be able to treat a 345// MachineFunction as a graph of MachineBasicBlocks... and to walk it 346// in inverse order. Inverse order for a function is considered 347// to be when traversing the predecessor edges of a MBB 348// instead of the successor edges. 349// 350template <> struct GraphTraits<Inverse<MachineBasicBlock*> > { 351 typedef MachineBasicBlock NodeType; 352 typedef MachineBasicBlock::pred_iterator ChildIteratorType; 353 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) { 354 return G.Graph; 355 } 356 static inline ChildIteratorType child_begin(NodeType *N) { 357 return N->pred_begin(); 358 } 359 static inline ChildIteratorType child_end(NodeType *N) { 360 return N->pred_end(); 361 } 362}; 363 364template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > { 365 typedef const MachineBasicBlock NodeType; 366 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType; 367 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) { 368 return G.Graph; 369 } 370 static inline ChildIteratorType child_begin(NodeType *N) { 371 return N->pred_begin(); 372 } 373 static inline ChildIteratorType child_end(NodeType *N) { 374 return N->pred_end(); 375 } 376}; 377 378} // End llvm namespace 379 380#endif 381