1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- C++ -*-===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Collect the sequence of machine instructions for a basic block.
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define LLVM_CODEGEN_MACHINEBASICBLOCK_H
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineInstr.h"
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/GraphTraits.h"
1919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/Support/DataTypes.h"
2019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include <functional>
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace llvm {
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass Pass;
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass BasicBlock;
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass MachineFunction;
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass MCSymbol;
2819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanclass SlotIndexes;
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass StringRef;
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass raw_ostream;
3119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanclass MachineBranchProbabilityInfo;
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <>
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstruct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanprivate:
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  mutable ilist_half_node<MachineInstr> Sentinel;
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // this is only set by the MachineBasicBlock owning the LiveList
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  friend class MachineBasicBlock;
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock* Parent;
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic:
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineInstr *createSentinel() const {
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return static_cast<MachineInstr*>(&Sentinel);
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void destroySentinel(MachineInstr *) const {}
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineInstr *provideInitialHead() const { return createSentinel(); }
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); }
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static void noteHead(MachineInstr*, MachineInstr*) {}
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void addNodeToList(MachineInstr* N);
53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void removeNodeFromList(MachineInstr* N);
54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void transferNodesFromList(ilist_traits &SrcTraits,
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             ilist_iterator<MachineInstr> first,
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             ilist_iterator<MachineInstr> last);
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void deleteNode(MachineInstr *N);
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanprivate:
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void createNode(const MachineInstr &);
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass MachineBasicBlock : public ilist_node<MachineBasicBlock> {
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef ilist<MachineInstr> Instructions;
64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Instructions Insts;
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const BasicBlock *BB;
66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int Number;
67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction *xParent;
6819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// Predecessors/Successors - Keep track of the predecessor / successor
70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// basicblocks.
71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<MachineBasicBlock *> Predecessors;
72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<MachineBasicBlock *> Successors;
73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
7519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// Weights - Keep track of the weights to the successors. This vector
7619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// has the same order as Successors, or it is empty if we don't use it
7719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// (disable optimization).
7819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  std::vector<uint32_t> Weights;
7919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  typedef std::vector<uint32_t>::iterator weight_iterator;
8019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// LiveIns - Keep track of the physical registers that are livein of
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// the basicblock.
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<unsigned> LiveIns;
84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// Alignment - Alignment of the basic block. Zero if the basic block does
86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// not need to be aligned.
87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Alignment;
8819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
8919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// IsLandingPad - Indicate that this basic block is entered via an
9019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// exception handler.
9119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool IsLandingPad;
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// AddressTaken - Indicate that this basic block is potentially the
94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// target of an indirect branch.
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool AddressTaken;
96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Intrusive list support
98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock() {}
99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ~MachineBasicBlock();
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // MachineBasicBlocks are allocated and owned by MachineFunction.
105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  friend class MachineFunction;
106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic:
108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getBasicBlock - Return the LLVM basic block that this instance
109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// corresponded to originally. Note that this may be NULL if this instance
110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// does not correspond directly to an LLVM basic block.
111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const BasicBlock *getBasicBlock() const { return BB; }
113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getName - Return the name of the corresponding LLVM basic block, or
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// "(null)".
116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  StringRef getName() const;
117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// hasAddressTaken - Test whether this block is potentially the target
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// of an indirect branch.
120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool hasAddressTaken() const { return AddressTaken; }
121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// setHasAddressTaken - Set this block to reflect that it potentially
123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// is the target of an indirect branch.
124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void setHasAddressTaken() { AddressTaken = true; }
125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getParent - Return the MachineFunction containing this basic block.
127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const MachineFunction *getParent() const { return xParent; }
129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction *getParent() { return xParent; }
130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef Instructions::iterator                              iterator;
132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef Instructions::const_iterator                  const_iterator;
133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef std::reverse_iterator<iterator>             reverse_iterator;
135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned size() const { return (unsigned)Insts.size(); }
137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool empty() const { return Insts.empty(); }
138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineInstr& front() { return Insts.front(); }
140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineInstr& back()  { return Insts.back(); }
141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const MachineInstr& front() const { return Insts.front(); }
142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const MachineInstr& back()  const { return Insts.back(); }
143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  iterator                begin()       { return Insts.begin();  }
145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const_iterator          begin() const { return Insts.begin();  }
146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  iterator                  end()       { return Insts.end();    }
147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const_iterator            end() const { return Insts.end();    }
148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  reverse_iterator       rbegin()       { return Insts.rbegin(); }
149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const_reverse_iterator rbegin() const { return Insts.rbegin(); }
150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  reverse_iterator       rend  ()       { return Insts.rend();   }
151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const_reverse_iterator rend  () const { return Insts.rend();   }
152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Machine-CFG iterators
154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef std::vector<MachineBasicBlock *>::iterator       pred_iterator;
155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef std::vector<MachineBasicBlock *>::iterator       succ_iterator;
157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef std::vector<MachineBasicBlock *>::reverse_iterator
159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                         pred_reverse_iterator;
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                   const_pred_reverse_iterator;
162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef std::vector<MachineBasicBlock *>::reverse_iterator
163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                         succ_reverse_iterator;
164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                   const_succ_reverse_iterator;
166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  pred_iterator        pred_begin()       { return Predecessors.begin(); }
168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const_pred_iterator  pred_begin() const { return Predecessors.begin(); }
169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  pred_iterator        pred_end()         { return Predecessors.end();   }
170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const_pred_iterator  pred_end()   const { return Predecessors.end();   }
171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  pred_reverse_iterator        pred_rbegin()
172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          { return Predecessors.rbegin();}
173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const_pred_reverse_iterator  pred_rbegin() const
174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          { return Predecessors.rbegin();}
175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  pred_reverse_iterator        pred_rend()
176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          { return Predecessors.rend();  }
177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const_pred_reverse_iterator  pred_rend()   const
178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          { return Predecessors.rend();  }
179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned             pred_size()  const {
180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return (unsigned)Predecessors.size();
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool                 pred_empty() const { return Predecessors.empty(); }
183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  succ_iterator        succ_begin()       { return Successors.begin();   }
184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const_succ_iterator  succ_begin() const { return Successors.begin();   }
185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  succ_iterator        succ_end()         { return Successors.end();     }
186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const_succ_iterator  succ_end()   const { return Successors.end();     }
187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  succ_reverse_iterator        succ_rbegin()
188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          { return Successors.rbegin();  }
189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const_succ_reverse_iterator  succ_rbegin() const
190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          { return Successors.rbegin();  }
191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  succ_reverse_iterator        succ_rend()
192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          { return Successors.rend();    }
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const_succ_reverse_iterator  succ_rend()   const
194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          { return Successors.rend();    }
195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned             succ_size()  const {
196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return (unsigned)Successors.size();
197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool                 succ_empty() const { return Successors.empty();   }
199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // LiveIn management methods.
201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// addLiveIn - Add the specified register as a live in.  Note that it
203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// is an error to add the same register to the same set more than once.
204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void addLiveIn(unsigned Reg)  { LiveIns.push_back(Reg); }
205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// removeLiveIn - Remove the specified register from the live in set.
207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void removeLiveIn(unsigned Reg);
209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// isLiveIn - Return true if the specified register is in the live in set.
211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isLiveIn(unsigned Reg) const;
213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Iteration support for live in sets.  These sets are kept in sorted
215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // order by their register number.
216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef std::vector<unsigned>::const_iterator livein_iterator;
217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  livein_iterator livein_begin() const { return LiveIns.begin(); }
218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  livein_iterator livein_end()   const { return LiveIns.end(); }
219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool            livein_empty() const { return LiveIns.empty(); }
220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getAlignment - Return alignment of the basic block.
222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned getAlignment() const { return Alignment; }
224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// setAlignment - Set alignment of the basic block.
226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void setAlignment(unsigned Align) { Alignment = Align; }
228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
22919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// isLandingPad - Returns true if the block is a landing pad. That is
23019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// this basic block is entered via an exception handler.
23119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool isLandingPad() const { return IsLandingPad; }
23219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
23319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// setIsLandingPad - Indicates the block is a landing pad.  That is
23419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// this basic block is entered via an exception handler.
23519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  void setIsLandingPad(bool V = true) { IsLandingPad = V; }
23619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
23719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// getLandingPadSuccessor - If this block has a successor that is a landing
23819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// pad, return it. Otherwise return NULL.
23919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const MachineBasicBlock *getLandingPadSuccessor() const;
24019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Code Layout methods.
242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// moveBefore/moveAfter - move 'this' block before or after the specified
244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// block.  This only moves the block, it does not modify the CFG or adjust
245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// potential fall-throughs at the end of the block.
246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void moveBefore(MachineBasicBlock *NewAfter);
247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void moveAfter(MachineBasicBlock *NewBefore);
248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// updateTerminator - Update the terminator instructions in block to account
250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// for changes to the layout. If the block previously used a fallthrough,
251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// it may now need a branch, and if it previously used branching it may now
252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// be able to use a fallthrough.
253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void updateTerminator();
254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Machine-CFG mutators
25619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
25819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// The Predecessors list of succ is automatically updated. WEIGHT
25919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// parameter is stored in Weights list and it may be used by
26019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// MachineBranchProbabilityInfo analysis to calculate branch probability.
261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
26219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  void addSuccessor(MachineBasicBlock *succ, uint32_t weight = 0);
263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// removeSuccessor - Remove successor from the successors list of this
265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void removeSuccessor(MachineBasicBlock *succ);
268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// removeSuccessor - Remove specified successor from the successors list of
270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// this MachineBasicBlock. The Predecessors list of succ is automatically
271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// updated.  Return the iterator to the element after the one removed.
272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  succ_iterator removeSuccessor(succ_iterator I);
27419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
27519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// replaceSuccessor - Replace successor OLD with NEW and update weight info.
27619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ///
27719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New);
27819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
27919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// transferSuccessors - Transfers all the successors from MBB to this
281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// machine basic block (i.e., copies all the successors fromMBB and
282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// remove all the successors from fromMBB).
283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void transferSuccessors(MachineBasicBlock *fromMBB);
284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// transferSuccessorsAndUpdatePHIs - Transfers all the successors, as
286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// in transferSuccessors, and update PHI operands in the successor blocks
287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// which refer to fromMBB to refer to this.
288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB);
289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// isSuccessor - Return true if the specified MBB is a successor of this
291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// block.
292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isSuccessor(const MachineBasicBlock *MBB) const;
293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// isLayoutSuccessor - Return true if the specified MBB will be emitted
295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// immediately after this block, such that if this block exits by
296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// falling through, control will transfer to the specified MBB. Note
297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// that MBB need not be a successor at all, for example if this block
298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// ends with an unconditional branch to some other block.
299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// canFallThrough - Return true if the block can implicitly transfer
302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// control to the block after it by falling off the end of it.  This should
303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// return false if it can reach the block after it, but it uses an explicit
304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// branch to do so (e.g., a table jump).  True is a conservative answer.
305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool canFallThrough();
306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// Returns a pointer to the first instructon in this block that is not a
308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// PHINode instruction. When adding instruction to the beginning of the
309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// basic block, they should be added before the returned value, not before
310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// the first instruction, which might be PHI.
311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// Returns end() is there's no non-PHI instruction.
312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  iterator getFirstNonPHI();
313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
31419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// SkipPHIsAndLabels - Return the first instruction in MBB after I that is
31519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// not a PHI or a label. This is the correct point to insert copies at the
31619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// beginning of a basic block.
31719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  iterator SkipPHIsAndLabels(iterator I);
31819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getFirstTerminator - returns an iterator to the first terminator
320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// instruction of this basic block. If a terminator does not exist,
321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// it returns end()
322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  iterator getFirstTerminator();
323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
32419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const_iterator getFirstTerminator() const {
32519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return const_cast<MachineBasicBlock*>(this)->getFirstTerminator();
32619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
32719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
32819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// getLastNonDebugInstr - returns an iterator to the last non-debug
32919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// instruction in the basic block, or end()
33019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  iterator getLastNonDebugInstr();
33119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
33219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const_iterator getLastNonDebugInstr() const {
33319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return const_cast<MachineBasicBlock*>(this)->getLastNonDebugInstr();
33419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
33519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// SplitCriticalEdge - Split the critical edge from this block to the
337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// given successor block, and return the newly created block, or null
338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// if splitting is not possible.
339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// This function updates LiveVariables, MachineDominatorTree, and
341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// MachineLoopInfo, as applicable.
342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P);
343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void pop_front() { Insts.pop_front(); }
345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void pop_back() { Insts.pop_back(); }
346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void push_back(MachineInstr *MI) { Insts.push_back(MI); }
347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  template<typename IT>
348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
35019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  iterator insertAfter(iterator I, MachineInstr *M) {
35119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return Insts.insertAfter(I, M);
35219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // erase - Remove the specified element or range from the instruction list.
355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // These functions delete any instructions removed.
356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //
357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  iterator erase(iterator I)             { return Insts.erase(I); }
358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineInstr *remove(MachineInstr *I)  { return Insts.remove(I); }
360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void clear()                           { Insts.clear(); }
361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// splice - Take an instruction from MBB 'Other' at the position From,
363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// and insert it into this MBB right before 'where'.
364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void splice(iterator where, MachineBasicBlock *Other, iterator From) {
365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Insts.splice(where, Other->Insts, From);
366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// splice - Take a block of instructions from MBB 'Other' in the range [From,
369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// To), and insert them into this MBB right before 'where'.
370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void splice(iterator where, MachineBasicBlock *Other, iterator From,
371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              iterator To) {
372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Insts.splice(where, Other->Insts, From, To);
373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// removeFromParent - This method unlinks 'this' from the containing
376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// function, and returns it, but does not delete it.
377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *removeFromParent();
378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// eraseFromParent - This method unlinks 'this' from the containing
380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// function and deletes it.
381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void eraseFromParent();
382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// 'Old', change the code and CFG so that it branches to 'New' instead.
385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// the CFG to be inserted.  If we have proven that MBB can only branch to
389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// DestB can be null. Besides DestA and DestB, retain other edges leading
391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// to LandingPads (currently there can be only one; we don't check or require
392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// that here). Note it is possible that DestA and/or DestB are LandingPads.
393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            MachineBasicBlock *DestB,
395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            bool isCond);
396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// any DBG_VALUE instructions.  Return UnknownLoc if there is none.
399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc findDebugLoc(MachineBasicBlock::iterator &MBBI);
400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Debugging methods.
402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void dump() const;
40319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  void print(raw_ostream &OS, SlotIndexes* = 0) const;
404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getNumber - MachineBasicBlocks are uniquely numbered at the function
406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// level, unless they're not in a MachineFunction yet, in which case this
407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// will return -1.
408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int getNumber() const { return Number; }
410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void setNumber(int N) { Number = N; }
411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getSymbol - Return the MCSymbol for this basic block.
413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MCSymbol *getSymbol() const;
41519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
41619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
41719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanprivate:
41819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// getWeightIterator - Return weight iterator corresponding to the I
41919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// successor iterator.
42019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  weight_iterator getWeightIterator(succ_iterator I);
42119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
42219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  friend class MachineBranchProbabilityInfo;
42319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
42419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// getSuccWeight - Return weight of the edge from this block to MBB. This
42519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// method should NOT be called directly, but by using getEdgeWeight method
42619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// from MachineBranchProbabilityInfo class.
42719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  uint32_t getSuccWeight(MachineBasicBlock *succ);
42819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
42919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
43019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Methods used to maintain doubly linked list of blocks...
431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  friend struct ilist_traits<MachineBasicBlock>;
432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Machine-CFG mutators
434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// Don't do this unless you know what you're doing, because it doesn't
437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// update pred's successors list. Use pred->addSuccessor instead.
438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void addPredecessor(MachineBasicBlock *pred);
440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// removePredecessor - Remove pred as a predecessor of this
442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// MachineBasicBlock. Don't do this unless you know what you're
443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// doing, because it doesn't update pred's successors list. Use
444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// pred->removeSuccessor instead.
445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void removePredecessor(MachineBasicBlock *pred);
447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanraw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid WriteAsOperand(raw_ostream &, const MachineBasicBlock*, bool t);
452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
45319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// This is useful when building IndexedMaps keyed on basic block pointers.
45419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstruct MBB2NumberFunctor :
45519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  public std::unary_function<const MachineBasicBlock*, unsigned> {
45619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned operator()(const MachineBasicBlock *MBB) const {
45719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return MBB->getNumber();
45819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
45919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman};
46019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===--------------------------------------------------------------------===//
462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GraphTraits specializations for machine basic block graphs (machine-CFGs)
463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===--------------------------------------------------------------------===//
464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Provide specializations of GraphTraits to be able to treat a
466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// MachineFunction as a graph of MachineBasicBlocks...
467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <> struct GraphTraits<MachineBasicBlock *> {
470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef MachineBasicBlock NodeType;
471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef MachineBasicBlock::succ_iterator ChildIteratorType;
472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
473894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline ChildIteratorType child_begin(NodeType *N) {
475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return N->succ_begin();
476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline ChildIteratorType child_end(NodeType *N) {
478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return N->succ_end();
479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <> struct GraphTraits<const MachineBasicBlock *> {
483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef const MachineBasicBlock NodeType;
484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline ChildIteratorType child_begin(NodeType *N) {
488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return N->succ_begin();
489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline ChildIteratorType child_end(NodeType *N) {
491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return N->succ_end();
492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Provide specializations of GraphTraits to be able to treat a
496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// MachineFunction as a graph of MachineBasicBlocks... and to walk it
497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// in inverse order.  Inverse order for a function is considered
498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// to be when traversing the predecessor edges of a MBB
499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// instead of the successor edges.
500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef MachineBasicBlock NodeType;
503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef MachineBasicBlock::pred_iterator ChildIteratorType;
504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return G.Graph;
506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline ChildIteratorType child_begin(NodeType *N) {
508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return N->pred_begin();
509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline ChildIteratorType child_end(NodeType *N) {
511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return N->pred_end();
512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef const MachineBasicBlock NodeType;
517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return G.Graph;
520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline ChildIteratorType child_begin(NodeType *N) {
522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return N->pred_begin();
523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline ChildIteratorType child_end(NodeType *N) {
525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return N->pred_end();
526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} // End llvm namespace
530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
532