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" // Get the definition of Value
26 #include "llvm/ValueHolder.h"
27 #include "llvm/InstrTypes.h"
35 typedef UseTy<BasicBlock> BasicBlockUse;
37 class BasicBlock : public Value { // Basic blocks are data objects also
39 typedef ValueHolder<Instruction, BasicBlock> InstListType;
41 InstListType InstList;
43 friend class ValueHolder<BasicBlock,Method>;
44 void setParent(Method *parent);
47 BasicBlock(const string &Name = "", Method *Parent = 0);
50 // Specialize setName to take care of symbol table majik
51 virtual void setName(const string &name);
53 const Method *getParent() const { return (const Method*)InstList.getParent();}
54 Method *getParent() { return (Method*)InstList.getParent(); }
56 const InstListType &getInstList() const { return InstList; }
57 InstListType &getInstList() { return InstList; }
59 // getTerminator() - If this is a well formed basic block, then this returns
60 // a pointer to the terminator instruction. If it is not, then you get a null
63 TerminatorInst *getTerminator();
64 const TerminatorInst *const getTerminator() const;
66 // hasConstantPoolReferences() - This predicate is true if there is a
67 // reference to this basic block in the constant pool for this method. For
68 // example, if a block is reached through a switch table, that table resides
69 // in the constant pool, and the basic block is reference from it.
71 bool hasConstantPoolReferences() const;
73 // dropAllReferences() - This function causes all the subinstructions to "let
74 // go" of all references that they are maintaining. This allows one to
75 // 'delete' a whole class at a time, even though there may be circular
76 // references... first all references are dropped, and all use counts go to
77 // zero. Then everything is delete'd for real. Note that no operations are
78 // valid on an object that has "dropped all references", except operator
81 void dropAllReferences();
83 // splitBasicBlock - This splits a basic block into two at the specified
84 // instruction. Note that all instructions BEFORE the specified iterator stay
85 // as part of the original basic block, an unconditional branch is added to
86 // the new BB, and the rest of the instructions in the BB are moved to the new
87 // BB, including the old terminator. The newly formed BasicBlock is returned.
88 // This function invalidates the specified iterator.
90 // Note that this only works on well formed basic blocks (must have a
91 // terminator), and 'I' must not be the end of instruction list (which would
92 // cause a degenerate basic block to be formed, having a terminator inside of
95 BasicBlock *splitBasicBlock(InstListType::iterator I);
97 //===--------------------------------------------------------------------===//
98 // Predecessor iterator code
99 //===--------------------------------------------------------------------===//
101 // This is used to figure out what basic blocks we could be coming from.
104 // Forward declare iterator class template...
105 template <class _Ptr, class _USE_iterator> class PredIterator;
107 typedef PredIterator<BasicBlock*, use_iterator> pred_iterator;
108 typedef PredIterator<const BasicBlock*,
109 use_const_iterator> pred_const_iterator;
111 inline pred_iterator pred_begin() ;
112 inline pred_const_iterator pred_begin() const;
113 inline pred_iterator pred_end() ;
114 inline pred_const_iterator pred_end() const;
116 //===--------------------------------------------------------------------===//
117 // Successor iterator code
118 //===--------------------------------------------------------------------===//
120 // This is used to figure out what basic blocks we could be going to...
123 // Forward declare iterator class template...
124 template <class _Term, class _BB> class SuccIterator;
126 typedef SuccIterator<TerminatorInst*, BasicBlock*> succ_iterator;
127 typedef SuccIterator<const TerminatorInst*,
128 const BasicBlock*> succ_const_iterator;
130 inline succ_iterator succ_begin() ;
131 inline succ_const_iterator succ_begin() const;
132 inline succ_iterator succ_end() ;
133 inline succ_const_iterator succ_end() const;
135 //===--------------------------------------------------------------------===//
136 // END of interesting code...
137 //===--------------------------------------------------------------------===//
139 // Thank god C++ compilers are good at stomping out tons of templated code...
141 template <class _Ptr, class _USE_iterator> // Predecessor Iterator
146 typedef PredIterator<_Ptr,_USE_iterator> _Self;
148 typedef bidirectional_iterator_tag iterator_category;
149 typedef _Ptr pointer;
151 inline PredIterator(_Ptr BB) : ThisBB(BB), It(BB->use_begin()) {}
152 inline PredIterator(_Ptr BB, bool) : ThisBB(BB), It(BB->use_end()) {}
154 inline bool operator==(const _Self& x) const { return It == x.It; }
155 inline bool operator!=(const _Self& x) const { return !operator==(x); }
157 inline pointer operator*() const {
158 assert ((*It)->getValueType() == Value::InstructionVal);
159 return ((Instruction *)(*It))->getParent();
161 inline pointer *operator->() const { return &(operator*()); }
163 inline _Self& operator++() { // Preincrement
164 do { // Loop to ignore constant pool references
166 } while (It != ThisBB->use_end() &&
167 ((*It)->getValueType() != Value::ConstantVal));
170 //((*It)->getValueType() != Value::BasicBlockVal));
174 inline _Self operator++(int) { // Postincrement
175 _Self tmp = *this; ++*this; return tmp;
178 inline _Self& operator--() { --It; return *this; } // Predecrement
179 inline _Self operator--(int) { // Postdecrement
180 _Self tmp = *this; --*this; return tmp;
184 template <class _Term, class _BB> // Successor Iterator
189 typedef SuccIterator<_Term, _BB> _Self;
190 typedef forward_iterator_tag iterator_category;
193 inline SuccIterator(_Term T) : Term(T), idx(0) {} // begin iterator
194 inline SuccIterator(_Term T, bool)
195 : Term(T), idx(Term->getNumSuccessors()) {} // end iterator
197 inline bool operator==(const _Self& x) const { return idx == x.idx; }
198 inline bool operator!=(const _Self& x) const { return !operator==(x); }
200 inline pointer operator*() const { return Term->getSuccessor(idx); }
201 inline pointer *operator->() const { return &(operator*()); }
203 inline _Self& operator++() { ++idx; return *this; } // Preincrement
204 inline _Self operator++(int) { // Postincrement
205 _Self tmp = *this; ++*this; return tmp;
208 inline _Self& operator--() { --idx; return *this; } // Predecrement
209 inline _Self operator--(int) { // Postdecrement
210 _Self tmp = *this; --*this; return tmp;
216 //===--------------------------------------------------------------------===//
217 // Implement some stuff prototyped above...
218 //===--------------------------------------------------------------------===//
220 inline BasicBlock::pred_iterator BasicBlock::pred_begin() {
221 return pred_iterator(this);
223 inline BasicBlock::pred_const_iterator BasicBlock::pred_begin() const {
224 return pred_const_iterator(this);
226 inline BasicBlock::pred_iterator BasicBlock::pred_end() {
227 return pred_iterator(this,true);
229 inline BasicBlock::pred_const_iterator BasicBlock::pred_end() const {
230 return pred_const_iterator(this,true);
233 inline BasicBlock::succ_iterator BasicBlock::succ_begin() {
234 return succ_iterator(getTerminator());
236 inline BasicBlock::succ_const_iterator BasicBlock::succ_begin() const {
237 return succ_const_iterator(getTerminator());
239 inline BasicBlock::succ_iterator BasicBlock::succ_end() {
240 return succ_iterator(getTerminator(),true);
242 inline BasicBlock::succ_const_iterator BasicBlock::succ_end() const {
243 return succ_const_iterator(getTerminator(),true);