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_IR_FUNCTION_H 00019 #define LLVM_IR_FUNCTION_H 00020 00021 #include "llvm/IR/Argument.h" 00022 #include "llvm/IR/Attributes.h" 00023 #include "llvm/IR/BasicBlock.h" 00024 #include "llvm/IR/CallingConv.h" 00025 #include "llvm/IR/GlobalValue.h" 00026 #include "llvm/Support/Compiler.h" 00027 00028 namespace llvm { 00029 00030 class FunctionType; 00031 class LLVMContext; 00032 00033 // Traits for intrusive list of basic blocks... 00034 template<> struct ilist_traits<BasicBlock> 00035 : public SymbolTableListTraits<BasicBlock, Function> { 00036 00037 // createSentinel is used to get hold of the node that marks the end of the 00038 // list... (same trick used here as in ilist_traits<Instruction>) 00039 BasicBlock *createSentinel() const { 00040 return static_cast<BasicBlock*>(&Sentinel); 00041 } 00042 static void destroySentinel(BasicBlock*) {} 00043 00044 BasicBlock *provideInitialHead() const { return createSentinel(); } 00045 BasicBlock *ensureHead(BasicBlock*) const { return createSentinel(); } 00046 static void noteHead(BasicBlock*, BasicBlock*) {} 00047 00048 static ValueSymbolTable *getSymTab(Function *ItemParent); 00049 private: 00050 mutable ilist_half_node<BasicBlock> Sentinel; 00051 }; 00052 00053 template<> struct ilist_traits<Argument> 00054 : public SymbolTableListTraits<Argument, Function> { 00055 00056 Argument *createSentinel() const { 00057 return static_cast<Argument*>(&Sentinel); 00058 } 00059 static void destroySentinel(Argument*) {} 00060 00061 Argument *provideInitialHead() const { return createSentinel(); } 00062 Argument *ensureHead(Argument*) const { return createSentinel(); } 00063 static void noteHead(Argument*, Argument*) {} 00064 00065 static ValueSymbolTable *getSymTab(Function *ItemParent); 00066 private: 00067 mutable ilist_half_node<Argument> Sentinel; 00068 }; 00069 00070 class Function : public GlobalValue, 00071 public ilist_node<Function> { 00072 public: 00073 typedef iplist<Argument> ArgumentListType; 00074 typedef iplist<BasicBlock> BasicBlockListType; 00075 00076 // BasicBlock iterators... 00077 typedef BasicBlockListType::iterator iterator; 00078 typedef BasicBlockListType::const_iterator const_iterator; 00079 00080 typedef ArgumentListType::iterator arg_iterator; 00081 typedef ArgumentListType::const_iterator const_arg_iterator; 00082 00083 private: 00084 // Important things that make up a function! 00085 BasicBlockListType BasicBlocks; ///< The basic blocks 00086 mutable ArgumentListType ArgumentList; ///< The formal arguments 00087 ValueSymbolTable *SymTab; ///< Symbol table of args/instructions 00088 AttributeSet AttributeSets; ///< Parameter attributes 00089 00090 // HasLazyArguments is stored in Value::SubclassData. 00091 /*bool HasLazyArguments;*/ 00092 00093 // The Calling Convention is stored in Value::SubclassData. 00094 /*CallingConv::ID CallingConvention;*/ 00095 00096 friend class SymbolTableListTraits<Function, Module>; 00097 00098 void setParent(Module *parent); 00099 00100 /// hasLazyArguments/CheckLazyArguments - The argument list of a function is 00101 /// built on demand, so that the list isn't allocated until the first client 00102 /// needs it. The hasLazyArguments predicate returns true if the arg list 00103 /// hasn't been set up yet. 00104 bool hasLazyArguments() const { 00105 return getSubclassDataFromValue() & 1; 00106 } 00107 void CheckLazyArguments() const { 00108 if (hasLazyArguments()) 00109 BuildLazyArguments(); 00110 } 00111 void BuildLazyArguments() const; 00112 00113 Function(const Function&) LLVM_DELETED_FUNCTION; 00114 void operator=(const Function&) LLVM_DELETED_FUNCTION; 00115 00116 /// Do the actual lookup of an intrinsic ID when the query could not be 00117 /// answered from the cache. 00118 unsigned lookupIntrinsicID() const LLVM_READONLY; 00119 00120 /// Function ctor - If the (optional) Module argument is specified, the 00121 /// function is automatically inserted into the end of the function list for 00122 /// the module. 00123 /// 00124 Function(FunctionType *Ty, LinkageTypes Linkage, 00125 const Twine &N = "", Module *M = 0); 00126 00127 public: 00128 static Function *Create(FunctionType *Ty, LinkageTypes Linkage, 00129 const Twine &N = "", Module *M = 0) { 00130 return new(0) Function(Ty, Linkage, N, M); 00131 } 00132 00133 ~Function(); 00134 00135 Type *getReturnType() const; // Return the type of the ret val 00136 FunctionType *getFunctionType() const; // Return the FunctionType for me 00137 00138 /// getContext - Return a pointer to the LLVMContext associated with this 00139 /// function, or NULL if this function is not bound to a context yet. 00140 LLVMContext &getContext() const; 00141 00142 /// isVarArg - Return true if this function takes a variable number of 00143 /// arguments. 00144 bool isVarArg() const; 00145 00146 /// getIntrinsicID - This method returns the ID number of the specified 00147 /// function, or Intrinsic::not_intrinsic if the function is not an 00148 /// intrinsic, or if the pointer is null. This value is always defined to be 00149 /// zero to allow easy checking for whether a function is intrinsic or not. 00150 /// The particular intrinsic functions which correspond to this value are 00151 /// defined in llvm/Intrinsics.h. Results are cached in the LLVM context, 00152 /// subsequent requests for the same ID return results much faster from the 00153 /// cache. 00154 /// 00155 unsigned getIntrinsicID() const LLVM_READONLY; 00156 bool isIntrinsic() const { return getName().startswith("llvm."); } 00157 00158 /// getCallingConv()/setCallingConv(CC) - These method get and set the 00159 /// calling convention of this function. The enum values for the known 00160 /// calling conventions are defined in CallingConv.h. 00161 CallingConv::ID getCallingConv() const { 00162 return static_cast<CallingConv::ID>(getSubclassDataFromValue() >> 1); 00163 } 00164 void setCallingConv(CallingConv::ID CC) { 00165 setValueSubclassData((getSubclassDataFromValue() & 1) | 00166 (static_cast<unsigned>(CC) << 1)); 00167 } 00168 00169 /// getAttributes - Return the attribute list for this Function. 00170 /// 00171 AttributeSet getAttributes() const { return AttributeSets; } 00172 00173 /// setAttributes - Set the attribute list for this Function. 00174 /// 00175 void setAttributes(AttributeSet attrs) { AttributeSets = attrs; } 00176 00177 /// addFnAttr - Add function attributes to this function. 00178 /// 00179 void addFnAttr(Attribute::AttrKind N) { 00180 setAttributes(AttributeSets.addAttribute(getContext(), 00181 AttributeSet::FunctionIndex, N)); 00182 } 00183 00184 /// addFnAttr - Add function attributes to this function. 00185 /// 00186 void addFnAttr(StringRef Kind) { 00187 setAttributes( 00188 AttributeSets.addAttribute(getContext(), 00189 AttributeSet::FunctionIndex, Kind)); 00190 } 00191 00192 /// \brief Return true if the function has the attribute. 00193 bool hasFnAttribute(Attribute::AttrKind Kind) const { 00194 return AttributeSets.hasAttribute(AttributeSet::FunctionIndex, Kind); 00195 } 00196 bool hasFnAttribute(StringRef Kind) const { 00197 return AttributeSets.hasAttribute(AttributeSet::FunctionIndex, Kind); 00198 } 00199 00200 /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm 00201 /// to use during code generation. 00202 bool hasGC() const; 00203 const char *getGC() const; 00204 void setGC(const char *Str); 00205 void clearGC(); 00206 00207 /// @brief adds the attribute to the list of attributes. 00208 void addAttribute(unsigned i, Attribute::AttrKind attr); 00209 00210 /// @brief adds the attributes to the list of attributes. 00211 void addAttributes(unsigned i, AttributeSet attrs); 00212 00213 /// @brief removes the attributes from the list of attributes. 00214 void removeAttributes(unsigned i, AttributeSet attr); 00215 00216 /// @brief Extract the alignment for a call or parameter (0=unknown). 00217 unsigned getParamAlignment(unsigned i) const { 00218 return AttributeSets.getParamAlignment(i); 00219 } 00220 00221 /// @brief Determine if the function does not access memory. 00222 bool doesNotAccessMemory() const { 00223 return AttributeSets.hasAttribute(AttributeSet::FunctionIndex, 00224 Attribute::ReadNone); 00225 } 00226 void setDoesNotAccessMemory() { 00227 addFnAttr(Attribute::ReadNone); 00228 } 00229 00230 /// @brief Determine if the function does not access or only reads memory. 00231 bool onlyReadsMemory() const { 00232 return doesNotAccessMemory() || 00233 AttributeSets.hasAttribute(AttributeSet::FunctionIndex, 00234 Attribute::ReadOnly); 00235 } 00236 void setOnlyReadsMemory() { 00237 addFnAttr(Attribute::ReadOnly); 00238 } 00239 00240 /// @brief Determine if the function cannot return. 00241 bool doesNotReturn() const { 00242 return AttributeSets.hasAttribute(AttributeSet::FunctionIndex, 00243 Attribute::NoReturn); 00244 } 00245 void setDoesNotReturn() { 00246 addFnAttr(Attribute::NoReturn); 00247 } 00248 00249 /// @brief Determine if the function cannot unwind. 00250 bool doesNotThrow() const { 00251 return AttributeSets.hasAttribute(AttributeSet::FunctionIndex, 00252 Attribute::NoUnwind); 00253 } 00254 void setDoesNotThrow() { 00255 addFnAttr(Attribute::NoUnwind); 00256 } 00257 00258 /// @brief Determine if the call cannot be duplicated. 00259 bool cannotDuplicate() const { 00260 return AttributeSets.hasAttribute(AttributeSet::FunctionIndex, 00261 Attribute::NoDuplicate); 00262 } 00263 void setCannotDuplicate() { 00264 addFnAttr(Attribute::NoDuplicate); 00265 } 00266 00267 /// @brief True if the ABI mandates (or the user requested) that this 00268 /// function be in a unwind table. 00269 bool hasUWTable() const { 00270 return AttributeSets.hasAttribute(AttributeSet::FunctionIndex, 00271 Attribute::UWTable); 00272 } 00273 void setHasUWTable() { 00274 addFnAttr(Attribute::UWTable); 00275 } 00276 00277 /// @brief True if this function needs an unwind table. 00278 bool needsUnwindTableEntry() const { 00279 return hasUWTable() || !doesNotThrow(); 00280 } 00281 00282 /// @brief Determine if the function returns a structure through first 00283 /// pointer argument. 00284 bool hasStructRetAttr() const { 00285 return AttributeSets.hasAttribute(1, Attribute::StructRet); 00286 } 00287 00288 /// @brief Determine if the parameter does not alias other parameters. 00289 /// @param n The parameter to check. 1 is the first parameter, 0 is the return 00290 bool doesNotAlias(unsigned n) const { 00291 return AttributeSets.hasAttribute(n, Attribute::NoAlias); 00292 } 00293 void setDoesNotAlias(unsigned n) { 00294 addAttribute(n, Attribute::NoAlias); 00295 } 00296 00297 /// @brief Determine if the parameter can be captured. 00298 /// @param n The parameter to check. 1 is the first parameter, 0 is the return 00299 bool doesNotCapture(unsigned n) const { 00300 return AttributeSets.hasAttribute(n, Attribute::NoCapture); 00301 } 00302 void setDoesNotCapture(unsigned n) { 00303 addAttribute(n, Attribute::NoCapture); 00304 } 00305 00306 /// copyAttributesFrom - copy all additional attributes (those not needed to 00307 /// create a Function) from the Function Src to this one. 00308 void copyAttributesFrom(const GlobalValue *Src); 00309 00310 /// deleteBody - This method deletes the body of the function, and converts 00311 /// the linkage to external. 00312 /// 00313 void deleteBody() { 00314 dropAllReferences(); 00315 setLinkage(ExternalLinkage); 00316 } 00317 00318 /// removeFromParent - This method unlinks 'this' from the containing module, 00319 /// but does not delete it. 00320 /// 00321 virtual void removeFromParent(); 00322 00323 /// eraseFromParent - This method unlinks 'this' from the containing module 00324 /// and deletes it. 00325 /// 00326 virtual void eraseFromParent(); 00327 00328 00329 /// Get the underlying elements of the Function... the basic block list is 00330 /// empty for external functions. 00331 /// 00332 const ArgumentListType &getArgumentList() const { 00333 CheckLazyArguments(); 00334 return ArgumentList; 00335 } 00336 ArgumentListType &getArgumentList() { 00337 CheckLazyArguments(); 00338 return ArgumentList; 00339 } 00340 static iplist<Argument> Function::*getSublistAccess(Argument*) { 00341 return &Function::ArgumentList; 00342 } 00343 00344 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; } 00345 BasicBlockListType &getBasicBlockList() { return BasicBlocks; } 00346 static iplist<BasicBlock> Function::*getSublistAccess(BasicBlock*) { 00347 return &Function::BasicBlocks; 00348 } 00349 00350 const BasicBlock &getEntryBlock() const { return front(); } 00351 BasicBlock &getEntryBlock() { return front(); } 00352 00353 //===--------------------------------------------------------------------===// 00354 // Symbol Table Accessing functions... 00355 00356 /// getSymbolTable() - Return the symbol table... 00357 /// 00358 inline ValueSymbolTable &getValueSymbolTable() { return *SymTab; } 00359 inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; } 00360 00361 00362 //===--------------------------------------------------------------------===// 00363 // BasicBlock iterator forwarding functions 00364 // 00365 iterator begin() { return BasicBlocks.begin(); } 00366 const_iterator begin() const { return BasicBlocks.begin(); } 00367 iterator end () { return BasicBlocks.end(); } 00368 const_iterator end () const { return BasicBlocks.end(); } 00369 00370 size_t size() const { return BasicBlocks.size(); } 00371 bool empty() const { return BasicBlocks.empty(); } 00372 const BasicBlock &front() const { return BasicBlocks.front(); } 00373 BasicBlock &front() { return BasicBlocks.front(); } 00374 const BasicBlock &back() const { return BasicBlocks.back(); } 00375 BasicBlock &back() { return BasicBlocks.back(); } 00376 00377 //===--------------------------------------------------------------------===// 00378 // Argument iterator forwarding functions 00379 // 00380 arg_iterator arg_begin() { 00381 CheckLazyArguments(); 00382 return ArgumentList.begin(); 00383 } 00384 const_arg_iterator arg_begin() const { 00385 CheckLazyArguments(); 00386 return ArgumentList.begin(); 00387 } 00388 arg_iterator arg_end() { 00389 CheckLazyArguments(); 00390 return ArgumentList.end(); 00391 } 00392 const_arg_iterator arg_end() const { 00393 CheckLazyArguments(); 00394 return ArgumentList.end(); 00395 } 00396 00397 size_t arg_size() const; 00398 bool arg_empty() const; 00399 00400 /// viewCFG - This function is meant for use from the debugger. You can just 00401 /// say 'call F->viewCFG()' and a ghostview window should pop up from the 00402 /// program, displaying the CFG of the current function with the code for each 00403 /// basic block inside. This depends on there being a 'dot' and 'gv' program 00404 /// in your path. 00405 /// 00406 void viewCFG() const; 00407 00408 /// viewCFGOnly - This function is meant for use from the debugger. It works 00409 /// just like viewCFG, but it does not include the contents of basic blocks 00410 /// into the nodes, just the label. If you are only interested in the CFG 00411 /// this can make the graph smaller. 00412 /// 00413 void viewCFGOnly() const; 00414 00415 /// Methods for support type inquiry through isa, cast, and dyn_cast: 00416 static inline bool classof(const Value *V) { 00417 return V->getValueID() == Value::FunctionVal; 00418 } 00419 00420 /// dropAllReferences() - This method causes all the subinstructions to "let 00421 /// go" of all references that they are maintaining. This allows one to 00422 /// 'delete' a whole module at a time, even though there may be circular 00423 /// references... first all references are dropped, and all use counts go to 00424 /// zero. Then everything is deleted for real. Note that no operations are 00425 /// valid on an object that has "dropped all references", except operator 00426 /// delete. 00427 /// 00428 /// Since no other object in the module can have references into the body of a 00429 /// function, dropping all references deletes the entire body of the function, 00430 /// including any contained basic blocks. 00431 /// 00432 void dropAllReferences(); 00433 00434 /// hasAddressTaken - returns true if there are any uses of this function 00435 /// other than direct calls or invokes to it, or blockaddress expressions. 00436 /// Optionally passes back an offending user for diagnostic purposes. 00437 /// 00438 bool hasAddressTaken(const User** = 0) const; 00439 00440 /// isDefTriviallyDead - Return true if it is trivially safe to remove 00441 /// this function definition from the module (because it isn't externally 00442 /// visible, does not have its address taken, and has no callers). To make 00443 /// this more accurate, call removeDeadConstantUsers first. 00444 bool isDefTriviallyDead() const; 00445 00446 /// callsFunctionThatReturnsTwice - Return true if the function has a call to 00447 /// setjmp or other function that gcc recognizes as "returning twice". 00448 bool callsFunctionThatReturnsTwice() const; 00449 00450 private: 00451 // Shadow Value::setValueSubclassData with a private forwarding method so that 00452 // subclasses cannot accidentally use it. 00453 void setValueSubclassData(unsigned short D) { 00454 Value::setValueSubclassData(D); 00455 } 00456 }; 00457 00458 inline ValueSymbolTable * 00459 ilist_traits<BasicBlock>::getSymTab(Function *F) { 00460 return F ? &F->getValueSymbolTable() : 0; 00461 } 00462 00463 inline ValueSymbolTable * 00464 ilist_traits<Argument>::getSymTab(Function *F) { 00465 return F ? &F->getValueSymbolTable() : 0; 00466 } 00467 00468 } // End llvm namespace 00469 00470 #endif