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/DataTypes.h" 20 #include <functional> 21 22 namespace llvm { 23 24 class Pass; 25 class BasicBlock; 26 class MachineFunction; 27 class MCSymbol; 28 class SlotIndexes; 29 class StringRef; 30 class raw_ostream; 31 class MachineBranchProbabilityInfo; 32 33 template <> 34 struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> { 35 private: 36 mutable ilist_half_node<MachineInstr> Sentinel; 37 38 // this is only set by the MachineBasicBlock owning the LiveList 39 friend class MachineBasicBlock; 40 MachineBasicBlock* Parent; 41 42 public: 43 MachineInstr *createSentinel() const { 44 return static_cast<MachineInstr*>(&Sentinel); 45 } 46 void destroySentinel(MachineInstr *) const {} 47 48 MachineInstr *provideInitialHead() const { return createSentinel(); } 49 MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); } 50 static void noteHead(MachineInstr*, MachineInstr*) {} 51 52 void addNodeToList(MachineInstr* N); 53 void removeNodeFromList(MachineInstr* N); 54 void transferNodesFromList(ilist_traits &SrcTraits, 55 ilist_iterator<MachineInstr> first, 56 ilist_iterator<MachineInstr> last); 57 void deleteNode(MachineInstr *N); 58 private: 59 void createNode(const MachineInstr &); 60 }; 61 62 class MachineBasicBlock : public ilist_node<MachineBasicBlock> { 63 typedef ilist<MachineInstr> Instructions; 64 Instructions Insts; 65 const BasicBlock *BB; 66 int Number; 67 MachineFunction *xParent; 68 69 /// Predecessors/Successors - Keep track of the predecessor / successor 70 /// basicblocks. 71 std::vector<MachineBasicBlock *> Predecessors; 72 std::vector<MachineBasicBlock *> Successors; 73 74 75 /// Weights - Keep track of the weights to the successors. This vector 76 /// has the same order as Successors, or it is empty if we don't use it 77 /// (disable optimization). 78 std::vector<uint32_t> Weights; 79 typedef std::vector<uint32_t>::iterator weight_iterator; 80 typedef std::vector<uint32_t>::const_iterator const_weight_iterator; 81 82 /// LiveIns - Keep track of the physical registers that are livein of 83 /// the basicblock. 84 std::vector<unsigned> LiveIns; 85 86 /// Alignment - Alignment of the basic block. Zero if the basic block does 87 /// not need to be aligned. 88 /// The alignment is specified as log2(bytes). 89 unsigned Alignment; 90 91 /// IsLandingPad - Indicate that this basic block is entered via an 92 /// exception handler. 93 bool IsLandingPad; 94 95 /// AddressTaken - Indicate that this basic block is potentially the 96 /// target of an indirect branch. 97 bool AddressTaken; 98 99 // Intrusive list support 100 MachineBasicBlock() {} 101 102 explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb); 103 104 ~MachineBasicBlock(); 105 106 // MachineBasicBlocks are allocated and owned by MachineFunction. 107 friend class MachineFunction; 108 109 public: 110 /// getBasicBlock - Return the LLVM basic block that this instance 111 /// corresponded to originally. Note that this may be NULL if this instance 112 /// does not correspond directly to an LLVM basic block. 113 /// 114 const BasicBlock *getBasicBlock() const { return BB; } 115 116 /// getName - Return the name of the corresponding LLVM basic block, or 117 /// "(null)". 118 StringRef getName() const; 119 120 /// getFullName - Return a formatted string to identify this block and its 121 /// parent function. 122 std::string getFullName() const; 123 124 /// hasAddressTaken - Test whether this block is potentially the target 125 /// of an indirect branch. 126 bool hasAddressTaken() const { return AddressTaken; } 127 128 /// setHasAddressTaken - Set this block to reflect that it potentially 129 /// is the target of an indirect branch. 130 void setHasAddressTaken() { AddressTaken = true; } 131 132 /// getParent - Return the MachineFunction containing this basic block. 133 /// 134 const MachineFunction *getParent() const { return xParent; } 135 MachineFunction *getParent() { return xParent; } 136 137 138 /// bundle_iterator - MachineBasicBlock iterator that automatically skips over 139 /// MIs that are inside bundles (i.e. walk top level MIs only). 140 template<typename Ty, typename IterTy> 141 class bundle_iterator 142 : public std::iterator<std::bidirectional_iterator_tag, Ty, ptrdiff_t> { 143 IterTy MII; 144 145 public: 146 bundle_iterator(IterTy mii) : MII(mii) { 147 assert(!MII->isInsideBundle() && 148 "It's not legal to initialize bundle_iterator with a bundled MI"); 149 } 150 151 bundle_iterator(Ty &mi) : MII(mi) { 152 assert(!mi.isInsideBundle() && 153 "It's not legal to initialize bundle_iterator with a bundled MI"); 154 } 155 bundle_iterator(Ty *mi) : MII(mi) { 156 assert((!mi || !mi->isInsideBundle()) && 157 "It's not legal to initialize bundle_iterator with a bundled MI"); 158 } 159 bundle_iterator(const bundle_iterator &I) : MII(I.MII) {} 160 bundle_iterator() : MII(0) {} 161 162 Ty &operator*() const { return *MII; } 163 Ty *operator->() const { return &operator*(); } 164 165 operator Ty*() const { return MII; } 166 167 bool operator==(const bundle_iterator &x) const { 168 return MII == x.MII; 169 } 170 bool operator!=(const bundle_iterator &x) const { 171 return !operator==(x); 172 } 173 174 // Increment and decrement operators... 175 bundle_iterator &operator--() { // predecrement - Back up 176 do { 177 --MII; 178 } while (MII->isInsideBundle()); 179 return *this; 180 } 181 bundle_iterator &operator++() { // preincrement - Advance 182 do { 183 ++MII; 184 } while (MII->isInsideBundle()); 185 return *this; 186 } 187 bundle_iterator operator--(int) { // postdecrement operators... 188 bundle_iterator tmp = *this; 189 do { 190 --MII; 191 } while (MII->isInsideBundle()); 192 return tmp; 193 } 194 bundle_iterator operator++(int) { // postincrement operators... 195 bundle_iterator tmp = *this; 196 do { 197 ++MII; 198 } while (MII->isInsideBundle()); 199 return tmp; 200 } 201 202 IterTy getInstrIterator() const { 203 return MII; 204 } 205 }; 206 207 typedef Instructions::iterator instr_iterator; 208 typedef Instructions::const_iterator const_instr_iterator; 209 typedef std::reverse_iterator<instr_iterator> reverse_instr_iterator; 210 typedef 211 std::reverse_iterator<const_instr_iterator> const_reverse_instr_iterator; 212 213 typedef 214 bundle_iterator<MachineInstr,instr_iterator> iterator; 215 typedef 216 bundle_iterator<const MachineInstr,const_instr_iterator> const_iterator; 217 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 218 typedef std::reverse_iterator<iterator> reverse_iterator; 219 220 221 unsigned size() const { return (unsigned)Insts.size(); } 222 bool empty() const { return Insts.empty(); } 223 224 MachineInstr& front() { return Insts.front(); } 225 MachineInstr& back() { return Insts.back(); } 226 const MachineInstr& front() const { return Insts.front(); } 227 const MachineInstr& back() const { return Insts.back(); } 228 229 instr_iterator instr_begin() { return Insts.begin(); } 230 const_instr_iterator instr_begin() const { return Insts.begin(); } 231 instr_iterator instr_end() { return Insts.end(); } 232 const_instr_iterator instr_end() const { return Insts.end(); } 233 reverse_instr_iterator instr_rbegin() { return Insts.rbegin(); } 234 const_reverse_instr_iterator instr_rbegin() const { return Insts.rbegin(); } 235 reverse_instr_iterator instr_rend () { return Insts.rend(); } 236 const_reverse_instr_iterator instr_rend () const { return Insts.rend(); } 237 238 iterator begin() { return Insts.begin(); } 239 const_iterator begin() const { return Insts.begin(); } 240 iterator end() { 241 instr_iterator II = instr_end(); 242 if (II != instr_begin()) { 243 while (II->isInsideBundle()) 244 --II; 245 } 246 return II; 247 } 248 const_iterator end() const { 249 const_instr_iterator II = instr_end(); 250 if (II != instr_begin()) { 251 while (II->isInsideBundle()) 252 --II; 253 } 254 return II; 255 } 256 reverse_iterator rbegin() { 257 reverse_instr_iterator II = instr_rbegin(); 258 if (II != instr_rend()) { 259 while (II->isInsideBundle()) 260 ++II; 261 } 262 return II; 263 } 264 const_reverse_iterator rbegin() const { 265 const_reverse_instr_iterator II = instr_rbegin(); 266 if (II != instr_rend()) { 267 while (II->isInsideBundle()) 268 ++II; 269 } 270 return II; 271 } 272 reverse_iterator rend () { return Insts.rend(); } 273 const_reverse_iterator rend () const { return Insts.rend(); } 274 275 276 // Machine-CFG iterators 277 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator; 278 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator; 279 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator; 280 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator; 281 typedef std::vector<MachineBasicBlock *>::reverse_iterator 282 pred_reverse_iterator; 283 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator 284 const_pred_reverse_iterator; 285 typedef std::vector<MachineBasicBlock *>::reverse_iterator 286 succ_reverse_iterator; 287 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator 288 const_succ_reverse_iterator; 289 290 pred_iterator pred_begin() { return Predecessors.begin(); } 291 const_pred_iterator pred_begin() const { return Predecessors.begin(); } 292 pred_iterator pred_end() { return Predecessors.end(); } 293 const_pred_iterator pred_end() const { return Predecessors.end(); } 294 pred_reverse_iterator pred_rbegin() 295 { return Predecessors.rbegin();} 296 const_pred_reverse_iterator pred_rbegin() const 297 { return Predecessors.rbegin();} 298 pred_reverse_iterator pred_rend() 299 { return Predecessors.rend(); } 300 const_pred_reverse_iterator pred_rend() const 301 { return Predecessors.rend(); } 302 unsigned pred_size() const { 303 return (unsigned)Predecessors.size(); 304 } 305 bool pred_empty() const { return Predecessors.empty(); } 306 succ_iterator succ_begin() { return Successors.begin(); } 307 const_succ_iterator succ_begin() const { return Successors.begin(); } 308 succ_iterator succ_end() { return Successors.end(); } 309 const_succ_iterator succ_end() const { return Successors.end(); } 310 succ_reverse_iterator succ_rbegin() 311 { return Successors.rbegin(); } 312 const_succ_reverse_iterator succ_rbegin() const 313 { return Successors.rbegin(); } 314 succ_reverse_iterator succ_rend() 315 { return Successors.rend(); } 316 const_succ_reverse_iterator succ_rend() const 317 { return Successors.rend(); } 318 unsigned succ_size() const { 319 return (unsigned)Successors.size(); 320 } 321 bool succ_empty() const { return Successors.empty(); } 322 323 // LiveIn management methods. 324 325 /// addLiveIn - Add the specified register as a live in. Note that it 326 /// is an error to add the same register to the same set more than once. 327 void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); } 328 329 /// removeLiveIn - Remove the specified register from the live in set. 330 /// 331 void removeLiveIn(unsigned Reg); 332 333 /// isLiveIn - Return true if the specified register is in the live in set. 334 /// 335 bool isLiveIn(unsigned Reg) const; 336 337 // Iteration support for live in sets. These sets are kept in sorted 338 // order by their register number. 339 typedef std::vector<unsigned>::const_iterator livein_iterator; 340 livein_iterator livein_begin() const { return LiveIns.begin(); } 341 livein_iterator livein_end() const { return LiveIns.end(); } 342 bool livein_empty() const { return LiveIns.empty(); } 343 344 /// getAlignment - Return alignment of the basic block. 345 /// The alignment is specified as log2(bytes). 346 /// 347 unsigned getAlignment() const { return Alignment; } 348 349 /// setAlignment - Set alignment of the basic block. 350 /// The alignment is specified as log2(bytes). 351 /// 352 void setAlignment(unsigned Align) { Alignment = Align; } 353 354 /// isLandingPad - Returns true if the block is a landing pad. That is 355 /// this basic block is entered via an exception handler. 356 bool isLandingPad() const { return IsLandingPad; } 357 358 /// setIsLandingPad - Indicates the block is a landing pad. That is 359 /// this basic block is entered via an exception handler. 360 void setIsLandingPad(bool V = true) { IsLandingPad = V; } 361 362 /// getLandingPadSuccessor - If this block has a successor that is a landing 363 /// pad, return it. Otherwise return NULL. 364 const MachineBasicBlock *getLandingPadSuccessor() const; 365 366 // Code Layout methods. 367 368 /// moveBefore/moveAfter - move 'this' block before or after the specified 369 /// block. This only moves the block, it does not modify the CFG or adjust 370 /// potential fall-throughs at the end of the block. 371 void moveBefore(MachineBasicBlock *NewAfter); 372 void moveAfter(MachineBasicBlock *NewBefore); 373 374 /// updateTerminator - Update the terminator instructions in block to account 375 /// for changes to the layout. If the block previously used a fallthrough, 376 /// it may now need a branch, and if it previously used branching it may now 377 /// be able to use a fallthrough. 378 void updateTerminator(); 379 380 // Machine-CFG mutators 381 382 /// addSuccessor - Add succ as a successor of this MachineBasicBlock. 383 /// The Predecessors list of succ is automatically updated. WEIGHT 384 /// parameter is stored in Weights list and it may be used by 385 /// MachineBranchProbabilityInfo analysis to calculate branch probability. 386 /// 387 void addSuccessor(MachineBasicBlock *succ, uint32_t weight = 0); 388 389 /// removeSuccessor - Remove successor from the successors list of this 390 /// MachineBasicBlock. The Predecessors list of succ is automatically updated. 391 /// 392 void removeSuccessor(MachineBasicBlock *succ); 393 394 /// removeSuccessor - Remove specified successor from the successors list of 395 /// this MachineBasicBlock. The Predecessors list of succ is automatically 396 /// updated. Return the iterator to the element after the one removed. 397 /// 398 succ_iterator removeSuccessor(succ_iterator I); 399 400 /// replaceSuccessor - Replace successor OLD with NEW and update weight info. 401 /// 402 void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New); 403 404 405 /// transferSuccessors - Transfers all the successors from MBB to this 406 /// machine basic block (i.e., copies all the successors fromMBB and 407 /// remove all the successors from fromMBB). 408 void transferSuccessors(MachineBasicBlock *fromMBB); 409 410 /// transferSuccessorsAndUpdatePHIs - Transfers all the successors, as 411 /// in transferSuccessors, and update PHI operands in the successor blocks 412 /// which refer to fromMBB to refer to this. 413 void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB); 414 415 /// isSuccessor - Return true if the specified MBB is a successor of this 416 /// block. 417 bool isSuccessor(const MachineBasicBlock *MBB) const; 418 419 /// isLayoutSuccessor - Return true if the specified MBB will be emitted 420 /// immediately after this block, such that if this block exits by 421 /// falling through, control will transfer to the specified MBB. Note 422 /// that MBB need not be a successor at all, for example if this block 423 /// ends with an unconditional branch to some other block. 424 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const; 425 426 /// canFallThrough - Return true if the block can implicitly transfer 427 /// control to the block after it by falling off the end of it. This should 428 /// return false if it can reach the block after it, but it uses an explicit 429 /// branch to do so (e.g., a table jump). True is a conservative answer. 430 bool canFallThrough(); 431 432 /// Returns a pointer to the first instructon in this block that is not a 433 /// PHINode instruction. When adding instruction to the beginning of the 434 /// basic block, they should be added before the returned value, not before 435 /// the first instruction, which might be PHI. 436 /// Returns end() is there's no non-PHI instruction. 437 iterator getFirstNonPHI(); 438 439 /// SkipPHIsAndLabels - Return the first instruction in MBB after I that is 440 /// not a PHI or a label. This is the correct point to insert copies at the 441 /// beginning of a basic block. 442 iterator SkipPHIsAndLabels(iterator I); 443 444 /// getFirstTerminator - returns an iterator to the first terminator 445 /// instruction of this basic block. If a terminator does not exist, 446 /// it returns end() 447 iterator getFirstTerminator(); 448 const_iterator getFirstTerminator() const; 449 450 /// getFirstInstrTerminator - Same getFirstTerminator but it ignores bundles 451 /// and return an instr_iterator instead. 452 instr_iterator getFirstInstrTerminator(); 453 454 /// getLastNonDebugInstr - returns an iterator to the last non-debug 455 /// instruction in the basic block, or end() 456 iterator getLastNonDebugInstr(); 457 const_iterator getLastNonDebugInstr() const; 458 459 /// SplitCriticalEdge - Split the critical edge from this block to the 460 /// given successor block, and return the newly created block, or null 461 /// if splitting is not possible. 462 /// 463 /// This function updates LiveVariables, MachineDominatorTree, and 464 /// MachineLoopInfo, as applicable. 465 MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P); 466 467 void pop_front() { Insts.pop_front(); } 468 void pop_back() { Insts.pop_back(); } 469 void push_back(MachineInstr *MI) { Insts.push_back(MI); } 470 471 template<typename IT> 472 void insert(instr_iterator I, IT S, IT E) { 473 Insts.insert(I, S, E); 474 } 475 instr_iterator insert(instr_iterator I, MachineInstr *M) { 476 return Insts.insert(I, M); 477 } 478 instr_iterator insertAfter(instr_iterator I, MachineInstr *M) { 479 return Insts.insertAfter(I, M); 480 } 481 482 template<typename IT> 483 void insert(iterator I, IT S, IT E) { 484 Insts.insert(I.getInstrIterator(), S, E); 485 } 486 iterator insert(iterator I, MachineInstr *M) { 487 return Insts.insert(I.getInstrIterator(), M); 488 } 489 iterator insertAfter(iterator I, MachineInstr *M) { 490 return Insts.insertAfter(I.getInstrIterator(), M); 491 } 492 493 /// erase - Remove the specified element or range from the instruction list. 494 /// These functions delete any instructions removed. 495 /// 496 instr_iterator erase(instr_iterator I) { 497 return Insts.erase(I); 498 } 499 instr_iterator erase(instr_iterator I, instr_iterator E) { 500 return Insts.erase(I, E); 501 } 502 instr_iterator erase_instr(MachineInstr *I) { 503 instr_iterator MII(I); 504 return erase(MII); 505 } 506 507 iterator erase(iterator I); 508 iterator erase(iterator I, iterator E) { 509 return Insts.erase(I.getInstrIterator(), E.getInstrIterator()); 510 } 511 iterator erase(MachineInstr *I) { 512 iterator MII(I); 513 return erase(MII); 514 } 515 516 /// remove - Remove the instruction from the instruction list. This function 517 /// does not delete the instruction. WARNING: Note, if the specified 518 /// instruction is a bundle this function will remove all the bundled 519 /// instructions as well. It is up to the caller to keep a list of the 520 /// bundled instructions and re-insert them if desired. This function is 521 /// *not recommended* for manipulating instructions with bundles. Use 522 /// splice instead. 523 MachineInstr *remove(MachineInstr *I); 524 void clear() { 525 Insts.clear(); 526 } 527 528 /// splice - Take an instruction from MBB 'Other' at the position From, 529 /// and insert it into this MBB right before 'where'. 530 void splice(instr_iterator where, MachineBasicBlock *Other, 531 instr_iterator From) { 532 Insts.splice(where, Other->Insts, From); 533 } 534 void splice(iterator where, MachineBasicBlock *Other, iterator From); 535 536 /// splice - Take a block of instructions from MBB 'Other' in the range [From, 537 /// To), and insert them into this MBB right before 'where'. 538 void splice(instr_iterator where, MachineBasicBlock *Other, instr_iterator From, 539 instr_iterator To) { 540 Insts.splice(where, Other->Insts, From, To); 541 } 542 void splice(iterator where, MachineBasicBlock *Other, iterator From, 543 iterator To) { 544 Insts.splice(where.getInstrIterator(), Other->Insts, 545 From.getInstrIterator(), To.getInstrIterator()); 546 } 547 548 /// removeFromParent - This method unlinks 'this' from the containing 549 /// function, and returns it, but does not delete it. 550 MachineBasicBlock *removeFromParent(); 551 552 /// eraseFromParent - This method unlinks 'this' from the containing 553 /// function and deletes it. 554 void eraseFromParent(); 555 556 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to 557 /// 'Old', change the code and CFG so that it branches to 'New' instead. 558 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New); 559 560 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in 561 /// the CFG to be inserted. If we have proven that MBB can only branch to 562 /// DestA and DestB, remove any other MBB successors from the CFG. DestA and 563 /// DestB can be null. Besides DestA and DestB, retain other edges leading 564 /// to LandingPads (currently there can be only one; we don't check or require 565 /// that here). Note it is possible that DestA and/or DestB are LandingPads. 566 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA, 567 MachineBasicBlock *DestB, 568 bool isCond); 569 570 /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping 571 /// any DBG_VALUE instructions. Return UnknownLoc if there is none. 572 DebugLoc findDebugLoc(instr_iterator MBBI); 573 DebugLoc findDebugLoc(iterator MBBI) { 574 return findDebugLoc(MBBI.getInstrIterator()); 575 } 576 577 // Debugging methods. 578 void dump() const; 579 void print(raw_ostream &OS, SlotIndexes* = 0) const; 580 581 /// getNumber - MachineBasicBlocks are uniquely numbered at the function 582 /// level, unless they're not in a MachineFunction yet, in which case this 583 /// will return -1. 584 /// 585 int getNumber() const { return Number; } 586 void setNumber(int N) { Number = N; } 587 588 /// getSymbol - Return the MCSymbol for this basic block. 589 /// 590 MCSymbol *getSymbol() const; 591 592 593 private: 594 /// getWeightIterator - Return weight iterator corresponding to the I 595 /// successor iterator. 596 weight_iterator getWeightIterator(succ_iterator I); 597 const_weight_iterator getWeightIterator(const_succ_iterator I) const; 598 599 friend class MachineBranchProbabilityInfo; 600 601 /// getSuccWeight - Return weight of the edge from this block to MBB. This 602 /// method should NOT be called directly, but by using getEdgeWeight method 603 /// from MachineBranchProbabilityInfo class. 604 uint32_t getSuccWeight(const MachineBasicBlock *succ) const; 605 606 607 // Methods used to maintain doubly linked list of blocks... 608 friend struct ilist_traits<MachineBasicBlock>; 609 610 // Machine-CFG mutators 611 612 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock. 613 /// Don't do this unless you know what you're doing, because it doesn't 614 /// update pred's successors list. Use pred->addSuccessor instead. 615 /// 616 void addPredecessor(MachineBasicBlock *pred); 617 618 /// removePredecessor - Remove pred as a predecessor of this 619 /// MachineBasicBlock. Don't do this unless you know what you're 620 /// doing, because it doesn't update pred's successors list. Use 621 /// pred->removeSuccessor instead. 622 /// 623 void removePredecessor(MachineBasicBlock *pred); 624 }; 625 626 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB); 627 628 void WriteAsOperand(raw_ostream &, const MachineBasicBlock*, bool t); 629 630 // This is useful when building IndexedMaps keyed on basic block pointers. 631 struct MBB2NumberFunctor : 632 public std::unary_function<const MachineBasicBlock*, unsigned> { 633 unsigned operator()(const MachineBasicBlock *MBB) const { 634 return MBB->getNumber(); 635 } 636 }; 637 638 //===--------------------------------------------------------------------===// 639 // GraphTraits specializations for machine basic block graphs (machine-CFGs) 640 //===--------------------------------------------------------------------===// 641 642 // Provide specializations of GraphTraits to be able to treat a 643 // MachineFunction as a graph of MachineBasicBlocks... 644 // 645 646 template <> struct GraphTraits<MachineBasicBlock *> { 647 typedef MachineBasicBlock NodeType; 648 typedef MachineBasicBlock::succ_iterator ChildIteratorType; 649 650 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; } 651 static inline ChildIteratorType child_begin(NodeType *N) { 652 return N->succ_begin(); 653 } 654 static inline ChildIteratorType child_end(NodeType *N) { 655 return N->succ_end(); 656 } 657 }; 658 659 template <> struct GraphTraits<const MachineBasicBlock *> { 660 typedef const MachineBasicBlock NodeType; 661 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType; 662 663 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; } 664 static inline ChildIteratorType child_begin(NodeType *N) { 665 return N->succ_begin(); 666 } 667 static inline ChildIteratorType child_end(NodeType *N) { 668 return N->succ_end(); 669 } 670 }; 671 672 // Provide specializations of GraphTraits to be able to treat a 673 // MachineFunction as a graph of MachineBasicBlocks... and to walk it 674 // in inverse order. Inverse order for a function is considered 675 // to be when traversing the predecessor edges of a MBB 676 // instead of the successor edges. 677 // 678 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > { 679 typedef MachineBasicBlock NodeType; 680 typedef MachineBasicBlock::pred_iterator ChildIteratorType; 681 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) { 682 return G.Graph; 683 } 684 static inline ChildIteratorType child_begin(NodeType *N) { 685 return N->pred_begin(); 686 } 687 static inline ChildIteratorType child_end(NodeType *N) { 688 return N->pred_end(); 689 } 690 }; 691 692 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > { 693 typedef const MachineBasicBlock NodeType; 694 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType; 695 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) { 696 return G.Graph; 697 } 698 static inline ChildIteratorType child_begin(NodeType *N) { 699 return N->pred_begin(); 700 } 701 static inline ChildIteratorType child_end(NodeType *N) { 702 return N->pred_end(); 703 } 704 }; 705 706 } // End llvm namespace 707 708 #endif 709