OSDN Git Service

First chunk of MachineInstr bundle support.
[android-x86/external-llvm.git] / include / llvm / CodeGen / MachineBasicBlock.h
1 //===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Collect the sequence of machine instructions for a basic block.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H
15 #define LLVM_CODEGEN_MACHINEBASICBLOCK_H
16
17 #include "llvm/CodeGen/MachineInstr.h"
18 #include "llvm/ADT/GraphTraits.h"
19 #include "llvm/Support/DataTypes.h"
20 #include <functional>
21
22 namespace llvm {
23
24 class Pass;
25 class BasicBlock;
26 class MachineFunction;
27 class MCSymbol;
28 class SlotIndexes;
29 class StringRef;
30 class raw_ostream;
31 class MachineBranchProbabilityInfo;
32
33 template <>
34 struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
35 private:
36   mutable ilist_half_node<MachineInstr> Sentinel;
37
38   // this is only set by the MachineBasicBlock owning the LiveList
39   friend class MachineBasicBlock;
40   MachineBasicBlock* Parent;
41
42 public:
43   MachineInstr *createSentinel() const {
44     return static_cast<MachineInstr*>(&Sentinel);
45   }
46   void destroySentinel(MachineInstr *) const {}
47
48   MachineInstr *provideInitialHead() const { return createSentinel(); }
49   MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); }
50   static void noteHead(MachineInstr*, MachineInstr*) {}
51
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);
58 private:
59   void createNode(const MachineInstr &);
60 };
61
62 class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
63   typedef ilist<MachineInstr> Instructions;
64   Instructions Insts;
65   const BasicBlock *BB;
66   int Number;
67   MachineFunction *xParent;
68
69   /// Predecessors/Successors - Keep track of the predecessor / successor
70   /// basicblocks.
71   std::vector<MachineBasicBlock *> Predecessors;
72   std::vector<MachineBasicBlock *> Successors;
73
74
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
81   /// LiveIns - Keep track of the physical registers that are livein of
82   /// the basicblock.
83   std::vector<unsigned> LiveIns;
84
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).
88   unsigned Alignment;
89
90   /// IsLandingPad - Indicate that this basic block is entered via an
91   /// exception handler.
92   bool IsLandingPad;
93
94   /// AddressTaken - Indicate that this basic block is potentially the
95   /// target of an indirect branch.
96   bool AddressTaken;
97
98   // Intrusive list support
99   MachineBasicBlock() {}
100
101   explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
102
103   ~MachineBasicBlock();
104
105   // MachineBasicBlocks are allocated and owned by MachineFunction.
106   friend class MachineFunction;
107
108 public:
109   /// getBasicBlock - Return the LLVM basic block that this instance
110   /// corresponded to originally. Note that this may be NULL if this instance
111   /// does not correspond directly to an LLVM basic block.
112   ///
113   const BasicBlock *getBasicBlock() const { return BB; }
114
115   /// getName - Return the name of the corresponding LLVM basic block, or
116   /// "(null)".
117   StringRef getName() const;
118
119   /// hasAddressTaken - Test whether this block is potentially the target
120   /// of an indirect branch.
121   bool hasAddressTaken() const { return AddressTaken; }
122
123   /// setHasAddressTaken - Set this block to reflect that it potentially
124   /// is the target of an indirect branch.
125   void setHasAddressTaken() { AddressTaken = true; }
126
127   /// getParent - Return the MachineFunction containing this basic block.
128   ///
129   const MachineFunction *getParent() const { return xParent; }
130   MachineFunction *getParent() { return xParent; }
131
132
133   /// bundle_iterator - MachineBasicBlock iterator that automatically skips over
134   /// MIs that are inside bundles (i.e. walk top level MIs only).
135   template<typename Ty, typename IterTy>
136   class bundle_iterator
137     : public std::iterator<std::bidirectional_iterator_tag, Ty, ptrdiff_t> {
138     IterTy MII;
139
140   public:
141     bundle_iterator(IterTy mii) : MII(mii) {
142       assert(!MII->isInsideBundle() &&
143              "It's not legal to initialize bundle_iterator with a bundled MI");
144     }
145
146     bundle_iterator(Ty &mi) : MII(mi) {
147       assert(!mi.isInsideBundle() &&
148              "It's not legal to initialize bundle_iterator with a bundled MI");
149     }
150     bundle_iterator(Ty *mi) : MII(mi) {
151       assert((!mi || !mi->isInsideBundle()) &&
152              "It's not legal to initialize bundle_iterator with a bundled MI");
153     }
154     bundle_iterator(const bundle_iterator &I) : MII(I.MII) {}
155     bundle_iterator() : MII(0) {}
156
157     Ty &operator*() const { return *MII; }
158     Ty *operator->() const { return &operator*(); }
159
160     operator Ty*() const { return MII; }
161
162     bool operator==(const bundle_iterator &x) const {
163       return MII == x.MII;
164     }
165     bool operator!=(const bundle_iterator &x) const {
166       return !operator==(x);
167     }
168     
169     // Increment and decrement operators...
170     bundle_iterator &operator--() {      // predecrement - Back up
171       do {
172         --MII;
173       } while (MII->isInsideBundle());
174       return *this;
175     }
176     bundle_iterator &operator++() {      // preincrement - Advance
177       do {
178         ++MII;
179       } while (MII->isInsideBundle());
180       return *this;
181     }
182     bundle_iterator operator--(int) {    // postdecrement operators...
183       bundle_iterator tmp = *this;
184       do {
185         --MII;
186       } while (MII->isInsideBundle());
187       return tmp;
188     }
189     bundle_iterator operator++(int) {    // postincrement operators...
190       bundle_iterator tmp = *this;
191       do {
192         ++MII;
193       } while (MII->isInsideBundle());
194       return tmp;
195     }
196
197     IterTy getInsnIterator() const {
198       return MII;
199     }    
200   };
201
202   typedef Instructions::iterator                                  insn_iterator;
203   typedef Instructions::const_iterator                      const_insn_iterator;
204   typedef std::reverse_iterator<insn_iterator>            reverse_insn_iterator;
205   typedef
206   std::reverse_iterator<const_insn_iterator>        const_reverse_insn_iterator;
207
208   typedef
209   bundle_iterator<MachineInstr,insn_iterator>                          iterator;
210   typedef
211   bundle_iterator<const MachineInstr,const_insn_iterator>        const_iterator;
212   typedef std::reverse_iterator<const_iterator>          const_reverse_iterator;
213   typedef std::reverse_iterator<iterator>                      reverse_iterator;
214
215
216   unsigned size() const { return (unsigned)Insts.size(); }
217   bool empty() const { return Insts.empty(); }
218
219   MachineInstr& front() { return Insts.front(); }
220   MachineInstr& back()  { return Insts.back(); }
221   const MachineInstr& front() const { return Insts.front(); }
222   const MachineInstr& back()  const { return Insts.back(); }
223
224   insn_iterator                insn_begin()       { return Insts.begin();  }
225   const_insn_iterator          insn_begin() const { return Insts.begin();  }
226   insn_iterator                  insn_end()       { return Insts.end();    }
227   const_insn_iterator            insn_end() const { return Insts.end();    }
228   reverse_insn_iterator       insn_rbegin()       { return Insts.rbegin(); }
229   const_reverse_insn_iterator insn_rbegin() const { return Insts.rbegin(); }
230   reverse_insn_iterator       insn_rend  ()       { return Insts.rend();   }
231   const_reverse_insn_iterator insn_rend  () const { return Insts.rend();   }
232
233   iterator                begin()       { return Insts.begin();  }
234   const_iterator          begin() const { return Insts.begin();  }
235   iterator                  end()       {
236     insn_iterator II = insn_end();
237     if (II != insn_begin()) {
238       while (II->isInsideBundle())
239         --II;
240     }
241     return II;
242   }
243   const_iterator            end() const {
244     const_insn_iterator II = insn_end();
245     if (II != insn_begin()) {
246       while (II->isInsideBundle())
247         --II;
248     }
249     return II;
250   }
251   reverse_iterator       rbegin()       {
252     reverse_insn_iterator II = insn_rbegin();
253     if (II != insn_rend()) {
254       while (II->isInsideBundle())
255         ++II;
256     }
257     return II;
258   }
259   const_reverse_iterator rbegin() const {
260     const_reverse_insn_iterator II = insn_rbegin();
261     if (II != insn_rend()) {
262       while (II->isInsideBundle())
263         ++II;
264     }
265     return II;
266   }
267   reverse_iterator       rend  ()       { return Insts.rend();   }
268   const_reverse_iterator rend  () const { return Insts.rend();   }
269
270
271   // Machine-CFG iterators
272   typedef std::vector<MachineBasicBlock *>::iterator       pred_iterator;
273   typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
274   typedef std::vector<MachineBasicBlock *>::iterator       succ_iterator;
275   typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
276   typedef std::vector<MachineBasicBlock *>::reverse_iterator
277                                                          pred_reverse_iterator;
278   typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
279                                                    const_pred_reverse_iterator;
280   typedef std::vector<MachineBasicBlock *>::reverse_iterator
281                                                          succ_reverse_iterator;
282   typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
283                                                    const_succ_reverse_iterator;
284
285   pred_iterator        pred_begin()       { return Predecessors.begin(); }
286   const_pred_iterator  pred_begin() const { return Predecessors.begin(); }
287   pred_iterator        pred_end()         { return Predecessors.end();   }
288   const_pred_iterator  pred_end()   const { return Predecessors.end();   }
289   pred_reverse_iterator        pred_rbegin()
290                                           { return Predecessors.rbegin();}
291   const_pred_reverse_iterator  pred_rbegin() const
292                                           { return Predecessors.rbegin();}
293   pred_reverse_iterator        pred_rend()
294                                           { return Predecessors.rend();  }
295   const_pred_reverse_iterator  pred_rend()   const
296                                           { return Predecessors.rend();  }
297   unsigned             pred_size()  const {
298     return (unsigned)Predecessors.size();
299   }
300   bool                 pred_empty() const { return Predecessors.empty(); }
301   succ_iterator        succ_begin()       { return Successors.begin();   }
302   const_succ_iterator  succ_begin() const { return Successors.begin();   }
303   succ_iterator        succ_end()         { return Successors.end();     }
304   const_succ_iterator  succ_end()   const { return Successors.end();     }
305   succ_reverse_iterator        succ_rbegin()
306                                           { return Successors.rbegin();  }
307   const_succ_reverse_iterator  succ_rbegin() const
308                                           { return Successors.rbegin();  }
309   succ_reverse_iterator        succ_rend()
310                                           { return Successors.rend();    }
311   const_succ_reverse_iterator  succ_rend()   const
312                                           { return Successors.rend();    }
313   unsigned             succ_size()  const {
314     return (unsigned)Successors.size();
315   }
316   bool                 succ_empty() const { return Successors.empty();   }
317
318   // LiveIn management methods.
319
320   /// addLiveIn - Add the specified register as a live in.  Note that it
321   /// is an error to add the same register to the same set more than once.
322   void addLiveIn(unsigned Reg)  { LiveIns.push_back(Reg); }
323
324   /// removeLiveIn - Remove the specified register from the live in set.
325   ///
326   void removeLiveIn(unsigned Reg);
327
328   /// isLiveIn - Return true if the specified register is in the live in set.
329   ///
330   bool isLiveIn(unsigned Reg) const;
331
332   // Iteration support for live in sets.  These sets are kept in sorted
333   // order by their register number.
334   typedef std::vector<unsigned>::const_iterator livein_iterator;
335   livein_iterator livein_begin() const { return LiveIns.begin(); }
336   livein_iterator livein_end()   const { return LiveIns.end(); }
337   bool            livein_empty() const { return LiveIns.empty(); }
338
339   /// getAlignment - Return alignment of the basic block.
340   /// The alignment is specified as log2(bytes).
341   ///
342   unsigned getAlignment() const { return Alignment; }
343
344   /// setAlignment - Set alignment of the basic block.
345   /// The alignment is specified as log2(bytes).
346   ///
347   void setAlignment(unsigned Align) { Alignment = Align; }
348
349   /// isLandingPad - Returns true if the block is a landing pad. That is
350   /// this basic block is entered via an exception handler.
351   bool isLandingPad() const { return IsLandingPad; }
352
353   /// setIsLandingPad - Indicates the block is a landing pad.  That is
354   /// this basic block is entered via an exception handler.
355   void setIsLandingPad(bool V = true) { IsLandingPad = V; }
356
357   /// getLandingPadSuccessor - If this block has a successor that is a landing
358   /// pad, return it. Otherwise return NULL.
359   const MachineBasicBlock *getLandingPadSuccessor() const;
360
361   // Code Layout methods.
362   
363   /// moveBefore/moveAfter - move 'this' block before or after the specified
364   /// block.  This only moves the block, it does not modify the CFG or adjust
365   /// potential fall-throughs at the end of the block.
366   void moveBefore(MachineBasicBlock *NewAfter);
367   void moveAfter(MachineBasicBlock *NewBefore);
368
369   /// updateTerminator - Update the terminator instructions in block to account
370   /// for changes to the layout. If the block previously used a fallthrough,
371   /// it may now need a branch, and if it previously used branching it may now
372   /// be able to use a fallthrough.
373   void updateTerminator();
374
375   // Machine-CFG mutators
376
377   /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
378   /// The Predecessors list of succ is automatically updated. WEIGHT
379   /// parameter is stored in Weights list and it may be used by
380   /// MachineBranchProbabilityInfo analysis to calculate branch probability.
381   ///
382   void addSuccessor(MachineBasicBlock *succ, uint32_t weight = 0);
383
384   /// removeSuccessor - Remove successor from the successors list of this
385   /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
386   ///
387   void removeSuccessor(MachineBasicBlock *succ);
388
389   /// removeSuccessor - Remove specified successor from the successors list of
390   /// this MachineBasicBlock. The Predecessors list of succ is automatically
391   /// updated.  Return the iterator to the element after the one removed.
392   ///
393   succ_iterator removeSuccessor(succ_iterator I);
394
395   /// replaceSuccessor - Replace successor OLD with NEW and update weight info.
396   ///
397   void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New);
398
399
400   /// transferSuccessors - Transfers all the successors from MBB to this
401   /// machine basic block (i.e., copies all the successors fromMBB and
402   /// remove all the successors from fromMBB).
403   void transferSuccessors(MachineBasicBlock *fromMBB);
404
405   /// transferSuccessorsAndUpdatePHIs - Transfers all the successors, as
406   /// in transferSuccessors, and update PHI operands in the successor blocks
407   /// which refer to fromMBB to refer to this.
408   void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB);
409   
410   /// isSuccessor - Return true if the specified MBB is a successor of this
411   /// block.
412   bool isSuccessor(const MachineBasicBlock *MBB) const;
413
414   /// isLayoutSuccessor - Return true if the specified MBB will be emitted
415   /// immediately after this block, such that if this block exits by
416   /// falling through, control will transfer to the specified MBB. Note
417   /// that MBB need not be a successor at all, for example if this block
418   /// ends with an unconditional branch to some other block.
419   bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
420
421   /// canFallThrough - Return true if the block can implicitly transfer
422   /// control to the block after it by falling off the end of it.  This should
423   /// return false if it can reach the block after it, but it uses an explicit
424   /// branch to do so (e.g., a table jump).  True is a conservative answer.
425   bool canFallThrough();
426
427   /// Returns a pointer to the first instructon in this block that is not a 
428   /// PHINode instruction. When adding instruction to the beginning of the
429   /// basic block, they should be added before the returned value, not before
430   /// the first instruction, which might be PHI.
431   /// Returns end() is there's no non-PHI instruction.
432   iterator getFirstNonPHI();
433
434   /// SkipPHIsAndLabels - Return the first instruction in MBB after I that is
435   /// not a PHI or a label. This is the correct point to insert copies at the
436   /// beginning of a basic block.
437   iterator SkipPHIsAndLabels(iterator I);
438
439   /// getFirstTerminator - returns an iterator to the first terminator
440   /// instruction of this basic block. If a terminator does not exist,
441   /// it returns end()
442   iterator getFirstTerminator();
443   const_iterator getFirstTerminator() const;
444
445   /// getFirstInsnTerminator - Same getFirstTerminator but it ignores bundles
446   /// and return an insn_iterator instead.
447   insn_iterator getFirstInsnTerminator();
448
449   /// getLastNonDebugInstr - returns an iterator to the last non-debug
450   /// instruction in the basic block, or end()
451   iterator getLastNonDebugInstr();
452   const_iterator getLastNonDebugInstr() const;
453
454   /// SplitCriticalEdge - Split the critical edge from this block to the
455   /// given successor block, and return the newly created block, or null
456   /// if splitting is not possible.
457   ///
458   /// This function updates LiveVariables, MachineDominatorTree, and
459   /// MachineLoopInfo, as applicable.
460   MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P);
461
462   void pop_front() { Insts.pop_front(); }
463   void pop_back() { Insts.pop_back(); }
464   void push_back(MachineInstr *MI) { Insts.push_back(MI); }
465
466   template<typename IT>
467   void insert(insn_iterator I, IT S, IT E) {
468     Insts.insert(I, S, E);
469   }
470   insn_iterator insert(insn_iterator I, MachineInstr *M) {
471     return Insts.insert(I, M);
472   }
473   insn_iterator insertAfter(insn_iterator I, MachineInstr *M) { 
474     return Insts.insertAfter(I, M); 
475   }
476
477   template<typename IT>
478   void insert(iterator I, IT S, IT E) {
479     Insts.insert(I.getInsnIterator(), S, E);
480   }
481   iterator insert(iterator I, MachineInstr *M) {
482     return Insts.insert(I.getInsnIterator(), M);
483   }
484   iterator insertAfter(iterator I, MachineInstr *M) { 
485     return Insts.insertAfter(I.getInsnIterator(), M); 
486   }
487
488   // erase - Remove the specified element or range from the instruction list.
489   // These functions delete any instructions removed.
490   //
491   insn_iterator erase(insn_iterator I) {
492     return Insts.erase(I);
493   }
494   insn_iterator erase(insn_iterator I, insn_iterator E) {
495     return Insts.erase(I, E);
496   }
497
498   iterator erase(iterator I)             {
499     return Insts.erase(I.getInsnIterator());
500   }
501   iterator erase(iterator I, iterator E) {
502     return Insts.erase(I.getInsnIterator(), E.getInsnIterator());
503   }
504
505   iterator erase(MachineInstr *I)        { iterator MII(I); return erase(MII); }
506   MachineInstr *remove(MachineInstr *I)  { return Insts.remove(I); }
507   void clear()                           { Insts.clear(); }
508
509   /// splice - Take an instruction from MBB 'Other' at the position From,
510   /// and insert it into this MBB right before 'where'.
511   void splice(insn_iterator where, MachineBasicBlock *Other,
512               insn_iterator From) {
513     Insts.splice(where, Other->Insts, From);
514   }
515   void splice(iterator where, MachineBasicBlock *Other, iterator From) {
516     Insts.splice(where.getInsnIterator(), Other->Insts, From.getInsnIterator());
517   }
518
519   /// splice - Take a block of instructions from MBB 'Other' in the range [From,
520   /// To), and insert them into this MBB right before 'where'.
521   void splice(insn_iterator where, MachineBasicBlock *Other, insn_iterator From,
522               insn_iterator To) {
523     Insts.splice(where, Other->Insts, From, To);
524   }
525   void splice(iterator where, MachineBasicBlock *Other, iterator From,
526               iterator To) {
527     Insts.splice(where.getInsnIterator(), Other->Insts,
528                  From.getInsnIterator(), To.getInsnIterator());
529   }
530
531   /// removeFromParent - This method unlinks 'this' from the containing
532   /// function, and returns it, but does not delete it.
533   MachineBasicBlock *removeFromParent();
534   
535   /// eraseFromParent - This method unlinks 'this' from the containing
536   /// function and deletes it.
537   void eraseFromParent();
538
539   /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
540   /// 'Old', change the code and CFG so that it branches to 'New' instead.
541   void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
542
543   /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
544   /// the CFG to be inserted.  If we have proven that MBB can only branch to
545   /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
546   /// DestB can be null. Besides DestA and DestB, retain other edges leading
547   /// to LandingPads (currently there can be only one; we don't check or require
548   /// that here). Note it is possible that DestA and/or DestB are LandingPads.
549   bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
550                             MachineBasicBlock *DestB,
551                             bool isCond);
552
553   /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
554   /// any DBG_VALUE instructions.  Return UnknownLoc if there is none.
555   DebugLoc findDebugLoc(insn_iterator MBBI);
556   DebugLoc findDebugLoc(iterator MBBI) {
557     return findDebugLoc(MBBI.getInsnIterator());
558   }
559
560   // Debugging methods.
561   void dump() const;
562   void print(raw_ostream &OS, SlotIndexes* = 0) const;
563
564   /// getNumber - MachineBasicBlocks are uniquely numbered at the function
565   /// level, unless they're not in a MachineFunction yet, in which case this
566   /// will return -1.
567   ///
568   int getNumber() const { return Number; }
569   void setNumber(int N) { Number = N; }
570
571   /// getSymbol - Return the MCSymbol for this basic block.
572   ///
573   MCSymbol *getSymbol() const;
574
575
576 private:
577   /// getWeightIterator - Return weight iterator corresponding to the I
578   /// successor iterator.
579   weight_iterator getWeightIterator(succ_iterator I);
580
581   friend class MachineBranchProbabilityInfo;
582
583   /// getSuccWeight - Return weight of the edge from this block to MBB. This
584   /// method should NOT be called directly, but by using getEdgeWeight method
585   /// from MachineBranchProbabilityInfo class.
586   uint32_t getSuccWeight(MachineBasicBlock *succ);
587
588
589   // Methods used to maintain doubly linked list of blocks...
590   friend struct ilist_traits<MachineBasicBlock>;
591
592   // Machine-CFG mutators
593
594   /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
595   /// Don't do this unless you know what you're doing, because it doesn't
596   /// update pred's successors list. Use pred->addSuccessor instead.
597   ///
598   void addPredecessor(MachineBasicBlock *pred);
599
600   /// removePredecessor - Remove pred as a predecessor of this
601   /// MachineBasicBlock. Don't do this unless you know what you're
602   /// doing, because it doesn't update pred's successors list. Use
603   /// pred->removeSuccessor instead.
604   ///
605   void removePredecessor(MachineBasicBlock *pred);
606 };
607
608 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
609
610 void WriteAsOperand(raw_ostream &, const MachineBasicBlock*, bool t);
611
612 // This is useful when building IndexedMaps keyed on basic block pointers.
613 struct MBB2NumberFunctor :
614   public std::unary_function<const MachineBasicBlock*, unsigned> {
615   unsigned operator()(const MachineBasicBlock *MBB) const {
616     return MBB->getNumber();
617   }
618 };
619
620 //===--------------------------------------------------------------------===//
621 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
622 //===--------------------------------------------------------------------===//
623
624 // Provide specializations of GraphTraits to be able to treat a
625 // MachineFunction as a graph of MachineBasicBlocks...
626 //
627
628 template <> struct GraphTraits<MachineBasicBlock *> {
629   typedef MachineBasicBlock NodeType;
630   typedef MachineBasicBlock::succ_iterator ChildIteratorType;
631
632   static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
633   static inline ChildIteratorType child_begin(NodeType *N) {
634     return N->succ_begin();
635   }
636   static inline ChildIteratorType child_end(NodeType *N) {
637     return N->succ_end();
638   }
639 };
640
641 template <> struct GraphTraits<const MachineBasicBlock *> {
642   typedef const MachineBasicBlock NodeType;
643   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
644
645   static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
646   static inline ChildIteratorType child_begin(NodeType *N) {
647     return N->succ_begin();
648   }
649   static inline ChildIteratorType child_end(NodeType *N) {
650     return N->succ_end();
651   }
652 };
653
654 // Provide specializations of GraphTraits to be able to treat a
655 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
656 // in inverse order.  Inverse order for a function is considered
657 // to be when traversing the predecessor edges of a MBB
658 // instead of the successor edges.
659 //
660 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
661   typedef MachineBasicBlock NodeType;
662   typedef MachineBasicBlock::pred_iterator ChildIteratorType;
663   static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
664     return G.Graph;
665   }
666   static inline ChildIteratorType child_begin(NodeType *N) {
667     return N->pred_begin();
668   }
669   static inline ChildIteratorType child_end(NodeType *N) {
670     return N->pred_end();
671   }
672 };
673
674 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
675   typedef const MachineBasicBlock NodeType;
676   typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
677   static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
678     return G.Graph;
679   }
680   static inline ChildIteratorType child_begin(NodeType *N) {
681     return N->pred_begin();
682   }
683   static inline ChildIteratorType child_end(NodeType *N) {
684     return N->pred_end();
685   }
686 };
687
688 } // End llvm namespace
689
690 #endif