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 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, public Annotable,
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 std::string &N = "", Module *M = 0);
119 static Function *Create(const FunctionType *Ty, LinkageTypes Linkage,
120 const std::string &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 /// isVarArg - Return true if this function takes a variable number of
131 bool isVarArg() const;
133 /// isDeclaration - Is the body of this function unknown? (The basic block
134 /// list is empty if so.) This is true for function declarations, but not
135 /// true for function definitions.
137 virtual bool isDeclaration() const { return BasicBlocks.empty(); }
139 /// getIntrinsicID - This method returns the ID number of the specified
140 /// function, or Intrinsic::not_intrinsic if the function is not an
141 /// instrinsic, or if the pointer is null. This value is always defined to be
142 /// zero to allow easy checking for whether a function is intrinsic or not.
143 /// The particular intrinsic functions which correspond to this value are
144 /// defined in llvm/Intrinsics.h.
146 unsigned getIntrinsicID() const;
147 bool isIntrinsic() const { return getIntrinsicID() != 0; }
149 /// getCallingConv()/setCallingConv(uint) - These method get and set the
150 /// calling convention of this function. The enum values for the known
151 /// calling conventions are defined in CallingConv.h.
152 unsigned getCallingConv() const { return SubclassData >> 1; }
153 void setCallingConv(unsigned CC) {
154 SubclassData = (SubclassData & 1) | (CC << 1);
157 /// getAttributes - Return the attribute list for this Function.
159 const AttrListPtr &getAttributes() const { return AttributeList; }
161 /// setAttributes - Set the attribute list for this Function.
163 void setAttributes(const AttrListPtr &attrs) { AttributeList = attrs; }
165 /// hasFnAttr - Return true if this function has the given attribute.
166 bool hasFnAttr(Attributes N) const {
167 // Function Attributes are stored at ~0 index
168 return AttributeList.paramHasAttr(~0U, N);
171 /// addFnAttr - Add function attributes to this function.
173 void addFnAttr(Attributes N) {
174 // Function Attributes are stored at ~0 index
175 addAttribute(~0U, N);
178 /// removeFnAttr - Remove function attributes from this function.
180 void removeFnAttr(Attributes N) {
181 // Function Attributes are stored at ~0 index
182 removeAttribute(~0U, N);
185 /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm
186 /// to use during code generation.
188 const char *getGC() const;
189 void setGC(const char *Str);
192 /// @brief Determine whether the function has the given attribute.
193 bool paramHasAttr(unsigned i, Attributes attr) const {
194 return AttributeList.paramHasAttr(i, attr);
197 /// addAttribute - adds the attribute to the list of attributes.
198 void addAttribute(unsigned i, Attributes attr);
200 /// removeAttribute - removes the attribute from the list of attributes.
201 void removeAttribute(unsigned i, Attributes attr);
203 /// @brief Extract the alignment for a call or parameter (0=unknown).
204 unsigned getParamAlignment(unsigned i) const {
205 return AttributeList.getParamAlignment(i);
208 /// @brief Determine if the function does not access memory.
209 bool doesNotAccessMemory() const {
210 return hasFnAttr(Attribute::ReadNone);
212 void setDoesNotAccessMemory(bool DoesNotAccessMemory = true) {
213 if (DoesNotAccessMemory) addFnAttr(Attribute::ReadNone);
214 else removeFnAttr(Attribute::ReadNone);
217 /// @brief Determine if the function does not access or only reads memory.
218 bool onlyReadsMemory() const {
219 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
221 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
222 if (OnlyReadsMemory) addFnAttr(Attribute::ReadOnly);
223 else removeFnAttr(Attribute::ReadOnly | Attribute::ReadNone);
226 /// @brief Determine if the function cannot return.
227 bool doesNotReturn() const {
228 return hasFnAttr(Attribute::NoReturn);
230 void setDoesNotReturn(bool DoesNotReturn = true) {
231 if (DoesNotReturn) addFnAttr(Attribute::NoReturn);
232 else removeFnAttr(Attribute::NoReturn);
235 /// @brief Determine if the function cannot unwind.
236 bool doesNotThrow() const {
237 return hasFnAttr(Attribute::NoUnwind);
239 void setDoesNotThrow(bool DoesNotThrow = true) {
240 if (DoesNotThrow) addFnAttr(Attribute::NoUnwind);
241 else removeFnAttr(Attribute::NoUnwind);
244 /// @brief Determine if the function returns a structure through first
245 /// pointer argument.
246 bool hasStructRetAttr() const {
247 return paramHasAttr(1, Attribute::StructRet);
250 /// @brief Determine if the parameter does not alias other parameters.
251 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
252 bool doesNotAlias(unsigned n) const {
253 return paramHasAttr(n, Attribute::NoAlias);
255 void setDoesNotAlias(unsigned n, bool DoesNotAlias = true) {
256 if (DoesNotAlias) addAttribute(n, Attribute::NoAlias);
257 else removeAttribute(n, Attribute::NoAlias);
260 /// @brief Determine if the parameter can be captured.
261 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
262 bool doesNotCapture(unsigned n) const {
263 return paramHasAttr(n, Attribute::NoCapture);
265 void setDoesNotCapture(unsigned n, bool DoesNotCapture = true) {
266 if (DoesNotCapture) addAttribute(n, Attribute::NoCapture);
267 else removeAttribute(n, Attribute::NoCapture);
270 /// copyAttributesFrom - copy all additional attributes (those not needed to
271 /// create a Function) from the Function Src to this one.
272 void copyAttributesFrom(const GlobalValue *Src);
274 /// deleteBody - This method deletes the body of the function, and converts
275 /// the linkage to external.
279 setLinkage(ExternalLinkage);
282 /// removeFromParent - This method unlinks 'this' from the containing module,
283 /// but does not delete it.
285 virtual void removeFromParent();
287 /// eraseFromParent - This method unlinks 'this' from the containing module
290 virtual void eraseFromParent();
293 /// Get the underlying elements of the Function... the basic block list is
294 /// empty for external functions.
296 const ArgumentListType &getArgumentList() const {
297 CheckLazyArguments();
300 ArgumentListType &getArgumentList() {
301 CheckLazyArguments();
304 static iplist<Argument> Function::*getSublistAccess(Argument*) {
305 return &Function::ArgumentList;
308 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
309 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
310 static iplist<BasicBlock> Function::*getSublistAccess(BasicBlock*) {
311 return &Function::BasicBlocks;
314 const BasicBlock &getEntryBlock() const { return front(); }
315 BasicBlock &getEntryBlock() { return front(); }
317 //===--------------------------------------------------------------------===//
318 // Symbol Table Accessing functions...
320 /// getSymbolTable() - Return the symbol table...
322 inline ValueSymbolTable &getValueSymbolTable() { return *SymTab; }
323 inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
326 //===--------------------------------------------------------------------===//
327 // BasicBlock iterator forwarding functions
329 iterator begin() { return BasicBlocks.begin(); }
330 const_iterator begin() const { return BasicBlocks.begin(); }
331 iterator end () { return BasicBlocks.end(); }
332 const_iterator end () const { return BasicBlocks.end(); }
334 size_t size() const { return BasicBlocks.size(); }
335 bool empty() const { return BasicBlocks.empty(); }
336 const BasicBlock &front() const { return BasicBlocks.front(); }
337 BasicBlock &front() { return BasicBlocks.front(); }
338 const BasicBlock &back() const { return BasicBlocks.back(); }
339 BasicBlock &back() { return BasicBlocks.back(); }
341 //===--------------------------------------------------------------------===//
342 // Argument iterator forwarding functions
344 arg_iterator arg_begin() {
345 CheckLazyArguments();
346 return ArgumentList.begin();
348 const_arg_iterator arg_begin() const {
349 CheckLazyArguments();
350 return ArgumentList.begin();
352 arg_iterator arg_end() {
353 CheckLazyArguments();
354 return ArgumentList.end();
356 const_arg_iterator arg_end() const {
357 CheckLazyArguments();
358 return ArgumentList.end();
361 size_t arg_size() const;
362 bool arg_empty() const;
364 /// viewCFG - This function is meant for use from the debugger. You can just
365 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
366 /// program, displaying the CFG of the current function with the code for each
367 /// basic block inside. This depends on there being a 'dot' and 'gv' program
370 void viewCFG() const;
372 /// viewCFGOnly - This function is meant for use from the debugger. It works
373 /// just like viewCFG, but it does not include the contents of basic blocks
374 /// into the nodes, just the label. If you are only interested in the CFG
375 /// this can make the graph smaller.
377 void viewCFGOnly() const;
379 /// Methods for support type inquiry through isa, cast, and dyn_cast:
380 static inline bool classof(const Function *) { return true; }
381 static inline bool classof(const Value *V) {
382 return V->getValueID() == Value::FunctionVal;
385 /// dropAllReferences() - This method causes all the subinstructions to "let
386 /// go" of all references that they are maintaining. This allows one to
387 /// 'delete' a whole module at a time, even though there may be circular
388 /// references... first all references are dropped, and all use counts go to
389 /// zero. Then everything is deleted for real. Note that no operations are
390 /// valid on an object that has "dropped all references", except operator
393 /// Since no other object in the module can have references into the body of a
394 /// function, dropping all references deletes the entire body of the function,
395 /// including any contained basic blocks.
397 void dropAllReferences();
399 /// hasAddressTaken - returns true if there are any uses of this function
400 /// other than direct calls or invokes to it.
401 bool hasAddressTaken() const;
404 inline ValueSymbolTable *
405 ilist_traits<BasicBlock>::getSymTab(Function *F) {
406 return F ? &F->getValueSymbolTable() : 0;
409 inline ValueSymbolTable *
410 ilist_traits<Argument>::getSymTab(Function *F) {
411 return F ? &F->getValueSymbolTable() : 0;
414 } // End llvm namespace