ab5bc973ee89f9bf5480461795ed442e1bde7b0a
[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
30 // Traits for intrusive list of instructions...
31 template<> struct ilist_traits<BasicBlock>
32   : public SymbolTableListTraits<BasicBlock, Function, Function> {
33
34   // createSentinel is used to create a node that marks the end of the list...
35   static BasicBlock *createSentinel();
36   static void destroySentinel(BasicBlock *BB) { delete BB; }
37   static iplist<BasicBlock> &getList(Function *F);
38 };
39
40 template<> struct ilist_traits<Argument>
41   : public SymbolTableListTraits<Argument, Function, Function> {
42
43   // createSentinel is used to create a node that marks the end of the list...
44   static Argument *createSentinel();
45   static void destroySentinel(Argument *A) { delete A; }
46   static iplist<Argument> &getList(Function *F);
47 };
48
49 class Function : public GlobalValue, public Annotable {
50 public:
51   typedef iplist<Argument> ArgumentListType;
52   typedef iplist<BasicBlock> BasicBlockListType;
53
54   // BasicBlock iterators...
55   typedef BasicBlockListType::iterator iterator;
56   typedef BasicBlockListType::const_iterator const_iterator;
57   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
58   typedef std::reverse_iterator<iterator>             reverse_iterator;
59
60   typedef ArgumentListType::iterator arg_iterator;
61   typedef ArgumentListType::const_iterator const_arg_iterator;
62   typedef std::reverse_iterator<const_arg_iterator> const_reverse_arg_iterator;
63   typedef std::reverse_iterator<arg_iterator> reverse_arg_iterator;
64   typedef arg_iterator aiterator; // legacy, deprecated
65   typedef const_arg_iterator const_aiterator; // legacy, deprecated
66   typedef const_reverse_arg_iterator const_reverse_aiterator; // legacy, deprecated
67   typedef reverse_arg_iterator reverse_aiterator; // legacy, deprecated
68
69 private:
70   // Important things that make up a function!
71   BasicBlockListType  BasicBlocks;      // The basic blocks
72   ArgumentListType ArgumentList;        // The formal arguments
73
74   SymbolTable *SymTab;
75   
76   friend class SymbolTableListTraits<Function, Module, 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 public:
84   /// Function ctor - If the (optional) Module argument is specified, the
85   /// function is automatically inserted into the end of the function list for
86   /// the module.
87   ///
88   Function(const FunctionType *Ty, LinkageTypes Linkage,
89            const std::string &N = "", Module *M = 0);
90   ~Function();
91
92   const Type *getReturnType() const;           // Return the type of the ret val
93   const FunctionType *getFunctionType() const; // Return the FunctionType for me
94
95   /// isVarArg - Return true if this function takes a variable number of
96   /// arguments.
97   bool isVarArg() const;
98
99   /// isExternal - Is the body of this function unknown? (The basic block list
100   /// is empty if so.) This is true for external functions, defined as forward
101   /// "declare"ations
102   ///
103   virtual bool isExternal() const { return BasicBlocks.empty(); }
104
105   /// getIntrinsicID - This method returns the ID number of the specified
106   /// function, or Intrinsic::not_intrinsic if the function is not an
107   /// instrinsic, or if the pointer is null.  This value is always defined to be
108   /// zero to allow easy checking for whether a function is intrinsic or not.
109   /// The particular intrinsic functions which correspond to this value are
110   /// defined in llvm/Intrinsics.h.
111   ///
112   unsigned getIntrinsicID() const;
113   bool isIntrinsic() const { return getIntrinsicID() != 0; }
114
115   /// renameLocalSymbols - This method goes through the Function's symbol table
116   /// and renames any symbols that conflict with symbols at global scope.  This
117   /// is required before printing out to a textual form, to ensure that there is
118   /// no ambiguity when parsing.
119   void renameLocalSymbols();
120
121
122   /// deleteBody - This method deletes the body of the function, and converts
123   /// the linkage to external.
124   ///
125   void deleteBody() {
126     dropAllReferences();
127     setLinkage(ExternalLinkage);
128   }
129
130   /// removeFromParent - This method unlinks 'this' from the containing module,
131   /// but does not delete it.
132   ///
133   void removeFromParent();
134
135   /// eraseFromParent - This method unlinks 'this' from the containing module
136   /// and deletes it.
137   ///
138   void eraseFromParent();
139
140
141   // getNext/Prev - Return the next or previous function in the list.  These
142   // methods should never be used directly, and are only used to implement the
143   // function list as part of the module.
144   //
145         Function *getNext()       { return Next; }
146   const Function *getNext() const { return Next; }
147         Function *getPrev()       { return Prev; }
148   const Function *getPrev() const { return Prev; }
149
150   /// Get the underlying elements of the Function... the basic block list is
151   /// empty for external functions.
152   ///
153   const ArgumentListType &getArgumentList() const { return ArgumentList; }
154         ArgumentListType &getArgumentList()       { return ArgumentList; }
155
156   const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
157         BasicBlockListType &getBasicBlockList()       { return BasicBlocks; }
158
159   const BasicBlock       &getEntryBlock() const   { return front(); }
160         BasicBlock       &getEntryBlock()         { return front(); }
161
162   //===--------------------------------------------------------------------===//
163   // Symbol Table Accessing functions...
164
165   /// getSymbolTable() - Return the symbol table...
166   ///
167   inline       SymbolTable &getSymbolTable()       { return *SymTab; }
168   inline const SymbolTable &getSymbolTable() const { return *SymTab; }
169
170   
171   //===--------------------------------------------------------------------===//
172   // BasicBlock iterator forwarding functions
173   //
174   iterator                begin()       { return BasicBlocks.begin(); }
175   const_iterator          begin() const { return BasicBlocks.begin(); }
176   iterator                end  ()       { return BasicBlocks.end();   }
177   const_iterator          end  () const { return BasicBlocks.end();   }
178
179   reverse_iterator       rbegin()       { return BasicBlocks.rbegin(); }
180   const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
181   reverse_iterator       rend  ()       { return BasicBlocks.rend();   }
182   const_reverse_iterator rend  () const { return BasicBlocks.rend();   }
183
184   size_t                   size() const { return BasicBlocks.size();  }
185   bool                    empty() const { return BasicBlocks.empty(); }
186   const BasicBlock       &front() const { return BasicBlocks.front(); }
187         BasicBlock       &front()       { return BasicBlocks.front(); }
188   const BasicBlock        &back() const { return BasicBlocks.back();  }
189         BasicBlock        &back()       { return BasicBlocks.back();  }
190
191   //===--------------------------------------------------------------------===//
192   // Argument iterator forwarding functions
193   //
194   arg_iterator                arg_begin()       { return ArgumentList.begin(); }
195   const_arg_iterator          arg_begin() const { return ArgumentList.begin(); }
196   arg_iterator                arg_end  ()       { return ArgumentList.end();   }
197   const_arg_iterator          arg_end  () const { return ArgumentList.end();   }
198
199   reverse_arg_iterator       arg_rbegin()       { return ArgumentList.rbegin(); }
200   const_reverse_arg_iterator arg_rbegin() const { return ArgumentList.rbegin(); }
201   reverse_arg_iterator       arg_rend  ()       { return ArgumentList.rend();   }
202   const_reverse_arg_iterator arg_rend  () const { return ArgumentList.rend();   }
203
204   size_t                      arg_size () const { return ArgumentList.size();  }
205   bool                        arg_empty() const { return ArgumentList.empty(); }
206   const Argument             &arg_front() const { return ArgumentList.front(); }
207         Argument             &arg_front()       { return ArgumentList.front(); }
208   const Argument             &arg_back () const { return ArgumentList.back();  }
209         Argument             &arg_back ()       { return ArgumentList.back();  }
210
211   //===--------------------------------------------------------------------===//
212   // Argument iterator forwarding functions (legacy, deprecated, will be removed)
213   //
214   arg_iterator                abegin()       { return ArgumentList.begin(); }
215   const_arg_iterator          abegin() const { return ArgumentList.begin(); }
216   arg_iterator                aend  ()       { return ArgumentList.end();   }
217   const_arg_iterator          aend  () const { return ArgumentList.end();   }
218
219   reverse_arg_iterator       arbegin()       { return ArgumentList.rbegin(); }
220   const_reverse_arg_iterator arbegin() const { return ArgumentList.rbegin(); }
221   reverse_arg_iterator       arend  ()       { return ArgumentList.rend();   }
222   const_reverse_arg_iterator arend  () const { return ArgumentList.rend();   }
223
224   size_t                       asize() const { return ArgumentList.size();  }
225   bool                        aempty() const { return ArgumentList.empty(); }
226   const Argument             &afront() const { return ArgumentList.front(); }
227         Argument             &afront()       { return ArgumentList.front(); }
228   const Argument              &aback() const { return ArgumentList.back();  }
229         Argument              &aback()       { return ArgumentList.back();  }
230
231   virtual void print(std::ostream &OS) const { print(OS, 0); }
232   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
233
234   /// viewCFG - This function is meant for use from the debugger.  You can just
235   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
236   /// program, displaying the CFG of the current function with the code for each
237   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
238   /// in your path.
239   ///
240   void viewCFG() const;
241   
242   /// viewCFGOnly - This function is meant for use from the debugger.  It works
243   /// just like viewCFG, but it does not include the contents of basic blocks
244   /// into the nodes, just the label.  If you are only interested in the CFG
245   /// this can make the graph smaller.
246   ///
247   void viewCFGOnly() const;
248
249   /// Methods for support type inquiry through isa, cast, and dyn_cast:
250   static inline bool classof(const Function *) { return true; }
251   static inline bool classof(const Value *V) {
252     return V->getValueType() == Value::FunctionVal;
253   }
254
255   /// dropAllReferences() - This method causes all the subinstructions to "let
256   /// go" of all references that they are maintaining.  This allows one to
257   /// 'delete' a whole module at a time, even though there may be circular
258   /// references... first all references are dropped, and all use counts go to
259   /// zero.  Then everything is deleted for real.  Note that no operations are
260   /// valid on an object that has "dropped all references", except operator 
261   /// delete.
262   ///
263   /// Since no other object in the module can have references into the body of a
264   /// function, dropping all references deletes the entire body of the function,
265   /// including any contained basic blocks.
266   ///
267   void dropAllReferences();
268 };
269
270 } // End llvm namespace
271
272 #endif