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