Fixes for BB iterators, additional methods added for DCE pass
[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 void advancePastConstPool() {
152       // Loop to ignore constant pool references
153       while (It != ThisBB->use_end() && 
154              ((*It)->getValueType() != Value::InstructionVal))
155         ++It;
156     }
157
158     inline PredIterator(_Ptr BB) : ThisBB(BB), It(BB->use_begin()) {
159       advancePastConstPool();
160     }
161     inline PredIterator(_Ptr BB, bool) : ThisBB(BB), It(BB->use_end()) {}
162
163     inline bool operator==(const _Self& x) const { return It == x.It; }
164     inline bool operator!=(const _Self& x) const { return !operator==(x); }
165
166     inline pointer operator*() const { 
167       assert ((*It)->getValueType() == Value::InstructionVal);
168       return ((Instruction *)(*It))->getParent(); 
169     }
170     inline pointer *operator->() const { return &(operator*()); }
171
172     inline _Self& operator++() {   // Preincrement
173       ++It; advancePastConstPool();
174       return *this; 
175     }
176
177     inline _Self operator++(int) { // Postincrement
178       _Self tmp = *this; ++*this; return tmp; 
179     }
180
181     inline _Self& operator--() { --It; return *this; }  // Predecrement
182     inline _Self operator--(int) { // Postdecrement
183       _Self tmp = *this; --*this; return tmp;
184     }
185   };
186
187   template <class _Term, class _BB>           // Successor Iterator
188   class SuccIterator {
189     const _Term Term;
190     unsigned idx;
191   public:
192     typedef SuccIterator<_Term, _BB> _Self;
193     typedef forward_iterator_tag iterator_category;
194     typedef _BB pointer;
195     
196     inline SuccIterator(_Term T) : Term(T), idx(0) {}         // begin iterator
197     inline SuccIterator(_Term T, bool) 
198       : Term(T), idx(Term->getNumSuccessors()) {}             // end iterator
199     
200     inline bool operator==(const _Self& x) const { return idx == x.idx; }
201     inline bool operator!=(const _Self& x) const { return !operator==(x); }
202
203     inline pointer operator*() const { return Term->getSuccessor(idx); }
204     inline pointer *operator->() const { return &(operator*()); }
205     
206     inline _Self& operator++() { ++idx; return *this; } // Preincrement
207     inline _Self operator++(int) { // Postincrement
208       _Self tmp = *this; ++*this; return tmp; 
209     }
210
211     inline _Self& operator--() { --idx; return *this; }  // Predecrement
212     inline _Self operator--(int) { // Postdecrement
213       _Self tmp = *this; --*this; return tmp;
214     }
215   };
216 };
217
218
219 //===--------------------------------------------------------------------===//
220 // Implement some stuff prototyped above...
221 //===--------------------------------------------------------------------===//
222
223 inline BasicBlock::pred_iterator       BasicBlock::pred_begin()       {
224   return pred_iterator(this);
225 }
226 inline BasicBlock::pred_const_iterator BasicBlock::pred_begin() const {
227   return pred_const_iterator(this);
228 }
229 inline BasicBlock::pred_iterator       BasicBlock::pred_end()         {
230   return pred_iterator(this,true);
231 }
232 inline BasicBlock::pred_const_iterator BasicBlock::pred_end()   const {
233   return pred_const_iterator(this,true);
234 }
235
236 inline BasicBlock::succ_iterator       BasicBlock::succ_begin()       {
237   return succ_iterator(getTerminator());
238 }
239 inline BasicBlock::succ_const_iterator BasicBlock::succ_begin() const {
240   return succ_const_iterator(getTerminator());
241 }
242 inline BasicBlock::succ_iterator       BasicBlock::succ_end()         {
243   return succ_iterator(getTerminator(),true);
244 }
245 inline BasicBlock::succ_const_iterator BasicBlock::succ_end()   const {
246   return succ_const_iterator(getTerminator(),true);
247 }
248
249 #endif