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