1 //===-- llvm/BasicBlock.h - Represent a basic block in the VM ----*- C++ -*--=//
3 // This file contains the declaration of the BasicBlock class, which represents
4 // a single basic block in the VM.
6 // Note that basic blocks themselves are Def's, because they are referenced
7 // by instructions like branches and can go in switch tables and stuff...
9 // This may see wierd at first, but it's really pretty cool. :)
11 //===----------------------------------------------------------------------===//
13 // Note that well formed basic blocks are formed of a list of instructions
14 // followed by a single TerminatorInst instruction. TerminatorInst's may not
15 // occur in the middle of basic blocks, and must terminate the blocks.
17 // This code allows malformed basic blocks to occur, because it may be useful
18 // in the intermediate stage of analysis or modification of a program.
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_BASICBLOCK_H
23 #define LLVM_BASICBLOCK_H
25 #include "llvm/Value.h"
26 #include "llvm/ValueHolder.h"
27 #include "llvm/Support/GraphTraits.h"
28 #include "llvm/InstrTypes.h"
34 class MachineCodeForBasicBlock;
36 class BasicBlock : public Value { // Basic blocks are data objects also
37 template <class _Ptr, class _USE_iterator> class PredIterator;
38 template <class _Term, class _BB> class SuccIterator;
40 typedef ValueHolder<Instruction, BasicBlock, Method> InstListType;
42 InstListType InstList;
43 MachineCodeForBasicBlock* machineInstrVec;
45 friend class ValueHolder<BasicBlock,Method,Method>;
46 void setParent(Method *parent);
49 // Instruction iterators...
50 typedef InstListType::iterator iterator;
51 typedef InstListType::const_iterator const_iterator;
52 typedef reverse_iterator<const_iterator> const_reverse_iterator;
53 typedef reverse_iterator<iterator> reverse_iterator;
55 // Predecessor and successor iterators...
56 typedef PredIterator<BasicBlock, Value::use_iterator> pred_iterator;
57 typedef PredIterator<const BasicBlock,
58 Value::use_const_iterator> pred_const_iterator;
59 typedef SuccIterator<TerminatorInst*, BasicBlock> succ_iterator;
60 typedef SuccIterator<const TerminatorInst*,
61 const BasicBlock> succ_const_iterator;
64 BasicBlock(const string &Name = "", Method *Parent = 0);
67 // Specialize setName to take care of symbol table majik
68 virtual void setName(const string &name, SymbolTable *ST = 0);
70 // getParent - Return the enclosing method, or null if none
71 const Method *getParent() const { return InstList.getParent(); }
72 Method *getParent() { return InstList.getParent(); }
74 // getTerminator() - If this is a well formed basic block, then this returns
75 // a pointer to the terminator instruction. If it is not, then you get a null
78 TerminatorInst *getTerminator();
79 const TerminatorInst *const getTerminator() const;
81 // Machine code accessor...
82 inline MachineCodeForBasicBlock& getMachineInstrVec() const {
83 return *machineInstrVec;
86 //===--------------------------------------------------------------------===//
87 // Instruction iterator methods
89 inline iterator begin() { return InstList.begin(); }
90 inline const_iterator begin() const { return InstList.begin(); }
91 inline iterator end () { return InstList.end(); }
92 inline const_iterator end () const { return InstList.end(); }
94 inline reverse_iterator rbegin() { return InstList.rbegin(); }
95 inline const_reverse_iterator rbegin() const { return InstList.rbegin(); }
96 inline reverse_iterator rend () { return InstList.rend(); }
97 inline const_reverse_iterator rend () const { return InstList.rend(); }
99 inline unsigned size() const { return InstList.size(); }
100 inline bool empty() const { return InstList.empty(); }
101 inline const Instruction *front() const { return InstList.front(); }
102 inline Instruction *front() { return InstList.front(); }
103 inline const Instruction *back() const { return InstList.back(); }
104 inline Instruction *back() { return InstList.back(); }
106 // getInstList() - Return the underlying instruction list container. You need
107 // to access it directly if you want to modify it currently.
109 const InstListType &getInstList() const { return InstList; }
110 InstListType &getInstList() { return InstList; }
112 // Methods for support type inquiry through isa, cast, and dyn_cast:
113 static inline bool isa(const BasicBlock *BB) { return true; }
114 static inline bool isa(const Value *V) {
115 return V->getValueType() == Value::BasicBlockVal;
118 // hasConstantPoolReferences() - This predicate is true if there is a
119 // reference to this basic block in the constant pool for this method. For
120 // example, if a block is reached through a switch table, that table resides
121 // in the constant pool, and the basic block is reference from it.
123 bool hasConstantPoolReferences() const;
125 // dropAllReferences() - This function causes all the subinstructions to "let
126 // go" of all references that they are maintaining. This allows one to
127 // 'delete' a whole class at a time, even though there may be circular
128 // references... first all references are dropped, and all use counts go to
129 // zero. Then everything is delete'd for real. Note that no operations are
130 // valid on an object that has "dropped all references", except operator
133 void dropAllReferences();
135 // removePredecessor - This method is used to notify a BasicBlock that the
136 // specified Predecessor of the block is no longer able to reach it. This is
137 // actually not used to update the Predecessor list, but is actually used to
138 // update the PHI nodes that reside in the block. Note that this should be
139 // called while the predecessor still refers to this block.
141 void removePredecessor(BasicBlock *Pred);
143 // splitBasicBlock - This splits a basic block into two at the specified
144 // instruction. Note that all instructions BEFORE the specified iterator stay
145 // as part of the original basic block, an unconditional branch is added to
146 // the new BB, and the rest of the instructions in the BB are moved to the new
147 // BB, including the old terminator. The newly formed BasicBlock is returned.
148 // This function invalidates the specified iterator.
150 // Note that this only works on well formed basic blocks (must have a
151 // terminator), and 'I' must not be the end of instruction list (which would
152 // cause a degenerate basic block to be formed, having a terminator inside of
155 BasicBlock *splitBasicBlock(iterator I);
158 //===--------------------------------------------------------------------===//
159 // Predecessor and Successor Iterators
161 template <class _Ptr, class _USE_iterator> // Predecessor Iterator
162 class PredIterator : public std::bidirectional_iterator<_Ptr, ptrdiff_t> {
166 typedef PredIterator<_Ptr,_USE_iterator> _Self;
168 inline void advancePastConstPool() {
170 // Loop to ignore constant pool references
171 while (It != BB->use_end() &&
172 (((*It)->getValueType() != Value::InstructionVal) ||
173 !(((Instruction*)(*It))->isTerminator())))
177 inline PredIterator(_Ptr *bb) : BB(bb), It(bb->use_begin()) {
178 advancePastConstPool();
180 inline PredIterator(_Ptr *bb, bool) : BB(bb), It(bb->use_end()) {}
182 inline bool operator==(const _Self& x) const { return It == x.It; }
183 inline bool operator!=(const _Self& x) const { return !operator==(x); }
185 inline pointer operator*() const {
186 return cast<Instruction>(*It)->getParent();
188 inline pointer *operator->() const { return &(operator*()); }
190 inline _Self& operator++() { // Preincrement
191 ++It; advancePastConstPool();
195 inline _Self operator++(int) { // Postincrement
196 _Self tmp = *this; ++*this; return tmp;
199 inline _Self& operator--() { --It; return *this; } // Predecrement
200 inline _Self operator--(int) { // Postdecrement
201 _Self tmp = *this; --*this; return tmp;
205 inline pred_iterator pred_begin() { return pred_iterator(this); }
206 inline pred_const_iterator pred_begin() const {
207 return pred_const_iterator(this);
209 inline pred_iterator pred_end() { return pred_iterator(this, true); }
210 inline pred_const_iterator pred_end() const {
211 return pred_const_iterator(this, true);
214 template <class _Term, class _BB> // Successor Iterator
215 class SuccIterator : public std::bidirectional_iterator<_BB, ptrdiff_t> {
219 typedef SuccIterator<_Term, _BB> _Self;
220 // TODO: This can be random access iterator, need operator+ and stuff tho
222 inline SuccIterator(_Term T) : Term(T), idx(0) { // begin iterator
223 assert(T && "getTerminator returned null!");
225 inline SuccIterator(_Term T, bool) // end iterator
226 : Term(T), idx(Term->getNumSuccessors()) {
227 assert(T && "getTerminator returned null!");
230 inline bool operator==(const _Self& x) const { return idx == x.idx; }
231 inline bool operator!=(const _Self& x) const { return !operator==(x); }
233 inline pointer operator*() const { return Term->getSuccessor(idx); }
234 inline pointer operator->() const { return operator*(); }
236 inline _Self& operator++() { ++idx; return *this; } // Preincrement
237 inline _Self operator++(int) { // Postincrement
238 _Self tmp = *this; ++*this; return tmp;
241 inline _Self& operator--() { --idx; return *this; } // Predecrement
242 inline _Self operator--(int) { // Postdecrement
243 _Self tmp = *this; --*this; return tmp;
247 inline succ_iterator succ_begin() { return succ_iterator(getTerminator()); }
248 inline succ_const_iterator succ_begin() const {
249 return succ_const_iterator(getTerminator());
251 inline succ_iterator succ_end() {return succ_iterator(getTerminator(), true);}
252 inline succ_const_iterator succ_end() const {
253 return succ_const_iterator(getTerminator(), true);
258 //===--------------------------------------------------------------------===//
259 // GraphTraits specializations for basic block graphs (CFGs)
260 //===--------------------------------------------------------------------===//
262 // Provide specializations of GraphTraits to be able to treat a method as a
263 // graph of basic blocks...
265 template <> struct GraphTraits<BasicBlock*> {
266 typedef BasicBlock NodeType;
267 typedef BasicBlock::succ_iterator ChildIteratorType;
269 static NodeType *getEntryNode(BasicBlock *BB) { return BB; }
270 static inline ChildIteratorType child_begin(NodeType *N) {
271 return N->succ_begin();
273 static inline ChildIteratorType child_end(NodeType *N) {
274 return N->succ_end();
278 template <> struct GraphTraits<const BasicBlock*> {
279 typedef const BasicBlock NodeType;
280 typedef BasicBlock::succ_const_iterator ChildIteratorType;
282 static NodeType *getEntryNode(const BasicBlock *BB) { return BB; }
284 static inline ChildIteratorType child_begin(NodeType *N) {
285 return N->succ_begin();
287 static inline ChildIteratorType child_end(NodeType *N) {
288 return N->succ_end();
292 // Provide specializations of GraphTraits to be able to treat a method as a
293 // graph of basic blocks... and to walk it in inverse order. Inverse order for
294 // a method is considered to be when traversing the predecessor edges of a BB
295 // instead of the successor edges.
297 template <> struct GraphTraits<Inverse<BasicBlock*> > {
298 typedef BasicBlock NodeType;
299 typedef BasicBlock::pred_iterator ChildIteratorType;
300 static NodeType *getEntryNode(Inverse<BasicBlock *> G) { return G.Graph; }
301 static inline ChildIteratorType child_begin(NodeType *N) {
302 return N->pred_begin();
304 static inline ChildIteratorType child_end(NodeType *N) {
305 return N->pred_end();
309 template <> struct GraphTraits<Inverse<const BasicBlock*> > {
310 typedef const BasicBlock NodeType;
311 typedef BasicBlock::pred_const_iterator ChildIteratorType;
312 static NodeType *getEntryNode(Inverse<const BasicBlock*> G) {
315 static inline ChildIteratorType child_begin(NodeType *N) {
316 return N->pred_begin();
318 static inline ChildIteratorType child_end(NodeType *N) {
319 return N->pred_end();