MachineBasicBlock.h revision b5e8b8c8f762ae9a95f52a477440e426622d5446
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
22namespace llvm {
23
24class Pass;
25class BasicBlock;
26class MachineFunction;
27class MCSymbol;
28class SlotIndexes;
29class StringRef;
30class raw_ostream;
31class MachineBranchProbabilityInfo;
32
33template <>
34struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
35private:
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
42public:
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);
58private:
59  void createNode(const MachineInstr &);
60};
61
62class 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
108public:
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 getInstrIterator() const {
198      return MII;
199    }
200  };
201
202  typedef Instructions::iterator                                 instr_iterator;
203  typedef Instructions::const_iterator                     const_instr_iterator;
204  typedef std::reverse_iterator<instr_iterator>          reverse_instr_iterator;
205  typedef
206  std::reverse_iterator<const_instr_iterator>      const_reverse_instr_iterator;
207
208  typedef
209  bundle_iterator<MachineInstr,instr_iterator>                         iterator;
210  typedef
211  bundle_iterator<const MachineInstr,const_instr_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  instr_iterator                instr_begin()       { return Insts.begin();  }
225  const_instr_iterator          instr_begin() const { return Insts.begin();  }
226  instr_iterator                  instr_end()       { return Insts.end();    }
227  const_instr_iterator            instr_end() const { return Insts.end();    }
228  reverse_instr_iterator       instr_rbegin()       { return Insts.rbegin(); }
229  const_reverse_instr_iterator instr_rbegin() const { return Insts.rbegin(); }
230  reverse_instr_iterator       instr_rend  ()       { return Insts.rend();   }
231  const_reverse_instr_iterator instr_rend  () const { return Insts.rend();   }
232
233  iterator                begin()       { return Insts.begin();  }
234  const_iterator          begin() const { return Insts.begin();  }
235  iterator                  end()       {
236    instr_iterator II = instr_end();
237    if (II != instr_begin()) {
238      while (II->isInsideBundle())
239        --II;
240    }
241    return II;
242  }
243  const_iterator            end() const {
244    const_instr_iterator II = instr_end();
245    if (II != instr_begin()) {
246      while (II->isInsideBundle())
247        --II;
248    }
249    return II;
250  }
251  reverse_iterator       rbegin()       {
252    reverse_instr_iterator II = instr_rbegin();
253    if (II != instr_rend()) {
254      while (II->isInsideBundle())
255        ++II;
256    }
257    return II;
258  }
259  const_reverse_iterator rbegin() const {
260    const_reverse_instr_iterator II = instr_rbegin();
261    if (II != instr_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  /// getFirstInstrTerminator - Same getFirstTerminator but it ignores bundles
446  /// and return an instr_iterator instead.
447  instr_iterator getFirstInstrTerminator();
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(instr_iterator I, IT S, IT E) {
468    Insts.insert(I, S, E);
469  }
470  instr_iterator insert(instr_iterator I, MachineInstr *M) {
471    return Insts.insert(I, M);
472  }
473  instr_iterator insertAfter(instr_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.getInstrIterator(), S, E);
480  }
481  iterator insert(iterator I, MachineInstr *M) {
482    return Insts.insert(I.getInstrIterator(), M);
483  }
484  iterator insertAfter(iterator I, MachineInstr *M) {
485    return Insts.insertAfter(I.getInstrIterator(), M);
486  }
487
488  /// erase - Remove the specified element or range from the instruction list.
489  /// These functions delete any instructions removed.
490  ///
491  instr_iterator erase(instr_iterator I) {
492    return Insts.erase(I);
493  }
494  instr_iterator erase(instr_iterator I, instr_iterator E) {
495    return Insts.erase(I, E);
496  }
497  instr_iterator erase_instr(MachineInstr *I) {
498    instr_iterator MII(I);
499    return erase(MII);
500  }
501
502  iterator erase(iterator I);
503  iterator erase(iterator I, iterator E) {
504    return Insts.erase(I.getInstrIterator(), E.getInstrIterator());
505  }
506  iterator erase(MachineInstr *I) {
507    iterator MII(I);
508    return erase(MII);
509  }
510
511  /// remove - Remove the instruction from the instruction list. This function
512  /// does not delete the instruction. WARNING: Note, if the specified
513  /// instruction is a bundle this function will remove all the bundled
514  /// instructions as well. It is up to the caller to keep a list of the
515  /// bundled instructions and re-insert them if desired. This function is
516  /// *not recommended* for manipulating instructions with bundles. Use
517  /// splice instead.
518  MachineInstr *remove(MachineInstr *I);
519  void clear() {
520    Insts.clear();
521  }
522
523  /// splice - Take an instruction from MBB 'Other' at the position From,
524  /// and insert it into this MBB right before 'where'.
525  void splice(instr_iterator where, MachineBasicBlock *Other,
526              instr_iterator From) {
527    Insts.splice(where, Other->Insts, From);
528  }
529  void splice(iterator where, MachineBasicBlock *Other, iterator From);
530
531  /// splice - Take a block of instructions from MBB 'Other' in the range [From,
532  /// To), and insert them into this MBB right before 'where'.
533  void splice(instr_iterator where, MachineBasicBlock *Other, instr_iterator From,
534              instr_iterator To) {
535    Insts.splice(where, Other->Insts, From, To);
536  }
537  void splice(iterator where, MachineBasicBlock *Other, iterator From,
538              iterator To) {
539    Insts.splice(where.getInstrIterator(), Other->Insts,
540                 From.getInstrIterator(), To.getInstrIterator());
541  }
542
543  /// removeFromParent - This method unlinks 'this' from the containing
544  /// function, and returns it, but does not delete it.
545  MachineBasicBlock *removeFromParent();
546
547  /// eraseFromParent - This method unlinks 'this' from the containing
548  /// function and deletes it.
549  void eraseFromParent();
550
551  /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
552  /// 'Old', change the code and CFG so that it branches to 'New' instead.
553  void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
554
555  /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
556  /// the CFG to be inserted.  If we have proven that MBB can only branch to
557  /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
558  /// DestB can be null. Besides DestA and DestB, retain other edges leading
559  /// to LandingPads (currently there can be only one; we don't check or require
560  /// that here). Note it is possible that DestA and/or DestB are LandingPads.
561  bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
562                            MachineBasicBlock *DestB,
563                            bool isCond);
564
565  /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
566  /// any DBG_VALUE instructions.  Return UnknownLoc if there is none.
567  DebugLoc findDebugLoc(instr_iterator MBBI);
568  DebugLoc findDebugLoc(iterator MBBI) {
569    return findDebugLoc(MBBI.getInstrIterator());
570  }
571
572  // Debugging methods.
573  void dump() const;
574  void print(raw_ostream &OS, SlotIndexes* = 0) const;
575
576  /// getNumber - MachineBasicBlocks are uniquely numbered at the function
577  /// level, unless they're not in a MachineFunction yet, in which case this
578  /// will return -1.
579  ///
580  int getNumber() const { return Number; }
581  void setNumber(int N) { Number = N; }
582
583  /// getSymbol - Return the MCSymbol for this basic block.
584  ///
585  MCSymbol *getSymbol() const;
586
587
588private:
589  /// getWeightIterator - Return weight iterator corresponding to the I
590  /// successor iterator.
591  weight_iterator getWeightIterator(succ_iterator I);
592
593  friend class MachineBranchProbabilityInfo;
594
595  /// getSuccWeight - Return weight of the edge from this block to MBB. This
596  /// method should NOT be called directly, but by using getEdgeWeight method
597  /// from MachineBranchProbabilityInfo class.
598  uint32_t getSuccWeight(MachineBasicBlock *succ);
599
600
601  // Methods used to maintain doubly linked list of blocks...
602  friend struct ilist_traits<MachineBasicBlock>;
603
604  // Machine-CFG mutators
605
606  /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
607  /// Don't do this unless you know what you're doing, because it doesn't
608  /// update pred's successors list. Use pred->addSuccessor instead.
609  ///
610  void addPredecessor(MachineBasicBlock *pred);
611
612  /// removePredecessor - Remove pred as a predecessor of this
613  /// MachineBasicBlock. Don't do this unless you know what you're
614  /// doing, because it doesn't update pred's successors list. Use
615  /// pred->removeSuccessor instead.
616  ///
617  void removePredecessor(MachineBasicBlock *pred);
618};
619
620raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
621
622void WriteAsOperand(raw_ostream &, const MachineBasicBlock*, bool t);
623
624// This is useful when building IndexedMaps keyed on basic block pointers.
625struct MBB2NumberFunctor :
626  public std::unary_function<const MachineBasicBlock*, unsigned> {
627  unsigned operator()(const MachineBasicBlock *MBB) const {
628    return MBB->getNumber();
629  }
630};
631
632//===--------------------------------------------------------------------===//
633// GraphTraits specializations for machine basic block graphs (machine-CFGs)
634//===--------------------------------------------------------------------===//
635
636// Provide specializations of GraphTraits to be able to treat a
637// MachineFunction as a graph of MachineBasicBlocks...
638//
639
640template <> struct GraphTraits<MachineBasicBlock *> {
641  typedef MachineBasicBlock NodeType;
642  typedef MachineBasicBlock::succ_iterator ChildIteratorType;
643
644  static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
645  static inline ChildIteratorType child_begin(NodeType *N) {
646    return N->succ_begin();
647  }
648  static inline ChildIteratorType child_end(NodeType *N) {
649    return N->succ_end();
650  }
651};
652
653template <> struct GraphTraits<const MachineBasicBlock *> {
654  typedef const MachineBasicBlock NodeType;
655  typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
656
657  static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
658  static inline ChildIteratorType child_begin(NodeType *N) {
659    return N->succ_begin();
660  }
661  static inline ChildIteratorType child_end(NodeType *N) {
662    return N->succ_end();
663  }
664};
665
666// Provide specializations of GraphTraits to be able to treat a
667// MachineFunction as a graph of MachineBasicBlocks... and to walk it
668// in inverse order.  Inverse order for a function is considered
669// to be when traversing the predecessor edges of a MBB
670// instead of the successor edges.
671//
672template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
673  typedef MachineBasicBlock NodeType;
674  typedef MachineBasicBlock::pred_iterator ChildIteratorType;
675  static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
676    return G.Graph;
677  }
678  static inline ChildIteratorType child_begin(NodeType *N) {
679    return N->pred_begin();
680  }
681  static inline ChildIteratorType child_end(NodeType *N) {
682    return N->pred_end();
683  }
684};
685
686template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
687  typedef const MachineBasicBlock NodeType;
688  typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
689  static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
690    return G.Graph;
691  }
692  static inline ChildIteratorType child_begin(NodeType *N) {
693    return N->pred_begin();
694  }
695  static inline ChildIteratorType child_end(NodeType *N) {
696    return N->pred_end();
697  }
698};
699
700} // End llvm namespace
701
702#endif
703