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