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