6873ef2deb73ce231634915083243e52a9d527cb
[oota-llvm.git] / include / llvm / BasicBlock.h
1 //===-- llvm/BasicBlock.h - Represent a basic block in the VM ----*- C++ -*--=//
2 //
3 // This file contains the declaration of the BasicBlock class, which represents
4 // a single basic block in the VM.
5 //
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...
8 //
9 // This may see wierd at first, but it's really pretty cool.  :)
10 //
11 //===----------------------------------------------------------------------===//
12 //
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.
16 //
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.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #ifndef LLVM_BASICBLOCK_H
23 #define LLVM_BASICBLOCK_H
24
25 #include "llvm/Value.h"               // Get the definition of Value
26 #include "llvm/ValueHolder.h"
27 #include "llvm/InstrTypes.h"
28 #include <list>
29
30 class Instruction;
31 class Method;
32 class BasicBlock;
33 class TerminatorInst;
34
35 typedef UseTy<BasicBlock> BasicBlockUse;
36
37 class BasicBlock : public Value {       // Basic blocks are data objects also
38 public:
39   typedef ValueHolder<Instruction, BasicBlock> InstListType;
40 private :
41   InstListType InstList;
42
43   friend class ValueHolder<BasicBlock,Method>;
44   void setParent(Method *parent);
45
46 public:
47   BasicBlock(const string &Name = "", Method *Parent = 0);
48   ~BasicBlock();
49
50   // Specialize setName to take care of symbol table majik
51   virtual void setName(const string &name);
52
53   const Method *getParent() const { return (const Method*)InstList.getParent();}
54         Method *getParent()       { return (Method*)InstList.getParent(); }
55
56   const InstListType &getInstList() const { return InstList; }
57         InstListType &getInstList()       { return InstList; }
58
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
61   // pointer back.
62   //
63   TerminatorInst *getTerminator();
64   const TerminatorInst *const getTerminator() const;
65
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.
70   //
71   bool hasConstantPoolReferences() const;
72
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 
79   // delete.
80   //
81   void dropAllReferences();
82
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.
89   //
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
93   // the basic block).
94   //
95   BasicBlock *splitBasicBlock(InstListType::iterator I);
96
97   //===--------------------------------------------------------------------===//
98   // Predecessor iterator code
99   //===--------------------------------------------------------------------===//
100   // 
101   // This is used to figure out what basic blocks we could be coming from.
102   //
103
104   // Forward declare iterator class template...
105   template <class _Ptr, class _USE_iterator> class PredIterator;
106
107   typedef PredIterator<BasicBlock*, use_iterator> pred_iterator;
108   typedef PredIterator<const BasicBlock*, 
109                        use_const_iterator> pred_const_iterator;
110
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;
115
116   //===--------------------------------------------------------------------===//
117   // Successor iterator code
118   //===--------------------------------------------------------------------===//
119   // 
120   // This is used to figure out what basic blocks we could be going to...
121   //
122
123   // Forward declare iterator class template...
124   template <class _Term, class _BB> class SuccIterator;
125
126   typedef SuccIterator<TerminatorInst*, BasicBlock*> succ_iterator;
127   typedef SuccIterator<const TerminatorInst*, 
128                        const BasicBlock*> succ_const_iterator;
129
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;
134
135   //===--------------------------------------------------------------------===//
136   // END of interesting code...
137   //===--------------------------------------------------------------------===//
138   //
139   // Thank god C++ compilers are good at stomping out tons of templated code...
140   //
141   template <class _Ptr,  class _USE_iterator> // Predecessor Iterator
142   class PredIterator {
143     const _Ptr ThisBB;
144     _USE_iterator It;
145   public:
146     typedef PredIterator<_Ptr,_USE_iterator> _Self;
147
148     typedef bidirectional_iterator_tag iterator_category;
149     typedef _Ptr pointer;
150
151     inline PredIterator(_Ptr BB) : ThisBB(BB), It(BB->use_begin()) {}
152     inline PredIterator(_Ptr BB, bool) : ThisBB(BB), It(BB->use_end()) {}
153
154     inline bool operator==(const _Self& x) const { return It == x.It; }
155     inline bool operator!=(const _Self& x) const { return !operator==(x); }
156
157     inline pointer operator*() const { 
158       assert ((*It)->getValueType() == Value::InstructionVal);
159       return ((Instruction *)(*It))->getParent(); 
160     }
161     inline pointer *operator->() const { return &(operator*()); }
162
163     inline _Self& operator++() {   // Preincrement
164       do {       // Loop to ignore constant pool references
165         ++It;
166       } while (It != ThisBB->use_end() && 
167                ((*It)->getValueType() != Value::ConstantVal));
168
169                // DOES THIS WORK???            
170       //((*It)->getValueType() != Value::BasicBlockVal));
171       return *this; 
172     }
173
174     inline _Self operator++(int) { // Postincrement
175       _Self tmp = *this; ++*this; return tmp; 
176     }
177
178     inline _Self& operator--() { --It; return *this; }  // Predecrement
179     inline _Self operator--(int) { // Postdecrement
180       _Self tmp = *this; --*this; return tmp;
181     }
182   };
183
184   template <class _Term, class _BB>           // Successor Iterator
185   class SuccIterator {
186     const _Term Term;
187     unsigned idx;
188   public:
189     typedef SuccIterator<_Term, _BB> _Self;
190     typedef forward_iterator_tag iterator_category;
191     typedef _BB pointer;
192     
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
196     
197     inline bool operator==(const _Self& x) const { return idx == x.idx; }
198     inline bool operator!=(const _Self& x) const { return !operator==(x); }
199
200     inline pointer operator*() const { return Term->getSuccessor(idx); }
201     inline pointer *operator->() const { return &(operator*()); }
202     
203     inline _Self& operator++() { ++idx; return *this; } // Preincrement
204     inline _Self operator++(int) { // Postincrement
205       _Self tmp = *this; ++*this; return tmp; 
206     }
207
208     inline _Self& operator--() { --idx; return *this; }  // Predecrement
209     inline _Self operator--(int) { // Postdecrement
210       _Self tmp = *this; --*this; return tmp;
211     }
212   };
213 };
214
215
216 //===--------------------------------------------------------------------===//
217 // Implement some stuff prototyped above...
218 //===--------------------------------------------------------------------===//
219
220 inline BasicBlock::pred_iterator       BasicBlock::pred_begin()       {
221   return pred_iterator(this);
222 }
223 inline BasicBlock::pred_const_iterator BasicBlock::pred_begin() const {
224   return pred_const_iterator(this);
225 }
226 inline BasicBlock::pred_iterator       BasicBlock::pred_end()         {
227   return pred_iterator(this,true);
228 }
229 inline BasicBlock::pred_const_iterator BasicBlock::pred_end()   const {
230   return pred_const_iterator(this,true);
231 }
232
233 inline BasicBlock::succ_iterator       BasicBlock::succ_begin()       {
234   return succ_iterator(getTerminator());
235 }
236 inline BasicBlock::succ_const_iterator BasicBlock::succ_begin() const {
237   return succ_const_iterator(getTerminator());
238 }
239 inline BasicBlock::succ_iterator       BasicBlock::succ_end()         {
240   return succ_iterator(getTerminator(),true);
241 }
242 inline BasicBlock::succ_const_iterator BasicBlock::succ_end()   const {
243   return succ_const_iterator(getTerminator(),true);
244 }
245
246 #endif