Function.h revision 4d452de1506344bf1d28bb4a8c4ddb78c51a28c8
1//===-- llvm/Function.h - Class to represent a single function --*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file contains the declaration of the Function class, which represents a 11// single function/procedure in LLVM. 12// 13// A function basically consists of a list of basic blocks, a list of arguments, 14// and a symbol table. 15// 16//===----------------------------------------------------------------------===// 17 18#ifndef LLVM_FUNCTION_H 19#define LLVM_FUNCTION_H 20 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" 26 27namespace llvm { 28 29class FunctionType; 30 31// Traits for intrusive list of instructions... 32template<> struct ilist_traits<BasicBlock> 33 : public SymbolTableListTraits<BasicBlock, Function> { 34 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(); 41}; 42 43template<> struct ilist_traits<Argument> 44 : public SymbolTableListTraits<Argument, Function> { 45 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(); 52}; 53 54class Function : public GlobalValue, public Annotable, 55 public ilist_node<Function> { 56public: 57 typedef iplist<Argument> ArgumentListType; 58 typedef iplist<BasicBlock> BasicBlockListType; 59 60 // BasicBlock iterators... 61 typedef BasicBlockListType::iterator iterator; 62 typedef BasicBlockListType::const_iterator const_iterator; 63 64 typedef ArgumentListType::iterator arg_iterator; 65 typedef ArgumentListType::const_iterator const_arg_iterator; 66 67private: 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 73 74 // The Calling Convention is stored in Value::SubclassData. 75 /*unsigned CallingConvention;*/ 76 77 friend class SymbolTableListTraits<Function, Module>; 78 79 void setParent(Module *parent); 80 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; 87 } 88 void CheckLazyArguments() const { 89 if (hasLazyArguments()) 90 BuildLazyArguments(); 91 } 92 void BuildLazyArguments() const; 93 94 Function(const Function&); // DO NOT IMPLEMENT 95 void operator=(const Function&); // DO NOT IMPLEMENT 96 97 /// Function ctor - If the (optional) Module argument is specified, the 98 /// function is automatically inserted into the end of the function list for 99 /// the module. 100 /// 101 Function(const FunctionType *Ty, LinkageTypes Linkage, 102 const std::string &N = "", Module *M = 0); 103 104public: 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); 108 } 109 110 ~Function(); 111 112 const Type *getReturnType() const; // Return the type of the ret val 113 const FunctionType *getFunctionType() const; // Return the FunctionType for me 114 115 /// isVarArg - Return true if this function takes a variable number of 116 /// arguments. 117 bool isVarArg() const; 118 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. 122 /// 123 virtual bool isDeclaration() const { return BasicBlocks.empty(); } 124 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. 131 /// 132 unsigned getIntrinsicID(bool noAssert = false) const; 133 bool isIntrinsic() const { return getIntrinsicID() != 0; } 134 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); 141 } 142 143 /// getAttributes - Return the attribute list for this Function. 144 /// 145 const AttrListPtr &getAttributes() const { return AttributeList; } 146 147 /// setAttributes - Set the attribute list for this Function. 148 /// 149 void setAttributes(const AttrListPtr &attrs) { AttributeList = attrs; } 150 151 /// hasFnAttr - Return true if this function has the given attribute. 152 bool hasFnAttr(Attributes N) const { 153 // Function Attributes are stored at ~0 index 154 return AttributeList.paramHasAttr(~0U, N); 155 } 156 157 /// addFnAttr - Add function attributes to this function. 158 /// 159 void addFnAttr(Attributes N) { 160 // Function Attributes are stored at ~0 index 161 addAttribute(~0U, N); 162 } 163 164 /// removeFnAttr - Remove function attributes from this function. 165 /// 166 void removeFnAttr(Attributes N) { 167 // Function Attributes are stored at ~0 index 168 removeAttribute(~0U, N); 169 } 170 171 /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm 172 /// to use during code generation. 173 bool hasGC() const; 174 const char *getGC() const; 175 void setGC(const char *Str); 176 void clearGC(); 177 178 /// @brief Determine whether the function has the given attribute. 179 bool paramHasAttr(unsigned i, Attributes attr) const { 180 return AttributeList.paramHasAttr(i, attr); 181 } 182 183 /// addAttribute - adds the attribute to the list of attributes. 184 void addAttribute(unsigned i, Attributes attr); 185 186 /// removeAttribute - removes the attribute from the list of attributes. 187 void removeAttribute(unsigned i, Attributes attr); 188 189 /// @brief Extract the alignment for a call or parameter (0=unknown). 190 unsigned getParamAlignment(unsigned i) const { 191 return AttributeList.getParamAlignment(i); 192 } 193 194 /// @brief Determine if the function does not access memory. 195 bool doesNotAccessMemory() const { 196 return hasFnAttr(Attribute::ReadNone); 197 } 198 void setDoesNotAccessMemory(bool DoesNotAccessMemory = true) { 199 if (DoesNotAccessMemory) addFnAttr(Attribute::ReadNone); 200 else removeFnAttr(Attribute::ReadNone); 201 } 202 203 /// @brief Determine if the function does not access or only reads memory. 204 bool onlyReadsMemory() const { 205 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly); 206 } 207 void setOnlyReadsMemory(bool OnlyReadsMemory = true) { 208 if (OnlyReadsMemory) addFnAttr(Attribute::ReadOnly); 209 else removeFnAttr(Attribute::ReadOnly | Attribute::ReadNone); 210 } 211 212 /// @brief Determine if the function cannot return. 213 bool doesNotReturn() const { 214 return hasFnAttr(Attribute::NoReturn); 215 } 216 void setDoesNotReturn(bool DoesNotReturn = true) { 217 if (DoesNotReturn) addFnAttr(Attribute::NoReturn); 218 else removeFnAttr(Attribute::NoReturn); 219 } 220 221 /// @brief Determine if the function cannot unwind. 222 bool doesNotThrow() const { 223 return hasFnAttr(Attribute::NoUnwind); 224 } 225 void setDoesNotThrow(bool DoesNotThrow = true) { 226 if (DoesNotThrow) addFnAttr(Attribute::NoUnwind); 227 else removeFnAttr(Attribute::NoUnwind); 228 } 229 230 /// @brief Determine if the function returns a structure through first 231 /// pointer argument. 232 bool hasStructRetAttr() const { 233 return paramHasAttr(1, Attribute::StructRet); 234 } 235 236 /// @brief Determine if the parameter does not alias other parameters. 237 /// @param n The parameter to check. 1 is the first parameter, 0 is the return 238 bool doesNotAlias(unsigned n) const { 239 return paramHasAttr(n, Attribute::NoAlias); 240 } 241 void setDoesNotAlias(unsigned n, bool DoesNotAlias = true) { 242 if (DoesNotAlias) addAttribute(n, Attribute::NoAlias); 243 else removeAttribute(n, Attribute::NoAlias); 244 } 245 246 /// @brief Determine if the parameter can be captured. 247 /// @param n The parameter to check. 1 is the first parameter, 0 is the return 248 bool doesNotCapture(unsigned n) const { 249 return paramHasAttr(n, Attribute::NoCapture); 250 } 251 void setDoesNotCapture(unsigned n, bool DoesNotCapture = true) { 252 if (DoesNotCapture) addAttribute(n, Attribute::NoCapture); 253 else removeAttribute(n, Attribute::NoCapture); 254 } 255 256 /// copyAttributesFrom - copy all additional attributes (those not needed to 257 /// create a Function) from the Function Src to this one. 258 void copyAttributesFrom(const GlobalValue *Src); 259 260 /// deleteBody - This method deletes the body of the function, and converts 261 /// the linkage to external. 262 /// 263 void deleteBody() { 264 dropAllReferences(); 265 setLinkage(ExternalLinkage); 266 } 267 268 /// removeFromParent - This method unlinks 'this' from the containing module, 269 /// but does not delete it. 270 /// 271 virtual void removeFromParent(); 272 273 /// eraseFromParent - This method unlinks 'this' from the containing module 274 /// and deletes it. 275 /// 276 virtual void eraseFromParent(); 277 278 279 /// Get the underlying elements of the Function... the basic block list is 280 /// empty for external functions. 281 /// 282 const ArgumentListType &getArgumentList() const { 283 CheckLazyArguments(); 284 return ArgumentList; 285 } 286 ArgumentListType &getArgumentList() { 287 CheckLazyArguments(); 288 return ArgumentList; 289 } 290 291 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; } 292 BasicBlockListType &getBasicBlockList() { return BasicBlocks; } 293 294 const BasicBlock &getEntryBlock() const { return front(); } 295 BasicBlock &getEntryBlock() { return front(); } 296 297 //===--------------------------------------------------------------------===// 298 // Symbol Table Accessing functions... 299 300 /// getSymbolTable() - Return the symbol table... 301 /// 302 inline ValueSymbolTable &getValueSymbolTable() { return *SymTab; } 303 inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; } 304 305 306 //===--------------------------------------------------------------------===// 307 // BasicBlock iterator forwarding functions 308 // 309 iterator begin() { return BasicBlocks.begin(); } 310 const_iterator begin() const { return BasicBlocks.begin(); } 311 iterator end () { return BasicBlocks.end(); } 312 const_iterator end () const { return BasicBlocks.end(); } 313 314 size_t size() const { return BasicBlocks.size(); } 315 bool empty() const { return BasicBlocks.empty(); } 316 const BasicBlock &front() const { return BasicBlocks.front(); } 317 BasicBlock &front() { return BasicBlocks.front(); } 318 const BasicBlock &back() const { return BasicBlocks.back(); } 319 BasicBlock &back() { return BasicBlocks.back(); } 320 321 //===--------------------------------------------------------------------===// 322 // Argument iterator forwarding functions 323 // 324 arg_iterator arg_begin() { 325 CheckLazyArguments(); 326 return ArgumentList.begin(); 327 } 328 const_arg_iterator arg_begin() const { 329 CheckLazyArguments(); 330 return ArgumentList.begin(); 331 } 332 arg_iterator arg_end() { 333 CheckLazyArguments(); 334 return ArgumentList.end(); 335 } 336 const_arg_iterator arg_end() const { 337 CheckLazyArguments(); 338 return ArgumentList.end(); 339 } 340 341 size_t arg_size() const; 342 bool arg_empty() const; 343 344 /// viewCFG - This function is meant for use from the debugger. You can just 345 /// say 'call F->viewCFG()' and a ghostview window should pop up from the 346 /// program, displaying the CFG of the current function with the code for each 347 /// basic block inside. This depends on there being a 'dot' and 'gv' program 348 /// in your path. 349 /// 350 void viewCFG() const; 351 352 /// viewCFGOnly - This function is meant for use from the debugger. It works 353 /// just like viewCFG, but it does not include the contents of basic blocks 354 /// into the nodes, just the label. If you are only interested in the CFG 355 /// this can make the graph smaller. 356 /// 357 void viewCFGOnly() const; 358 359 /// Methods for support type inquiry through isa, cast, and dyn_cast: 360 static inline bool classof(const Function *) { return true; } 361 static inline bool classof(const Value *V) { 362 return V->getValueID() == Value::FunctionVal; 363 } 364 365 /// dropAllReferences() - This method causes all the subinstructions to "let 366 /// go" of all references that they are maintaining. This allows one to 367 /// 'delete' a whole module at a time, even though there may be circular 368 /// references... first all references are dropped, and all use counts go to 369 /// zero. Then everything is deleted for real. Note that no operations are 370 /// valid on an object that has "dropped all references", except operator 371 /// delete. 372 /// 373 /// Since no other object in the module can have references into the body of a 374 /// function, dropping all references deletes the entire body of the function, 375 /// including any contained basic blocks. 376 /// 377 void dropAllReferences(); 378 379 static unsigned getBasicBlockListOffset() { 380 Function *Obj = 0; 381 return unsigned(reinterpret_cast<uintptr_t>(&Obj->BasicBlocks)); 382 } 383 static unsigned getArgumentListOffset() { 384 Function *Obj = 0; 385 return unsigned(reinterpret_cast<uintptr_t>(&Obj->ArgumentList)); 386 } 387}; 388 389inline ValueSymbolTable * 390ilist_traits<BasicBlock>::getSymTab(Function *F) { 391 return F ? &F->getValueSymbolTable() : 0; 392} 393 394inline ValueSymbolTable * 395ilist_traits<Argument>::getSymTab(Function *F) { 396 return F ? &F->getValueSymbolTable() : 0; 397} 398 399inline int 400ilist_traits<BasicBlock>::getListOffset() { 401 return Function::getBasicBlockListOffset(); 402} 403 404inline int 405ilist_traits<Argument>::getListOffset() { 406 return Function::getArgumentListOffset(); 407} 408 409 410} // End llvm namespace 411 412#endif 413