1 //===-- llvm/Function.h - Class to represent a single function --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file contains the declaration of the Function class, which represents a
11 // single function/procedure in LLVM.
13 // A function basically consists of a list of basic blocks, a list of arguments,
14 // and a symbol table.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_FUNCTION_H
19 #define LLVM_FUNCTION_H
21 #include "llvm/GlobalValue.h"
22 #include "llvm/BasicBlock.h"
23 #include "llvm/Argument.h"
24 #include "llvm/Support/Annotation.h"
30 // Traits for intrusive list of instructions...
31 template<> struct ilist_traits<BasicBlock>
32 : public SymbolTableListTraits<BasicBlock, Function, Function> {
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);
40 template<> struct ilist_traits<Argument>
41 : public SymbolTableListTraits<Argument, Function, Function> {
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);
49 class Function : public GlobalValue, public Annotable {
51 typedef iplist<Argument> ArgumentListType;
52 typedef iplist<BasicBlock> BasicBlockListType;
54 // BasicBlock iterators...
55 typedef BasicBlockListType::iterator iterator;
56 typedef BasicBlockListType::const_iterator const_iterator;
58 typedef ArgumentListType::iterator arg_iterator;
59 typedef ArgumentListType::const_iterator const_arg_iterator;
60 typedef arg_iterator aiterator; // legacy, deprecated
61 typedef const_arg_iterator const_aiterator; // legacy, deprecated
64 // Important things that make up a function!
65 BasicBlockListType BasicBlocks; // The basic blocks
66 ArgumentListType ArgumentList; // The formal arguments
70 friend class SymbolTableListTraits<Function, Module, Module>;
72 void setParent(Module *parent);
73 Function *Prev, *Next;
74 void setNext(Function *N) { Next = N; }
75 void setPrev(Function *N) { Prev = N; }
78 /// Function ctor - If the (optional) Module argument is specified, the
79 /// function is automatically inserted into the end of the function list for
82 Function(const FunctionType *Ty, LinkageTypes Linkage,
83 const std::string &N = "", Module *M = 0);
86 const Type *getReturnType() const; // Return the type of the ret val
87 const FunctionType *getFunctionType() const; // Return the FunctionType for me
89 /// isVarArg - Return true if this function takes a variable number of
91 bool isVarArg() const;
93 /// isExternal - Is the body of this function unknown? (The basic block list
94 /// is empty if so.) This is true for external functions, defined as forward
97 virtual bool isExternal() const { return BasicBlocks.empty(); }
99 /// getIntrinsicID - This method returns the ID number of the specified
100 /// function, or Intrinsic::not_intrinsic if the function is not an
101 /// instrinsic, or if the pointer is null. This value is always defined to be
102 /// zero to allow easy checking for whether a function is intrinsic or not.
103 /// The particular intrinsic functions which correspond to this value are
104 /// defined in llvm/Intrinsics.h.
106 unsigned getIntrinsicID() const;
107 bool isIntrinsic() const { return getIntrinsicID() != 0; }
109 /// renameLocalSymbols - This method goes through the Function's symbol table
110 /// and renames any symbols that conflict with symbols at global scope. This
111 /// is required before printing out to a textual form, to ensure that there is
112 /// no ambiguity when parsing.
113 void renameLocalSymbols();
116 /// deleteBody - This method deletes the body of the function, and converts
117 /// the linkage to external.
121 setLinkage(ExternalLinkage);
124 /// removeFromParent - This method unlinks 'this' from the containing module,
125 /// but does not delete it.
127 void removeFromParent();
129 /// eraseFromParent - This method unlinks 'this' from the containing module
132 void eraseFromParent();
135 // getNext/Prev - Return the next or previous function in the list. These
136 // methods should never be used directly, and are only used to implement the
137 // function list as part of the module.
139 Function *getNext() { return Next; }
140 const Function *getNext() const { return Next; }
141 Function *getPrev() { return Prev; }
142 const Function *getPrev() const { return Prev; }
144 /// Get the underlying elements of the Function... the basic block list is
145 /// empty for external functions.
147 const ArgumentListType &getArgumentList() const { return ArgumentList; }
148 ArgumentListType &getArgumentList() { return ArgumentList; }
150 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
151 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
153 const BasicBlock &getEntryBlock() const { return front(); }
154 BasicBlock &getEntryBlock() { return front(); }
156 //===--------------------------------------------------------------------===//
157 // Symbol Table Accessing functions...
159 /// getSymbolTable() - Return the symbol table...
161 inline SymbolTable &getSymbolTable() { return *SymTab; }
162 inline const SymbolTable &getSymbolTable() const { return *SymTab; }
165 //===--------------------------------------------------------------------===//
166 // BasicBlock iterator forwarding functions
168 iterator begin() { return BasicBlocks.begin(); }
169 const_iterator begin() const { return BasicBlocks.begin(); }
170 iterator end () { return BasicBlocks.end(); }
171 const_iterator end () const { return BasicBlocks.end(); }
173 size_t size() const { return BasicBlocks.size(); }
174 bool empty() const { return BasicBlocks.empty(); }
175 const BasicBlock &front() const { return BasicBlocks.front(); }
176 BasicBlock &front() { return BasicBlocks.front(); }
177 const BasicBlock &back() const { return BasicBlocks.back(); }
178 BasicBlock &back() { return BasicBlocks.back(); }
180 //===--------------------------------------------------------------------===//
181 // Argument iterator forwarding functions
183 arg_iterator arg_begin() { return ArgumentList.begin(); }
184 const_arg_iterator arg_begin() const { return ArgumentList.begin(); }
185 arg_iterator arg_end () { return ArgumentList.end(); }
186 const_arg_iterator arg_end () const { return ArgumentList.end(); }
188 size_t arg_size () const { return ArgumentList.size(); }
189 bool arg_empty() const { return ArgumentList.empty(); }
191 //===--------------------------------------------------------------------===//
192 // Argument iterator forwarding functions (legacy, deprecated, will be removed)
194 arg_iterator abegin() { return ArgumentList.begin(); }
195 const_arg_iterator abegin() const { return ArgumentList.begin(); }
196 arg_iterator aend () { return ArgumentList.end(); }
197 const_arg_iterator aend () const { return ArgumentList.end(); }
199 size_t asize() const { return ArgumentList.size(); }
200 bool aempty() const { return ArgumentList.empty(); }
202 virtual void print(std::ostream &OS) const { print(OS, 0); }
203 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
205 /// viewCFG - This function is meant for use from the debugger. You can just
206 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
207 /// program, displaying the CFG of the current function with the code for each
208 /// basic block inside. This depends on there being a 'dot' and 'gv' program
211 void viewCFG() const;
213 /// viewCFGOnly - This function is meant for use from the debugger. It works
214 /// just like viewCFG, but it does not include the contents of basic blocks
215 /// into the nodes, just the label. If you are only interested in the CFG
216 /// this can make the graph smaller.
218 void viewCFGOnly() const;
220 /// Methods for support type inquiry through isa, cast, and dyn_cast:
221 static inline bool classof(const Function *) { return true; }
222 static inline bool classof(const Value *V) {
223 return V->getValueType() == Value::FunctionVal;
226 /// dropAllReferences() - This method causes all the subinstructions to "let
227 /// go" of all references that they are maintaining. This allows one to
228 /// 'delete' a whole module at a time, even though there may be circular
229 /// references... first all references are dropped, and all use counts go to
230 /// zero. Then everything is deleted for real. Note that no operations are
231 /// valid on an object that has "dropped all references", except operator
234 /// Since no other object in the module can have references into the body of a
235 /// function, dropping all references deletes the entire body of the function,
236 /// including any contained basic blocks.
238 void dropAllReferences();
241 } // End llvm namespace