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/Support/BranchProbability.h"
20 #include "llvm/MC/MCRegisterInfo.h"
21 #include "llvm/Support/DataTypes.h"
28 class MachineFunction;
34 class MachineBranchProbabilityInfo;
36 // Forward declaration to avoid circular include problem with TargetRegisterInfo
37 typedef unsigned LaneBitmask;
40 struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
42 mutable ilist_half_node<MachineInstr> Sentinel;
44 // this is only set by the MachineBasicBlock owning the LiveList
45 friend class MachineBasicBlock;
46 MachineBasicBlock* Parent;
49 MachineInstr *createSentinel() const {
50 return static_cast<MachineInstr*>(&Sentinel);
52 void destroySentinel(MachineInstr *) const {}
54 MachineInstr *provideInitialHead() const { return createSentinel(); }
55 MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); }
56 static void noteHead(MachineInstr*, MachineInstr*) {}
58 void addNodeToList(MachineInstr* N);
59 void removeNodeFromList(MachineInstr* N);
60 void transferNodesFromList(ilist_traits &SrcTraits,
61 ilist_iterator<MachineInstr> First,
62 ilist_iterator<MachineInstr> Last);
63 void deleteNode(MachineInstr *N);
65 void createNode(const MachineInstr &);
68 class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
70 /// Pair of physical register and lane mask.
71 /// This is not simply a std::pair typedef because the members should be named
72 /// clearly as they both have an integer type.
73 struct RegisterMaskPair {
78 RegisterMaskPair(MCPhysReg PhysReg, LaneBitmask LaneMask)
79 : PhysReg(PhysReg), LaneMask(LaneMask) {}
83 typedef ilist<MachineInstr> Instructions;
87 MachineFunction *xParent;
89 /// Keep track of the predecessor / successor basic blocks.
90 std::vector<MachineBasicBlock *> Predecessors;
91 std::vector<MachineBasicBlock *> Successors;
93 /// Keep track of the weights to the successors. This vector has the same
94 /// order as Successors, or it is empty if we don't use it (disable
96 std::vector<uint32_t> Weights;
97 typedef std::vector<uint32_t>::iterator weight_iterator;
98 typedef std::vector<uint32_t>::const_iterator const_weight_iterator;
100 /// Keep track of the probabilities to the successors. This vector has the
101 /// same order as Successors, or it is empty if we don't use it (disable
103 std::vector<BranchProbability> Probs;
104 typedef std::vector<BranchProbability>::iterator probability_iterator;
105 typedef std::vector<BranchProbability>::const_iterator
106 const_probability_iterator;
108 /// Keep track of the physical registers that are livein of the basicblock.
109 typedef std::vector<RegisterMaskPair> LiveInVector;
110 LiveInVector LiveIns;
112 /// Alignment of the basic block. Zero if the basic block does not need to be
113 /// aligned. The alignment is specified as log2(bytes).
114 unsigned Alignment = 0;
116 /// Indicate that this basic block is entered via an exception handler.
117 bool IsEHPad = false;
119 /// Indicate that this basic block is potentially the target of an indirect
121 bool AddressTaken = false;
123 /// Indicate that this basic block is the entry block of an EH funclet.
124 bool IsEHFuncletEntry = false;
126 /// Indicate that this basic block is the entry block of a cleanup funclet.
127 bool IsCleanupFuncletEntry = false;
129 /// \brief since getSymbol is a relatively heavy-weight operation, the symbol
130 /// is only computed once and is cached.
131 mutable MCSymbol *CachedMCSymbol = nullptr;
133 // Intrusive list support
134 MachineBasicBlock() {}
136 explicit MachineBasicBlock(MachineFunction &MF, const BasicBlock *BB);
138 ~MachineBasicBlock();
140 // MachineBasicBlocks are allocated and owned by MachineFunction.
141 friend class MachineFunction;
144 /// Return the LLVM basic block that this instance corresponded to originally.
145 /// Note that this may be NULL if this instance does not correspond directly
146 /// to an LLVM basic block.
147 const BasicBlock *getBasicBlock() const { return BB; }
149 /// Return the name of the corresponding LLVM basic block, or "(null)".
150 StringRef getName() const;
152 /// Return a formatted string to identify this block and its parent function.
153 std::string getFullName() const;
155 /// Test whether this block is potentially the target of an indirect branch.
156 bool hasAddressTaken() const { return AddressTaken; }
158 /// Set this block to reflect that it potentially is the target of an indirect
160 void setHasAddressTaken() { AddressTaken = true; }
162 /// Return the MachineFunction containing this basic block.
163 const MachineFunction *getParent() const { return xParent; }
164 MachineFunction *getParent() { return xParent; }
166 /// MachineBasicBlock iterator that automatically skips over MIs that are
167 /// inside bundles (i.e. walk top level MIs only).
168 template<typename Ty, typename IterTy>
169 class bundle_iterator
170 : public std::iterator<std::bidirectional_iterator_tag, Ty, ptrdiff_t> {
174 bundle_iterator(IterTy MI) : MII(MI) {}
176 bundle_iterator(Ty &MI) : MII(MI) {
177 assert(!MI.isBundledWithPred() &&
178 "It's not legal to initialize bundle_iterator with a bundled MI");
180 bundle_iterator(Ty *MI) : MII(MI) {
181 assert((!MI || !MI->isBundledWithPred()) &&
182 "It's not legal to initialize bundle_iterator with a bundled MI");
184 // Template allows conversion from const to nonconst.
185 template<class OtherTy, class OtherIterTy>
186 bundle_iterator(const bundle_iterator<OtherTy, OtherIterTy> &I)
187 : MII(I.getInstrIterator()) {}
188 bundle_iterator() : MII(nullptr) {}
190 Ty &operator*() const { return *MII; }
191 Ty *operator->() const { return &operator*(); }
193 operator Ty *() const { return MII.getNodePtrUnchecked(); }
195 bool operator==(const bundle_iterator &X) const {
198 bool operator!=(const bundle_iterator &X) const {
199 return !operator==(X);
202 // Increment and decrement operators...
203 bundle_iterator &operator--() { // predecrement - Back up
205 while (MII->isBundledWithPred());
208 bundle_iterator &operator++() { // preincrement - Advance
209 while (MII->isBundledWithSucc())
214 bundle_iterator operator--(int) { // postdecrement operators...
215 bundle_iterator tmp = *this;
219 bundle_iterator operator++(int) { // postincrement operators...
220 bundle_iterator tmp = *this;
225 IterTy getInstrIterator() const {
230 typedef Instructions::iterator instr_iterator;
231 typedef Instructions::const_iterator const_instr_iterator;
232 typedef std::reverse_iterator<instr_iterator> reverse_instr_iterator;
234 std::reverse_iterator<const_instr_iterator> const_reverse_instr_iterator;
237 bundle_iterator<MachineInstr,instr_iterator> iterator;
239 bundle_iterator<const MachineInstr,const_instr_iterator> const_iterator;
240 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
241 typedef std::reverse_iterator<iterator> reverse_iterator;
244 unsigned size() const { return (unsigned)Insts.size(); }
245 bool empty() const { return Insts.empty(); }
247 MachineInstr &instr_front() { return Insts.front(); }
248 MachineInstr &instr_back() { return Insts.back(); }
249 const MachineInstr &instr_front() const { return Insts.front(); }
250 const MachineInstr &instr_back() const { return Insts.back(); }
252 MachineInstr &front() { return Insts.front(); }
253 MachineInstr &back() { return *--end(); }
254 const MachineInstr &front() const { return Insts.front(); }
255 const MachineInstr &back() const { return *--end(); }
257 instr_iterator instr_begin() { return Insts.begin(); }
258 const_instr_iterator instr_begin() const { return Insts.begin(); }
259 instr_iterator instr_end() { return Insts.end(); }
260 const_instr_iterator instr_end() const { return Insts.end(); }
261 reverse_instr_iterator instr_rbegin() { return Insts.rbegin(); }
262 const_reverse_instr_iterator instr_rbegin() const { return Insts.rbegin(); }
263 reverse_instr_iterator instr_rend () { return Insts.rend(); }
264 const_reverse_instr_iterator instr_rend () const { return Insts.rend(); }
266 iterator begin() { return instr_begin(); }
267 const_iterator begin() const { return instr_begin(); }
268 iterator end () { return instr_end(); }
269 const_iterator end () const { return instr_end(); }
270 reverse_iterator rbegin() { return instr_rbegin(); }
271 const_reverse_iterator rbegin() const { return instr_rbegin(); }
272 reverse_iterator rend () { return instr_rend(); }
273 const_reverse_iterator rend () const { return instr_rend(); }
275 inline iterator_range<iterator> terminators() {
276 return iterator_range<iterator>(getFirstTerminator(), end());
278 inline iterator_range<const_iterator> terminators() const {
279 return iterator_range<const_iterator>(getFirstTerminator(), end());
282 // Machine-CFG iterators
283 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
284 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
285 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
286 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
287 typedef std::vector<MachineBasicBlock *>::reverse_iterator
288 pred_reverse_iterator;
289 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
290 const_pred_reverse_iterator;
291 typedef std::vector<MachineBasicBlock *>::reverse_iterator
292 succ_reverse_iterator;
293 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
294 const_succ_reverse_iterator;
295 pred_iterator pred_begin() { return Predecessors.begin(); }
296 const_pred_iterator pred_begin() const { return Predecessors.begin(); }
297 pred_iterator pred_end() { return Predecessors.end(); }
298 const_pred_iterator pred_end() const { return Predecessors.end(); }
299 pred_reverse_iterator pred_rbegin()
300 { return Predecessors.rbegin();}
301 const_pred_reverse_iterator pred_rbegin() const
302 { return Predecessors.rbegin();}
303 pred_reverse_iterator pred_rend()
304 { return Predecessors.rend(); }
305 const_pred_reverse_iterator pred_rend() const
306 { return Predecessors.rend(); }
307 unsigned pred_size() const {
308 return (unsigned)Predecessors.size();
310 bool pred_empty() const { return Predecessors.empty(); }
311 succ_iterator succ_begin() { return Successors.begin(); }
312 const_succ_iterator succ_begin() const { return Successors.begin(); }
313 succ_iterator succ_end() { return Successors.end(); }
314 const_succ_iterator succ_end() const { return Successors.end(); }
315 succ_reverse_iterator succ_rbegin()
316 { return Successors.rbegin(); }
317 const_succ_reverse_iterator succ_rbegin() const
318 { return Successors.rbegin(); }
319 succ_reverse_iterator succ_rend()
320 { return Successors.rend(); }
321 const_succ_reverse_iterator succ_rend() const
322 { return Successors.rend(); }
323 unsigned succ_size() const {
324 return (unsigned)Successors.size();
326 bool succ_empty() const { return Successors.empty(); }
328 inline iterator_range<pred_iterator> predecessors() {
329 return iterator_range<pred_iterator>(pred_begin(), pred_end());
331 inline iterator_range<const_pred_iterator> predecessors() const {
332 return iterator_range<const_pred_iterator>(pred_begin(), pred_end());
334 inline iterator_range<succ_iterator> successors() {
335 return iterator_range<succ_iterator>(succ_begin(), succ_end());
337 inline iterator_range<const_succ_iterator> successors() const {
338 return iterator_range<const_succ_iterator>(succ_begin(), succ_end());
341 // LiveIn management methods.
343 /// Adds the specified register as a live in. Note that it is an error to add
344 /// the same register to the same set more than once unless the intention is
345 /// to call sortUniqueLiveIns after all registers are added.
346 void addLiveIn(MCPhysReg PhysReg, LaneBitmask LaneMask = ~0u) {
347 LiveIns.push_back(RegisterMaskPair(PhysReg, LaneMask));
349 void addLiveIn(const RegisterMaskPair &RegMaskPair) {
350 LiveIns.push_back(RegMaskPair);
353 /// Sorts and uniques the LiveIns vector. It can be significantly faster to do
354 /// this than repeatedly calling isLiveIn before calling addLiveIn for every
355 /// LiveIn insertion.
356 void sortUniqueLiveIns();
358 /// Add PhysReg as live in to this block, and ensure that there is a copy of
359 /// PhysReg to a virtual register of class RC. Return the virtual register
360 /// that is a copy of the live in PhysReg.
361 unsigned addLiveIn(MCPhysReg PhysReg, const TargetRegisterClass *RC);
363 /// Remove the specified register from the live in set.
364 void removeLiveIn(MCPhysReg Reg, LaneBitmask LaneMask = ~0u);
366 /// Return true if the specified register is in the live in set.
367 bool isLiveIn(MCPhysReg Reg, LaneBitmask LaneMask = ~0u) const;
369 // Iteration support for live in sets. These sets are kept in sorted
370 // order by their register number.
371 typedef LiveInVector::const_iterator livein_iterator;
372 livein_iterator livein_begin() const { return LiveIns.begin(); }
373 livein_iterator livein_end() const { return LiveIns.end(); }
374 bool livein_empty() const { return LiveIns.empty(); }
375 iterator_range<livein_iterator> liveins() const {
376 return make_range(livein_begin(), livein_end());
379 /// Get the clobber mask for the start of this basic block. Funclets use this
380 /// to prevent register allocation across funclet transitions.
381 const uint32_t *getBeginClobberMask(const TargetRegisterInfo *TRI) const;
383 /// Get the clobber mask for the end of the basic block.
384 /// \see getBeginClobberMask()
385 const uint32_t *getEndClobberMask(const TargetRegisterInfo *TRI) const;
387 /// Return alignment of the basic block. The alignment is specified as
389 unsigned getAlignment() const { return Alignment; }
391 /// Set alignment of the basic block. The alignment is specified as
393 void setAlignment(unsigned Align) { Alignment = Align; }
395 /// Returns true if the block is a landing pad. That is this basic block is
396 /// entered via an exception handler.
397 bool isEHPad() const { return IsEHPad; }
399 /// Indicates the block is a landing pad. That is this basic block is entered
400 /// via an exception handler.
401 void setIsEHPad(bool V = true) { IsEHPad = V; }
403 /// If this block has a successor that is a landing pad, return it. Otherwise
405 const MachineBasicBlock *getLandingPadSuccessor() const;
407 bool hasEHPadSuccessor() const;
409 /// Returns true if this is the entry block of an EH funclet.
410 bool isEHFuncletEntry() const { return IsEHFuncletEntry; }
412 /// Indicates if this is the entry block of an EH funclet.
413 void setIsEHFuncletEntry(bool V = true) { IsEHFuncletEntry = V; }
415 /// Returns true if this is the entry block of a cleanup funclet.
416 bool isCleanupFuncletEntry() const { return IsCleanupFuncletEntry; }
418 /// Indicates if this is the entry block of a cleanup funclet.
419 void setIsCleanupFuncletEntry(bool V = true) { IsCleanupFuncletEntry = V; }
421 // Code Layout methods.
423 /// Move 'this' block before or after the specified block. This only moves
424 /// the block, it does not modify the CFG or adjust potential fall-throughs at
425 /// the end of the block.
426 void moveBefore(MachineBasicBlock *NewAfter);
427 void moveAfter(MachineBasicBlock *NewBefore);
429 /// Update the terminator instructions in block to account for changes to the
430 /// layout. If the block previously used a fallthrough, it may now need a
431 /// branch, and if it previously used branching it may now be able to use a
433 void updateTerminator();
435 // Machine-CFG mutators
437 /// Add Succ as a successor of this MachineBasicBlock. The Predecessors list
438 /// of Succ is automatically updated. WEIGHT parameter is stored in Weights
439 /// list and it may be used by MachineBranchProbabilityInfo analysis to
440 /// calculate branch probability.
442 /// Note that duplicate Machine CFG edges are not allowed.
443 void addSuccessor(MachineBasicBlock *Succ, uint32_t Weight = 0);
445 /// Add Succ as a successor of this MachineBasicBlock. The Predecessors list
446 /// of Succ is automatically updated. The weight is not provided because BPI
447 /// is not available (e.g. -O0 is used), in which case edge weights won't be
448 /// used. Using this interface can save some space.
449 void addSuccessorWithoutWeight(MachineBasicBlock *Succ);
451 /// Add Succ as a successor of this MachineBasicBlock. The Predecessors list
452 /// of Succ is automatically updated. PROB parameter is stored in
453 /// Probabilities list.
455 /// Note that duplicate Machine CFG edges are not allowed.
456 void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob);
458 /// Add Succ as a successor of this MachineBasicBlock. The Predecessors list
459 /// of Succ is automatically updated. The probability is not provided because
460 /// BPI is not available (e.g. -O0 is used), in which case edge probabilities
461 /// won't be used. Using this interface can save some space.
462 void addSuccessorWithoutProb(MachineBasicBlock *Succ);
464 /// Set successor weight of a given iterator.
465 void setSuccWeight(succ_iterator I, uint32_t Weight);
467 /// Set successor probability of a given iterator.
468 void setSuccProbability(succ_iterator I, BranchProbability Prob);
470 /// Normalize probabilities of all successors so that the sum of them becomes
472 void normalizeSuccProbs() {
473 BranchProbability::normalizeProbabilities(Probs);
476 /// Remove successor from the successors list of this MachineBasicBlock. The
477 /// Predecessors list of Succ is automatically updated.
478 void removeSuccessor(MachineBasicBlock *Succ);
480 /// Remove specified successor from the successors list of this
481 /// MachineBasicBlock. The Predecessors list of Succ is automatically updated.
482 /// Return the iterator to the element after the one removed.
483 succ_iterator removeSuccessor(succ_iterator I);
485 /// Replace successor OLD with NEW and update weight info.
486 void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New);
488 /// Transfers all the successors from MBB to this machine basic block (i.e.,
489 /// copies all the successors FromMBB and remove all the successors from
491 void transferSuccessors(MachineBasicBlock *FromMBB);
493 /// Transfers all the successors, as in transferSuccessors, and update PHI
494 /// operands in the successor blocks which refer to FromMBB to refer to this.
495 void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB);
497 /// Return true if any of the successors have weights attached to them.
498 bool hasSuccessorWeights() const { return !Weights.empty(); }
500 /// Return true if any of the successors have probabilities attached to them.
501 bool hasSuccessorProbabilities() const { return !Probs.empty(); }
503 /// Return true if the specified MBB is a predecessor of this block.
504 bool isPredecessor(const MachineBasicBlock *MBB) const;
506 /// Return true if the specified MBB is a successor of this block.
507 bool isSuccessor(const MachineBasicBlock *MBB) const;
509 /// Return true if the specified MBB will be emitted immediately after this
510 /// block, such that if this block exits by falling through, control will
511 /// transfer to the specified MBB. Note that MBB need not be a successor at
512 /// all, for example if this block ends with an unconditional branch to some
514 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
516 /// Return true if the block can implicitly transfer control to the block
517 /// after it by falling off the end of it. This should return false if it can
518 /// reach the block after it, but it uses an explicit branch to do so (e.g., a
519 /// table jump). True is a conservative answer.
520 bool canFallThrough();
522 /// Returns a pointer to the first instruction in this block that is not a
523 /// PHINode instruction. When adding instructions to the beginning of the
524 /// basic block, they should be added before the returned value, not before
525 /// the first instruction, which might be PHI.
526 /// Returns end() is there's no non-PHI instruction.
527 iterator getFirstNonPHI();
529 /// Return the first instruction in MBB after I that is not a PHI or a label.
530 /// This is the correct point to insert copies at the beginning of a basic
532 iterator SkipPHIsAndLabels(iterator I);
534 /// Returns an iterator to the first terminator instruction of this basic
535 /// block. If a terminator does not exist, it returns end().
536 iterator getFirstTerminator();
537 const_iterator getFirstTerminator() const {
538 return const_cast<MachineBasicBlock *>(this)->getFirstTerminator();
541 /// Same getFirstTerminator but it ignores bundles and return an
542 /// instr_iterator instead.
543 instr_iterator getFirstInstrTerminator();
545 /// Returns an iterator to the first non-debug instruction in the basic block,
547 iterator getFirstNonDebugInstr();
548 const_iterator getFirstNonDebugInstr() const {
549 return const_cast<MachineBasicBlock *>(this)->getFirstNonDebugInstr();
552 /// Returns an iterator to the last non-debug instruction in the basic block,
554 iterator getLastNonDebugInstr();
555 const_iterator getLastNonDebugInstr() const {
556 return const_cast<MachineBasicBlock *>(this)->getLastNonDebugInstr();
559 /// Convenience function that returns true if the block ends in a return
561 bool isReturnBlock() const {
562 return !empty() && back().isReturn();
565 /// Split the critical edge from this block to the given successor block, and
566 /// return the newly created block, or null if splitting is not possible.
568 /// This function updates LiveVariables, MachineDominatorTree, and
569 /// MachineLoopInfo, as applicable.
570 MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P);
572 void pop_front() { Insts.pop_front(); }
573 void pop_back() { Insts.pop_back(); }
574 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
576 /// Insert MI into the instruction list before I, possibly inside a bundle.
578 /// If the insertion point is inside a bundle, MI will be added to the bundle,
579 /// otherwise MI will not be added to any bundle. That means this function
580 /// alone can't be used to prepend or append instructions to bundles. See
581 /// MIBundleBuilder::insert() for a more reliable way of doing that.
582 instr_iterator insert(instr_iterator I, MachineInstr *M);
584 /// Insert a range of instructions into the instruction list before I.
585 template<typename IT>
586 void insert(iterator I, IT S, IT E) {
587 assert((I == end() || I->getParent() == this) &&
588 "iterator points outside of basic block");
589 Insts.insert(I.getInstrIterator(), S, E);
592 /// Insert MI into the instruction list before I.
593 iterator insert(iterator I, MachineInstr *MI) {
594 assert((I == end() || I->getParent() == this) &&
595 "iterator points outside of basic block");
596 assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
597 "Cannot insert instruction with bundle flags");
598 return Insts.insert(I.getInstrIterator(), MI);
601 /// Insert MI into the instruction list after I.
602 iterator insertAfter(iterator I, MachineInstr *MI) {
603 assert((I == end() || I->getParent() == this) &&
604 "iterator points outside of basic block");
605 assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
606 "Cannot insert instruction with bundle flags");
607 return Insts.insertAfter(I.getInstrIterator(), MI);
610 /// Remove an instruction from the instruction list and delete it.
612 /// If the instruction is part of a bundle, the other instructions in the
613 /// bundle will still be bundled after removing the single instruction.
614 instr_iterator erase(instr_iterator I);
616 /// Remove an instruction from the instruction list and delete it.
618 /// If the instruction is part of a bundle, the other instructions in the
619 /// bundle will still be bundled after removing the single instruction.
620 instr_iterator erase_instr(MachineInstr *I) {
621 return erase(instr_iterator(I));
624 /// Remove a range of instructions from the instruction list and delete them.
625 iterator erase(iterator I, iterator E) {
626 return Insts.erase(I.getInstrIterator(), E.getInstrIterator());
629 /// Remove an instruction or bundle from the instruction list and delete it.
631 /// If I points to a bundle of instructions, they are all erased.
632 iterator erase(iterator I) {
633 return erase(I, std::next(I));
636 /// Remove an instruction from the instruction list and delete it.
638 /// If I is the head of a bundle of instructions, the whole bundle will be
640 iterator erase(MachineInstr *I) {
641 return erase(iterator(I));
644 /// Remove the unbundled instruction from the instruction list without
647 /// This function can not be used to remove bundled instructions, use
648 /// remove_instr to remove individual instructions from a bundle.
649 MachineInstr *remove(MachineInstr *I) {
650 assert(!I->isBundled() && "Cannot remove bundled instructions");
651 return Insts.remove(instr_iterator(I));
654 /// Remove the possibly bundled instruction from the instruction list
655 /// without deleting it.
657 /// If the instruction is part of a bundle, the other instructions in the
658 /// bundle will still be bundled after removing the single instruction.
659 MachineInstr *remove_instr(MachineInstr *I);
665 /// Take an instruction from MBB 'Other' at the position From, and insert it
666 /// into this MBB right before 'Where'.
668 /// If From points to a bundle of instructions, the whole bundle is moved.
669 void splice(iterator Where, MachineBasicBlock *Other, iterator From) {
670 // The range splice() doesn't allow noop moves, but this one does.
672 splice(Where, Other, From, std::next(From));
675 /// Take a block of instructions from MBB 'Other' in the range [From, To),
676 /// and insert them into this MBB right before 'Where'.
678 /// The instruction at 'Where' must not be included in the range of
679 /// instructions to move.
680 void splice(iterator Where, MachineBasicBlock *Other,
681 iterator From, iterator To) {
682 Insts.splice(Where.getInstrIterator(), Other->Insts,
683 From.getInstrIterator(), To.getInstrIterator());
686 /// This method unlinks 'this' from the containing function, and returns it,
687 /// but does not delete it.
688 MachineBasicBlock *removeFromParent();
690 /// This method unlinks 'this' from the containing function and deletes it.
691 void eraseFromParent();
693 /// Given a machine basic block that branched to 'Old', change the code and
694 /// CFG so that it branches to 'New' instead.
695 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
697 /// Various pieces of code can cause excess edges in the CFG to be inserted.
698 /// If we have proven that MBB can only branch to DestA and DestB, remove any
699 /// other MBB successors from the CFG. DestA and DestB can be null. Besides
700 /// DestA and DestB, retain other edges leading to LandingPads (currently
701 /// there can be only one; we don't check or require that here). Note it is
702 /// possible that DestA and/or DestB are LandingPads.
703 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
704 MachineBasicBlock *DestB,
707 /// Find the next valid DebugLoc starting at MBBI, skipping any DBG_VALUE
708 /// instructions. Return UnknownLoc if there is none.
709 DebugLoc findDebugLoc(instr_iterator MBBI);
710 DebugLoc findDebugLoc(iterator MBBI) {
711 return findDebugLoc(MBBI.getInstrIterator());
714 /// Possible outcome of a register liveness query to computeRegisterLiveness()
715 enum LivenessQueryResult {
716 LQR_Live, ///< Register is known to be live.
717 LQR_OverlappingLive, ///< Register itself is not live, but some overlapping
719 LQR_Dead, ///< Register is known to be dead.
720 LQR_Unknown ///< Register liveness not decidable from local
724 /// Return whether (physical) register \p Reg has been <def>ined and not
725 /// <kill>ed as of just before \p Before.
727 /// Search is localised to a neighborhood of \p Neighborhood instructions
728 /// before (searching for defs or kills) and \p Neighborhood instructions
729 /// after (searching just for defs) \p Before.
731 /// \p Reg must be a physical register.
732 LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI,
734 const_iterator Before,
735 unsigned Neighborhood=10) const;
737 // Debugging methods.
739 void print(raw_ostream &OS, SlotIndexes* = nullptr) const;
740 void print(raw_ostream &OS, ModuleSlotTracker &MST,
741 SlotIndexes * = nullptr) const;
743 // Printing method used by LoopInfo.
744 void printAsOperand(raw_ostream &OS, bool PrintType = true) const;
746 /// MachineBasicBlocks are uniquely numbered at the function level, unless
747 /// they're not in a MachineFunction yet, in which case this will return -1.
748 int getNumber() const { return Number; }
749 void setNumber(int N) { Number = N; }
751 /// Return the MCSymbol for this basic block.
752 MCSymbol *getSymbol() const;
756 /// Return weight iterator corresponding to the I successor iterator.
757 weight_iterator getWeightIterator(succ_iterator I);
758 const_weight_iterator getWeightIterator(const_succ_iterator I) const;
760 /// Return probability iterator corresponding to the I successor iterator.
761 probability_iterator getProbabilityIterator(succ_iterator I);
762 const_probability_iterator
763 getProbabilityIterator(const_succ_iterator I) const;
765 friend class MachineBranchProbabilityInfo;
766 friend class MIPrinter;
768 /// Return weight of the edge from this block to MBB. This method should NOT
769 /// be called directly, but by using getEdgeWeight method from
770 /// MachineBranchProbabilityInfo class.
771 uint32_t getSuccWeight(const_succ_iterator Succ) const;
773 /// Return probability of the edge from this block to MBB. This method should
774 /// NOT be called directly, but by using getEdgeProbability method from
775 /// MachineBranchProbabilityInfo class.
776 BranchProbability getSuccProbability(const_succ_iterator Succ) const;
778 // Methods used to maintain doubly linked list of blocks...
779 friend struct ilist_traits<MachineBasicBlock>;
781 // Machine-CFG mutators
783 /// Remove Pred as a predecessor of this MachineBasicBlock. Don't do this
784 /// unless you know what you're doing, because it doesn't update Pred's
785 /// successors list. Use Pred->addSuccessor instead.
786 void addPredecessor(MachineBasicBlock *Pred);
788 /// Remove Pred as a predecessor of this MachineBasicBlock. Don't do this
789 /// unless you know what you're doing, because it doesn't update Pred's
790 /// successors list. Use Pred->removeSuccessor instead.
791 void removePredecessor(MachineBasicBlock *Pred);
794 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
796 // This is useful when building IndexedMaps keyed on basic block pointers.
797 struct MBB2NumberFunctor :
798 public std::unary_function<const MachineBasicBlock*, unsigned> {
799 unsigned operator()(const MachineBasicBlock *MBB) const {
800 return MBB->getNumber();
804 //===--------------------------------------------------------------------===//
805 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
806 //===--------------------------------------------------------------------===//
808 // Provide specializations of GraphTraits to be able to treat a
809 // MachineFunction as a graph of MachineBasicBlocks.
812 template <> struct GraphTraits<MachineBasicBlock *> {
813 typedef MachineBasicBlock NodeType;
814 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
816 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
817 static inline ChildIteratorType child_begin(NodeType *N) {
818 return N->succ_begin();
820 static inline ChildIteratorType child_end(NodeType *N) {
821 return N->succ_end();
825 template <> struct GraphTraits<const MachineBasicBlock *> {
826 typedef const MachineBasicBlock NodeType;
827 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
829 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
830 static inline ChildIteratorType child_begin(NodeType *N) {
831 return N->succ_begin();
833 static inline ChildIteratorType child_end(NodeType *N) {
834 return N->succ_end();
838 // Provide specializations of GraphTraits to be able to treat a
839 // MachineFunction as a graph of MachineBasicBlocks and to walk it
840 // in inverse order. Inverse order for a function is considered
841 // to be when traversing the predecessor edges of a MBB
842 // instead of the successor edges.
844 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
845 typedef MachineBasicBlock NodeType;
846 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
847 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
850 static inline ChildIteratorType child_begin(NodeType *N) {
851 return N->pred_begin();
853 static inline ChildIteratorType child_end(NodeType *N) {
854 return N->pred_end();
858 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
859 typedef const MachineBasicBlock NodeType;
860 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
861 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
864 static inline ChildIteratorType child_begin(NodeType *N) {
865 return N->pred_begin();
867 static inline ChildIteratorType child_end(NodeType *N) {
868 return N->pred_end();
874 /// MachineInstrSpan provides an interface to get an iteration range
875 /// containing the instruction it was initialized with, along with all
876 /// those instructions inserted prior to or following that instruction
877 /// at some point after the MachineInstrSpan is constructed.
878 class MachineInstrSpan {
879 MachineBasicBlock &MBB;
880 MachineBasicBlock::iterator I, B, E;
882 MachineInstrSpan(MachineBasicBlock::iterator I)
883 : MBB(*I->getParent()),
885 B(I == MBB.begin() ? MBB.end() : std::prev(I)),
888 MachineBasicBlock::iterator begin() {
889 return B == MBB.end() ? MBB.begin() : std::next(B);
891 MachineBasicBlock::iterator end() { return E; }
892 bool empty() { return begin() == end(); }
894 MachineBasicBlock::iterator getInitial() { return I; }
897 } // End llvm namespace