1 //===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Collect the sequence of machine instructions for a basic block.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H
15 #define LLVM_CODEGEN_MACHINEBASICBLOCK_H
17 #include "llvm/ADT/GraphTraits.h"
18 #include "llvm/CodeGen/MachineInstr.h"
19 #include "llvm/MC/MCRegisterInfo.h"
20 #include "llvm/Support/DataTypes.h"
27 class MachineFunction;
33 class MachineBranchProbabilityInfo;
36 struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
38 mutable ilist_half_node<MachineInstr> Sentinel;
40 // this is only set by the MachineBasicBlock owning the LiveList
41 friend class MachineBasicBlock;
42 MachineBasicBlock* Parent;
45 MachineInstr *createSentinel() const {
46 return static_cast<MachineInstr*>(&Sentinel);
48 void destroySentinel(MachineInstr *) const {}
50 MachineInstr *provideInitialHead() const { return createSentinel(); }
51 MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); }
52 static void noteHead(MachineInstr*, MachineInstr*) {}
54 void addNodeToList(MachineInstr* N);
55 void removeNodeFromList(MachineInstr* N);
56 void transferNodesFromList(ilist_traits &SrcTraits,
57 ilist_iterator<MachineInstr> first,
58 ilist_iterator<MachineInstr> last);
59 void deleteNode(MachineInstr *N);
61 void createNode(const MachineInstr &);
64 class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
66 /// Pair of physical register and lane mask.
67 /// This is not simply a std::pair typedef because the members should be named
68 /// clearly as they both have an integer type.
69 struct RegisterMaskPair {
74 RegisterMaskPair(MCPhysReg PhysReg, unsigned LaneMask)
75 : PhysReg(PhysReg), LaneMask(LaneMask) {}
79 typedef ilist<MachineInstr> Instructions;
83 MachineFunction *xParent;
85 /// Keep track of the predecessor / successor basic blocks.
86 std::vector<MachineBasicBlock *> Predecessors;
87 std::vector<MachineBasicBlock *> Successors;
89 /// Keep track of the weights to the successors. This vector has the same
90 /// order as Successors, or it is empty if we don't use it (disable
92 std::vector<uint32_t> Weights;
93 typedef std::vector<uint32_t>::iterator weight_iterator;
94 typedef std::vector<uint32_t>::const_iterator const_weight_iterator;
96 /// Keep track of the physical registers that are livein of the basicblock.
97 typedef std::vector<RegisterMaskPair> LiveInVector;
100 /// Alignment of the basic block. Zero if the basic block does not need to be
101 /// aligned. The alignment is specified as log2(bytes).
102 unsigned Alignment = 0;
104 /// Indicate that this basic block is entered via an exception handler.
105 bool IsEHPad = false;
107 /// Indicate that this basic block is potentially the target of an indirect
109 bool AddressTaken = false;
111 /// Indicate that this basic block is the entry block of an EH funclet.
112 bool IsEHFuncletEntry = false;
114 /// \brief since getSymbol is a relatively heavy-weight operation, the symbol
115 /// is only computed once and is cached.
116 mutable MCSymbol *CachedMCSymbol = nullptr;
118 // Intrusive list support
119 MachineBasicBlock() {}
121 explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
123 ~MachineBasicBlock();
125 // MachineBasicBlocks are allocated and owned by MachineFunction.
126 friend class MachineFunction;
129 /// Return the LLVM basic block that this instance corresponded to originally.
130 /// Note that this may be NULL if this instance does not correspond directly
131 /// to an LLVM basic block.
132 const BasicBlock *getBasicBlock() const { return BB; }
134 /// Return the name of the corresponding LLVM basic block, or "(null)".
135 StringRef getName() const;
137 /// Return a formatted string to identify this block and its parent function.
138 std::string getFullName() const;
140 /// Test whether this block is potentially the target of an indirect branch.
141 bool hasAddressTaken() const { return AddressTaken; }
143 /// Set this block to reflect that it potentially is the target of an indirect
145 void setHasAddressTaken() { AddressTaken = true; }
147 /// Return the MachineFunction containing this basic block.
148 const MachineFunction *getParent() const { return xParent; }
149 MachineFunction *getParent() { return xParent; }
151 /// MachineBasicBlock iterator that automatically skips over MIs that are
152 /// inside bundles (i.e. walk top level MIs only).
153 template<typename Ty, typename IterTy>
154 class bundle_iterator
155 : public std::iterator<std::bidirectional_iterator_tag, Ty, ptrdiff_t> {
159 bundle_iterator(IterTy mii) : MII(mii) {}
161 bundle_iterator(Ty &mi) : MII(mi) {
162 assert(!mi.isBundledWithPred() &&
163 "It's not legal to initialize bundle_iterator with a bundled MI");
165 bundle_iterator(Ty *mi) : MII(mi) {
166 assert((!mi || !mi->isBundledWithPred()) &&
167 "It's not legal to initialize bundle_iterator with a bundled MI");
169 // Template allows conversion from const to nonconst.
170 template<class OtherTy, class OtherIterTy>
171 bundle_iterator(const bundle_iterator<OtherTy, OtherIterTy> &I)
172 : MII(I.getInstrIterator()) {}
173 bundle_iterator() : MII(nullptr) {}
175 Ty &operator*() const { return *MII; }
176 Ty *operator->() const { return &operator*(); }
178 operator Ty*() const { return MII; }
180 bool operator==(const bundle_iterator &x) const {
183 bool operator!=(const bundle_iterator &x) const {
184 return !operator==(x);
187 // Increment and decrement operators...
188 bundle_iterator &operator--() { // predecrement - Back up
190 while (MII->isBundledWithPred());
193 bundle_iterator &operator++() { // preincrement - Advance
194 while (MII->isBundledWithSucc())
199 bundle_iterator operator--(int) { // postdecrement operators...
200 bundle_iterator tmp = *this;
204 bundle_iterator operator++(int) { // postincrement operators...
205 bundle_iterator tmp = *this;
210 IterTy getInstrIterator() const {
215 typedef Instructions::iterator instr_iterator;
216 typedef Instructions::const_iterator const_instr_iterator;
217 typedef std::reverse_iterator<instr_iterator> reverse_instr_iterator;
219 std::reverse_iterator<const_instr_iterator> const_reverse_instr_iterator;
222 bundle_iterator<MachineInstr,instr_iterator> iterator;
224 bundle_iterator<const MachineInstr,const_instr_iterator> const_iterator;
225 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
226 typedef std::reverse_iterator<iterator> reverse_iterator;
229 unsigned size() const { return (unsigned)Insts.size(); }
230 bool empty() const { return Insts.empty(); }
232 MachineInstr &instr_front() { return Insts.front(); }
233 MachineInstr &instr_back() { return Insts.back(); }
234 const MachineInstr &instr_front() const { return Insts.front(); }
235 const MachineInstr &instr_back() const { return Insts.back(); }
237 MachineInstr &front() { return Insts.front(); }
238 MachineInstr &back() { return *--end(); }
239 const MachineInstr &front() const { return Insts.front(); }
240 const MachineInstr &back() const { return *--end(); }
242 instr_iterator instr_begin() { return Insts.begin(); }
243 const_instr_iterator instr_begin() const { return Insts.begin(); }
244 instr_iterator instr_end() { return Insts.end(); }
245 const_instr_iterator instr_end() const { return Insts.end(); }
246 reverse_instr_iterator instr_rbegin() { return Insts.rbegin(); }
247 const_reverse_instr_iterator instr_rbegin() const { return Insts.rbegin(); }
248 reverse_instr_iterator instr_rend () { return Insts.rend(); }
249 const_reverse_instr_iterator instr_rend () const { return Insts.rend(); }
251 iterator begin() { return instr_begin(); }
252 const_iterator begin() const { return instr_begin(); }
253 iterator end () { return instr_end(); }
254 const_iterator end () const { return instr_end(); }
255 reverse_iterator rbegin() { return instr_rbegin(); }
256 const_reverse_iterator rbegin() const { return instr_rbegin(); }
257 reverse_iterator rend () { return instr_rend(); }
258 const_reverse_iterator rend () const { return instr_rend(); }
260 inline iterator_range<iterator> terminators() {
261 return iterator_range<iterator>(getFirstTerminator(), end());
263 inline iterator_range<const_iterator> terminators() const {
264 return iterator_range<const_iterator>(getFirstTerminator(), end());
267 // Machine-CFG iterators
268 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
269 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
270 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
271 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
272 typedef std::vector<MachineBasicBlock *>::reverse_iterator
273 pred_reverse_iterator;
274 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
275 const_pred_reverse_iterator;
276 typedef std::vector<MachineBasicBlock *>::reverse_iterator
277 succ_reverse_iterator;
278 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
279 const_succ_reverse_iterator;
280 pred_iterator pred_begin() { return Predecessors.begin(); }
281 const_pred_iterator pred_begin() const { return Predecessors.begin(); }
282 pred_iterator pred_end() { return Predecessors.end(); }
283 const_pred_iterator pred_end() const { return Predecessors.end(); }
284 pred_reverse_iterator pred_rbegin()
285 { return Predecessors.rbegin();}
286 const_pred_reverse_iterator pred_rbegin() const
287 { return Predecessors.rbegin();}
288 pred_reverse_iterator pred_rend()
289 { return Predecessors.rend(); }
290 const_pred_reverse_iterator pred_rend() const
291 { return Predecessors.rend(); }
292 unsigned pred_size() const {
293 return (unsigned)Predecessors.size();
295 bool pred_empty() const { return Predecessors.empty(); }
296 succ_iterator succ_begin() { return Successors.begin(); }
297 const_succ_iterator succ_begin() const { return Successors.begin(); }
298 succ_iterator succ_end() { return Successors.end(); }
299 const_succ_iterator succ_end() const { return Successors.end(); }
300 succ_reverse_iterator succ_rbegin()
301 { return Successors.rbegin(); }
302 const_succ_reverse_iterator succ_rbegin() const
303 { return Successors.rbegin(); }
304 succ_reverse_iterator succ_rend()
305 { return Successors.rend(); }
306 const_succ_reverse_iterator succ_rend() const
307 { return Successors.rend(); }
308 unsigned succ_size() const {
309 return (unsigned)Successors.size();
311 bool succ_empty() const { return Successors.empty(); }
313 inline iterator_range<pred_iterator> predecessors() {
314 return iterator_range<pred_iterator>(pred_begin(), pred_end());
316 inline iterator_range<const_pred_iterator> predecessors() const {
317 return iterator_range<const_pred_iterator>(pred_begin(), pred_end());
319 inline iterator_range<succ_iterator> successors() {
320 return iterator_range<succ_iterator>(succ_begin(), succ_end());
322 inline iterator_range<const_succ_iterator> successors() const {
323 return iterator_range<const_succ_iterator>(succ_begin(), succ_end());
326 // LiveIn management methods.
328 /// Adds the specified register as a live in. Note that it is an error to add
329 /// the same register to the same set more than once unless the intention is
330 /// to call sortUniqueLiveIns after all registers are added.
331 void addLiveIn(MCPhysReg PhysReg, unsigned LaneMask = ~0u) {
332 LiveIns.push_back(RegisterMaskPair(PhysReg, LaneMask));
334 void addLiveIn(const RegisterMaskPair &RegMaskPair) {
335 LiveIns.push_back(RegMaskPair);
338 /// Sorts and uniques the LiveIns vector. It can be significantly faster to do
339 /// this than repeatedly calling isLiveIn before calling addLiveIn for every
340 /// LiveIn insertion.
341 void sortUniqueLiveIns();
343 /// Add PhysReg as live in to this block, and ensure that there is a copy of
344 /// PhysReg to a virtual register of class RC. Return the virtual register
345 /// that is a copy of the live in PhysReg.
346 unsigned addLiveIn(MCPhysReg PhysReg, const TargetRegisterClass *RC);
348 /// Remove the specified register from the live in set.
349 void removeLiveIn(MCPhysReg Reg, unsigned LaneMask = ~0u);
351 /// Return true if the specified register is in the live in set.
352 bool isLiveIn(MCPhysReg Reg, unsigned LaneMask = ~0u) const;
354 // Iteration support for live in sets. These sets are kept in sorted
355 // order by their register number.
356 typedef LiveInVector::const_iterator livein_iterator;
357 livein_iterator livein_begin() const { return LiveIns.begin(); }
358 livein_iterator livein_end() const { return LiveIns.end(); }
359 bool livein_empty() const { return LiveIns.empty(); }
360 iterator_range<livein_iterator> liveins() const {
361 return make_range(livein_begin(), livein_end());
364 /// Return alignment of the basic block. The alignment is specified as
366 unsigned getAlignment() const { return Alignment; }
368 /// Set alignment of the basic block. The alignment is specified as
370 void setAlignment(unsigned Align) { Alignment = Align; }
372 /// Returns true if the block is a landing pad. That is this basic block is
373 /// entered via an exception handler.
374 bool isEHPad() const { return IsEHPad; }
376 /// Indicates the block is a landing pad. That is this basic block is entered
377 /// via an exception handler.
378 void setIsEHPad(bool V = true) { IsEHPad = V; }
380 /// If this block has a successor that is a landing pad, return it. Otherwise
382 const MachineBasicBlock *getLandingPadSuccessor() const;
384 /// Returns true if this is the entry block of an EH funclet.
385 bool isEHFuncletEntry() const { return IsEHFuncletEntry; }
387 /// Indicates if this is the entry block of an EH funclet.
388 void setIsEHFuncletEntry(bool V = true) { IsEHFuncletEntry = V; }
390 // Code Layout methods.
392 /// Move 'this' block before or after the specified block. This only moves
393 /// the block, it does not modify the CFG or adjust potential fall-throughs at
394 /// the end of the block.
395 void moveBefore(MachineBasicBlock *NewAfter);
396 void moveAfter(MachineBasicBlock *NewBefore);
398 /// Update the terminator instructions in block to account for changes to the
399 /// layout. If the block previously used a fallthrough, it may now need a
400 /// branch, and if it previously used branching it may now be able to use a
402 void updateTerminator();
404 // Machine-CFG mutators
406 /// Add succ as a successor of this MachineBasicBlock. The Predecessors list
407 /// of succ is automatically updated. WEIGHT parameter is stored in Weights
408 /// list and it may be used by MachineBranchProbabilityInfo analysis to
409 /// calculate branch probability.
411 /// Note that duplicate Machine CFG edges are not allowed.
412 void addSuccessor(MachineBasicBlock *succ, uint32_t weight = 0);
414 /// Set successor weight of a given iterator.
415 void setSuccWeight(succ_iterator I, uint32_t weight);
417 /// Remove successor from the successors list of this MachineBasicBlock. The
418 /// Predecessors list of succ is automatically updated.
419 void removeSuccessor(MachineBasicBlock *succ);
421 /// Remove specified successor from the successors list of this
422 /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
423 /// Return the iterator to the element after the one removed.
424 succ_iterator removeSuccessor(succ_iterator I);
426 /// Replace successor OLD with NEW and update weight info.
427 void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New);
429 /// Transfers all the successors from MBB to this machine basic block (i.e.,
430 /// copies all the successors fromMBB and remove all the successors from
432 void transferSuccessors(MachineBasicBlock *fromMBB);
434 /// Transfers all the successors, as in transferSuccessors, and update PHI
435 /// operands in the successor blocks which refer to fromMBB to refer to this.
436 void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB);
438 /// Return true if any of the successors have weights attached to them.
439 bool hasSuccessorWeights() const { return !Weights.empty(); }
441 /// Return true if the specified MBB is a predecessor of this block.
442 bool isPredecessor(const MachineBasicBlock *MBB) const;
444 /// Return true if the specified MBB is a successor of this block.
445 bool isSuccessor(const MachineBasicBlock *MBB) const;
447 /// Return true if the specified MBB will be emitted immediately after this
448 /// block, such that if this block exits by falling through, control will
449 /// transfer to the specified MBB. Note that MBB need not be a successor at
450 /// all, for example if this block ends with an unconditional branch to some
452 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
454 /// Return true if the block can implicitly transfer control to the block
455 /// after it by falling off the end of it. This should return false if it can
456 /// reach the block after it, but it uses an explicit branch to do so (e.g., a
457 /// table jump). True is a conservative answer.
458 bool canFallThrough();
460 /// Returns a pointer to the first instruction in this block that is not a
461 /// PHINode instruction. When adding instructions to the beginning of the
462 /// basic block, they should be added before the returned value, not before
463 /// the first instruction, which might be PHI.
464 /// Returns end() is there's no non-PHI instruction.
465 iterator getFirstNonPHI();
467 /// Return the first instruction in MBB after I that is not a PHI or a label.
468 /// This is the correct point to insert copies at the beginning of a basic
470 iterator SkipPHIsAndLabels(iterator I);
472 /// Returns an iterator to the first terminator instruction of this basic
473 /// block. If a terminator does not exist, it returns end().
474 iterator getFirstTerminator();
475 const_iterator getFirstTerminator() const {
476 return const_cast<MachineBasicBlock *>(this)->getFirstTerminator();
479 /// Same getFirstTerminator but it ignores bundles and return an
480 /// instr_iterator instead.
481 instr_iterator getFirstInstrTerminator();
483 /// Returns an iterator to the first non-debug instruction in the basic block,
485 iterator getFirstNonDebugInstr();
486 const_iterator getFirstNonDebugInstr() const {
487 return const_cast<MachineBasicBlock *>(this)->getFirstNonDebugInstr();
490 /// Returns an iterator to the last non-debug instruction in the basic block,
492 iterator getLastNonDebugInstr();
493 const_iterator getLastNonDebugInstr() const {
494 return const_cast<MachineBasicBlock *>(this)->getLastNonDebugInstr();
497 /// Split the critical edge from this block to the given successor block, and
498 /// return the newly created block, or null if splitting is not possible.
500 /// This function updates LiveVariables, MachineDominatorTree, and
501 /// MachineLoopInfo, as applicable.
502 MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P);
504 void pop_front() { Insts.pop_front(); }
505 void pop_back() { Insts.pop_back(); }
506 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
508 /// Insert MI into the instruction list before I, possibly inside a bundle.
510 /// If the insertion point is inside a bundle, MI will be added to the bundle,
511 /// otherwise MI will not be added to any bundle. That means this function
512 /// alone can't be used to prepend or append instructions to bundles. See
513 /// MIBundleBuilder::insert() for a more reliable way of doing that.
514 instr_iterator insert(instr_iterator I, MachineInstr *M);
516 /// Insert a range of instructions into the instruction list before I.
517 template<typename IT>
518 void insert(iterator I, IT S, IT E) {
519 assert((I == end() || I->getParent() == this) &&
520 "iterator points outside of basic block");
521 Insts.insert(I.getInstrIterator(), S, E);
524 /// Insert MI into the instruction list before I.
525 iterator insert(iterator I, MachineInstr *MI) {
526 assert((I == end() || I->getParent() == this) &&
527 "iterator points outside of basic block");
528 assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
529 "Cannot insert instruction with bundle flags");
530 return Insts.insert(I.getInstrIterator(), MI);
533 /// Insert MI into the instruction list after I.
534 iterator insertAfter(iterator I, MachineInstr *MI) {
535 assert((I == end() || I->getParent() == this) &&
536 "iterator points outside of basic block");
537 assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
538 "Cannot insert instruction with bundle flags");
539 return Insts.insertAfter(I.getInstrIterator(), MI);
542 /// Remove an instruction from the instruction list and delete it.
544 /// If the instruction is part of a bundle, the other instructions in the
545 /// bundle will still be bundled after removing the single instruction.
546 instr_iterator erase(instr_iterator I);
548 /// Remove an instruction from the instruction list and delete it.
550 /// If the instruction is part of a bundle, the other instructions in the
551 /// bundle will still be bundled after removing the single instruction.
552 instr_iterator erase_instr(MachineInstr *I) {
553 return erase(instr_iterator(I));
556 /// Remove a range of instructions from the instruction list and delete them.
557 iterator erase(iterator I, iterator E) {
558 return Insts.erase(I.getInstrIterator(), E.getInstrIterator());
561 /// Remove an instruction or bundle from the instruction list and delete it.
563 /// If I points to a bundle of instructions, they are all erased.
564 iterator erase(iterator I) {
565 return erase(I, std::next(I));
568 /// Remove an instruction from the instruction list and delete it.
570 /// If I is the head of a bundle of instructions, the whole bundle will be
572 iterator erase(MachineInstr *I) {
573 return erase(iterator(I));
576 /// Remove the unbundled instruction from the instruction list without
579 /// This function can not be used to remove bundled instructions, use
580 /// remove_instr to remove individual instructions from a bundle.
581 MachineInstr *remove(MachineInstr *I) {
582 assert(!I->isBundled() && "Cannot remove bundled instructions");
583 return Insts.remove(I);
586 /// Remove the possibly bundled instruction from the instruction list
587 /// without deleting 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 MachineInstr *remove_instr(MachineInstr *I);
597 /// Take an instruction from MBB 'Other' at the position From, and insert it
598 /// into this MBB right before 'Where'.
600 /// If From points to a bundle of instructions, the whole bundle is moved.
601 void splice(iterator Where, MachineBasicBlock *Other, iterator From) {
602 // The range splice() doesn't allow noop moves, but this one does.
604 splice(Where, Other, From, std::next(From));
607 /// Take a block of instructions from MBB 'Other' in the range [From, To),
608 /// and insert them into this MBB right before 'Where'.
610 /// The instruction at 'Where' must not be included in the range of
611 /// instructions to move.
612 void splice(iterator Where, MachineBasicBlock *Other,
613 iterator From, iterator To) {
614 Insts.splice(Where.getInstrIterator(), Other->Insts,
615 From.getInstrIterator(), To.getInstrIterator());
618 /// This method unlinks 'this' from the containing function, and returns it,
619 /// but does not delete it.
620 MachineBasicBlock *removeFromParent();
622 /// This method unlinks 'this' from the containing function and deletes it.
623 void eraseFromParent();
625 /// Given a machine basic block that branched to 'Old', change the code and
626 /// CFG so that it branches to 'New' instead.
627 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
629 /// Various pieces of code can cause excess edges in the CFG to be inserted.
630 /// If we have proven that MBB can only branch to DestA and DestB, remove any
631 /// other MBB successors from the CFG. DestA and DestB can be null. Besides
632 /// DestA and DestB, retain other edges leading to LandingPads (currently
633 /// there can be only one; we don't check or require that here). Note it is
634 /// possible that DestA and/or DestB are LandingPads.
635 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
636 MachineBasicBlock *DestB,
639 /// Find the next valid DebugLoc starting at MBBI, skipping any DBG_VALUE
640 /// instructions. Return UnknownLoc if there is none.
641 DebugLoc findDebugLoc(instr_iterator MBBI);
642 DebugLoc findDebugLoc(iterator MBBI) {
643 return findDebugLoc(MBBI.getInstrIterator());
646 /// Possible outcome of a register liveness query to computeRegisterLiveness()
647 enum LivenessQueryResult {
648 LQR_Live, ///< Register is known to be live.
649 LQR_OverlappingLive, ///< Register itself is not live, but some overlapping
651 LQR_Dead, ///< Register is known to be dead.
652 LQR_Unknown ///< Register liveness not decidable from local
656 /// Return whether (physical) register \p Reg has been <def>ined and not
657 /// <kill>ed as of just before \p Before.
659 /// Search is localised to a neighborhood of \p Neighborhood instructions
660 /// before (searching for defs or kills) and \p Neighborhood instructions
661 /// after (searching just for defs) \p Before.
663 /// \p Reg must be a physical register.
664 LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI,
666 const_iterator Before,
667 unsigned Neighborhood=10) const;
669 // Debugging methods.
671 void print(raw_ostream &OS, SlotIndexes* = nullptr) const;
672 void print(raw_ostream &OS, ModuleSlotTracker &MST,
673 SlotIndexes * = nullptr) const;
675 // Printing method used by LoopInfo.
676 void printAsOperand(raw_ostream &OS, bool PrintType = true) const;
678 /// MachineBasicBlocks are uniquely numbered at the function level, unless
679 /// they're not in a MachineFunction yet, in which case this will return -1.
680 int getNumber() const { return Number; }
681 void setNumber(int N) { Number = N; }
683 /// Return the MCSymbol for this basic block.
684 MCSymbol *getSymbol() const;
688 /// Return weight iterator corresponding to the I successor iterator.
689 weight_iterator getWeightIterator(succ_iterator I);
690 const_weight_iterator getWeightIterator(const_succ_iterator I) const;
692 friend class MachineBranchProbabilityInfo;
693 friend class MIPrinter;
695 /// Return weight of the edge from this block to MBB. This method should NOT
696 /// be called directly, but by using getEdgeWeight method from
697 /// MachineBranchProbabilityInfo class.
698 uint32_t getSuccWeight(const_succ_iterator Succ) const;
701 // Methods used to maintain doubly linked list of blocks...
702 friend struct ilist_traits<MachineBasicBlock>;
704 // Machine-CFG mutators
706 /// Remove pred as a predecessor of this MachineBasicBlock. Don't do this
707 /// unless you know what you're doing, because it doesn't update pred's
708 /// successors list. Use pred->addSuccessor instead.
709 void addPredecessor(MachineBasicBlock *pred);
711 /// Remove pred as a predecessor of this MachineBasicBlock. Don't do this
712 /// unless you know what you're doing, because it doesn't update pred's
713 /// successors list. Use pred->removeSuccessor instead.
714 void removePredecessor(MachineBasicBlock *pred);
717 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
719 // This is useful when building IndexedMaps keyed on basic block pointers.
720 struct MBB2NumberFunctor :
721 public std::unary_function<const MachineBasicBlock*, unsigned> {
722 unsigned operator()(const MachineBasicBlock *MBB) const {
723 return MBB->getNumber();
727 //===--------------------------------------------------------------------===//
728 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
729 //===--------------------------------------------------------------------===//
731 // Provide specializations of GraphTraits to be able to treat a
732 // MachineFunction as a graph of MachineBasicBlocks.
735 template <> struct GraphTraits<MachineBasicBlock *> {
736 typedef MachineBasicBlock NodeType;
737 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
739 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
740 static inline ChildIteratorType child_begin(NodeType *N) {
741 return N->succ_begin();
743 static inline ChildIteratorType child_end(NodeType *N) {
744 return N->succ_end();
748 template <> struct GraphTraits<const MachineBasicBlock *> {
749 typedef const MachineBasicBlock NodeType;
750 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
752 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
753 static inline ChildIteratorType child_begin(NodeType *N) {
754 return N->succ_begin();
756 static inline ChildIteratorType child_end(NodeType *N) {
757 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 NodeType;
769 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
770 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
773 static inline ChildIteratorType child_begin(NodeType *N) {
774 return N->pred_begin();
776 static inline ChildIteratorType child_end(NodeType *N) {
777 return N->pred_end();
781 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
782 typedef const MachineBasicBlock NodeType;
783 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
784 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
787 static inline ChildIteratorType child_begin(NodeType *N) {
788 return N->pred_begin();
790 static inline ChildIteratorType child_end(NodeType *N) {
791 return N->pred_end();
797 /// MachineInstrSpan provides an interface to get an iteration range
798 /// containing the instruction it was initialized with, along with all
799 /// those instructions inserted prior to or following that instruction
800 /// at some point after the MachineInstrSpan is constructed.
801 class MachineInstrSpan {
802 MachineBasicBlock &MBB;
803 MachineBasicBlock::iterator I, B, E;
805 MachineInstrSpan(MachineBasicBlock::iterator I)
806 : MBB(*I->getParent()),
808 B(I == MBB.begin() ? MBB.end() : std::prev(I)),
811 MachineBasicBlock::iterator begin() {
812 return B == MBB.end() ? MBB.begin() : std::next(B);
814 MachineBasicBlock::iterator end() { return E; }
815 bool empty() { return begin() == end(); }
817 MachineBasicBlock::iterator getInitial() { return I; }
820 } // End llvm namespace