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/ilist.h"
19 #include "llvm/ADT/ilist_node.h"
20 #include "llvm/ADT/iterator_range.h"
21 #include "llvm/ADT/simple_ilist.h"
22 #include "llvm/CodeGen/MachineInstrBundleIterator.h"
23 #include "llvm/CodeGen/MachineInstr.h"
24 #include "llvm/IR/DebugLoc.h"
25 #include "llvm/Support/BranchProbability.h"
26 #include "llvm/MC/LaneBitmask.h"
27 #include "llvm/MC/MCRegisterInfo.h"
38 class MachineFunction;
40 class ModuleSlotTracker;
45 class TargetRegisterClass;
46 class TargetRegisterInfo;
48 template <> struct ilist_traits<MachineInstr> {
50 friend class MachineBasicBlock; // Set by the owning MachineBasicBlock.
52 MachineBasicBlock *Parent;
54 using instr_iterator =
55 simple_ilist<MachineInstr, ilist_sentinel_tracking<true>>::iterator;
58 void addNodeToList(MachineInstr *N);
59 void removeNodeFromList(MachineInstr *N);
60 void transferNodesFromList(ilist_traits &OldList, instr_iterator First,
62 void deleteNode(MachineInstr *MI);
65 class MachineBasicBlock
66 : public ilist_node_with_parent<MachineBasicBlock, MachineFunction> {
68 /// Pair of physical register and lane mask.
69 /// This is not simply a std::pair typedef because the members should be named
70 /// clearly as they both have an integer type.
71 struct RegisterMaskPair {
76 RegisterMaskPair(MCPhysReg PhysReg, LaneBitmask LaneMask)
77 : PhysReg(PhysReg), LaneMask(LaneMask) {}
81 using Instructions = ilist<MachineInstr, ilist_sentinel_tracking<true>>;
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 probabilities to the successors. This vector has the
93 /// same order as Successors, or it is empty if we don't use it (disable
95 std::vector<BranchProbability> Probs;
96 using probability_iterator = std::vector<BranchProbability>::iterator;
97 using const_probability_iterator =
98 std::vector<BranchProbability>::const_iterator;
100 /// Keep track of the physical registers that are livein of the basicblock.
101 using LiveInVector = std::vector<RegisterMaskPair>;
102 LiveInVector LiveIns;
104 /// Alignment of the basic block. Zero if the basic block does not need to be
105 /// aligned. The alignment is specified as log2(bytes).
106 unsigned Alignment = 0;
108 /// Indicate that this basic block is entered via an exception handler.
109 bool IsEHPad = false;
111 /// Indicate that this basic block is potentially the target of an indirect
113 bool AddressTaken = false;
115 /// Indicate that this basic block is the entry block of an EH funclet.
116 bool IsEHFuncletEntry = false;
118 /// Indicate that this basic block is the entry block of a cleanup funclet.
119 bool IsCleanupFuncletEntry = false;
121 /// \brief since getSymbol is a relatively heavy-weight operation, the symbol
122 /// is only computed once and is cached.
123 mutable MCSymbol *CachedMCSymbol = nullptr;
125 // Intrusive list support
126 MachineBasicBlock() = default;
128 explicit MachineBasicBlock(MachineFunction &MF, const BasicBlock *BB);
130 ~MachineBasicBlock();
132 // MachineBasicBlocks are allocated and owned by MachineFunction.
133 friend class MachineFunction;
136 /// Return the LLVM basic block that this instance corresponded to originally.
137 /// Note that this may be NULL if this instance does not correspond directly
138 /// to an LLVM basic block.
139 const BasicBlock *getBasicBlock() const { return BB; }
141 /// Return the name of the corresponding LLVM basic block, or an empty string.
142 StringRef getName() const;
144 /// Return a formatted string to identify this block and its parent function.
145 std::string getFullName() const;
147 /// Test whether this block is potentially the target of an indirect branch.
148 bool hasAddressTaken() const { return AddressTaken; }
150 /// Set this block to reflect that it potentially is the target of an indirect
152 void setHasAddressTaken() { AddressTaken = true; }
154 /// Return the MachineFunction containing this basic block.
155 const MachineFunction *getParent() const { return xParent; }
156 MachineFunction *getParent() { return xParent; }
158 using instr_iterator = Instructions::iterator;
159 using const_instr_iterator = Instructions::const_iterator;
160 using reverse_instr_iterator = Instructions::reverse_iterator;
161 using const_reverse_instr_iterator = Instructions::const_reverse_iterator;
163 using iterator = MachineInstrBundleIterator<MachineInstr>;
164 using const_iterator = MachineInstrBundleIterator<const MachineInstr>;
165 using reverse_iterator = MachineInstrBundleIterator<MachineInstr, true>;
166 using const_reverse_iterator =
167 MachineInstrBundleIterator<const MachineInstr, true>;
169 unsigned size() const { return (unsigned)Insts.size(); }
170 bool empty() const { return Insts.empty(); }
172 MachineInstr &instr_front() { return Insts.front(); }
173 MachineInstr &instr_back() { return Insts.back(); }
174 const MachineInstr &instr_front() const { return Insts.front(); }
175 const MachineInstr &instr_back() const { return Insts.back(); }
177 MachineInstr &front() { return Insts.front(); }
178 MachineInstr &back() { return *--end(); }
179 const MachineInstr &front() const { return Insts.front(); }
180 const MachineInstr &back() const { return *--end(); }
182 instr_iterator instr_begin() { return Insts.begin(); }
183 const_instr_iterator instr_begin() const { return Insts.begin(); }
184 instr_iterator instr_end() { return Insts.end(); }
185 const_instr_iterator instr_end() const { return Insts.end(); }
186 reverse_instr_iterator instr_rbegin() { return Insts.rbegin(); }
187 const_reverse_instr_iterator instr_rbegin() const { return Insts.rbegin(); }
188 reverse_instr_iterator instr_rend () { return Insts.rend(); }
189 const_reverse_instr_iterator instr_rend () const { return Insts.rend(); }
191 using instr_range = iterator_range<instr_iterator>;
192 using const_instr_range = iterator_range<const_instr_iterator>;
193 instr_range instrs() { return instr_range(instr_begin(), instr_end()); }
194 const_instr_range instrs() const {
195 return const_instr_range(instr_begin(), instr_end());
198 iterator begin() { return instr_begin(); }
199 const_iterator begin() const { return instr_begin(); }
200 iterator end () { return instr_end(); }
201 const_iterator end () const { return instr_end(); }
202 reverse_iterator rbegin() {
203 return reverse_iterator::getAtBundleBegin(instr_rbegin());
205 const_reverse_iterator rbegin() const {
206 return const_reverse_iterator::getAtBundleBegin(instr_rbegin());
208 reverse_iterator rend() { return reverse_iterator(instr_rend()); }
209 const_reverse_iterator rend() const {
210 return const_reverse_iterator(instr_rend());
213 /// Support for MachineInstr::getNextNode().
214 static Instructions MachineBasicBlock::*getSublistAccess(MachineInstr *) {
215 return &MachineBasicBlock::Insts;
218 inline iterator_range<iterator> terminators() {
219 return make_range(getFirstTerminator(), end());
221 inline iterator_range<const_iterator> terminators() const {
222 return make_range(getFirstTerminator(), end());
225 // Machine-CFG iterators
226 using pred_iterator = std::vector<MachineBasicBlock *>::iterator;
227 using const_pred_iterator = std::vector<MachineBasicBlock *>::const_iterator;
228 using succ_iterator = std::vector<MachineBasicBlock *>::iterator;
229 using const_succ_iterator = std::vector<MachineBasicBlock *>::const_iterator;
230 using pred_reverse_iterator =
231 std::vector<MachineBasicBlock *>::reverse_iterator;
232 using const_pred_reverse_iterator =
233 std::vector<MachineBasicBlock *>::const_reverse_iterator;
234 using succ_reverse_iterator =
235 std::vector<MachineBasicBlock *>::reverse_iterator;
236 using const_succ_reverse_iterator =
237 std::vector<MachineBasicBlock *>::const_reverse_iterator;
238 pred_iterator pred_begin() { return Predecessors.begin(); }
239 const_pred_iterator pred_begin() const { return Predecessors.begin(); }
240 pred_iterator pred_end() { return Predecessors.end(); }
241 const_pred_iterator pred_end() const { return Predecessors.end(); }
242 pred_reverse_iterator pred_rbegin()
243 { return Predecessors.rbegin();}
244 const_pred_reverse_iterator pred_rbegin() const
245 { return Predecessors.rbegin();}
246 pred_reverse_iterator pred_rend()
247 { return Predecessors.rend(); }
248 const_pred_reverse_iterator pred_rend() const
249 { return Predecessors.rend(); }
250 unsigned pred_size() const {
251 return (unsigned)Predecessors.size();
253 bool pred_empty() const { return Predecessors.empty(); }
254 succ_iterator succ_begin() { return Successors.begin(); }
255 const_succ_iterator succ_begin() const { return Successors.begin(); }
256 succ_iterator succ_end() { return Successors.end(); }
257 const_succ_iterator succ_end() const { return Successors.end(); }
258 succ_reverse_iterator succ_rbegin()
259 { return Successors.rbegin(); }
260 const_succ_reverse_iterator succ_rbegin() const
261 { return Successors.rbegin(); }
262 succ_reverse_iterator succ_rend()
263 { return Successors.rend(); }
264 const_succ_reverse_iterator succ_rend() const
265 { return Successors.rend(); }
266 unsigned succ_size() const {
267 return (unsigned)Successors.size();
269 bool succ_empty() const { return Successors.empty(); }
271 inline iterator_range<pred_iterator> predecessors() {
272 return make_range(pred_begin(), pred_end());
274 inline iterator_range<const_pred_iterator> predecessors() const {
275 return make_range(pred_begin(), pred_end());
277 inline iterator_range<succ_iterator> successors() {
278 return make_range(succ_begin(), succ_end());
280 inline iterator_range<const_succ_iterator> successors() const {
281 return make_range(succ_begin(), succ_end());
284 // LiveIn management methods.
286 /// Adds the specified register as a live in. Note that it is an error to add
287 /// the same register to the same set more than once unless the intention is
288 /// to call sortUniqueLiveIns after all registers are added.
289 void addLiveIn(MCPhysReg PhysReg,
290 LaneBitmask LaneMask = LaneBitmask::getAll()) {
291 LiveIns.push_back(RegisterMaskPair(PhysReg, LaneMask));
293 void addLiveIn(const RegisterMaskPair &RegMaskPair) {
294 LiveIns.push_back(RegMaskPair);
297 /// Sorts and uniques the LiveIns vector. It can be significantly faster to do
298 /// this than repeatedly calling isLiveIn before calling addLiveIn for every
299 /// LiveIn insertion.
300 void sortUniqueLiveIns();
302 /// Clear live in list.
305 /// Add PhysReg as live in to this block, and ensure that there is a copy of
306 /// PhysReg to a virtual register of class RC. Return the virtual register
307 /// that is a copy of the live in PhysReg.
308 unsigned addLiveIn(MCPhysReg PhysReg, const TargetRegisterClass *RC);
310 /// Remove the specified register from the live in set.
311 void removeLiveIn(MCPhysReg Reg,
312 LaneBitmask LaneMask = LaneBitmask::getAll());
314 /// Return true if the specified register is in the live in set.
315 bool isLiveIn(MCPhysReg Reg,
316 LaneBitmask LaneMask = LaneBitmask::getAll()) const;
318 // Iteration support for live in sets. These sets are kept in sorted
319 // order by their register number.
320 using livein_iterator = LiveInVector::const_iterator;
322 /// Unlike livein_begin, this method does not check that the liveness
323 /// information is accurate. Still for debug purposes it may be useful
324 /// to have iterators that won't assert if the liveness information
326 livein_iterator livein_begin_dbg() const { return LiveIns.begin(); }
327 iterator_range<livein_iterator> liveins_dbg() const {
328 return make_range(livein_begin_dbg(), livein_end());
331 livein_iterator livein_begin() const;
332 livein_iterator livein_end() const { return LiveIns.end(); }
333 bool livein_empty() const { return LiveIns.empty(); }
334 iterator_range<livein_iterator> liveins() const {
335 return make_range(livein_begin(), livein_end());
338 /// Get the clobber mask for the start of this basic block. Funclets use this
339 /// to prevent register allocation across funclet transitions.
340 const uint32_t *getBeginClobberMask(const TargetRegisterInfo *TRI) const;
342 /// Get the clobber mask for the end of the basic block.
343 /// \see getBeginClobberMask()
344 const uint32_t *getEndClobberMask(const TargetRegisterInfo *TRI) const;
346 /// Return alignment of the basic block. The alignment is specified as
348 unsigned getAlignment() const { return Alignment; }
350 /// Set alignment of the basic block. The alignment is specified as
352 void setAlignment(unsigned Align) { Alignment = Align; }
354 /// Returns true if the block is a landing pad. That is this basic block is
355 /// entered via an exception handler.
356 bool isEHPad() const { return IsEHPad; }
358 /// Indicates the block is a landing pad. That is this basic block is entered
359 /// via an exception handler.
360 void setIsEHPad(bool V = true) { IsEHPad = V; }
362 bool hasEHPadSuccessor() const;
364 /// Returns true if this is the entry block of an EH funclet.
365 bool isEHFuncletEntry() const { return IsEHFuncletEntry; }
367 /// Indicates if this is the entry block of an EH funclet.
368 void setIsEHFuncletEntry(bool V = true) { IsEHFuncletEntry = V; }
370 /// Returns true if this is the entry block of a cleanup funclet.
371 bool isCleanupFuncletEntry() const { return IsCleanupFuncletEntry; }
373 /// Indicates if this is the entry block of a cleanup funclet.
374 void setIsCleanupFuncletEntry(bool V = true) { IsCleanupFuncletEntry = V; }
376 // Code Layout methods.
378 /// Move 'this' block before or after the specified block. This only moves
379 /// the block, it does not modify the CFG or adjust potential fall-throughs at
380 /// the end of the block.
381 void moveBefore(MachineBasicBlock *NewAfter);
382 void moveAfter(MachineBasicBlock *NewBefore);
384 /// Update the terminator instructions in block to account for changes to the
385 /// layout. If the block previously used a fallthrough, it may now need a
386 /// branch, and if it previously used branching it may now be able to use a
388 void updateTerminator();
390 // Machine-CFG mutators
392 /// Add Succ as a successor of this MachineBasicBlock. The Predecessors list
393 /// of Succ is automatically updated. PROB parameter is stored in
394 /// Probabilities list. The default probability is set as unknown. Mixing
395 /// known and unknown probabilities in successor list is not allowed. When all
396 /// successors have unknown probabilities, 1 / N is returned as the
397 /// probability for each successor, where N is the number of successors.
399 /// Note that duplicate Machine CFG edges are not allowed.
400 void addSuccessor(MachineBasicBlock *Succ,
401 BranchProbability Prob = BranchProbability::getUnknown());
403 /// Add Succ as a successor of this MachineBasicBlock. The Predecessors list
404 /// of Succ is automatically updated. The probability is not provided because
405 /// BPI is not available (e.g. -O0 is used), in which case edge probabilities
406 /// won't be used. Using this interface can save some space.
407 void addSuccessorWithoutProb(MachineBasicBlock *Succ);
409 /// Set successor probability of a given iterator.
410 void setSuccProbability(succ_iterator I, BranchProbability Prob);
412 /// Normalize probabilities of all successors so that the sum of them becomes
413 /// one. This is usually done when the current update on this MBB is done, and
414 /// the sum of its successors' probabilities is not guaranteed to be one. The
415 /// user is responsible for the correct use of this function.
416 /// MBB::removeSuccessor() has an option to do this automatically.
417 void normalizeSuccProbs() {
418 BranchProbability::normalizeProbabilities(Probs.begin(), Probs.end());
421 /// Validate successors' probabilities and check if the sum of them is
422 /// approximate one. This only works in DEBUG mode.
423 void validateSuccProbs() const;
425 /// Remove successor from the successors list of this MachineBasicBlock. The
426 /// Predecessors list of Succ is automatically updated.
427 /// If NormalizeSuccProbs is true, then normalize successors' probabilities
428 /// after the successor is removed.
429 void removeSuccessor(MachineBasicBlock *Succ,
430 bool NormalizeSuccProbs = false);
432 /// Remove specified successor from the successors list of this
433 /// MachineBasicBlock. The Predecessors list of Succ is automatically updated.
434 /// If NormalizeSuccProbs is true, then normalize successors' probabilities
435 /// after the successor is removed.
436 /// Return the iterator to the element after the one removed.
437 succ_iterator removeSuccessor(succ_iterator I,
438 bool NormalizeSuccProbs = false);
440 /// Replace successor OLD with NEW and update probability info.
441 void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New);
443 /// Transfers all the successors from MBB to this machine basic block (i.e.,
444 /// copies all the successors FromMBB and remove all the successors from
446 void transferSuccessors(MachineBasicBlock *FromMBB);
448 /// Transfers all the successors, as in transferSuccessors, and update PHI
449 /// operands in the successor blocks which refer to FromMBB to refer to this.
450 void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB);
452 /// Return true if any of the successors have probabilities attached to them.
453 bool hasSuccessorProbabilities() const { return !Probs.empty(); }
455 /// Return true if the specified MBB is a predecessor of this block.
456 bool isPredecessor(const MachineBasicBlock *MBB) const;
458 /// Return true if the specified MBB is a successor of this block.
459 bool isSuccessor(const MachineBasicBlock *MBB) const;
461 /// Return true if the specified MBB will be emitted immediately after this
462 /// block, such that if this block exits by falling through, control will
463 /// transfer to the specified MBB. Note that MBB need not be a successor at
464 /// all, for example if this block ends with an unconditional branch to some
466 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
468 /// Return the fallthrough block if the block can implicitly
469 /// transfer control to the block after it by falling off the end of
470 /// it. This should return null if it can reach the block after
471 /// it, but it uses an explicit branch to do so (e.g., a table
472 /// jump). Non-null return is a conservative answer.
473 MachineBasicBlock *getFallThrough();
475 /// Return true if the block can implicitly transfer control to the
476 /// block after it by falling off the end of it. This should return
477 /// false if it can reach the block after it, but it uses an
478 /// explicit branch to do so (e.g., a table jump). True is a
479 /// conservative answer.
480 bool canFallThrough();
482 /// Returns a pointer to the first instruction in this block that is not a
483 /// PHINode instruction. When adding instructions to the beginning of the
484 /// basic block, they should be added before the returned value, not before
485 /// the first instruction, which might be PHI.
486 /// Returns end() is there's no non-PHI instruction.
487 iterator getFirstNonPHI();
489 /// Return the first instruction in MBB after I that is not a PHI or a label.
490 /// This is the correct point to insert lowered copies at the beginning of a
491 /// basic block that must be before any debugging information.
492 iterator SkipPHIsAndLabels(iterator I);
494 /// Return the first instruction in MBB after I that is not a PHI, label or
495 /// debug. This is the correct point to insert copies at the beginning of a
497 iterator SkipPHIsLabelsAndDebug(iterator I);
499 /// Returns an iterator to the first terminator instruction of this basic
500 /// block. If a terminator does not exist, it returns end().
501 iterator getFirstTerminator();
502 const_iterator getFirstTerminator() const {
503 return const_cast<MachineBasicBlock *>(this)->getFirstTerminator();
506 /// Same getFirstTerminator but it ignores bundles and return an
507 /// instr_iterator instead.
508 instr_iterator getFirstInstrTerminator();
510 /// Returns an iterator to the first non-debug instruction in the basic block,
512 iterator getFirstNonDebugInstr();
513 const_iterator getFirstNonDebugInstr() const {
514 return const_cast<MachineBasicBlock *>(this)->getFirstNonDebugInstr();
517 /// Returns an iterator to the last non-debug instruction in the basic block,
519 iterator getLastNonDebugInstr();
520 const_iterator getLastNonDebugInstr() const {
521 return const_cast<MachineBasicBlock *>(this)->getLastNonDebugInstr();
524 /// Convenience function that returns true if the block ends in a return
526 bool isReturnBlock() const {
527 return !empty() && back().isReturn();
530 /// Split the critical edge from this block to the given successor block, and
531 /// return the newly created block, or null if splitting is not possible.
533 /// This function updates LiveVariables, MachineDominatorTree, and
534 /// MachineLoopInfo, as applicable.
535 MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *Succ, Pass &P);
537 /// Check if the edge between this block and the given successor \p
538 /// Succ, can be split. If this returns true a subsequent call to
539 /// SplitCriticalEdge is guaranteed to return a valid basic block if
540 /// no changes occured in the meantime.
541 bool canSplitCriticalEdge(const MachineBasicBlock *Succ) const;
543 void pop_front() { Insts.pop_front(); }
544 void pop_back() { Insts.pop_back(); }
545 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
547 /// Insert MI into the instruction list before I, possibly inside a bundle.
549 /// If the insertion point is inside a bundle, MI will be added to the bundle,
550 /// otherwise MI will not be added to any bundle. That means this function
551 /// alone can't be used to prepend or append instructions to bundles. See
552 /// MIBundleBuilder::insert() for a more reliable way of doing that.
553 instr_iterator insert(instr_iterator I, MachineInstr *M);
555 /// Insert a range of instructions into the instruction list before I.
556 template<typename IT>
557 void insert(iterator I, IT S, IT E) {
558 assert((I == end() || I->getParent() == this) &&
559 "iterator points outside of basic block");
560 Insts.insert(I.getInstrIterator(), S, E);
563 /// Insert MI into the instruction list before I.
564 iterator insert(iterator I, MachineInstr *MI) {
565 assert((I == end() || I->getParent() == this) &&
566 "iterator points outside of basic block");
567 assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
568 "Cannot insert instruction with bundle flags");
569 return Insts.insert(I.getInstrIterator(), MI);
572 /// Insert MI into the instruction list after I.
573 iterator insertAfter(iterator I, MachineInstr *MI) {
574 assert((I == end() || I->getParent() == this) &&
575 "iterator points outside of basic block");
576 assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
577 "Cannot insert instruction with bundle flags");
578 return Insts.insertAfter(I.getInstrIterator(), MI);
581 /// Remove an instruction from the instruction list and delete it.
583 /// If the instruction is part of a bundle, the other instructions in the
584 /// bundle will still be bundled after removing the single instruction.
585 instr_iterator erase(instr_iterator I);
587 /// Remove an instruction from the instruction list and delete it.
589 /// If the instruction is part of a bundle, the other instructions in the
590 /// bundle will still be bundled after removing the single instruction.
591 instr_iterator erase_instr(MachineInstr *I) {
592 return erase(instr_iterator(I));
595 /// Remove a range of instructions from the instruction list and delete them.
596 iterator erase(iterator I, iterator E) {
597 return Insts.erase(I.getInstrIterator(), E.getInstrIterator());
600 /// Remove an instruction or bundle from the instruction list and delete it.
602 /// If I points to a bundle of instructions, they are all erased.
603 iterator erase(iterator I) {
604 return erase(I, std::next(I));
607 /// Remove an instruction from the instruction list and delete it.
609 /// If I is the head of a bundle of instructions, the whole bundle will be
611 iterator erase(MachineInstr *I) {
612 return erase(iterator(I));
615 /// Remove the unbundled instruction from the instruction list without
618 /// This function can not be used to remove bundled instructions, use
619 /// remove_instr to remove individual instructions from a bundle.
620 MachineInstr *remove(MachineInstr *I) {
621 assert(!I->isBundled() && "Cannot remove bundled instructions");
622 return Insts.remove(instr_iterator(I));
625 /// Remove the possibly bundled instruction from the instruction list
626 /// without deleting it.
628 /// If the instruction is part of a bundle, the other instructions in the
629 /// bundle will still be bundled after removing the single instruction.
630 MachineInstr *remove_instr(MachineInstr *I);
636 /// Take an instruction from MBB 'Other' at the position From, and insert it
637 /// into this MBB right before 'Where'.
639 /// If From points to a bundle of instructions, the whole bundle is moved.
640 void splice(iterator Where, MachineBasicBlock *Other, iterator From) {
641 // The range splice() doesn't allow noop moves, but this one does.
643 splice(Where, Other, From, std::next(From));
646 /// Take a block of instructions from MBB 'Other' in the range [From, To),
647 /// and insert them into this MBB right before 'Where'.
649 /// The instruction at 'Where' must not be included in the range of
650 /// instructions to move.
651 void splice(iterator Where, MachineBasicBlock *Other,
652 iterator From, iterator To) {
653 Insts.splice(Where.getInstrIterator(), Other->Insts,
654 From.getInstrIterator(), To.getInstrIterator());
657 /// This method unlinks 'this' from the containing function, and returns it,
658 /// but does not delete it.
659 MachineBasicBlock *removeFromParent();
661 /// This method unlinks 'this' from the containing function and deletes it.
662 void eraseFromParent();
664 /// Given a machine basic block that branched to 'Old', change the code and
665 /// CFG so that it branches to 'New' instead.
666 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
668 /// Various pieces of code can cause excess edges in the CFG to be inserted.
669 /// If we have proven that MBB can only branch to DestA and DestB, remove any
670 /// other MBB successors from the CFG. DestA and DestB can be null. Besides
671 /// DestA and DestB, retain other edges leading to LandingPads (currently
672 /// there can be only one; we don't check or require that here). Note it is
673 /// possible that DestA and/or DestB are LandingPads.
674 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
675 MachineBasicBlock *DestB,
678 /// Find the next valid DebugLoc starting at MBBI, skipping any DBG_VALUE
679 /// instructions. Return UnknownLoc if there is none.
680 DebugLoc findDebugLoc(instr_iterator MBBI);
681 DebugLoc findDebugLoc(iterator MBBI) {
682 return findDebugLoc(MBBI.getInstrIterator());
685 /// Find and return the merged DebugLoc of the branch instructions of the
686 /// block. Return UnknownLoc if there is none.
687 DebugLoc findBranchDebugLoc();
689 /// Possible outcome of a register liveness query to computeRegisterLiveness()
690 enum LivenessQueryResult {
691 LQR_Live, ///< Register is known to be (at least partially) live.
692 LQR_Dead, ///< Register is known to be fully dead.
693 LQR_Unknown ///< Register liveness not decidable from local neighborhood.
696 /// Return whether (physical) register \p Reg has been <def>ined and not
697 /// <kill>ed as of just before \p Before.
699 /// Search is localised to a neighborhood of \p Neighborhood instructions
700 /// before (searching for defs or kills) and \p Neighborhood instructions
701 /// after (searching just for defs) \p Before.
703 /// \p Reg must be a physical register.
704 LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI,
706 const_iterator Before,
707 unsigned Neighborhood = 10) const;
709 // Debugging methods.
711 void print(raw_ostream &OS, const SlotIndexes* = nullptr) const;
712 void print(raw_ostream &OS, ModuleSlotTracker &MST,
713 const SlotIndexes* = nullptr) const;
715 // Printing method used by LoopInfo.
716 void printAsOperand(raw_ostream &OS, bool PrintType = true) const;
718 /// MachineBasicBlocks are uniquely numbered at the function level, unless
719 /// they're not in a MachineFunction yet, in which case this will return -1.
720 int getNumber() const { return Number; }
721 void setNumber(int N) { Number = N; }
723 /// Return the MCSymbol for this basic block.
724 MCSymbol *getSymbol() const;
727 /// Return probability iterator corresponding to the I successor iterator.
728 probability_iterator getProbabilityIterator(succ_iterator I);
729 const_probability_iterator
730 getProbabilityIterator(const_succ_iterator I) const;
732 friend class MachineBranchProbabilityInfo;
733 friend class MIPrinter;
735 /// Return probability of the edge from this block to MBB. This method should
736 /// NOT be called directly, but by using getEdgeProbability method from
737 /// MachineBranchProbabilityInfo class.
738 BranchProbability getSuccProbability(const_succ_iterator Succ) const;
740 // Methods used to maintain doubly linked list of blocks...
741 friend struct ilist_callback_traits<MachineBasicBlock>;
743 // Machine-CFG mutators
745 /// Remove Pred as a predecessor of this MachineBasicBlock. Don't do this
746 /// unless you know what you're doing, because it doesn't update Pred's
747 /// successors list. Use Pred->addSuccessor instead.
748 void addPredecessor(MachineBasicBlock *Pred);
750 /// Remove Pred as a predecessor of this MachineBasicBlock. Don't do this
751 /// unless you know what you're doing, because it doesn't update Pred's
752 /// successors list. Use Pred->removeSuccessor instead.
753 void removePredecessor(MachineBasicBlock *Pred);
756 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
758 // This is useful when building IndexedMaps keyed on basic block pointers.
759 struct MBB2NumberFunctor :
760 public std::unary_function<const MachineBasicBlock*, unsigned> {
761 unsigned operator()(const MachineBasicBlock *MBB) const {
762 return MBB->getNumber();
766 //===--------------------------------------------------------------------===//
767 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
768 //===--------------------------------------------------------------------===//
770 // Provide specializations of GraphTraits to be able to treat a
771 // MachineFunction as a graph of MachineBasicBlocks.
774 template <> struct GraphTraits<MachineBasicBlock *> {
775 using NodeRef = MachineBasicBlock *;
776 using ChildIteratorType = MachineBasicBlock::succ_iterator;
778 static NodeRef getEntryNode(MachineBasicBlock *BB) { return BB; }
779 static ChildIteratorType child_begin(NodeRef N) { return N->succ_begin(); }
780 static ChildIteratorType child_end(NodeRef N) { return N->succ_end(); }
783 template <> struct GraphTraits<const MachineBasicBlock *> {
784 using NodeRef = const MachineBasicBlock *;
785 using ChildIteratorType = MachineBasicBlock::const_succ_iterator;
787 static NodeRef getEntryNode(const MachineBasicBlock *BB) { return BB; }
788 static ChildIteratorType child_begin(NodeRef N) { return N->succ_begin(); }
789 static ChildIteratorType child_end(NodeRef N) { return N->succ_end(); }
792 // Provide specializations of GraphTraits to be able to treat a
793 // MachineFunction as a graph of MachineBasicBlocks and to walk it
794 // in inverse order. Inverse order for a function is considered
795 // to be when traversing the predecessor edges of a MBB
796 // instead of the successor edges.
798 template <> struct GraphTraits<Inverse<MachineBasicBlock*>> {
799 using NodeRef = MachineBasicBlock *;
800 using ChildIteratorType = MachineBasicBlock::pred_iterator;
802 static NodeRef getEntryNode(Inverse<MachineBasicBlock *> G) {
806 static ChildIteratorType child_begin(NodeRef N) { return N->pred_begin(); }
807 static ChildIteratorType child_end(NodeRef N) { return N->pred_end(); }
810 template <> struct GraphTraits<Inverse<const MachineBasicBlock*>> {
811 using NodeRef = const MachineBasicBlock *;
812 using ChildIteratorType = MachineBasicBlock::const_pred_iterator;
814 static NodeRef getEntryNode(Inverse<const MachineBasicBlock *> G) {
818 static ChildIteratorType child_begin(NodeRef N) { return N->pred_begin(); }
819 static ChildIteratorType child_end(NodeRef N) { return N->pred_end(); }
822 /// MachineInstrSpan provides an interface to get an iteration range
823 /// containing the instruction it was initialized with, along with all
824 /// those instructions inserted prior to or following that instruction
825 /// at some point after the MachineInstrSpan is constructed.
826 class MachineInstrSpan {
827 MachineBasicBlock &MBB;
828 MachineBasicBlock::iterator I, B, E;
831 MachineInstrSpan(MachineBasicBlock::iterator I)
832 : MBB(*I->getParent()),
834 B(I == MBB.begin() ? MBB.end() : std::prev(I)),
837 MachineBasicBlock::iterator begin() {
838 return B == MBB.end() ? MBB.begin() : std::next(B);
840 MachineBasicBlock::iterator end() { return E; }
841 bool empty() { return begin() == end(); }
843 MachineBasicBlock::iterator getInitial() { return I; }
846 /// Increment \p It until it points to a non-debug instruction or to \p End
847 /// and return the resulting iterator. This function should only be used
848 /// MachineBasicBlock::{iterator, const_iterator, instr_iterator,
849 /// const_instr_iterator} and the respective reverse iterators.
850 template<typename IterT>
851 inline IterT skipDebugInstructionsForward(IterT It, IterT End) {
852 while (It != End && It->isDebugValue())
857 /// Decrement \p It until it points to a non-debug instruction or to \p Begin
858 /// and return the resulting iterator. This function should only be used
859 /// MachineBasicBlock::{iterator, const_iterator, instr_iterator,
860 /// const_instr_iterator} and the respective reverse iterators.
861 template<class IterT>
862 inline IterT skipDebugInstructionsBackward(IterT It, IterT Begin) {
863 while (It != Begin && It->isDebugValue())
868 } // end namespace llvm
870 #endif // LLVM_CODEGEN_MACHINEBASICBLOCK_H