1 //===- llvm/CodeGen/MachineBasicBlock.h -------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Collect the sequence of machine instructions for a basic block. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H 14 #define LLVM_CODEGEN_MACHINEBASICBLOCK_H 15 16 #include "llvm/ADT/GraphTraits.h" 17 #include "llvm/ADT/ilist.h" 18 #include "llvm/ADT/iterator_range.h" 19 #include "llvm/ADT/SparseBitVector.h" 20 #include "llvm/CodeGen/MachineInstr.h" 21 #include "llvm/CodeGen/MachineInstrBundleIterator.h" 22 #include "llvm/IR/DebugLoc.h" 23 #include "llvm/MC/LaneBitmask.h" 24 #include "llvm/Support/BranchProbability.h" 25 #include <cassert> 26 #include <cstdint> 27 #include <functional> 28 #include <iterator> 29 #include <string> 30 #include <vector> 31 32 namespace llvm { 33 34 class BasicBlock; 35 class MachineFunction; 36 class MCSymbol; 37 class ModuleSlotTracker; 38 class Pass; 39 class Printable; 40 class SlotIndexes; 41 class StringRef; 42 class raw_ostream; 43 class LiveIntervals; 44 class TargetRegisterClass; 45 class TargetRegisterInfo; 46 47 // This structure uniquely identifies a basic block section. 48 // Possible values are 49 // {Type: Default, Number: (unsigned)} (These are regular section IDs) 50 // {Type: Exception, Number: 0} (ExceptionSectionID) 51 // {Type: Cold, Number: 0} (ColdSectionID) 52 struct MBBSectionID { 53 enum SectionType { 54 Default = 0, // Regular section (these sections are distinguished by the 55 // Number field). 56 Exception, // Special section type for exception handling blocks 57 Cold, // Special section type for cold blocks 58 } Type; 59 unsigned Number; 60 MBBSectionIDMBBSectionID61 MBBSectionID(unsigned N) : Type(Default), Number(N) {} 62 63 // Special unique sections for cold and exception blocks. 64 const static MBBSectionID ColdSectionID; 65 const static MBBSectionID ExceptionSectionID; 66 67 bool operator==(const MBBSectionID &Other) const { 68 return Type == Other.Type && Number == Other.Number; 69 } 70 71 bool operator!=(const MBBSectionID &Other) const { return !(*this == Other); } 72 73 private: 74 // This is only used to construct the special cold and exception sections. MBBSectionIDMBBSectionID75 MBBSectionID(SectionType T) : Type(T), Number(0) {} 76 }; 77 78 template <> struct ilist_traits<MachineInstr> { 79 private: 80 friend class MachineBasicBlock; // Set by the owning MachineBasicBlock. 81 82 MachineBasicBlock *Parent; 83 84 using instr_iterator = 85 simple_ilist<MachineInstr, ilist_sentinel_tracking<true>>::iterator; 86 87 public: 88 void addNodeToList(MachineInstr *N); 89 void removeNodeFromList(MachineInstr *N); 90 void transferNodesFromList(ilist_traits &FromList, instr_iterator First, 91 instr_iterator Last); 92 void deleteNode(MachineInstr *MI); 93 }; 94 95 class MachineBasicBlock 96 : public ilist_node_with_parent<MachineBasicBlock, MachineFunction> { 97 public: 98 /// Pair of physical register and lane mask. 99 /// This is not simply a std::pair typedef because the members should be named 100 /// clearly as they both have an integer type. 101 struct RegisterMaskPair { 102 public: 103 MCPhysReg PhysReg; 104 LaneBitmask LaneMask; 105 106 RegisterMaskPair(MCPhysReg PhysReg, LaneBitmask LaneMask) 107 : PhysReg(PhysReg), LaneMask(LaneMask) {} 108 }; 109 110 private: 111 using Instructions = ilist<MachineInstr, ilist_sentinel_tracking<true>>; 112 113 Instructions Insts; 114 const BasicBlock *BB; 115 int Number; 116 MachineFunction *xParent; 117 118 /// Keep track of the predecessor / successor basic blocks. 119 std::vector<MachineBasicBlock *> Predecessors; 120 std::vector<MachineBasicBlock *> Successors; 121 122 /// Keep track of the probabilities to the successors. This vector has the 123 /// same order as Successors, or it is empty if we don't use it (disable 124 /// optimization). 125 std::vector<BranchProbability> Probs; 126 using probability_iterator = std::vector<BranchProbability>::iterator; 127 using const_probability_iterator = 128 std::vector<BranchProbability>::const_iterator; 129 130 Optional<uint64_t> IrrLoopHeaderWeight; 131 132 /// Keep track of the physical registers that are livein of the basicblock. 133 using LiveInVector = std::vector<RegisterMaskPair>; 134 LiveInVector LiveIns; 135 136 /// Alignment of the basic block. One if the basic block does not need to be 137 /// aligned. 138 Align Alignment; 139 140 /// Indicate that this basic block is entered via an exception handler. 141 bool IsEHPad = false; 142 143 /// Indicate that this basic block is potentially the target of an indirect 144 /// branch. 145 bool AddressTaken = false; 146 147 /// Indicate that this basic block needs its symbol be emitted regardless of 148 /// whether the flow just falls-through to it. 149 bool LabelMustBeEmitted = false; 150 151 /// Indicate that this basic block is the entry block of an EH scope, i.e., 152 /// the block that used to have a catchpad or cleanuppad instruction in the 153 /// LLVM IR. 154 bool IsEHScopeEntry = false; 155 156 /// Indicates if this is a target block of a catchret. 157 bool IsEHCatchretTarget = false; 158 159 /// Indicate that this basic block is the entry block of an EH funclet. 160 bool IsEHFuncletEntry = false; 161 162 /// Indicate that this basic block is the entry block of a cleanup funclet. 163 bool IsCleanupFuncletEntry = false; 164 165 /// With basic block sections, this stores the Section ID of the basic block. 166 MBBSectionID SectionID{0}; 167 168 // Indicate that this basic block begins a section. 169 bool IsBeginSection = false; 170 171 // Indicate that this basic block ends a section. 172 bool IsEndSection = false; 173 174 /// Indicate that this basic block is the indirect dest of an INLINEASM_BR. 175 bool IsInlineAsmBrIndirectTarget = false; 176 177 /// since getSymbol is a relatively heavy-weight operation, the symbol 178 /// is only computed once and is cached. 179 mutable MCSymbol *CachedMCSymbol = nullptr; 180 181 /// Cached MCSymbol for this block (used if IsEHCatchRetTarget). 182 mutable MCSymbol *CachedEHCatchretMCSymbol = nullptr; 183 184 /// Marks the end of the basic block. Used during basic block sections to 185 /// calculate the size of the basic block, or the BB section ending with it. 186 mutable MCSymbol *CachedEndMCSymbol = nullptr; 187 188 // Intrusive list support 189 MachineBasicBlock() = default; 190 191 explicit MachineBasicBlock(MachineFunction &MF, const BasicBlock *BB); 192 193 ~MachineBasicBlock(); 194 195 // MachineBasicBlocks are allocated and owned by MachineFunction. 196 friend class MachineFunction; 197 198 public: 199 /// Return the LLVM basic block that this instance corresponded to originally. 200 /// Note that this may be NULL if this instance does not correspond directly 201 /// to an LLVM basic block. 202 const BasicBlock *getBasicBlock() const { return BB; } 203 204 /// Return the name of the corresponding LLVM basic block, or an empty string. 205 StringRef getName() const; 206 207 /// Return a formatted string to identify this block and its parent function. 208 std::string getFullName() const; 209 210 /// Test whether this block is potentially the target of an indirect branch. 211 bool hasAddressTaken() const { return AddressTaken; } 212 213 /// Set this block to reflect that it potentially is the target of an indirect 214 /// branch. 215 void setHasAddressTaken() { AddressTaken = true; } 216 217 /// Test whether this block must have its label emitted. 218 bool hasLabelMustBeEmitted() const { return LabelMustBeEmitted; } 219 220 /// Set this block to reflect that, regardless how we flow to it, we need 221 /// its label be emitted. 222 void setLabelMustBeEmitted() { LabelMustBeEmitted = true; } 223 224 /// Return the MachineFunction containing this basic block. 225 const MachineFunction *getParent() const { return xParent; } 226 MachineFunction *getParent() { return xParent; } 227 228 using instr_iterator = Instructions::iterator; 229 using const_instr_iterator = Instructions::const_iterator; 230 using reverse_instr_iterator = Instructions::reverse_iterator; 231 using const_reverse_instr_iterator = Instructions::const_reverse_iterator; 232 233 using iterator = MachineInstrBundleIterator<MachineInstr>; 234 using const_iterator = MachineInstrBundleIterator<const MachineInstr>; 235 using reverse_iterator = MachineInstrBundleIterator<MachineInstr, true>; 236 using const_reverse_iterator = 237 MachineInstrBundleIterator<const MachineInstr, true>; 238 239 unsigned size() const { return (unsigned)Insts.size(); } 240 bool empty() const { return Insts.empty(); } 241 242 MachineInstr &instr_front() { return Insts.front(); } 243 MachineInstr &instr_back() { return Insts.back(); } 244 const MachineInstr &instr_front() const { return Insts.front(); } 245 const MachineInstr &instr_back() const { return Insts.back(); } 246 247 MachineInstr &front() { return Insts.front(); } 248 MachineInstr &back() { return *--end(); } 249 const MachineInstr &front() const { return Insts.front(); } 250 const MachineInstr &back() const { return *--end(); } 251 252 instr_iterator instr_begin() { return Insts.begin(); } 253 const_instr_iterator instr_begin() const { return Insts.begin(); } 254 instr_iterator instr_end() { return Insts.end(); } 255 const_instr_iterator instr_end() const { return Insts.end(); } 256 reverse_instr_iterator instr_rbegin() { return Insts.rbegin(); } 257 const_reverse_instr_iterator instr_rbegin() const { return Insts.rbegin(); } 258 reverse_instr_iterator instr_rend () { return Insts.rend(); } 259 const_reverse_instr_iterator instr_rend () const { return Insts.rend(); } 260 261 using instr_range = iterator_range<instr_iterator>; 262 using const_instr_range = iterator_range<const_instr_iterator>; 263 instr_range instrs() { return instr_range(instr_begin(), instr_end()); } 264 const_instr_range instrs() const { 265 return const_instr_range(instr_begin(), instr_end()); 266 } 267 268 iterator begin() { return instr_begin(); } 269 const_iterator begin() const { return instr_begin(); } 270 iterator end () { return instr_end(); } 271 const_iterator end () const { return instr_end(); } 272 reverse_iterator rbegin() { 273 return reverse_iterator::getAtBundleBegin(instr_rbegin()); 274 } 275 const_reverse_iterator rbegin() const { 276 return const_reverse_iterator::getAtBundleBegin(instr_rbegin()); 277 } 278 reverse_iterator rend() { return reverse_iterator(instr_rend()); } 279 const_reverse_iterator rend() const { 280 return const_reverse_iterator(instr_rend()); 281 } 282 283 /// Support for MachineInstr::getNextNode(). 284 static Instructions MachineBasicBlock::*getSublistAccess(MachineInstr *) { 285 return &MachineBasicBlock::Insts; 286 } 287 288 inline iterator_range<iterator> terminators() { 289 return make_range(getFirstTerminator(), end()); 290 } 291 inline iterator_range<const_iterator> terminators() const { 292 return make_range(getFirstTerminator(), end()); 293 } 294 295 /// Returns a range that iterates over the phis in the basic block. 296 inline iterator_range<iterator> phis() { 297 return make_range(begin(), getFirstNonPHI()); 298 } 299 inline iterator_range<const_iterator> phis() const { 300 return const_cast<MachineBasicBlock *>(this)->phis(); 301 } 302 303 // Machine-CFG iterators 304 using pred_iterator = std::vector<MachineBasicBlock *>::iterator; 305 using const_pred_iterator = std::vector<MachineBasicBlock *>::const_iterator; 306 using succ_iterator = std::vector<MachineBasicBlock *>::iterator; 307 using const_succ_iterator = std::vector<MachineBasicBlock *>::const_iterator; 308 using pred_reverse_iterator = 309 std::vector<MachineBasicBlock *>::reverse_iterator; 310 using const_pred_reverse_iterator = 311 std::vector<MachineBasicBlock *>::const_reverse_iterator; 312 using succ_reverse_iterator = 313 std::vector<MachineBasicBlock *>::reverse_iterator; 314 using const_succ_reverse_iterator = 315 std::vector<MachineBasicBlock *>::const_reverse_iterator; 316 pred_iterator pred_begin() { return Predecessors.begin(); } 317 const_pred_iterator pred_begin() const { return Predecessors.begin(); } 318 pred_iterator pred_end() { return Predecessors.end(); } 319 const_pred_iterator pred_end() const { return Predecessors.end(); } 320 pred_reverse_iterator pred_rbegin() 321 { return Predecessors.rbegin();} 322 const_pred_reverse_iterator pred_rbegin() const 323 { return Predecessors.rbegin();} 324 pred_reverse_iterator pred_rend() 325 { return Predecessors.rend(); } 326 const_pred_reverse_iterator pred_rend() const 327 { return Predecessors.rend(); } 328 unsigned pred_size() const { 329 return (unsigned)Predecessors.size(); 330 } 331 bool pred_empty() const { return Predecessors.empty(); } 332 succ_iterator succ_begin() { return Successors.begin(); } 333 const_succ_iterator succ_begin() const { return Successors.begin(); } 334 succ_iterator succ_end() { return Successors.end(); } 335 const_succ_iterator succ_end() const { return Successors.end(); } 336 succ_reverse_iterator succ_rbegin() 337 { return Successors.rbegin(); } 338 const_succ_reverse_iterator succ_rbegin() const 339 { return Successors.rbegin(); } 340 succ_reverse_iterator succ_rend() 341 { return Successors.rend(); } 342 const_succ_reverse_iterator succ_rend() const 343 { return Successors.rend(); } 344 unsigned succ_size() const { 345 return (unsigned)Successors.size(); 346 } 347 bool succ_empty() const { return Successors.empty(); } 348 349 inline iterator_range<pred_iterator> predecessors() { 350 return make_range(pred_begin(), pred_end()); 351 } 352 inline iterator_range<const_pred_iterator> predecessors() const { 353 return make_range(pred_begin(), pred_end()); 354 } 355 inline iterator_range<succ_iterator> successors() { 356 return make_range(succ_begin(), succ_end()); 357 } 358 inline iterator_range<const_succ_iterator> successors() const { 359 return make_range(succ_begin(), succ_end()); 360 } 361 362 // LiveIn management methods. 363 364 /// Adds the specified register as a live in. Note that it is an error to add 365 /// the same register to the same set more than once unless the intention is 366 /// to call sortUniqueLiveIns after all registers are added. 367 void addLiveIn(MCRegister PhysReg, 368 LaneBitmask LaneMask = LaneBitmask::getAll()) { 369 LiveIns.push_back(RegisterMaskPair(PhysReg, LaneMask)); 370 } 371 void addLiveIn(const RegisterMaskPair &RegMaskPair) { 372 LiveIns.push_back(RegMaskPair); 373 } 374 375 /// Sorts and uniques the LiveIns vector. It can be significantly faster to do 376 /// this than repeatedly calling isLiveIn before calling addLiveIn for every 377 /// LiveIn insertion. 378 void sortUniqueLiveIns(); 379 380 /// Clear live in list. 381 void clearLiveIns(); 382 383 /// Add PhysReg as live in to this block, and ensure that there is a copy of 384 /// PhysReg to a virtual register of class RC. Return the virtual register 385 /// that is a copy of the live in PhysReg. 386 Register addLiveIn(MCRegister PhysReg, const TargetRegisterClass *RC); 387 388 /// Remove the specified register from the live in set. 389 void removeLiveIn(MCPhysReg Reg, 390 LaneBitmask LaneMask = LaneBitmask::getAll()); 391 392 /// Return true if the specified register is in the live in set. 393 bool isLiveIn(MCPhysReg Reg, 394 LaneBitmask LaneMask = LaneBitmask::getAll()) const; 395 396 // Iteration support for live in sets. These sets are kept in sorted 397 // order by their register number. 398 using livein_iterator = LiveInVector::const_iterator; 399 #ifndef NDEBUG 400 /// Unlike livein_begin, this method does not check that the liveness 401 /// information is accurate. Still for debug purposes it may be useful 402 /// to have iterators that won't assert if the liveness information 403 /// is not current. 404 livein_iterator livein_begin_dbg() const { return LiveIns.begin(); } 405 iterator_range<livein_iterator> liveins_dbg() const { 406 return make_range(livein_begin_dbg(), livein_end()); 407 } 408 #endif 409 livein_iterator livein_begin() const; 410 livein_iterator livein_end() const { return LiveIns.end(); } 411 bool livein_empty() const { return LiveIns.empty(); } 412 iterator_range<livein_iterator> liveins() const { 413 return make_range(livein_begin(), livein_end()); 414 } 415 416 /// Remove entry from the livein set and return iterator to the next. 417 livein_iterator removeLiveIn(livein_iterator I); 418 419 class liveout_iterator { 420 public: 421 using iterator_category = std::input_iterator_tag; 422 using difference_type = std::ptrdiff_t; 423 using value_type = RegisterMaskPair; 424 using pointer = const RegisterMaskPair *; 425 using reference = const RegisterMaskPair &; 426 427 liveout_iterator(const MachineBasicBlock &MBB, MCPhysReg ExceptionPointer, 428 MCPhysReg ExceptionSelector, bool End) 429 : ExceptionPointer(ExceptionPointer), 430 ExceptionSelector(ExceptionSelector), BlockI(MBB.succ_begin()), 431 BlockEnd(MBB.succ_end()) { 432 if (End) 433 BlockI = BlockEnd; 434 else if (BlockI != BlockEnd) { 435 LiveRegI = (*BlockI)->livein_begin(); 436 if (!advanceToValidPosition()) 437 return; 438 if (LiveRegI->PhysReg == ExceptionPointer || 439 LiveRegI->PhysReg == ExceptionSelector) 440 ++(*this); 441 } 442 } 443 444 liveout_iterator &operator++() { 445 do { 446 ++LiveRegI; 447 if (!advanceToValidPosition()) 448 return *this; 449 } while ((*BlockI)->isEHPad() && 450 (LiveRegI->PhysReg == ExceptionPointer || 451 LiveRegI->PhysReg == ExceptionSelector)); 452 return *this; 453 } 454 455 liveout_iterator operator++(int) { 456 liveout_iterator Tmp = *this; 457 ++(*this); 458 return Tmp; 459 } 460 461 reference operator*() const { 462 return *LiveRegI; 463 } 464 465 pointer operator->() const { 466 return &*LiveRegI; 467 } 468 469 bool operator==(const liveout_iterator &RHS) const { 470 if (BlockI != BlockEnd) 471 return BlockI == RHS.BlockI && LiveRegI == RHS.LiveRegI; 472 return RHS.BlockI == BlockEnd; 473 } 474 475 bool operator!=(const liveout_iterator &RHS) const { 476 return !(*this == RHS); 477 } 478 private: 479 bool advanceToValidPosition() { 480 if (LiveRegI != (*BlockI)->livein_end()) 481 return true; 482 483 do { 484 ++BlockI; 485 } while (BlockI != BlockEnd && (*BlockI)->livein_empty()); 486 if (BlockI == BlockEnd) 487 return false; 488 489 LiveRegI = (*BlockI)->livein_begin(); 490 return true; 491 } 492 493 MCPhysReg ExceptionPointer, ExceptionSelector; 494 const_succ_iterator BlockI; 495 const_succ_iterator BlockEnd; 496 livein_iterator LiveRegI; 497 }; 498 499 /// Iterator scanning successor basic blocks' liveins to determine the 500 /// registers potentially live at the end of this block. There may be 501 /// duplicates or overlapping registers in the list returned. 502 liveout_iterator liveout_begin() const; 503 liveout_iterator liveout_end() const { 504 return liveout_iterator(*this, 0, 0, true); 505 } 506 iterator_range<liveout_iterator> liveouts() const { 507 return make_range(liveout_begin(), liveout_end()); 508 } 509 510 /// Get the clobber mask for the start of this basic block. Funclets use this 511 /// to prevent register allocation across funclet transitions. 512 const uint32_t *getBeginClobberMask(const TargetRegisterInfo *TRI) const; 513 514 /// Get the clobber mask for the end of the basic block. 515 /// \see getBeginClobberMask() 516 const uint32_t *getEndClobberMask(const TargetRegisterInfo *TRI) const; 517 518 /// Return alignment of the basic block. 519 Align getAlignment() const { return Alignment; } 520 521 /// Set alignment of the basic block. 522 void setAlignment(Align A) { Alignment = A; } 523 524 /// Returns true if the block is a landing pad. That is this basic block is 525 /// entered via an exception handler. 526 bool isEHPad() const { return IsEHPad; } 527 528 /// Indicates the block is a landing pad. That is this basic block is entered 529 /// via an exception handler. 530 void setIsEHPad(bool V = true) { IsEHPad = V; } 531 532 bool hasEHPadSuccessor() const; 533 534 /// Returns true if this is the entry block of the function. 535 bool isEntryBlock() const; 536 537 /// Returns true if this is the entry block of an EH scope, i.e., the block 538 /// that used to have a catchpad or cleanuppad instruction in the LLVM IR. 539 bool isEHScopeEntry() const { return IsEHScopeEntry; } 540 541 /// Indicates if this is the entry block of an EH scope, i.e., the block that 542 /// that used to have a catchpad or cleanuppad instruction in the LLVM IR. 543 void setIsEHScopeEntry(bool V = true) { IsEHScopeEntry = V; } 544 545 /// Returns true if this is a target block of a catchret. 546 bool isEHCatchretTarget() const { return IsEHCatchretTarget; } 547 548 /// Indicates if this is a target block of a catchret. 549 void setIsEHCatchretTarget(bool V = true) { IsEHCatchretTarget = V; } 550 551 /// Returns true if this is the entry block of an EH funclet. 552 bool isEHFuncletEntry() const { return IsEHFuncletEntry; } 553 554 /// Indicates if this is the entry block of an EH funclet. 555 void setIsEHFuncletEntry(bool V = true) { IsEHFuncletEntry = V; } 556 557 /// Returns true if this is the entry block of a cleanup funclet. 558 bool isCleanupFuncletEntry() const { return IsCleanupFuncletEntry; } 559 560 /// Indicates if this is the entry block of a cleanup funclet. 561 void setIsCleanupFuncletEntry(bool V = true) { IsCleanupFuncletEntry = V; } 562 563 /// Returns true if this block begins any section. 564 bool isBeginSection() const { return IsBeginSection; } 565 566 /// Returns true if this block ends any section. 567 bool isEndSection() const { return IsEndSection; } 568 569 void setIsBeginSection(bool V = true) { IsBeginSection = V; } 570 571 void setIsEndSection(bool V = true) { IsEndSection = V; } 572 573 /// Returns the section ID of this basic block. 574 MBBSectionID getSectionID() const { return SectionID; } 575 576 /// Returns the unique section ID number of this basic block. 577 unsigned getSectionIDNum() const { 578 return ((unsigned)MBBSectionID::SectionType::Cold) - 579 ((unsigned)SectionID.Type) + SectionID.Number; 580 } 581 582 /// Sets the section ID for this basic block. 583 void setSectionID(MBBSectionID V) { SectionID = V; } 584 585 /// Returns the MCSymbol marking the end of this basic block. 586 MCSymbol *getEndSymbol() const; 587 588 /// Returns true if this block may have an INLINEASM_BR (overestimate, by 589 /// checking if any of the successors are indirect targets of any inlineasm_br 590 /// in the function). 591 bool mayHaveInlineAsmBr() const; 592 593 /// Returns true if this is the indirect dest of an INLINEASM_BR. 594 bool isInlineAsmBrIndirectTarget() const { 595 return IsInlineAsmBrIndirectTarget; 596 } 597 598 /// Indicates if this is the indirect dest of an INLINEASM_BR. 599 void setIsInlineAsmBrIndirectTarget(bool V = true) { 600 IsInlineAsmBrIndirectTarget = V; 601 } 602 603 /// Returns true if it is legal to hoist instructions into this block. 604 bool isLegalToHoistInto() const; 605 606 // Code Layout methods. 607 608 /// Move 'this' block before or after the specified block. This only moves 609 /// the block, it does not modify the CFG or adjust potential fall-throughs at 610 /// the end of the block. 611 void moveBefore(MachineBasicBlock *NewAfter); 612 void moveAfter(MachineBasicBlock *NewBefore); 613 614 /// Returns true if this and MBB belong to the same section. 615 bool sameSection(const MachineBasicBlock *MBB) const { 616 return getSectionID() == MBB->getSectionID(); 617 } 618 619 /// Update the terminator instructions in block to account for changes to 620 /// block layout which may have been made. PreviousLayoutSuccessor should be 621 /// set to the block which may have been used as fallthrough before the block 622 /// layout was modified. If the block previously fell through to that block, 623 /// it may now need a branch. If it previously branched to another block, it 624 /// may now be able to fallthrough to the current layout successor. 625 void updateTerminator(MachineBasicBlock *PreviousLayoutSuccessor); 626 627 // Machine-CFG mutators 628 629 /// Add Succ as a successor of this MachineBasicBlock. The Predecessors list 630 /// of Succ is automatically updated. PROB parameter is stored in 631 /// Probabilities list. The default probability is set as unknown. Mixing 632 /// known and unknown probabilities in successor list is not allowed. When all 633 /// successors have unknown probabilities, 1 / N is returned as the 634 /// probability for each successor, where N is the number of successors. 635 /// 636 /// Note that duplicate Machine CFG edges are not allowed. 637 void addSuccessor(MachineBasicBlock *Succ, 638 BranchProbability Prob = BranchProbability::getUnknown()); 639 640 /// Add Succ as a successor of this MachineBasicBlock. The Predecessors list 641 /// of Succ is automatically updated. The probability is not provided because 642 /// BPI is not available (e.g. -O0 is used), in which case edge probabilities 643 /// won't be used. Using this interface can save some space. 644 void addSuccessorWithoutProb(MachineBasicBlock *Succ); 645 646 /// Set successor probability of a given iterator. 647 void setSuccProbability(succ_iterator I, BranchProbability Prob); 648 649 /// Normalize probabilities of all successors so that the sum of them becomes 650 /// one. This is usually done when the current update on this MBB is done, and 651 /// the sum of its successors' probabilities is not guaranteed to be one. The 652 /// user is responsible for the correct use of this function. 653 /// MBB::removeSuccessor() has an option to do this automatically. 654 void normalizeSuccProbs() { 655 BranchProbability::normalizeProbabilities(Probs.begin(), Probs.end()); 656 } 657 658 /// Validate successors' probabilities and check if the sum of them is 659 /// approximate one. This only works in DEBUG mode. 660 void validateSuccProbs() const; 661 662 /// Remove successor from the successors list of this MachineBasicBlock. The 663 /// Predecessors list of Succ is automatically updated. 664 /// If NormalizeSuccProbs is true, then normalize successors' probabilities 665 /// after the successor is removed. 666 void removeSuccessor(MachineBasicBlock *Succ, 667 bool NormalizeSuccProbs = false); 668 669 /// Remove specified successor from the successors list of this 670 /// MachineBasicBlock. The Predecessors list of Succ is automatically updated. 671 /// If NormalizeSuccProbs is true, then normalize successors' probabilities 672 /// after the successor is removed. 673 /// Return the iterator to the element after the one removed. 674 succ_iterator removeSuccessor(succ_iterator I, 675 bool NormalizeSuccProbs = false); 676 677 /// Replace successor OLD with NEW and update probability info. 678 void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New); 679 680 /// Copy a successor (and any probability info) from original block to this 681 /// block's. Uses an iterator into the original blocks successors. 682 /// 683 /// This is useful when doing a partial clone of successors. Afterward, the 684 /// probabilities may need to be normalized. 685 void copySuccessor(MachineBasicBlock *Orig, succ_iterator I); 686 687 /// Split the old successor into old plus new and updates the probability 688 /// info. 689 void splitSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New, 690 bool NormalizeSuccProbs = false); 691 692 /// Transfers all the successors from MBB to this machine basic block (i.e., 693 /// copies all the successors FromMBB and remove all the successors from 694 /// FromMBB). 695 void transferSuccessors(MachineBasicBlock *FromMBB); 696 697 /// Transfers all the successors, as in transferSuccessors, and update PHI 698 /// operands in the successor blocks which refer to FromMBB to refer to this. 699 void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB); 700 701 /// move all pseudo probes in this block to the end of /c ToMBB To and tag 702 /// them dangling. 703 void moveAndDanglePseudoProbes(MachineBasicBlock *ToMBB); 704 705 /// Return true if any of the successors have probabilities attached to them. 706 bool hasSuccessorProbabilities() const { return !Probs.empty(); } 707 708 /// Return true if the specified MBB is a predecessor of this block. 709 bool isPredecessor(const MachineBasicBlock *MBB) const; 710 711 /// Return true if the specified MBB is a successor of this block. 712 bool isSuccessor(const MachineBasicBlock *MBB) const; 713 714 /// Return true if the specified MBB will be emitted immediately after this 715 /// block, such that if this block exits by falling through, control will 716 /// transfer to the specified MBB. Note that MBB need not be a successor at 717 /// all, for example if this block ends with an unconditional branch to some 718 /// other block. 719 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const; 720 721 /// Return the fallthrough block if the block can implicitly 722 /// transfer control to the block after it by falling off the end of 723 /// it. This should return null if it can reach the block after 724 /// it, but it uses an explicit branch to do so (e.g., a table 725 /// jump). Non-null return is a conservative answer. 726 MachineBasicBlock *getFallThrough(); 727 728 /// Return true if the block can implicitly transfer control to the 729 /// block after it by falling off the end of it. This should return 730 /// false if it can reach the block after it, but it uses an 731 /// explicit branch to do so (e.g., a table jump). True is a 732 /// conservative answer. 733 bool canFallThrough(); 734 735 /// Returns a pointer to the first instruction in this block that is not a 736 /// PHINode instruction. When adding instructions to the beginning of the 737 /// basic block, they should be added before the returned value, not before 738 /// the first instruction, which might be PHI. 739 /// Returns end() is there's no non-PHI instruction. 740 iterator getFirstNonPHI(); 741 742 /// Return the first instruction in MBB after I that is not a PHI or a label. 743 /// This is the correct point to insert lowered copies at the beginning of a 744 /// basic block that must be before any debugging information. 745 iterator SkipPHIsAndLabels(iterator I); 746 747 /// Return the first instruction in MBB after I that is not a PHI, label or 748 /// debug. This is the correct point to insert copies at the beginning of a 749 /// basic block. 750 iterator SkipPHIsLabelsAndDebug(iterator I, bool SkipPseudoOp = true); 751 752 /// Returns an iterator to the first terminator instruction of this basic 753 /// block. If a terminator does not exist, it returns end(). 754 iterator getFirstTerminator(); 755 const_iterator getFirstTerminator() const { 756 return const_cast<MachineBasicBlock *>(this)->getFirstTerminator(); 757 } 758 759 /// Same getFirstTerminator but it ignores bundles and return an 760 /// instr_iterator instead. 761 instr_iterator getFirstInstrTerminator(); 762 763 /// Returns an iterator to the first non-debug instruction in the basic block, 764 /// or end(). Skip any pseudo probe operation if \c SkipPseudoOp is true. 765 /// Pseudo probes are like debug instructions which do not turn into real 766 /// machine code. We try to use the function to skip both debug instructions 767 /// and pseudo probe operations to avoid API proliferation. This should work 768 /// most of the time when considering optimizing the rest of code in the 769 /// block, except for certain cases where pseudo probes are designed to block 770 /// the optimizations. For example, code merge like optimizations are supposed 771 /// to be blocked by pseudo probes for better AutoFDO profile quality. 772 /// Therefore, they should be considered as a valid instruction when this 773 /// function is called in a context of such optimizations. On the other hand, 774 /// \c SkipPseudoOp should be true when it's used in optimizations that 775 /// unlikely hurt profile quality, e.g., without block merging. The default 776 /// value of \c SkipPseudoOp is set to true to maximize code quality in 777 /// general, with an explict false value passed in in a few places like branch 778 /// folding and if-conversion to favor profile quality. 779 iterator getFirstNonDebugInstr(bool SkipPseudoOp = true); 780 const_iterator getFirstNonDebugInstr(bool SkipPseudoOp = true) const { 781 return const_cast<MachineBasicBlock *>(this)->getFirstNonDebugInstr( 782 SkipPseudoOp); 783 } 784 785 /// Returns an iterator to the last non-debug instruction in the basic block, 786 /// or end(). Skip any pseudo operation if \c SkipPseudoOp is true. 787 /// Pseudo probes are like debug instructions which do not turn into real 788 /// machine code. We try to use the function to skip both debug instructions 789 /// and pseudo probe operations to avoid API proliferation. This should work 790 /// most of the time when considering optimizing the rest of code in the 791 /// block, except for certain cases where pseudo probes are designed to block 792 /// the optimizations. For example, code merge like optimizations are supposed 793 /// to be blocked by pseudo probes for better AutoFDO profile quality. 794 /// Therefore, they should be considered as a valid instruction when this 795 /// function is called in a context of such optimizations. On the other hand, 796 /// \c SkipPseudoOp should be true when it's used in optimizations that 797 /// unlikely hurt profile quality, e.g., without block merging. The default 798 /// value of \c SkipPseudoOp is set to true to maximize code quality in 799 /// general, with an explict false value passed in in a few places like branch 800 /// folding and if-conversion to favor profile quality. 801 iterator getLastNonDebugInstr(bool SkipPseudoOp = true); 802 const_iterator getLastNonDebugInstr(bool SkipPseudoOp = true) const { 803 return const_cast<MachineBasicBlock *>(this)->getLastNonDebugInstr( 804 SkipPseudoOp); 805 } 806 807 /// Convenience function that returns true if the block ends in a return 808 /// instruction. 809 bool isReturnBlock() const { 810 return !empty() && back().isReturn(); 811 } 812 813 /// Convenience function that returns true if the bock ends in a EH scope 814 /// return instruction. 815 bool isEHScopeReturnBlock() const { 816 return !empty() && back().isEHScopeReturn(); 817 } 818 819 /// Split a basic block into 2 pieces at \p SplitPoint. A new block will be 820 /// inserted after this block, and all instructions after \p SplitInst moved 821 /// to it (\p SplitInst will be in the original block). If \p LIS is provided, 822 /// LiveIntervals will be appropriately updated. \return the newly inserted 823 /// block. 824 /// 825 /// If \p UpdateLiveIns is true, this will ensure the live ins list is 826 /// accurate, including for physreg uses/defs in the original block. 827 MachineBasicBlock *splitAt(MachineInstr &SplitInst, bool UpdateLiveIns = true, 828 LiveIntervals *LIS = nullptr); 829 830 /// Split the critical edge from this block to the given successor block, and 831 /// return the newly created block, or null if splitting is not possible. 832 /// 833 /// This function updates LiveVariables, MachineDominatorTree, and 834 /// MachineLoopInfo, as applicable. 835 MachineBasicBlock * 836 SplitCriticalEdge(MachineBasicBlock *Succ, Pass &P, 837 std::vector<SparseBitVector<>> *LiveInSets = nullptr); 838 839 /// Check if the edge between this block and the given successor \p 840 /// Succ, can be split. If this returns true a subsequent call to 841 /// SplitCriticalEdge is guaranteed to return a valid basic block if 842 /// no changes occurred in the meantime. 843 bool canSplitCriticalEdge(const MachineBasicBlock *Succ) const; 844 845 void pop_front() { Insts.pop_front(); } 846 void pop_back() { Insts.pop_back(); } 847 void push_back(MachineInstr *MI) { Insts.push_back(MI); } 848 849 /// Insert MI into the instruction list before I, possibly inside a bundle. 850 /// 851 /// If the insertion point is inside a bundle, MI will be added to the bundle, 852 /// otherwise MI will not be added to any bundle. That means this function 853 /// alone can't be used to prepend or append instructions to bundles. See 854 /// MIBundleBuilder::insert() for a more reliable way of doing that. 855 instr_iterator insert(instr_iterator I, MachineInstr *M); 856 857 /// Insert a range of instructions into the instruction list before I. 858 template<typename IT> 859 void insert(iterator I, IT S, IT E) { 860 assert((I == end() || I->getParent() == this) && 861 "iterator points outside of basic block"); 862 Insts.insert(I.getInstrIterator(), S, E); 863 } 864 865 /// Insert MI into the instruction list before I. 866 iterator insert(iterator I, MachineInstr *MI) { 867 assert((I == end() || I->getParent() == this) && 868 "iterator points outside of basic block"); 869 assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() && 870 "Cannot insert instruction with bundle flags"); 871 return Insts.insert(I.getInstrIterator(), MI); 872 } 873 874 /// Insert MI into the instruction list after I. 875 iterator insertAfter(iterator I, MachineInstr *MI) { 876 assert((I == end() || I->getParent() == this) && 877 "iterator points outside of basic block"); 878 assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() && 879 "Cannot insert instruction with bundle flags"); 880 return Insts.insertAfter(I.getInstrIterator(), MI); 881 } 882 883 /// If I is bundled then insert MI into the instruction list after the end of 884 /// the bundle, otherwise insert MI immediately after I. 885 instr_iterator insertAfterBundle(instr_iterator I, MachineInstr *MI) { 886 assert((I == instr_end() || I->getParent() == this) && 887 "iterator points outside of basic block"); 888 assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() && 889 "Cannot insert instruction with bundle flags"); 890 while (I->isBundledWithSucc()) 891 ++I; 892 return Insts.insertAfter(I, MI); 893 } 894 895 /// Remove an instruction from the instruction list and delete it. 896 /// 897 /// If the instruction is part of a bundle, the other instructions in the 898 /// bundle will still be bundled after removing the single instruction. 899 instr_iterator erase(instr_iterator I); 900 901 /// Remove an instruction from the instruction list and delete it. 902 /// 903 /// If the instruction is part of a bundle, the other instructions in the 904 /// bundle will still be bundled after removing the single instruction. 905 instr_iterator erase_instr(MachineInstr *I) { 906 return erase(instr_iterator(I)); 907 } 908 909 /// Remove a range of instructions from the instruction list and delete them. 910 iterator erase(iterator I, iterator E) { 911 return Insts.erase(I.getInstrIterator(), E.getInstrIterator()); 912 } 913 914 /// Remove an instruction or bundle from the instruction list and delete it. 915 /// 916 /// If I points to a bundle of instructions, they are all erased. 917 iterator erase(iterator I) { 918 return erase(I, std::next(I)); 919 } 920 921 /// Remove an instruction from the instruction list and delete it. 922 /// 923 /// If I is the head of a bundle of instructions, the whole bundle will be 924 /// erased. 925 iterator erase(MachineInstr *I) { 926 return erase(iterator(I)); 927 } 928 929 /// Remove the unbundled instruction from the instruction list without 930 /// deleting it. 931 /// 932 /// This function can not be used to remove bundled instructions, use 933 /// remove_instr to remove individual instructions from a bundle. 934 MachineInstr *remove(MachineInstr *I) { 935 assert(!I->isBundled() && "Cannot remove bundled instructions"); 936 return Insts.remove(instr_iterator(I)); 937 } 938 939 /// Remove the possibly bundled instruction from the instruction list 940 /// without deleting it. 941 /// 942 /// If the instruction is part of a bundle, the other instructions in the 943 /// bundle will still be bundled after removing the single instruction. 944 MachineInstr *remove_instr(MachineInstr *I); 945 946 void clear() { 947 Insts.clear(); 948 } 949 950 /// Take an instruction from MBB 'Other' at the position From, and insert it 951 /// into this MBB right before 'Where'. 952 /// 953 /// If From points to a bundle of instructions, the whole bundle is moved. 954 void splice(iterator Where, MachineBasicBlock *Other, iterator From) { 955 // The range splice() doesn't allow noop moves, but this one does. 956 if (Where != From) 957 splice(Where, Other, From, std::next(From)); 958 } 959 960 /// Take a block of instructions from MBB 'Other' in the range [From, To), 961 /// and insert them into this MBB right before 'Where'. 962 /// 963 /// The instruction at 'Where' must not be included in the range of 964 /// instructions to move. 965 void splice(iterator Where, MachineBasicBlock *Other, 966 iterator From, iterator To) { 967 Insts.splice(Where.getInstrIterator(), Other->Insts, 968 From.getInstrIterator(), To.getInstrIterator()); 969 } 970 971 /// This method unlinks 'this' from the containing function, and returns it, 972 /// but does not delete it. 973 MachineBasicBlock *removeFromParent(); 974 975 /// This method unlinks 'this' from the containing function and deletes it. 976 void eraseFromParent(); 977 978 /// Given a machine basic block that branched to 'Old', change the code and 979 /// CFG so that it branches to 'New' instead. 980 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New); 981 982 /// Update all phi nodes in this basic block to refer to basic block \p New 983 /// instead of basic block \p Old. 984 void replacePhiUsesWith(MachineBasicBlock *Old, MachineBasicBlock *New); 985 986 /// Find the next valid DebugLoc starting at MBBI, skipping any DBG_VALUE 987 /// and DBG_LABEL instructions. Return UnknownLoc if there is none. 988 DebugLoc findDebugLoc(instr_iterator MBBI); 989 DebugLoc findDebugLoc(iterator MBBI) { 990 return findDebugLoc(MBBI.getInstrIterator()); 991 } 992 993 /// Has exact same behavior as @ref findDebugLoc (it also 994 /// searches from the first to the last MI of this MBB) except 995 /// that this takes reverse iterator. 996 DebugLoc rfindDebugLoc(reverse_instr_iterator MBBI); 997 DebugLoc rfindDebugLoc(reverse_iterator MBBI) { 998 return rfindDebugLoc(MBBI.getInstrIterator()); 999 } 1000 1001 /// Find the previous valid DebugLoc preceding MBBI, skipping and DBG_VALUE 1002 /// instructions. Return UnknownLoc if there is none. 1003 DebugLoc findPrevDebugLoc(instr_iterator MBBI); 1004 DebugLoc findPrevDebugLoc(iterator MBBI) { 1005 return findPrevDebugLoc(MBBI.getInstrIterator()); 1006 } 1007 1008 /// Has exact same behavior as @ref findPrevDebugLoc (it also 1009 /// searches from the last to the first MI of this MBB) except 1010 /// that this takes reverse iterator. 1011 DebugLoc rfindPrevDebugLoc(reverse_instr_iterator MBBI); 1012 DebugLoc rfindPrevDebugLoc(reverse_iterator MBBI) { 1013 return rfindPrevDebugLoc(MBBI.getInstrIterator()); 1014 } 1015 1016 /// Find and return the merged DebugLoc of the branch instructions of the 1017 /// block. Return UnknownLoc if there is none. 1018 DebugLoc findBranchDebugLoc(); 1019 1020 /// Possible outcome of a register liveness query to computeRegisterLiveness() 1021 enum LivenessQueryResult { 1022 LQR_Live, ///< Register is known to be (at least partially) live. 1023 LQR_Dead, ///< Register is known to be fully dead. 1024 LQR_Unknown ///< Register liveness not decidable from local neighborhood. 1025 }; 1026 1027 /// Return whether (physical) register \p Reg has been defined and not 1028 /// killed as of just before \p Before. 1029 /// 1030 /// Search is localised to a neighborhood of \p Neighborhood instructions 1031 /// before (searching for defs or kills) and \p Neighborhood instructions 1032 /// after (searching just for defs) \p Before. 1033 /// 1034 /// \p Reg must be a physical register. 1035 LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI, 1036 MCRegister Reg, 1037 const_iterator Before, 1038 unsigned Neighborhood = 10) const; 1039 1040 // Debugging methods. 1041 void dump() const; 1042 void print(raw_ostream &OS, const SlotIndexes * = nullptr, 1043 bool IsStandalone = true) const; 1044 void print(raw_ostream &OS, ModuleSlotTracker &MST, 1045 const SlotIndexes * = nullptr, bool IsStandalone = true) const; 1046 1047 enum PrintNameFlag { 1048 PrintNameIr = (1 << 0), ///< Add IR name where available 1049 PrintNameAttributes = (1 << 1), ///< Print attributes 1050 }; 1051 1052 void printName(raw_ostream &os, unsigned printNameFlags = PrintNameIr, 1053 ModuleSlotTracker *moduleSlotTracker = nullptr) const; 1054 1055 // Printing method used by LoopInfo. 1056 void printAsOperand(raw_ostream &OS, bool PrintType = true) const; 1057 1058 /// MachineBasicBlocks are uniquely numbered at the function level, unless 1059 /// they're not in a MachineFunction yet, in which case this will return -1. 1060 int getNumber() const { return Number; } 1061 void setNumber(int N) { Number = N; } 1062 1063 /// Return the MCSymbol for this basic block. 1064 MCSymbol *getSymbol() const; 1065 1066 /// Return the EHCatchret Symbol for this basic block. 1067 MCSymbol *getEHCatchretSymbol() const; 1068 1069 Optional<uint64_t> getIrrLoopHeaderWeight() const { 1070 return IrrLoopHeaderWeight; 1071 } 1072 1073 void setIrrLoopHeaderWeight(uint64_t Weight) { 1074 IrrLoopHeaderWeight = Weight; 1075 } 1076 1077 private: 1078 /// Return probability iterator corresponding to the I successor iterator. 1079 probability_iterator getProbabilityIterator(succ_iterator I); 1080 const_probability_iterator 1081 getProbabilityIterator(const_succ_iterator I) const; 1082 1083 friend class MachineBranchProbabilityInfo; 1084 friend class MIPrinter; 1085 1086 /// Return probability of the edge from this block to MBB. This method should 1087 /// NOT be called directly, but by using getEdgeProbability method from 1088 /// MachineBranchProbabilityInfo class. 1089 BranchProbability getSuccProbability(const_succ_iterator Succ) const; 1090 1091 // Methods used to maintain doubly linked list of blocks... 1092 friend struct ilist_callback_traits<MachineBasicBlock>; 1093 1094 // Machine-CFG mutators 1095 1096 /// Add Pred as a predecessor of this MachineBasicBlock. Don't do this 1097 /// unless you know what you're doing, because it doesn't update Pred's 1098 /// successors list. Use Pred->addSuccessor instead. 1099 void addPredecessor(MachineBasicBlock *Pred); 1100 1101 /// Remove Pred as a predecessor of this MachineBasicBlock. Don't do this 1102 /// unless you know what you're doing, because it doesn't update Pred's 1103 /// successors list. Use Pred->removeSuccessor instead. 1104 void removePredecessor(MachineBasicBlock *Pred); 1105 }; 1106 1107 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB); 1108 1109 /// Prints a machine basic block reference. 1110 /// 1111 /// The format is: 1112 /// %bb.5 - a machine basic block with MBB.getNumber() == 5. 1113 /// 1114 /// Usage: OS << printMBBReference(MBB) << '\n'; 1115 Printable printMBBReference(const MachineBasicBlock &MBB); 1116 1117 // This is useful when building IndexedMaps keyed on basic block pointers. 1118 struct MBB2NumberFunctor { 1119 using argument_type = const MachineBasicBlock *; 1120 unsigned operator()(const MachineBasicBlock *MBB) const { 1121 return MBB->getNumber(); 1122 } 1123 }; 1124 1125 //===--------------------------------------------------------------------===// 1126 // GraphTraits specializations for machine basic block graphs (machine-CFGs) 1127 //===--------------------------------------------------------------------===// 1128 1129 // Provide specializations of GraphTraits to be able to treat a 1130 // MachineFunction as a graph of MachineBasicBlocks. 1131 // 1132 1133 template <> struct GraphTraits<MachineBasicBlock *> { 1134 using NodeRef = MachineBasicBlock *; 1135 using ChildIteratorType = MachineBasicBlock::succ_iterator; 1136 1137 static NodeRef getEntryNode(MachineBasicBlock *BB) { return BB; } 1138 static ChildIteratorType child_begin(NodeRef N) { return N->succ_begin(); } 1139 static ChildIteratorType child_end(NodeRef N) { return N->succ_end(); } 1140 }; 1141 1142 template <> struct GraphTraits<const MachineBasicBlock *> { 1143 using NodeRef = const MachineBasicBlock *; 1144 using ChildIteratorType = MachineBasicBlock::const_succ_iterator; 1145 1146 static NodeRef getEntryNode(const MachineBasicBlock *BB) { return BB; } 1147 static ChildIteratorType child_begin(NodeRef N) { return N->succ_begin(); } 1148 static ChildIteratorType child_end(NodeRef N) { return N->succ_end(); } 1149 }; 1150 1151 // Provide specializations of GraphTraits to be able to treat a 1152 // MachineFunction as a graph of MachineBasicBlocks and to walk it 1153 // in inverse order. Inverse order for a function is considered 1154 // to be when traversing the predecessor edges of a MBB 1155 // instead of the successor edges. 1156 // 1157 template <> struct GraphTraits<Inverse<MachineBasicBlock*>> { 1158 using NodeRef = MachineBasicBlock *; 1159 using ChildIteratorType = MachineBasicBlock::pred_iterator; 1160 1161 static NodeRef getEntryNode(Inverse<MachineBasicBlock *> G) { 1162 return G.Graph; 1163 } 1164 1165 static ChildIteratorType child_begin(NodeRef N) { return N->pred_begin(); } 1166 static ChildIteratorType child_end(NodeRef N) { return N->pred_end(); } 1167 }; 1168 1169 template <> struct GraphTraits<Inverse<const MachineBasicBlock*>> { 1170 using NodeRef = const MachineBasicBlock *; 1171 using ChildIteratorType = MachineBasicBlock::const_pred_iterator; 1172 1173 static NodeRef getEntryNode(Inverse<const MachineBasicBlock *> G) { 1174 return G.Graph; 1175 } 1176 1177 static ChildIteratorType child_begin(NodeRef N) { return N->pred_begin(); } 1178 static ChildIteratorType child_end(NodeRef N) { return N->pred_end(); } 1179 }; 1180 1181 /// MachineInstrSpan provides an interface to get an iteration range 1182 /// containing the instruction it was initialized with, along with all 1183 /// those instructions inserted prior to or following that instruction 1184 /// at some point after the MachineInstrSpan is constructed. 1185 class MachineInstrSpan { 1186 MachineBasicBlock &MBB; 1187 MachineBasicBlock::iterator I, B, E; 1188 1189 public: 1190 MachineInstrSpan(MachineBasicBlock::iterator I, MachineBasicBlock *BB) 1191 : MBB(*BB), I(I), B(I == MBB.begin() ? MBB.end() : std::prev(I)), 1192 E(std::next(I)) { 1193 assert(I == BB->end() || I->getParent() == BB); 1194 } 1195 1196 MachineBasicBlock::iterator begin() { 1197 return B == MBB.end() ? MBB.begin() : std::next(B); 1198 } 1199 MachineBasicBlock::iterator end() { return E; } 1200 bool empty() { return begin() == end(); } 1201 1202 MachineBasicBlock::iterator getInitial() { return I; } 1203 }; 1204 1205 /// Increment \p It until it points to a non-debug instruction or to \p End 1206 /// and return the resulting iterator. This function should only be used 1207 /// MachineBasicBlock::{iterator, const_iterator, instr_iterator, 1208 /// const_instr_iterator} and the respective reverse iterators. 1209 template <typename IterT> 1210 inline IterT skipDebugInstructionsForward(IterT It, IterT End, 1211 bool SkipPseudoOp = true) { 1212 while (It != End && 1213 (It->isDebugInstr() || (SkipPseudoOp && It->isPseudoProbe()))) 1214 ++It; 1215 return It; 1216 } 1217 1218 /// Decrement \p It until it points to a non-debug instruction or to \p Begin 1219 /// and return the resulting iterator. This function should only be used 1220 /// MachineBasicBlock::{iterator, const_iterator, instr_iterator, 1221 /// const_instr_iterator} and the respective reverse iterators. 1222 template <class IterT> 1223 inline IterT skipDebugInstructionsBackward(IterT It, IterT Begin, 1224 bool SkipPseudoOp = true) { 1225 while (It != Begin && 1226 (It->isDebugInstr() || (SkipPseudoOp && It->isPseudoProbe()))) 1227 --It; 1228 return It; 1229 } 1230 1231 /// Increment \p It, then continue incrementing it while it points to a debug 1232 /// instruction. A replacement for std::next. 1233 template <typename IterT> 1234 inline IterT next_nodbg(IterT It, IterT End, bool SkipPseudoOp = true) { 1235 return skipDebugInstructionsForward(std::next(It), End, SkipPseudoOp); 1236 } 1237 1238 /// Decrement \p It, then continue decrementing it while it points to a debug 1239 /// instruction. A replacement for std::prev. 1240 template <typename IterT> 1241 inline IterT prev_nodbg(IterT It, IterT Begin, bool SkipPseudoOp = true) { 1242 return skipDebugInstructionsBackward(std::prev(It), Begin, SkipPseudoOp); 1243 } 1244 1245 /// Construct a range iterator which begins at \p It and moves forwards until 1246 /// \p End is reached, skipping any debug instructions. 1247 template <typename IterT> 1248 inline auto instructionsWithoutDebug(IterT It, IterT End, 1249 bool SkipPseudoOp = true) { 1250 return make_filter_range(make_range(It, End), [=](const MachineInstr &MI) { 1251 return !MI.isDebugInstr() && !(SkipPseudoOp && MI.isPseudoProbe()); 1252 }); 1253 } 1254 1255 } // end namespace llvm 1256 1257 #endif // LLVM_CODEGEN_MACHINEBASICBLOCK_H 1258