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