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
69 unsigned CallingConvention;
71 friend class SymbolTableListTraits<Function, Module, Module>;
73 void setParent(Module *parent);
74 Function *Prev, *Next;
75 void setNext(Function *N) { Next = N; }
76 void setPrev(Function *N) { Prev = N; }
79 /// Function ctor - If the (optional) Module argument is specified, the
80 /// function is automatically inserted into the end of the function list for
83 Function(const FunctionType *Ty, LinkageTypes Linkage,
84 const std::string &N = "", Module *M = 0);
87 const Type *getReturnType() const; // Return the type of the ret val
88 const FunctionType *getFunctionType() const; // Return the FunctionType for me
90 /// isVarArg - Return true if this function takes a variable number of
92 bool isVarArg() const;
94 /// isExternal - Is the body of this function unknown? (The basic block list
95 /// is empty if so.) This is true for external functions, defined as forward
98 virtual bool isExternal() const { return BasicBlocks.empty(); }
100 /// getIntrinsicID - This method returns the ID number of the specified
101 /// function, or Intrinsic::not_intrinsic if the function is not an
102 /// instrinsic, or if the pointer is null. This value is always defined to be
103 /// zero to allow easy checking for whether a function is intrinsic or not.
104 /// The particular intrinsic functions which correspond to this value are
105 /// defined in llvm/Intrinsics.h.
107 unsigned getIntrinsicID() const;
108 bool isIntrinsic() const { return getIntrinsicID() != 0; }
110 /// getCallingConv()/setCallingConv(uint) - These method get and set the
111 /// calling convention of this function. The enum values for the known
112 /// calling conventions are defined in CallingConv.h.
113 unsigned getCallingConv() const { return CallingConvention; }
114 void setCallingConv(unsigned CC) { CallingConvention = CC; }
116 /// renameLocalSymbols - This method goes through the Function's symbol table
117 /// and renames any symbols that conflict with symbols at global scope. This
118 /// is required before printing out to a textual form, to ensure that there is
119 /// no ambiguity when parsing.
120 void renameLocalSymbols();
123 /// deleteBody - This method deletes the body of the function, and converts
124 /// the linkage to external.
128 setLinkage(ExternalLinkage);
131 /// removeFromParent - This method unlinks 'this' from the containing module,
132 /// but does not delete it.
134 void removeFromParent();
136 /// eraseFromParent - This method unlinks 'this' from the containing module
139 void eraseFromParent();
142 // getNext/Prev - Return the next or previous function in the list. These
143 // methods should never be used directly, and are only used to implement the
144 // function list as part of the module.
146 Function *getNext() { return Next; }
147 const Function *getNext() const { return Next; }
148 Function *getPrev() { return Prev; }
149 const Function *getPrev() const { return Prev; }
151 /// Get the underlying elements of the Function... the basic block list is
152 /// empty for external functions.
154 const ArgumentListType &getArgumentList() const { return ArgumentList; }
155 ArgumentListType &getArgumentList() { return ArgumentList; }
157 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
158 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
160 const BasicBlock &getEntryBlock() const { return front(); }
161 BasicBlock &getEntryBlock() { return front(); }
163 //===--------------------------------------------------------------------===//
164 // Symbol Table Accessing functions...
166 /// getSymbolTable() - Return the symbol table...
168 inline SymbolTable &getSymbolTable() { return *SymTab; }
169 inline const SymbolTable &getSymbolTable() const { return *SymTab; }
172 //===--------------------------------------------------------------------===//
173 // BasicBlock iterator forwarding functions
175 iterator begin() { return BasicBlocks.begin(); }
176 const_iterator begin() const { return BasicBlocks.begin(); }
177 iterator end () { return BasicBlocks.end(); }
178 const_iterator end () const { return BasicBlocks.end(); }
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(); }
187 //===--------------------------------------------------------------------===//
188 // Argument iterator forwarding functions
190 arg_iterator arg_begin() { return ArgumentList.begin(); }
191 const_arg_iterator arg_begin() const { return ArgumentList.begin(); }
192 arg_iterator arg_end () { return ArgumentList.end(); }
193 const_arg_iterator arg_end () const { return ArgumentList.end(); }
195 size_t arg_size () const { return ArgumentList.size(); }
196 bool arg_empty() const { return ArgumentList.empty(); }
198 //===--------------------------------------------------------------------===//
199 // Argument iterator forwarding functions (legacy, deprecated, will be removed)
201 arg_iterator abegin() { return ArgumentList.begin(); }
202 const_arg_iterator abegin() const { return ArgumentList.begin(); }
203 arg_iterator aend () { return ArgumentList.end(); }
204 const_arg_iterator aend () const { return ArgumentList.end(); }
206 size_t asize() const { return ArgumentList.size(); }
207 bool aempty() const { return ArgumentList.empty(); }
209 virtual void print(std::ostream &OS) const { print(OS, 0); }
210 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
212 /// viewCFG - This function is meant for use from the debugger. You can just
213 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
214 /// program, displaying the CFG of the current function with the code for each
215 /// basic block inside. This depends on there being a 'dot' and 'gv' program
218 void viewCFG() const;
220 /// viewCFGOnly - This function is meant for use from the debugger. It works
221 /// just like viewCFG, but it does not include the contents of basic blocks
222 /// into the nodes, just the label. If you are only interested in the CFG
223 /// this can make the graph smaller.
225 void viewCFGOnly() const;
227 /// Methods for support type inquiry through isa, cast, and dyn_cast:
228 static inline bool classof(const Function *) { return true; }
229 static inline bool classof(const Value *V) {
230 return V->getValueType() == Value::FunctionVal;
233 /// dropAllReferences() - This method causes all the subinstructions to "let
234 /// go" of all references that they are maintaining. This allows one to
235 /// 'delete' a whole module at a time, even though there may be circular
236 /// references... first all references are dropped, and all use counts go to
237 /// zero. Then everything is deleted for real. Note that no operations are
238 /// valid on an object that has "dropped all references", except operator
241 /// Since no other object in the module can have references into the body of a
242 /// function, dropping all references deletes the entire body of the function,
243 /// including any contained basic blocks.
245 void dropAllReferences();
248 } // End llvm namespace