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/Attributes.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,
55 public ilist_node<Function> {
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 AttrListPtr AttributeList; ///< 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);
81 /// hasLazyArguments/CheckLazyArguments - The argument list of a function is
82 /// built on demand, so that the list isn't allocated until the first client
83 /// needs it. The hasLazyArguments predicate returns true if the arg list
84 /// hasn't been set up yet.
85 bool hasLazyArguments() const {
86 return SubclassData & 1;
88 void CheckLazyArguments() const {
89 if (hasLazyArguments())
92 void BuildLazyArguments() const;
94 Function(const Function&); // DO NOT IMPLEMENT
95 void operator=(const Function&); // DO NOT IMPLEMENT
97 /// Function ctor - If the (optional) Module argument is specified, the
98 /// function is automatically inserted into the end of the function list for
101 Function(const FunctionType *Ty, LinkageTypes Linkage,
102 const std::string &N = "", Module *M = 0);
105 static Function *Create(const FunctionType *Ty, LinkageTypes Linkage,
106 const std::string &N = "", Module *M = 0) {
107 return new(0) Function(Ty, Linkage, N, M);
112 const Type *getReturnType() const; // Return the type of the ret val
113 const FunctionType *getFunctionType() const; // Return the FunctionType for me
115 /// isVarArg - Return true if this function takes a variable number of
117 bool isVarArg() const;
119 /// isDeclaration - Is the body of this function unknown? (The basic block
120 /// list is empty if so.) This is true for function declarations, but not
121 /// true for function definitions.
123 virtual bool isDeclaration() const { return BasicBlocks.empty(); }
125 /// getIntrinsicID - This method returns the ID number of the specified
126 /// function, or Intrinsic::not_intrinsic if the function is not an
127 /// instrinsic, or if the pointer is null. This value is always defined to be
128 /// zero to allow easy checking for whether a function is intrinsic or not.
129 /// The particular intrinsic functions which correspond to this value are
130 /// defined in llvm/Intrinsics.h.
132 unsigned getIntrinsicID(bool noAssert = false) const;
133 bool isIntrinsic() const { return getIntrinsicID() != 0; }
135 /// getCallingConv()/setCallingConv(uint) - These method get and set the
136 /// calling convention of this function. The enum values for the known
137 /// calling conventions are defined in CallingConv.h.
138 unsigned getCallingConv() const { return SubclassData >> 1; }
139 void setCallingConv(unsigned CC) {
140 SubclassData = (SubclassData & 1) | (CC << 1);
143 /// getAttributes - Return the attribute list for this Function.
145 const AttrListPtr &getAttributes() const { return AttributeList; }
147 /// setAttributes - Set the attribute list for this Function.
149 void setAttributes(const AttrListPtr &attrs) { AttributeList = attrs; }
151 /// hasFnAttr - Return true if this function has the given attribute.
152 bool hasFnAttr(Attributes N) const {
153 // Function Attributes are stored at ~0 index
154 return AttributeList.paramHasAttr(~0U, N);
157 /// addFnAttr - Add function attributes to this function.
159 void addFnAttr(Attributes N) {
160 // Function Attributes are stored at ~0 index
161 addAttribute(~0U, N);
164 /// removeFnAttr - Remove function attributes from this function.
166 void removeFnAttr(Attributes N) {
167 // Function Attributes are stored at ~0 index
168 removeAttribute(~0U, N);
171 /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm
172 /// to use during code generation.
174 const char *getGC() const;
175 void setGC(const char *Str);
178 /// @brief Determine whether the function has the given attribute.
179 bool paramHasAttr(unsigned i, Attributes attr) const {
180 return AttributeList.paramHasAttr(i, attr);
183 /// addAttribute - adds the attribute to the list of attributes.
184 void addAttribute(unsigned i, Attributes attr);
186 /// removeAttribute - removes the attribute from the list of attributes.
187 void removeAttribute(unsigned i, Attributes attr);
189 /// @brief Extract the alignment for a call or parameter (0=unknown).
190 unsigned getParamAlignment(unsigned i) const {
191 return AttributeList.getParamAlignment(i);
194 /// @brief Determine if the function does not access memory.
195 bool doesNotAccessMemory() const {
196 return hasFnAttr(Attribute::ReadNone);
198 void setDoesNotAccessMemory(bool DoesNotAccessMemory = true) {
199 if (DoesNotAccessMemory) addFnAttr(Attribute::ReadNone);
200 else removeFnAttr(Attribute::ReadNone);
203 /// @brief Determine if the function does not access or only reads memory.
204 bool onlyReadsMemory() const {
205 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
207 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
208 if (OnlyReadsMemory) addFnAttr(Attribute::ReadOnly);
209 else removeFnAttr(Attribute::ReadOnly | Attribute::ReadNone);
212 /// @brief Determine if the function cannot return.
213 bool doesNotReturn() const {
214 return hasFnAttr(Attribute::NoReturn);
216 void setDoesNotReturn(bool DoesNotReturn = true) {
217 if (DoesNotReturn) addFnAttr(Attribute::NoReturn);
218 else removeFnAttr(Attribute::NoReturn);
221 /// @brief Determine if the function cannot unwind.
222 bool doesNotThrow() const {
223 return hasFnAttr(Attribute::NoUnwind);
225 void setDoesNotThrow(bool DoesNotThrow = true) {
226 if (DoesNotThrow) addFnAttr(Attribute::NoUnwind);
227 else removeFnAttr(Attribute::NoUnwind);
230 /// @brief Determine if the function returns a structure through first
231 /// pointer argument.
232 bool hasStructRetAttr() const {
233 return paramHasAttr(1, Attribute::StructRet);
236 /// @brief Determine if the parameter does not alias other parameters.
237 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
238 bool doesNotAlias(unsigned n) const {
239 return paramHasAttr(n, Attribute::NoAlias);
241 void setDoesNotAlias(unsigned n, bool DoesNotAlias = true) {
242 if (DoesNotAlias) addAttribute(n, Attribute::NoAlias);
243 else removeAttribute(n, Attribute::NoAlias);
246 /// @brief Determine if the parameter can be captured.
247 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
248 bool doesNotCapture(unsigned n) const {
249 return paramHasAttr(n, Attribute::NoCapture);
251 void setDoesNotCapture(unsigned n, bool DoesNotCapture = true) {
252 if (DoesNotCapture) addAttribute(n, Attribute::NoCapture);
253 else removeAttribute(n, Attribute::NoCapture);
256 /// copyAttributesFrom - copy all additional attributes (those not needed to
257 /// create a Function) from the Function Src to this one.
258 void copyAttributesFrom(const GlobalValue *Src);
260 /// deleteBody - This method deletes the body of the function, and converts
261 /// the linkage to external.
265 setLinkage(ExternalLinkage);
268 /// removeFromParent - This method unlinks 'this' from the containing module,
269 /// but does not delete it.
271 virtual void removeFromParent();
273 /// eraseFromParent - This method unlinks 'this' from the containing module
276 virtual void eraseFromParent();
279 /// Get the underlying elements of the Function... the basic block list is
280 /// empty for external functions.
282 const ArgumentListType &getArgumentList() const {
283 CheckLazyArguments();
286 ArgumentListType &getArgumentList() {
287 CheckLazyArguments();
291 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
292 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
294 const BasicBlock &getEntryBlock() const { return front(); }
295 BasicBlock &getEntryBlock() { return front(); }
297 //===--------------------------------------------------------------------===//
298 // Symbol Table Accessing functions...
300 /// getSymbolTable() - Return the symbol table...
302 inline ValueSymbolTable &getValueSymbolTable() { return *SymTab; }
303 inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
306 //===--------------------------------------------------------------------===//
307 // BasicBlock iterator forwarding functions
309 iterator begin() { return BasicBlocks.begin(); }
310 const_iterator begin() const { return BasicBlocks.begin(); }
311 iterator end () { return BasicBlocks.end(); }
312 const_iterator end () const { return BasicBlocks.end(); }
314 size_t size() const { return BasicBlocks.size(); }
315 bool empty() const { return BasicBlocks.empty(); }
316 const BasicBlock &front() const { return BasicBlocks.front(); }
317 BasicBlock &front() { return BasicBlocks.front(); }
318 const BasicBlock &back() const { return BasicBlocks.back(); }
319 BasicBlock &back() { return BasicBlocks.back(); }
321 //===--------------------------------------------------------------------===//
322 // Argument iterator forwarding functions
324 arg_iterator arg_begin() {
325 CheckLazyArguments();
326 return ArgumentList.begin();
328 const_arg_iterator arg_begin() const {
329 CheckLazyArguments();
330 return ArgumentList.begin();
332 arg_iterator arg_end() {
333 CheckLazyArguments();
334 return ArgumentList.end();
336 const_arg_iterator arg_end() const {
337 CheckLazyArguments();
338 return ArgumentList.end();
341 size_t arg_size() const;
342 bool arg_empty() const;
344 /// viewCFG - This function is meant for use from the debugger. You can just
345 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
346 /// program, displaying the CFG of the current function with the code for each
347 /// basic block inside. This depends on there being a 'dot' and 'gv' program
350 void viewCFG() const;
352 /// viewCFGOnly - This function is meant for use from the debugger. It works
353 /// just like viewCFG, but it does not include the contents of basic blocks
354 /// into the nodes, just the label. If you are only interested in the CFG
355 /// this can make the graph smaller.
357 void viewCFGOnly() const;
359 /// Methods for support type inquiry through isa, cast, and dyn_cast:
360 static inline bool classof(const Function *) { return true; }
361 static inline bool classof(const Value *V) {
362 return V->getValueID() == Value::FunctionVal;
365 /// dropAllReferences() - This method causes all the subinstructions to "let
366 /// go" of all references that they are maintaining. This allows one to
367 /// 'delete' a whole module at a time, even though there may be circular
368 /// references... first all references are dropped, and all use counts go to
369 /// zero. Then everything is deleted for real. Note that no operations are
370 /// valid on an object that has "dropped all references", except operator
373 /// Since no other object in the module can have references into the body of a
374 /// function, dropping all references deletes the entire body of the function,
375 /// including any contained basic blocks.
377 void dropAllReferences();
379 static unsigned getBasicBlockListOffset() {
381 return unsigned(reinterpret_cast<uintptr_t>(&Obj->BasicBlocks));
383 static unsigned getArgumentListOffset() {
385 return unsigned(reinterpret_cast<uintptr_t>(&Obj->ArgumentList));
389 inline ValueSymbolTable *
390 ilist_traits<BasicBlock>::getSymTab(Function *F) {
391 return F ? &F->getValueSymbolTable() : 0;
394 inline ValueSymbolTable *
395 ilist_traits<Argument>::getSymTab(Function *F) {
396 return F ? &F->getValueSymbolTable() : 0;
400 ilist_traits<BasicBlock>::getListOffset() {
401 return Function::getBasicBlockListOffset();
405 ilist_traits<Argument>::getListOffset() {
406 return Function::getArgumentListOffset();
410 } // End llvm namespace