LLVM API Documentation
00001 //===-- llvm/Function.h - Class to represent a single function --*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains the declaration of the Function class, which represents a 00011 // single function/procedure in LLVM. 00012 // 00013 // A function basically consists of a list of basic blocks, a list of arguments, 00014 // and a symbol table. 00015 // 00016 //===----------------------------------------------------------------------===// 00017 00018 #ifndef LLVM_FUNCTION_H 00019 #define LLVM_FUNCTION_H 00020 00021 #include "llvm/GlobalValue.h" 00022 #include "llvm/BasicBlock.h" 00023 #include "llvm/Argument.h" 00024 #include "llvm/Attributes.h" 00025 00026 namespace llvm { 00027 00028 class FunctionType; 00029 class LLVMContext; 00030 00031 // Traits for intrusive list of basic blocks... 00032 template<> struct ilist_traits<BasicBlock> 00033 : public SymbolTableListTraits<BasicBlock, Function> { 00034 00035 // createSentinel is used to get hold of the node that marks the end of the 00036 // list... (same trick used here as in ilist_traits<Instruction>) 00037 BasicBlock *createSentinel() const { 00038 return static_cast<BasicBlock*>(&Sentinel); 00039 } 00040 static void destroySentinel(BasicBlock*) {} 00041 00042 BasicBlock *provideInitialHead() const { return createSentinel(); } 00043 BasicBlock *ensureHead(BasicBlock*) const { return createSentinel(); } 00044 static void noteHead(BasicBlock*, BasicBlock*) {} 00045 00046 static ValueSymbolTable *getSymTab(Function *ItemParent); 00047 private: 00048 mutable ilist_node<BasicBlock> Sentinel; 00049 }; 00050 00051 template<> struct ilist_traits<Argument> 00052 : public SymbolTableListTraits<Argument, Function> { 00053 00054 Argument *createSentinel() const { 00055 return static_cast<Argument*>(&Sentinel); 00056 } 00057 static void destroySentinel(Argument*) {} 00058 00059 Argument *provideInitialHead() const { return createSentinel(); } 00060 Argument *ensureHead(Argument*) const { return createSentinel(); } 00061 static void noteHead(Argument*, Argument*) {} 00062 00063 static ValueSymbolTable *getSymTab(Function *ItemParent); 00064 private: 00065 mutable ilist_node<Argument> Sentinel; 00066 }; 00067 00068 class Function : public GlobalValue, 00069 public ilist_node<Function> { 00070 public: 00071 typedef iplist<Argument> ArgumentListType; 00072 typedef iplist<BasicBlock> BasicBlockListType; 00073 00074 // BasicBlock iterators... 00075 typedef BasicBlockListType::iterator iterator; 00076 typedef BasicBlockListType::const_iterator const_iterator; 00077 00078 typedef ArgumentListType::iterator arg_iterator; 00079 typedef ArgumentListType::const_iterator const_arg_iterator; 00080 00081 private: 00082 // Important things that make up a function! 00083 BasicBlockListType BasicBlocks; ///< The basic blocks 00084 mutable ArgumentListType ArgumentList; ///< The formal arguments 00085 ValueSymbolTable *SymTab; ///< Symbol table of args/instructions 00086 AttrListPtr AttributeList; ///< Parameter attributes 00087 00088 // The Calling Convention is stored in Value::SubclassData. 00089 /*unsigned CallingConvention;*/ 00090 00091 friend class SymbolTableListTraits<Function, Module>; 00092 00093 void setParent(Module *parent); 00094 00095 /// hasLazyArguments/CheckLazyArguments - The argument list of a function is 00096 /// built on demand, so that the list isn't allocated until the first client 00097 /// needs it. The hasLazyArguments predicate returns true if the arg list 00098 /// hasn't been set up yet. 00099 bool hasLazyArguments() const { 00100 return SubclassData & 1; 00101 } 00102 void CheckLazyArguments() const { 00103 if (hasLazyArguments()) 00104 BuildLazyArguments(); 00105 } 00106 void BuildLazyArguments() const; 00107 00108 Function(const Function&); // DO NOT IMPLEMENT 00109 void operator=(const Function&); // DO NOT IMPLEMENT 00110 00111 /// Function ctor - If the (optional) Module argument is specified, the 00112 /// function is automatically inserted into the end of the function list for 00113 /// the module. 00114 /// 00115 Function(const FunctionType *Ty, LinkageTypes Linkage, 00116 const Twine &N = "", Module *M = 0); 00117 00118 public: 00119 static Function *Create(const FunctionType *Ty, LinkageTypes Linkage, 00120 const Twine &N = "", Module *M = 0) { 00121 return new(0) Function(Ty, Linkage, N, M); 00122 } 00123 00124 ~Function(); 00125 00126 const Type *getReturnType() const; // Return the type of the ret val 00127 const FunctionType *getFunctionType() const; // Return the FunctionType for me 00128 00129 /// getContext - Return a pointer to the LLVMContext associated with this 00130 /// function, or NULL if this function is not bound to a context yet. 00131 LLVMContext &getContext() const; 00132 00133 /// isVarArg - Return true if this function takes a variable number of 00134 /// arguments. 00135 bool isVarArg() const; 00136 00137 /// isDeclaration - Is the body of this function unknown? (The basic block 00138 /// list is empty if so.) This is true for function declarations, but not 00139 /// true for function definitions. 00140 /// 00141 virtual bool isDeclaration() const { return BasicBlocks.empty(); } 00142 00143 /// getIntrinsicID - This method returns the ID number of the specified 00144 /// function, or Intrinsic::not_intrinsic if the function is not an 00145 /// instrinsic, or if the pointer is null. This value is always defined to be 00146 /// zero to allow easy checking for whether a function is intrinsic or not. 00147 /// The particular intrinsic functions which correspond to this value are 00148 /// defined in llvm/Intrinsics.h. 00149 /// 00150 unsigned getIntrinsicID() const; 00151 bool isIntrinsic() const { return getIntrinsicID() != 0; } 00152 00153 /// getCallingConv()/setCallingConv(uint) - These method get and set the 00154 /// calling convention of this function. The enum values for the known 00155 /// calling conventions are defined in CallingConv.h. 00156 unsigned getCallingConv() const { return SubclassData >> 1; } 00157 void setCallingConv(unsigned CC) { 00158 SubclassData = (SubclassData & 1) | (CC << 1); 00159 } 00160 00161 /// getAttributes - Return the attribute list for this Function. 00162 /// 00163 const AttrListPtr &getAttributes() const { return AttributeList; } 00164 00165 /// setAttributes - Set the attribute list for this Function. 00166 /// 00167 void setAttributes(const AttrListPtr &attrs) { AttributeList = attrs; } 00168 00169 /// hasFnAttr - Return true if this function has the given attribute. 00170 bool hasFnAttr(Attributes N) const { 00171 // Function Attributes are stored at ~0 index 00172 return AttributeList.paramHasAttr(~0U, N); 00173 } 00174 00175 /// addFnAttr - Add function attributes to this function. 00176 /// 00177 void addFnAttr(Attributes N) { 00178 // Function Attributes are stored at ~0 index 00179 addAttribute(~0U, N); 00180 } 00181 00182 /// removeFnAttr - Remove function attributes from this function. 00183 /// 00184 void removeFnAttr(Attributes N) { 00185 // Function Attributes are stored at ~0 index 00186 removeAttribute(~0U, N); 00187 } 00188 00189 /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm 00190 /// to use during code generation. 00191 bool hasGC() const; 00192 const char *getGC() const; 00193 void setGC(const char *Str); 00194 void clearGC(); 00195 00196 /// @brief Determine whether the function has the given attribute. 00197 bool paramHasAttr(unsigned i, Attributes attr) const { 00198 return AttributeList.paramHasAttr(i, attr); 00199 } 00200 00201 /// addAttribute - adds the attribute to the list of attributes. 00202 void addAttribute(unsigned i, Attributes attr); 00203 00204 /// removeAttribute - removes the attribute from the list of attributes. 00205 void removeAttribute(unsigned i, Attributes attr); 00206 00207 /// @brief Extract the alignment for a call or parameter (0=unknown). 00208 unsigned getParamAlignment(unsigned i) const { 00209 return AttributeList.getParamAlignment(i); 00210 } 00211 00212 /// @brief Determine if the function does not access memory. 00213 bool doesNotAccessMemory() const { 00214 return hasFnAttr(Attribute::ReadNone); 00215 } 00216 void setDoesNotAccessMemory(bool DoesNotAccessMemory = true) { 00217 if (DoesNotAccessMemory) addFnAttr(Attribute::ReadNone); 00218 else removeFnAttr(Attribute::ReadNone); 00219 } 00220 00221 /// @brief Determine if the function does not access or only reads memory. 00222 bool onlyReadsMemory() const { 00223 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly); 00224 } 00225 void setOnlyReadsMemory(bool OnlyReadsMemory = true) { 00226 if (OnlyReadsMemory) addFnAttr(Attribute::ReadOnly); 00227 else removeFnAttr(Attribute::ReadOnly | Attribute::ReadNone); 00228 } 00229 00230 /// @brief Determine if the function cannot return. 00231 bool doesNotReturn() const { 00232 return hasFnAttr(Attribute::NoReturn); 00233 } 00234 void setDoesNotReturn(bool DoesNotReturn = true) { 00235 if (DoesNotReturn) addFnAttr(Attribute::NoReturn); 00236 else removeFnAttr(Attribute::NoReturn); 00237 } 00238 00239 /// @brief Determine if the function cannot unwind. 00240 bool doesNotThrow() const { 00241 return hasFnAttr(Attribute::NoUnwind); 00242 } 00243 void setDoesNotThrow(bool DoesNotThrow = true) { 00244 if (DoesNotThrow) addFnAttr(Attribute::NoUnwind); 00245 else removeFnAttr(Attribute::NoUnwind); 00246 } 00247 00248 /// @brief Determine if the function returns a structure through first 00249 /// pointer argument. 00250 bool hasStructRetAttr() const { 00251 return paramHasAttr(1, Attribute::StructRet); 00252 } 00253 00254 /// @brief Determine if the parameter does not alias other parameters. 00255 /// @param n The parameter to check. 1 is the first parameter, 0 is the return 00256 bool doesNotAlias(unsigned n) const { 00257 return paramHasAttr(n, Attribute::NoAlias); 00258 } 00259 void setDoesNotAlias(unsigned n, bool DoesNotAlias = true) { 00260 if (DoesNotAlias) addAttribute(n, Attribute::NoAlias); 00261 else removeAttribute(n, Attribute::NoAlias); 00262 } 00263 00264 /// @brief Determine if the parameter can be captured. 00265 /// @param n The parameter to check. 1 is the first parameter, 0 is the return 00266 bool doesNotCapture(unsigned n) const { 00267 return paramHasAttr(n, Attribute::NoCapture); 00268 } 00269 void setDoesNotCapture(unsigned n, bool DoesNotCapture = true) { 00270 if (DoesNotCapture) addAttribute(n, Attribute::NoCapture); 00271 else removeAttribute(n, Attribute::NoCapture); 00272 } 00273 00274 /// copyAttributesFrom - copy all additional attributes (those not needed to 00275 /// create a Function) from the Function Src to this one. 00276 void copyAttributesFrom(const GlobalValue *Src); 00277 00278 /// deleteBody - This method deletes the body of the function, and converts 00279 /// the linkage to external. 00280 /// 00281 void deleteBody() { 00282 dropAllReferences(); 00283 setLinkage(ExternalLinkage); 00284 } 00285 00286 /// removeFromParent - This method unlinks 'this' from the containing module, 00287 /// but does not delete it. 00288 /// 00289 virtual void removeFromParent(); 00290 00291 /// eraseFromParent - This method unlinks 'this' from the containing module 00292 /// and deletes it. 00293 /// 00294 virtual void eraseFromParent(); 00295 00296 00297 /// Get the underlying elements of the Function... the basic block list is 00298 /// empty for external functions. 00299 /// 00300 const ArgumentListType &getArgumentList() const { 00301 CheckLazyArguments(); 00302 return ArgumentList; 00303 } 00304 ArgumentListType &getArgumentList() { 00305 CheckLazyArguments(); 00306 return ArgumentList; 00307 } 00308 static iplist<Argument> Function::*getSublistAccess(Argument*) { 00309 return &Function::ArgumentList; 00310 } 00311 00312 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; } 00313 BasicBlockListType &getBasicBlockList() { return BasicBlocks; } 00314 static iplist<BasicBlock> Function::*getSublistAccess(BasicBlock*) { 00315 return &Function::BasicBlocks; 00316 } 00317 00318 const BasicBlock &getEntryBlock() const { return front(); } 00319 BasicBlock &getEntryBlock() { return front(); } 00320 00321 //===--------------------------------------------------------------------===// 00322 // Symbol Table Accessing functions... 00323 00324 /// getSymbolTable() - Return the symbol table... 00325 /// 00326 inline ValueSymbolTable &getValueSymbolTable() { return *SymTab; } 00327 inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; } 00328 00329 00330 //===--------------------------------------------------------------------===// 00331 // BasicBlock iterator forwarding functions 00332 // 00333 iterator begin() { return BasicBlocks.begin(); } 00334 const_iterator begin() const { return BasicBlocks.begin(); } 00335 iterator end () { return BasicBlocks.end(); } 00336 const_iterator end () const { return BasicBlocks.end(); } 00337 00338 size_t size() const { return BasicBlocks.size(); } 00339 bool empty() const { return BasicBlocks.empty(); } 00340 const BasicBlock &front() const { return BasicBlocks.front(); } 00341 BasicBlock &front() { return BasicBlocks.front(); } 00342 const BasicBlock &back() const { return BasicBlocks.back(); } 00343 BasicBlock &back() { return BasicBlocks.back(); } 00344 00345 //===--------------------------------------------------------------------===// 00346 // Argument iterator forwarding functions 00347 // 00348 arg_iterator arg_begin() { 00349 CheckLazyArguments(); 00350 return ArgumentList.begin(); 00351 } 00352 const_arg_iterator arg_begin() const { 00353 CheckLazyArguments(); 00354 return ArgumentList.begin(); 00355 } 00356 arg_iterator arg_end() { 00357 CheckLazyArguments(); 00358 return ArgumentList.end(); 00359 } 00360 const_arg_iterator arg_end() const { 00361 CheckLazyArguments(); 00362 return ArgumentList.end(); 00363 } 00364 00365 size_t arg_size() const; 00366 bool arg_empty() const; 00367 00368 /// viewCFG - This function is meant for use from the debugger. You can just 00369 /// say 'call F->viewCFG()' and a ghostview window should pop up from the 00370 /// program, displaying the CFG of the current function with the code for each 00371 /// basic block inside. This depends on there being a 'dot' and 'gv' program 00372 /// in your path. 00373 /// 00374 void viewCFG() const; 00375 00376 /// viewCFGOnly - This function is meant for use from the debugger. It works 00377 /// just like viewCFG, but it does not include the contents of basic blocks 00378 /// into the nodes, just the label. If you are only interested in the CFG 00379 /// this can make the graph smaller. 00380 /// 00381 void viewCFGOnly() const; 00382 00383 /// Methods for support type inquiry through isa, cast, and dyn_cast: 00384 static inline bool classof(const Function *) { return true; } 00385 static inline bool classof(const Value *V) { 00386 return V->getValueID() == Value::FunctionVal; 00387 } 00388 00389 /// dropAllReferences() - This method causes all the subinstructions to "let 00390 /// go" of all references that they are maintaining. This allows one to 00391 /// 'delete' a whole module at a time, even though there may be circular 00392 /// references... first all references are dropped, and all use counts go to 00393 /// zero. Then everything is deleted for real. Note that no operations are 00394 /// valid on an object that has "dropped all references", except operator 00395 /// delete. 00396 /// 00397 /// Since no other object in the module can have references into the body of a 00398 /// function, dropping all references deletes the entire body of the function, 00399 /// including any contained basic blocks. 00400 /// 00401 void dropAllReferences(); 00402 00403 /// hasAddressTaken - returns true if there are any uses of this function 00404 /// other than direct calls or invokes to it. 00405 bool hasAddressTaken() const; 00406 }; 00407 00408 inline ValueSymbolTable * 00409 ilist_traits<BasicBlock>::getSymTab(Function *F) { 00410 return F ? &F->getValueSymbolTable() : 0; 00411 } 00412 00413 inline ValueSymbolTable * 00414 ilist_traits<Argument>::getSymTab(Function *F) { 00415 return F ? &F->getValueSymbolTable() : 0; 00416 } 00417 00418 } // End llvm namespace 00419 00420 #endif