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/CodeGen/MachineInstr.h"
18 #include "llvm/ADT/GraphTraits.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;
75 /// Weights - Keep track of the weights to the successors. This vector
76 /// has the same order as Successors, or it is empty if we don't use it
77 /// (disable optimization).
78 std::vector<uint32_t> Weights;
79 typedef std::vector<uint32_t>::iterator weight_iterator;
80 typedef std::vector<uint32_t>::const_iterator const_weight_iterator;
82 /// LiveIns - Keep track of the physical registers that are livein of
84 std::vector<unsigned> LiveIns;
86 /// Alignment - Alignment of the basic block. Zero if the basic block does
87 /// not need to be aligned.
88 /// The alignment is specified as log2(bytes).
91 /// IsLandingPad - Indicate that this basic block is entered via an
92 /// exception handler.
95 /// AddressTaken - Indicate that this basic block is potentially the
96 /// target of an indirect branch.
99 // Intrusive list support
100 MachineBasicBlock() {}
102 explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
104 ~MachineBasicBlock();
106 // MachineBasicBlocks are allocated and owned by MachineFunction.
107 friend class MachineFunction;
110 /// getBasicBlock - Return the LLVM basic block that this instance
111 /// corresponded to originally. Note that this may be NULL if this instance
112 /// does not correspond directly to an LLVM basic block.
114 const BasicBlock *getBasicBlock() const { return BB; }
116 /// getName - Return the name of the corresponding LLVM basic block, or
118 StringRef getName() const;
120 /// hasAddressTaken - Test whether this block is potentially the target
121 /// of an indirect branch.
122 bool hasAddressTaken() const { return AddressTaken; }
124 /// setHasAddressTaken - Set this block to reflect that it potentially
125 /// is the target of an indirect branch.
126 void setHasAddressTaken() { AddressTaken = true; }
128 /// getParent - Return the MachineFunction containing this basic block.
130 const MachineFunction *getParent() const { return xParent; }
131 MachineFunction *getParent() { return xParent; }
134 /// bundle_iterator - MachineBasicBlock iterator that automatically skips over
135 /// MIs that are inside bundles (i.e. walk top level MIs only).
136 template<typename Ty, typename IterTy>
137 class bundle_iterator
138 : public std::iterator<std::bidirectional_iterator_tag, Ty, ptrdiff_t> {
142 bundle_iterator(IterTy mii) : MII(mii) {
143 assert(!MII->isInsideBundle() &&
144 "It's not legal to initialize bundle_iterator with a bundled MI");
147 bundle_iterator(Ty &mi) : MII(mi) {
148 assert(!mi.isInsideBundle() &&
149 "It's not legal to initialize bundle_iterator with a bundled MI");
151 bundle_iterator(Ty *mi) : MII(mi) {
152 assert((!mi || !mi->isInsideBundle()) &&
153 "It's not legal to initialize bundle_iterator with a bundled MI");
155 bundle_iterator(const bundle_iterator &I) : MII(I.MII) {}
156 bundle_iterator() : MII(0) {}
158 Ty &operator*() const { return *MII; }
159 Ty *operator->() const { return &operator*(); }
161 operator Ty*() const { return MII; }
163 bool operator==(const bundle_iterator &x) const {
166 bool operator!=(const bundle_iterator &x) const {
167 return !operator==(x);
170 // Increment and decrement operators...
171 bundle_iterator &operator--() { // predecrement - Back up
174 } while (MII->isInsideBundle());
177 bundle_iterator &operator++() { // preincrement - Advance
180 } while (MII->isInsideBundle());
183 bundle_iterator operator--(int) { // postdecrement operators...
184 bundle_iterator tmp = *this;
187 } while (MII->isInsideBundle());
190 bundle_iterator operator++(int) { // postincrement operators...
191 bundle_iterator tmp = *this;
194 } while (MII->isInsideBundle());
198 IterTy getInstrIterator() const {
203 typedef Instructions::iterator instr_iterator;
204 typedef Instructions::const_iterator const_instr_iterator;
205 typedef std::reverse_iterator<instr_iterator> reverse_instr_iterator;
207 std::reverse_iterator<const_instr_iterator> const_reverse_instr_iterator;
210 bundle_iterator<MachineInstr,instr_iterator> iterator;
212 bundle_iterator<const MachineInstr,const_instr_iterator> const_iterator;
213 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
214 typedef std::reverse_iterator<iterator> reverse_iterator;
217 unsigned size() const { return (unsigned)Insts.size(); }
218 bool empty() const { return Insts.empty(); }
220 MachineInstr& front() { return Insts.front(); }
221 MachineInstr& back() { return Insts.back(); }
222 const MachineInstr& front() const { return Insts.front(); }
223 const MachineInstr& back() const { return Insts.back(); }
225 instr_iterator instr_begin() { return Insts.begin(); }
226 const_instr_iterator instr_begin() const { return Insts.begin(); }
227 instr_iterator instr_end() { return Insts.end(); }
228 const_instr_iterator instr_end() const { return Insts.end(); }
229 reverse_instr_iterator instr_rbegin() { return Insts.rbegin(); }
230 const_reverse_instr_iterator instr_rbegin() const { return Insts.rbegin(); }
231 reverse_instr_iterator instr_rend () { return Insts.rend(); }
232 const_reverse_instr_iterator instr_rend () const { return Insts.rend(); }
234 iterator begin() { return Insts.begin(); }
235 const_iterator begin() const { return Insts.begin(); }
237 instr_iterator II = instr_end();
238 if (II != instr_begin()) {
239 while (II->isInsideBundle())
244 const_iterator end() const {
245 const_instr_iterator II = instr_end();
246 if (II != instr_begin()) {
247 while (II->isInsideBundle())
252 reverse_iterator rbegin() {
253 reverse_instr_iterator II = instr_rbegin();
254 if (II != instr_rend()) {
255 while (II->isInsideBundle())
260 const_reverse_iterator rbegin() const {
261 const_reverse_instr_iterator II = instr_rbegin();
262 if (II != instr_rend()) {
263 while (II->isInsideBundle())
268 reverse_iterator rend () { return Insts.rend(); }
269 const_reverse_iterator rend () const { return Insts.rend(); }
272 // Machine-CFG iterators
273 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
274 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
275 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
276 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
277 typedef std::vector<MachineBasicBlock *>::reverse_iterator
278 pred_reverse_iterator;
279 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
280 const_pred_reverse_iterator;
281 typedef std::vector<MachineBasicBlock *>::reverse_iterator
282 succ_reverse_iterator;
283 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
284 const_succ_reverse_iterator;
286 pred_iterator pred_begin() { return Predecessors.begin(); }
287 const_pred_iterator pred_begin() const { return Predecessors.begin(); }
288 pred_iterator pred_end() { return Predecessors.end(); }
289 const_pred_iterator pred_end() const { return Predecessors.end(); }
290 pred_reverse_iterator pred_rbegin()
291 { return Predecessors.rbegin();}
292 const_pred_reverse_iterator pred_rbegin() const
293 { return Predecessors.rbegin();}
294 pred_reverse_iterator pred_rend()
295 { return Predecessors.rend(); }
296 const_pred_reverse_iterator pred_rend() const
297 { return Predecessors.rend(); }
298 unsigned pred_size() const {
299 return (unsigned)Predecessors.size();
301 bool pred_empty() const { return Predecessors.empty(); }
302 succ_iterator succ_begin() { return Successors.begin(); }
303 const_succ_iterator succ_begin() const { return Successors.begin(); }
304 succ_iterator succ_end() { return Successors.end(); }
305 const_succ_iterator succ_end() const { return Successors.end(); }
306 succ_reverse_iterator succ_rbegin()
307 { return Successors.rbegin(); }
308 const_succ_reverse_iterator succ_rbegin() const
309 { return Successors.rbegin(); }
310 succ_reverse_iterator succ_rend()
311 { return Successors.rend(); }
312 const_succ_reverse_iterator succ_rend() const
313 { return Successors.rend(); }
314 unsigned succ_size() const {
315 return (unsigned)Successors.size();
317 bool succ_empty() const { return Successors.empty(); }
319 // LiveIn management methods.
321 /// addLiveIn - Add the specified register as a live in. Note that it
322 /// is an error to add the same register to the same set more than once.
323 void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); }
325 /// removeLiveIn - Remove the specified register from the live in set.
327 void removeLiveIn(unsigned Reg);
329 /// isLiveIn - Return true if the specified register is in the live in set.
331 bool isLiveIn(unsigned Reg) const;
333 // Iteration support for live in sets. These sets are kept in sorted
334 // order by their register number.
335 typedef std::vector<unsigned>::const_iterator livein_iterator;
336 livein_iterator livein_begin() const { return LiveIns.begin(); }
337 livein_iterator livein_end() const { return LiveIns.end(); }
338 bool livein_empty() const { return LiveIns.empty(); }
340 /// getAlignment - Return alignment of the basic block.
341 /// The alignment is specified as log2(bytes).
343 unsigned getAlignment() const { return Alignment; }
345 /// setAlignment - Set alignment of the basic block.
346 /// The alignment is specified as log2(bytes).
348 void setAlignment(unsigned Align) { Alignment = Align; }
350 /// isLandingPad - Returns true if the block is a landing pad. That is
351 /// this basic block is entered via an exception handler.
352 bool isLandingPad() const { return IsLandingPad; }
354 /// setIsLandingPad - Indicates the block is a landing pad. That is
355 /// this basic block is entered via an exception handler.
356 void setIsLandingPad(bool V = true) { IsLandingPad = V; }
358 /// getLandingPadSuccessor - If this block has a successor that is a landing
359 /// pad, return it. Otherwise return NULL.
360 const MachineBasicBlock *getLandingPadSuccessor() const;
362 // Code Layout methods.
364 /// moveBefore/moveAfter - move 'this' block before or after the specified
365 /// block. This only moves the block, it does not modify the CFG or adjust
366 /// potential fall-throughs at the end of the block.
367 void moveBefore(MachineBasicBlock *NewAfter);
368 void moveAfter(MachineBasicBlock *NewBefore);
370 /// updateTerminator - Update the terminator instructions in block to account
371 /// for changes to the layout. If the block previously used a fallthrough,
372 /// it may now need a branch, and if it previously used branching it may now
373 /// be able to use a fallthrough.
374 void updateTerminator();
376 // Machine-CFG mutators
378 /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
379 /// The Predecessors list of succ is automatically updated. WEIGHT
380 /// parameter is stored in Weights list and it may be used by
381 /// MachineBranchProbabilityInfo analysis to calculate branch probability.
383 void addSuccessor(MachineBasicBlock *succ, uint32_t weight = 0);
385 /// removeSuccessor - Remove successor from the successors list of this
386 /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
388 void removeSuccessor(MachineBasicBlock *succ);
390 /// removeSuccessor - Remove specified successor from the successors list of
391 /// this MachineBasicBlock. The Predecessors list of succ is automatically
392 /// updated. Return the iterator to the element after the one removed.
394 succ_iterator removeSuccessor(succ_iterator I);
396 /// replaceSuccessor - Replace successor OLD with NEW and update weight info.
398 void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New);
401 /// transferSuccessors - Transfers all the successors from MBB to this
402 /// machine basic block (i.e., copies all the successors fromMBB and
403 /// remove all the successors from fromMBB).
404 void transferSuccessors(MachineBasicBlock *fromMBB);
406 /// transferSuccessorsAndUpdatePHIs - Transfers all the successors, as
407 /// in transferSuccessors, and update PHI operands in the successor blocks
408 /// which refer to fromMBB to refer to this.
409 void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB);
411 /// isSuccessor - Return true if the specified MBB is a successor of this
413 bool isSuccessor(const MachineBasicBlock *MBB) const;
415 /// isLayoutSuccessor - Return true if the specified MBB will be emitted
416 /// immediately after this block, such that if this block exits by
417 /// falling through, control will transfer to the specified MBB. Note
418 /// that MBB need not be a successor at all, for example if this block
419 /// ends with an unconditional branch to some other block.
420 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
422 /// canFallThrough - Return true if the block can implicitly transfer
423 /// control to the block after it by falling off the end of it. This should
424 /// return false if it can reach the block after it, but it uses an explicit
425 /// branch to do so (e.g., a table jump). True is a conservative answer.
426 bool canFallThrough();
428 /// Returns a pointer to the first instructon in this block that is not a
429 /// PHINode instruction. When adding instruction to the beginning of the
430 /// basic block, they should be added before the returned value, not before
431 /// the first instruction, which might be PHI.
432 /// Returns end() is there's no non-PHI instruction.
433 iterator getFirstNonPHI();
435 /// SkipPHIsAndLabels - Return the first instruction in MBB after I that is
436 /// not a PHI or a label. This is the correct point to insert copies at the
437 /// beginning of a basic block.
438 iterator SkipPHIsAndLabels(iterator I);
440 /// getFirstTerminator - returns an iterator to the first terminator
441 /// instruction of this basic block. If a terminator does not exist,
443 iterator getFirstTerminator();
444 const_iterator getFirstTerminator() const;
446 /// getFirstInstrTerminator - Same getFirstTerminator but it ignores bundles
447 /// and return an instr_iterator instead.
448 instr_iterator getFirstInstrTerminator();
450 /// getLastNonDebugInstr - returns an iterator to the last non-debug
451 /// instruction in the basic block, or end()
452 iterator getLastNonDebugInstr();
453 const_iterator getLastNonDebugInstr() const;
455 /// SplitCriticalEdge - Split the critical edge from this block to the
456 /// given successor block, and return the newly created block, or null
457 /// if splitting is not possible.
459 /// This function updates LiveVariables, MachineDominatorTree, and
460 /// MachineLoopInfo, as applicable.
461 MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P);
463 void pop_front() { Insts.pop_front(); }
464 void pop_back() { Insts.pop_back(); }
465 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
467 template<typename IT>
468 void insert(instr_iterator I, IT S, IT E) {
469 Insts.insert(I, S, E);
471 instr_iterator insert(instr_iterator I, MachineInstr *M) {
472 return Insts.insert(I, M);
474 instr_iterator insertAfter(instr_iterator I, MachineInstr *M) {
475 return Insts.insertAfter(I, M);
478 template<typename IT>
479 void insert(iterator I, IT S, IT E) {
480 Insts.insert(I.getInstrIterator(), S, E);
482 iterator insert(iterator I, MachineInstr *M) {
483 return Insts.insert(I.getInstrIterator(), M);
485 iterator insertAfter(iterator I, MachineInstr *M) {
486 return Insts.insertAfter(I.getInstrIterator(), M);
489 /// erase - Remove the specified element or range from the instruction list.
490 /// These functions delete any instructions removed.
492 instr_iterator erase(instr_iterator I) {
493 return Insts.erase(I);
495 instr_iterator erase(instr_iterator I, instr_iterator E) {
496 return Insts.erase(I, E);
498 instr_iterator erase_instr(MachineInstr *I) {
499 instr_iterator MII(I);
503 iterator erase(iterator I);
504 iterator erase(iterator I, iterator E) {
505 return Insts.erase(I.getInstrIterator(), E.getInstrIterator());
507 iterator erase(MachineInstr *I) {
512 /// remove - Remove the instruction from the instruction list. This function
513 /// does not delete the instruction. WARNING: Note, if the specified
514 /// instruction is a bundle this function will remove all the bundled
515 /// instructions as well. It is up to the caller to keep a list of the
516 /// bundled instructions and re-insert them if desired. This function is
517 /// *not recommended* for manipulating instructions with bundles. Use
519 MachineInstr *remove(MachineInstr *I);
524 /// splice - Take an instruction from MBB 'Other' at the position From,
525 /// and insert it into this MBB right before 'where'.
526 void splice(instr_iterator where, MachineBasicBlock *Other,
527 instr_iterator From) {
528 Insts.splice(where, Other->Insts, From);
530 void splice(iterator where, MachineBasicBlock *Other, iterator From);
532 /// splice - Take a block of instructions from MBB 'Other' in the range [From,
533 /// To), and insert them into this MBB right before 'where'.
534 void splice(instr_iterator where, MachineBasicBlock *Other, instr_iterator From,
536 Insts.splice(where, Other->Insts, From, To);
538 void splice(iterator where, MachineBasicBlock *Other, iterator From,
540 Insts.splice(where.getInstrIterator(), Other->Insts,
541 From.getInstrIterator(), To.getInstrIterator());
544 /// removeFromParent - This method unlinks 'this' from the containing
545 /// function, and returns it, but does not delete it.
546 MachineBasicBlock *removeFromParent();
548 /// eraseFromParent - This method unlinks 'this' from the containing
549 /// function and deletes it.
550 void eraseFromParent();
552 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
553 /// 'Old', change the code and CFG so that it branches to 'New' instead.
554 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
556 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
557 /// the CFG to be inserted. If we have proven that MBB can only branch to
558 /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
559 /// DestB can be null. Besides DestA and DestB, retain other edges leading
560 /// to LandingPads (currently there can be only one; we don't check or require
561 /// that here). Note it is possible that DestA and/or DestB are LandingPads.
562 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
563 MachineBasicBlock *DestB,
566 /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
567 /// any DBG_VALUE instructions. Return UnknownLoc if there is none.
568 DebugLoc findDebugLoc(instr_iterator MBBI);
569 DebugLoc findDebugLoc(iterator MBBI) {
570 return findDebugLoc(MBBI.getInstrIterator());
573 // Debugging methods.
575 void print(raw_ostream &OS, SlotIndexes* = 0) const;
577 /// getNumber - MachineBasicBlocks are uniquely numbered at the function
578 /// level, unless they're not in a MachineFunction yet, in which case this
581 int getNumber() const { return Number; }
582 void setNumber(int N) { Number = N; }
584 /// getSymbol - Return the MCSymbol for this basic block.
586 MCSymbol *getSymbol() const;
590 /// getWeightIterator - Return weight iterator corresponding to the I
591 /// successor iterator.
592 weight_iterator getWeightIterator(succ_iterator I);
593 const_weight_iterator getWeightIterator(const_succ_iterator I) const;
595 friend class MachineBranchProbabilityInfo;
597 /// getSuccWeight - Return weight of the edge from this block to MBB. This
598 /// method should NOT be called directly, but by using getEdgeWeight method
599 /// from MachineBranchProbabilityInfo class.
600 uint32_t getSuccWeight(const MachineBasicBlock *succ) const;
603 // Methods used to maintain doubly linked list of blocks...
604 friend struct ilist_traits<MachineBasicBlock>;
606 // Machine-CFG mutators
608 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
609 /// Don't do this unless you know what you're doing, because it doesn't
610 /// update pred's successors list. Use pred->addSuccessor instead.
612 void addPredecessor(MachineBasicBlock *pred);
614 /// removePredecessor - Remove pred as a predecessor of this
615 /// MachineBasicBlock. Don't do this unless you know what you're
616 /// doing, because it doesn't update pred's successors list. Use
617 /// pred->removeSuccessor instead.
619 void removePredecessor(MachineBasicBlock *pred);
622 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
624 void WriteAsOperand(raw_ostream &, const MachineBasicBlock*, bool t);
626 // This is useful when building IndexedMaps keyed on basic block pointers.
627 struct MBB2NumberFunctor :
628 public std::unary_function<const MachineBasicBlock*, unsigned> {
629 unsigned operator()(const MachineBasicBlock *MBB) const {
630 return MBB->getNumber();
634 //===--------------------------------------------------------------------===//
635 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
636 //===--------------------------------------------------------------------===//
638 // Provide specializations of GraphTraits to be able to treat a
639 // MachineFunction as a graph of MachineBasicBlocks...
642 template <> struct GraphTraits<MachineBasicBlock *> {
643 typedef MachineBasicBlock NodeType;
644 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
646 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
647 static inline ChildIteratorType child_begin(NodeType *N) {
648 return N->succ_begin();
650 static inline ChildIteratorType child_end(NodeType *N) {
651 return N->succ_end();
655 template <> struct GraphTraits<const MachineBasicBlock *> {
656 typedef const MachineBasicBlock NodeType;
657 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
659 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
660 static inline ChildIteratorType child_begin(NodeType *N) {
661 return N->succ_begin();
663 static inline ChildIteratorType child_end(NodeType *N) {
664 return N->succ_end();
668 // Provide specializations of GraphTraits to be able to treat a
669 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
670 // in inverse order. Inverse order for a function is considered
671 // to be when traversing the predecessor edges of a MBB
672 // instead of the successor edges.
674 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
675 typedef MachineBasicBlock NodeType;
676 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
677 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
680 static inline ChildIteratorType child_begin(NodeType *N) {
681 return N->pred_begin();
683 static inline ChildIteratorType child_end(NodeType *N) {
684 return N->pred_end();
688 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
689 typedef const MachineBasicBlock NodeType;
690 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
691 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
694 static inline ChildIteratorType child_begin(NodeType *N) {
695 return N->pred_begin();
697 static inline ChildIteratorType child_end(NodeType *N) {
698 return N->pred_end();
702 //===----------------------------------------------------------------------===//
703 // MachineOperand iterator
706 /// MachineOperands - Iterator that can visit all operands on a MachineInstr,
707 /// or all operands on a bundle of MachineInstrs.
711 /// for (MIOperands MIO(MI, true); MIO.isValid(); ++MIO) {
712 /// if (!MIO->isReg())
718 MachineBasicBlock::instr_iterator InstrI, InstrE;
719 MachineInstr::mop_iterator OpI, OpE;
721 // If the operands on InstrI are exhausted, advance InstrI to the next
722 // bundled instruction with operands.
725 // Don't advance off the basic block, or into a new bundle.
726 if (++InstrI == InstrE || !InstrI->isInsideBundle())
728 OpI = InstrI->operands_begin();
729 OpE = InstrI->operands_end();
734 /// MIOperands - Create an iterator that visits all operands on MI, or all
735 /// operands on every instruction in the bundle containing MI.
737 /// @param MI The instruction to examine.
738 /// @param WholeBundle When true, visit all operands on the entire bundle.
740 explicit MIOperands(MachineInstr *MI, bool WholeBundle = false) {
742 InstrI = MI->getBundleStart();
743 InstrE = MI->getParent()->instr_end();
745 InstrI = InstrE = MI;
748 OpI = InstrI->operands_begin();
749 OpE = InstrI->operands_end();
754 /// isValid - Returns true until all the operands have been visited.
755 bool isValid() const { return OpI != OpE; }
757 /// Preincrement. Move to the next operand.
758 MIOperands &operator++() {
759 assert(isValid() && "Cannot advance MIOperands beyond the last operand");
765 MachineOperand &operator* () const { return *OpI; }
766 MachineOperand *operator->() const { return &*OpI; }
769 } // End llvm namespace