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/CallingConv.h"
23 #include "llvm/BasicBlock.h"
24 #include "llvm/Argument.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_half_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_half_node<Argument> Sentinel;
69 class Function : public GlobalValue,
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 /*CallingConv::ID 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 Twine &N = "", Module *M = 0);
120 static Function *Create(const FunctionType *Ty, LinkageTypes Linkage,
121 const Twine &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() const;
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(CC) - 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 CallingConv::ID getCallingConv() const {
158 return static_cast<CallingConv::ID>(SubclassData >> 1);
160 void setCallingConv(CallingConv::ID CC) {
161 SubclassData = (SubclassData & 1) | (static_cast<unsigned>(CC) << 1);
164 /// getAttributes - Return the attribute list for this Function.
166 const AttrListPtr &getAttributes() const { return AttributeList; }
168 /// setAttributes - Set the attribute list for this Function.
170 void setAttributes(const AttrListPtr &attrs) { AttributeList = attrs; }
172 /// hasFnAttr - Return true if this function has the given attribute.
173 bool hasFnAttr(Attributes N) const {
174 // Function Attributes are stored at ~0 index
175 return AttributeList.paramHasAttr(~0U, N);
178 /// addFnAttr - Add function attributes to this function.
180 void addFnAttr(Attributes N) {
181 // Function Attributes are stored at ~0 index
182 addAttribute(~0U, N);
185 /// removeFnAttr - Remove function attributes from this function.
187 void removeFnAttr(Attributes N) {
188 // Function Attributes are stored at ~0 index
189 removeAttribute(~0U, N);
192 /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm
193 /// to use during code generation.
195 const char *getGC() const;
196 void setGC(const char *Str);
199 /// @brief Determine whether the function has the given attribute.
200 bool paramHasAttr(unsigned i, Attributes attr) const {
201 return AttributeList.paramHasAttr(i, attr);
204 /// addAttribute - adds the attribute to the list of attributes.
205 void addAttribute(unsigned i, Attributes attr);
207 /// removeAttribute - removes the attribute from the list of attributes.
208 void removeAttribute(unsigned i, Attributes attr);
210 /// @brief Extract the alignment for a call or parameter (0=unknown).
211 unsigned getParamAlignment(unsigned i) const {
212 return AttributeList.getParamAlignment(i);
215 /// @brief Determine if the function does not access memory.
216 bool doesNotAccessMemory() const {
217 return hasFnAttr(Attribute::ReadNone);
219 void setDoesNotAccessMemory(bool DoesNotAccessMemory = true) {
220 if (DoesNotAccessMemory) addFnAttr(Attribute::ReadNone);
221 else removeFnAttr(Attribute::ReadNone);
224 /// @brief Determine if the function does not access or only reads memory.
225 bool onlyReadsMemory() const {
226 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
228 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
229 if (OnlyReadsMemory) addFnAttr(Attribute::ReadOnly);
230 else removeFnAttr(Attribute::ReadOnly | Attribute::ReadNone);
233 /// @brief Determine if the function cannot return.
234 bool doesNotReturn() const {
235 return hasFnAttr(Attribute::NoReturn);
237 void setDoesNotReturn(bool DoesNotReturn = true) {
238 if (DoesNotReturn) addFnAttr(Attribute::NoReturn);
239 else removeFnAttr(Attribute::NoReturn);
242 /// @brief Determine if the function cannot unwind.
243 bool doesNotThrow() const {
244 return hasFnAttr(Attribute::NoUnwind);
246 void setDoesNotThrow(bool DoesNotThrow = true) {
247 if (DoesNotThrow) addFnAttr(Attribute::NoUnwind);
248 else removeFnAttr(Attribute::NoUnwind);
251 /// @brief Determine if the function returns a structure through first
252 /// pointer argument.
253 bool hasStructRetAttr() const {
254 return paramHasAttr(1, Attribute::StructRet);
257 /// @brief Determine if the parameter does not alias other parameters.
258 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
259 bool doesNotAlias(unsigned n) const {
260 return paramHasAttr(n, Attribute::NoAlias);
262 void setDoesNotAlias(unsigned n, bool DoesNotAlias = true) {
263 if (DoesNotAlias) addAttribute(n, Attribute::NoAlias);
264 else removeAttribute(n, Attribute::NoAlias);
267 /// @brief Determine if the parameter can be captured.
268 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
269 bool doesNotCapture(unsigned n) const {
270 return paramHasAttr(n, Attribute::NoCapture);
272 void setDoesNotCapture(unsigned n, bool DoesNotCapture = true) {
273 if (DoesNotCapture) addAttribute(n, Attribute::NoCapture);
274 else removeAttribute(n, Attribute::NoCapture);
277 /// copyAttributesFrom - copy all additional attributes (those not needed to
278 /// create a Function) from the Function Src to this one.
279 void copyAttributesFrom(const GlobalValue *Src);
281 /// deleteBody - This method deletes the body of the function, and converts
282 /// the linkage to external.
286 setLinkage(ExternalLinkage);
289 /// removeFromParent - This method unlinks 'this' from the containing module,
290 /// but does not delete it.
292 virtual void removeFromParent();
294 /// eraseFromParent - This method unlinks 'this' from the containing module
297 virtual void eraseFromParent();
300 /// Get the underlying elements of the Function... the basic block list is
301 /// empty for external functions.
303 const ArgumentListType &getArgumentList() const {
304 CheckLazyArguments();
307 ArgumentListType &getArgumentList() {
308 CheckLazyArguments();
311 static iplist<Argument> Function::*getSublistAccess(Argument*) {
312 return &Function::ArgumentList;
315 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
316 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
317 static iplist<BasicBlock> Function::*getSublistAccess(BasicBlock*) {
318 return &Function::BasicBlocks;
321 const BasicBlock &getEntryBlock() const { return front(); }
322 BasicBlock &getEntryBlock() { return front(); }
324 //===--------------------------------------------------------------------===//
325 // Symbol Table Accessing functions...
327 /// getSymbolTable() - Return the symbol table...
329 inline ValueSymbolTable &getValueSymbolTable() { return *SymTab; }
330 inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
333 //===--------------------------------------------------------------------===//
334 // BasicBlock iterator forwarding functions
336 iterator begin() { return BasicBlocks.begin(); }
337 const_iterator begin() const { return BasicBlocks.begin(); }
338 iterator end () { return BasicBlocks.end(); }
339 const_iterator end () const { return BasicBlocks.end(); }
341 size_t size() const { return BasicBlocks.size(); }
342 bool empty() const { return BasicBlocks.empty(); }
343 const BasicBlock &front() const { return BasicBlocks.front(); }
344 BasicBlock &front() { return BasicBlocks.front(); }
345 const BasicBlock &back() const { return BasicBlocks.back(); }
346 BasicBlock &back() { return BasicBlocks.back(); }
348 //===--------------------------------------------------------------------===//
349 // Argument iterator forwarding functions
351 arg_iterator arg_begin() {
352 CheckLazyArguments();
353 return ArgumentList.begin();
355 const_arg_iterator arg_begin() const {
356 CheckLazyArguments();
357 return ArgumentList.begin();
359 arg_iterator arg_end() {
360 CheckLazyArguments();
361 return ArgumentList.end();
363 const_arg_iterator arg_end() const {
364 CheckLazyArguments();
365 return ArgumentList.end();
368 size_t arg_size() const;
369 bool arg_empty() const;
371 /// viewCFG - This function is meant for use from the debugger. You can just
372 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
373 /// program, displaying the CFG of the current function with the code for each
374 /// basic block inside. This depends on there being a 'dot' and 'gv' program
377 void viewCFG() const;
379 /// viewCFGOnly - This function is meant for use from the debugger. It works
380 /// just like viewCFG, but it does not include the contents of basic blocks
381 /// into the nodes, just the label. If you are only interested in the CFG
382 /// this can make the graph smaller.
384 void viewCFGOnly() const;
386 /// Methods for support type inquiry through isa, cast, and dyn_cast:
387 static inline bool classof(const Function *) { return true; }
388 static inline bool classof(const Value *V) {
389 return V->getValueID() == Value::FunctionVal;
392 /// dropAllReferences() - This method causes all the subinstructions to "let
393 /// go" of all references that they are maintaining. This allows one to
394 /// 'delete' a whole module at a time, even though there may be circular
395 /// references... first all references are dropped, and all use counts go to
396 /// zero. Then everything is deleted for real. Note that no operations are
397 /// valid on an object that has "dropped all references", except operator
400 /// Since no other object in the module can have references into the body of a
401 /// function, dropping all references deletes the entire body of the function,
402 /// including any contained basic blocks.
404 void dropAllReferences();
406 /// hasAddressTaken - returns true if there are any uses of this function
407 /// other than direct calls or invokes to it.
408 bool hasAddressTaken() const;
411 inline ValueSymbolTable *
412 ilist_traits<BasicBlock>::getSymTab(Function *F) {
413 return F ? &F->getValueSymbolTable() : 0;
416 inline ValueSymbolTable *
417 ilist_traits<Argument>::getSymTab(Function *F) {
418 return F ? &F->getValueSymbolTable() : 0;
421 } // End llvm namespace