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