Implement DCE of global values
[oota-llvm.git] / include / llvm / Function.h
1 //===-- llvm/Method.h - Class to represent a single VM method ----*- C++ -*--=//
2 //
3 // This file contains the declaration of the Method class, which represents a 
4 // single Method/function/procedure in the VM.
5 //
6 // Note that basic blocks themselves are Def's, because they are referenced
7 // by instructions like calls and can go in virtual function tables and stuff.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef LLVM_METHOD_H
12 #define LLVM_METHOD_H
13
14 #include "llvm/SymTabValue.h"
15 #include "llvm/BasicBlock.h"
16 #include "llvm/GlobalValue.h"
17
18 class Instruction;
19 class BasicBlock;
20 class MethodArgument;
21 class MethodType;
22 class Module;
23
24 class Method : public GlobalValue, public SymTabValue {
25 public:
26   typedef ValueHolder<MethodArgument, Method, Method> ArgumentListType;
27   typedef ValueHolder<BasicBlock    , Method, Method> BasicBlocksType;
28
29   // BasicBlock iterators...
30   typedef BasicBlocksType::iterator iterator;
31   typedef BasicBlocksType::const_iterator const_iterator;
32   typedef reverse_iterator<const_iterator> const_reverse_iterator;
33   typedef reverse_iterator<iterator>             reverse_iterator;
34
35 private:
36
37   // Important things that make up a method!
38   BasicBlocksType  BasicBlocks;         // The basic blocks
39   ArgumentListType ArgumentList;        // The formal arguments
40   
41   friend class ValueHolder<Method, Module, Module>;
42   void setParent(Module *parent);
43
44 public:
45   Method(const MethodType *Ty, const string &Name = "");
46   ~Method();
47
48   // Specialize setName to handle symbol table majik...
49   virtual void setName(const string &name, SymbolTable *ST = 0);
50
51   const Type *getReturnType() const;        // Return the return type of method
52   const MethodType *getMethodType() const;  // Return the MethodType for me
53
54   // Is the body of this method unknown? (the basic block list is empty if so)
55   // this is true for external methods, defined as forward "declare"ations
56   bool isExternal() const { return BasicBlocks.empty(); }
57
58   // Get the underlying elements of the Method... both the argument list and
59   // basic block list are empty for external methods.
60   //
61   inline const ArgumentListType &getArgumentList() const{ return ArgumentList; }
62   inline       ArgumentListType &getArgumentList()      { return ArgumentList; }
63
64   inline const BasicBlocksType  &getBasicBlocks() const { return BasicBlocks; }
65   inline       BasicBlocksType  &getBasicBlocks()       { return BasicBlocks; }
66
67   inline const BasicBlock       *getEntryNode() const   { return front(); }
68   inline       BasicBlock       *getEntryNode()         { return front(); }
69   
70   //===--------------------------------------------------------------------===//
71   // BasicBlock iterator forwarding functions
72   //
73   inline iterator                begin()       { return BasicBlocks.begin(); }
74   inline const_iterator          begin() const { return BasicBlocks.begin(); }
75   inline iterator                end  ()       { return BasicBlocks.end();   }
76   inline const_iterator          end  () const { return BasicBlocks.end();   }
77
78   inline reverse_iterator       rbegin()       { return BasicBlocks.rbegin(); }
79   inline const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
80   inline reverse_iterator       rend  ()       { return BasicBlocks.rend();   }
81   inline const_reverse_iterator rend  () const { return BasicBlocks.rend();   }
82
83   inline unsigned                 size() const { return BasicBlocks.size(); }
84   inline bool                    empty() const { return BasicBlocks.empty(); }
85   inline const BasicBlock       *front() const { return BasicBlocks.front(); }
86   inline       BasicBlock       *front()       { return BasicBlocks.front(); }
87   inline const BasicBlock        *back() const { return BasicBlocks.back(); }
88   inline       BasicBlock        *back()       { return BasicBlocks.back(); }
89
90
91   // Methods for support type inquiry through isa, cast, and dyn_cast:
92   static inline bool classof(const Method *T) { return true; }
93   static inline bool classof(const Value *V) {
94     return V->getValueType() == Value::MethodVal;
95   }
96
97   // dropAllReferences() - This function causes all the subinstructions to "let
98   // go" of all references that they are maintaining.  This allows one to
99   // 'delete' a whole class at a time, even though there may be circular
100   // references... first all references are dropped, and all use counts go to
101   // zero.  Then everything is delete'd for real.  Note that no operations are
102   // valid on an object that has "dropped all references", except operator 
103   // delete.
104   //
105   void dropAllReferences();
106
107   //===--------------------------------------------------------------------===//
108   // Method Instruction iterator code
109   //===--------------------------------------------------------------------===//
110   // 
111   template <class _BB_t, class _BB_i_t, class _BI_t, class _II_t> 
112   class InstIterator;
113   typedef InstIterator<BasicBlocksType, iterator, 
114                        BasicBlock::iterator, Instruction*> inst_iterator;
115   typedef InstIterator<const BasicBlocksType, const_iterator, 
116                        BasicBlock::const_iterator,
117                        const Instruction*> inst_const_iterator;
118
119   // This inner class is used to implement inst_begin() & inst_end() for
120   // inst_iterator and inst_const_iterator's.
121   //
122   template <class _BB_t, class _BB_i_t, class _BI_t, class _II_t>
123   class InstIterator {
124     typedef _BB_t   BBty;
125     typedef _BB_i_t BBIty;
126     typedef _BI_t   BIty;
127     typedef _II_t   IIty;
128     _BB_t  &BBs;      // BasicBlocksType
129     _BB_i_t BB;       // BasicBlocksType::iterator
130     _BI_t   BI;       // BasicBlock::iterator
131   public:
132     typedef bidirectional_iterator_tag iterator_category;
133
134     template<class M> InstIterator(M &m) 
135       : BBs(m.getBasicBlocks()), BB(BBs.begin()) {    // begin ctor
136       if (BB != BBs.end()) {
137         BI = (*BB)->begin();
138         resyncInstructionIterator();
139       }
140     }
141
142     template<class M> InstIterator(M &m, bool) 
143       : BBs(m.getBasicBlocks()), BB(BBs.end()) {    // end ctor
144     }
145
146     // Accessors to get at the underlying iterators...
147     inline BBIty &getBasicBlockIterator()  { return BB; }
148     inline BIty  &getInstructionIterator() { return BI; }
149
150     inline IIty operator*()  const { return *BI; }
151     inline IIty operator->() const { return operator*(); }
152
153     inline bool operator==(const InstIterator &y) const { 
154       return BB == y.BB && (BB == BBs.end() || BI == y.BI);
155     }
156     inline bool operator!=(const InstIterator& y) const { 
157       return !operator==(y);
158     }
159
160     // resyncInstructionIterator - This should be called if the 
161     // InstructionIterator is modified outside of our control.  This resynchs
162     // the internals of the InstIterator to a consistent state.
163     //
164     inline void resyncInstructionIterator() {
165       // The only way that the II could be broken is if it is now pointing to
166       // the end() of the current BasicBlock and there are successor BBs.
167       while (BI == (*BB)->end()) {
168         ++BB;
169         if (BB == BBs.end()) break;
170         BI = (*BB)->begin();
171       }
172     }
173
174     InstIterator& operator++() { 
175       ++BI;
176       resyncInstructionIterator();   // Make sure it is still valid.
177       return *this; 
178     }
179     inline InstIterator operator++(int) { 
180       InstIterator tmp = *this; ++*this; return tmp; 
181     }
182     
183     InstIterator& operator--() { 
184       while (BB == BBs.end() || BI == (*BB)->begin()) {
185         --BB;
186         BI = (*BB)->end();
187       }
188       --BI;
189       return *this; 
190     }
191     inline InstIterator  operator--(int) { 
192       InstIterator tmp = *this; --*this; return tmp; 
193     }
194
195     inline bool atEnd() const { return BB == BBs.end(); }
196   };
197
198   inline inst_iterator inst_begin() { return inst_iterator(*this); }
199   inline inst_iterator inst_end()   { return inst_iterator(*this, true); }
200   inline inst_const_iterator inst_begin() const { return inst_const_iterator(*this); }
201   inline inst_const_iterator inst_end()   const { return inst_const_iterator(*this, true); }
202 };
203
204 // Provide specializations of GraphTraits to be able to treat a method as a 
205 // graph of basic blocks... these are the same as the basic block iterators,
206 // except that the root node is implicitly the first node of the method.
207 //
208 template <> struct GraphTraits<Method*> : public GraphTraits<BasicBlock*> {
209   static NodeType *getEntryNode(Method *M) { return M->front(); }
210 };
211 template <> struct GraphTraits<const Method*> :
212   public GraphTraits<const BasicBlock*> {
213   static NodeType *getEntryNode(const Method *M) { return M->front(); }
214 };
215
216 // Provide specializations of GraphTraits to be able to treat a method as a 
217 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
218 // a method is considered to be when traversing the predecessor edges of a BB
219 // instead of the successor edges.
220 //
221 template <> struct GraphTraits<Inverse<Method*> > :
222   public GraphTraits<Inverse<BasicBlock*> > {
223   static NodeType *getEntryNode(Inverse<Method *> G) { return G.Graph->front();}
224 };
225 template <> struct GraphTraits<Inverse<const Method*> > :
226   public GraphTraits<Inverse<const BasicBlock*> > {
227   static NodeType *getEntryNode(Inverse<const Method *> G) {
228     return G.Graph->front();
229   }
230 };
231
232 #endif