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