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