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 instructions...
32 template<> struct ilist_traits<BasicBlock>
33 : public SymbolTableListTraits<BasicBlock, Function> {
35 // createSentinel is used to create a node that marks the end of the list...
36 static BasicBlock *createSentinel();
37 static void destroySentinel(BasicBlock *BB) { delete BB; }
38 static iplist<BasicBlock> &getList(Function *F);
39 static ValueSymbolTable *getSymTab(Function *ItemParent);
40 static int getListOffset();
43 template<> struct ilist_traits<Argument>
44 : public SymbolTableListTraits<Argument, Function> {
46 // createSentinel is used to create a node that marks the end of the list...
47 static Argument *createSentinel();
48 static void destroySentinel(Argument *A) { delete A; }
49 static iplist<Argument> &getList(Function *F);
50 static ValueSymbolTable *getSymTab(Function *ItemParent);
51 static int getListOffset();
54 class Function : public GlobalValue, public Annotable,
55 public ilist_node<Function> {
57 typedef iplist<Argument> ArgumentListType;
58 typedef iplist<BasicBlock> BasicBlockListType;
60 // BasicBlock iterators...
61 typedef BasicBlockListType::iterator iterator;
62 typedef BasicBlockListType::const_iterator const_iterator;
64 typedef ArgumentListType::iterator arg_iterator;
65 typedef ArgumentListType::const_iterator const_arg_iterator;
68 // Important things that make up a function!
69 BasicBlockListType BasicBlocks; ///< The basic blocks
70 mutable ArgumentListType ArgumentList; ///< The formal arguments
71 ValueSymbolTable *SymTab; ///< Symbol table of args/instructions
72 AttrListPtr AttributeList; ///< Parameter attributes
74 // The Calling Convention is stored in Value::SubclassData.
75 /*unsigned CallingConvention;*/
77 friend class SymbolTableListTraits<Function, Module>;
79 void setParent(Module *parent);
81 /// hasLazyArguments/CheckLazyArguments - The argument list of a function is
82 /// built on demand, so that the list isn't allocated until the first client
83 /// needs it. The hasLazyArguments predicate returns true if the arg list
84 /// hasn't been set up yet.
85 bool hasLazyArguments() const {
86 return SubclassData & 1;
88 void CheckLazyArguments() const {
89 if (hasLazyArguments())
92 void BuildLazyArguments() const;
94 Function(const Function&); // DO NOT IMPLEMENT
95 void operator=(const Function&); // DO NOT IMPLEMENT
97 /// Function ctor - If the (optional) Module argument is specified, the
98 /// function is automatically inserted into the end of the function list for
101 Function(const FunctionType *Ty, LinkageTypes Linkage,
102 const std::string &N = "", Module *M = 0);
105 static Function *Create(const FunctionType *Ty, LinkageTypes Linkage,
106 const std::string &N = "", Module *M = 0) {
107 return new(0) Function(Ty, Linkage, N, M);
112 const Type *getReturnType() const; // Return the type of the ret val
113 const FunctionType *getFunctionType() const; // Return the FunctionType for me
115 /// isVarArg - Return true if this function takes a variable number of
117 bool isVarArg() const;
119 /// isDeclaration - Is the body of this function unknown? (The basic block
120 /// list is empty if so.) This is true for function declarations, but not
121 /// true for function definitions.
123 virtual bool isDeclaration() const { return BasicBlocks.empty(); }
125 /// getIntrinsicID - This method returns the ID number of the specified
126 /// function, or Intrinsic::not_intrinsic if the function is not an
127 /// instrinsic, or if the pointer is null. This value is always defined to be
128 /// zero to allow easy checking for whether a function is intrinsic or not.
129 /// The particular intrinsic functions which correspond to this value are
130 /// defined in llvm/Intrinsics.h.
132 unsigned getIntrinsicID(bool noAssert = false) const;
133 bool isIntrinsic() const { return getIntrinsicID() != 0; }
135 /// getCallingConv()/setCallingConv(uint) - These method get and set the
136 /// calling convention of this function. The enum values for the known
137 /// calling conventions are defined in CallingConv.h.
138 unsigned getCallingConv() const { return SubclassData >> 1; }
139 void setCallingConv(unsigned CC) {
140 SubclassData = (SubclassData & 1) | (CC << 1);
143 /// getAttributes - Return the parameter attributes for this Function.
145 const AttrListPtr &getAttributes() const { return AttributeList; }
147 /// setAttributes - Set the parameter attributes for this Function.
149 void setAttributes(const AttrListPtr &attrs) { AttributeList = attrs; }
152 /// hasNote - Return true if this function has given note.
153 bool hasNote(Attributes N) const {
154 // Notes are stored at ~0 index in parameter attribute list
155 return (paramHasAttr(~0, N));
158 /// setNotes - Set notes for this function
160 void setNotes(const Attributes N) {
161 // Notes are stored at ~0 index in parameter attribute list
165 /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm
166 /// to use during code generation.
168 const char *getGC() const;
169 void setGC(const char *Str);
172 /// @brief Determine whether the function has the given attribute.
173 bool paramHasAttr(unsigned i, Attributes attr) const {
174 return AttributeList.paramHasAttr(i, attr);
177 /// addAttribute - adds the attribute to the list of attributes.
178 void addAttribute(unsigned i, Attributes attr);
180 /// removeAttribute - removes the attribute from the list of attributes.
181 void removeAttribute(unsigned i, Attributes attr);
183 /// @brief Extract the alignment for a call or parameter (0=unknown).
184 unsigned getParamAlignment(unsigned i) const {
185 return AttributeList.getParamAlignment(i);
188 /// @brief Determine if the function does not access memory.
189 bool doesNotAccessMemory() const {
190 return paramHasAttr(0, Attribute::ReadNone);
192 void setDoesNotAccessMemory(bool DoesNotAccessMemory = true) {
193 if (DoesNotAccessMemory) addAttribute(0, Attribute::ReadNone);
194 else removeAttribute(0, Attribute::ReadNone);
197 /// @brief Determine if the function does not access or only reads memory.
198 bool onlyReadsMemory() const {
199 return doesNotAccessMemory() || paramHasAttr(0, Attribute::ReadOnly);
201 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
202 if (OnlyReadsMemory) addAttribute(0, Attribute::ReadOnly);
203 else removeAttribute(0, Attribute::ReadOnly | Attribute::ReadNone);
206 /// @brief Determine if the function cannot return.
207 bool doesNotReturn() const {
208 return paramHasAttr(0, Attribute::NoReturn);
210 void setDoesNotReturn(bool DoesNotReturn = true) {
211 if (DoesNotReturn) addAttribute(0, Attribute::NoReturn);
212 else removeAttribute(0, Attribute::NoReturn);
215 /// @brief Determine if the function cannot unwind.
216 bool doesNotThrow() const {
217 return paramHasAttr(0, Attribute::NoUnwind);
219 void setDoesNotThrow(bool DoesNotThrow = true) {
220 if (DoesNotThrow) addAttribute(0, Attribute::NoUnwind);
221 else removeAttribute(0, Attribute::NoUnwind);
224 /// @brief Determine if the function returns a structure through first
225 /// pointer argument.
226 bool hasStructRetAttr() const {
227 return paramHasAttr(1, Attribute::StructRet);
230 /// copyAttributesFrom - copy all additional attributes (those not needed to
231 /// create a Function) from the Function Src to this one.
232 void copyAttributesFrom(const GlobalValue *Src);
234 /// deleteBody - This method deletes the body of the function, and converts
235 /// the linkage to external.
239 setLinkage(ExternalLinkage);
242 /// removeFromParent - This method unlinks 'this' from the containing module,
243 /// but does not delete it.
245 virtual void removeFromParent();
247 /// eraseFromParent - This method unlinks 'this' from the containing module
250 virtual void eraseFromParent();
253 /// Get the underlying elements of the Function... the basic block list is
254 /// empty for external functions.
256 const ArgumentListType &getArgumentList() const {
257 CheckLazyArguments();
260 ArgumentListType &getArgumentList() {
261 CheckLazyArguments();
265 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
266 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
268 const BasicBlock &getEntryBlock() const { return front(); }
269 BasicBlock &getEntryBlock() { return front(); }
271 //===--------------------------------------------------------------------===//
272 // Symbol Table Accessing functions...
274 /// getSymbolTable() - Return the symbol table...
276 inline ValueSymbolTable &getValueSymbolTable() { return *SymTab; }
277 inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
280 //===--------------------------------------------------------------------===//
281 // BasicBlock iterator forwarding functions
283 iterator begin() { return BasicBlocks.begin(); }
284 const_iterator begin() const { return BasicBlocks.begin(); }
285 iterator end () { return BasicBlocks.end(); }
286 const_iterator end () const { return BasicBlocks.end(); }
288 size_t size() const { return BasicBlocks.size(); }
289 bool empty() const { return BasicBlocks.empty(); }
290 const BasicBlock &front() const { return BasicBlocks.front(); }
291 BasicBlock &front() { return BasicBlocks.front(); }
292 const BasicBlock &back() const { return BasicBlocks.back(); }
293 BasicBlock &back() { return BasicBlocks.back(); }
295 //===--------------------------------------------------------------------===//
296 // Argument iterator forwarding functions
298 arg_iterator arg_begin() {
299 CheckLazyArguments();
300 return ArgumentList.begin();
302 const_arg_iterator arg_begin() const {
303 CheckLazyArguments();
304 return ArgumentList.begin();
306 arg_iterator arg_end() {
307 CheckLazyArguments();
308 return ArgumentList.end();
310 const_arg_iterator arg_end() const {
311 CheckLazyArguments();
312 return ArgumentList.end();
315 size_t arg_size() const;
316 bool arg_empty() const;
318 /// viewCFG - This function is meant for use from the debugger. You can just
319 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
320 /// program, displaying the CFG of the current function with the code for each
321 /// basic block inside. This depends on there being a 'dot' and 'gv' program
324 void viewCFG() const;
326 /// viewCFGOnly - This function is meant for use from the debugger. It works
327 /// just like viewCFG, but it does not include the contents of basic blocks
328 /// into the nodes, just the label. If you are only interested in the CFG
329 /// this can make the graph smaller.
331 void viewCFGOnly() const;
333 /// Methods for support type inquiry through isa, cast, and dyn_cast:
334 static inline bool classof(const Function *) { return true; }
335 static inline bool classof(const Value *V) {
336 return V->getValueID() == Value::FunctionVal;
339 /// dropAllReferences() - This method causes all the subinstructions to "let
340 /// go" of all references that they are maintaining. This allows one to
341 /// 'delete' a whole module at a time, even though there may be circular
342 /// references... first all references are dropped, and all use counts go to
343 /// zero. Then everything is deleted for real. Note that no operations are
344 /// valid on an object that has "dropped all references", except operator
347 /// Since no other object in the module can have references into the body of a
348 /// function, dropping all references deletes the entire body of the function,
349 /// including any contained basic blocks.
351 void dropAllReferences();
353 static unsigned getBasicBlockListOffset() {
355 return unsigned(reinterpret_cast<uintptr_t>(&Obj->BasicBlocks));
357 static unsigned getArgumentListOffset() {
359 return unsigned(reinterpret_cast<uintptr_t>(&Obj->ArgumentList));
363 inline ValueSymbolTable *
364 ilist_traits<BasicBlock>::getSymTab(Function *F) {
365 return F ? &F->getValueSymbolTable() : 0;
368 inline ValueSymbolTable *
369 ilist_traits<Argument>::getSymTab(Function *F) {
370 return F ? &F->getValueSymbolTable() : 0;
374 ilist_traits<BasicBlock>::getListOffset() {
375 return Function::getBasicBlockListOffset();
379 ilist_traits<Argument>::getListOffset() {
380 return Function::getArgumentListOffset();
384 } // End llvm namespace