LLVM  4.0.0
MachineBasicBlock.h
Go to the documentation of this file.
1 //===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Collect the sequence of machine instructions for a basic block.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H
15 #define LLVM_CODEGEN_MACHINEBASICBLOCK_H
16 
17 #include "llvm/ADT/GraphTraits.h"
22 #include "llvm/MC/LaneBitmask.h"
23 #include "llvm/MC/MCRegisterInfo.h"
24 #include "llvm/Support/DataTypes.h"
25 #include <functional>
26 
27 namespace llvm {
28 
29 class Pass;
30 class BasicBlock;
31 class MachineFunction;
32 class MCSymbol;
33 class MIPrinter;
34 class SlotIndexes;
35 class StringRef;
36 class raw_ostream;
37 class MachineBranchProbabilityInfo;
38 
39 template <> struct ilist_traits<MachineInstr> {
40 private:
41  friend class MachineBasicBlock; // Set by the owning MachineBasicBlock.
42  MachineBasicBlock *Parent;
43 
46 
47 public:
51  instr_iterator Last);
52 
53  void deleteNode(MachineInstr *MI);
54 };
55 
57  : public ilist_node_with_parent<MachineBasicBlock, MachineFunction> {
58 public:
59  /// Pair of physical register and lane mask.
60  /// This is not simply a std::pair typedef because the members should be named
61  /// clearly as they both have an integer type.
63  public:
66 
68  : PhysReg(PhysReg), LaneMask(LaneMask) {}
69  };
70 
71 private:
73  Instructions Insts;
74  const BasicBlock *BB;
75  int Number;
76  MachineFunction *xParent;
77 
78  /// Keep track of the predecessor / successor basic blocks.
79  std::vector<MachineBasicBlock *> Predecessors;
80  std::vector<MachineBasicBlock *> Successors;
81 
82  /// Keep track of the probabilities to the successors. This vector has the
83  /// same order as Successors, or it is empty if we don't use it (disable
84  /// optimization).
85  std::vector<BranchProbability> Probs;
86  typedef std::vector<BranchProbability>::iterator probability_iterator;
87  typedef std::vector<BranchProbability>::const_iterator
88  const_probability_iterator;
89 
90  /// Keep track of the physical registers that are livein of the basicblock.
91  typedef std::vector<RegisterMaskPair> LiveInVector;
92  LiveInVector LiveIns;
93 
94  /// Alignment of the basic block. Zero if the basic block does not need to be
95  /// aligned. The alignment is specified as log2(bytes).
96  unsigned Alignment = 0;
97 
98  /// Indicate that this basic block is entered via an exception handler.
99  bool IsEHPad = false;
100 
101  /// Indicate that this basic block is potentially the target of an indirect
102  /// branch.
103  bool AddressTaken = false;
104 
105  /// Indicate that this basic block is the entry block of an EH funclet.
106  bool IsEHFuncletEntry = false;
107 
108  /// Indicate that this basic block is the entry block of a cleanup funclet.
109  bool IsCleanupFuncletEntry = false;
110 
111  /// \brief since getSymbol is a relatively heavy-weight operation, the symbol
112  /// is only computed once and is cached.
113  mutable MCSymbol *CachedMCSymbol = nullptr;
114 
115  // Intrusive list support
116  MachineBasicBlock() {}
117 
118  explicit MachineBasicBlock(MachineFunction &MF, const BasicBlock *BB);
119 
120  ~MachineBasicBlock();
121 
122  // MachineBasicBlocks are allocated and owned by MachineFunction.
123  friend class MachineFunction;
124 
125 public:
126  /// Return the LLVM basic block that this instance corresponded to originally.
127  /// Note that this may be NULL if this instance does not correspond directly
128  /// to an LLVM basic block.
129  const BasicBlock *getBasicBlock() const { return BB; }
130 
131  /// Return the name of the corresponding LLVM basic block, or "(null)".
132  StringRef getName() const;
133 
134  /// Return a formatted string to identify this block and its parent function.
135  std::string getFullName() const;
136 
137  /// Test whether this block is potentially the target of an indirect branch.
138  bool hasAddressTaken() const { return AddressTaken; }
139 
140  /// Set this block to reflect that it potentially is the target of an indirect
141  /// branch.
142  void setHasAddressTaken() { AddressTaken = true; }
143 
144  /// Return the MachineFunction containing this basic block.
145  const MachineFunction *getParent() const { return xParent; }
146  MachineFunction *getParent() { return xParent; }
147 
152 
158 
159  unsigned size() const { return (unsigned)Insts.size(); }
160  bool empty() const { return Insts.empty(); }
161 
162  MachineInstr &instr_front() { return Insts.front(); }
163  MachineInstr &instr_back() { return Insts.back(); }
164  const MachineInstr &instr_front() const { return Insts.front(); }
165  const MachineInstr &instr_back() const { return Insts.back(); }
166 
167  MachineInstr &front() { return Insts.front(); }
168  MachineInstr &back() { return *--end(); }
169  const MachineInstr &front() const { return Insts.front(); }
170  const MachineInstr &back() const { return *--end(); }
171 
172  instr_iterator instr_begin() { return Insts.begin(); }
173  const_instr_iterator instr_begin() const { return Insts.begin(); }
174  instr_iterator instr_end() { return Insts.end(); }
175  const_instr_iterator instr_end() const { return Insts.end(); }
176  reverse_instr_iterator instr_rbegin() { return Insts.rbegin(); }
177  const_reverse_instr_iterator instr_rbegin() const { return Insts.rbegin(); }
178  reverse_instr_iterator instr_rend () { return Insts.rend(); }
179  const_reverse_instr_iterator instr_rend () const { return Insts.rend(); }
180 
186  }
187 
188  iterator begin() { return instr_begin(); }
189  const_iterator begin() const { return instr_begin(); }
190  iterator end () { return instr_end(); }
191  const_iterator end () const { return instr_end(); }
194  }
197  }
201  }
202 
203  /// Support for MachineInstr::getNextNode().
205  return &MachineBasicBlock::Insts;
206  }
207 
209  return make_range(getFirstTerminator(), end());
210  }
212  return make_range(getFirstTerminator(), end());
213  }
214 
215  // Machine-CFG iterators
216  typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
217  typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
218  typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
219  typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
220  typedef std::vector<MachineBasicBlock *>::reverse_iterator
222  typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
224  typedef std::vector<MachineBasicBlock *>::reverse_iterator
226  typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
228  pred_iterator pred_begin() { return Predecessors.begin(); }
229  const_pred_iterator pred_begin() const { return Predecessors.begin(); }
230  pred_iterator pred_end() { return Predecessors.end(); }
231  const_pred_iterator pred_end() const { return Predecessors.end(); }
233  { return Predecessors.rbegin();}
235  { return Predecessors.rbegin();}
237  { return Predecessors.rend(); }
239  { return Predecessors.rend(); }
240  unsigned pred_size() const {
241  return (unsigned)Predecessors.size();
242  }
243  bool pred_empty() const { return Predecessors.empty(); }
244  succ_iterator succ_begin() { return Successors.begin(); }
245  const_succ_iterator succ_begin() const { return Successors.begin(); }
246  succ_iterator succ_end() { return Successors.end(); }
247  const_succ_iterator succ_end() const { return Successors.end(); }
249  { return Successors.rbegin(); }
251  { return Successors.rbegin(); }
253  { return Successors.rend(); }
255  { return Successors.rend(); }
256  unsigned succ_size() const {
257  return (unsigned)Successors.size();
258  }
259  bool succ_empty() const { return Successors.empty(); }
260 
262  return make_range(pred_begin(), pred_end());
263  }
265  return make_range(pred_begin(), pred_end());
266  }
268  return make_range(succ_begin(), succ_end());
269  }
271  return make_range(succ_begin(), succ_end());
272  }
273 
274  // LiveIn management methods.
275 
276  /// Adds the specified register as a live in. Note that it is an error to add
277  /// the same register to the same set more than once unless the intention is
278  /// to call sortUniqueLiveIns after all registers are added.
279  void addLiveIn(MCPhysReg PhysReg,
280  LaneBitmask LaneMask = LaneBitmask::getAll()) {
281  LiveIns.push_back(RegisterMaskPair(PhysReg, LaneMask));
282  }
283  void addLiveIn(const RegisterMaskPair &RegMaskPair) {
284  LiveIns.push_back(RegMaskPair);
285  }
286 
287  /// Sorts and uniques the LiveIns vector. It can be significantly faster to do
288  /// this than repeatedly calling isLiveIn before calling addLiveIn for every
289  /// LiveIn insertion.
290  void sortUniqueLiveIns();
291 
292  /// Clear live in list.
293  void clearLiveIns();
294 
295  /// Add PhysReg as live in to this block, and ensure that there is a copy of
296  /// PhysReg to a virtual register of class RC. Return the virtual register
297  /// that is a copy of the live in PhysReg.
298  unsigned addLiveIn(MCPhysReg PhysReg, const TargetRegisterClass *RC);
299 
300  /// Remove the specified register from the live in set.
302  LaneBitmask LaneMask = LaneBitmask::getAll());
303 
304  /// Return true if the specified register is in the live in set.
305  bool isLiveIn(MCPhysReg Reg,
306  LaneBitmask LaneMask = LaneBitmask::getAll()) const;
307 
308  // Iteration support for live in sets. These sets are kept in sorted
309  // order by their register number.
310  typedef LiveInVector::const_iterator livein_iterator;
311 #ifndef NDEBUG
312  /// Unlike livein_begin, this method does not check that the liveness
313  /// information is accurate. Still for debug purposes it may be useful
314  /// to have iterators that won't assert if the liveness information
315  /// is not current.
316  livein_iterator livein_begin_dbg() const { return LiveIns.begin(); }
319  }
320 #endif
322  livein_iterator livein_end() const { return LiveIns.end(); }
323  bool livein_empty() const { return LiveIns.empty(); }
325  return make_range(livein_begin(), livein_end());
326  }
327 
328  /// Get the clobber mask for the start of this basic block. Funclets use this
329  /// to prevent register allocation across funclet transitions.
330  const uint32_t *getBeginClobberMask(const TargetRegisterInfo *TRI) const;
331 
332  /// Get the clobber mask for the end of the basic block.
333  /// \see getBeginClobberMask()
334  const uint32_t *getEndClobberMask(const TargetRegisterInfo *TRI) const;
335 
336  /// Return alignment of the basic block. The alignment is specified as
337  /// log2(bytes).
338  unsigned getAlignment() const { return Alignment; }
339 
340  /// Set alignment of the basic block. The alignment is specified as
341  /// log2(bytes).
342  void setAlignment(unsigned Align) { Alignment = Align; }
343 
344  /// Returns true if the block is a landing pad. That is this basic block is
345  /// entered via an exception handler.
346  bool isEHPad() const { return IsEHPad; }
347 
348  /// Indicates the block is a landing pad. That is this basic block is entered
349  /// via an exception handler.
350  void setIsEHPad(bool V = true) { IsEHPad = V; }
351 
352  bool hasEHPadSuccessor() const;
353 
354  /// Returns true if this is the entry block of an EH funclet.
355  bool isEHFuncletEntry() const { return IsEHFuncletEntry; }
356 
357  /// Indicates if this is the entry block of an EH funclet.
358  void setIsEHFuncletEntry(bool V = true) { IsEHFuncletEntry = V; }
359 
360  /// Returns true if this is the entry block of a cleanup funclet.
361  bool isCleanupFuncletEntry() const { return IsCleanupFuncletEntry; }
362 
363  /// Indicates if this is the entry block of a cleanup funclet.
364  void setIsCleanupFuncletEntry(bool V = true) { IsCleanupFuncletEntry = V; }
365 
366  // Code Layout methods.
367 
368  /// Move 'this' block before or after the specified block. This only moves
369  /// the block, it does not modify the CFG or adjust potential fall-throughs at
370  /// the end of the block.
371  void moveBefore(MachineBasicBlock *NewAfter);
372  void moveAfter(MachineBasicBlock *NewBefore);
373 
374  /// Update the terminator instructions in block to account for changes to the
375  /// layout. If the block previously used a fallthrough, it may now need a
376  /// branch, and if it previously used branching it may now be able to use a
377  /// fallthrough.
378  void updateTerminator();
379 
380  // Machine-CFG mutators
381 
382  /// Add Succ as a successor of this MachineBasicBlock. The Predecessors list
383  /// of Succ is automatically updated. PROB parameter is stored in
384  /// Probabilities list. The default probability is set as unknown. Mixing
385  /// known and unknown probabilities in successor list is not allowed. When all
386  /// successors have unknown probabilities, 1 / N is returned as the
387  /// probability for each successor, where N is the number of successors.
388  ///
389  /// Note that duplicate Machine CFG edges are not allowed.
390  void addSuccessor(MachineBasicBlock *Succ,
392 
393  /// Add Succ as a successor of this MachineBasicBlock. The Predecessors list
394  /// of Succ is automatically updated. The probability is not provided because
395  /// BPI is not available (e.g. -O0 is used), in which case edge probabilities
396  /// won't be used. Using this interface can save some space.
398 
399  /// Set successor probability of a given iterator.
401 
402  /// Normalize probabilities of all successors so that the sum of them becomes
403  /// one. This is usually done when the current update on this MBB is done, and
404  /// the sum of its successors' probabilities is not guaranteed to be one. The
405  /// user is responsible for the correct use of this function.
406  /// MBB::removeSuccessor() has an option to do this automatically.
408  BranchProbability::normalizeProbabilities(Probs.begin(), Probs.end());
409  }
410 
411  /// Validate successors' probabilities and check if the sum of them is
412  /// approximate one. This only works in DEBUG mode.
413  void validateSuccProbs() const;
414 
415  /// Remove successor from the successors list of this MachineBasicBlock. The
416  /// Predecessors list of Succ is automatically updated.
417  /// If NormalizeSuccProbs is true, then normalize successors' probabilities
418  /// after the successor is removed.
420  bool NormalizeSuccProbs = false);
421 
422  /// Remove specified successor from the successors list of this
423  /// MachineBasicBlock. The Predecessors list of Succ is automatically updated.
424  /// If NormalizeSuccProbs is true, then normalize successors' probabilities
425  /// after the successor is removed.
426  /// Return the iterator to the element after the one removed.
428  bool NormalizeSuccProbs = false);
429 
430  /// Replace successor OLD with NEW and update probability info.
432 
433  /// Transfers all the successors from MBB to this machine basic block (i.e.,
434  /// copies all the successors FromMBB and remove all the successors from
435  /// FromMBB).
436  void transferSuccessors(MachineBasicBlock *FromMBB);
437 
438  /// Transfers all the successors, as in transferSuccessors, and update PHI
439  /// operands in the successor blocks which refer to FromMBB to refer to this.
441 
442  /// Return true if any of the successors have probabilities attached to them.
443  bool hasSuccessorProbabilities() const { return !Probs.empty(); }
444 
445  /// Return true if the specified MBB is a predecessor of this block.
446  bool isPredecessor(const MachineBasicBlock *MBB) const;
447 
448  /// Return true if the specified MBB is a successor of this block.
449  bool isSuccessor(const MachineBasicBlock *MBB) const;
450 
451  /// Return true if the specified MBB will be emitted immediately after this
452  /// block, such that if this block exits by falling through, control will
453  /// transfer to the specified MBB. Note that MBB need not be a successor at
454  /// all, for example if this block ends with an unconditional branch to some
455  /// other block.
456  bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
457 
458  /// Return true if the block can implicitly transfer control to the block
459  /// after it by falling off the end of it. This should return false if it can
460  /// reach the block after it, but it uses an explicit branch to do so (e.g., a
461  /// table jump). True is a conservative answer.
462  bool canFallThrough();
463 
464  /// Returns a pointer to the first instruction in this block that is not a
465  /// PHINode instruction. When adding instructions to the beginning of the
466  /// basic block, they should be added before the returned value, not before
467  /// the first instruction, which might be PHI.
468  /// Returns end() is there's no non-PHI instruction.
470 
471  /// Return the first instruction in MBB after I that is not a PHI or a label.
472  /// This is the correct point to insert lowered copies at the beginning of a
473  /// basic block that must be before any debugging information.
475 
476  /// Return the first instruction in MBB after I that is not a PHI, label or
477  /// debug. This is the correct point to insert copies at the beginning of a
478  /// basic block.
480 
481  /// Returns an iterator to the first terminator instruction of this basic
482  /// block. If a terminator does not exist, it returns end().
485  return const_cast<MachineBasicBlock *>(this)->getFirstTerminator();
486  }
487 
488  /// Same getFirstTerminator but it ignores bundles and return an
489  /// instr_iterator instead.
491 
492  /// Returns an iterator to the first non-debug instruction in the basic block,
493  /// or end().
496  return const_cast<MachineBasicBlock *>(this)->getFirstNonDebugInstr();
497  }
498 
499  /// Returns an iterator to the last non-debug instruction in the basic block,
500  /// or end().
503  return const_cast<MachineBasicBlock *>(this)->getLastNonDebugInstr();
504  }
505 
506  /// Convenience function that returns true if the block ends in a return
507  /// instruction.
508  bool isReturnBlock() const {
509  return !empty() && back().isReturn();
510  }
511 
512  /// Split the critical edge from this block to the given successor block, and
513  /// return the newly created block, or null if splitting is not possible.
514  ///
515  /// This function updates LiveVariables, MachineDominatorTree, and
516  /// MachineLoopInfo, as applicable.
518 
519  /// Check if the edge between this block and the given successor \p
520  /// Succ, can be split. If this returns true a subsequent call to
521  /// SplitCriticalEdge is guaranteed to return a valid basic block if
522  /// no changes occured in the meantime.
523  bool canSplitCriticalEdge(const MachineBasicBlock *Succ) const;
524 
525  void pop_front() { Insts.pop_front(); }
526  void pop_back() { Insts.pop_back(); }
527  void push_back(MachineInstr *MI) { Insts.push_back(MI); }
528 
529  /// Insert MI into the instruction list before I, possibly inside a bundle.
530  ///
531  /// If the insertion point is inside a bundle, MI will be added to the bundle,
532  /// otherwise MI will not be added to any bundle. That means this function
533  /// alone can't be used to prepend or append instructions to bundles. See
534  /// MIBundleBuilder::insert() for a more reliable way of doing that.
536 
537  /// Insert a range of instructions into the instruction list before I.
538  template<typename IT>
539  void insert(iterator I, IT S, IT E) {
540  assert((I == end() || I->getParent() == this) &&
541  "iterator points outside of basic block");
542  Insts.insert(I.getInstrIterator(), S, E);
543  }
544 
545  /// Insert MI into the instruction list before I.
547  assert((I == end() || I->getParent() == this) &&
548  "iterator points outside of basic block");
549  assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
550  "Cannot insert instruction with bundle flags");
551  return Insts.insert(I.getInstrIterator(), MI);
552  }
553 
554  /// Insert MI into the instruction list after I.
556  assert((I == end() || I->getParent() == this) &&
557  "iterator points outside of basic block");
558  assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
559  "Cannot insert instruction with bundle flags");
560  return Insts.insertAfter(I.getInstrIterator(), MI);
561  }
562 
563  /// Remove an instruction from the instruction list and delete it.
564  ///
565  /// If the instruction is part of a bundle, the other instructions in the
566  /// bundle will still be bundled after removing the single instruction.
568 
569  /// Remove an instruction from the instruction list and delete it.
570  ///
571  /// If the instruction is part of a bundle, the other instructions in the
572  /// bundle will still be bundled after removing the single instruction.
574  return erase(instr_iterator(I));
575  }
576 
577  /// Remove a range of instructions from the instruction list and delete them.
579  return Insts.erase(I.getInstrIterator(), E.getInstrIterator());
580  }
581 
582  /// Remove an instruction or bundle from the instruction list and delete it.
583  ///
584  /// If I points to a bundle of instructions, they are all erased.
586  return erase(I, std::next(I));
587  }
588 
589  /// Remove an instruction from the instruction list and delete it.
590  ///
591  /// If I is the head of a bundle of instructions, the whole bundle will be
592  /// erased.
594  return erase(iterator(I));
595  }
596 
597  /// Remove the unbundled instruction from the instruction list without
598  /// deleting it.
599  ///
600  /// This function can not be used to remove bundled instructions, use
601  /// remove_instr to remove individual instructions from a bundle.
603  assert(!I->isBundled() && "Cannot remove bundled instructions");
604  return Insts.remove(instr_iterator(I));
605  }
606 
607  /// Remove the possibly bundled instruction from the instruction list
608  /// without deleting it.
609  ///
610  /// If the instruction is part of a bundle, the other instructions in the
611  /// bundle will still be bundled after removing the single instruction.
613 
614  void clear() {
615  Insts.clear();
616  }
617 
618  /// Take an instruction from MBB 'Other' at the position From, and insert it
619  /// into this MBB right before 'Where'.
620  ///
621  /// If From points to a bundle of instructions, the whole bundle is moved.
623  // The range splice() doesn't allow noop moves, but this one does.
624  if (Where != From)
625  splice(Where, Other, From, std::next(From));
626  }
627 
628  /// Take a block of instructions from MBB 'Other' in the range [From, To),
629  /// and insert them into this MBB right before 'Where'.
630  ///
631  /// The instruction at 'Where' must not be included in the range of
632  /// instructions to move.
634  iterator From, iterator To) {
635  Insts.splice(Where.getInstrIterator(), Other->Insts,
636  From.getInstrIterator(), To.getInstrIterator());
637  }
638 
639  /// This method unlinks 'this' from the containing function, and returns it,
640  /// but does not delete it.
642 
643  /// This method unlinks 'this' from the containing function and deletes it.
644  void eraseFromParent();
645 
646  /// Given a machine basic block that branched to 'Old', change the code and
647  /// CFG so that it branches to 'New' instead.
649 
650  /// Various pieces of code can cause excess edges in the CFG to be inserted.
651  /// If we have proven that MBB can only branch to DestA and DestB, remove any
652  /// other MBB successors from the CFG. DestA and DestB can be null. Besides
653  /// DestA and DestB, retain other edges leading to LandingPads (currently
654  /// there can be only one; we don't check or require that here). Note it is
655  /// possible that DestA and/or DestB are LandingPads.
657  MachineBasicBlock *DestB,
658  bool IsCond);
659 
660  /// Find the next valid DebugLoc starting at MBBI, skipping any DBG_VALUE
661  /// instructions. Return UnknownLoc if there is none.
664  return findDebugLoc(MBBI.getInstrIterator());
665  }
666 
667  /// Possible outcome of a register liveness query to computeRegisterLiveness()
669  LQR_Live, ///< Register is known to be (at least partially) live.
670  LQR_Dead, ///< Register is known to be fully dead.
671  LQR_Unknown ///< Register liveness not decidable from local neighborhood.
672  };
673 
674  /// Return whether (physical) register \p Reg has been <def>ined and not
675  /// <kill>ed as of just before \p Before.
676  ///
677  /// Search is localised to a neighborhood of \p Neighborhood instructions
678  /// before (searching for defs or kills) and \p Neighborhood instructions
679  /// after (searching just for defs) \p Before.
680  ///
681  /// \p Reg must be a physical register.
683  unsigned Reg,
684  const_iterator Before,
685  unsigned Neighborhood=10) const;
686 
687  // Debugging methods.
688  void dump() const;
689  void print(raw_ostream &OS, const SlotIndexes* = nullptr) const;
690  void print(raw_ostream &OS, ModuleSlotTracker &MST,
691  const SlotIndexes* = nullptr) const;
692 
693  // Printing method used by LoopInfo.
694  void printAsOperand(raw_ostream &OS, bool PrintType = true) const;
695 
696  /// MachineBasicBlocks are uniquely numbered at the function level, unless
697  /// they're not in a MachineFunction yet, in which case this will return -1.
698  int getNumber() const { return Number; }
699  void setNumber(int N) { Number = N; }
700 
701  /// Return the MCSymbol for this basic block.
702  MCSymbol *getSymbol() const;
703 
704 
705 private:
706  /// Return probability iterator corresponding to the I successor iterator.
707  probability_iterator getProbabilityIterator(succ_iterator I);
708  const_probability_iterator
709  getProbabilityIterator(const_succ_iterator I) const;
710 
712  friend class MIPrinter;
713 
714  /// Return probability of the edge from this block to MBB. This method should
715  /// NOT be called directly, but by using getEdgeProbability method from
716  /// MachineBranchProbabilityInfo class.
717  BranchProbability getSuccProbability(const_succ_iterator Succ) const;
718 
719  // Methods used to maintain doubly linked list of blocks...
721 
722  // Machine-CFG mutators
723 
724  /// Remove Pred as a predecessor of this MachineBasicBlock. Don't do this
725  /// unless you know what you're doing, because it doesn't update Pred's
726  /// successors list. Use Pred->addSuccessor instead.
727  void addPredecessor(MachineBasicBlock *Pred);
728 
729  /// Remove Pred as a predecessor of this MachineBasicBlock. Don't do this
730  /// unless you know what you're doing, because it doesn't update Pred's
731  /// successors list. Use Pred->removeSuccessor instead.
732  void removePredecessor(MachineBasicBlock *Pred);
733 };
734 
736 
737 // This is useful when building IndexedMaps keyed on basic block pointers.
739  public std::unary_function<const MachineBasicBlock*, unsigned> {
740  unsigned operator()(const MachineBasicBlock *MBB) const {
741  return MBB->getNumber();
742  }
743 };
744 
745 //===--------------------------------------------------------------------===//
746 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
747 //===--------------------------------------------------------------------===//
748 
749 // Provide specializations of GraphTraits to be able to treat a
750 // MachineFunction as a graph of MachineBasicBlocks.
751 //
752 
753 template <> struct GraphTraits<MachineBasicBlock *> {
756 
757  static NodeRef getEntryNode(MachineBasicBlock *BB) { return BB; }
758  static ChildIteratorType child_begin(NodeRef N) { return N->succ_begin(); }
759  static ChildIteratorType child_end(NodeRef N) { return N->succ_end(); }
760 };
761 
762 template <> struct GraphTraits<const MachineBasicBlock *> {
763  typedef const MachineBasicBlock *NodeRef;
765 
766  static NodeRef getEntryNode(const MachineBasicBlock *BB) { return BB; }
767  static ChildIteratorType child_begin(NodeRef N) { return N->succ_begin(); }
768  static ChildIteratorType child_end(NodeRef N) { return N->succ_end(); }
769 };
770 
771 // Provide specializations of GraphTraits to be able to treat a
772 // MachineFunction as a graph of MachineBasicBlocks and to walk it
773 // in inverse order. Inverse order for a function is considered
774 // to be when traversing the predecessor edges of a MBB
775 // instead of the successor edges.
776 //
777 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
781  return G.Graph;
782  }
783  static ChildIteratorType child_begin(NodeRef N) { return N->pred_begin(); }
784  static ChildIteratorType child_end(NodeRef N) { return N->pred_end(); }
785 };
786 
787 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
788  typedef const MachineBasicBlock *NodeRef;
791  return G.Graph;
792  }
793  static ChildIteratorType child_begin(NodeRef N) { return N->pred_begin(); }
794  static ChildIteratorType child_end(NodeRef N) { return N->pred_end(); }
795 };
796 
797 
798 
799 /// MachineInstrSpan provides an interface to get an iteration range
800 /// containing the instruction it was initialized with, along with all
801 /// those instructions inserted prior to or following that instruction
802 /// at some point after the MachineInstrSpan is constructed.
804  MachineBasicBlock &MBB;
806 public:
808  : MBB(*I->getParent()),
809  I(I),
810  B(I == MBB.begin() ? MBB.end() : std::prev(I)),
811  E(std::next(I)) {}
812 
814  return B == MBB.end() ? MBB.begin() : std::next(B);
815  }
817  bool empty() { return begin() == end(); }
818 
820 };
821 
822 /// Increment \p It until it points to a non-debug instruction or to \p End
823 /// and return the resulting iterator. This function should only be used
824 /// MachineBasicBlock::{iterator, const_iterator, instr_iterator,
825 /// const_instr_iterator} and the respective reverse iterators.
826 template<typename IterT>
827 inline IterT skipDebugInstructionsForward(IterT It, IterT End) {
828  while (It != End && It->isDebugValue())
829  It++;
830  return It;
831 }
832 
833 /// Decrement \p It until it points to a non-debug instruction or to \p Begin
834 /// and return the resulting iterator. This function should only be used
835 /// MachineBasicBlock::{iterator, const_iterator, instr_iterator,
836 /// const_instr_iterator} and the respective reverse iterators.
837 template<class IterT>
838 inline IterT skipDebugInstructionsBackward(IterT It, IterT Begin) {
839  while (It != Begin && It->isDebugValue())
840  It--;
841  return It;
842 }
843 
844 } // End llvm namespace
845 
846 #endif
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:81
pred_reverse_iterator pred_rbegin()
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::ZeroOrMore, cl::values(clEnumValN(DefaultIT,"arm-default-it","Generate IT block based on arch"), clEnumValN(RestrictedIT,"arm-restrict-it","Disallow deprecated IT based on ARMv8"), clEnumValN(NoRestrictedIT,"arm-no-restrict-it","Allow IT blocks based on ARMv7")))
unsigned succ_size() const
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
bool isEHPad() const
Returns true if the block is a landing pad.
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
const_iterator getLastNonDebugInstr() const
A common definition of LaneBitmask for use in TableGen and CodeGen.
pred_reverse_iterator pred_rend()
const_instr_iterator instr_begin() const
instr_iterator instr_begin()
iterator_range< livein_iterator > liveins() const
instr_iterator instr_end()
iterator erase(iterator where)
Definition: ilist.h:280
static NodeRef getEntryNode(Inverse< const MachineBasicBlock * > G)
Various leaf nodes.
Definition: ISDOpcodes.h:60
void addNodeToList(NodeTy *)
When an MBB is added to an MF, we need to update the parent pointer of the MBB, the MBB numbering...
Definition: ilist.h:66
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
RegisterMaskPair(MCPhysReg PhysReg, LaneBitmask LaneMask)
void setIsEHPad(bool V=true)
Indicates the block is a landing pad.
MachineInstr & instr_front()
Instructions::const_reverse_iterator const_reverse_instr_iterator
aarch64 AArch64 CCMP Pass
Instructions::const_iterator const_instr_iterator
const_succ_iterator succ_begin() const
static ChildIteratorType child_begin(NodeRef N)
instr_iterator getFirstInstrTerminator()
Same getFirstTerminator but it ignores bundles and return an instr_iterator instead.
static LaneBitmask getAll()
Definition: LaneBitmask.h:75
iterator getFirstNonDebugInstr()
Returns an iterator to the first non-debug instruction in the basic block, or end().
MachineBasicBlock::pred_iterator ChildIteratorType
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
MachineBasicBlock::succ_iterator ChildIteratorType
void print(raw_ostream &OS, const SlotIndexes *=nullptr) const
static NodeRef getEntryNode(Inverse< MachineBasicBlock * > G)
void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
MachineInstr & instr_back()
bool isCleanupFuncletEntry() const
Returns true if this is the entry block of a cleanup funclet.
void transferSuccessors(MachineBasicBlock *FromMBB)
Transfers all the successors from MBB to this machine basic block (i.e., copies all the successors Fr...
iterator_range< const_iterator > terminators() const
iterator_range< livein_iterator > liveins_dbg() const
iterator insertAfter(iterator I, MachineInstr *MI)
Insert MI into the instruction list after I.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Template traits for intrusive list.
Definition: ilist.h:101
void addSuccessorWithoutProb(MachineBasicBlock *Succ)
Add Succ as a successor of this MachineBasicBlock.
DebugLoc findDebugLoc(iterator MBBI)
MachineInstrBundleIterator< MachineInstr > iterator
void moveAfter(MachineBasicBlock *NewBefore)
A debug info location.
Definition: DebugLoc.h:34
Manage lifetime of a slot tracker for printing IR.
iterator_range< const_succ_iterator > successors() const
Instructions::iterator instr_iterator
MachineInstrSpan(MachineBasicBlock::iterator I)
iterator insert(iterator I, MachineInstr *MI)
Insert MI into the instruction list before I.
MachineBasicBlock * removeFromParent()
This method unlinks 'this' from the containing function, and returns it, but does not delete it...
void setAlignment(unsigned Align)
Set alignment of the basic block.
void removeLiveIn(MCPhysReg Reg, LaneBitmask LaneMask=LaneBitmask::getAll())
Remove the specified register from the live in set.
static ChildIteratorType child_begin(NodeRef N)
iterator_range< succ_iterator > successors()
void splice(iterator Where, MachineBasicBlock *Other, iterator From, iterator To)
Take a block of instructions from MBB 'Other' in the range [From, To), and insert them into this MBB ...
MachineInstrSpan provides an interface to get an iteration range containing the instruction it was in...
std::vector< MachineBasicBlock * >::const_reverse_iterator const_pred_reverse_iterator
void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New)
Given a machine basic block that branched to 'Old', change the code and CFG so that it branches to 'N...
void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
const_iterator getFirstNonDebugInstr() const
iterator erase(MachineInstr *I)
Remove an instruction from the instruction list and delete it.
void insert(iterator I, IT S, IT E)
Insert a range of instructions into the instruction list before I.
std::vector< MachineBasicBlock * >::iterator succ_iterator
Reg
All possible values of the reg field in the ModR/M byte.
iterator_range< iterator > terminators()
MachineInstr * remove_instr(MachineInstr *I)
Remove the possibly bundled instruction from the instruction list without deleting it...
const MachineInstr & instr_front() const
void setIsCleanupFuncletEntry(bool V=true)
Indicates if this is the entry block of a cleanup funclet.
MachineBasicBlock::iterator end()
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:662
A simple intrusive list implementation.
Definition: simple_ilist.h:74
static ChildIteratorType child_end(NodeRef N)
bool isBundledWithSucc() const
Return true if this instruction is part of a bundle, and it is not the last instruction in the bundle...
Definition: MachineInstr.h:233
SlotIndexes pass.
Definition: SlotIndexes.h:323
const_succ_iterator succ_end() const
const MachineInstr & front() const
static void normalizeProbabilities(ProbabilityIter Begin, ProbabilityIter End)
iterator_range< const_instr_iterator > const_instr_range
MachineBasicBlock * MBB
bool canFallThrough()
Return true if the block can implicitly transfer control to the block after it by falling off the end...
static Instructions MachineBasicBlock::* getSublistAccess(MachineInstr *)
Support for MachineInstr::getNextNode().
const uint32_t * getBeginClobberMask(const TargetRegisterInfo *TRI) const
Get the clobber mask for the start of this basic block.
iterator getLastNonDebugInstr()
Returns an iterator to the last non-debug instruction in the basic block, or end().
std::vector< MachineBasicBlock * >::iterator pred_iterator
void printAsOperand(raw_ostream &OS, bool PrintType=true) const
void setSuccProbability(succ_iterator I, BranchProbability Prob)
Set successor probability of a given iterator.
succ_reverse_iterator succ_rend()
reverse_iterator rend()
LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI, unsigned Reg, const_iterator Before, unsigned Neighborhood=10) const
Return whether (physical) register Reg has been <def>ined and not <kill>ed as of just before Before...
const_pred_iterator pred_end() const
reverse_iterator rbegin()
void normalizeSuccProbs()
Normalize probabilities of all successors so that the sum of them becomes one.
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
const_reverse_instr_iterator instr_rend() const
#define P(N)
An ilist node that can access its parent list.
Definition: ilist_node.h:243
bool isReturn(QueryType Type=AnyInBundle) const
Definition: MachineInstr.h:420
void addLiveIn(MCPhysReg PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
iterator SkipPHIsAndLabels(iterator I)
Return the first instruction in MBB after I that is not a PHI or a label.
instr_iterator erase_instr(MachineInstr *I)
Remove an instruction from the instruction list and delete it.
livein_iterator livein_begin() const
LLVM Basic Block Representation.
Definition: BasicBlock.h:51
static ChildIteratorType child_end(NodeRef N)
succ_reverse_iterator succ_rbegin()
static ChildIteratorType child_begin(NodeRef N)
const_iterator begin() const
livein_iterator livein_begin_dbg() const
Unlike livein_begin, this method does not check that the liveness information is accurate.
livein_iterator livein_end() const
Register is known to be fully dead.
void pop_front()
Definition: ilist.h:327
void splice(iterator where, iplist_impl &L2)
Definition: ilist.h:342
void clearLiveIns()
Clear live in list.
bool isEHFuncletEntry() const
Returns true if this is the entry block of an EH funclet.
static const unsigned End
static MachineInstrBundleIterator getAtBundleBegin(instr_iterator MI)
Get the bundle iterator for the given instruction's bundle.
This class prints out the machine instructions using the MIR serialization format.
Definition: MIRPrinter.cpp:102
const_reverse_instr_iterator instr_rbegin() const
bool isBundledWithPred() const
Return true if this instruction is part of a bundle, and it is not the first instruction in the bundl...
Definition: MachineInstr.h:229
iterator_range< pred_iterator > predecessors()
LivenessQueryResult
Possible outcome of a register liveness query to computeRegisterLiveness()
Register is known to be (at least partially) live.
const_iterator getFirstTerminator() const
const MachineInstr & instr_back() const
void moveBefore(MachineBasicBlock *NewAfter)
Move 'this' block before or after the specified block.
iterator erase(iterator I)
Remove an instruction or bundle from the instruction list and delete it.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
iterator erase(iterator I, iterator E)
Remove a range of instructions from the instruction list and delete them.
An intrusive list with ownership and callbacks specified/controlled by ilist_traits, only with API safe for polymorphic types.
Definition: ilist.h:403
bool isReturnBlock() const
Convenience function that returns true if the block ends in a return instruction. ...
std::vector< MachineBasicBlock * >::const_iterator const_pred_iterator
const_instr_range instrs() const
DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any DBG_VALUE instructions.
bool canSplitCriticalEdge(const MachineBasicBlock *Succ) const
Check if the edge between this block and the given successor Succ, can be split.
std::string getFullName() const
Return a formatted string to identify this block and its parent function.
static BranchProbability getUnknown()
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Iterator for intrusive lists based on ilist_node.
const_iterator end() const
const GraphType & Graph
Definition: GraphTraits.h:80
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
reverse_instr_iterator instr_rbegin()
void removeNodeFromList(NodeTy *)
Definition: ilist.h:67
const_succ_reverse_iterator succ_rbegin() const
std::vector< MachineBasicBlock * >::const_reverse_iterator const_succ_reverse_iterator
const_succ_reverse_iterator succ_rend() const
void updateTerminator()
Update the terminator instructions in block to account for changes to the layout. ...
std::vector< MachineBasicBlock * >::reverse_iterator succ_reverse_iterator
const DataFlowGraph & G
Definition: RDFGraph.cpp:206
bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
void validateSuccProbs() const
Validate successors' probabilities and check if the sum of them is approximate one.
MachineBasicBlock::iterator getInitial()
void addLiveIn(const RegisterMaskPair &RegMaskPair)
static void deleteNode(NodeTy *V)
Definition: ilist.h:42
void sortUniqueLiveIns()
Sorts and uniques the LiveIns vector.
IterT skipDebugInstructionsBackward(IterT It, IterT Begin)
Decrement It until it points to a non-debug instruction or to Begin and return the resulting iterator...
const_reverse_iterator rbegin() const
reverse_instr_iterator instr_rend()
A range adaptor for a pair of iterators.
StringRef getName() const
Return the name of the corresponding LLVM basic block, or "(null)".
void push_back(pointer val)
Definition: ilist.h:326
MachineBasicBlock::const_succ_iterator ChildIteratorType
void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New)
Replace successor OLD with NEW and update probability info.
void setHasAddressTaken()
Set this block to reflect that it potentially is the target of an indirect branch.
MachineFunction * getParent()
MachineInstrBundleIterator< const MachineInstr, true > const_reverse_iterator
IterT skipDebugInstructionsForward(IterT It, IterT End)
Increment It until it points to a non-debug instruction or to End and return the resulting iterator...
void setIsEHFuncletEntry(bool V=true)
Indicates if this is the entry block of an EH funclet.
bool hasSuccessorProbabilities() const
Return true if any of the successors have probabilities attached to them.
Representation of each machine instruction.
Definition: MachineInstr.h:52
pointer remove(iterator &IT)
Definition: ilist.h:264
bool hasAddressTaken() const
Test whether this block is potentially the target of an indirect branch.
const_pred_iterator pred_begin() const
iterator insert(iterator where, pointer New)
Definition: ilist.h:241
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
void clear()
Definition: ilist.h:322
Register liveness not decidable from local neighborhood.
static NodeRef getEntryNode(const MachineBasicBlock *BB)
const_reverse_iterator rend() const
void push_back(MachineInstr *MI)
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
const_pred_reverse_iterator pred_rbegin() const
Pair of physical register and lane mask.
const uint32_t * getEndClobberMask(const TargetRegisterInfo *TRI) const
Get the clobber mask for the end of the basic block.
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
iterator getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:1726
void removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
bool isLiveIn(MCPhysReg Reg, LaneBitmask LaneMask=LaneBitmask::getAll()) const
Return true if the specified register is in the live in set.
Instructions::reverse_iterator reverse_instr_iterator
iterator_range< instr_iterator > instr_range
MachineBasicBlock::const_pred_iterator ChildIteratorType
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MachineBasicBlock * SplitCriticalEdge(MachineBasicBlock *Succ, Pass &P)
Split the critical edge from this block to the given successor block, and return the newly created bl...
std::vector< MachineBasicBlock * >::const_iterator const_succ_iterator
static ChildIteratorType child_end(NodeRef N)
aarch64 promote const
std::vector< MachineBasicBlock * >::reverse_iterator pred_reverse_iterator
LiveInVector::const_iterator livein_iterator
MachineInstrBundleIterator< const MachineInstr > const_iterator
static const Function * getParent(const Value *V)
Callbacks do nothing by default in iplist and ilist.
Definition: ilist.h:65
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
iterator SkipPHIsLabelsAndDebug(iterator I)
Return the first instruction in MBB after I that is not a PHI, label or debug.
const_pred_reverse_iterator pred_rend() const
IRTranslator LLVM IR MI
bool isPredecessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a predecessor of this block.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
const MachineInstr & back() const
void pop_back()
Definition: ilist.h:331
bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
const_instr_iterator instr_end() const
iterator insertAfter(iterator where, pointer New)
Definition: ilist.h:250
MachineInstrBundleIterator< MachineInstr, true > reverse_iterator
unsigned pred_size() const
unsigned operator()(const MachineBasicBlock *MBB) const
bool CorrectExtraCFGEdges(MachineBasicBlock *DestA, MachineBasicBlock *DestB, bool IsCond)
Various pieces of code can cause excess edges in the CFG to be inserted.
static NodeRef getEntryNode(MachineBasicBlock *BB)
void transferNodesFromList(ilist_callback_traits &OldList, Iterator, Iterator)
Callback before transferring nodes to this list.
Definition: ilist.h:73
iterator_range< const_pred_iterator > predecessors() const
MachineBasicBlock::iterator begin()
unsigned getAlignment() const
Return alignment of the basic block.