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"
27 // Traits for intrusive list of instructions...
28 template<> struct ilist_traits<BasicBlock>
29 : public SymbolTableListTraits<BasicBlock, Function, Function> {
31 // createNode is used to create a node that marks the end of the list...
32 static BasicBlock *createNode();
34 static iplist<BasicBlock> &getList(Function *F);
37 template<> struct ilist_traits<Argument>
38 : public SymbolTableListTraits<Argument, Function, Function> {
40 // createNode is used to create a node that marks the end of the list...
41 static Argument *createNode();
42 static iplist<Argument> &getList(Function *F);
45 class Function : public GlobalValue {
47 typedef iplist<Argument> ArgumentListType;
48 typedef iplist<BasicBlock> BasicBlockListType;
50 // BasicBlock iterators...
51 typedef BasicBlockListType::iterator iterator;
52 typedef BasicBlockListType::const_iterator const_iterator;
53 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
54 typedef std::reverse_iterator<iterator> reverse_iterator;
56 typedef ArgumentListType::iterator aiterator;
57 typedef ArgumentListType::const_iterator const_aiterator;
58 typedef std::reverse_iterator<const_aiterator> const_reverse_aiterator;
59 typedef std::reverse_iterator<aiterator> reverse_aiterator;
63 // Important things that make up a function!
64 BasicBlockListType BasicBlocks; // The basic blocks
65 ArgumentListType ArgumentList; // The formal arguments
69 friend class SymbolTableListTraits<Function, Module, Module>;
71 void setParent(Module *parent);
72 Function *Prev, *Next;
73 void setNext(Function *N) { Next = N; }
74 void setPrev(Function *N) { Prev = N; }
77 /// Function ctor - If the (optional) Module argument is specified, the
78 /// function is automatically inserted into the end of the function list for
81 Function(const FunctionType *Ty, LinkageTypes Linkage,
82 const std::string &N = "", Module *M = 0);
85 // Specialize setName to handle symbol table majik...
86 virtual void setName(const std::string &name, SymbolTable *ST = 0);
88 const Type *getReturnType() const; // Return the type of the ret val
89 const FunctionType *getFunctionType() const; // Return the FunctionType for me
91 /// isExternal - Is the body of this function unknown? (the basic block list
92 /// is empty if so) this is true for external functions, defined as forward
95 virtual bool isExternal() const { return BasicBlocks.empty(); }
97 /// getIntrinsicID - This method returns the ID number of the specified
98 /// function, or LLVMIntrinsic::not_intrinsic if the function is not an
99 /// instrinsic, or if the pointer is null. This value is always defined to be
100 /// zero to allow easy checking for whether a function is intrinsic or not.
101 /// The particular intrinsic functions which correspond to this value are
102 /// defined in llvm/Intrinsics.h.
104 unsigned getIntrinsicID() const;
105 bool isIntrinsic() const { return getIntrinsicID() != 0; }
107 /// deleteBody - This method deletes the body of the function, and converts
108 /// the linkage to external.
111 setLinkage(ExternalLinkage);
114 // getNext/Prev - Return the next or previous function in the list. These
115 // methods should never be used directly, and are only used to implement the
116 // function list as part of the module.
118 Function *getNext() { return Next; }
119 const Function *getNext() const { return Next; }
120 Function *getPrev() { return Prev; }
121 const Function *getPrev() const { return Prev; }
123 /// Get the underlying elements of the Function... the basic block list is
124 /// empty for external functions.
126 const ArgumentListType &getArgumentList() const { return ArgumentList; }
127 ArgumentListType &getArgumentList() { return ArgumentList; }
129 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
130 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
132 const BasicBlock &getEntryBlock() const { return front(); }
133 BasicBlock &getEntryBlock() { return front(); }
135 //===--------------------------------------------------------------------===//
136 // Symbol Table Accessing functions...
138 /// getSymbolTable() - Return the symbol table...
140 inline SymbolTable &getSymbolTable() { return *SymTab; }
141 inline const SymbolTable &getSymbolTable() const { return *SymTab; }
144 //===--------------------------------------------------------------------===//
145 // BasicBlock iterator forwarding functions
147 iterator begin() { return BasicBlocks.begin(); }
148 const_iterator begin() const { return BasicBlocks.begin(); }
149 iterator end () { return BasicBlocks.end(); }
150 const_iterator end () const { return BasicBlocks.end(); }
152 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
153 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
154 reverse_iterator rend () { return BasicBlocks.rend(); }
155 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
157 unsigned size() const { return BasicBlocks.size(); }
158 bool empty() const { return BasicBlocks.empty(); }
159 const BasicBlock &front() const { return BasicBlocks.front(); }
160 BasicBlock &front() { return BasicBlocks.front(); }
161 const BasicBlock &back() const { return BasicBlocks.back(); }
162 BasicBlock &back() { return BasicBlocks.back(); }
164 //===--------------------------------------------------------------------===//
165 // Argument iterator forwarding functions
167 aiterator abegin() { return ArgumentList.begin(); }
168 const_aiterator abegin() const { return ArgumentList.begin(); }
169 aiterator aend () { return ArgumentList.end(); }
170 const_aiterator aend () const { return ArgumentList.end(); }
172 reverse_aiterator arbegin() { return ArgumentList.rbegin(); }
173 const_reverse_aiterator arbegin() const { return ArgumentList.rbegin(); }
174 reverse_aiterator arend () { return ArgumentList.rend(); }
175 const_reverse_aiterator arend () const { return ArgumentList.rend(); }
177 unsigned asize() const { return ArgumentList.size(); }
178 bool aempty() const { return ArgumentList.empty(); }
179 const Argument &afront() const { return ArgumentList.front(); }
180 Argument &afront() { return ArgumentList.front(); }
181 const Argument &aback() const { return ArgumentList.back(); }
182 Argument &aback() { return ArgumentList.back(); }
184 virtual void print(std::ostream &OS) const { print(OS, 0); }
185 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
187 /// viewCFG - This function is meant for use from the debugger. You can just
188 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
189 /// program, displaying the CFG of the current function with the code for each
190 /// basic block inside. This depends on there being a 'dot' and 'gv' program
193 void viewCFG() const;
195 /// viewCFGOnly - This function is meant for use from the debugger. It works
196 /// just like viewCFG, but it does not include the contents of basic blocks
197 /// into the nodes, just the label. If you are only interested in the CFG t
198 /// his can make the graph smaller.
200 void viewCFGOnly() const;
202 /// Methods for support type inquiry through isa, cast, and dyn_cast:
203 static inline bool classof(const Function *) { return true; }
204 static inline bool classof(const Value *V) {
205 return V->getValueType() == Value::FunctionVal;
208 /// dropAllReferences() - This method causes all the subinstructions to "let
209 /// go" of all references that they are maintaining. This allows one to
210 /// 'delete' a whole module at a time, even though there may be circular
211 /// references... first all references are dropped, and all use counts go to
212 /// zero. Then everything is delete'd for real. Note that no operations are
213 /// valid on an object that has "dropped all references", except operator
216 /// Since no other object in the module can have references into the body of a
217 /// function, dropping all references deletes the entire body of the function,
218 /// including any contained basic blocks.
220 void dropAllReferences();