Devirtualizing Value destructor (PR889). Patch by Pawel Kunio!
[oota-llvm.git] / include / llvm / Function.h
1 //===-- llvm/Function.h - Class to represent a single function --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declaration of the Function class, which represents a
11 // single function/procedure in LLVM.
12 //
13 // A function basically consists of a list of basic blocks, a list of arguments,
14 // and a symbol table.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_FUNCTION_H
19 #define LLVM_FUNCTION_H
20
21 #include "llvm/GlobalValue.h"
22 #include "llvm/BasicBlock.h"
23 #include "llvm/Argument.h"
24 #include "llvm/ParameterAttributes.h"
25 #include "llvm/Support/Annotation.h"
26
27 namespace llvm {
28
29 class FunctionType;
30 class ParamAttrsList;
31
32 // Traits for intrusive list of instructions...
33 template<> struct ilist_traits<BasicBlock>
34   : public SymbolTableListTraits<BasicBlock, Function> {
35
36   // createSentinel is used to create a node that marks the end of the list...
37   static BasicBlock *createSentinel();
38   static void destroySentinel(BasicBlock *BB) { delete BB; }
39   static iplist<BasicBlock> &getList(Function *F);
40   static ValueSymbolTable *getSymTab(Function *ItemParent);
41   static int getListOffset();
42 };
43
44 template<> struct ilist_traits<Argument>
45   : public SymbolTableListTraits<Argument, Function> {
46
47   // createSentinel is used to create a node that marks the end of the list...
48   static Argument *createSentinel();
49   static void destroySentinel(Argument *A) { delete A; }
50   static iplist<Argument> &getList(Function *F);
51   static ValueSymbolTable *getSymTab(Function *ItemParent);
52   static int getListOffset();
53 };
54
55 class Function : public GlobalValue, public Annotable {
56 protected:
57   static void destroyThis(Function*v);
58   friend class Value;
59 public:
60   typedef iplist<Argument> ArgumentListType;
61   typedef iplist<BasicBlock> BasicBlockListType;
62
63   // BasicBlock iterators...
64   typedef BasicBlockListType::iterator iterator;
65   typedef BasicBlockListType::const_iterator const_iterator;
66
67   typedef ArgumentListType::iterator arg_iterator;
68   typedef ArgumentListType::const_iterator const_arg_iterator;
69
70 private:
71   // Important things that make up a function!
72   BasicBlockListType  BasicBlocks;        ///< The basic blocks
73   mutable ArgumentListType ArgumentList;  ///< The formal arguments
74   ValueSymbolTable *SymTab;               ///< Symbol table of args/instructions
75   const ParamAttrsList *ParamAttrs;       ///< Parameter attributes
76   
77   // The Calling Convention is stored in Value::SubclassData.
78   /*unsigned CallingConvention;*/
79
80   friend class SymbolTableListTraits<Function, Module>;
81
82   void setParent(Module *parent);
83   Function *Prev, *Next;
84   void setNext(Function *N) { Next = N; }
85   void setPrev(Function *N) { Prev = N; }
86
87   // getNext/Prev - Return the next or previous function in the list.  These
88   // methods should never be used directly, and are only used to implement the
89   // function list as part of the module.
90   //
91   Function *getNext()             { return Next; }
92   const Function *getNext() const { return Next; }
93   Function *getPrev()             { return Prev; }
94   const Function *getPrev() const { return Prev; }
95
96   /// hasLazyArguments/CheckLazyArguments - The argument list of a function is
97   /// built on demand, so that the list isn't allocated until the first client
98   /// needs it.  The hasLazyArguments predicate returns true if the arg list
99   /// hasn't been set up yet.
100   bool hasLazyArguments() const {
101     return SubclassData & 1;
102   }
103   void CheckLazyArguments() const {
104     if (hasLazyArguments())
105       BuildLazyArguments();
106   }
107   void BuildLazyArguments() const;
108 public:
109   /// Function ctor - If the (optional) Module argument is specified, the
110   /// function is automatically inserted into the end of the function list for
111   /// the module.
112   ///
113   Function(const FunctionType *Ty, LinkageTypes Linkage,
114            const std::string &N = "", Module *M = 0);
115
116   const Type *getReturnType() const;           // Return the type of the ret val
117   const FunctionType *getFunctionType() const; // Return the FunctionType for me
118
119   /// isVarArg - Return true if this function takes a variable number of
120   /// arguments.
121   bool isVarArg() const;
122
123   /// isDeclaration - Is the body of this function unknown? (The basic block 
124   /// list is empty if so.) This is true for function declarations, but not 
125   /// true for function definitions.
126   ///
127   virtual bool isDeclaration() const { return BasicBlocks.empty(); }
128
129   /// getIntrinsicID - This method returns the ID number of the specified
130   /// function, or Intrinsic::not_intrinsic if the function is not an
131   /// instrinsic, or if the pointer is null.  This value is always defined to be
132   /// zero to allow easy checking for whether a function is intrinsic or not.
133   /// The particular intrinsic functions which correspond to this value are
134   /// defined in llvm/Intrinsics.h.
135   ///
136   unsigned getIntrinsicID(bool noAssert = false) const;
137   bool isIntrinsic() const { return getIntrinsicID() != 0; }
138
139   /// getCallingConv()/setCallingConv(uint) - These method get and set the
140   /// calling convention of this function.  The enum values for the known
141   /// calling conventions are defined in CallingConv.h.
142   unsigned getCallingConv() const { return SubclassData >> 1; }
143   void setCallingConv(unsigned CC) {
144     SubclassData = (SubclassData & 1) | (CC << 1);
145   }
146   
147   /// Obtains a constant pointer to the ParamAttrsList object which holds the
148   /// parameter attributes information, if any. 
149   /// @returns 0 if no parameter attributes have been set.
150   /// @brief Get the parameter attributes.
151   const ParamAttrsList *getParamAttrs() const { return ParamAttrs; }
152
153   /// Sets the parameter attributes for this Function. To construct a 
154   /// ParamAttrsList, see ParameterAttributes.h
155   /// @brief Set the parameter attributes.
156   void setParamAttrs(const ParamAttrsList *attrs);
157
158   /// @brief Determine whether the function has the given attribute.
159   bool paramHasAttr(uint16_t i, ParameterAttributes attr) const {
160     return ParamAttrs && ParamAttrs->paramHasAttr(i, attr);
161   }
162
163   /// @brief Determine if the function does not access memory.
164   bool doesNotAccessMemory() const {
165     return paramHasAttr(0, ParamAttr::ReadNone);
166   }
167
168   /// @brief Determine if the function does not access or only reads memory.
169   bool onlyReadsMemory() const {
170     return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
171   }
172
173   /// @brief Determine if the function returns a structure.
174   bool isStructReturn() const {
175     return paramHasAttr(1, ParamAttr::StructRet);
176   }
177
178   /// deleteBody - This method deletes the body of the function, and converts
179   /// the linkage to external.
180   ///
181   void deleteBody() {
182     dropAllReferences();
183     setLinkage(ExternalLinkage);
184   }
185
186   /// removeFromParent - This method unlinks 'this' from the containing module,
187   /// but does not delete it.
188   ///
189   void removeFromParent();
190
191   /// eraseFromParent - This method unlinks 'this' from the containing module
192   /// and deletes it.
193   ///
194   void eraseFromParent();
195
196
197   /// Get the underlying elements of the Function... the basic block list is
198   /// empty for external functions.
199   ///
200   const ArgumentListType &getArgumentList() const {
201     CheckLazyArguments();
202     return ArgumentList;
203   }
204   ArgumentListType &getArgumentList() {
205     CheckLazyArguments();
206     return ArgumentList;
207   }
208
209   const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
210         BasicBlockListType &getBasicBlockList()       { return BasicBlocks; }
211
212   const BasicBlock       &getEntryBlock() const   { return front(); }
213         BasicBlock       &getEntryBlock()         { return front(); }
214
215   //===--------------------------------------------------------------------===//
216   // Symbol Table Accessing functions...
217
218   /// getSymbolTable() - Return the symbol table...
219   ///
220   inline       ValueSymbolTable &getValueSymbolTable()       { return *SymTab; }
221   inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
222
223
224   //===--------------------------------------------------------------------===//
225   // BasicBlock iterator forwarding functions
226   //
227   iterator                begin()       { return BasicBlocks.begin(); }
228   const_iterator          begin() const { return BasicBlocks.begin(); }
229   iterator                end  ()       { return BasicBlocks.end();   }
230   const_iterator          end  () const { return BasicBlocks.end();   }
231
232   size_t                   size() const { return BasicBlocks.size();  }
233   bool                    empty() const { return BasicBlocks.empty(); }
234   const BasicBlock       &front() const { return BasicBlocks.front(); }
235         BasicBlock       &front()       { return BasicBlocks.front(); }
236   const BasicBlock        &back() const { return BasicBlocks.back();  }
237         BasicBlock        &back()       { return BasicBlocks.back();  }
238
239   //===--------------------------------------------------------------------===//
240   // Argument iterator forwarding functions
241   //
242   arg_iterator arg_begin() {
243     CheckLazyArguments();
244     return ArgumentList.begin();
245   }
246   const_arg_iterator arg_begin() const {
247     CheckLazyArguments();
248     return ArgumentList.begin();
249   }
250   arg_iterator arg_end() {
251     CheckLazyArguments();
252     return ArgumentList.end();
253   }
254   const_arg_iterator arg_end() const {
255     CheckLazyArguments();
256     return ArgumentList.end();
257   }
258
259   size_t arg_size() const;
260   bool arg_empty() const;
261
262   virtual void print(std::ostream &OS) const { print(OS, 0); }
263   void print(std::ostream *OS) const { if (OS) print(*OS); }
264   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
265
266   /// viewCFG - This function is meant for use from the debugger.  You can just
267   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
268   /// program, displaying the CFG of the current function with the code for each
269   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
270   /// in your path.
271   ///
272   void viewCFG() const;
273
274   /// viewCFGOnly - This function is meant for use from the debugger.  It works
275   /// just like viewCFG, but it does not include the contents of basic blocks
276   /// into the nodes, just the label.  If you are only interested in the CFG
277   /// this can make the graph smaller.
278   ///
279   void viewCFGOnly() const;
280
281   /// Methods for support type inquiry through isa, cast, and dyn_cast:
282   static inline bool classof(const Function *) { return true; }
283   static inline bool classof(const Value *V) {
284     return V->getValueID() == Value::FunctionVal;
285   }
286
287   /// dropAllReferences() - This method causes all the subinstructions to "let
288   /// go" of all references that they are maintaining.  This allows one to
289   /// 'delete' a whole module at a time, even though there may be circular
290   /// references... first all references are dropped, and all use counts go to
291   /// zero.  Then everything is deleted for real.  Note that no operations are
292   /// valid on an object that has "dropped all references", except operator
293   /// delete.
294   ///
295   /// Since no other object in the module can have references into the body of a
296   /// function, dropping all references deletes the entire body of the function,
297   /// including any contained basic blocks.
298   ///
299   void dropAllReferences();
300   
301   static unsigned getBasicBlockListOffset() {
302     Function *Obj = 0;
303     return unsigned(reinterpret_cast<uintptr_t>(&Obj->BasicBlocks));
304   }
305   static unsigned getArgumentListOffset() {
306     Function *Obj = 0;
307     return unsigned(reinterpret_cast<uintptr_t>(&Obj->ArgumentList));
308   }
309 };
310
311 inline ValueSymbolTable *
312 ilist_traits<BasicBlock>::getSymTab(Function *F) {
313   return F ? &F->getValueSymbolTable() : 0;
314 }
315
316 inline ValueSymbolTable *
317 ilist_traits<Argument>::getSymTab(Function *F) {
318   return F ? &F->getValueSymbolTable() : 0;
319 }
320
321 inline int 
322 ilist_traits<BasicBlock>::getListOffset() {
323   return Function::getBasicBlockListOffset();
324 }
325
326 inline int 
327 ilist_traits<Argument>::getListOffset() {
328   return Function::getArgumentListOffset();
329 }
330
331
332 } // End llvm namespace
333
334 #endif