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/ParameterAttributes.h"
25 #include "llvm/Support/Annotation.h"
32 // Traits for intrusive list of instructions...
33 template<> struct ilist_traits<BasicBlock>
34 : public SymbolTableListTraits<BasicBlock, Function> {
36 // createSentinel is used to create a node that marks the end of the list...
37 static BasicBlock *createSentinel();
38 static void destroySentinel(BasicBlock *BB) { delete BB; }
39 static iplist<BasicBlock> &getList(Function *F);
40 static ValueSymbolTable *getSymTab(Function *ItemParent);
41 static int getListOffset();
44 template<> struct ilist_traits<Argument>
45 : public SymbolTableListTraits<Argument, Function> {
47 // createSentinel is used to create a node that marks the end of the list...
48 static Argument *createSentinel();
49 static void destroySentinel(Argument *A) { delete A; }
50 static iplist<Argument> &getList(Function *F);
51 static ValueSymbolTable *getSymTab(Function *ItemParent);
52 static int getListOffset();
55 class Function : public GlobalValue, public Annotable {
57 typedef iplist<Argument> ArgumentListType;
58 typedef iplist<BasicBlock> BasicBlockListType;
60 // BasicBlock iterators...
61 typedef BasicBlockListType::iterator iterator;
62 typedef BasicBlockListType::const_iterator const_iterator;
64 typedef ArgumentListType::iterator arg_iterator;
65 typedef ArgumentListType::const_iterator const_arg_iterator;
68 // Important things that make up a function!
69 BasicBlockListType BasicBlocks; ///< The basic blocks
70 mutable ArgumentListType ArgumentList; ///< The formal arguments
71 ValueSymbolTable *SymTab; ///< Symbol table of args/instructions
72 const ParamAttrsList *ParamAttrs; ///< Parameter attributes
74 // The Calling Convention is stored in Value::SubclassData.
75 /*unsigned CallingConvention;*/
77 friend class SymbolTableListTraits<Function, Module>;
79 void setParent(Module *parent);
80 Function *Prev, *Next;
81 void setNext(Function *N) { Next = N; }
82 void setPrev(Function *N) { Prev = N; }
84 // getNext/Prev - Return the next or previous function in the list. These
85 // methods should never be used directly, and are only used to implement the
86 // function list as part of the module.
88 Function *getNext() { return Next; }
89 const Function *getNext() const { return Next; }
90 Function *getPrev() { return Prev; }
91 const Function *getPrev() const { return Prev; }
93 /// hasLazyArguments/CheckLazyArguments - The argument list of a function is
94 /// built on demand, so that the list isn't allocated until the first client
95 /// needs it. The hasLazyArguments predicate returns true if the arg list
96 /// hasn't been set up yet.
97 bool hasLazyArguments() const {
98 return SubclassData & 1;
100 void CheckLazyArguments() const {
101 if (hasLazyArguments())
102 BuildLazyArguments();
104 void BuildLazyArguments() const;
106 /// Function ctor - If the (optional) Module argument is specified, the
107 /// function is automatically inserted into the end of the function list for
110 Function(const FunctionType *Ty, LinkageTypes Linkage,
111 const std::string &N = "", Module *M = 0);
114 const Type *getReturnType() const; // Return the type of the ret val
115 const FunctionType *getFunctionType() const; // Return the FunctionType for me
117 /// isVarArg - Return true if this function takes a variable number of
119 bool isVarArg() const;
121 /// isDeclaration - Is the body of this function unknown? (The basic block
122 /// list is empty if so.) This is true for function declarations, but not
123 /// true for function definitions.
125 virtual bool isDeclaration() const { return BasicBlocks.empty(); }
127 /// getIntrinsicID - This method returns the ID number of the specified
128 /// function, or Intrinsic::not_intrinsic if the function is not an
129 /// instrinsic, or if the pointer is null. This value is always defined to be
130 /// zero to allow easy checking for whether a function is intrinsic or not.
131 /// The particular intrinsic functions which correspond to this value are
132 /// defined in llvm/Intrinsics.h.
134 unsigned getIntrinsicID(bool noAssert = false) const;
135 bool isIntrinsic() const { return getIntrinsicID() != 0; }
137 /// getCallingConv()/setCallingConv(uint) - These method get and set the
138 /// calling convention of this function. The enum values for the known
139 /// calling conventions are defined in CallingConv.h.
140 unsigned getCallingConv() const { return SubclassData >> 1; }
141 void setCallingConv(unsigned CC) {
142 SubclassData = (SubclassData & 1) | (CC << 1);
145 /// Obtains a constant pointer to the ParamAttrsList object which holds the
146 /// parameter attributes information, if any.
147 /// @returns 0 if no parameter attributes have been set.
148 /// @brief Get the parameter attributes.
149 const ParamAttrsList *getParamAttrs() const { return ParamAttrs; }
151 /// Sets the parameter attributes for this Function. To construct a
152 /// ParamAttrsList, see ParameterAttributes.h
153 /// @brief Set the parameter attributes.
154 void setParamAttrs(const ParamAttrsList *attrs);
156 /// @brief Determine whether the function has the given attribute.
157 bool paramHasAttr(uint16_t i, ParameterAttributes attr) const {
158 return ParamAttrs && ParamAttrs->paramHasAttr(i, attr);
161 /// @brief Determine if the function does not access memory.
162 bool doesNotAccessMemory() const {
163 return paramHasAttr(0, ParamAttr::ReadNone);
166 /// @brief Determine if the function does not access or only reads memory.
167 bool onlyReadsMemory() const {
168 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
171 /// @brief Determine if the function returns a structure.
172 bool isStructReturn() const {
173 return paramHasAttr(1, ParamAttr::StructRet);
176 /// deleteBody - This method deletes the body of the function, and converts
177 /// the linkage to external.
181 setLinkage(ExternalLinkage);
184 /// removeFromParent - This method unlinks 'this' from the containing module,
185 /// but does not delete it.
187 void removeFromParent();
189 /// eraseFromParent - This method unlinks 'this' from the containing module
192 void eraseFromParent();
195 /// Get the underlying elements of the Function... the basic block list is
196 /// empty for external functions.
198 const ArgumentListType &getArgumentList() const {
199 CheckLazyArguments();
202 ArgumentListType &getArgumentList() {
203 CheckLazyArguments();
207 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
208 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
210 const BasicBlock &getEntryBlock() const { return front(); }
211 BasicBlock &getEntryBlock() { return front(); }
213 //===--------------------------------------------------------------------===//
214 // Symbol Table Accessing functions...
216 /// getSymbolTable() - Return the symbol table...
218 inline ValueSymbolTable &getValueSymbolTable() { return *SymTab; }
219 inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
222 //===--------------------------------------------------------------------===//
223 // BasicBlock iterator forwarding functions
225 iterator begin() { return BasicBlocks.begin(); }
226 const_iterator begin() const { return BasicBlocks.begin(); }
227 iterator end () { return BasicBlocks.end(); }
228 const_iterator end () const { return BasicBlocks.end(); }
230 size_t size() const { return BasicBlocks.size(); }
231 bool empty() const { return BasicBlocks.empty(); }
232 const BasicBlock &front() const { return BasicBlocks.front(); }
233 BasicBlock &front() { return BasicBlocks.front(); }
234 const BasicBlock &back() const { return BasicBlocks.back(); }
235 BasicBlock &back() { return BasicBlocks.back(); }
237 //===--------------------------------------------------------------------===//
238 // Argument iterator forwarding functions
240 arg_iterator arg_begin() {
241 CheckLazyArguments();
242 return ArgumentList.begin();
244 const_arg_iterator arg_begin() const {
245 CheckLazyArguments();
246 return ArgumentList.begin();
248 arg_iterator arg_end() {
249 CheckLazyArguments();
250 return ArgumentList.end();
252 const_arg_iterator arg_end() const {
253 CheckLazyArguments();
254 return ArgumentList.end();
257 size_t arg_size() const;
258 bool arg_empty() const;
260 virtual void print(std::ostream &OS) const { print(OS, 0); }
261 void print(std::ostream *OS) const { if (OS) print(*OS); }
262 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
264 /// viewCFG - This function is meant for use from the debugger. You can just
265 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
266 /// program, displaying the CFG of the current function with the code for each
267 /// basic block inside. This depends on there being a 'dot' and 'gv' program
270 void viewCFG() const;
272 /// viewCFGOnly - This function is meant for use from the debugger. It works
273 /// just like viewCFG, but it does not include the contents of basic blocks
274 /// into the nodes, just the label. If you are only interested in the CFG
275 /// this can make the graph smaller.
277 void viewCFGOnly() const;
279 /// Methods for support type inquiry through isa, cast, and dyn_cast:
280 static inline bool classof(const Function *) { return true; }
281 static inline bool classof(const Value *V) {
282 return V->getValueID() == Value::FunctionVal;
285 /// dropAllReferences() - This method causes all the subinstructions to "let
286 /// go" of all references that they are maintaining. This allows one to
287 /// 'delete' a whole module at a time, even though there may be circular
288 /// references... first all references are dropped, and all use counts go to
289 /// zero. Then everything is deleted for real. Note that no operations are
290 /// valid on an object that has "dropped all references", except operator
293 /// Since no other object in the module can have references into the body of a
294 /// function, dropping all references deletes the entire body of the function,
295 /// including any contained basic blocks.
297 void dropAllReferences();
299 static unsigned getBasicBlockListOffset() {
301 return unsigned(reinterpret_cast<uintptr_t>(&Obj->BasicBlocks));
303 static unsigned getArgumentListOffset() {
305 return unsigned(reinterpret_cast<uintptr_t>(&Obj->ArgumentList));
309 inline ValueSymbolTable *
310 ilist_traits<BasicBlock>::getSymTab(Function *F) {
311 return F ? &F->getValueSymbolTable() : 0;
314 inline ValueSymbolTable *
315 ilist_traits<Argument>::getSymTab(Function *F) {
316 return F ? &F->getValueSymbolTable() : 0;
320 ilist_traits<BasicBlock>::getListOffset() {
321 return Function::getBasicBlockListOffset();
325 ilist_traits<Argument>::getListOffset() {
326 return Function::getArgumentListOffset();
330 } // End llvm namespace