1 //===-- llvm/Function.h - Class to represent a single function --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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"
25 #include "llvm/ParameterAttributes.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 PAListPtr 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(const Function&); // DO NOT IMPLEMENT
106 void operator=(const Function&); // DO NOT IMPLEMENT
108 /// Function ctor - If the (optional) Module argument is specified, the
109 /// function is automatically inserted into the end of the function list for
112 Function(const FunctionType *Ty, LinkageTypes Linkage,
113 const std::string &N = "", Module *M = 0);
116 const Type *getReturnType() const; // Return the type of the ret val
117 const FunctionType *getFunctionType() const; // Return the FunctionType for me
119 /// isVarArg - Return true if this function takes a variable number of
121 bool isVarArg() const;
123 /// isDeclaration - Is the body of this function unknown? (The basic block
124 /// list is empty if so.) This is true for function declarations, but not
125 /// true for function definitions.
127 virtual bool isDeclaration() const { return BasicBlocks.empty(); }
129 /// getIntrinsicID - This method returns the ID number of the specified
130 /// function, or Intrinsic::not_intrinsic if the function is not an
131 /// instrinsic, or if the pointer is null. This value is always defined to be
132 /// zero to allow easy checking for whether a function is intrinsic or not.
133 /// The particular intrinsic functions which correspond to this value are
134 /// defined in llvm/Intrinsics.h.
136 unsigned getIntrinsicID(bool noAssert = false) const;
137 bool isIntrinsic() const { return getIntrinsicID() != 0; }
139 /// getCallingConv()/setCallingConv(uint) - These method get and set the
140 /// calling convention of this function. The enum values for the known
141 /// calling conventions are defined in CallingConv.h.
142 unsigned getCallingConv() const { return SubclassData >> 1; }
143 void setCallingConv(unsigned CC) {
144 SubclassData = (SubclassData & 1) | (CC << 1);
147 /// getParamAttrs - Return the parameter attributes for this Function.
149 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
151 /// setParamAttrs - Set the parameter attributes for this Function.
153 void setParamAttrs(const PAListPtr &attrs) { ParamAttrs = attrs; }
155 /// hasCollector/getCollector/setCollector/clearCollector - The name of the
156 /// garbage collection algorithm to use during code generation.
157 bool hasCollector() const;
158 const char *getCollector() const;
159 void setCollector(const char *Str);
160 void clearCollector();
162 /// @brief Determine whether the function has the given attribute.
163 bool paramHasAttr(unsigned i, ParameterAttributes attr) const {
164 return ParamAttrs.paramHasAttr(i, attr);
167 /// @brief Extract the alignment for a call or parameter (0=unknown).
168 unsigned getParamAlignment(unsigned i) const {
169 return ParamAttrs.getParamAlignment(i);
172 /// @brief Determine if the function cannot return.
173 bool doesNotReturn() const { return paramHasAttr(0, ParamAttr::NoReturn); }
175 /// @brief Determine if the function cannot unwind.
176 bool doesNotThrow() const {
177 return paramHasAttr(0, ParamAttr::NoUnwind);
180 /// @brief Determine if the function does not access memory.
181 bool doesNotAccessMemory() const {
182 return paramHasAttr(0, ParamAttr::ReadNone);
185 /// @brief Determine if the function does not access or only reads memory.
186 bool onlyReadsMemory() const {
187 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
190 /// @brief Determine if the function returns a structure through first
191 /// pointer argument.
192 bool hasStructRetAttr() const {
193 return paramHasAttr(1, ParamAttr::StructRet);
196 /// deleteBody - This method deletes the body of the function, and converts
197 /// the linkage to external.
201 setLinkage(ExternalLinkage);
204 /// removeFromParent - This method unlinks 'this' from the containing module,
205 /// but does not delete it.
207 void removeFromParent();
209 /// eraseFromParent - This method unlinks 'this' from the containing module
212 void eraseFromParent();
215 /// Get the underlying elements of the Function... the basic block list is
216 /// empty for external functions.
218 const ArgumentListType &getArgumentList() const {
219 CheckLazyArguments();
222 ArgumentListType &getArgumentList() {
223 CheckLazyArguments();
227 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
228 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
230 const BasicBlock &getEntryBlock() const { return front(); }
231 BasicBlock &getEntryBlock() { return front(); }
233 //===--------------------------------------------------------------------===//
234 // Symbol Table Accessing functions...
236 /// getSymbolTable() - Return the symbol table...
238 inline ValueSymbolTable &getValueSymbolTable() { return *SymTab; }
239 inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
242 //===--------------------------------------------------------------------===//
243 // BasicBlock iterator forwarding functions
245 iterator begin() { return BasicBlocks.begin(); }
246 const_iterator begin() const { return BasicBlocks.begin(); }
247 iterator end () { return BasicBlocks.end(); }
248 const_iterator end () const { return BasicBlocks.end(); }
250 size_t size() const { return BasicBlocks.size(); }
251 bool empty() const { return BasicBlocks.empty(); }
252 const BasicBlock &front() const { return BasicBlocks.front(); }
253 BasicBlock &front() { return BasicBlocks.front(); }
254 const BasicBlock &back() const { return BasicBlocks.back(); }
255 BasicBlock &back() { return BasicBlocks.back(); }
257 //===--------------------------------------------------------------------===//
258 // Argument iterator forwarding functions
260 arg_iterator arg_begin() {
261 CheckLazyArguments();
262 return ArgumentList.begin();
264 const_arg_iterator arg_begin() const {
265 CheckLazyArguments();
266 return ArgumentList.begin();
268 arg_iterator arg_end() {
269 CheckLazyArguments();
270 return ArgumentList.end();
272 const_arg_iterator arg_end() const {
273 CheckLazyArguments();
274 return ArgumentList.end();
277 size_t arg_size() const;
278 bool arg_empty() const;
280 virtual void print(std::ostream &OS) const { print(OS, 0); }
281 void print(std::ostream *OS) const { if (OS) print(*OS); }
282 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
284 /// viewCFG - This function is meant for use from the debugger. You can just
285 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
286 /// program, displaying the CFG of the current function with the code for each
287 /// basic block inside. This depends on there being a 'dot' and 'gv' program
290 void viewCFG() const;
292 /// viewCFGOnly - This function is meant for use from the debugger. It works
293 /// just like viewCFG, but it does not include the contents of basic blocks
294 /// into the nodes, just the label. If you are only interested in the CFG
295 /// this can make the graph smaller.
297 void viewCFGOnly() const;
299 /// Methods for support type inquiry through isa, cast, and dyn_cast:
300 static inline bool classof(const Function *) { return true; }
301 static inline bool classof(const Value *V) {
302 return V->getValueID() == Value::FunctionVal;
305 /// dropAllReferences() - This method causes all the subinstructions to "let
306 /// go" of all references that they are maintaining. This allows one to
307 /// 'delete' a whole module at a time, even though there may be circular
308 /// references... first all references are dropped, and all use counts go to
309 /// zero. Then everything is deleted for real. Note that no operations are
310 /// valid on an object that has "dropped all references", except operator
313 /// Since no other object in the module can have references into the body of a
314 /// function, dropping all references deletes the entire body of the function,
315 /// including any contained basic blocks.
317 void dropAllReferences();
319 static unsigned getBasicBlockListOffset() {
321 return unsigned(reinterpret_cast<uintptr_t>(&Obj->BasicBlocks));
323 static unsigned getArgumentListOffset() {
325 return unsigned(reinterpret_cast<uintptr_t>(&Obj->ArgumentList));
329 inline ValueSymbolTable *
330 ilist_traits<BasicBlock>::getSymTab(Function *F) {
331 return F ? &F->getValueSymbolTable() : 0;
334 inline ValueSymbolTable *
335 ilist_traits<Argument>::getSymTab(Function *F) {
336 return F ? &F->getValueSymbolTable() : 0;
340 ilist_traits<BasicBlock>::getListOffset() {
341 return Function::getBasicBlockListOffset();
345 ilist_traits<Argument>::getListOffset() {
346 return Function::getArgumentListOffset();
350 } // End llvm namespace