LLVM API Documentation
00001 //===-- llvm/Function.h - Class to represent a single function --*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source 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 00026 namespace llvm { 00027 00028 class FunctionType; 00029 00030 // Traits for intrusive list of instructions... 00031 template<> struct ilist_traits<BasicBlock> 00032 : public SymbolTableListTraits<BasicBlock, Function, Function> { 00033 00034 // createNode is used to create a node that marks the end of the list... 00035 static BasicBlock *createNode(); 00036 00037 static iplist<BasicBlock> &getList(Function *F); 00038 }; 00039 00040 template<> struct ilist_traits<Argument> 00041 : public SymbolTableListTraits<Argument, Function, Function> { 00042 00043 // createNode is used to create a node that marks the end of the list... 00044 static Argument *createNode(); 00045 static iplist<Argument> &getList(Function *F); 00046 }; 00047 00048 class Function : public GlobalValue, public Annotable { 00049 public: 00050 typedef iplist<Argument> ArgumentListType; 00051 typedef iplist<BasicBlock> BasicBlockListType; 00052 00053 // BasicBlock iterators... 00054 typedef BasicBlockListType::iterator iterator; 00055 typedef BasicBlockListType::const_iterator const_iterator; 00056 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00057 typedef std::reverse_iterator<iterator> reverse_iterator; 00058 00059 typedef ArgumentListType::iterator aiterator; 00060 typedef ArgumentListType::const_iterator const_aiterator; 00061 typedef std::reverse_iterator<const_aiterator> const_reverse_aiterator; 00062 typedef std::reverse_iterator<aiterator> reverse_aiterator; 00063 00064 private: 00065 // Important things that make up a function! 00066 BasicBlockListType BasicBlocks; // The basic blocks 00067 ArgumentListType ArgumentList; // The formal arguments 00068 00069 SymbolTable *SymTab; 00070 00071 friend class SymbolTableListTraits<Function, Module, Module>; 00072 00073 void setParent(Module *parent); 00074 Function *Prev, *Next; 00075 void setNext(Function *N) { Next = N; } 00076 void setPrev(Function *N) { Prev = N; } 00077 00078 public: 00079 /// Function ctor - If the (optional) Module argument is specified, the 00080 /// function is automatically inserted into the end of the function list for 00081 /// the module. 00082 /// 00083 Function(const FunctionType *Ty, LinkageTypes Linkage, 00084 const std::string &N = "", Module *M = 0); 00085 ~Function(); 00086 00087 // Specialize setName to handle symbol table majik... 00088 virtual void setName(const std::string &name, SymbolTable *ST = 0); 00089 00090 const Type *getReturnType() const; // Return the type of the ret val 00091 const FunctionType *getFunctionType() const; // Return the FunctionType for me 00092 00093 /// isExternal - Is the body of this function unknown? (The basic block list 00094 /// is empty if so.) This is true for external functions, defined as forward 00095 /// "declare"ations 00096 /// 00097 virtual bool isExternal() const { return BasicBlocks.empty(); } 00098 00099 /// getIntrinsicID - This method returns the ID number of the specified 00100 /// function, or Intrinsic::not_intrinsic if the function is not an 00101 /// instrinsic, or if the pointer is null. This value is always defined to be 00102 /// zero to allow easy checking for whether a function is intrinsic or not. 00103 /// The particular intrinsic functions which correspond to this value are 00104 /// defined in llvm/Intrinsics.h. 00105 /// 00106 unsigned getIntrinsicID() const; 00107 bool isIntrinsic() const { return getIntrinsicID() != 0; } 00108 00109 /// renameLocalSymbols - This method goes through the Function's symbol table 00110 /// and renames any symbols that conflict with symbols at global scope. This 00111 /// is required before printing out to a textual form, to ensure that there is 00112 /// no ambiguity when parsing. 00113 void renameLocalSymbols(); 00114 00115 00116 /// deleteBody - This method deletes the body of the function, and converts 00117 /// the linkage to external. 00118 /// 00119 void deleteBody() { 00120 dropAllReferences(); 00121 setLinkage(ExternalLinkage); 00122 } 00123 00124 /// removeFromParent - This method unlinks 'this' from the containing module, 00125 /// but does not delete it. 00126 /// 00127 void removeFromParent(); 00128 00129 /// eraseFromParent - This method unlinks 'this' from the containing module 00130 /// and deletes it. 00131 /// 00132 void eraseFromParent(); 00133 00134 00135 // getNext/Prev - Return the next or previous function in the list. These 00136 // methods should never be used directly, and are only used to implement the 00137 // function list as part of the module. 00138 // 00139 Function *getNext() { return Next; } 00140 const Function *getNext() const { return Next; } 00141 Function *getPrev() { return Prev; } 00142 const Function *getPrev() const { return Prev; } 00143 00144 /// Get the underlying elements of the Function... the basic block list is 00145 /// empty for external functions. 00146 /// 00147 const ArgumentListType &getArgumentList() const { return ArgumentList; } 00148 ArgumentListType &getArgumentList() { return ArgumentList; } 00149 00150 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; } 00151 BasicBlockListType &getBasicBlockList() { return BasicBlocks; } 00152 00153 const BasicBlock &getEntryBlock() const { return front(); } 00154 BasicBlock &getEntryBlock() { return front(); } 00155 00156 //===--------------------------------------------------------------------===// 00157 // Symbol Table Accessing functions... 00158 00159 /// getSymbolTable() - Return the symbol table... 00160 /// 00161 inline SymbolTable &getSymbolTable() { return *SymTab; } 00162 inline const SymbolTable &getSymbolTable() const { return *SymTab; } 00163 00164 00165 //===--------------------------------------------------------------------===// 00166 // BasicBlock iterator forwarding functions 00167 // 00168 iterator begin() { return BasicBlocks.begin(); } 00169 const_iterator begin() const { return BasicBlocks.begin(); } 00170 iterator end () { return BasicBlocks.end(); } 00171 const_iterator end () const { return BasicBlocks.end(); } 00172 00173 reverse_iterator rbegin() { return BasicBlocks.rbegin(); } 00174 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); } 00175 reverse_iterator rend () { return BasicBlocks.rend(); } 00176 const_reverse_iterator rend () const { return BasicBlocks.rend(); } 00177 00178 size_t size() const { return BasicBlocks.size(); } 00179 bool empty() const { return BasicBlocks.empty(); } 00180 const BasicBlock &front() const { return BasicBlocks.front(); } 00181 BasicBlock &front() { return BasicBlocks.front(); } 00182 const BasicBlock &back() const { return BasicBlocks.back(); } 00183 BasicBlock &back() { return BasicBlocks.back(); } 00184 00185 //===--------------------------------------------------------------------===// 00186 // Argument iterator forwarding functions 00187 // 00188 aiterator abegin() { return ArgumentList.begin(); } 00189 const_aiterator abegin() const { return ArgumentList.begin(); } 00190 aiterator aend () { return ArgumentList.end(); } 00191 const_aiterator aend () const { return ArgumentList.end(); } 00192 00193 reverse_aiterator arbegin() { return ArgumentList.rbegin(); } 00194 const_reverse_aiterator arbegin() const { return ArgumentList.rbegin(); } 00195 reverse_aiterator arend () { return ArgumentList.rend(); } 00196 const_reverse_aiterator arend () const { return ArgumentList.rend(); } 00197 00198 size_t asize() const { return ArgumentList.size(); } 00199 bool aempty() const { return ArgumentList.empty(); } 00200 const Argument &afront() const { return ArgumentList.front(); } 00201 Argument &afront() { return ArgumentList.front(); } 00202 const Argument &aback() const { return ArgumentList.back(); } 00203 Argument &aback() { return ArgumentList.back(); } 00204 00205 virtual void print(std::ostream &OS) const { print(OS, 0); } 00206 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const; 00207 00208 /// viewCFG - This function is meant for use from the debugger. You can just 00209 /// say 'call F->viewCFG()' and a ghostview window should pop up from the 00210 /// program, displaying the CFG of the current function with the code for each 00211 /// basic block inside. This depends on there being a 'dot' and 'gv' program 00212 /// in your path. 00213 /// 00214 void viewCFG() const; 00215 00216 /// viewCFGOnly - This function is meant for use from the debugger. It works 00217 /// just like viewCFG, but it does not include the contents of basic blocks 00218 /// into the nodes, just the label. If you are only interested in the CFG 00219 /// this can make the graph smaller. 00220 /// 00221 void viewCFGOnly() const; 00222 00223 /// Methods for support type inquiry through isa, cast, and dyn_cast: 00224 static inline bool classof(const Function *) { return true; } 00225 static inline bool classof(const Value *V) { 00226 return V->getValueType() == Value::FunctionVal; 00227 } 00228 00229 /// dropAllReferences() - This method causes all the subinstructions to "let 00230 /// go" of all references that they are maintaining. This allows one to 00231 /// 'delete' a whole module at a time, even though there may be circular 00232 /// references... first all references are dropped, and all use counts go to 00233 /// zero. Then everything is deleted for real. Note that no operations are 00234 /// valid on an object that has "dropped all references", except operator 00235 /// delete. 00236 /// 00237 /// Since no other object in the module can have references into the body of a 00238 /// function, dropping all references deletes the entire body of the function, 00239 /// including any contained basic blocks. 00240 /// 00241 void dropAllReferences(); 00242 }; 00243 00244 } // End llvm namespace 00245 00246 #endif