The guaranteed alignment of ptr+offset is only the minimum of
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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
26 namespace llvm {
27
28 class FunctionType;
29 class ParamAttrsList;
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   ParamAttrsList *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 public:
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   ~Function();
112
113   const Type *getReturnType() const;           // Return the type of the ret val
114   const FunctionType *getFunctionType() const; // Return the FunctionType for me
115
116   /// isVarArg - Return true if this function takes a variable number of
117   /// arguments.
118   bool isVarArg() const;
119
120   /// isDeclaration - Is the body of this function unknown? (The basic block 
121   /// list is empty if so.) This is true for function declarations, but not 
122   /// true for function definitions.
123   ///
124   virtual bool isDeclaration() const { return BasicBlocks.empty(); }
125
126   /// getIntrinsicID - This method returns the ID number of the specified
127   /// function, or Intrinsic::not_intrinsic if the function is not an
128   /// instrinsic, or if the pointer is null.  This value is always defined to be
129   /// zero to allow easy checking for whether a function is intrinsic or not.
130   /// The particular intrinsic functions which correspond to this value are
131   /// defined in llvm/Intrinsics.h.
132   ///
133   unsigned getIntrinsicID(bool noAssert = false) const;
134   bool isIntrinsic() const { return getIntrinsicID() != 0; }
135
136   /// getCallingConv()/setCallingConv(uint) - These method get and set the
137   /// calling convention of this function.  The enum values for the known
138   /// calling conventions are defined in CallingConv.h.
139   unsigned getCallingConv() const { return SubclassData >> 1; }
140   void setCallingConv(unsigned CC) {
141     SubclassData = (SubclassData & 1) | (CC << 1);
142   }
143   
144   /// Obtains a constant pointer to the ParamAttrsList object which holds the
145   /// parameter attributes information, if any. 
146   /// @returns 0 if no parameter attributes have been set.
147   /// @brief Get the parameter attributes.
148   const ParamAttrsList *getParamAttrs() const { return ParamAttrs; }
149
150   /// Sets the parameter attributes for this Function. To construct a 
151   /// ParamAttrsList, see ParameterAttributes.h
152   /// @brief Set the parameter attributes.
153   void setParamAttrs(ParamAttrsList *attrs);
154
155   /// deleteBody - This method deletes the body of the function, and converts
156   /// the linkage to external.
157   ///
158   void deleteBody() {
159     dropAllReferences();
160     setLinkage(ExternalLinkage);
161   }
162
163   /// removeFromParent - This method unlinks 'this' from the containing module,
164   /// but does not delete it.
165   ///
166   void removeFromParent();
167
168   /// eraseFromParent - This method unlinks 'this' from the containing module
169   /// and deletes it.
170   ///
171   void eraseFromParent();
172
173
174   /// Get the underlying elements of the Function... the basic block list is
175   /// empty for external functions.
176   ///
177   const ArgumentListType &getArgumentList() const {
178     CheckLazyArguments();
179     return ArgumentList;
180   }
181   ArgumentListType &getArgumentList() {
182     CheckLazyArguments();
183     return ArgumentList;
184   }
185
186   const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
187         BasicBlockListType &getBasicBlockList()       { return BasicBlocks; }
188
189   const BasicBlock       &getEntryBlock() const   { return front(); }
190         BasicBlock       &getEntryBlock()         { return front(); }
191
192   //===--------------------------------------------------------------------===//
193   // Symbol Table Accessing functions...
194
195   /// getSymbolTable() - Return the symbol table...
196   ///
197   inline       ValueSymbolTable &getValueSymbolTable()       { return *SymTab; }
198   inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
199
200
201   //===--------------------------------------------------------------------===//
202   // BasicBlock iterator forwarding functions
203   //
204   iterator                begin()       { return BasicBlocks.begin(); }
205   const_iterator          begin() const { return BasicBlocks.begin(); }
206   iterator                end  ()       { return BasicBlocks.end();   }
207   const_iterator          end  () const { return BasicBlocks.end();   }
208
209   size_t                   size() const { return BasicBlocks.size();  }
210   bool                    empty() const { return BasicBlocks.empty(); }
211   const BasicBlock       &front() const { return BasicBlocks.front(); }
212         BasicBlock       &front()       { return BasicBlocks.front(); }
213   const BasicBlock        &back() const { return BasicBlocks.back();  }
214         BasicBlock        &back()       { return BasicBlocks.back();  }
215
216   //===--------------------------------------------------------------------===//
217   // Argument iterator forwarding functions
218   //
219   arg_iterator arg_begin() {
220     CheckLazyArguments();
221     return ArgumentList.begin();
222   }
223   const_arg_iterator arg_begin() const {
224     CheckLazyArguments();
225     return ArgumentList.begin();
226   }
227   arg_iterator arg_end() {
228     CheckLazyArguments();
229     return ArgumentList.end();
230   }
231   const_arg_iterator arg_end() const {
232     CheckLazyArguments();
233     return ArgumentList.end();
234   }
235
236   size_t arg_size() const;
237   bool arg_empty() const;
238
239   virtual void print(std::ostream &OS) const { print(OS, 0); }
240   void print(std::ostream *OS) const { if (OS) print(*OS); }
241   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
242
243   /// viewCFG - This function is meant for use from the debugger.  You can just
244   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
245   /// program, displaying the CFG of the current function with the code for each
246   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
247   /// in your path.
248   ///
249   void viewCFG() const;
250
251   /// viewCFGOnly - This function is meant for use from the debugger.  It works
252   /// just like viewCFG, but it does not include the contents of basic blocks
253   /// into the nodes, just the label.  If you are only interested in the CFG
254   /// this can make the graph smaller.
255   ///
256   void viewCFGOnly() const;
257
258   /// Methods for support type inquiry through isa, cast, and dyn_cast:
259   static inline bool classof(const Function *) { return true; }
260   static inline bool classof(const Value *V) {
261     return V->getValueID() == Value::FunctionVal;
262   }
263
264   /// dropAllReferences() - This method causes all the subinstructions to "let
265   /// go" of all references that they are maintaining.  This allows one to
266   /// 'delete' a whole module at a time, even though there may be circular
267   /// references... first all references are dropped, and all use counts go to
268   /// zero.  Then everything is deleted for real.  Note that no operations are
269   /// valid on an object that has "dropped all references", except operator
270   /// delete.
271   ///
272   /// Since no other object in the module can have references into the body of a
273   /// function, dropping all references deletes the entire body of the function,
274   /// including any contained basic blocks.
275   ///
276   void dropAllReferences();
277   
278   static unsigned getBasicBlockListOffset() {
279     Function *Obj = 0;
280     return unsigned(reinterpret_cast<uintptr_t>(&Obj->BasicBlocks));
281   }
282   static unsigned getArgumentListOffset() {
283     Function *Obj = 0;
284     return unsigned(reinterpret_cast<uintptr_t>(&Obj->ArgumentList));
285   }
286 };
287
288 inline ValueSymbolTable *
289 ilist_traits<BasicBlock>::getSymTab(Function *F) {
290   return F ? &F->getValueSymbolTable() : 0;
291 }
292
293 inline ValueSymbolTable *
294 ilist_traits<Argument>::getSymTab(Function *F) {
295   return F ? &F->getValueSymbolTable() : 0;
296 }
297
298 inline int 
299 ilist_traits<BasicBlock>::getListOffset() {
300   return Function::getBasicBlockListOffset();
301 }
302
303 inline int 
304 ilist_traits<Argument>::getListOffset() {
305   return Function::getArgumentListOffset();
306 }
307
308
309 } // End llvm namespace
310
311 #endif