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