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