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"
29 // Traits for intrusive list of instructions...
30 template<> struct ilist_traits<BasicBlock>
31 : public SymbolTableListTraits<BasicBlock, Function, Function> {
33 // createNode is used to create a node that marks the end of the list...
34 static BasicBlock *createNode();
36 static iplist<BasicBlock> &getList(Function *F);
39 template<> struct ilist_traits<Argument>
40 : public SymbolTableListTraits<Argument, Function, Function> {
42 // createNode is used to create a node that marks the end of the list...
43 static Argument *createNode();
44 static iplist<Argument> &getList(Function *F);
47 class Function : public GlobalValue {
49 typedef iplist<Argument> ArgumentListType;
50 typedef iplist<BasicBlock> BasicBlockListType;
52 // BasicBlock iterators...
53 typedef BasicBlockListType::iterator iterator;
54 typedef BasicBlockListType::const_iterator const_iterator;
55 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
56 typedef std::reverse_iterator<iterator> reverse_iterator;
58 typedef ArgumentListType::iterator aiterator;
59 typedef ArgumentListType::const_iterator const_aiterator;
60 typedef std::reverse_iterator<const_aiterator> const_reverse_aiterator;
61 typedef std::reverse_iterator<aiterator> reverse_aiterator;
65 // Important things that make up a function!
66 BasicBlockListType BasicBlocks; // The basic blocks
67 ArgumentListType ArgumentList; // The formal arguments
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 // Specialize setName to handle symbol table majik...
88 virtual void setName(const std::string &name, SymbolTable *ST = 0);
90 const Type *getReturnType() const; // Return the type of the ret val
91 const FunctionType *getFunctionType() const; // Return the FunctionType for me
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 /// deleteBody - This method deletes the body of the function, and converts
110 /// the linkage to external.
113 setLinkage(ExternalLinkage);
116 // getNext/Prev - Return the next or previous function in the list. These
117 // methods should never be used directly, and are only used to implement the
118 // function list as part of the module.
120 Function *getNext() { return Next; }
121 const Function *getNext() const { return Next; }
122 Function *getPrev() { return Prev; }
123 const Function *getPrev() const { return Prev; }
125 /// Get the underlying elements of the Function... the basic block list is
126 /// empty for external functions.
128 const ArgumentListType &getArgumentList() const { return ArgumentList; }
129 ArgumentListType &getArgumentList() { return ArgumentList; }
131 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
132 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
134 const BasicBlock &getEntryBlock() const { return front(); }
135 BasicBlock &getEntryBlock() { return front(); }
137 //===--------------------------------------------------------------------===//
138 // Symbol Table Accessing functions...
140 /// getSymbolTable() - Return the symbol table...
142 inline SymbolTable &getSymbolTable() { return *SymTab; }
143 inline const SymbolTable &getSymbolTable() const { return *SymTab; }
146 //===--------------------------------------------------------------------===//
147 // BasicBlock iterator forwarding functions
149 iterator begin() { return BasicBlocks.begin(); }
150 const_iterator begin() const { return BasicBlocks.begin(); }
151 iterator end () { return BasicBlocks.end(); }
152 const_iterator end () const { return BasicBlocks.end(); }
154 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
155 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
156 reverse_iterator rend () { return BasicBlocks.rend(); }
157 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
159 unsigned size() const { return BasicBlocks.size(); }
160 bool empty() const { return BasicBlocks.empty(); }
161 const BasicBlock &front() const { return BasicBlocks.front(); }
162 BasicBlock &front() { return BasicBlocks.front(); }
163 const BasicBlock &back() const { return BasicBlocks.back(); }
164 BasicBlock &back() { return BasicBlocks.back(); }
166 //===--------------------------------------------------------------------===//
167 // Argument iterator forwarding functions
169 aiterator abegin() { return ArgumentList.begin(); }
170 const_aiterator abegin() const { return ArgumentList.begin(); }
171 aiterator aend () { return ArgumentList.end(); }
172 const_aiterator aend () const { return ArgumentList.end(); }
174 reverse_aiterator arbegin() { return ArgumentList.rbegin(); }
175 const_reverse_aiterator arbegin() const { return ArgumentList.rbegin(); }
176 reverse_aiterator arend () { return ArgumentList.rend(); }
177 const_reverse_aiterator arend () const { return ArgumentList.rend(); }
179 unsigned asize() const { return ArgumentList.size(); }
180 bool aempty() const { return ArgumentList.empty(); }
181 const Argument &afront() const { return ArgumentList.front(); }
182 Argument &afront() { return ArgumentList.front(); }
183 const Argument &aback() const { return ArgumentList.back(); }
184 Argument &aback() { return ArgumentList.back(); }
186 virtual void print(std::ostream &OS) const { print(OS, 0); }
187 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
189 /// viewCFG - This function is meant for use from the debugger. You can just
190 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
191 /// program, displaying the CFG of the current function with the code for each
192 /// basic block inside. This depends on there being a 'dot' and 'gv' program
195 void viewCFG() const;
197 /// viewCFGOnly - This function is meant for use from the debugger. It works
198 /// just like viewCFG, but it does not include the contents of basic blocks
199 /// into the nodes, just the label. If you are only interested in the CFG t
200 /// his can make the graph smaller.
202 void viewCFGOnly() const;
204 /// Methods for support type inquiry through isa, cast, and dyn_cast:
205 static inline bool classof(const Function *) { return true; }
206 static inline bool classof(const Value *V) {
207 return V->getValueType() == Value::FunctionVal;
210 /// dropAllReferences() - This method causes all the subinstructions to "let
211 /// go" of all references that they are maintaining. This allows one to
212 /// 'delete' a whole module at a time, even though there may be circular
213 /// references... first all references are dropped, and all use counts go to
214 /// zero. Then everything is delete'd for real. Note that no operations are
215 /// valid on an object that has "dropped all references", except operator
218 /// Since no other object in the module can have references into the body of a
219 /// function, dropping all references deletes the entire body of the function,
220 /// including any contained basic blocks.
222 void dropAllReferences();
225 } // End llvm namespace