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