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