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"
31 // Traits for intrusive list of instructions...
32 template<> struct ilist_traits<BasicBlock>
33 : public SymbolTableListTraits<BasicBlock, Function> {
35 // createSentinel is used to create a node that marks the end of the list...
36 static BasicBlock *createSentinel();
37 static void destroySentinel(BasicBlock *BB) { delete BB; }
38 static iplist<BasicBlock> &getList(Function *F);
39 static ValueSymbolTable *getSymTab(Function *ItemParent);
40 static int getListOffset();
43 template<> struct ilist_traits<Argument>
44 : public SymbolTableListTraits<Argument, Function> {
46 // createSentinel is used to create a node that marks the end of the list...
47 static Argument *createSentinel();
48 static void destroySentinel(Argument *A) { delete A; }
49 static iplist<Argument> &getList(Function *F);
50 static ValueSymbolTable *getSymTab(Function *ItemParent);
51 static int getListOffset();
54 class Function : public GlobalValue, public Annotable {
56 typedef iplist<Argument> ArgumentListType;
57 typedef iplist<BasicBlock> BasicBlockListType;
59 // BasicBlock iterators...
60 typedef BasicBlockListType::iterator iterator;
61 typedef BasicBlockListType::const_iterator const_iterator;
63 typedef ArgumentListType::iterator arg_iterator;
64 typedef ArgumentListType::const_iterator const_arg_iterator;
67 // Important things that make up a function!
68 BasicBlockListType BasicBlocks; ///< The basic blocks
69 mutable ArgumentListType ArgumentList; ///< The formal arguments
70 ValueSymbolTable *SymTab; ///< Symbol table of args/instructions
71 const ParamAttrsList *ParamAttrs; ///< Parameter attributes
73 // The Calling Convention is stored in Value::SubclassData.
74 /*unsigned CallingConvention;*/
76 friend class SymbolTableListTraits<Function, Module>;
78 void setParent(Module *parent);
79 Function *Prev, *Next;
80 void setNext(Function *N) { Next = N; }
81 void setPrev(Function *N) { Prev = N; }
83 // getNext/Prev - Return the next or previous function in the list. These
84 // methods should never be used directly, and are only used to implement the
85 // function list as part of the module.
87 Function *getNext() { return Next; }
88 const Function *getNext() const { return Next; }
89 Function *getPrev() { return Prev; }
90 const Function *getPrev() const { return Prev; }
92 /// hasLazyArguments/CheckLazyArguments - The argument list of a function is
93 /// built on demand, so that the list isn't allocated until the first client
94 /// needs it. The hasLazyArguments predicate returns true if the arg list
95 /// hasn't been set up yet.
96 bool hasLazyArguments() const {
97 return SubclassData & 1;
99 void CheckLazyArguments() const {
100 if (hasLazyArguments())
101 BuildLazyArguments();
103 void BuildLazyArguments() const;
105 /// Function ctor - If the (optional) Module argument is specified, the
106 /// function is automatically inserted into the end of the function list for
109 Function(const FunctionType *Ty, LinkageTypes Linkage,
110 const std::string &N = "", Module *M = 0);
113 const Type *getReturnType() const; // Return the type of the ret val
114 const FunctionType *getFunctionType() const; // Return the FunctionType for me
116 /// isVarArg - Return true if this function takes a variable number of
118 bool isVarArg() const;
120 /// isDeclaration - Is the body of this function unknown? (The basic block
121 /// list is empty if so.) This is true for function declarations, but not
122 /// true for function definitions.
124 virtual bool isDeclaration() const { return BasicBlocks.empty(); }
126 /// getIntrinsicID - This method returns the ID number of the specified
127 /// function, or Intrinsic::not_intrinsic if the function is not an
128 /// instrinsic, or if the pointer is null. This value is always defined to be
129 /// zero to allow easy checking for whether a function is intrinsic or not.
130 /// The particular intrinsic functions which correspond to this value are
131 /// defined in llvm/Intrinsics.h.
133 unsigned getIntrinsicID(bool noAssert = false) const;
134 bool isIntrinsic() const { return getIntrinsicID() != 0; }
136 /// getCallingConv()/setCallingConv(uint) - These method get and set the
137 /// calling convention of this function. The enum values for the known
138 /// calling conventions are defined in CallingConv.h.
139 unsigned getCallingConv() const { return SubclassData >> 1; }
140 void setCallingConv(unsigned CC) {
141 SubclassData = (SubclassData & 1) | (CC << 1);
144 /// Obtains a constant pointer to the ParamAttrsList object which holds the
145 /// parameter attributes information, if any.
146 /// @returns 0 if no parameter attributes have been set.
147 /// @brief Get the parameter attributes.
148 const ParamAttrsList *getParamAttrs() const { return ParamAttrs; }
150 /// Sets the parameter attributes for this Function. To construct a
151 /// ParamAttrsList, see ParameterAttributes.h
152 /// @brief Set the parameter attributes.
153 void setParamAttrs(const ParamAttrsList *attrs);
155 /// @brief Determine if the function returns a structure.
156 bool isStructReturn() const;
158 /// deleteBody - This method deletes the body of the function, and converts
159 /// the linkage to external.
163 setLinkage(ExternalLinkage);
166 /// removeFromParent - This method unlinks 'this' from the containing module,
167 /// but does not delete it.
169 void removeFromParent();
171 /// eraseFromParent - This method unlinks 'this' from the containing module
174 void eraseFromParent();
177 /// Get the underlying elements of the Function... the basic block list is
178 /// empty for external functions.
180 const ArgumentListType &getArgumentList() const {
181 CheckLazyArguments();
184 ArgumentListType &getArgumentList() {
185 CheckLazyArguments();
189 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
190 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
192 const BasicBlock &getEntryBlock() const { return front(); }
193 BasicBlock &getEntryBlock() { return front(); }
195 //===--------------------------------------------------------------------===//
196 // Symbol Table Accessing functions...
198 /// getSymbolTable() - Return the symbol table...
200 inline ValueSymbolTable &getValueSymbolTable() { return *SymTab; }
201 inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
204 //===--------------------------------------------------------------------===//
205 // BasicBlock iterator forwarding functions
207 iterator begin() { return BasicBlocks.begin(); }
208 const_iterator begin() const { return BasicBlocks.begin(); }
209 iterator end () { return BasicBlocks.end(); }
210 const_iterator end () const { return BasicBlocks.end(); }
212 size_t size() const { return BasicBlocks.size(); }
213 bool empty() const { return BasicBlocks.empty(); }
214 const BasicBlock &front() const { return BasicBlocks.front(); }
215 BasicBlock &front() { return BasicBlocks.front(); }
216 const BasicBlock &back() const { return BasicBlocks.back(); }
217 BasicBlock &back() { return BasicBlocks.back(); }
219 //===--------------------------------------------------------------------===//
220 // Argument iterator forwarding functions
222 arg_iterator arg_begin() {
223 CheckLazyArguments();
224 return ArgumentList.begin();
226 const_arg_iterator arg_begin() const {
227 CheckLazyArguments();
228 return ArgumentList.begin();
230 arg_iterator arg_end() {
231 CheckLazyArguments();
232 return ArgumentList.end();
234 const_arg_iterator arg_end() const {
235 CheckLazyArguments();
236 return ArgumentList.end();
239 size_t arg_size() const;
240 bool arg_empty() const;
242 virtual void print(std::ostream &OS) const { print(OS, 0); }
243 void print(std::ostream *OS) const { if (OS) print(*OS); }
244 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
246 /// viewCFG - This function is meant for use from the debugger. You can just
247 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
248 /// program, displaying the CFG of the current function with the code for each
249 /// basic block inside. This depends on there being a 'dot' and 'gv' program
252 void viewCFG() const;
254 /// viewCFGOnly - This function is meant for use from the debugger. It works
255 /// just like viewCFG, but it does not include the contents of basic blocks
256 /// into the nodes, just the label. If you are only interested in the CFG
257 /// this can make the graph smaller.
259 void viewCFGOnly() const;
261 /// Methods for support type inquiry through isa, cast, and dyn_cast:
262 static inline bool classof(const Function *) { return true; }
263 static inline bool classof(const Value *V) {
264 return V->getValueID() == Value::FunctionVal;
267 /// dropAllReferences() - This method causes all the subinstructions to "let
268 /// go" of all references that they are maintaining. This allows one to
269 /// 'delete' a whole module at a time, even though there may be circular
270 /// references... first all references are dropped, and all use counts go to
271 /// zero. Then everything is deleted for real. Note that no operations are
272 /// valid on an object that has "dropped all references", except operator
275 /// Since no other object in the module can have references into the body of a
276 /// function, dropping all references deletes the entire body of the function,
277 /// including any contained basic blocks.
279 void dropAllReferences();
281 static unsigned getBasicBlockListOffset() {
283 return unsigned(reinterpret_cast<uintptr_t>(&Obj->BasicBlocks));
285 static unsigned getArgumentListOffset() {
287 return unsigned(reinterpret_cast<uintptr_t>(&Obj->ArgumentList));
291 inline ValueSymbolTable *
292 ilist_traits<BasicBlock>::getSymTab(Function *F) {
293 return F ? &F->getValueSymbolTable() : 0;
296 inline ValueSymbolTable *
297 ilist_traits<Argument>::getSymTab(Function *F) {
298 return F ? &F->getValueSymbolTable() : 0;
302 ilist_traits<BasicBlock>::getListOffset() {
303 return Function::getBasicBlockListOffset();
307 ilist_traits<Argument>::getListOffset() {
308 return Function::getArgumentListOffset();
312 } // End llvm namespace