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*) {}
41 static iplist<BasicBlock> &getList(Function *F);
42 static ValueSymbolTable *getSymTab(Function *ItemParent);
43 static int getListOffset();
45 mutable ilist_node<BasicBlock> Sentinel;
48 template<> struct ilist_traits<Argument>
49 : public SymbolTableListTraits<Argument, Function> {
51 Argument *createSentinel() const {
52 return static_cast<Argument*>(&Sentinel);
54 static void destroySentinel(Argument*) {}
55 static iplist<Argument> &getList(Function *F);
56 static ValueSymbolTable *getSymTab(Function *ItemParent);
57 static int getListOffset();
59 mutable ilist_node<Argument> Sentinel;
62 class Function : public GlobalValue, public Annotable,
63 public ilist_node<Function> {
65 typedef iplist<Argument> ArgumentListType;
66 typedef iplist<BasicBlock> BasicBlockListType;
68 // BasicBlock iterators...
69 typedef BasicBlockListType::iterator iterator;
70 typedef BasicBlockListType::const_iterator const_iterator;
72 typedef ArgumentListType::iterator arg_iterator;
73 typedef ArgumentListType::const_iterator const_arg_iterator;
76 // Important things that make up a function!
77 BasicBlockListType BasicBlocks; ///< The basic blocks
78 mutable ArgumentListType ArgumentList; ///< The formal arguments
79 ValueSymbolTable *SymTab; ///< Symbol table of args/instructions
80 AttrListPtr AttributeList; ///< Parameter attributes
82 // The Calling Convention is stored in Value::SubclassData.
83 /*unsigned CallingConvention;*/
85 friend class SymbolTableListTraits<Function, Module>;
87 void setParent(Module *parent);
89 /// hasLazyArguments/CheckLazyArguments - The argument list of a function is
90 /// built on demand, so that the list isn't allocated until the first client
91 /// needs it. The hasLazyArguments predicate returns true if the arg list
92 /// hasn't been set up yet.
93 bool hasLazyArguments() const {
94 return SubclassData & 1;
96 void CheckLazyArguments() const {
97 if (hasLazyArguments())
100 void BuildLazyArguments() const;
102 Function(const Function&); // DO NOT IMPLEMENT
103 void operator=(const Function&); // DO NOT IMPLEMENT
105 /// Function ctor - If the (optional) Module argument is specified, the
106 /// function is automatically inserted into the end of the function list for
109 Function(const FunctionType *Ty, LinkageTypes Linkage,
110 const std::string &N = "", Module *M = 0);
113 static Function *Create(const FunctionType *Ty, LinkageTypes Linkage,
114 const std::string &N = "", Module *M = 0) {
115 return new(0) Function(Ty, Linkage, N, M);
120 const Type *getReturnType() const; // Return the type of the ret val
121 const FunctionType *getFunctionType() const; // Return the FunctionType for me
123 /// isVarArg - Return true if this function takes a variable number of
125 bool isVarArg() const;
127 /// isDeclaration - Is the body of this function unknown? (The basic block
128 /// list is empty if so.) This is true for function declarations, but not
129 /// true for function definitions.
131 virtual bool isDeclaration() const { return BasicBlocks.empty(); }
133 /// getIntrinsicID - This method returns the ID number of the specified
134 /// function, or Intrinsic::not_intrinsic if the function is not an
135 /// instrinsic, or if the pointer is null. This value is always defined to be
136 /// zero to allow easy checking for whether a function is intrinsic or not.
137 /// The particular intrinsic functions which correspond to this value are
138 /// defined in llvm/Intrinsics.h.
140 unsigned getIntrinsicID() const;
141 bool isIntrinsic() const { return getIntrinsicID() != 0; }
143 /// getCallingConv()/setCallingConv(uint) - These method get and set the
144 /// calling convention of this function. The enum values for the known
145 /// calling conventions are defined in CallingConv.h.
146 unsigned getCallingConv() const { return SubclassData >> 1; }
147 void setCallingConv(unsigned CC) {
148 SubclassData = (SubclassData & 1) | (CC << 1);
151 /// getAttributes - Return the attribute list for this Function.
153 const AttrListPtr &getAttributes() const { return AttributeList; }
155 /// setAttributes - Set the attribute list for this Function.
157 void setAttributes(const AttrListPtr &attrs) { AttributeList = attrs; }
159 /// hasFnAttr - Return true if this function has the given attribute.
160 bool hasFnAttr(Attributes N) const {
161 // Function Attributes are stored at ~0 index
162 return AttributeList.paramHasAttr(~0U, N);
165 /// addFnAttr - Add function attributes to this function.
167 void addFnAttr(Attributes N) {
168 // Function Attributes are stored at ~0 index
169 addAttribute(~0U, N);
172 /// removeFnAttr - Remove function attributes from this function.
174 void removeFnAttr(Attributes N) {
175 // Function Attributes are stored at ~0 index
176 removeAttribute(~0U, N);
179 /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm
180 /// to use during code generation.
182 const char *getGC() const;
183 void setGC(const char *Str);
186 /// @brief Determine whether the function has the given attribute.
187 bool paramHasAttr(unsigned i, Attributes attr) const {
188 return AttributeList.paramHasAttr(i, attr);
191 /// addAttribute - adds the attribute to the list of attributes.
192 void addAttribute(unsigned i, Attributes attr);
194 /// removeAttribute - removes the attribute from the list of attributes.
195 void removeAttribute(unsigned i, Attributes attr);
197 /// @brief Extract the alignment for a call or parameter (0=unknown).
198 unsigned getParamAlignment(unsigned i) const {
199 return AttributeList.getParamAlignment(i);
202 /// @brief Determine if the function does not access memory.
203 bool doesNotAccessMemory() const {
204 return hasFnAttr(Attribute::ReadNone);
206 void setDoesNotAccessMemory(bool DoesNotAccessMemory = true) {
207 if (DoesNotAccessMemory) addFnAttr(Attribute::ReadNone);
208 else removeFnAttr(Attribute::ReadNone);
211 /// @brief Determine if the function does not access or only reads memory.
212 bool onlyReadsMemory() const {
213 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
215 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
216 if (OnlyReadsMemory) addFnAttr(Attribute::ReadOnly);
217 else removeFnAttr(Attribute::ReadOnly | Attribute::ReadNone);
220 /// @brief Determine if the function cannot return.
221 bool doesNotReturn() const {
222 return hasFnAttr(Attribute::NoReturn);
224 void setDoesNotReturn(bool DoesNotReturn = true) {
225 if (DoesNotReturn) addFnAttr(Attribute::NoReturn);
226 else removeFnAttr(Attribute::NoReturn);
229 /// @brief Determine if the function cannot unwind.
230 bool doesNotThrow() const {
231 return hasFnAttr(Attribute::NoUnwind);
233 void setDoesNotThrow(bool DoesNotThrow = true) {
234 if (DoesNotThrow) addFnAttr(Attribute::NoUnwind);
235 else removeFnAttr(Attribute::NoUnwind);
238 /// @brief Determine if the function returns a structure through first
239 /// pointer argument.
240 bool hasStructRetAttr() const {
241 return paramHasAttr(1, Attribute::StructRet);
244 /// @brief Determine if the parameter does not alias other parameters.
245 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
246 bool doesNotAlias(unsigned n) const {
247 return paramHasAttr(n, Attribute::NoAlias);
249 void setDoesNotAlias(unsigned n, bool DoesNotAlias = true) {
250 if (DoesNotAlias) addAttribute(n, Attribute::NoAlias);
251 else removeAttribute(n, Attribute::NoAlias);
254 /// @brief Determine if the parameter can be captured.
255 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
256 bool doesNotCapture(unsigned n) const {
257 return paramHasAttr(n, Attribute::NoCapture);
259 void setDoesNotCapture(unsigned n, bool DoesNotCapture = true) {
260 if (DoesNotCapture) addAttribute(n, Attribute::NoCapture);
261 else removeAttribute(n, Attribute::NoCapture);
264 /// copyAttributesFrom - copy all additional attributes (those not needed to
265 /// create a Function) from the Function Src to this one.
266 void copyAttributesFrom(const GlobalValue *Src);
268 /// deleteBody - This method deletes the body of the function, and converts
269 /// the linkage to external.
273 setLinkage(ExternalLinkage);
276 /// removeFromParent - This method unlinks 'this' from the containing module,
277 /// but does not delete it.
279 virtual void removeFromParent();
281 /// eraseFromParent - This method unlinks 'this' from the containing module
284 virtual void eraseFromParent();
287 /// Get the underlying elements of the Function... the basic block list is
288 /// empty for external functions.
290 const ArgumentListType &getArgumentList() const {
291 CheckLazyArguments();
294 ArgumentListType &getArgumentList() {
295 CheckLazyArguments();
299 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
300 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
302 const BasicBlock &getEntryBlock() const { return front(); }
303 BasicBlock &getEntryBlock() { return front(); }
305 //===--------------------------------------------------------------------===//
306 // Symbol Table Accessing functions...
308 /// getSymbolTable() - Return the symbol table...
310 inline ValueSymbolTable &getValueSymbolTable() { return *SymTab; }
311 inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
314 //===--------------------------------------------------------------------===//
315 // BasicBlock iterator forwarding functions
317 iterator begin() { return BasicBlocks.begin(); }
318 const_iterator begin() const { return BasicBlocks.begin(); }
319 iterator end () { return BasicBlocks.end(); }
320 const_iterator end () const { return BasicBlocks.end(); }
322 size_t size() const { return BasicBlocks.size(); }
323 bool empty() const { return BasicBlocks.empty(); }
324 const BasicBlock &front() const { return BasicBlocks.front(); }
325 BasicBlock &front() { return BasicBlocks.front(); }
326 const BasicBlock &back() const { return BasicBlocks.back(); }
327 BasicBlock &back() { return BasicBlocks.back(); }
329 //===--------------------------------------------------------------------===//
330 // Argument iterator forwarding functions
332 arg_iterator arg_begin() {
333 CheckLazyArguments();
334 return ArgumentList.begin();
336 const_arg_iterator arg_begin() const {
337 CheckLazyArguments();
338 return ArgumentList.begin();
340 arg_iterator arg_end() {
341 CheckLazyArguments();
342 return ArgumentList.end();
344 const_arg_iterator arg_end() const {
345 CheckLazyArguments();
346 return ArgumentList.end();
349 size_t arg_size() const;
350 bool arg_empty() const;
352 /// viewCFG - This function is meant for use from the debugger. You can just
353 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
354 /// program, displaying the CFG of the current function with the code for each
355 /// basic block inside. This depends on there being a 'dot' and 'gv' program
358 void viewCFG() const;
360 /// viewCFGOnly - This function is meant for use from the debugger. It works
361 /// just like viewCFG, but it does not include the contents of basic blocks
362 /// into the nodes, just the label. If you are only interested in the CFG
363 /// this can make the graph smaller.
365 void viewCFGOnly() const;
367 /// Methods for support type inquiry through isa, cast, and dyn_cast:
368 static inline bool classof(const Function *) { return true; }
369 static inline bool classof(const Value *V) {
370 return V->getValueID() == Value::FunctionVal;
373 /// dropAllReferences() - This method causes all the subinstructions to "let
374 /// go" of all references that they are maintaining. This allows one to
375 /// 'delete' a whole module at a time, even though there may be circular
376 /// references... first all references are dropped, and all use counts go to
377 /// zero. Then everything is deleted for real. Note that no operations are
378 /// valid on an object that has "dropped all references", except operator
381 /// Since no other object in the module can have references into the body of a
382 /// function, dropping all references deletes the entire body of the function,
383 /// including any contained basic blocks.
385 void dropAllReferences();
387 static unsigned getBasicBlockListOffset() {
389 return unsigned(reinterpret_cast<uintptr_t>(&Obj->BasicBlocks));
391 static unsigned getArgumentListOffset() {
393 return unsigned(reinterpret_cast<uintptr_t>(&Obj->ArgumentList));
397 inline ValueSymbolTable *
398 ilist_traits<BasicBlock>::getSymTab(Function *F) {
399 return F ? &F->getValueSymbolTable() : 0;
402 inline ValueSymbolTable *
403 ilist_traits<Argument>::getSymTab(Function *F) {
404 return F ? &F->getValueSymbolTable() : 0;
408 ilist_traits<BasicBlock>::getListOffset() {
409 return Function::getBasicBlockListOffset();
413 ilist_traits<Argument>::getListOffset() {
414 return Function::getArgumentListOffset();
418 } // End llvm namespace