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/Attributes.h"
31 // Traits for intrusive list of basic blocks...
32 template<> struct ilist_traits<BasicBlock>
33 : public SymbolTableListTraits<BasicBlock, Function> {
35 // createSentinel is used to get hold of the node that marks the end of the
36 // list... (same trick used here as in ilist_traits<Instruction>)
37 BasicBlock *createSentinel() const {
38 return static_cast<BasicBlock*>(&Sentinel);
40 static void destroySentinel(BasicBlock*) {}
42 BasicBlock *provideInitialHead() const { return createSentinel(); }
43 BasicBlock *ensureHead(BasicBlock*) const { return createSentinel(); }
44 static void noteHead(BasicBlock*, BasicBlock*) {}
46 static ValueSymbolTable *getSymTab(Function *ItemParent);
48 mutable ilist_node<BasicBlock> Sentinel;
51 template<> struct ilist_traits<Argument>
52 : public SymbolTableListTraits<Argument, Function> {
54 Argument *createSentinel() const {
55 return static_cast<Argument*>(&Sentinel);
57 static void destroySentinel(Argument*) {}
59 Argument *provideInitialHead() const { return createSentinel(); }
60 Argument *ensureHead(Argument*) const { return createSentinel(); }
61 static void noteHead(Argument*, Argument*) {}
63 static ValueSymbolTable *getSymTab(Function *ItemParent);
65 mutable ilist_node<Argument> Sentinel;
68 class Function : public GlobalValue,
69 public ilist_node<Function> {
71 typedef iplist<Argument> ArgumentListType;
72 typedef iplist<BasicBlock> BasicBlockListType;
74 // BasicBlock iterators...
75 typedef BasicBlockListType::iterator iterator;
76 typedef BasicBlockListType::const_iterator const_iterator;
78 typedef ArgumentListType::iterator arg_iterator;
79 typedef ArgumentListType::const_iterator const_arg_iterator;
82 // Important things that make up a function!
83 BasicBlockListType BasicBlocks; ///< The basic blocks
84 mutable ArgumentListType ArgumentList; ///< The formal arguments
85 ValueSymbolTable *SymTab; ///< Symbol table of args/instructions
86 AttrListPtr AttributeList; ///< Parameter attributes
88 // The Calling Convention is stored in Value::SubclassData.
89 /*unsigned CallingConvention;*/
91 friend class SymbolTableListTraits<Function, Module>;
93 void setParent(Module *parent);
95 /// hasLazyArguments/CheckLazyArguments - The argument list of a function is
96 /// built on demand, so that the list isn't allocated until the first client
97 /// needs it. The hasLazyArguments predicate returns true if the arg list
98 /// hasn't been set up yet.
99 bool hasLazyArguments() const {
100 return SubclassData & 1;
102 void CheckLazyArguments() const {
103 if (hasLazyArguments())
104 BuildLazyArguments();
106 void BuildLazyArguments() const;
108 Function(const Function&); // DO NOT IMPLEMENT
109 void operator=(const Function&); // DO NOT IMPLEMENT
111 /// Function ctor - If the (optional) Module argument is specified, the
112 /// function is automatically inserted into the end of the function list for
115 Function(const FunctionType *Ty, LinkageTypes Linkage,
116 const Twine &N = "", Module *M = 0);
119 static Function *Create(const FunctionType *Ty, LinkageTypes Linkage,
120 const Twine &N = "", Module *M = 0) {
121 return new(0) Function(Ty, Linkage, N, M);
126 const Type *getReturnType() const; // Return the type of the ret val
127 const FunctionType *getFunctionType() const; // Return the FunctionType for me
129 /// getContext - Return a pointer to the LLVMContext associated with this
130 /// function, or NULL if this function is not bound to a context yet.
131 LLVMContext &getContext() const;
133 /// isVarArg - Return true if this function takes a variable number of
135 bool isVarArg() const;
137 /// isDeclaration - Is the body of this function unknown? (The basic block
138 /// list is empty if so.) This is true for function declarations, but not
139 /// true for function definitions.
141 virtual bool isDeclaration() const { return BasicBlocks.empty(); }
143 /// getIntrinsicID - This method returns the ID number of the specified
144 /// function, or Intrinsic::not_intrinsic if the function is not an
145 /// instrinsic, or if the pointer is null. This value is always defined to be
146 /// zero to allow easy checking for whether a function is intrinsic or not.
147 /// The particular intrinsic functions which correspond to this value are
148 /// defined in llvm/Intrinsics.h.
150 unsigned getIntrinsicID() const;
151 bool isIntrinsic() const { return getIntrinsicID() != 0; }
153 /// getCallingConv()/setCallingConv(uint) - These method get and set the
154 /// calling convention of this function. The enum values for the known
155 /// calling conventions are defined in CallingConv.h.
156 unsigned getCallingConv() const { return SubclassData >> 1; }
157 void setCallingConv(unsigned CC) {
158 SubclassData = (SubclassData & 1) | (CC << 1);
161 /// getAttributes - Return the attribute list for this Function.
163 const AttrListPtr &getAttributes() const { return AttributeList; }
165 /// setAttributes - Set the attribute list for this Function.
167 void setAttributes(const AttrListPtr &attrs) { AttributeList = attrs; }
169 /// hasFnAttr - Return true if this function has the given attribute.
170 bool hasFnAttr(Attributes N) const {
171 // Function Attributes are stored at ~0 index
172 return AttributeList.paramHasAttr(~0U, N);
175 /// addFnAttr - Add function attributes to this function.
177 void addFnAttr(Attributes N) {
178 // Function Attributes are stored at ~0 index
179 addAttribute(~0U, N);
182 /// removeFnAttr - Remove function attributes from this function.
184 void removeFnAttr(Attributes N) {
185 // Function Attributes are stored at ~0 index
186 removeAttribute(~0U, N);
189 /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm
190 /// to use during code generation.
192 const char *getGC() const;
193 void setGC(const char *Str);
196 /// @brief Determine whether the function has the given attribute.
197 bool paramHasAttr(unsigned i, Attributes attr) const {
198 return AttributeList.paramHasAttr(i, attr);
201 /// addAttribute - adds the attribute to the list of attributes.
202 void addAttribute(unsigned i, Attributes attr);
204 /// removeAttribute - removes the attribute from the list of attributes.
205 void removeAttribute(unsigned i, Attributes attr);
207 /// @brief Extract the alignment for a call or parameter (0=unknown).
208 unsigned getParamAlignment(unsigned i) const {
209 return AttributeList.getParamAlignment(i);
212 /// @brief Determine if the function does not access memory.
213 bool doesNotAccessMemory() const {
214 return hasFnAttr(Attribute::ReadNone);
216 void setDoesNotAccessMemory(bool DoesNotAccessMemory = true) {
217 if (DoesNotAccessMemory) addFnAttr(Attribute::ReadNone);
218 else removeFnAttr(Attribute::ReadNone);
221 /// @brief Determine if the function does not access or only reads memory.
222 bool onlyReadsMemory() const {
223 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
225 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
226 if (OnlyReadsMemory) addFnAttr(Attribute::ReadOnly);
227 else removeFnAttr(Attribute::ReadOnly | Attribute::ReadNone);
230 /// @brief Determine if the function cannot return.
231 bool doesNotReturn() const {
232 return hasFnAttr(Attribute::NoReturn);
234 void setDoesNotReturn(bool DoesNotReturn = true) {
235 if (DoesNotReturn) addFnAttr(Attribute::NoReturn);
236 else removeFnAttr(Attribute::NoReturn);
239 /// @brief Determine if the function cannot unwind.
240 bool doesNotThrow() const {
241 return hasFnAttr(Attribute::NoUnwind);
243 void setDoesNotThrow(bool DoesNotThrow = true) {
244 if (DoesNotThrow) addFnAttr(Attribute::NoUnwind);
245 else removeFnAttr(Attribute::NoUnwind);
248 /// @brief Determine if the function returns a structure through first
249 /// pointer argument.
250 bool hasStructRetAttr() const {
251 return paramHasAttr(1, Attribute::StructRet);
254 /// @brief Determine if the parameter does not alias other parameters.
255 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
256 bool doesNotAlias(unsigned n) const {
257 return paramHasAttr(n, Attribute::NoAlias);
259 void setDoesNotAlias(unsigned n, bool DoesNotAlias = true) {
260 if (DoesNotAlias) addAttribute(n, Attribute::NoAlias);
261 else removeAttribute(n, Attribute::NoAlias);
264 /// @brief Determine if the parameter can be captured.
265 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
266 bool doesNotCapture(unsigned n) const {
267 return paramHasAttr(n, Attribute::NoCapture);
269 void setDoesNotCapture(unsigned n, bool DoesNotCapture = true) {
270 if (DoesNotCapture) addAttribute(n, Attribute::NoCapture);
271 else removeAttribute(n, Attribute::NoCapture);
274 /// copyAttributesFrom - copy all additional attributes (those not needed to
275 /// create a Function) from the Function Src to this one.
276 void copyAttributesFrom(const GlobalValue *Src);
278 /// deleteBody - This method deletes the body of the function, and converts
279 /// the linkage to external.
283 setLinkage(ExternalLinkage);
286 /// removeFromParent - This method unlinks 'this' from the containing module,
287 /// but does not delete it.
289 virtual void removeFromParent();
291 /// eraseFromParent - This method unlinks 'this' from the containing module
294 virtual void eraseFromParent();
297 /// Get the underlying elements of the Function... the basic block list is
298 /// empty for external functions.
300 const ArgumentListType &getArgumentList() const {
301 CheckLazyArguments();
304 ArgumentListType &getArgumentList() {
305 CheckLazyArguments();
308 static iplist<Argument> Function::*getSublistAccess(Argument*) {
309 return &Function::ArgumentList;
312 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
313 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
314 static iplist<BasicBlock> Function::*getSublistAccess(BasicBlock*) {
315 return &Function::BasicBlocks;
318 const BasicBlock &getEntryBlock() const { return front(); }
319 BasicBlock &getEntryBlock() { return front(); }
321 //===--------------------------------------------------------------------===//
322 // Symbol Table Accessing functions...
324 /// getSymbolTable() - Return the symbol table...
326 inline ValueSymbolTable &getValueSymbolTable() { return *SymTab; }
327 inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
330 //===--------------------------------------------------------------------===//
331 // BasicBlock iterator forwarding functions
333 iterator begin() { return BasicBlocks.begin(); }
334 const_iterator begin() const { return BasicBlocks.begin(); }
335 iterator end () { return BasicBlocks.end(); }
336 const_iterator end () const { return BasicBlocks.end(); }
338 size_t size() const { return BasicBlocks.size(); }
339 bool empty() const { return BasicBlocks.empty(); }
340 const BasicBlock &front() const { return BasicBlocks.front(); }
341 BasicBlock &front() { return BasicBlocks.front(); }
342 const BasicBlock &back() const { return BasicBlocks.back(); }
343 BasicBlock &back() { return BasicBlocks.back(); }
345 //===--------------------------------------------------------------------===//
346 // Argument iterator forwarding functions
348 arg_iterator arg_begin() {
349 CheckLazyArguments();
350 return ArgumentList.begin();
352 const_arg_iterator arg_begin() const {
353 CheckLazyArguments();
354 return ArgumentList.begin();
356 arg_iterator arg_end() {
357 CheckLazyArguments();
358 return ArgumentList.end();
360 const_arg_iterator arg_end() const {
361 CheckLazyArguments();
362 return ArgumentList.end();
365 size_t arg_size() const;
366 bool arg_empty() const;
368 /// viewCFG - This function is meant for use from the debugger. You can just
369 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
370 /// program, displaying the CFG of the current function with the code for each
371 /// basic block inside. This depends on there being a 'dot' and 'gv' program
374 void viewCFG() const;
376 /// viewCFGOnly - This function is meant for use from the debugger. It works
377 /// just like viewCFG, but it does not include the contents of basic blocks
378 /// into the nodes, just the label. If you are only interested in the CFG
379 /// this can make the graph smaller.
381 void viewCFGOnly() const;
383 /// Methods for support type inquiry through isa, cast, and dyn_cast:
384 static inline bool classof(const Function *) { return true; }
385 static inline bool classof(const Value *V) {
386 return V->getValueID() == Value::FunctionVal;
389 /// dropAllReferences() - This method causes all the subinstructions to "let
390 /// go" of all references that they are maintaining. This allows one to
391 /// 'delete' a whole module at a time, even though there may be circular
392 /// references... first all references are dropped, and all use counts go to
393 /// zero. Then everything is deleted for real. Note that no operations are
394 /// valid on an object that has "dropped all references", except operator
397 /// Since no other object in the module can have references into the body of a
398 /// function, dropping all references deletes the entire body of the function,
399 /// including any contained basic blocks.
401 void dropAllReferences();
403 /// hasAddressTaken - returns true if there are any uses of this function
404 /// other than direct calls or invokes to it.
405 bool hasAddressTaken() const;
408 inline ValueSymbolTable *
409 ilist_traits<BasicBlock>::getSymTab(Function *F) {
410 return F ? &F->getValueSymbolTable() : 0;
413 inline ValueSymbolTable *
414 ilist_traits<Argument>::getSymTab(Function *F) {
415 return F ? &F->getValueSymbolTable() : 0;
418 } // End llvm namespace