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/ADT/iterator_range.h"
19 #include "llvm/CodeGen/MachineInstrBundleIterator.h"
20 #include "llvm/CodeGen/MachineInstr.h"
21 #include "llvm/Support/BranchProbability.h"
22 #include "llvm/MC/LaneBitmask.h"
23 #include "llvm/MC/MCRegisterInfo.h"
24 #include "llvm/Support/DataTypes.h"
31 class MachineFunction;
37 class MachineBranchProbabilityInfo;
39 template <> struct ilist_traits<MachineInstr> {
41 friend class MachineBasicBlock; // Set by the owning MachineBasicBlock.
42 MachineBasicBlock *Parent;
44 typedef simple_ilist<MachineInstr, ilist_sentinel_tracking<true>>::iterator
48 void addNodeToList(MachineInstr *N);
49 void removeNodeFromList(MachineInstr *N);
50 void transferNodesFromList(ilist_traits &OldList, instr_iterator First,
53 void deleteNode(MachineInstr *MI);
56 class MachineBasicBlock
57 : public ilist_node_with_parent<MachineBasicBlock, MachineFunction> {
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.
62 struct RegisterMaskPair {
67 RegisterMaskPair(MCPhysReg PhysReg, LaneBitmask LaneMask)
68 : PhysReg(PhysReg), LaneMask(LaneMask) {}
72 typedef ilist<MachineInstr, ilist_sentinel_tracking<true>> Instructions;
76 MachineFunction *xParent;
78 /// Keep track of the predecessor / successor basic blocks.
79 std::vector<MachineBasicBlock *> Predecessors;
80 std::vector<MachineBasicBlock *> Successors;
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
85 std::vector<BranchProbability> Probs;
86 typedef std::vector<BranchProbability>::iterator probability_iterator;
87 typedef std::vector<BranchProbability>::const_iterator
88 const_probability_iterator;
90 /// Keep track of the physical registers that are livein of the basicblock.
91 typedef std::vector<RegisterMaskPair> LiveInVector;
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;
98 /// Indicate that this basic block is entered via an exception handler.
101 /// Indicate that this basic block is potentially the target of an indirect
103 bool AddressTaken = false;
105 /// Indicate that this basic block is the entry block of an EH funclet.
106 bool IsEHFuncletEntry = false;
108 /// Indicate that this basic block is the entry block of a cleanup funclet.
109 bool IsCleanupFuncletEntry = false;
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;
115 // Intrusive list support
116 MachineBasicBlock() {}
118 explicit MachineBasicBlock(MachineFunction &MF, const BasicBlock *BB);
120 ~MachineBasicBlock();
122 // MachineBasicBlocks are allocated and owned by MachineFunction.
123 friend class MachineFunction;
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; }
131 /// Return the name of the corresponding LLVM basic block, or "(null)".
132 StringRef getName() const;
134 /// Return a formatted string to identify this block and its parent function.
135 std::string getFullName() const;
137 /// Test whether this block is potentially the target of an indirect branch.
138 bool hasAddressTaken() const { return AddressTaken; }
140 /// Set this block to reflect that it potentially is the target of an indirect
142 void setHasAddressTaken() { AddressTaken = true; }
144 /// Return the MachineFunction containing this basic block.
145 const MachineFunction *getParent() const { return xParent; }
146 MachineFunction *getParent() { return xParent; }
148 typedef Instructions::iterator instr_iterator;
149 typedef Instructions::const_iterator const_instr_iterator;
150 typedef Instructions::reverse_iterator reverse_instr_iterator;
151 typedef Instructions::const_reverse_iterator const_reverse_instr_iterator;
153 typedef MachineInstrBundleIterator<MachineInstr> iterator;
154 typedef MachineInstrBundleIterator<const MachineInstr> const_iterator;
155 typedef MachineInstrBundleIterator<MachineInstr, true> reverse_iterator;
156 typedef MachineInstrBundleIterator<const MachineInstr, true>
157 const_reverse_iterator;
159 unsigned size() const { return (unsigned)Insts.size(); }
160 bool empty() const { return Insts.empty(); }
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(); }
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(); }
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(); }
181 typedef iterator_range<instr_iterator> instr_range;
182 typedef iterator_range<const_instr_iterator> const_instr_range;
183 instr_range instrs() { return instr_range(instr_begin(), instr_end()); }
184 const_instr_range instrs() const {
185 return const_instr_range(instr_begin(), instr_end());
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(); }
192 reverse_iterator rbegin() {
193 return reverse_iterator::getAtBundleBegin(instr_rbegin());
195 const_reverse_iterator rbegin() const {
196 return const_reverse_iterator::getAtBundleBegin(instr_rbegin());
198 reverse_iterator rend() { return reverse_iterator(instr_rend()); }
199 const_reverse_iterator rend() const {
200 return const_reverse_iterator(instr_rend());
203 /// Support for MachineInstr::getNextNode().
204 static Instructions MachineBasicBlock::*getSublistAccess(MachineInstr *) {
205 return &MachineBasicBlock::Insts;
208 inline iterator_range<iterator> terminators() {
209 return make_range(getFirstTerminator(), end());
211 inline iterator_range<const_iterator> terminators() const {
212 return make_range(getFirstTerminator(), end());
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
221 pred_reverse_iterator;
222 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
223 const_pred_reverse_iterator;
224 typedef std::vector<MachineBasicBlock *>::reverse_iterator
225 succ_reverse_iterator;
226 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
227 const_succ_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(); }
232 pred_reverse_iterator pred_rbegin()
233 { return Predecessors.rbegin();}
234 const_pred_reverse_iterator pred_rbegin() const
235 { return Predecessors.rbegin();}
236 pred_reverse_iterator pred_rend()
237 { return Predecessors.rend(); }
238 const_pred_reverse_iterator pred_rend() const
239 { return Predecessors.rend(); }
240 unsigned pred_size() const {
241 return (unsigned)Predecessors.size();
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(); }
248 succ_reverse_iterator succ_rbegin()
249 { return Successors.rbegin(); }
250 const_succ_reverse_iterator succ_rbegin() const
251 { return Successors.rbegin(); }
252 succ_reverse_iterator succ_rend()
253 { return Successors.rend(); }
254 const_succ_reverse_iterator succ_rend() const
255 { return Successors.rend(); }
256 unsigned succ_size() const {
257 return (unsigned)Successors.size();
259 bool succ_empty() const { return Successors.empty(); }
261 inline iterator_range<pred_iterator> predecessors() {
262 return make_range(pred_begin(), pred_end());
264 inline iterator_range<const_pred_iterator> predecessors() const {
265 return make_range(pred_begin(), pred_end());
267 inline iterator_range<succ_iterator> successors() {
268 return make_range(succ_begin(), succ_end());
270 inline iterator_range<const_succ_iterator> successors() const {
271 return make_range(succ_begin(), succ_end());
274 // LiveIn management methods.
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));
283 void addLiveIn(const RegisterMaskPair &RegMaskPair) {
284 LiveIns.push_back(RegMaskPair);
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();
292 /// Clear live in list.
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);
300 /// Remove the specified register from the live in set.
301 void removeLiveIn(MCPhysReg Reg,
302 LaneBitmask LaneMask = LaneBitmask::getAll());
304 /// Return true if the specified register is in the live in set.
305 bool isLiveIn(MCPhysReg Reg,
306 LaneBitmask LaneMask = LaneBitmask::getAll()) const;
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 livein_iterator livein_begin() const { return LiveIns.begin(); }
312 livein_iterator livein_end() const { return LiveIns.end(); }
313 bool livein_empty() const { return LiveIns.empty(); }
314 iterator_range<livein_iterator> liveins() const {
315 return make_range(livein_begin(), livein_end());
318 /// Get the clobber mask for the start of this basic block. Funclets use this
319 /// to prevent register allocation across funclet transitions.
320 const uint32_t *getBeginClobberMask(const TargetRegisterInfo *TRI) const;
322 /// Get the clobber mask for the end of the basic block.
323 /// \see getBeginClobberMask()
324 const uint32_t *getEndClobberMask(const TargetRegisterInfo *TRI) const;
326 /// Return alignment of the basic block. The alignment is specified as
328 unsigned getAlignment() const { return Alignment; }
330 /// Set alignment of the basic block. The alignment is specified as
332 void setAlignment(unsigned Align) { Alignment = Align; }
334 /// Returns true if the block is a landing pad. That is this basic block is
335 /// entered via an exception handler.
336 bool isEHPad() const { return IsEHPad; }
338 /// Indicates the block is a landing pad. That is this basic block is entered
339 /// via an exception handler.
340 void setIsEHPad(bool V = true) { IsEHPad = V; }
342 bool hasEHPadSuccessor() const;
344 /// Returns true if this is the entry block of an EH funclet.
345 bool isEHFuncletEntry() const { return IsEHFuncletEntry; }
347 /// Indicates if this is the entry block of an EH funclet.
348 void setIsEHFuncletEntry(bool V = true) { IsEHFuncletEntry = V; }
350 /// Returns true if this is the entry block of a cleanup funclet.
351 bool isCleanupFuncletEntry() const { return IsCleanupFuncletEntry; }
353 /// Indicates if this is the entry block of a cleanup funclet.
354 void setIsCleanupFuncletEntry(bool V = true) { IsCleanupFuncletEntry = V; }
356 // Code Layout methods.
358 /// Move 'this' block before or after the specified block. This only moves
359 /// the block, it does not modify the CFG or adjust potential fall-throughs at
360 /// the end of the block.
361 void moveBefore(MachineBasicBlock *NewAfter);
362 void moveAfter(MachineBasicBlock *NewBefore);
364 /// Update the terminator instructions in block to account for changes to the
365 /// layout. If the block previously used a fallthrough, it may now need a
366 /// branch, and if it previously used branching it may now be able to use a
368 void updateTerminator();
370 // Machine-CFG mutators
372 /// Add Succ as a successor of this MachineBasicBlock. The Predecessors list
373 /// of Succ is automatically updated. PROB parameter is stored in
374 /// Probabilities list. The default probability is set as unknown. Mixing
375 /// known and unknown probabilities in successor list is not allowed. When all
376 /// successors have unknown probabilities, 1 / N is returned as the
377 /// probability for each successor, where N is the number of successors.
379 /// Note that duplicate Machine CFG edges are not allowed.
380 void addSuccessor(MachineBasicBlock *Succ,
381 BranchProbability Prob = BranchProbability::getUnknown());
383 /// Add Succ as a successor of this MachineBasicBlock. The Predecessors list
384 /// of Succ is automatically updated. The probability is not provided because
385 /// BPI is not available (e.g. -O0 is used), in which case edge probabilities
386 /// won't be used. Using this interface can save some space.
387 void addSuccessorWithoutProb(MachineBasicBlock *Succ);
389 /// Set successor probability of a given iterator.
390 void setSuccProbability(succ_iterator I, BranchProbability Prob);
392 /// Normalize probabilities of all successors so that the sum of them becomes
393 /// one. This is usually done when the current update on this MBB is done, and
394 /// the sum of its successors' probabilities is not guaranteed to be one. The
395 /// user is responsible for the correct use of this function.
396 /// MBB::removeSuccessor() has an option to do this automatically.
397 void normalizeSuccProbs() {
398 BranchProbability::normalizeProbabilities(Probs.begin(), Probs.end());
401 /// Validate successors' probabilities and check if the sum of them is
402 /// approximate one. This only works in DEBUG mode.
403 void validateSuccProbs() const;
405 /// Remove successor from the successors list of this MachineBasicBlock. The
406 /// Predecessors list of Succ is automatically updated.
407 /// If NormalizeSuccProbs is true, then normalize successors' probabilities
408 /// after the successor is removed.
409 void removeSuccessor(MachineBasicBlock *Succ,
410 bool NormalizeSuccProbs = false);
412 /// Remove specified successor from the successors list of this
413 /// MachineBasicBlock. The Predecessors list of Succ is automatically updated.
414 /// If NormalizeSuccProbs is true, then normalize successors' probabilities
415 /// after the successor is removed.
416 /// Return the iterator to the element after the one removed.
417 succ_iterator removeSuccessor(succ_iterator I,
418 bool NormalizeSuccProbs = false);
420 /// Replace successor OLD with NEW and update probability info.
421 void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New);
423 /// Transfers all the successors from MBB to this machine basic block (i.e.,
424 /// copies all the successors FromMBB and remove all the successors from
426 void transferSuccessors(MachineBasicBlock *FromMBB);
428 /// Transfers all the successors, as in transferSuccessors, and update PHI
429 /// operands in the successor blocks which refer to FromMBB to refer to this.
430 void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB);
432 /// Return true if any of the successors have probabilities attached to them.
433 bool hasSuccessorProbabilities() const { return !Probs.empty(); }
435 /// Return true if the specified MBB is a predecessor of this block.
436 bool isPredecessor(const MachineBasicBlock *MBB) const;
438 /// Return true if the specified MBB is a successor of this block.
439 bool isSuccessor(const MachineBasicBlock *MBB) const;
441 /// Return true if the specified MBB will be emitted immediately after this
442 /// block, such that if this block exits by falling through, control will
443 /// transfer to the specified MBB. Note that MBB need not be a successor at
444 /// all, for example if this block ends with an unconditional branch to some
446 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
448 /// Return true if the block can implicitly transfer control to the block
449 /// after it by falling off the end of it. This should return false if it can
450 /// reach the block after it, but it uses an explicit branch to do so (e.g., a
451 /// table jump). True is a conservative answer.
452 bool canFallThrough();
454 /// Returns a pointer to the first instruction in this block that is not a
455 /// PHINode instruction. When adding instructions to the beginning of the
456 /// basic block, they should be added before the returned value, not before
457 /// the first instruction, which might be PHI.
458 /// Returns end() is there's no non-PHI instruction.
459 iterator getFirstNonPHI();
461 /// Return the first instruction in MBB after I that is not a PHI or a label.
462 /// This is the correct point to insert lowered copies at the beginning of a
463 /// basic block that must be before any debugging information.
464 iterator SkipPHIsAndLabels(iterator I);
466 /// Return the first instruction in MBB after I that is not a PHI, label or
467 /// debug. This is the correct point to insert copies at the beginning of a
469 iterator SkipPHIsLabelsAndDebug(iterator I);
471 /// Returns an iterator to the first terminator instruction of this basic
472 /// block. If a terminator does not exist, it returns end().
473 iterator getFirstTerminator();
474 const_iterator getFirstTerminator() const {
475 return const_cast<MachineBasicBlock *>(this)->getFirstTerminator();
478 /// Same getFirstTerminator but it ignores bundles and return an
479 /// instr_iterator instead.
480 instr_iterator getFirstInstrTerminator();
482 /// Returns an iterator to the first non-debug instruction in the basic block,
484 iterator getFirstNonDebugInstr();
485 const_iterator getFirstNonDebugInstr() const {
486 return const_cast<MachineBasicBlock *>(this)->getFirstNonDebugInstr();
489 /// Returns an iterator to the last non-debug instruction in the basic block,
491 iterator getLastNonDebugInstr();
492 const_iterator getLastNonDebugInstr() const {
493 return const_cast<MachineBasicBlock *>(this)->getLastNonDebugInstr();
496 /// Convenience function that returns true if the block ends in a return
498 bool isReturnBlock() const {
499 return !empty() && back().isReturn();
502 /// Split the critical edge from this block to the given successor block, and
503 /// return the newly created block, or null if splitting is not possible.
505 /// This function updates LiveVariables, MachineDominatorTree, and
506 /// MachineLoopInfo, as applicable.
507 MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *Succ, Pass &P);
509 /// Check if the edge between this block and the given successor \p
510 /// Succ, can be split. If this returns true a subsequent call to
511 /// SplitCriticalEdge is guaranteed to return a valid basic block if
512 /// no changes occured in the meantime.
513 bool canSplitCriticalEdge(const MachineBasicBlock *Succ) const;
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(instr_iterator(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 (at least partially) live.
660 LQR_Dead, ///< Register is known to be fully dead.
661 LQR_Unknown ///< Register liveness not decidable from local neighborhood.
664 /// Return whether (physical) register \p Reg has been <def>ined and not
665 /// <kill>ed as of just before \p Before.
667 /// Search is localised to a neighborhood of \p Neighborhood instructions
668 /// before (searching for defs or kills) and \p Neighborhood instructions
669 /// after (searching just for defs) \p Before.
671 /// \p Reg must be a physical register.
672 LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI,
674 const_iterator Before,
675 unsigned Neighborhood=10) const;
677 // Debugging methods.
679 void print(raw_ostream &OS, const SlotIndexes* = nullptr) const;
680 void print(raw_ostream &OS, ModuleSlotTracker &MST,
681 const SlotIndexes* = nullptr) const;
683 // Printing method used by LoopInfo.
684 void printAsOperand(raw_ostream &OS, bool PrintType = true) const;
686 /// MachineBasicBlocks are uniquely numbered at the function level, unless
687 /// they're not in a MachineFunction yet, in which case this will return -1.
688 int getNumber() const { return Number; }
689 void setNumber(int N) { Number = N; }
691 /// Return the MCSymbol for this basic block.
692 MCSymbol *getSymbol() const;
696 /// Return probability iterator corresponding to the I successor iterator.
697 probability_iterator getProbabilityIterator(succ_iterator I);
698 const_probability_iterator
699 getProbabilityIterator(const_succ_iterator I) const;
701 friend class MachineBranchProbabilityInfo;
702 friend class MIPrinter;
704 /// Return probability of the edge from this block to MBB. This method should
705 /// NOT be called directly, but by using getEdgeProbability method from
706 /// MachineBranchProbabilityInfo class.
707 BranchProbability getSuccProbability(const_succ_iterator Succ) const;
709 // Methods used to maintain doubly linked list of blocks...
710 friend struct ilist_callback_traits<MachineBasicBlock>;
712 // Machine-CFG mutators
714 /// Remove Pred as a predecessor of this MachineBasicBlock. Don't do this
715 /// unless you know what you're doing, because it doesn't update Pred's
716 /// successors list. Use Pred->addSuccessor instead.
717 void addPredecessor(MachineBasicBlock *Pred);
719 /// Remove Pred as a predecessor of this MachineBasicBlock. Don't do this
720 /// unless you know what you're doing, because it doesn't update Pred's
721 /// successors list. Use Pred->removeSuccessor instead.
722 void removePredecessor(MachineBasicBlock *Pred);
725 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
727 // This is useful when building IndexedMaps keyed on basic block pointers.
728 struct MBB2NumberFunctor :
729 public std::unary_function<const MachineBasicBlock*, unsigned> {
730 unsigned operator()(const MachineBasicBlock *MBB) const {
731 return MBB->getNumber();
735 //===--------------------------------------------------------------------===//
736 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
737 //===--------------------------------------------------------------------===//
739 // Provide specializations of GraphTraits to be able to treat a
740 // MachineFunction as a graph of MachineBasicBlocks.
743 template <> struct GraphTraits<MachineBasicBlock *> {
744 typedef MachineBasicBlock *NodeRef;
745 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
747 static NodeRef getEntryNode(MachineBasicBlock *BB) { return BB; }
748 static ChildIteratorType child_begin(NodeRef N) { return N->succ_begin(); }
749 static ChildIteratorType child_end(NodeRef N) { return N->succ_end(); }
752 template <> struct GraphTraits<const MachineBasicBlock *> {
753 typedef const MachineBasicBlock *NodeRef;
754 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
756 static NodeRef getEntryNode(const MachineBasicBlock *BB) { return BB; }
757 static ChildIteratorType child_begin(NodeRef N) { return N->succ_begin(); }
758 static ChildIteratorType child_end(NodeRef N) { return N->succ_end(); }
761 // Provide specializations of GraphTraits to be able to treat a
762 // MachineFunction as a graph of MachineBasicBlocks and to walk it
763 // in inverse order. Inverse order for a function is considered
764 // to be when traversing the predecessor edges of a MBB
765 // instead of the successor edges.
767 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
768 typedef MachineBasicBlock *NodeRef;
769 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
770 static NodeRef getEntryNode(Inverse<MachineBasicBlock *> G) {
773 static ChildIteratorType child_begin(NodeRef N) { return N->pred_begin(); }
774 static ChildIteratorType child_end(NodeRef N) { return N->pred_end(); }
777 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
778 typedef const MachineBasicBlock *NodeRef;
779 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
780 static NodeRef getEntryNode(Inverse<const MachineBasicBlock *> G) {
783 static ChildIteratorType child_begin(NodeRef N) { return N->pred_begin(); }
784 static ChildIteratorType child_end(NodeRef N) { return N->pred_end(); }
789 /// MachineInstrSpan provides an interface to get an iteration range
790 /// containing the instruction it was initialized with, along with all
791 /// those instructions inserted prior to or following that instruction
792 /// at some point after the MachineInstrSpan is constructed.
793 class MachineInstrSpan {
794 MachineBasicBlock &MBB;
795 MachineBasicBlock::iterator I, B, E;
797 MachineInstrSpan(MachineBasicBlock::iterator I)
798 : MBB(*I->getParent()),
800 B(I == MBB.begin() ? MBB.end() : std::prev(I)),
803 MachineBasicBlock::iterator begin() {
804 return B == MBB.end() ? MBB.begin() : std::next(B);
806 MachineBasicBlock::iterator end() { return E; }
807 bool empty() { return begin() == end(); }
809 MachineBasicBlock::iterator getInitial() { return I; }
812 /// Increment \p It until it points to a non-debug instruction or to \p End
813 /// and return the resulting iterator. This function should only be used
814 /// MachineBasicBlock::{iterator, const_iterator, instr_iterator,
815 /// const_instr_iterator} and the respective reverse iterators.
816 template<typename IterT>
817 inline IterT skipDebugInstructionsForward(IterT It, IterT End) {
818 while (It != End && It->isDebugValue())
823 /// Decrement \p It until it points to a non-debug instruction or to \p Begin
824 /// and return the resulting iterator. This function should only be used
825 /// MachineBasicBlock::{iterator, const_iterator, instr_iterator,
826 /// const_instr_iterator} and the respective reverse iterators.
827 template<class IterT>
828 inline IterT skipDebugInstructionsBackward(IterT It, IterT Begin) {
829 while (It != Begin && It->isDebugValue())
834 } // End llvm namespace