1 //===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Collect the sequence of machine instructions for a basic block.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H
15 #define LLVM_CODEGEN_MACHINEBASICBLOCK_H
17 #include "llvm/ADT/GraphTraits.h"
18 #include "llvm/CodeGen/MachineInstr.h"
19 #include "llvm/MC/MCRegisterInfo.h"
20 #include "llvm/Support/DataTypes.h"
27 class MachineFunction;
33 class MachineBranchProbabilityInfo;
35 // Forward declaration to avoid circular include problem with TargetRegisterInfo
36 typedef unsigned LaneBitmask;
39 struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
41 mutable ilist_half_node<MachineInstr> Sentinel;
43 // this is only set by the MachineBasicBlock owning the LiveList
44 friend class MachineBasicBlock;
45 MachineBasicBlock* Parent;
48 MachineInstr *createSentinel() const {
49 return static_cast<MachineInstr*>(&Sentinel);
51 void destroySentinel(MachineInstr *) const {}
53 MachineInstr *provideInitialHead() const { return createSentinel(); }
54 MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); }
55 static void noteHead(MachineInstr*, MachineInstr*) {}
57 void addNodeToList(MachineInstr* N);
58 void removeNodeFromList(MachineInstr* N);
59 void transferNodesFromList(ilist_traits &SrcTraits,
60 ilist_iterator<MachineInstr> First,
61 ilist_iterator<MachineInstr> Last);
62 void deleteNode(MachineInstr *N);
64 void createNode(const MachineInstr &);
67 class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
69 /// Pair of physical register and lane mask.
70 /// This is not simply a std::pair typedef because the members should be named
71 /// clearly as they both have an integer type.
72 struct RegisterMaskPair {
77 RegisterMaskPair(MCPhysReg PhysReg, LaneBitmask LaneMask)
78 : PhysReg(PhysReg), LaneMask(LaneMask) {}
82 typedef ilist<MachineInstr> Instructions;
86 MachineFunction *xParent;
88 /// Keep track of the predecessor / successor basic blocks.
89 std::vector<MachineBasicBlock *> Predecessors;
90 std::vector<MachineBasicBlock *> Successors;
92 /// Keep track of the weights to the successors. This vector has the same
93 /// order as Successors, or it is empty if we don't use it (disable
95 std::vector<uint32_t> Weights;
96 typedef std::vector<uint32_t>::iterator weight_iterator;
97 typedef std::vector<uint32_t>::const_iterator const_weight_iterator;
99 /// Keep track of the physical registers that are livein of the basicblock.
100 typedef std::vector<RegisterMaskPair> LiveInVector;
101 LiveInVector LiveIns;
103 /// Alignment of the basic block. Zero if the basic block does not need to be
104 /// aligned. The alignment is specified as log2(bytes).
105 unsigned Alignment = 0;
107 /// Indicate that this basic block is entered via an exception handler.
108 bool IsEHPad = false;
110 /// Indicate that this basic block is potentially the target of an indirect
112 bool AddressTaken = false;
114 /// Indicate that this basic block is the entry block of an EH funclet.
115 bool IsEHFuncletEntry = false;
117 /// \brief since getSymbol is a relatively heavy-weight operation, the symbol
118 /// is only computed once and is cached.
119 mutable MCSymbol *CachedMCSymbol = nullptr;
121 // Intrusive list support
122 MachineBasicBlock() {}
124 explicit MachineBasicBlock(MachineFunction &MF, const BasicBlock *BB);
126 ~MachineBasicBlock();
128 // MachineBasicBlocks are allocated and owned by MachineFunction.
129 friend class MachineFunction;
132 /// Return the LLVM basic block that this instance corresponded to originally.
133 /// Note that this may be NULL if this instance does not correspond directly
134 /// to an LLVM basic block.
135 const BasicBlock *getBasicBlock() const { return BB; }
137 /// Return the name of the corresponding LLVM basic block, or "(null)".
138 StringRef getName() const;
140 /// Return a formatted string to identify this block and its parent function.
141 std::string getFullName() const;
143 /// Test whether this block is potentially the target of an indirect branch.
144 bool hasAddressTaken() const { return AddressTaken; }
146 /// Set this block to reflect that it potentially is the target of an indirect
148 void setHasAddressTaken() { AddressTaken = true; }
150 /// Return the MachineFunction containing this basic block.
151 const MachineFunction *getParent() const { return xParent; }
152 MachineFunction *getParent() { return xParent; }
154 /// MachineBasicBlock iterator that automatically skips over MIs that are
155 /// inside bundles (i.e. walk top level MIs only).
156 template<typename Ty, typename IterTy>
157 class bundle_iterator
158 : public std::iterator<std::bidirectional_iterator_tag, Ty, ptrdiff_t> {
162 bundle_iterator(IterTy MI) : MII(MI) {}
164 bundle_iterator(Ty &MI) : MII(MI) {
165 assert(!MI.isBundledWithPred() &&
166 "It's not legal to initialize bundle_iterator with a bundled MI");
168 bundle_iterator(Ty *MI) : MII(MI) {
169 assert((!MI || !MI->isBundledWithPred()) &&
170 "It's not legal to initialize bundle_iterator with a bundled MI");
172 // Template allows conversion from const to nonconst.
173 template<class OtherTy, class OtherIterTy>
174 bundle_iterator(const bundle_iterator<OtherTy, OtherIterTy> &I)
175 : MII(I.getInstrIterator()) {}
176 bundle_iterator() : MII(nullptr) {}
178 Ty &operator*() const { return *MII; }
179 Ty *operator->() const { return &operator*(); }
181 operator Ty*() const { return MII; }
183 bool operator==(const bundle_iterator &X) const {
186 bool operator!=(const bundle_iterator &X) const {
187 return !operator==(X);
190 // Increment and decrement operators...
191 bundle_iterator &operator--() { // predecrement - Back up
193 while (MII->isBundledWithPred());
196 bundle_iterator &operator++() { // preincrement - Advance
197 while (MII->isBundledWithSucc())
202 bundle_iterator operator--(int) { // postdecrement operators...
203 bundle_iterator tmp = *this;
207 bundle_iterator operator++(int) { // postincrement operators...
208 bundle_iterator tmp = *this;
213 IterTy getInstrIterator() const {
218 typedef Instructions::iterator instr_iterator;
219 typedef Instructions::const_iterator const_instr_iterator;
220 typedef std::reverse_iterator<instr_iterator> reverse_instr_iterator;
222 std::reverse_iterator<const_instr_iterator> const_reverse_instr_iterator;
225 bundle_iterator<MachineInstr,instr_iterator> iterator;
227 bundle_iterator<const MachineInstr,const_instr_iterator> const_iterator;
228 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
229 typedef std::reverse_iterator<iterator> reverse_iterator;
232 unsigned size() const { return (unsigned)Insts.size(); }
233 bool empty() const { return Insts.empty(); }
235 MachineInstr &instr_front() { return Insts.front(); }
236 MachineInstr &instr_back() { return Insts.back(); }
237 const MachineInstr &instr_front() const { return Insts.front(); }
238 const MachineInstr &instr_back() const { return Insts.back(); }
240 MachineInstr &front() { return Insts.front(); }
241 MachineInstr &back() { return *--end(); }
242 const MachineInstr &front() const { return Insts.front(); }
243 const MachineInstr &back() const { return *--end(); }
245 instr_iterator instr_begin() { return Insts.begin(); }
246 const_instr_iterator instr_begin() const { return Insts.begin(); }
247 instr_iterator instr_end() { return Insts.end(); }
248 const_instr_iterator instr_end() const { return Insts.end(); }
249 reverse_instr_iterator instr_rbegin() { return Insts.rbegin(); }
250 const_reverse_instr_iterator instr_rbegin() const { return Insts.rbegin(); }
251 reverse_instr_iterator instr_rend () { return Insts.rend(); }
252 const_reverse_instr_iterator instr_rend () const { return Insts.rend(); }
254 iterator begin() { return instr_begin(); }
255 const_iterator begin() const { return instr_begin(); }
256 iterator end () { return instr_end(); }
257 const_iterator end () const { return instr_end(); }
258 reverse_iterator rbegin() { return instr_rbegin(); }
259 const_reverse_iterator rbegin() const { return instr_rbegin(); }
260 reverse_iterator rend () { return instr_rend(); }
261 const_reverse_iterator rend () const { return instr_rend(); }
263 inline iterator_range<iterator> terminators() {
264 return iterator_range<iterator>(getFirstTerminator(), end());
266 inline iterator_range<const_iterator> terminators() const {
267 return iterator_range<const_iterator>(getFirstTerminator(), end());
270 // Machine-CFG iterators
271 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
272 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
273 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
274 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
275 typedef std::vector<MachineBasicBlock *>::reverse_iterator
276 pred_reverse_iterator;
277 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
278 const_pred_reverse_iterator;
279 typedef std::vector<MachineBasicBlock *>::reverse_iterator
280 succ_reverse_iterator;
281 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
282 const_succ_reverse_iterator;
283 pred_iterator pred_begin() { return Predecessors.begin(); }
284 const_pred_iterator pred_begin() const { return Predecessors.begin(); }
285 pred_iterator pred_end() { return Predecessors.end(); }
286 const_pred_iterator pred_end() const { return Predecessors.end(); }
287 pred_reverse_iterator pred_rbegin()
288 { return Predecessors.rbegin();}
289 const_pred_reverse_iterator pred_rbegin() const
290 { return Predecessors.rbegin();}
291 pred_reverse_iterator pred_rend()
292 { return Predecessors.rend(); }
293 const_pred_reverse_iterator pred_rend() const
294 { return Predecessors.rend(); }
295 unsigned pred_size() const {
296 return (unsigned)Predecessors.size();
298 bool pred_empty() const { return Predecessors.empty(); }
299 succ_iterator succ_begin() { return Successors.begin(); }
300 const_succ_iterator succ_begin() const { return Successors.begin(); }
301 succ_iterator succ_end() { return Successors.end(); }
302 const_succ_iterator succ_end() const { return Successors.end(); }
303 succ_reverse_iterator succ_rbegin()
304 { return Successors.rbegin(); }
305 const_succ_reverse_iterator succ_rbegin() const
306 { return Successors.rbegin(); }
307 succ_reverse_iterator succ_rend()
308 { return Successors.rend(); }
309 const_succ_reverse_iterator succ_rend() const
310 { return Successors.rend(); }
311 unsigned succ_size() const {
312 return (unsigned)Successors.size();
314 bool succ_empty() const { return Successors.empty(); }
316 inline iterator_range<pred_iterator> predecessors() {
317 return iterator_range<pred_iterator>(pred_begin(), pred_end());
319 inline iterator_range<const_pred_iterator> predecessors() const {
320 return iterator_range<const_pred_iterator>(pred_begin(), pred_end());
322 inline iterator_range<succ_iterator> successors() {
323 return iterator_range<succ_iterator>(succ_begin(), succ_end());
325 inline iterator_range<const_succ_iterator> successors() const {
326 return iterator_range<const_succ_iterator>(succ_begin(), succ_end());
329 // LiveIn management methods.
331 /// Adds the specified register as a live in. Note that it is an error to add
332 /// the same register to the same set more than once unless the intention is
333 /// to call sortUniqueLiveIns after all registers are added.
334 void addLiveIn(MCPhysReg PhysReg, LaneBitmask LaneMask = ~0u) {
335 LiveIns.push_back(RegisterMaskPair(PhysReg, LaneMask));
337 void addLiveIn(const RegisterMaskPair &RegMaskPair) {
338 LiveIns.push_back(RegMaskPair);
341 /// Sorts and uniques the LiveIns vector. It can be significantly faster to do
342 /// this than repeatedly calling isLiveIn before calling addLiveIn for every
343 /// LiveIn insertion.
344 void sortUniqueLiveIns();
346 /// Add PhysReg as live in to this block, and ensure that there is a copy of
347 /// PhysReg to a virtual register of class RC. Return the virtual register
348 /// that is a copy of the live in PhysReg.
349 unsigned addLiveIn(MCPhysReg PhysReg, const TargetRegisterClass *RC);
351 /// Remove the specified register from the live in set.
352 void removeLiveIn(MCPhysReg Reg, LaneBitmask LaneMask = ~0u);
354 /// Return true if the specified register is in the live in set.
355 bool isLiveIn(MCPhysReg Reg, LaneBitmask LaneMask = ~0u) const;
357 // Iteration support for live in sets. These sets are kept in sorted
358 // order by their register number.
359 typedef LiveInVector::const_iterator livein_iterator;
360 livein_iterator livein_begin() const { return LiveIns.begin(); }
361 livein_iterator livein_end() const { return LiveIns.end(); }
362 bool livein_empty() const { return LiveIns.empty(); }
363 iterator_range<livein_iterator> liveins() const {
364 return make_range(livein_begin(), livein_end());
367 /// Return alignment of the basic block. The alignment is specified as
369 unsigned getAlignment() const { return Alignment; }
371 /// Set alignment of the basic block. The alignment is specified as
373 void setAlignment(unsigned Align) { Alignment = Align; }
375 /// Returns true if the block is a landing pad. That is this basic block is
376 /// entered via an exception handler.
377 bool isEHPad() const { return IsEHPad; }
379 /// Indicates the block is a landing pad. That is this basic block is entered
380 /// via an exception handler.
381 void setIsEHPad(bool V = true) { IsEHPad = V; }
383 /// If this block has a successor that is a landing pad, return it. Otherwise
385 const MachineBasicBlock *getLandingPadSuccessor() const;
387 bool hasEHPadSuccessor() const;
389 /// Returns true if this is the entry block of an EH funclet.
390 bool isEHFuncletEntry() const { return IsEHFuncletEntry; }
392 /// Indicates if this is the entry block of an EH funclet.
393 void setIsEHFuncletEntry(bool V = true) { IsEHFuncletEntry = V; }
395 // Code Layout methods.
397 /// Move 'this' block before or after the specified block. This only moves
398 /// the block, it does not modify the CFG or adjust potential fall-throughs at
399 /// the end of the block.
400 void moveBefore(MachineBasicBlock *NewAfter);
401 void moveAfter(MachineBasicBlock *NewBefore);
403 /// Update the terminator instructions in block to account for changes to the
404 /// layout. If the block previously used a fallthrough, it may now need a
405 /// branch, and if it previously used branching it may now be able to use a
407 void updateTerminator();
409 // Machine-CFG mutators
411 /// Add Succ as a successor of this MachineBasicBlock. The Predecessors list
412 /// of Succ is automatically updated. WEIGHT parameter is stored in Weights
413 /// list and it may be used by MachineBranchProbabilityInfo analysis to
414 /// calculate branch probability.
416 /// Note that duplicate Machine CFG edges are not allowed.
417 void addSuccessor(MachineBasicBlock *Succ, uint32_t Weight = 0);
419 /// Set successor weight of a given iterator.
420 void setSuccWeight(succ_iterator I, uint32_t Weight);
422 /// Remove successor from the successors list of this MachineBasicBlock. The
423 /// Predecessors list of Succ is automatically updated.
424 void removeSuccessor(MachineBasicBlock *Succ);
426 /// Remove specified successor from the successors list of this
427 /// MachineBasicBlock. The Predecessors list of Succ is automatically updated.
428 /// Return the iterator to the element after the one removed.
429 succ_iterator removeSuccessor(succ_iterator I);
431 /// Replace successor OLD with NEW and update weight info.
432 void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New);
434 /// Transfers all the successors from MBB to this machine basic block (i.e.,
435 /// copies all the successors FromMBB and remove all the successors from
437 void transferSuccessors(MachineBasicBlock *FromMBB);
439 /// Transfers all the successors, as in transferSuccessors, and update PHI
440 /// operands in the successor blocks which refer to FromMBB to refer to this.
441 void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB);
443 /// Return true if any of the successors have weights attached to them.
444 bool hasSuccessorWeights() const { return !Weights.empty(); }
446 /// Return true if the specified MBB is a predecessor of this block.
447 bool isPredecessor(const MachineBasicBlock *MBB) const;
449 /// Return true if the specified MBB is a successor of this block.
450 bool isSuccessor(const MachineBasicBlock *MBB) const;
452 /// Return true if the specified MBB will be emitted immediately after this
453 /// block, such that if this block exits by falling through, control will
454 /// transfer to the specified MBB. Note that MBB need not be a successor at
455 /// all, for example if this block ends with an unconditional branch to some
457 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
459 /// Return true if the block can implicitly transfer control to the block
460 /// after it by falling off the end of it. This should return false if it can
461 /// reach the block after it, but it uses an explicit branch to do so (e.g., a
462 /// table jump). True is a conservative answer.
463 bool canFallThrough();
465 /// Returns a pointer to the first instruction in this block that is not a
466 /// PHINode instruction. When adding instructions to the beginning of the
467 /// basic block, they should be added before the returned value, not before
468 /// the first instruction, which might be PHI.
469 /// Returns end() is there's no non-PHI instruction.
470 iterator getFirstNonPHI();
472 /// Return the first instruction in MBB after I that is not a PHI or a label.
473 /// This is the correct point to insert copies at the beginning of a basic
475 iterator SkipPHIsAndLabels(iterator I);
477 /// Returns an iterator to the first terminator instruction of this basic
478 /// block. If a terminator does not exist, it returns end().
479 iterator getFirstTerminator();
480 const_iterator getFirstTerminator() const {
481 return const_cast<MachineBasicBlock *>(this)->getFirstTerminator();
484 /// Same getFirstTerminator but it ignores bundles and return an
485 /// instr_iterator instead.
486 instr_iterator getFirstInstrTerminator();
488 /// Returns an iterator to the first non-debug instruction in the basic block,
490 iterator getFirstNonDebugInstr();
491 const_iterator getFirstNonDebugInstr() const {
492 return const_cast<MachineBasicBlock *>(this)->getFirstNonDebugInstr();
495 /// Returns an iterator to the last non-debug instruction in the basic block,
497 iterator getLastNonDebugInstr();
498 const_iterator getLastNonDebugInstr() const {
499 return const_cast<MachineBasicBlock *>(this)->getLastNonDebugInstr();
502 /// Convenience function that returns true if the block has no successors and
503 /// contains a return instruction.
504 bool isReturnBlock() const {
505 return !empty() && back().isReturn();
508 /// Split the critical edge from this block to the given successor block, and
509 /// return the newly created block, or null if splitting is not possible.
511 /// This function updates LiveVariables, MachineDominatorTree, and
512 /// MachineLoopInfo, as applicable.
513 MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P);
515 void pop_front() { Insts.pop_front(); }
516 void pop_back() { Insts.pop_back(); }
517 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
519 /// Insert MI into the instruction list before I, possibly inside a bundle.
521 /// If the insertion point is inside a bundle, MI will be added to the bundle,
522 /// otherwise MI will not be added to any bundle. That means this function
523 /// alone can't be used to prepend or append instructions to bundles. See
524 /// MIBundleBuilder::insert() for a more reliable way of doing that.
525 instr_iterator insert(instr_iterator I, MachineInstr *M);
527 /// Insert a range of instructions into the instruction list before I.
528 template<typename IT>
529 void insert(iterator I, IT S, IT E) {
530 assert((I == end() || I->getParent() == this) &&
531 "iterator points outside of basic block");
532 Insts.insert(I.getInstrIterator(), S, E);
535 /// Insert MI into the instruction list before I.
536 iterator insert(iterator I, MachineInstr *MI) {
537 assert((I == end() || I->getParent() == this) &&
538 "iterator points outside of basic block");
539 assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
540 "Cannot insert instruction with bundle flags");
541 return Insts.insert(I.getInstrIterator(), MI);
544 /// Insert MI into the instruction list after I.
545 iterator insertAfter(iterator I, MachineInstr *MI) {
546 assert((I == end() || I->getParent() == this) &&
547 "iterator points outside of basic block");
548 assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
549 "Cannot insert instruction with bundle flags");
550 return Insts.insertAfter(I.getInstrIterator(), MI);
553 /// Remove an instruction from the instruction list and delete it.
555 /// If the instruction is part of a bundle, the other instructions in the
556 /// bundle will still be bundled after removing the single instruction.
557 instr_iterator erase(instr_iterator I);
559 /// Remove an instruction from the instruction list and delete it.
561 /// If the instruction is part of a bundle, the other instructions in the
562 /// bundle will still be bundled after removing the single instruction.
563 instr_iterator erase_instr(MachineInstr *I) {
564 return erase(instr_iterator(I));
567 /// Remove a range of instructions from the instruction list and delete them.
568 iterator erase(iterator I, iterator E) {
569 return Insts.erase(I.getInstrIterator(), E.getInstrIterator());
572 /// Remove an instruction or bundle from the instruction list and delete it.
574 /// If I points to a bundle of instructions, they are all erased.
575 iterator erase(iterator I) {
576 return erase(I, std::next(I));
579 /// Remove an instruction from the instruction list and delete it.
581 /// If I is the head of a bundle of instructions, the whole bundle will be
583 iterator erase(MachineInstr *I) {
584 return erase(iterator(I));
587 /// Remove the unbundled instruction from the instruction list without
590 /// This function can not be used to remove bundled instructions, use
591 /// remove_instr to remove individual instructions from a bundle.
592 MachineInstr *remove(MachineInstr *I) {
593 assert(!I->isBundled() && "Cannot remove bundled instructions");
594 return Insts.remove(I);
597 /// Remove the possibly bundled instruction from the instruction list
598 /// without deleting it.
600 /// If the instruction is part of a bundle, the other instructions in the
601 /// bundle will still be bundled after removing the single instruction.
602 MachineInstr *remove_instr(MachineInstr *I);
608 /// Take an instruction from MBB 'Other' at the position From, and insert it
609 /// into this MBB right before 'Where'.
611 /// If From points to a bundle of instructions, the whole bundle is moved.
612 void splice(iterator Where, MachineBasicBlock *Other, iterator From) {
613 // The range splice() doesn't allow noop moves, but this one does.
615 splice(Where, Other, From, std::next(From));
618 /// Take a block of instructions from MBB 'Other' in the range [From, To),
619 /// and insert them into this MBB right before 'Where'.
621 /// The instruction at 'Where' must not be included in the range of
622 /// instructions to move.
623 void splice(iterator Where, MachineBasicBlock *Other,
624 iterator From, iterator To) {
625 Insts.splice(Where.getInstrIterator(), Other->Insts,
626 From.getInstrIterator(), To.getInstrIterator());
629 /// This method unlinks 'this' from the containing function, and returns it,
630 /// but does not delete it.
631 MachineBasicBlock *removeFromParent();
633 /// This method unlinks 'this' from the containing function and deletes it.
634 void eraseFromParent();
636 /// Given a machine basic block that branched to 'Old', change the code and
637 /// CFG so that it branches to 'New' instead.
638 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
640 /// Various pieces of code can cause excess edges in the CFG to be inserted.
641 /// If we have proven that MBB can only branch to DestA and DestB, remove any
642 /// other MBB successors from the CFG. DestA and DestB can be null. Besides
643 /// DestA and DestB, retain other edges leading to LandingPads (currently
644 /// there can be only one; we don't check or require that here). Note it is
645 /// possible that DestA and/or DestB are LandingPads.
646 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
647 MachineBasicBlock *DestB,
650 /// Find the next valid DebugLoc starting at MBBI, skipping any DBG_VALUE
651 /// instructions. Return UnknownLoc if there is none.
652 DebugLoc findDebugLoc(instr_iterator MBBI);
653 DebugLoc findDebugLoc(iterator MBBI) {
654 return findDebugLoc(MBBI.getInstrIterator());
657 /// Possible outcome of a register liveness query to computeRegisterLiveness()
658 enum LivenessQueryResult {
659 LQR_Live, ///< Register is known to be live.
660 LQR_OverlappingLive, ///< Register itself is not live, but some overlapping
662 LQR_Dead, ///< Register is known to be dead.
663 LQR_Unknown ///< Register liveness not decidable from local
667 /// Return whether (physical) register \p Reg has been <def>ined and not
668 /// <kill>ed as of just before \p Before.
670 /// Search is localised to a neighborhood of \p Neighborhood instructions
671 /// before (searching for defs or kills) and \p Neighborhood instructions
672 /// after (searching just for defs) \p Before.
674 /// \p Reg must be a physical register.
675 LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI,
677 const_iterator Before,
678 unsigned Neighborhood=10) const;
680 // Debugging methods.
682 void print(raw_ostream &OS, SlotIndexes* = nullptr) const;
683 void print(raw_ostream &OS, ModuleSlotTracker &MST,
684 SlotIndexes * = nullptr) const;
686 // Printing method used by LoopInfo.
687 void printAsOperand(raw_ostream &OS, bool PrintType = true) const;
689 /// MachineBasicBlocks are uniquely numbered at the function level, unless
690 /// they're not in a MachineFunction yet, in which case this will return -1.
691 int getNumber() const { return Number; }
692 void setNumber(int N) { Number = N; }
694 /// Return the MCSymbol for this basic block.
695 MCSymbol *getSymbol() const;
699 /// Return weight iterator corresponding to the I successor iterator.
700 weight_iterator getWeightIterator(succ_iterator I);
701 const_weight_iterator getWeightIterator(const_succ_iterator I) const;
703 friend class MachineBranchProbabilityInfo;
704 friend class MIPrinter;
706 /// Return weight of the edge from this block to MBB. This method should NOT
707 /// be called directly, but by using getEdgeWeight method from
708 /// MachineBranchProbabilityInfo class.
709 uint32_t getSuccWeight(const_succ_iterator Succ) const;
712 // Methods used to maintain doubly linked list of blocks...
713 friend struct ilist_traits<MachineBasicBlock>;
715 // Machine-CFG mutators
717 /// Remove Pred as a predecessor of this MachineBasicBlock. Don't do this
718 /// unless you know what you're doing, because it doesn't update Pred's
719 /// successors list. Use Pred->addSuccessor instead.
720 void addPredecessor(MachineBasicBlock *Pred);
722 /// Remove Pred as a predecessor of this MachineBasicBlock. Don't do this
723 /// unless you know what you're doing, because it doesn't update Pred's
724 /// successors list. Use Pred->removeSuccessor instead.
725 void removePredecessor(MachineBasicBlock *Pred);
728 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
730 // This is useful when building IndexedMaps keyed on basic block pointers.
731 struct MBB2NumberFunctor :
732 public std::unary_function<const MachineBasicBlock*, unsigned> {
733 unsigned operator()(const MachineBasicBlock *MBB) const {
734 return MBB->getNumber();
738 //===--------------------------------------------------------------------===//
739 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
740 //===--------------------------------------------------------------------===//
742 // Provide specializations of GraphTraits to be able to treat a
743 // MachineFunction as a graph of MachineBasicBlocks.
746 template <> struct GraphTraits<MachineBasicBlock *> {
747 typedef MachineBasicBlock NodeType;
748 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
750 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
751 static inline ChildIteratorType child_begin(NodeType *N) {
752 return N->succ_begin();
754 static inline ChildIteratorType child_end(NodeType *N) {
755 return N->succ_end();
759 template <> struct GraphTraits<const MachineBasicBlock *> {
760 typedef const MachineBasicBlock NodeType;
761 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
763 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
764 static inline ChildIteratorType child_begin(NodeType *N) {
765 return N->succ_begin();
767 static inline ChildIteratorType child_end(NodeType *N) {
768 return N->succ_end();
772 // Provide specializations of GraphTraits to be able to treat a
773 // MachineFunction as a graph of MachineBasicBlocks and to walk it
774 // in inverse order. Inverse order for a function is considered
775 // to be when traversing the predecessor edges of a MBB
776 // instead of the successor edges.
778 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
779 typedef MachineBasicBlock NodeType;
780 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
781 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
784 static inline ChildIteratorType child_begin(NodeType *N) {
785 return N->pred_begin();
787 static inline ChildIteratorType child_end(NodeType *N) {
788 return N->pred_end();
792 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
793 typedef const MachineBasicBlock NodeType;
794 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
795 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
798 static inline ChildIteratorType child_begin(NodeType *N) {
799 return N->pred_begin();
801 static inline ChildIteratorType child_end(NodeType *N) {
802 return N->pred_end();
808 /// MachineInstrSpan provides an interface to get an iteration range
809 /// containing the instruction it was initialized with, along with all
810 /// those instructions inserted prior to or following that instruction
811 /// at some point after the MachineInstrSpan is constructed.
812 class MachineInstrSpan {
813 MachineBasicBlock &MBB;
814 MachineBasicBlock::iterator I, B, E;
816 MachineInstrSpan(MachineBasicBlock::iterator I)
817 : MBB(*I->getParent()),
819 B(I == MBB.begin() ? MBB.end() : std::prev(I)),
822 MachineBasicBlock::iterator begin() {
823 return B == MBB.end() ? MBB.begin() : std::next(B);
825 MachineBasicBlock::iterator end() { return E; }
826 bool empty() { return begin() == end(); }
828 MachineBasicBlock::iterator getInitial() { return I; }
831 } // End llvm namespace