Get rid of the Pass+Context magic.
[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 is distributed under the University of Illinois Open Source
6 // 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/Support/Annotation.h"
25 #include "llvm/Attributes.h"
26
27 namespace llvm {
28
29 class FunctionType;
30 class LLVMContext;
31
32 // Traits for intrusive list of basic blocks...
33 template<> struct ilist_traits<BasicBlock>
34   : public SymbolTableListTraits<BasicBlock, Function> {
35
36   // createSentinel is used to get hold of the node that marks the end of the
37   // list... (same trick used here as in ilist_traits<Instruction>)
38   BasicBlock *createSentinel() const {
39     return static_cast<BasicBlock*>(&Sentinel);
40   }
41   static void destroySentinel(BasicBlock*) {}
42
43   BasicBlock *provideInitialHead() const { return createSentinel(); }
44   BasicBlock *ensureHead(BasicBlock*) const { return createSentinel(); }
45   static void noteHead(BasicBlock*, BasicBlock*) {}
46
47   static ValueSymbolTable *getSymTab(Function *ItemParent);
48 private:
49   mutable ilist_node<BasicBlock> Sentinel;
50 };
51
52 template<> struct ilist_traits<Argument>
53   : public SymbolTableListTraits<Argument, Function> {
54
55   Argument *createSentinel() const {
56     return static_cast<Argument*>(&Sentinel);
57   }
58   static void destroySentinel(Argument*) {}
59
60   Argument *provideInitialHead() const { return createSentinel(); }
61   Argument *ensureHead(Argument*) const { return createSentinel(); }
62   static void noteHead(Argument*, Argument*) {}
63
64   static ValueSymbolTable *getSymTab(Function *ItemParent);
65 private:
66   mutable ilist_node<Argument> Sentinel;
67 };
68
69 class Function : public GlobalValue, public Annotable,
70                  public ilist_node<Function> {
71 public:
72   typedef iplist<Argument> ArgumentListType;
73   typedef iplist<BasicBlock> BasicBlockListType;
74
75   // BasicBlock iterators...
76   typedef BasicBlockListType::iterator iterator;
77   typedef BasicBlockListType::const_iterator const_iterator;
78
79   typedef ArgumentListType::iterator arg_iterator;
80   typedef ArgumentListType::const_iterator const_arg_iterator;
81
82 private:
83   // Important things that make up a function!
84   BasicBlockListType  BasicBlocks;        ///< The basic blocks
85   mutable ArgumentListType ArgumentList;  ///< The formal arguments
86   ValueSymbolTable *SymTab;               ///< Symbol table of args/instructions
87   AttrListPtr AttributeList;              ///< Parameter attributes
88
89   // The Calling Convention is stored in Value::SubclassData.
90   /*unsigned CallingConvention;*/
91
92   friend class SymbolTableListTraits<Function, Module>;
93
94   void setParent(Module *parent);
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   
109   Function(const Function&); // DO NOT IMPLEMENT
110   void operator=(const Function&); // DO NOT IMPLEMENT
111
112   /// Function ctor - If the (optional) Module argument is specified, the
113   /// function is automatically inserted into the end of the function list for
114   /// the module.
115   ///
116   Function(const FunctionType *Ty, LinkageTypes Linkage,
117            const std::string &N = "", Module *M = 0);
118
119 public:
120   static Function *Create(const FunctionType *Ty, LinkageTypes Linkage,
121                           const std::string &N = "", Module *M = 0) {
122     return new(0) Function(Ty, Linkage, N, M);
123   }
124
125   ~Function();
126
127   const Type *getReturnType() const;           // Return the type of the ret val
128   const FunctionType *getFunctionType() const; // Return the FunctionType for me
129
130   /// getContext - Return a pointer to the LLVMContext associated with this 
131   /// function, or NULL if this function is not bound to a context yet.
132   LLVMContext &getContext() const;
133
134   /// isVarArg - Return true if this function takes a variable number of
135   /// arguments.
136   bool isVarArg() const;
137
138   /// isDeclaration - Is the body of this function unknown? (The basic block 
139   /// list is empty if so.) This is true for function declarations, but not 
140   /// true for function definitions.
141   ///
142   virtual bool isDeclaration() const { return BasicBlocks.empty(); }
143
144   /// getIntrinsicID - This method returns the ID number of the specified
145   /// function, or Intrinsic::not_intrinsic if the function is not an
146   /// instrinsic, or if the pointer is null.  This value is always defined to be
147   /// zero to allow easy checking for whether a function is intrinsic or not.
148   /// The particular intrinsic functions which correspond to this value are
149   /// defined in llvm/Intrinsics.h.
150   ///
151   unsigned getIntrinsicID() const;
152   bool isIntrinsic() const { return getIntrinsicID() != 0; }
153
154   /// getCallingConv()/setCallingConv(uint) - These method get and set the
155   /// calling convention of this function.  The enum values for the known
156   /// calling conventions are defined in CallingConv.h.
157   unsigned getCallingConv() const { return SubclassData >> 1; }
158   void setCallingConv(unsigned CC) {
159     SubclassData = (SubclassData & 1) | (CC << 1);
160   }
161   
162   /// getAttributes - Return the attribute list for this Function.
163   ///
164   const AttrListPtr &getAttributes() const { return AttributeList; }
165
166   /// setAttributes - Set the attribute list for this Function.
167   ///
168   void setAttributes(const AttrListPtr &attrs) { AttributeList = attrs; }
169
170   /// hasFnAttr - Return true if this function has the given attribute.
171   bool hasFnAttr(Attributes N) const {
172     // Function Attributes are stored at ~0 index 
173     return AttributeList.paramHasAttr(~0U, N);
174   }
175
176   /// addFnAttr - Add function attributes to this function.
177   ///
178   void addFnAttr(Attributes N) { 
179     // Function Attributes are stored at ~0 index 
180     addAttribute(~0U, N);
181   }
182
183   /// removeFnAttr - Remove function attributes from this function.
184   ///
185   void removeFnAttr(Attributes N) {
186     // Function Attributes are stored at ~0 index 
187     removeAttribute(~0U, N);
188   }
189
190   /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm
191   ///                             to use during code generation.
192   bool hasGC() const;
193   const char *getGC() const;
194   void setGC(const char *Str);
195   void clearGC();
196
197   /// @brief Determine whether the function has the given attribute.
198   bool paramHasAttr(unsigned i, Attributes attr) const {
199     return AttributeList.paramHasAttr(i, attr);
200   }
201
202   /// addAttribute - adds the attribute to the list of attributes.
203   void addAttribute(unsigned i, Attributes attr);
204   
205   /// removeAttribute - removes the attribute from the list of attributes.
206   void removeAttribute(unsigned i, Attributes attr);
207
208   /// @brief Extract the alignment for a call or parameter (0=unknown).
209   unsigned getParamAlignment(unsigned i) const {
210     return AttributeList.getParamAlignment(i);
211   }
212
213   /// @brief Determine if the function does not access memory.
214   bool doesNotAccessMemory() const {
215     return hasFnAttr(Attribute::ReadNone);
216   }
217   void setDoesNotAccessMemory(bool DoesNotAccessMemory = true) {
218     if (DoesNotAccessMemory) addFnAttr(Attribute::ReadNone);
219     else removeFnAttr(Attribute::ReadNone);
220   }
221
222   /// @brief Determine if the function does not access or only reads memory.
223   bool onlyReadsMemory() const {
224     return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
225   }
226   void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
227     if (OnlyReadsMemory) addFnAttr(Attribute::ReadOnly);
228     else removeFnAttr(Attribute::ReadOnly | Attribute::ReadNone);
229   }
230
231   /// @brief Determine if the function cannot return.
232   bool doesNotReturn() const {
233     return hasFnAttr(Attribute::NoReturn);
234   }
235   void setDoesNotReturn(bool DoesNotReturn = true) {
236     if (DoesNotReturn) addFnAttr(Attribute::NoReturn);
237     else removeFnAttr(Attribute::NoReturn);
238   }
239
240   /// @brief Determine if the function cannot unwind.
241   bool doesNotThrow() const {
242     return hasFnAttr(Attribute::NoUnwind);
243   }
244   void setDoesNotThrow(bool DoesNotThrow = true) {
245     if (DoesNotThrow) addFnAttr(Attribute::NoUnwind);
246     else removeFnAttr(Attribute::NoUnwind);
247   }
248
249   /// @brief Determine if the function returns a structure through first 
250   /// pointer argument.
251   bool hasStructRetAttr() const {
252     return paramHasAttr(1, Attribute::StructRet);
253   }
254
255   /// @brief Determine if the parameter does not alias other parameters.
256   /// @param n The parameter to check. 1 is the first parameter, 0 is the return
257   bool doesNotAlias(unsigned n) const {
258     return paramHasAttr(n, Attribute::NoAlias);
259   }
260   void setDoesNotAlias(unsigned n, bool DoesNotAlias = true) {
261     if (DoesNotAlias) addAttribute(n, Attribute::NoAlias);
262     else removeAttribute(n, Attribute::NoAlias);
263   }
264
265   /// @brief Determine if the parameter can be captured.
266   /// @param n The parameter to check. 1 is the first parameter, 0 is the return
267   bool doesNotCapture(unsigned n) const {
268     return paramHasAttr(n, Attribute::NoCapture);
269   }
270   void setDoesNotCapture(unsigned n, bool DoesNotCapture = true) {
271     if (DoesNotCapture) addAttribute(n, Attribute::NoCapture);
272     else removeAttribute(n, Attribute::NoCapture);
273   }
274
275   /// copyAttributesFrom - copy all additional attributes (those not needed to
276   /// create a Function) from the Function Src to this one.
277   void copyAttributesFrom(const GlobalValue *Src);
278
279   /// deleteBody - This method deletes the body of the function, and converts
280   /// the linkage to external.
281   ///
282   void deleteBody() {
283     dropAllReferences();
284     setLinkage(ExternalLinkage);
285   }
286
287   /// removeFromParent - This method unlinks 'this' from the containing module,
288   /// but does not delete it.
289   ///
290   virtual void removeFromParent();
291
292   /// eraseFromParent - This method unlinks 'this' from the containing module
293   /// and deletes it.
294   ///
295   virtual void eraseFromParent();
296
297
298   /// Get the underlying elements of the Function... the basic block list is
299   /// empty for external functions.
300   ///
301   const ArgumentListType &getArgumentList() const {
302     CheckLazyArguments();
303     return ArgumentList;
304   }
305   ArgumentListType &getArgumentList() {
306     CheckLazyArguments();
307     return ArgumentList;
308   }
309   static iplist<Argument> Function::*getSublistAccess(Argument*) {
310     return &Function::ArgumentList;
311   }
312
313   const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
314         BasicBlockListType &getBasicBlockList()       { return BasicBlocks; }
315   static iplist<BasicBlock> Function::*getSublistAccess(BasicBlock*) {
316     return &Function::BasicBlocks;
317   }
318
319   const BasicBlock       &getEntryBlock() const   { return front(); }
320         BasicBlock       &getEntryBlock()         { return front(); }
321
322   //===--------------------------------------------------------------------===//
323   // Symbol Table Accessing functions...
324
325   /// getSymbolTable() - Return the symbol table...
326   ///
327   inline       ValueSymbolTable &getValueSymbolTable()       { return *SymTab; }
328   inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
329
330
331   //===--------------------------------------------------------------------===//
332   // BasicBlock iterator forwarding functions
333   //
334   iterator                begin()       { return BasicBlocks.begin(); }
335   const_iterator          begin() const { return BasicBlocks.begin(); }
336   iterator                end  ()       { return BasicBlocks.end();   }
337   const_iterator          end  () const { return BasicBlocks.end();   }
338
339   size_t                   size() const { return BasicBlocks.size();  }
340   bool                    empty() const { return BasicBlocks.empty(); }
341   const BasicBlock       &front() const { return BasicBlocks.front(); }
342         BasicBlock       &front()       { return BasicBlocks.front(); }
343   const BasicBlock        &back() const { return BasicBlocks.back();  }
344         BasicBlock        &back()       { return BasicBlocks.back();  }
345
346   //===--------------------------------------------------------------------===//
347   // Argument iterator forwarding functions
348   //
349   arg_iterator arg_begin() {
350     CheckLazyArguments();
351     return ArgumentList.begin();
352   }
353   const_arg_iterator arg_begin() const {
354     CheckLazyArguments();
355     return ArgumentList.begin();
356   }
357   arg_iterator arg_end() {
358     CheckLazyArguments();
359     return ArgumentList.end();
360   }
361   const_arg_iterator arg_end() const {
362     CheckLazyArguments();
363     return ArgumentList.end();
364   }
365
366   size_t arg_size() const;
367   bool arg_empty() const;
368
369   /// viewCFG - This function is meant for use from the debugger.  You can just
370   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
371   /// program, displaying the CFG of the current function with the code for each
372   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
373   /// in your path.
374   ///
375   void viewCFG() const;
376
377   /// viewCFGOnly - This function is meant for use from the debugger.  It works
378   /// just like viewCFG, but it does not include the contents of basic blocks
379   /// into the nodes, just the label.  If you are only interested in the CFG
380   /// this can make the graph smaller.
381   ///
382   void viewCFGOnly() const;
383
384   /// Methods for support type inquiry through isa, cast, and dyn_cast:
385   static inline bool classof(const Function *) { return true; }
386   static inline bool classof(const Value *V) {
387     return V->getValueID() == Value::FunctionVal;
388   }
389
390   /// dropAllReferences() - This method causes all the subinstructions to "let
391   /// go" of all references that they are maintaining.  This allows one to
392   /// 'delete' a whole module at a time, even though there may be circular
393   /// references... first all references are dropped, and all use counts go to
394   /// zero.  Then everything is deleted for real.  Note that no operations are
395   /// valid on an object that has "dropped all references", except operator
396   /// delete.
397   ///
398   /// Since no other object in the module can have references into the body of a
399   /// function, dropping all references deletes the entire body of the function,
400   /// including any contained basic blocks.
401   ///
402   void dropAllReferences();
403
404   /// hasAddressTaken - returns true if there are any uses of this function
405   /// other than direct calls or invokes to it.
406   bool hasAddressTaken() const;
407 };
408
409 inline ValueSymbolTable *
410 ilist_traits<BasicBlock>::getSymTab(Function *F) {
411   return F ? &F->getValueSymbolTable() : 0;
412 }
413
414 inline ValueSymbolTable *
415 ilist_traits<Argument>::getSymTab(Function *F) {
416   return F ? &F->getValueSymbolTable() : 0;
417 }
418
419 } // End llvm namespace
420
421 #endif