bitcode writer support for blockaddress.
[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/CallingConv.h"
23 #include "llvm/BasicBlock.h"
24 #include "llvm/Argument.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_half_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_half_node<Argument> Sentinel;
67 };
68
69 class Function : public GlobalValue,
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   /*CallingConv::ID 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 Twine &N = "", Module *M = 0);
118
119 public:
120   static Function *Create(const FunctionType *Ty, LinkageTypes Linkage,
121                           const Twine &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(CC) - 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   CallingConv::ID getCallingConv() const {
158     return static_cast<CallingConv::ID>(SubclassData >> 1);
159   }
160   void setCallingConv(CallingConv::ID CC) {
161     SubclassData = (SubclassData & 1) | (static_cast<unsigned>(CC) << 1);
162   }
163   
164   /// getAttributes - Return the attribute list for this Function.
165   ///
166   const AttrListPtr &getAttributes() const { return AttributeList; }
167
168   /// setAttributes - Set the attribute list for this Function.
169   ///
170   void setAttributes(const AttrListPtr &attrs) { AttributeList = attrs; }
171
172   /// hasFnAttr - Return true if this function has the given attribute.
173   bool hasFnAttr(Attributes N) const {
174     // Function Attributes are stored at ~0 index 
175     return AttributeList.paramHasAttr(~0U, N);
176   }
177
178   /// addFnAttr - Add function attributes to this function.
179   ///
180   void addFnAttr(Attributes N) { 
181     // Function Attributes are stored at ~0 index 
182     addAttribute(~0U, N);
183   }
184
185   /// removeFnAttr - Remove function attributes from this function.
186   ///
187   void removeFnAttr(Attributes N) {
188     // Function Attributes are stored at ~0 index 
189     removeAttribute(~0U, N);
190   }
191
192   /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm
193   ///                             to use during code generation.
194   bool hasGC() const;
195   const char *getGC() const;
196   void setGC(const char *Str);
197   void clearGC();
198
199   /// @brief Determine whether the function has the given attribute.
200   bool paramHasAttr(unsigned i, Attributes attr) const {
201     return AttributeList.paramHasAttr(i, attr);
202   }
203
204   /// addAttribute - adds the attribute to the list of attributes.
205   void addAttribute(unsigned i, Attributes attr);
206   
207   /// removeAttribute - removes the attribute from the list of attributes.
208   void removeAttribute(unsigned i, Attributes attr);
209
210   /// @brief Extract the alignment for a call or parameter (0=unknown).
211   unsigned getParamAlignment(unsigned i) const {
212     return AttributeList.getParamAlignment(i);
213   }
214
215   /// @brief Determine if the function does not access memory.
216   bool doesNotAccessMemory() const {
217     return hasFnAttr(Attribute::ReadNone);
218   }
219   void setDoesNotAccessMemory(bool DoesNotAccessMemory = true) {
220     if (DoesNotAccessMemory) addFnAttr(Attribute::ReadNone);
221     else removeFnAttr(Attribute::ReadNone);
222   }
223
224   /// @brief Determine if the function does not access or only reads memory.
225   bool onlyReadsMemory() const {
226     return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
227   }
228   void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
229     if (OnlyReadsMemory) addFnAttr(Attribute::ReadOnly);
230     else removeFnAttr(Attribute::ReadOnly | Attribute::ReadNone);
231   }
232
233   /// @brief Determine if the function cannot return.
234   bool doesNotReturn() const {
235     return hasFnAttr(Attribute::NoReturn);
236   }
237   void setDoesNotReturn(bool DoesNotReturn = true) {
238     if (DoesNotReturn) addFnAttr(Attribute::NoReturn);
239     else removeFnAttr(Attribute::NoReturn);
240   }
241
242   /// @brief Determine if the function cannot unwind.
243   bool doesNotThrow() const {
244     return hasFnAttr(Attribute::NoUnwind);
245   }
246   void setDoesNotThrow(bool DoesNotThrow = true) {
247     if (DoesNotThrow) addFnAttr(Attribute::NoUnwind);
248     else removeFnAttr(Attribute::NoUnwind);
249   }
250
251   /// @brief Determine if the function returns a structure through first 
252   /// pointer argument.
253   bool hasStructRetAttr() const {
254     return paramHasAttr(1, Attribute::StructRet);
255   }
256
257   /// @brief Determine if the parameter does not alias other parameters.
258   /// @param n The parameter to check. 1 is the first parameter, 0 is the return
259   bool doesNotAlias(unsigned n) const {
260     return paramHasAttr(n, Attribute::NoAlias);
261   }
262   void setDoesNotAlias(unsigned n, bool DoesNotAlias = true) {
263     if (DoesNotAlias) addAttribute(n, Attribute::NoAlias);
264     else removeAttribute(n, Attribute::NoAlias);
265   }
266
267   /// @brief Determine if the parameter can be captured.
268   /// @param n The parameter to check. 1 is the first parameter, 0 is the return
269   bool doesNotCapture(unsigned n) const {
270     return paramHasAttr(n, Attribute::NoCapture);
271   }
272   void setDoesNotCapture(unsigned n, bool DoesNotCapture = true) {
273     if (DoesNotCapture) addAttribute(n, Attribute::NoCapture);
274     else removeAttribute(n, Attribute::NoCapture);
275   }
276
277   /// copyAttributesFrom - copy all additional attributes (those not needed to
278   /// create a Function) from the Function Src to this one.
279   void copyAttributesFrom(const GlobalValue *Src);
280
281   /// deleteBody - This method deletes the body of the function, and converts
282   /// the linkage to external.
283   ///
284   void deleteBody() {
285     dropAllReferences();
286     setLinkage(ExternalLinkage);
287   }
288
289   /// removeFromParent - This method unlinks 'this' from the containing module,
290   /// but does not delete it.
291   ///
292   virtual void removeFromParent();
293
294   /// eraseFromParent - This method unlinks 'this' from the containing module
295   /// and deletes it.
296   ///
297   virtual void eraseFromParent();
298
299
300   /// Get the underlying elements of the Function... the basic block list is
301   /// empty for external functions.
302   ///
303   const ArgumentListType &getArgumentList() const {
304     CheckLazyArguments();
305     return ArgumentList;
306   }
307   ArgumentListType &getArgumentList() {
308     CheckLazyArguments();
309     return ArgumentList;
310   }
311   static iplist<Argument> Function::*getSublistAccess(Argument*) {
312     return &Function::ArgumentList;
313   }
314
315   const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
316         BasicBlockListType &getBasicBlockList()       { return BasicBlocks; }
317   static iplist<BasicBlock> Function::*getSublistAccess(BasicBlock*) {
318     return &Function::BasicBlocks;
319   }
320
321   const BasicBlock       &getEntryBlock() const   { return front(); }
322         BasicBlock       &getEntryBlock()         { return front(); }
323
324   //===--------------------------------------------------------------------===//
325   // Symbol Table Accessing functions...
326
327   /// getSymbolTable() - Return the symbol table...
328   ///
329   inline       ValueSymbolTable &getValueSymbolTable()       { return *SymTab; }
330   inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
331
332
333   //===--------------------------------------------------------------------===//
334   // BasicBlock iterator forwarding functions
335   //
336   iterator                begin()       { return BasicBlocks.begin(); }
337   const_iterator          begin() const { return BasicBlocks.begin(); }
338   iterator                end  ()       { return BasicBlocks.end();   }
339   const_iterator          end  () const { return BasicBlocks.end();   }
340
341   size_t                   size() const { return BasicBlocks.size();  }
342   bool                    empty() const { return BasicBlocks.empty(); }
343   const BasicBlock       &front() const { return BasicBlocks.front(); }
344         BasicBlock       &front()       { return BasicBlocks.front(); }
345   const BasicBlock        &back() const { return BasicBlocks.back();  }
346         BasicBlock        &back()       { return BasicBlocks.back();  }
347
348   //===--------------------------------------------------------------------===//
349   // Argument iterator forwarding functions
350   //
351   arg_iterator arg_begin() {
352     CheckLazyArguments();
353     return ArgumentList.begin();
354   }
355   const_arg_iterator arg_begin() const {
356     CheckLazyArguments();
357     return ArgumentList.begin();
358   }
359   arg_iterator arg_end() {
360     CheckLazyArguments();
361     return ArgumentList.end();
362   }
363   const_arg_iterator arg_end() const {
364     CheckLazyArguments();
365     return ArgumentList.end();
366   }
367
368   size_t arg_size() const;
369   bool arg_empty() const;
370
371   /// viewCFG - This function is meant for use from the debugger.  You can just
372   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
373   /// program, displaying the CFG of the current function with the code for each
374   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
375   /// in your path.
376   ///
377   void viewCFG() const;
378
379   /// viewCFGOnly - This function is meant for use from the debugger.  It works
380   /// just like viewCFG, but it does not include the contents of basic blocks
381   /// into the nodes, just the label.  If you are only interested in the CFG
382   /// this can make the graph smaller.
383   ///
384   void viewCFGOnly() const;
385
386   /// Methods for support type inquiry through isa, cast, and dyn_cast:
387   static inline bool classof(const Function *) { return true; }
388   static inline bool classof(const Value *V) {
389     return V->getValueID() == Value::FunctionVal;
390   }
391
392   /// dropAllReferences() - This method causes all the subinstructions to "let
393   /// go" of all references that they are maintaining.  This allows one to
394   /// 'delete' a whole module at a time, even though there may be circular
395   /// references... first all references are dropped, and all use counts go to
396   /// zero.  Then everything is deleted for real.  Note that no operations are
397   /// valid on an object that has "dropped all references", except operator
398   /// delete.
399   ///
400   /// Since no other object in the module can have references into the body of a
401   /// function, dropping all references deletes the entire body of the function,
402   /// including any contained basic blocks.
403   ///
404   void dropAllReferences();
405
406   /// hasAddressTaken - returns true if there are any uses of this function
407   /// other than direct calls or invokes to it.
408   bool hasAddressTaken() const;
409 };
410
411 inline ValueSymbolTable *
412 ilist_traits<BasicBlock>::getSymTab(Function *F) {
413   return F ? &F->getValueSymbolTable() : 0;
414 }
415
416 inline ValueSymbolTable *
417 ilist_traits<Argument>::getSymTab(Function *F) {
418   return F ? &F->getValueSymbolTable() : 0;
419 }
420
421 } // End llvm namespace
422
423 #endif