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"
32 // Traits for intrusive list of basic blocks...
33 template<> struct ilist_traits<BasicBlock>
34 : public SymbolTableListTraits<BasicBlock, Function> {
36 // createSentinel is used to get hold of the node that marks the end of the
37 // list... (same trick used here as in ilist_traits<Instruction>)
38 BasicBlock *createSentinel() const {
39 return static_cast<BasicBlock*>(&Sentinel);
41 static void destroySentinel(BasicBlock*) {}
43 BasicBlock *provideInitialHead() const { return createSentinel(); }
44 BasicBlock *ensureHead(BasicBlock*) const { return createSentinel(); }
45 static void noteHead(BasicBlock*, BasicBlock*) {}
47 static ValueSymbolTable *getSymTab(Function *ItemParent);
49 mutable ilist_node<BasicBlock> Sentinel;
52 template<> struct ilist_traits<Argument>
53 : public SymbolTableListTraits<Argument, Function> {
55 Argument *createSentinel() const {
56 return static_cast<Argument*>(&Sentinel);
58 static void destroySentinel(Argument*) {}
60 Argument *provideInitialHead() const { return createSentinel(); }
61 Argument *ensureHead(Argument*) const { return createSentinel(); }
62 static void noteHead(Argument*, Argument*) {}
64 static ValueSymbolTable *getSymTab(Function *ItemParent);
66 mutable ilist_node<Argument> Sentinel;
69 class Function : public GlobalValue, public Annotable,
70 public ilist_node<Function> {
72 typedef iplist<Argument> ArgumentListType;
73 typedef iplist<BasicBlock> BasicBlockListType;
75 // BasicBlock iterators...
76 typedef BasicBlockListType::iterator iterator;
77 typedef BasicBlockListType::const_iterator const_iterator;
79 typedef ArgumentListType::iterator arg_iterator;
80 typedef ArgumentListType::const_iterator const_arg_iterator;
83 // Important things that make up a function!
84 BasicBlockListType BasicBlocks; ///< The basic blocks
85 mutable ArgumentListType ArgumentList; ///< The formal arguments
86 ValueSymbolTable *SymTab; ///< Symbol table of args/instructions
87 AttrListPtr AttributeList; ///< Parameter attributes
89 // The Calling Convention is stored in Value::SubclassData.
90 /*unsigned CallingConvention;*/
92 friend class SymbolTableListTraits<Function, Module>;
94 void setParent(Module *parent);
96 /// hasLazyArguments/CheckLazyArguments - The argument list of a function is
97 /// built on demand, so that the list isn't allocated until the first client
98 /// needs it. The hasLazyArguments predicate returns true if the arg list
99 /// hasn't been set up yet.
100 bool hasLazyArguments() const {
101 return SubclassData & 1;
103 void CheckLazyArguments() const {
104 if (hasLazyArguments())
105 BuildLazyArguments();
107 void BuildLazyArguments() const;
109 Function(const Function&); // DO NOT IMPLEMENT
110 void operator=(const Function&); // DO NOT IMPLEMENT
112 /// Function ctor - If the (optional) Module argument is specified, the
113 /// function is automatically inserted into the end of the function list for
116 Function(const FunctionType *Ty, LinkageTypes Linkage,
117 const std::string &N = "", Module *M = 0);
120 static Function *Create(const FunctionType *Ty, LinkageTypes Linkage,
121 const std::string &N = "", Module *M = 0) {
122 return new(0) Function(Ty, Linkage, N, M);
127 const Type *getReturnType() const; // Return the type of the ret val
128 const FunctionType *getFunctionType() const; // Return the FunctionType for me
130 /// getContext - Return a pointer to the LLVMContext associated with this
131 /// function, or NULL if this function is not bound to a context yet.
132 LLVMContext* getContext();
134 /// isVarArg - Return true if this function takes a variable number of
136 bool isVarArg() const;
138 /// isDeclaration - Is the body of this function unknown? (The basic block
139 /// list is empty if so.) This is true for function declarations, but not
140 /// true for function definitions.
142 virtual bool isDeclaration() const { return BasicBlocks.empty(); }
144 /// getIntrinsicID - This method returns the ID number of the specified
145 /// function, or Intrinsic::not_intrinsic if the function is not an
146 /// instrinsic, or if the pointer is null. This value is always defined to be
147 /// zero to allow easy checking for whether a function is intrinsic or not.
148 /// The particular intrinsic functions which correspond to this value are
149 /// defined in llvm/Intrinsics.h.
151 unsigned getIntrinsicID() const;
152 bool isIntrinsic() const { return getIntrinsicID() != 0; }
154 /// getCallingConv()/setCallingConv(uint) - These method get and set the
155 /// calling convention of this function. The enum values for the known
156 /// calling conventions are defined in CallingConv.h.
157 unsigned getCallingConv() const { return SubclassData >> 1; }
158 void setCallingConv(unsigned CC) {
159 SubclassData = (SubclassData & 1) | (CC << 1);
162 /// getAttributes - Return the attribute list for this Function.
164 const AttrListPtr &getAttributes() const { return AttributeList; }
166 /// setAttributes - Set the attribute list for this Function.
168 void setAttributes(const AttrListPtr &attrs) { AttributeList = attrs; }
170 /// hasFnAttr - Return true if this function has the given attribute.
171 bool hasFnAttr(Attributes N) const {
172 // Function Attributes are stored at ~0 index
173 return AttributeList.paramHasAttr(~0U, N);
176 /// addFnAttr - Add function attributes to this function.
178 void addFnAttr(Attributes N) {
179 // Function Attributes are stored at ~0 index
180 addAttribute(~0U, N);
183 /// removeFnAttr - Remove function attributes from this function.
185 void removeFnAttr(Attributes N) {
186 // Function Attributes are stored at ~0 index
187 removeAttribute(~0U, N);
190 /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm
191 /// to use during code generation.
193 const char *getGC() const;
194 void setGC(const char *Str);
197 /// @brief Determine whether the function has the given attribute.
198 bool paramHasAttr(unsigned i, Attributes attr) const {
199 return AttributeList.paramHasAttr(i, attr);
202 /// addAttribute - adds the attribute to the list of attributes.
203 void addAttribute(unsigned i, Attributes attr);
205 /// removeAttribute - removes the attribute from the list of attributes.
206 void removeAttribute(unsigned i, Attributes attr);
208 /// @brief Extract the alignment for a call or parameter (0=unknown).
209 unsigned getParamAlignment(unsigned i) const {
210 return AttributeList.getParamAlignment(i);
213 /// @brief Determine if the function does not access memory.
214 bool doesNotAccessMemory() const {
215 return hasFnAttr(Attribute::ReadNone);
217 void setDoesNotAccessMemory(bool DoesNotAccessMemory = true) {
218 if (DoesNotAccessMemory) addFnAttr(Attribute::ReadNone);
219 else removeFnAttr(Attribute::ReadNone);
222 /// @brief Determine if the function does not access or only reads memory.
223 bool onlyReadsMemory() const {
224 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
226 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
227 if (OnlyReadsMemory) addFnAttr(Attribute::ReadOnly);
228 else removeFnAttr(Attribute::ReadOnly | Attribute::ReadNone);
231 /// @brief Determine if the function cannot return.
232 bool doesNotReturn() const {
233 return hasFnAttr(Attribute::NoReturn);
235 void setDoesNotReturn(bool DoesNotReturn = true) {
236 if (DoesNotReturn) addFnAttr(Attribute::NoReturn);
237 else removeFnAttr(Attribute::NoReturn);
240 /// @brief Determine if the function cannot unwind.
241 bool doesNotThrow() const {
242 return hasFnAttr(Attribute::NoUnwind);
244 void setDoesNotThrow(bool DoesNotThrow = true) {
245 if (DoesNotThrow) addFnAttr(Attribute::NoUnwind);
246 else removeFnAttr(Attribute::NoUnwind);
249 /// @brief Determine if the function returns a structure through first
250 /// pointer argument.
251 bool hasStructRetAttr() const {
252 return paramHasAttr(1, Attribute::StructRet);
255 /// @brief Determine if the parameter does not alias other parameters.
256 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
257 bool doesNotAlias(unsigned n) const {
258 return paramHasAttr(n, Attribute::NoAlias);
260 void setDoesNotAlias(unsigned n, bool DoesNotAlias = true) {
261 if (DoesNotAlias) addAttribute(n, Attribute::NoAlias);
262 else removeAttribute(n, Attribute::NoAlias);
265 /// @brief Determine if the parameter can be captured.
266 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
267 bool doesNotCapture(unsigned n) const {
268 return paramHasAttr(n, Attribute::NoCapture);
270 void setDoesNotCapture(unsigned n, bool DoesNotCapture = true) {
271 if (DoesNotCapture) addAttribute(n, Attribute::NoCapture);
272 else removeAttribute(n, Attribute::NoCapture);
275 /// copyAttributesFrom - copy all additional attributes (those not needed to
276 /// create a Function) from the Function Src to this one.
277 void copyAttributesFrom(const GlobalValue *Src);
279 /// deleteBody - This method deletes the body of the function, and converts
280 /// the linkage to external.
284 setLinkage(ExternalLinkage);
287 /// removeFromParent - This method unlinks 'this' from the containing module,
288 /// but does not delete it.
290 virtual void removeFromParent();
292 /// eraseFromParent - This method unlinks 'this' from the containing module
295 virtual void eraseFromParent();
298 /// Get the underlying elements of the Function... the basic block list is
299 /// empty for external functions.
301 const ArgumentListType &getArgumentList() const {
302 CheckLazyArguments();
305 ArgumentListType &getArgumentList() {
306 CheckLazyArguments();
309 static iplist<Argument> Function::*getSublistAccess(Argument*) {
310 return &Function::ArgumentList;
313 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
314 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
315 static iplist<BasicBlock> Function::*getSublistAccess(BasicBlock*) {
316 return &Function::BasicBlocks;
319 const BasicBlock &getEntryBlock() const { return front(); }
320 BasicBlock &getEntryBlock() { return front(); }
322 //===--------------------------------------------------------------------===//
323 // Symbol Table Accessing functions...
325 /// getSymbolTable() - Return the symbol table...
327 inline ValueSymbolTable &getValueSymbolTable() { return *SymTab; }
328 inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
331 //===--------------------------------------------------------------------===//
332 // BasicBlock iterator forwarding functions
334 iterator begin() { return BasicBlocks.begin(); }
335 const_iterator begin() const { return BasicBlocks.begin(); }
336 iterator end () { return BasicBlocks.end(); }
337 const_iterator end () const { return BasicBlocks.end(); }
339 size_t size() const { return BasicBlocks.size(); }
340 bool empty() const { return BasicBlocks.empty(); }
341 const BasicBlock &front() const { return BasicBlocks.front(); }
342 BasicBlock &front() { return BasicBlocks.front(); }
343 const BasicBlock &back() const { return BasicBlocks.back(); }
344 BasicBlock &back() { return BasicBlocks.back(); }
346 //===--------------------------------------------------------------------===//
347 // Argument iterator forwarding functions
349 arg_iterator arg_begin() {
350 CheckLazyArguments();
351 return ArgumentList.begin();
353 const_arg_iterator arg_begin() const {
354 CheckLazyArguments();
355 return ArgumentList.begin();
357 arg_iterator arg_end() {
358 CheckLazyArguments();
359 return ArgumentList.end();
361 const_arg_iterator arg_end() const {
362 CheckLazyArguments();
363 return ArgumentList.end();
366 size_t arg_size() const;
367 bool arg_empty() const;
369 /// viewCFG - This function is meant for use from the debugger. You can just
370 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
371 /// program, displaying the CFG of the current function with the code for each
372 /// basic block inside. This depends on there being a 'dot' and 'gv' program
375 void viewCFG() const;
377 /// viewCFGOnly - This function is meant for use from the debugger. It works
378 /// just like viewCFG, but it does not include the contents of basic blocks
379 /// into the nodes, just the label. If you are only interested in the CFG
380 /// this can make the graph smaller.
382 void viewCFGOnly() const;
384 /// Methods for support type inquiry through isa, cast, and dyn_cast:
385 static inline bool classof(const Function *) { return true; }
386 static inline bool classof(const Value *V) {
387 return V->getValueID() == Value::FunctionVal;
390 /// dropAllReferences() - This method causes all the subinstructions to "let
391 /// go" of all references that they are maintaining. This allows one to
392 /// 'delete' a whole module at a time, even though there may be circular
393 /// references... first all references are dropped, and all use counts go to
394 /// zero. Then everything is deleted for real. Note that no operations are
395 /// valid on an object that has "dropped all references", except operator
398 /// Since no other object in the module can have references into the body of a
399 /// function, dropping all references deletes the entire body of the function,
400 /// including any contained basic blocks.
402 void dropAllReferences();
404 /// hasAddressTaken - returns true if there are any uses of this function
405 /// other than direct calls or invokes to it.
406 bool hasAddressTaken() const;
409 inline ValueSymbolTable *
410 ilist_traits<BasicBlock>::getSymTab(Function *F) {
411 return F ? &F->getValueSymbolTable() : 0;
414 inline ValueSymbolTable *
415 ilist_traits<Argument>::getSymTab(Function *F) {
416 return F ? &F->getValueSymbolTable() : 0;
419 } // End llvm namespace