LLVM API Documentation

Function.h

Go to the documentation of this file.
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(~0, N);
00162   }
00163 
00164   /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm
00165   ///                             to use during code generation.
00166   bool hasGC() const;
00167   const char *getGC() const;
00168   void setGC(const char *Str);
00169   void clearGC();
00170 
00171   /// @brief Determine whether the function has the given attribute.
00172   bool paramHasAttr(unsigned i, Attributes attr) const {
00173     return AttributeList.paramHasAttr(i, attr);
00174   }
00175 
00176   /// addAttribute - adds the attribute to the list of attributes.
00177   void addAttribute(unsigned i, Attributes attr);
00178   
00179   /// removeAttribute - removes the attribute from the list of attributes.
00180   void removeAttribute(unsigned i, Attributes attr);
00181 
00182   /// @brief Extract the alignment for a call or parameter (0=unknown).
00183   unsigned getParamAlignment(unsigned i) const {
00184     return AttributeList.getParamAlignment(i);
00185   }
00186 
00187   /// @brief Determine if the function does not access memory.
00188   bool doesNotAccessMemory() const {
00189     return paramHasAttr(~0, Attribute::ReadNone);
00190   }
00191   void setDoesNotAccessMemory(bool DoesNotAccessMemory = true) {
00192     if (DoesNotAccessMemory) addAttribute(~0, Attribute::ReadNone);
00193     else removeAttribute(~0, Attribute::ReadNone);
00194   }
00195 
00196   /// @brief Determine if the function does not access or only reads memory.
00197   bool onlyReadsMemory() const {
00198     return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
00199   }
00200   void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
00201     if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
00202     else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
00203   }
00204 
00205   /// @brief Determine if the function cannot return.
00206   bool doesNotReturn() const {
00207     return paramHasAttr(~0, Attribute::NoReturn);
00208   }
00209   void setDoesNotReturn(bool DoesNotReturn = true) {
00210     if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
00211     else removeAttribute(~0, Attribute::NoReturn);
00212   }
00213 
00214   /// @brief Determine if the function cannot unwind.
00215   bool doesNotThrow() const {
00216     return paramHasAttr(~0, Attribute::NoUnwind);
00217   }
00218   void setDoesNotThrow(bool DoesNotThrow = true) {
00219     if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
00220     else removeAttribute(~0, Attribute::NoUnwind);
00221   }
00222 
00223   /// @brief Determine if the function returns a structure through first 
00224   /// pointer argument.
00225   bool hasStructRetAttr() const {
00226     return paramHasAttr(1, Attribute::StructRet);
00227   }
00228 
00229   /// copyAttributesFrom - copy all additional attributes (those not needed to
00230   /// create a Function) from the Function Src to this one.
00231   void copyAttributesFrom(const GlobalValue *Src);
00232 
00233   /// deleteBody - This method deletes the body of the function, and converts
00234   /// the linkage to external.
00235   ///
00236   void deleteBody() {
00237     dropAllReferences();
00238     setLinkage(ExternalLinkage);
00239   }
00240 
00241   /// removeFromParent - This method unlinks 'this' from the containing module,
00242   /// but does not delete it.
00243   ///
00244   virtual void removeFromParent();
00245 
00246   /// eraseFromParent - This method unlinks 'this' from the containing module
00247   /// and deletes it.
00248   ///
00249   virtual void eraseFromParent();
00250 
00251 
00252   /// Get the underlying elements of the Function... the basic block list is
00253   /// empty for external functions.
00254   ///
00255   const ArgumentListType &getArgumentList() const {
00256     CheckLazyArguments();
00257     return ArgumentList;
00258   }
00259   ArgumentListType &getArgumentList() {
00260     CheckLazyArguments();
00261     return ArgumentList;
00262   }
00263 
00264   const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
00265         BasicBlockListType &getBasicBlockList()       { return BasicBlocks; }
00266 
00267   const BasicBlock       &getEntryBlock() const   { return front(); }
00268         BasicBlock       &getEntryBlock()         { return front(); }
00269 
00270   //===--------------------------------------------------------------------===//
00271   // Symbol Table Accessing functions...
00272 
00273   /// getSymbolTable() - Return the symbol table...
00274   ///
00275   inline       ValueSymbolTable &getValueSymbolTable()       { return *SymTab; }
00276   inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
00277 
00278 
00279   //===--------------------------------------------------------------------===//
00280   // BasicBlock iterator forwarding functions
00281   //
00282   iterator                begin()       { return BasicBlocks.begin(); }
00283   const_iterator          begin() const { return BasicBlocks.begin(); }
00284   iterator                end  ()       { return BasicBlocks.end();   }
00285   const_iterator          end  () const { return BasicBlocks.end();   }
00286 
00287   size_t                   size() const { return BasicBlocks.size();  }
00288   bool                    empty() const { return BasicBlocks.empty(); }
00289   const BasicBlock       &front() const { return BasicBlocks.front(); }
00290         BasicBlock       &front()       { return BasicBlocks.front(); }
00291   const BasicBlock        &back() const { return BasicBlocks.back();  }
00292         BasicBlock        &back()       { return BasicBlocks.back();  }
00293 
00294   //===--------------------------------------------------------------------===//
00295   // Argument iterator forwarding functions
00296   //
00297   arg_iterator arg_begin() {
00298     CheckLazyArguments();
00299     return ArgumentList.begin();
00300   }
00301   const_arg_iterator arg_begin() const {
00302     CheckLazyArguments();
00303     return ArgumentList.begin();
00304   }
00305   arg_iterator arg_end() {
00306     CheckLazyArguments();
00307     return ArgumentList.end();
00308   }
00309   const_arg_iterator arg_end() const {
00310     CheckLazyArguments();
00311     return ArgumentList.end();
00312   }
00313 
00314   size_t arg_size() const;
00315   bool arg_empty() const;
00316 
00317   /// viewCFG - This function is meant for use from the debugger.  You can just
00318   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
00319   /// program, displaying the CFG of the current function with the code for each
00320   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
00321   /// in your path.
00322   ///
00323   void viewCFG() const;
00324 
00325   /// viewCFGOnly - This function is meant for use from the debugger.  It works
00326   /// just like viewCFG, but it does not include the contents of basic blocks
00327   /// into the nodes, just the label.  If you are only interested in the CFG
00328   /// this can make the graph smaller.
00329   ///
00330   void viewCFGOnly() const;
00331 
00332   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00333   static inline bool classof(const Function *) { return true; }
00334   static inline bool classof(const Value *V) {
00335     return V->getValueID() == Value::FunctionVal;
00336   }
00337 
00338   /// dropAllReferences() - This method causes all the subinstructions to "let
00339   /// go" of all references that they are maintaining.  This allows one to
00340   /// 'delete' a whole module at a time, even though there may be circular
00341   /// references... first all references are dropped, and all use counts go to
00342   /// zero.  Then everything is deleted for real.  Note that no operations are
00343   /// valid on an object that has "dropped all references", except operator
00344   /// delete.
00345   ///
00346   /// Since no other object in the module can have references into the body of a
00347   /// function, dropping all references deletes the entire body of the function,
00348   /// including any contained basic blocks.
00349   ///
00350   void dropAllReferences();
00351   
00352   static unsigned getBasicBlockListOffset() {
00353     Function *Obj = 0;
00354     return unsigned(reinterpret_cast<uintptr_t>(&Obj->BasicBlocks));
00355   }
00356   static unsigned getArgumentListOffset() {
00357     Function *Obj = 0;
00358     return unsigned(reinterpret_cast<uintptr_t>(&Obj->ArgumentList));
00359   }
00360 };
00361 
00362 inline ValueSymbolTable *
00363 ilist_traits<BasicBlock>::getSymTab(Function *F) {
00364   return F ? &F->getValueSymbolTable() : 0;
00365 }
00366 
00367 inline ValueSymbolTable *
00368 ilist_traits<Argument>::getSymTab(Function *F) {
00369   return F ? &F->getValueSymbolTable() : 0;
00370 }
00371 
00372 inline int 
00373 ilist_traits<BasicBlock>::getListOffset() {
00374   return Function::getBasicBlockListOffset();
00375 }
00376 
00377 inline int 
00378 ilist_traits<Argument>::getListOffset() {
00379   return Function::getArgumentListOffset();
00380 }
00381 
00382 
00383 } // End llvm namespace
00384 
00385 #endif



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.