Expand ParameterAttributes to 32 bits (in preparation
[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 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 public:
57   typedef iplist<Argument> ArgumentListType;
58   typedef iplist<BasicBlock> BasicBlockListType;
59
60   // BasicBlock iterators...
61   typedef BasicBlockListType::iterator iterator;
62   typedef BasicBlockListType::const_iterator const_iterator;
63
64   typedef ArgumentListType::iterator arg_iterator;
65   typedef ArgumentListType::const_iterator const_arg_iterator;
66
67 private:
68   // Important things that make up a function!
69   BasicBlockListType  BasicBlocks;        ///< The basic blocks
70   mutable ArgumentListType ArgumentList;  ///< The formal arguments
71   ValueSymbolTable *SymTab;               ///< Symbol table of args/instructions
72   const ParamAttrsList *ParamAttrs;       ///< Parameter attributes
73   
74   // The Calling Convention is stored in Value::SubclassData.
75   /*unsigned CallingConvention;*/
76
77   friend class SymbolTableListTraits<Function, Module>;
78
79   void setParent(Module *parent);
80   Function *Prev, *Next;
81   void setNext(Function *N) { Next = N; }
82   void setPrev(Function *N) { Prev = N; }
83
84   // getNext/Prev - Return the next or previous function in the list.  These
85   // methods should never be used directly, and are only used to implement the
86   // function list as part of the module.
87   //
88   Function *getNext()             { return Next; }
89   const Function *getNext() const { return Next; }
90   Function *getPrev()             { return Prev; }
91   const Function *getPrev() const { return Prev; }
92
93   /// hasLazyArguments/CheckLazyArguments - The argument list of a function is
94   /// built on demand, so that the list isn't allocated until the first client
95   /// needs it.  The hasLazyArguments predicate returns true if the arg list
96   /// hasn't been set up yet.
97   bool hasLazyArguments() const {
98     return SubclassData & 1;
99   }
100   void CheckLazyArguments() const {
101     if (hasLazyArguments())
102       BuildLazyArguments();
103   }
104   void BuildLazyArguments() const;
105   
106   Function(const Function&); // DO NOT IMPLEMENT
107   void operator=(const Function&); // DO NOT IMPLEMENT
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   ~Function();
116
117   const Type *getReturnType() const;           // Return the type of the ret val
118   const FunctionType *getFunctionType() const; // Return the FunctionType for me
119
120   /// isVarArg - Return true if this function takes a variable number of
121   /// arguments.
122   bool isVarArg() const;
123
124   /// isDeclaration - Is the body of this function unknown? (The basic block 
125   /// list is empty if so.) This is true for function declarations, but not 
126   /// true for function definitions.
127   ///
128   virtual bool isDeclaration() const { return BasicBlocks.empty(); }
129
130   /// getIntrinsicID - This method returns the ID number of the specified
131   /// function, or Intrinsic::not_intrinsic if the function is not an
132   /// instrinsic, or if the pointer is null.  This value is always defined to be
133   /// zero to allow easy checking for whether a function is intrinsic or not.
134   /// The particular intrinsic functions which correspond to this value are
135   /// defined in llvm/Intrinsics.h.
136   ///
137   unsigned getIntrinsicID(bool noAssert = false) const;
138   bool isIntrinsic() const { return getIntrinsicID() != 0; }
139
140   /// getCallingConv()/setCallingConv(uint) - These method get and set the
141   /// calling convention of this function.  The enum values for the known
142   /// calling conventions are defined in CallingConv.h.
143   unsigned getCallingConv() const { return SubclassData >> 1; }
144   void setCallingConv(unsigned CC) {
145     SubclassData = (SubclassData & 1) | (CC << 1);
146   }
147   
148   /// Obtains a constant pointer to the ParamAttrsList object which holds the
149   /// parameter attributes information, if any. 
150   /// @returns 0 if no parameter attributes have been set.
151   /// @brief Get the parameter attributes.
152   const ParamAttrsList *getParamAttrs() const { return ParamAttrs; }
153
154   /// Sets the parameter attributes for this Function. To construct a 
155   /// ParamAttrsList, see ParameterAttributes.h
156   /// @brief Set the parameter attributes.
157   void setParamAttrs(const ParamAttrsList *attrs);
158
159   /// hasCollector/getCollector/setCollector/clearCollector - The name of the
160   /// garbage collection algorithm to use during code generation.
161   bool hasCollector() const;
162   const char *getCollector() const;
163   void setCollector(const char *Str);
164   void clearCollector();
165
166   /// @brief Determine whether the function has the given attribute.
167   bool paramHasAttr(uint16_t i, ParameterAttributes attr) const;
168   
169   /// @brief Determine if the function cannot return.
170   bool doesNotReturn() const;
171
172   /// @brief Determine if the function cannot unwind.
173   bool doesNotThrow() const;
174
175   /// @brief Determine if the function does not access memory.
176   bool doesNotAccessMemory() const;
177
178   /// @brief Determine if the function does not access or only reads memory.
179   bool onlyReadsMemory() const;
180
181   /// @brief Determine if the function returns a structure.
182   bool isStructReturn() const;
183
184   /// deleteBody - This method deletes the body of the function, and converts
185   /// the linkage to external.
186   ///
187   void deleteBody() {
188     dropAllReferences();
189     setLinkage(ExternalLinkage);
190   }
191
192   /// removeFromParent - This method unlinks 'this' from the containing module,
193   /// but does not delete it.
194   ///
195   void removeFromParent();
196
197   /// eraseFromParent - This method unlinks 'this' from the containing module
198   /// and deletes it.
199   ///
200   void eraseFromParent();
201
202
203   /// Get the underlying elements of the Function... the basic block list is
204   /// empty for external functions.
205   ///
206   const ArgumentListType &getArgumentList() const {
207     CheckLazyArguments();
208     return ArgumentList;
209   }
210   ArgumentListType &getArgumentList() {
211     CheckLazyArguments();
212     return ArgumentList;
213   }
214
215   const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
216         BasicBlockListType &getBasicBlockList()       { return BasicBlocks; }
217
218   const BasicBlock       &getEntryBlock() const   { return front(); }
219         BasicBlock       &getEntryBlock()         { return front(); }
220
221   //===--------------------------------------------------------------------===//
222   // Symbol Table Accessing functions...
223
224   /// getSymbolTable() - Return the symbol table...
225   ///
226   inline       ValueSymbolTable &getValueSymbolTable()       { return *SymTab; }
227   inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
228
229
230   //===--------------------------------------------------------------------===//
231   // BasicBlock iterator forwarding functions
232   //
233   iterator                begin()       { return BasicBlocks.begin(); }
234   const_iterator          begin() const { return BasicBlocks.begin(); }
235   iterator                end  ()       { return BasicBlocks.end();   }
236   const_iterator          end  () const { return BasicBlocks.end();   }
237
238   size_t                   size() const { return BasicBlocks.size();  }
239   bool                    empty() const { return BasicBlocks.empty(); }
240   const BasicBlock       &front() const { return BasicBlocks.front(); }
241         BasicBlock       &front()       { return BasicBlocks.front(); }
242   const BasicBlock        &back() const { return BasicBlocks.back();  }
243         BasicBlock        &back()       { return BasicBlocks.back();  }
244
245   //===--------------------------------------------------------------------===//
246   // Argument iterator forwarding functions
247   //
248   arg_iterator arg_begin() {
249     CheckLazyArguments();
250     return ArgumentList.begin();
251   }
252   const_arg_iterator arg_begin() const {
253     CheckLazyArguments();
254     return ArgumentList.begin();
255   }
256   arg_iterator arg_end() {
257     CheckLazyArguments();
258     return ArgumentList.end();
259   }
260   const_arg_iterator arg_end() const {
261     CheckLazyArguments();
262     return ArgumentList.end();
263   }
264
265   size_t arg_size() const;
266   bool arg_empty() const;
267
268   virtual void print(std::ostream &OS) const { print(OS, 0); }
269   void print(std::ostream *OS) const { if (OS) print(*OS); }
270   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
271
272   /// viewCFG - This function is meant for use from the debugger.  You can just
273   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
274   /// program, displaying the CFG of the current function with the code for each
275   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
276   /// in your path.
277   ///
278   void viewCFG() const;
279
280   /// viewCFGOnly - This function is meant for use from the debugger.  It works
281   /// just like viewCFG, but it does not include the contents of basic blocks
282   /// into the nodes, just the label.  If you are only interested in the CFG
283   /// this can make the graph smaller.
284   ///
285   void viewCFGOnly() const;
286
287   /// Methods for support type inquiry through isa, cast, and dyn_cast:
288   static inline bool classof(const Function *) { return true; }
289   static inline bool classof(const Value *V) {
290     return V->getValueID() == Value::FunctionVal;
291   }
292
293   /// dropAllReferences() - This method causes all the subinstructions to "let
294   /// go" of all references that they are maintaining.  This allows one to
295   /// 'delete' a whole module at a time, even though there may be circular
296   /// references... first all references are dropped, and all use counts go to
297   /// zero.  Then everything is deleted for real.  Note that no operations are
298   /// valid on an object that has "dropped all references", except operator
299   /// delete.
300   ///
301   /// Since no other object in the module can have references into the body of a
302   /// function, dropping all references deletes the entire body of the function,
303   /// including any contained basic blocks.
304   ///
305   void dropAllReferences();
306   
307   static unsigned getBasicBlockListOffset() {
308     Function *Obj = 0;
309     return unsigned(reinterpret_cast<uintptr_t>(&Obj->BasicBlocks));
310   }
311   static unsigned getArgumentListOffset() {
312     Function *Obj = 0;
313     return unsigned(reinterpret_cast<uintptr_t>(&Obj->ArgumentList));
314   }
315 };
316
317 inline ValueSymbolTable *
318 ilist_traits<BasicBlock>::getSymTab(Function *F) {
319   return F ? &F->getValueSymbolTable() : 0;
320 }
321
322 inline ValueSymbolTable *
323 ilist_traits<Argument>::getSymTab(Function *F) {
324   return F ? &F->getValueSymbolTable() : 0;
325 }
326
327 inline int 
328 ilist_traits<BasicBlock>::getListOffset() {
329   return Function::getBasicBlockListOffset();
330 }
331
332 inline int 
333 ilist_traits<Argument>::getListOffset() {
334   return Function::getArgumentListOffset();
335 }
336
337
338 } // End llvm namespace
339
340 #endif