1 //===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Collect the sequence of machine instructions for a basic block.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H
15 #define LLVM_CODEGEN_MACHINEBASICBLOCK_H
17 #include "llvm/ADT/GraphTraits.h"
18 #include "llvm/CodeGen/MachineInstr.h"
19 #include "llvm/Support/DataTypes.h"
26 class MachineFunction;
31 class MachineBranchProbabilityInfo;
34 struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
36 mutable ilist_half_node<MachineInstr> Sentinel;
38 // this is only set by the MachineBasicBlock owning the LiveList
39 friend class MachineBasicBlock;
40 MachineBasicBlock* Parent;
43 MachineInstr *createSentinel() const {
44 return static_cast<MachineInstr*>(&Sentinel);
46 void destroySentinel(MachineInstr *) const {}
48 MachineInstr *provideInitialHead() const { return createSentinel(); }
49 MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); }
50 static void noteHead(MachineInstr*, MachineInstr*) {}
52 void addNodeToList(MachineInstr* N);
53 void removeNodeFromList(MachineInstr* N);
54 void transferNodesFromList(ilist_traits &SrcTraits,
55 ilist_iterator<MachineInstr> first,
56 ilist_iterator<MachineInstr> last);
57 void deleteNode(MachineInstr *N);
59 void createNode(const MachineInstr &);
62 class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
63 typedef ilist<MachineInstr> Instructions;
67 MachineFunction *xParent;
69 /// Predecessors/Successors - Keep track of the predecessor / successor
71 std::vector<MachineBasicBlock *> Predecessors;
72 std::vector<MachineBasicBlock *> Successors;
74 /// Weights - Keep track of the weights to the successors. This vector
75 /// has the same order as Successors, or it is empty if we don't use it
76 /// (disable optimization).
77 std::vector<uint32_t> Weights;
78 typedef std::vector<uint32_t>::iterator weight_iterator;
79 typedef std::vector<uint32_t>::const_iterator const_weight_iterator;
81 /// LiveIns - Keep track of the physical registers that are livein of
83 std::vector<unsigned> LiveIns;
85 /// Alignment - Alignment of the basic block. Zero if the basic block does
86 /// not need to be aligned.
87 /// The alignment is specified as log2(bytes).
90 /// IsLandingPad - Indicate that this basic block is entered via an
91 /// exception handler.
94 /// AddressTaken - Indicate that this basic block is potentially the
95 /// target of an indirect branch.
98 /// \brief since getSymbol is a relatively heavy-weight operation, the symbol
99 /// is only computed once and is cached.
100 mutable MCSymbol *CachedMCSymbol;
102 // Intrusive list support
103 MachineBasicBlock() {}
105 explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
107 ~MachineBasicBlock();
109 // MachineBasicBlocks are allocated and owned by MachineFunction.
110 friend class MachineFunction;
113 /// getBasicBlock - Return the LLVM basic block that this instance
114 /// corresponded to originally. Note that this may be NULL if this instance
115 /// does not correspond directly to an LLVM basic block.
117 const BasicBlock *getBasicBlock() const { return BB; }
119 /// getName - Return the name of the corresponding LLVM basic block, or
121 StringRef getName() const;
123 /// getFullName - Return a formatted string to identify this block and its
125 std::string getFullName() const;
127 /// hasAddressTaken - Test whether this block is potentially the target
128 /// of an indirect branch.
129 bool hasAddressTaken() const { return AddressTaken; }
131 /// setHasAddressTaken - Set this block to reflect that it potentially
132 /// is the target of an indirect branch.
133 void setHasAddressTaken() { AddressTaken = true; }
135 /// getParent - Return the MachineFunction containing this basic block.
137 const MachineFunction *getParent() const { return xParent; }
138 MachineFunction *getParent() { return xParent; }
141 /// bundle_iterator - MachineBasicBlock iterator that automatically skips over
142 /// MIs that are inside bundles (i.e. walk top level MIs only).
143 template<typename Ty, typename IterTy>
144 class bundle_iterator
145 : public std::iterator<std::bidirectional_iterator_tag, Ty, ptrdiff_t> {
149 bundle_iterator(IterTy mii) : MII(mii) {}
151 bundle_iterator(Ty &mi) : MII(mi) {
152 assert(!mi.isBundledWithPred() &&
153 "It's not legal to initialize bundle_iterator with a bundled MI");
155 bundle_iterator(Ty *mi) : MII(mi) {
156 assert((!mi || !mi->isBundledWithPred()) &&
157 "It's not legal to initialize bundle_iterator with a bundled MI");
159 // Template allows conversion from const to nonconst.
160 template<class OtherTy, class OtherIterTy>
161 bundle_iterator(const bundle_iterator<OtherTy, OtherIterTy> &I)
162 : MII(I.getInstrIterator()) {}
163 bundle_iterator() : MII(0) {}
165 Ty &operator*() const { return *MII; }
166 Ty *operator->() const { return &operator*(); }
168 operator Ty*() const { return MII; }
170 bool operator==(const bundle_iterator &x) const {
173 bool operator!=(const bundle_iterator &x) const {
174 return !operator==(x);
177 // Increment and decrement operators...
178 bundle_iterator &operator--() { // predecrement - Back up
180 while (MII->isBundledWithPred());
183 bundle_iterator &operator++() { // preincrement - Advance
184 while (MII->isBundledWithSucc())
189 bundle_iterator operator--(int) { // postdecrement operators...
190 bundle_iterator tmp = *this;
194 bundle_iterator operator++(int) { // postincrement operators...
195 bundle_iterator tmp = *this;
200 IterTy getInstrIterator() const {
205 typedef Instructions::iterator instr_iterator;
206 typedef Instructions::const_iterator const_instr_iterator;
207 typedef std::reverse_iterator<instr_iterator> reverse_instr_iterator;
209 std::reverse_iterator<const_instr_iterator> const_reverse_instr_iterator;
212 bundle_iterator<MachineInstr,instr_iterator> iterator;
214 bundle_iterator<const MachineInstr,const_instr_iterator> const_iterator;
215 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
216 typedef std::reverse_iterator<iterator> reverse_iterator;
219 unsigned size() const { return (unsigned)Insts.size(); }
220 bool empty() const { return Insts.empty(); }
222 MachineInstr& front() { return Insts.front(); }
223 MachineInstr& back() { return Insts.back(); }
224 const MachineInstr& front() const { return Insts.front(); }
225 const MachineInstr& back() const { return Insts.back(); }
227 instr_iterator instr_begin() { return Insts.begin(); }
228 const_instr_iterator instr_begin() const { return Insts.begin(); }
229 instr_iterator instr_end() { return Insts.end(); }
230 const_instr_iterator instr_end() const { return Insts.end(); }
231 reverse_instr_iterator instr_rbegin() { return Insts.rbegin(); }
232 const_reverse_instr_iterator instr_rbegin() const { return Insts.rbegin(); }
233 reverse_instr_iterator instr_rend () { return Insts.rend(); }
234 const_reverse_instr_iterator instr_rend () const { return Insts.rend(); }
236 iterator begin() { return instr_begin(); }
237 const_iterator begin() const { return instr_begin(); }
238 iterator end () { return instr_end(); }
239 const_iterator end () const { return instr_end(); }
240 reverse_iterator rbegin() { return instr_rbegin(); }
241 const_reverse_iterator rbegin() const { return instr_rbegin(); }
242 reverse_iterator rend () { return instr_rend(); }
243 const_reverse_iterator rend () const { return instr_rend(); }
246 // Machine-CFG iterators
247 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
248 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
249 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
250 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
251 typedef std::vector<MachineBasicBlock *>::reverse_iterator
252 pred_reverse_iterator;
253 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
254 const_pred_reverse_iterator;
255 typedef std::vector<MachineBasicBlock *>::reverse_iterator
256 succ_reverse_iterator;
257 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
258 const_succ_reverse_iterator;
260 pred_iterator pred_begin() { return Predecessors.begin(); }
261 const_pred_iterator pred_begin() const { return Predecessors.begin(); }
262 pred_iterator pred_end() { return Predecessors.end(); }
263 const_pred_iterator pred_end() const { return Predecessors.end(); }
264 pred_reverse_iterator pred_rbegin()
265 { return Predecessors.rbegin();}
266 const_pred_reverse_iterator pred_rbegin() const
267 { return Predecessors.rbegin();}
268 pred_reverse_iterator pred_rend()
269 { return Predecessors.rend(); }
270 const_pred_reverse_iterator pred_rend() const
271 { return Predecessors.rend(); }
272 unsigned pred_size() const {
273 return (unsigned)Predecessors.size();
275 bool pred_empty() const { return Predecessors.empty(); }
276 succ_iterator succ_begin() { return Successors.begin(); }
277 const_succ_iterator succ_begin() const { return Successors.begin(); }
278 succ_iterator succ_end() { return Successors.end(); }
279 const_succ_iterator succ_end() const { return Successors.end(); }
280 succ_reverse_iterator succ_rbegin()
281 { return Successors.rbegin(); }
282 const_succ_reverse_iterator succ_rbegin() const
283 { return Successors.rbegin(); }
284 succ_reverse_iterator succ_rend()
285 { return Successors.rend(); }
286 const_succ_reverse_iterator succ_rend() const
287 { return Successors.rend(); }
288 unsigned succ_size() const {
289 return (unsigned)Successors.size();
291 bool succ_empty() const { return Successors.empty(); }
293 // LiveIn management methods.
295 /// addLiveIn - Add the specified register as a live in. Note that it
296 /// is an error to add the same register to the same set more than once.
297 void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); }
299 /// Add PhysReg as live in to this block, and ensure that there is a copy of
300 /// PhysReg to a virtual register of class RC. Return the virtual register
301 /// that is a copy of the live in PhysReg.
302 unsigned addLiveIn(unsigned PhysReg, const TargetRegisterClass *RC);
304 /// removeLiveIn - Remove the specified register from the live in set.
306 void removeLiveIn(unsigned Reg);
308 /// isLiveIn - Return true if the specified register is in the live in set.
310 bool isLiveIn(unsigned Reg) const;
312 // Iteration support for live in sets. These sets are kept in sorted
313 // order by their register number.
314 typedef std::vector<unsigned>::const_iterator livein_iterator;
315 livein_iterator livein_begin() const { return LiveIns.begin(); }
316 livein_iterator livein_end() const { return LiveIns.end(); }
317 bool livein_empty() const { return LiveIns.empty(); }
319 /// getAlignment - Return alignment of the basic block.
320 /// The alignment is specified as log2(bytes).
322 unsigned getAlignment() const { return Alignment; }
324 /// setAlignment - Set alignment of the basic block.
325 /// The alignment is specified as log2(bytes).
327 void setAlignment(unsigned Align) { Alignment = Align; }
329 /// isLandingPad - Returns true if the block is a landing pad. That is
330 /// this basic block is entered via an exception handler.
331 bool isLandingPad() const { return IsLandingPad; }
333 /// setIsLandingPad - Indicates the block is a landing pad. That is
334 /// this basic block is entered via an exception handler.
335 void setIsLandingPad(bool V = true) { IsLandingPad = V; }
337 /// getLandingPadSuccessor - If this block has a successor that is a landing
338 /// pad, return it. Otherwise return NULL.
339 const MachineBasicBlock *getLandingPadSuccessor() const;
341 // Code Layout methods.
343 /// moveBefore/moveAfter - move 'this' block before or after the specified
344 /// block. This only moves the block, it does not modify the CFG or adjust
345 /// potential fall-throughs at the end of the block.
346 void moveBefore(MachineBasicBlock *NewAfter);
347 void moveAfter(MachineBasicBlock *NewBefore);
349 /// updateTerminator - Update the terminator instructions in block to account
350 /// for changes to the layout. If the block previously used a fallthrough,
351 /// it may now need a branch, and if it previously used branching it may now
352 /// be able to use a fallthrough.
353 void updateTerminator();
355 // Machine-CFG mutators
357 /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
358 /// The Predecessors list of succ is automatically updated. WEIGHT
359 /// parameter is stored in Weights list and it may be used by
360 /// MachineBranchProbabilityInfo analysis to calculate branch probability.
362 /// Note that duplicate Machine CFG edges are not allowed.
364 void addSuccessor(MachineBasicBlock *succ, uint32_t weight = 0);
366 /// Set successor weight of a given iterator.
367 void setSuccWeight(succ_iterator I, uint32_t weight);
369 /// removeSuccessor - Remove successor from the successors list of this
370 /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
372 void removeSuccessor(MachineBasicBlock *succ);
374 /// removeSuccessor - Remove specified successor from the successors list of
375 /// this MachineBasicBlock. The Predecessors list of succ is automatically
376 /// updated. Return the iterator to the element after the one removed.
378 succ_iterator removeSuccessor(succ_iterator I);
380 /// replaceSuccessor - Replace successor OLD with NEW and update weight info.
382 void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New);
385 /// transferSuccessors - Transfers all the successors from MBB to this
386 /// machine basic block (i.e., copies all the successors fromMBB and
387 /// remove all the successors from fromMBB).
388 void transferSuccessors(MachineBasicBlock *fromMBB);
390 /// transferSuccessorsAndUpdatePHIs - Transfers all the successors, as
391 /// in transferSuccessors, and update PHI operands in the successor blocks
392 /// which refer to fromMBB to refer to this.
393 void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB);
395 /// isPredecessor - Return true if the specified MBB is a predecessor of this
397 bool isPredecessor(const MachineBasicBlock *MBB) const;
399 /// isSuccessor - Return true if the specified MBB is a successor of this
401 bool isSuccessor(const MachineBasicBlock *MBB) const;
403 /// isLayoutSuccessor - Return true if the specified MBB will be emitted
404 /// immediately after this block, such that if this block exits by
405 /// falling through, control will transfer to the specified MBB. Note
406 /// that MBB need not be a successor at all, for example if this block
407 /// ends with an unconditional branch to some other block.
408 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
410 /// canFallThrough - Return true if the block can implicitly transfer
411 /// control to the block after it by falling off the end of it. This should
412 /// return false if it can reach the block after it, but it uses an explicit
413 /// branch to do so (e.g., a table jump). True is a conservative answer.
414 bool canFallThrough();
416 /// Returns a pointer to the first instruction in this block that is not a
417 /// PHINode instruction. When adding instructions to the beginning of the
418 /// basic block, they should be added before the returned value, not before
419 /// the first instruction, which might be PHI.
420 /// Returns end() is there's no non-PHI instruction.
421 iterator getFirstNonPHI();
423 /// SkipPHIsAndLabels - Return the first instruction in MBB after I that is
424 /// not a PHI or a label. This is the correct point to insert copies at the
425 /// beginning of a basic block.
426 iterator SkipPHIsAndLabels(iterator I);
428 /// getFirstTerminator - returns an iterator to the first terminator
429 /// instruction of this basic block. If a terminator does not exist,
431 iterator getFirstTerminator();
432 const_iterator getFirstTerminator() const;
434 /// getFirstInstrTerminator - Same getFirstTerminator but it ignores bundles
435 /// and return an instr_iterator instead.
436 instr_iterator getFirstInstrTerminator();
438 /// getLastNonDebugInstr - returns an iterator to the last non-debug
439 /// instruction in the basic block, or end()
440 iterator getLastNonDebugInstr();
441 const_iterator getLastNonDebugInstr() const;
443 /// SplitCriticalEdge - Split the critical edge from this block to the
444 /// given successor block, and return the newly created block, or null
445 /// if splitting is not possible.
447 /// This function updates LiveVariables, MachineDominatorTree, and
448 /// MachineLoopInfo, as applicable.
449 MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P);
451 void pop_front() { Insts.pop_front(); }
452 void pop_back() { Insts.pop_back(); }
453 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
455 /// Insert MI into the instruction list before I, possibly inside a bundle.
457 /// If the insertion point is inside a bundle, MI will be added to the bundle,
458 /// otherwise MI will not be added to any bundle. That means this function
459 /// alone can't be used to prepend or append instructions to bundles. See
460 /// MIBundleBuilder::insert() for a more reliable way of doing that.
461 instr_iterator insert(instr_iterator I, MachineInstr *M);
463 /// Insert a range of instructions into the instruction list before I.
464 template<typename IT>
465 void insert(iterator I, IT S, IT E) {
466 Insts.insert(I.getInstrIterator(), S, E);
469 /// Insert MI into the instruction list before I.
470 iterator insert(iterator I, MachineInstr *MI) {
471 assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
472 "Cannot insert instruction with bundle flags");
473 return Insts.insert(I.getInstrIterator(), MI);
476 /// Insert MI into the instruction list after I.
477 iterator insertAfter(iterator I, MachineInstr *MI) {
478 assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
479 "Cannot insert instruction with bundle flags");
480 return Insts.insertAfter(I.getInstrIterator(), MI);
483 /// Remove an instruction from the instruction list and delete it.
485 /// If the instruction is part of a bundle, the other instructions in the
486 /// bundle will still be bundled after removing the single instruction.
487 instr_iterator erase(instr_iterator I);
489 /// Remove an instruction from the instruction list and delete it.
491 /// If the instruction is part of a bundle, the other instructions in the
492 /// bundle will still be bundled after removing the single instruction.
493 instr_iterator erase_instr(MachineInstr *I) {
494 return erase(instr_iterator(I));
497 /// Remove a range of instructions from the instruction list and delete them.
498 iterator erase(iterator I, iterator E) {
499 return Insts.erase(I.getInstrIterator(), E.getInstrIterator());
502 /// Remove an instruction or bundle from the instruction list and delete it.
504 /// If I points to a bundle of instructions, they are all erased.
505 iterator erase(iterator I) {
506 return erase(I, llvm::next(I));
509 /// Remove an instruction from the instruction list and delete it.
511 /// If I is the head of a bundle of instructions, the whole bundle will be
513 iterator erase(MachineInstr *I) {
514 return erase(iterator(I));
517 /// Remove the unbundled instruction from the instruction list without
520 /// This function can not be used to remove bundled instructions, use
521 /// remove_instr to remove individual instructions from a bundle.
522 MachineInstr *remove(MachineInstr *I) {
523 assert(!I->isBundled() && "Cannot remove bundled instructions");
524 return Insts.remove(I);
527 /// Remove the possibly bundled instruction from the instruction list
528 /// without deleting it.
530 /// If the instruction is part of a bundle, the other instructions in the
531 /// bundle will still be bundled after removing the single instruction.
532 MachineInstr *remove_instr(MachineInstr *I);
538 /// Take an instruction from MBB 'Other' at the position From, and insert it
539 /// into this MBB right before 'Where'.
541 /// If From points to a bundle of instructions, the whole bundle is moved.
542 void splice(iterator Where, MachineBasicBlock *Other, iterator From) {
543 // The range splice() doesn't allow noop moves, but this one does.
545 splice(Where, Other, From, llvm::next(From));
548 /// Take a block of instructions from MBB 'Other' in the range [From, To),
549 /// and insert them into this MBB right before 'Where'.
551 /// The instruction at 'Where' must not be included in the range of
552 /// instructions to move.
553 void splice(iterator Where, MachineBasicBlock *Other,
554 iterator From, iterator To) {
555 Insts.splice(Where.getInstrIterator(), Other->Insts,
556 From.getInstrIterator(), To.getInstrIterator());
559 /// removeFromParent - This method unlinks 'this' from the containing
560 /// function, and returns it, but does not delete it.
561 MachineBasicBlock *removeFromParent();
563 /// eraseFromParent - This method unlinks 'this' from the containing
564 /// function and deletes it.
565 void eraseFromParent();
567 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
568 /// 'Old', change the code and CFG so that it branches to 'New' instead.
569 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
571 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
572 /// the CFG to be inserted. If we have proven that MBB can only branch to
573 /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
574 /// DestB can be null. Besides DestA and DestB, retain other edges leading
575 /// to LandingPads (currently there can be only one; we don't check or require
576 /// that here). Note it is possible that DestA and/or DestB are LandingPads.
577 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
578 MachineBasicBlock *DestB,
581 /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
582 /// any DBG_VALUE instructions. Return UnknownLoc if there is none.
583 DebugLoc findDebugLoc(instr_iterator MBBI);
584 DebugLoc findDebugLoc(iterator MBBI) {
585 return findDebugLoc(MBBI.getInstrIterator());
588 /// Possible outcome of a register liveness query to computeRegisterLiveness()
589 enum LivenessQueryResult {
590 LQR_Live, ///< Register is known to be live.
591 LQR_OverlappingLive, ///< Register itself is not live, but some overlapping
593 LQR_Dead, ///< Register is known to be dead.
594 LQR_Unknown ///< Register liveness not decidable from local
598 /// computeRegisterLiveness - Return whether (physical) register \c Reg
599 /// has been <def>ined and not <kill>ed as of just before \c MI.
601 /// Search is localised to a neighborhood of
602 /// \c Neighborhood instructions before (searching for defs or kills) and
603 /// Neighborhood instructions after (searching just for defs) MI.
605 /// \c Reg must be a physical register.
606 LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI,
607 unsigned Reg, MachineInstr *MI,
608 unsigned Neighborhood=10);
610 // Debugging methods.
612 void print(raw_ostream &OS, SlotIndexes* = 0) const;
614 // Printing method used by LoopInfo.
615 void printAsOperand(raw_ostream &OS, bool PrintType = true);
617 /// getNumber - MachineBasicBlocks are uniquely numbered at the function
618 /// level, unless they're not in a MachineFunction yet, in which case this
621 int getNumber() const { return Number; }
622 void setNumber(int N) { Number = N; }
624 /// getSymbol - Return the MCSymbol for this basic block.
626 MCSymbol *getSymbol() const;
630 /// getWeightIterator - Return weight iterator corresponding to the I
631 /// successor iterator.
632 weight_iterator getWeightIterator(succ_iterator I);
633 const_weight_iterator getWeightIterator(const_succ_iterator I) const;
635 friend class MachineBranchProbabilityInfo;
637 /// getSuccWeight - Return weight of the edge from this block to MBB. This
638 /// method should NOT be called directly, but by using getEdgeWeight method
639 /// from MachineBranchProbabilityInfo class.
640 uint32_t getSuccWeight(const_succ_iterator Succ) const;
643 // Methods used to maintain doubly linked list of blocks...
644 friend struct ilist_traits<MachineBasicBlock>;
646 // Machine-CFG mutators
648 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
649 /// Don't do this unless you know what you're doing, because it doesn't
650 /// update pred's successors list. Use pred->addSuccessor instead.
652 void addPredecessor(MachineBasicBlock *pred);
654 /// removePredecessor - Remove pred as a predecessor of this
655 /// MachineBasicBlock. Don't do this unless you know what you're
656 /// doing, because it doesn't update pred's successors list. Use
657 /// pred->removeSuccessor instead.
659 void removePredecessor(MachineBasicBlock *pred);
662 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
664 // This is useful when building IndexedMaps keyed on basic block pointers.
665 struct MBB2NumberFunctor :
666 public std::unary_function<const MachineBasicBlock*, unsigned> {
667 unsigned operator()(const MachineBasicBlock *MBB) const {
668 return MBB->getNumber();
672 //===--------------------------------------------------------------------===//
673 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
674 //===--------------------------------------------------------------------===//
676 // Provide specializations of GraphTraits to be able to treat a
677 // MachineFunction as a graph of MachineBasicBlocks...
680 template <> struct GraphTraits<MachineBasicBlock *> {
681 typedef MachineBasicBlock NodeType;
682 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
684 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
685 static inline ChildIteratorType child_begin(NodeType *N) {
686 return N->succ_begin();
688 static inline ChildIteratorType child_end(NodeType *N) {
689 return N->succ_end();
693 template <> struct GraphTraits<const MachineBasicBlock *> {
694 typedef const MachineBasicBlock NodeType;
695 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
697 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
698 static inline ChildIteratorType child_begin(NodeType *N) {
699 return N->succ_begin();
701 static inline ChildIteratorType child_end(NodeType *N) {
702 return N->succ_end();
706 // Provide specializations of GraphTraits to be able to treat a
707 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
708 // in inverse order. Inverse order for a function is considered
709 // to be when traversing the predecessor edges of a MBB
710 // instead of the successor edges.
712 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
713 typedef MachineBasicBlock NodeType;
714 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
715 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
718 static inline ChildIteratorType child_begin(NodeType *N) {
719 return N->pred_begin();
721 static inline ChildIteratorType child_end(NodeType *N) {
722 return N->pred_end();
726 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
727 typedef const MachineBasicBlock NodeType;
728 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
729 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
732 static inline ChildIteratorType child_begin(NodeType *N) {
733 return N->pred_begin();
735 static inline ChildIteratorType child_end(NodeType *N) {
736 return N->pred_end();
742 /// MachineInstrSpan provides an interface to get an iteration range
743 /// containing the instruction it was initialized with, along with all
744 /// those instructions inserted prior to or following that instruction
745 /// at some point after the MachineInstrSpan is constructed.
746 class MachineInstrSpan {
747 MachineBasicBlock &MBB;
748 MachineBasicBlock::iterator I, B, E;
750 MachineInstrSpan(MachineBasicBlock::iterator I)
751 : MBB(*I->getParent()),
753 B(I == MBB.begin() ? MBB.end() : llvm::prior(I)),
756 MachineBasicBlock::iterator begin() {
757 return B == MBB.end() ? MBB.begin() : llvm::next(B);
759 MachineBasicBlock::iterator end() { return E; }
760 bool empty() { return begin() == end(); }
762 MachineBasicBlock::iterator getInitial() { return I; }
765 } // End llvm namespace