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 iplist<BasicBlock> &getList(Function *F);
47 static ValueSymbolTable *getSymTab(Function *ItemParent);
48 static int getListOffset();
50 mutable ilist_node<BasicBlock> Sentinel;
53 template<> struct ilist_traits<Argument>
54 : public SymbolTableListTraits<Argument, Function> {
56 Argument *createSentinel() const {
57 return static_cast<Argument*>(&Sentinel);
59 static void destroySentinel(Argument*) {}
61 Argument *provideInitialHead() const { return createSentinel(); }
62 Argument *ensureHead(Argument*) const { return createSentinel(); }
63 static void noteHead(Argument*, Argument*) {}
65 static iplist<Argument> &getList(Function *F);
66 static ValueSymbolTable *getSymTab(Function *ItemParent);
67 static int getListOffset();
69 mutable ilist_node<Argument> Sentinel;
72 class Function : public GlobalValue, public Annotable,
73 public ilist_node<Function> {
75 typedef iplist<Argument> ArgumentListType;
76 typedef iplist<BasicBlock> BasicBlockListType;
78 // BasicBlock iterators...
79 typedef BasicBlockListType::iterator iterator;
80 typedef BasicBlockListType::const_iterator const_iterator;
82 typedef ArgumentListType::iterator arg_iterator;
83 typedef ArgumentListType::const_iterator const_arg_iterator;
86 // Important things that make up a function!
87 BasicBlockListType BasicBlocks; ///< The basic blocks
88 mutable ArgumentListType ArgumentList; ///< The formal arguments
89 ValueSymbolTable *SymTab; ///< Symbol table of args/instructions
90 AttrListPtr AttributeList; ///< Parameter attributes
92 // The Calling Convention is stored in Value::SubclassData.
93 /*unsigned CallingConvention;*/
95 friend class SymbolTableListTraits<Function, Module>;
97 void setParent(Module *parent);
99 /// hasLazyArguments/CheckLazyArguments - The argument list of a function is
100 /// built on demand, so that the list isn't allocated until the first client
101 /// needs it. The hasLazyArguments predicate returns true if the arg list
102 /// hasn't been set up yet.
103 bool hasLazyArguments() const {
104 return SubclassData & 1;
106 void CheckLazyArguments() const {
107 if (hasLazyArguments())
108 BuildLazyArguments();
110 void BuildLazyArguments() const;
112 Function(const Function&); // DO NOT IMPLEMENT
113 void operator=(const Function&); // DO NOT IMPLEMENT
115 /// Function ctor - If the (optional) Module argument is specified, the
116 /// function is automatically inserted into the end of the function list for
119 Function(const FunctionType *Ty, LinkageTypes Linkage,
120 const std::string &N = "", Module *M = 0);
123 static Function *Create(const FunctionType *Ty, LinkageTypes Linkage,
124 const std::string &N = "", Module *M = 0) {
125 return new(0) Function(Ty, Linkage, N, M);
130 const Type *getReturnType() const; // Return the type of the ret val
131 const FunctionType *getFunctionType() const; // Return the FunctionType for me
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();
309 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
310 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
312 const BasicBlock &getEntryBlock() const { return front(); }
313 BasicBlock &getEntryBlock() { return front(); }
315 //===--------------------------------------------------------------------===//
316 // Symbol Table Accessing functions...
318 /// getSymbolTable() - Return the symbol table...
320 inline ValueSymbolTable &getValueSymbolTable() { return *SymTab; }
321 inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
324 //===--------------------------------------------------------------------===//
325 // BasicBlock iterator forwarding functions
327 iterator begin() { return BasicBlocks.begin(); }
328 const_iterator begin() const { return BasicBlocks.begin(); }
329 iterator end () { return BasicBlocks.end(); }
330 const_iterator end () const { return BasicBlocks.end(); }
332 size_t size() const { return BasicBlocks.size(); }
333 bool empty() const { return BasicBlocks.empty(); }
334 const BasicBlock &front() const { return BasicBlocks.front(); }
335 BasicBlock &front() { return BasicBlocks.front(); }
336 const BasicBlock &back() const { return BasicBlocks.back(); }
337 BasicBlock &back() { return BasicBlocks.back(); }
339 //===--------------------------------------------------------------------===//
340 // Argument iterator forwarding functions
342 arg_iterator arg_begin() {
343 CheckLazyArguments();
344 return ArgumentList.begin();
346 const_arg_iterator arg_begin() const {
347 CheckLazyArguments();
348 return ArgumentList.begin();
350 arg_iterator arg_end() {
351 CheckLazyArguments();
352 return ArgumentList.end();
354 const_arg_iterator arg_end() const {
355 CheckLazyArguments();
356 return ArgumentList.end();
359 size_t arg_size() const;
360 bool arg_empty() const;
362 /// viewCFG - This function is meant for use from the debugger. You can just
363 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
364 /// program, displaying the CFG of the current function with the code for each
365 /// basic block inside. This depends on there being a 'dot' and 'gv' program
368 void viewCFG() const;
370 /// viewCFGOnly - This function is meant for use from the debugger. It works
371 /// just like viewCFG, but it does not include the contents of basic blocks
372 /// into the nodes, just the label. If you are only interested in the CFG
373 /// this can make the graph smaller.
375 void viewCFGOnly() const;
377 /// Methods for support type inquiry through isa, cast, and dyn_cast:
378 static inline bool classof(const Function *) { return true; }
379 static inline bool classof(const Value *V) {
380 return V->getValueID() == Value::FunctionVal;
383 /// dropAllReferences() - This method causes all the subinstructions to "let
384 /// go" of all references that they are maintaining. This allows one to
385 /// 'delete' a whole module at a time, even though there may be circular
386 /// references... first all references are dropped, and all use counts go to
387 /// zero. Then everything is deleted for real. Note that no operations are
388 /// valid on an object that has "dropped all references", except operator
391 /// Since no other object in the module can have references into the body of a
392 /// function, dropping all references deletes the entire body of the function,
393 /// including any contained basic blocks.
395 void dropAllReferences();
397 static unsigned getBasicBlockListOffset() {
399 return unsigned(reinterpret_cast<uintptr_t>(&Obj->BasicBlocks));
401 static unsigned getArgumentListOffset() {
403 return unsigned(reinterpret_cast<uintptr_t>(&Obj->ArgumentList));
407 inline ValueSymbolTable *
408 ilist_traits<BasicBlock>::getSymTab(Function *F) {
409 return F ? &F->getValueSymbolTable() : 0;
412 inline ValueSymbolTable *
413 ilist_traits<Argument>::getSymTab(Function *F) {
414 return F ? &F->getValueSymbolTable() : 0;
418 ilist_traits<BasicBlock>::getListOffset() {
419 return Function::getBasicBlockListOffset();
423 ilist_traits<Argument>::getListOffset() {
424 return Function::getArgumentListOffset();
428 } // End llvm namespace