Function.h revision 49269d9e7eb5ce6e1a84a10ca0bdeab044d0db3d
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 26namespace llvm { 27 28class FunctionType; 29class ParamAttrsList; 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 { 55public: 56 typedef iplist<Argument> ArgumentListType; 57 typedef iplist<BasicBlock> BasicBlockListType; 58 59 // BasicBlock iterators... 60 typedef BasicBlockListType::iterator iterator; 61 typedef BasicBlockListType::const_iterator const_iterator; 62 63 typedef ArgumentListType::iterator arg_iterator; 64 typedef ArgumentListType::const_iterator const_arg_iterator; 65 66private: 67 // Important things that make up a function! 68 BasicBlockListType BasicBlocks; ///< The basic blocks 69 mutable ArgumentListType ArgumentList; ///< The formal arguments 70 ValueSymbolTable *SymTab; ///< Symbol table of args/instructions 71 const ParamAttrsList *ParamAttrs; ///< Parameter attributes 72 73 // The Calling Convention is stored in Value::SubclassData. 74 /*unsigned CallingConvention;*/ 75 76 friend class SymbolTableListTraits<Function, Module>; 77 78 void setParent(Module *parent); 79 Function *Prev, *Next; 80 void setNext(Function *N) { Next = N; } 81 void setPrev(Function *N) { Prev = N; } 82 83 // getNext/Prev - Return the next or previous function in the list. These 84 // methods should never be used directly, and are only used to implement the 85 // function list as part of the module. 86 // 87 Function *getNext() { return Next; } 88 const Function *getNext() const { return Next; } 89 Function *getPrev() { return Prev; } 90 const Function *getPrev() const { return Prev; } 91 92 /// hasLazyArguments/CheckLazyArguments - The argument list of a function is 93 /// built on demand, so that the list isn't allocated until the first client 94 /// needs it. The hasLazyArguments predicate returns true if the arg list 95 /// hasn't been set up yet. 96 bool hasLazyArguments() const { 97 return SubclassData & 1; 98 } 99 void CheckLazyArguments() const { 100 if (hasLazyArguments()) 101 BuildLazyArguments(); 102 } 103 void BuildLazyArguments() const; 104 105 Function(const Function&); // DO NOT IMPLEMENT 106 void operator=(const Function&); // DO NOT IMPLEMENT 107public: 108 /// Function ctor - If the (optional) Module argument is specified, the 109 /// function is automatically inserted into the end of the function list for 110 /// the module. 111 /// 112 Function(const FunctionType *Ty, LinkageTypes Linkage, 113 const std::string &N = "", Module *M = 0); 114 ~Function(); 115 116 const Type *getReturnType() const; // Return the type of the ret val 117 const FunctionType *getFunctionType() const; // Return the FunctionType for me 118 119 /// isVarArg - Return true if this function takes a variable number of 120 /// arguments. 121 bool isVarArg() const; 122 123 /// isDeclaration - Is the body of this function unknown? (The basic block 124 /// list is empty if so.) This is true for function declarations, but not 125 /// true for function definitions. 126 /// 127 virtual bool isDeclaration() const { return BasicBlocks.empty(); } 128 129 /// getIntrinsicID - This method returns the ID number of the specified 130 /// function, or Intrinsic::not_intrinsic if the function is not an 131 /// instrinsic, or if the pointer is null. This value is always defined to be 132 /// zero to allow easy checking for whether a function is intrinsic or not. 133 /// The particular intrinsic functions which correspond to this value are 134 /// defined in llvm/Intrinsics.h. 135 /// 136 unsigned getIntrinsicID(bool noAssert = false) const; 137 bool isIntrinsic() const { return getIntrinsicID() != 0; } 138 139 /// getCallingConv()/setCallingConv(uint) - These method get and set the 140 /// calling convention of this function. The enum values for the known 141 /// calling conventions are defined in CallingConv.h. 142 unsigned getCallingConv() const { return SubclassData >> 1; } 143 void setCallingConv(unsigned CC) { 144 SubclassData = (SubclassData & 1) | (CC << 1); 145 } 146 147 /// Obtains a constant pointer to the ParamAttrsList object which holds the 148 /// parameter attributes information, if any. 149 /// @returns 0 if no parameter attributes have been set. 150 /// @brief Get the parameter attributes. 151 const ParamAttrsList *getParamAttrs() const { return ParamAttrs; } 152 153 /// Sets the parameter attributes for this Function. To construct a 154 /// ParamAttrsList, see ParameterAttributes.h 155 /// @brief Set the parameter attributes. 156 void setParamAttrs(const ParamAttrsList *attrs); 157 158 /// hasCollector/getCollector/setCollector/clearCollector - The name of the 159 /// garbage collection algorithm to use during code generation. 160 bool hasCollector() const; 161 const char *getCollector() const; 162 void setCollector(const char *Str); 163 void clearCollector(); 164 165 /// @brief Determine whether the function has the given attribute. 166 bool paramHasAttr(uint16_t i, unsigned attr) const; 167 168 /// @brief Determine if the function cannot return. 169 bool doesNotReturn() const; 170 171 /// @brief Determine if the function cannot unwind. 172 bool doesNotThrow() const; 173 174 /// @brief Determine if the function does not access memory. 175 bool doesNotAccessMemory() const; 176 177 /// @brief Determine if the function does not access or only reads memory. 178 bool onlyReadsMemory() const; 179 180 /// @brief Determine if the function returns a structure. 181 bool isStructReturn() const; 182 183 /// deleteBody - This method deletes the body of the function, and converts 184 /// the linkage to external. 185 /// 186 void deleteBody() { 187 dropAllReferences(); 188 setLinkage(ExternalLinkage); 189 } 190 191 /// removeFromParent - This method unlinks 'this' from the containing module, 192 /// but does not delete it. 193 /// 194 void removeFromParent(); 195 196 /// eraseFromParent - This method unlinks 'this' from the containing module 197 /// and deletes it. 198 /// 199 void eraseFromParent(); 200 201 202 /// Get the underlying elements of the Function... the basic block list is 203 /// empty for external functions. 204 /// 205 const ArgumentListType &getArgumentList() const { 206 CheckLazyArguments(); 207 return ArgumentList; 208 } 209 ArgumentListType &getArgumentList() { 210 CheckLazyArguments(); 211 return ArgumentList; 212 } 213 214 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; } 215 BasicBlockListType &getBasicBlockList() { return BasicBlocks; } 216 217 const BasicBlock &getEntryBlock() const { return front(); } 218 BasicBlock &getEntryBlock() { return front(); } 219 220 //===--------------------------------------------------------------------===// 221 // Symbol Table Accessing functions... 222 223 /// getSymbolTable() - Return the symbol table... 224 /// 225 inline ValueSymbolTable &getValueSymbolTable() { return *SymTab; } 226 inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; } 227 228 229 //===--------------------------------------------------------------------===// 230 // BasicBlock iterator forwarding functions 231 // 232 iterator begin() { return BasicBlocks.begin(); } 233 const_iterator begin() const { return BasicBlocks.begin(); } 234 iterator end () { return BasicBlocks.end(); } 235 const_iterator end () const { return BasicBlocks.end(); } 236 237 size_t size() const { return BasicBlocks.size(); } 238 bool empty() const { return BasicBlocks.empty(); } 239 const BasicBlock &front() const { return BasicBlocks.front(); } 240 BasicBlock &front() { return BasicBlocks.front(); } 241 const BasicBlock &back() const { return BasicBlocks.back(); } 242 BasicBlock &back() { return BasicBlocks.back(); } 243 244 //===--------------------------------------------------------------------===// 245 // Argument iterator forwarding functions 246 // 247 arg_iterator arg_begin() { 248 CheckLazyArguments(); 249 return ArgumentList.begin(); 250 } 251 const_arg_iterator arg_begin() const { 252 CheckLazyArguments(); 253 return ArgumentList.begin(); 254 } 255 arg_iterator arg_end() { 256 CheckLazyArguments(); 257 return ArgumentList.end(); 258 } 259 const_arg_iterator arg_end() const { 260 CheckLazyArguments(); 261 return ArgumentList.end(); 262 } 263 264 size_t arg_size() const; 265 bool arg_empty() const; 266 267 virtual void print(std::ostream &OS) const { print(OS, 0); } 268 void print(std::ostream *OS) const { if (OS) print(*OS); } 269 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const; 270 271 /// viewCFG - This function is meant for use from the debugger. You can just 272 /// say 'call F->viewCFG()' and a ghostview window should pop up from the 273 /// program, displaying the CFG of the current function with the code for each 274 /// basic block inside. This depends on there being a 'dot' and 'gv' program 275 /// in your path. 276 /// 277 void viewCFG() const; 278 279 /// viewCFGOnly - This function is meant for use from the debugger. It works 280 /// just like viewCFG, but it does not include the contents of basic blocks 281 /// into the nodes, just the label. If you are only interested in the CFG 282 /// this can make the graph smaller. 283 /// 284 void viewCFGOnly() const; 285 286 /// Methods for support type inquiry through isa, cast, and dyn_cast: 287 static inline bool classof(const Function *) { return true; } 288 static inline bool classof(const Value *V) { 289 return V->getValueID() == Value::FunctionVal; 290 } 291 292 /// dropAllReferences() - This method causes all the subinstructions to "let 293 /// go" of all references that they are maintaining. This allows one to 294 /// 'delete' a whole module at a time, even though there may be circular 295 /// references... first all references are dropped, and all use counts go to 296 /// zero. Then everything is deleted for real. Note that no operations are 297 /// valid on an object that has "dropped all references", except operator 298 /// delete. 299 /// 300 /// Since no other object in the module can have references into the body of a 301 /// function, dropping all references deletes the entire body of the function, 302 /// including any contained basic blocks. 303 /// 304 void dropAllReferences(); 305 306 static unsigned getBasicBlockListOffset() { 307 Function *Obj = 0; 308 return unsigned(reinterpret_cast<uintptr_t>(&Obj->BasicBlocks)); 309 } 310 static unsigned getArgumentListOffset() { 311 Function *Obj = 0; 312 return unsigned(reinterpret_cast<uintptr_t>(&Obj->ArgumentList)); 313 } 314}; 315 316inline ValueSymbolTable * 317ilist_traits<BasicBlock>::getSymTab(Function *F) { 318 return F ? &F->getValueSymbolTable() : 0; 319} 320 321inline ValueSymbolTable * 322ilist_traits<Argument>::getSymTab(Function *F) { 323 return F ? &F->getValueSymbolTable() : 0; 324} 325 326inline int 327ilist_traits<BasicBlock>::getListOffset() { 328 return Function::getBasicBlockListOffset(); 329} 330 331inline int 332ilist_traits<Argument>::getListOffset() { 333 return Function::getArgumentListOffset(); 334} 335 336 337} // End llvm namespace 338 339#endif 340