MachineBasicBlock.h revision 239462
1219820Sjeff//===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- C++ -*-===//
2219820Sjeff//
3219820Sjeff//                     The LLVM Compiler Infrastructure
4219820Sjeff//
5219820Sjeff// This file is distributed under the University of Illinois Open Source
6219820Sjeff// License. See LICENSE.TXT for details.
7219820Sjeff//
8219820Sjeff//===----------------------------------------------------------------------===//
9219820Sjeff//
10219820Sjeff// Collect the sequence of machine instructions for a basic block.
11219820Sjeff//
12219820Sjeff//===----------------------------------------------------------------------===//
13219820Sjeff
14219820Sjeff#ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H
15219820Sjeff#define LLVM_CODEGEN_MACHINEBASICBLOCK_H
16219820Sjeff
17219820Sjeff#include "llvm/CodeGen/MachineInstr.h"
18219820Sjeff#include "llvm/ADT/GraphTraits.h"
19219820Sjeff#include "llvm/Support/DataTypes.h"
20219820Sjeff#include <functional>
21219820Sjeff
22219820Sjeffnamespace llvm {
23219820Sjeff
24219820Sjeffclass Pass;
25219820Sjeffclass BasicBlock;
26219820Sjeffclass MachineFunction;
27219820Sjeffclass MCSymbol;
28219820Sjeffclass SlotIndexes;
29219820Sjeffclass StringRef;
30219820Sjeffclass raw_ostream;
31219820Sjeffclass MachineBranchProbabilityInfo;
32219820Sjeff
33219820Sjefftemplate <>
34219820Sjeffstruct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
35219820Sjeffprivate:
36219820Sjeff  mutable ilist_half_node<MachineInstr> Sentinel;
37219820Sjeff
38219820Sjeff  // this is only set by the MachineBasicBlock owning the LiveList
39219820Sjeff  friend class MachineBasicBlock;
40219820Sjeff  MachineBasicBlock* Parent;
41219820Sjeff
42219820Sjeffpublic:
43219820Sjeff  MachineInstr *createSentinel() const {
44219820Sjeff    return static_cast<MachineInstr*>(&Sentinel);
45219820Sjeff  }
46219820Sjeff  void destroySentinel(MachineInstr *) const {}
47219820Sjeff
48219820Sjeff  MachineInstr *provideInitialHead() const { return createSentinel(); }
49219820Sjeff  MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); }
50219820Sjeff  static void noteHead(MachineInstr*, MachineInstr*) {}
51219820Sjeff
52219820Sjeff  void addNodeToList(MachineInstr* N);
53219820Sjeff  void removeNodeFromList(MachineInstr* N);
54219820Sjeff  void transferNodesFromList(ilist_traits &SrcTraits,
55219820Sjeff                             ilist_iterator<MachineInstr> first,
56219820Sjeff                             ilist_iterator<MachineInstr> last);
57219820Sjeff  void deleteNode(MachineInstr *N);
58219820Sjeffprivate:
59219820Sjeff  void createNode(const MachineInstr &);
60219820Sjeff};
61219820Sjeff
62219820Sjeffclass MachineBasicBlock : public ilist_node<MachineBasicBlock> {
63219820Sjeff  typedef ilist<MachineInstr> Instructions;
64219820Sjeff  Instructions Insts;
65219820Sjeff  const BasicBlock *BB;
66219820Sjeff  int Number;
67219820Sjeff  MachineFunction *xParent;
68219820Sjeff
69219820Sjeff  /// Predecessors/Successors - Keep track of the predecessor / successor
70219820Sjeff  /// basicblocks.
71219820Sjeff  std::vector<MachineBasicBlock *> Predecessors;
72219820Sjeff  std::vector<MachineBasicBlock *> Successors;
73219820Sjeff
74219820Sjeff
75219820Sjeff  /// Weights - Keep track of the weights to the successors. This vector
76219820Sjeff  /// has the same order as Successors, or it is empty if we don't use it
77219820Sjeff  /// (disable optimization).
78253449Sjhb  std::vector<uint32_t> Weights;
79219820Sjeff  typedef std::vector<uint32_t>::iterator weight_iterator;
80219820Sjeff  typedef std::vector<uint32_t>::const_iterator const_weight_iterator;
81219820Sjeff
82219820Sjeff  /// LiveIns - Keep track of the physical registers that are livein of
83219820Sjeff  /// the basicblock.
84219820Sjeff  std::vector<unsigned> LiveIns;
85219820Sjeff
86253449Sjhb  /// Alignment - Alignment of the basic block. Zero if the basic block does
87219820Sjeff  /// not need to be aligned.
88219820Sjeff  /// The alignment is specified as log2(bytes).
89253449Sjhb  unsigned Alignment;
90219820Sjeff
91219820Sjeff  /// IsLandingPad - Indicate that this basic block is entered via an
92219820Sjeff  /// exception handler.
93253449Sjhb  bool IsLandingPad;
94253449Sjhb
95219820Sjeff  /// AddressTaken - Indicate that this basic block is potentially the
96219820Sjeff  /// target of an indirect branch.
97219820Sjeff  bool AddressTaken;
98219820Sjeff
99219820Sjeff  // Intrusive list support
100254121Sjeff  MachineBasicBlock() {}
101254121Sjeff
102254121Sjeff  explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
103254121Sjeff
104254121Sjeff  ~MachineBasicBlock();
105254121Sjeff
106254121Sjeff  // MachineBasicBlocks are allocated and owned by MachineFunction.
107219820Sjeff  friend class MachineFunction;
108219820Sjeff
109253449Sjhbpublic:
110253449Sjhb  /// getBasicBlock - Return the LLVM basic block that this instance
111253449Sjhb  /// corresponded to originally. Note that this may be NULL if this instance
112253449Sjhb  /// does not correspond directly to an LLVM basic block.
113219820Sjeff  ///
114253449Sjhb  const BasicBlock *getBasicBlock() const { return BB; }
115253449Sjhb
116253048Sjhb  /// getName - Return the name of the corresponding LLVM basic block, or
117219820Sjeff  /// "(null)".
118219820Sjeff  StringRef getName() const;
119219820Sjeff
120219820Sjeff  /// getFullName - Return a formatted string to identify this block and its
121219820Sjeff  /// parent function.
122219820Sjeff  std::string getFullName() const;
123219820Sjeff
124219820Sjeff  /// hasAddressTaken - Test whether this block is potentially the target
125219820Sjeff  /// of an indirect branch.
126219820Sjeff  bool hasAddressTaken() const { return AddressTaken; }
127219820Sjeff
128219820Sjeff  /// setHasAddressTaken - Set this block to reflect that it potentially
129219820Sjeff  /// is the target of an indirect branch.
130219820Sjeff  void setHasAddressTaken() { AddressTaken = true; }
131219820Sjeff
132219820Sjeff  /// getParent - Return the MachineFunction containing this basic block.
133219820Sjeff  ///
134219820Sjeff  const MachineFunction *getParent() const { return xParent; }
135219820Sjeff  MachineFunction *getParent() { return xParent; }
136219820Sjeff
137219820Sjeff
138219820Sjeff  /// bundle_iterator - MachineBasicBlock iterator that automatically skips over
139219820Sjeff  /// MIs that are inside bundles (i.e. walk top level MIs only).
140219820Sjeff  template<typename Ty, typename IterTy>
141219820Sjeff  class bundle_iterator
142219820Sjeff    : public std::iterator<std::bidirectional_iterator_tag, Ty, ptrdiff_t> {
143219820Sjeff    IterTy MII;
144219820Sjeff
145219820Sjeff  public:
146219820Sjeff    bundle_iterator(IterTy mii) : MII(mii) {}
147219820Sjeff
148219820Sjeff    bundle_iterator(Ty &mi) : MII(mi) {
149219820Sjeff      assert(!mi.isInsideBundle() &&
150219820Sjeff             "It's not legal to initialize bundle_iterator with a bundled MI");
151219820Sjeff    }
152219820Sjeff    bundle_iterator(Ty *mi) : MII(mi) {
153219820Sjeff      assert((!mi || !mi->isInsideBundle()) &&
154219820Sjeff             "It's not legal to initialize bundle_iterator with a bundled MI");
155219820Sjeff    }
156219820Sjeff    // Template allows conversion from const to nonconst.
157219820Sjeff    template<class OtherTy, class OtherIterTy>
158219820Sjeff    bundle_iterator(const bundle_iterator<OtherTy, OtherIterTy> &I)
159219820Sjeff      : MII(I.getInstrIterator()) {}
160219820Sjeff    bundle_iterator() : MII(0) {}
161219820Sjeff
162219820Sjeff    Ty &operator*() const { return *MII; }
163219820Sjeff    Ty *operator->() const { return &operator*(); }
164219820Sjeff
165219820Sjeff    operator Ty*() const { return MII; }
166219820Sjeff
167219820Sjeff    bool operator==(const bundle_iterator &x) const {
168219820Sjeff      return MII == x.MII;
169219820Sjeff    }
170219820Sjeff    bool operator!=(const bundle_iterator &x) const {
171219820Sjeff      return !operator==(x);
172219820Sjeff    }
173219820Sjeff
174219820Sjeff    // Increment and decrement operators...
175219820Sjeff    bundle_iterator &operator--() {      // predecrement - Back up
176219820Sjeff      do --MII;
177219820Sjeff      while (MII->isInsideBundle());
178219820Sjeff      return *this;
179219820Sjeff    }
180219820Sjeff    bundle_iterator &operator++() {      // preincrement - Advance
181219820Sjeff      IterTy E = MII->getParent()->instr_end();
182219820Sjeff      do ++MII;
183219820Sjeff      while (MII != E && MII->isInsideBundle());
184219820Sjeff      return *this;
185219820Sjeff    }
186219820Sjeff    bundle_iterator operator--(int) {    // postdecrement operators...
187219820Sjeff      bundle_iterator tmp = *this;
188255932Salfred      --*this;
189255932Salfred      return tmp;
190219820Sjeff    }
191    bundle_iterator operator++(int) {    // postincrement operators...
192      bundle_iterator tmp = *this;
193      ++*this;
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 instr_begin();  }
234  const_iterator          begin() const { return instr_begin();  }
235  iterator                end  ()       { return instr_end();    }
236  const_iterator          end  () const { return instr_end();    }
237  reverse_iterator       rbegin()       { return instr_rbegin(); }
238  const_reverse_iterator rbegin() const { return instr_rbegin(); }
239  reverse_iterator       rend  ()       { return instr_rend();   }
240  const_reverse_iterator rend  () const { return instr_rend();   }
241
242
243  // Machine-CFG iterators
244  typedef std::vector<MachineBasicBlock *>::iterator       pred_iterator;
245  typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
246  typedef std::vector<MachineBasicBlock *>::iterator       succ_iterator;
247  typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
248  typedef std::vector<MachineBasicBlock *>::reverse_iterator
249                                                         pred_reverse_iterator;
250  typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
251                                                   const_pred_reverse_iterator;
252  typedef std::vector<MachineBasicBlock *>::reverse_iterator
253                                                         succ_reverse_iterator;
254  typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
255                                                   const_succ_reverse_iterator;
256
257  pred_iterator        pred_begin()       { return Predecessors.begin(); }
258  const_pred_iterator  pred_begin() const { return Predecessors.begin(); }
259  pred_iterator        pred_end()         { return Predecessors.end();   }
260  const_pred_iterator  pred_end()   const { return Predecessors.end();   }
261  pred_reverse_iterator        pred_rbegin()
262                                          { return Predecessors.rbegin();}
263  const_pred_reverse_iterator  pred_rbegin() const
264                                          { return Predecessors.rbegin();}
265  pred_reverse_iterator        pred_rend()
266                                          { return Predecessors.rend();  }
267  const_pred_reverse_iterator  pred_rend()   const
268                                          { return Predecessors.rend();  }
269  unsigned             pred_size()  const {
270    return (unsigned)Predecessors.size();
271  }
272  bool                 pred_empty() const { return Predecessors.empty(); }
273  succ_iterator        succ_begin()       { return Successors.begin();   }
274  const_succ_iterator  succ_begin() const { return Successors.begin();   }
275  succ_iterator        succ_end()         { return Successors.end();     }
276  const_succ_iterator  succ_end()   const { return Successors.end();     }
277  succ_reverse_iterator        succ_rbegin()
278                                          { return Successors.rbegin();  }
279  const_succ_reverse_iterator  succ_rbegin() const
280                                          { return Successors.rbegin();  }
281  succ_reverse_iterator        succ_rend()
282                                          { return Successors.rend();    }
283  const_succ_reverse_iterator  succ_rend()   const
284                                          { return Successors.rend();    }
285  unsigned             succ_size()  const {
286    return (unsigned)Successors.size();
287  }
288  bool                 succ_empty() const { return Successors.empty();   }
289
290  // LiveIn management methods.
291
292  /// addLiveIn - Add the specified register as a live in.  Note that it
293  /// is an error to add the same register to the same set more than once.
294  void addLiveIn(unsigned Reg)  { LiveIns.push_back(Reg); }
295
296  /// removeLiveIn - Remove the specified register from the live in set.
297  ///
298  void removeLiveIn(unsigned Reg);
299
300  /// isLiveIn - Return true if the specified register is in the live in set.
301  ///
302  bool isLiveIn(unsigned Reg) const;
303
304  // Iteration support for live in sets.  These sets are kept in sorted
305  // order by their register number.
306  typedef std::vector<unsigned>::const_iterator livein_iterator;
307  livein_iterator livein_begin() const { return LiveIns.begin(); }
308  livein_iterator livein_end()   const { return LiveIns.end(); }
309  bool            livein_empty() const { return LiveIns.empty(); }
310
311  /// getAlignment - Return alignment of the basic block.
312  /// The alignment is specified as log2(bytes).
313  ///
314  unsigned getAlignment() const { return Alignment; }
315
316  /// setAlignment - Set alignment of the basic block.
317  /// The alignment is specified as log2(bytes).
318  ///
319  void setAlignment(unsigned Align) { Alignment = Align; }
320
321  /// isLandingPad - Returns true if the block is a landing pad. That is
322  /// this basic block is entered via an exception handler.
323  bool isLandingPad() const { return IsLandingPad; }
324
325  /// setIsLandingPad - Indicates the block is a landing pad.  That is
326  /// this basic block is entered via an exception handler.
327  void setIsLandingPad(bool V = true) { IsLandingPad = V; }
328
329  /// getLandingPadSuccessor - If this block has a successor that is a landing
330  /// pad, return it. Otherwise return NULL.
331  const MachineBasicBlock *getLandingPadSuccessor() const;
332
333  // Code Layout methods.
334
335  /// moveBefore/moveAfter - move 'this' block before or after the specified
336  /// block.  This only moves the block, it does not modify the CFG or adjust
337  /// potential fall-throughs at the end of the block.
338  void moveBefore(MachineBasicBlock *NewAfter);
339  void moveAfter(MachineBasicBlock *NewBefore);
340
341  /// updateTerminator - Update the terminator instructions in block to account
342  /// for changes to the layout. If the block previously used a fallthrough,
343  /// it may now need a branch, and if it previously used branching it may now
344  /// be able to use a fallthrough.
345  void updateTerminator();
346
347  // Machine-CFG mutators
348
349  /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
350  /// The Predecessors list of succ is automatically updated. WEIGHT
351  /// parameter is stored in Weights list and it may be used by
352  /// MachineBranchProbabilityInfo analysis to calculate branch probability.
353  ///
354  void addSuccessor(MachineBasicBlock *succ, uint32_t weight = 0);
355
356  /// removeSuccessor - Remove successor from the successors list of this
357  /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
358  ///
359  void removeSuccessor(MachineBasicBlock *succ);
360
361  /// removeSuccessor - Remove specified successor from the successors list of
362  /// this MachineBasicBlock. The Predecessors list of succ is automatically
363  /// updated.  Return the iterator to the element after the one removed.
364  ///
365  succ_iterator removeSuccessor(succ_iterator I);
366
367  /// replaceSuccessor - Replace successor OLD with NEW and update weight info.
368  ///
369  void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New);
370
371
372  /// transferSuccessors - Transfers all the successors from MBB to this
373  /// machine basic block (i.e., copies all the successors fromMBB and
374  /// remove all the successors from fromMBB).
375  void transferSuccessors(MachineBasicBlock *fromMBB);
376
377  /// transferSuccessorsAndUpdatePHIs - Transfers all the successors, as
378  /// in transferSuccessors, and update PHI operands in the successor blocks
379  /// which refer to fromMBB to refer to this.
380  void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB);
381
382  /// isPredecessor - Return true if the specified MBB is a predecessor of this
383  /// block.
384  bool isPredecessor(const MachineBasicBlock *MBB) const;
385
386  /// isSuccessor - Return true if the specified MBB is a successor of this
387  /// block.
388  bool isSuccessor(const MachineBasicBlock *MBB) const;
389
390  /// isLayoutSuccessor - Return true if the specified MBB will be emitted
391  /// immediately after this block, such that if this block exits by
392  /// falling through, control will transfer to the specified MBB. Note
393  /// that MBB need not be a successor at all, for example if this block
394  /// ends with an unconditional branch to some other block.
395  bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
396
397  /// canFallThrough - Return true if the block can implicitly transfer
398  /// control to the block after it by falling off the end of it.  This should
399  /// return false if it can reach the block after it, but it uses an explicit
400  /// branch to do so (e.g., a table jump).  True is a conservative answer.
401  bool canFallThrough();
402
403  /// Returns a pointer to the first instructon in this block that is not a
404  /// PHINode instruction. When adding instruction to the beginning of the
405  /// basic block, they should be added before the returned value, not before
406  /// the first instruction, which might be PHI.
407  /// Returns end() is there's no non-PHI instruction.
408  iterator getFirstNonPHI();
409
410  /// SkipPHIsAndLabels - Return the first instruction in MBB after I that is
411  /// not a PHI or a label. This is the correct point to insert copies at the
412  /// beginning of a basic block.
413  iterator SkipPHIsAndLabels(iterator I);
414
415  /// getFirstTerminator - returns an iterator to the first terminator
416  /// instruction of this basic block. If a terminator does not exist,
417  /// it returns end()
418  iterator getFirstTerminator();
419  const_iterator getFirstTerminator() const;
420
421  /// getFirstInstrTerminator - Same getFirstTerminator but it ignores bundles
422  /// and return an instr_iterator instead.
423  instr_iterator getFirstInstrTerminator();
424
425  /// getLastNonDebugInstr - returns an iterator to the last non-debug
426  /// instruction in the basic block, or end()
427  iterator getLastNonDebugInstr();
428  const_iterator getLastNonDebugInstr() const;
429
430  /// SplitCriticalEdge - Split the critical edge from this block to the
431  /// given successor block, and return the newly created block, or null
432  /// if splitting is not possible.
433  ///
434  /// This function updates LiveVariables, MachineDominatorTree, and
435  /// MachineLoopInfo, as applicable.
436  MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P);
437
438  void pop_front() { Insts.pop_front(); }
439  void pop_back() { Insts.pop_back(); }
440  void push_back(MachineInstr *MI) { Insts.push_back(MI); }
441
442  template<typename IT>
443  void insert(instr_iterator I, IT S, IT E) {
444    Insts.insert(I, S, E);
445  }
446  instr_iterator insert(instr_iterator I, MachineInstr *M) {
447    return Insts.insert(I, M);
448  }
449  instr_iterator insertAfter(instr_iterator I, MachineInstr *M) {
450    return Insts.insertAfter(I, M);
451  }
452
453  template<typename IT>
454  void insert(iterator I, IT S, IT E) {
455    Insts.insert(I.getInstrIterator(), S, E);
456  }
457  iterator insert(iterator I, MachineInstr *M) {
458    return Insts.insert(I.getInstrIterator(), M);
459  }
460  iterator insertAfter(iterator I, MachineInstr *M) {
461    return Insts.insertAfter(I.getInstrIterator(), M);
462  }
463
464  /// erase - Remove the specified element or range from the instruction list.
465  /// These functions delete any instructions removed.
466  ///
467  instr_iterator erase(instr_iterator I) {
468    return Insts.erase(I);
469  }
470  instr_iterator erase(instr_iterator I, instr_iterator E) {
471    return Insts.erase(I, E);
472  }
473  instr_iterator erase_instr(MachineInstr *I) {
474    instr_iterator MII(I);
475    return erase(MII);
476  }
477
478  iterator erase(iterator I);
479  iterator erase(iterator I, iterator E) {
480    return Insts.erase(I.getInstrIterator(), E.getInstrIterator());
481  }
482  iterator erase(MachineInstr *I) {
483    iterator MII(I);
484    return erase(MII);
485  }
486
487  /// remove - Remove the instruction from the instruction list. This function
488  /// does not delete the instruction. WARNING: Note, if the specified
489  /// instruction is a bundle this function will remove all the bundled
490  /// instructions as well. It is up to the caller to keep a list of the
491  /// bundled instructions and re-insert them if desired. This function is
492  /// *not recommended* for manipulating instructions with bundles. Use
493  /// splice instead.
494  MachineInstr *remove(MachineInstr *I);
495  void clear() {
496    Insts.clear();
497  }
498
499  /// splice - Take an instruction from MBB 'Other' at the position From,
500  /// and insert it into this MBB right before 'where'.
501  void splice(instr_iterator where, MachineBasicBlock *Other,
502              instr_iterator From) {
503    Insts.splice(where, Other->Insts, From);
504  }
505  void splice(iterator where, MachineBasicBlock *Other, iterator From);
506
507  /// splice - Take a block of instructions from MBB 'Other' in the range [From,
508  /// To), and insert them into this MBB right before 'where'.
509  void splice(instr_iterator where, MachineBasicBlock *Other, instr_iterator From,
510              instr_iterator To) {
511    Insts.splice(where, Other->Insts, From, To);
512  }
513  void splice(iterator where, MachineBasicBlock *Other, iterator From,
514              iterator To) {
515    Insts.splice(where.getInstrIterator(), Other->Insts,
516                 From.getInstrIterator(), To.getInstrIterator());
517  }
518
519  /// removeFromParent - This method unlinks 'this' from the containing
520  /// function, and returns it, but does not delete it.
521  MachineBasicBlock *removeFromParent();
522
523  /// eraseFromParent - This method unlinks 'this' from the containing
524  /// function and deletes it.
525  void eraseFromParent();
526
527  /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
528  /// 'Old', change the code and CFG so that it branches to 'New' instead.
529  void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
530
531  /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
532  /// the CFG to be inserted.  If we have proven that MBB can only branch to
533  /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
534  /// DestB can be null. Besides DestA and DestB, retain other edges leading
535  /// to LandingPads (currently there can be only one; we don't check or require
536  /// that here). Note it is possible that DestA and/or DestB are LandingPads.
537  bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
538                            MachineBasicBlock *DestB,
539                            bool isCond);
540
541  /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
542  /// any DBG_VALUE instructions.  Return UnknownLoc if there is none.
543  DebugLoc findDebugLoc(instr_iterator MBBI);
544  DebugLoc findDebugLoc(iterator MBBI) {
545    return findDebugLoc(MBBI.getInstrIterator());
546  }
547
548  // Debugging methods.
549  void dump() const;
550  void print(raw_ostream &OS, SlotIndexes* = 0) const;
551
552  /// getNumber - MachineBasicBlocks are uniquely numbered at the function
553  /// level, unless they're not in a MachineFunction yet, in which case this
554  /// will return -1.
555  ///
556  int getNumber() const { return Number; }
557  void setNumber(int N) { Number = N; }
558
559  /// getSymbol - Return the MCSymbol for this basic block.
560  ///
561  MCSymbol *getSymbol() const;
562
563
564private:
565  /// getWeightIterator - Return weight iterator corresponding to the I
566  /// successor iterator.
567  weight_iterator getWeightIterator(succ_iterator I);
568  const_weight_iterator getWeightIterator(const_succ_iterator I) const;
569
570  friend class MachineBranchProbabilityInfo;
571
572  /// getSuccWeight - Return weight of the edge from this block to MBB. This
573  /// method should NOT be called directly, but by using getEdgeWeight method
574  /// from MachineBranchProbabilityInfo class.
575  uint32_t getSuccWeight(const MachineBasicBlock *succ) const;
576
577
578  // Methods used to maintain doubly linked list of blocks...
579  friend struct ilist_traits<MachineBasicBlock>;
580
581  // Machine-CFG mutators
582
583  /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
584  /// Don't do this unless you know what you're doing, because it doesn't
585  /// update pred's successors list. Use pred->addSuccessor instead.
586  ///
587  void addPredecessor(MachineBasicBlock *pred);
588
589  /// removePredecessor - Remove pred as a predecessor of this
590  /// MachineBasicBlock. Don't do this unless you know what you're
591  /// doing, because it doesn't update pred's successors list. Use
592  /// pred->removeSuccessor instead.
593  ///
594  void removePredecessor(MachineBasicBlock *pred);
595};
596
597raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
598
599void WriteAsOperand(raw_ostream &, const MachineBasicBlock*, bool t);
600
601// This is useful when building IndexedMaps keyed on basic block pointers.
602struct MBB2NumberFunctor :
603  public std::unary_function<const MachineBasicBlock*, unsigned> {
604  unsigned operator()(const MachineBasicBlock *MBB) const {
605    return MBB->getNumber();
606  }
607};
608
609//===--------------------------------------------------------------------===//
610// GraphTraits specializations for machine basic block graphs (machine-CFGs)
611//===--------------------------------------------------------------------===//
612
613// Provide specializations of GraphTraits to be able to treat a
614// MachineFunction as a graph of MachineBasicBlocks...
615//
616
617template <> struct GraphTraits<MachineBasicBlock *> {
618  typedef MachineBasicBlock NodeType;
619  typedef MachineBasicBlock::succ_iterator ChildIteratorType;
620
621  static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
622  static inline ChildIteratorType child_begin(NodeType *N) {
623    return N->succ_begin();
624  }
625  static inline ChildIteratorType child_end(NodeType *N) {
626    return N->succ_end();
627  }
628};
629
630template <> struct GraphTraits<const MachineBasicBlock *> {
631  typedef const MachineBasicBlock NodeType;
632  typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
633
634  static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
635  static inline ChildIteratorType child_begin(NodeType *N) {
636    return N->succ_begin();
637  }
638  static inline ChildIteratorType child_end(NodeType *N) {
639    return N->succ_end();
640  }
641};
642
643// Provide specializations of GraphTraits to be able to treat a
644// MachineFunction as a graph of MachineBasicBlocks... and to walk it
645// in inverse order.  Inverse order for a function is considered
646// to be when traversing the predecessor edges of a MBB
647// instead of the successor edges.
648//
649template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
650  typedef MachineBasicBlock NodeType;
651  typedef MachineBasicBlock::pred_iterator ChildIteratorType;
652  static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
653    return G.Graph;
654  }
655  static inline ChildIteratorType child_begin(NodeType *N) {
656    return N->pred_begin();
657  }
658  static inline ChildIteratorType child_end(NodeType *N) {
659    return N->pred_end();
660  }
661};
662
663template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
664  typedef const MachineBasicBlock NodeType;
665  typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
666  static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
667    return G.Graph;
668  }
669  static inline ChildIteratorType child_begin(NodeType *N) {
670    return N->pred_begin();
671  }
672  static inline ChildIteratorType child_end(NodeType *N) {
673    return N->pred_end();
674  }
675};
676
677} // End llvm namespace
678
679#endif
680