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