LLVM API Documentation

Module.h

Go to the documentation of this file.
00001 //===-- llvm/Module.h - C++ class to represent a VM module ------*- 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 /// @file
00011 /// Module.h This file contains the declarations for the Module class.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_MODULE_H
00016 #define LLVM_MODULE_H
00017 
00018 #include "llvm/Function.h"
00019 #include "llvm/GlobalVariable.h"
00020 #include "llvm/GlobalAlias.h"
00021 #include "llvm/Metadata.h"
00022 #include "llvm/Support/DataTypes.h"
00023 #include <vector>
00024 
00025 namespace llvm {
00026 
00027 class GlobalValueRefMap;   // Used by ConstantVals.cpp
00028 class FunctionType;
00029 class LLVMContext;
00030 
00031 template<> struct ilist_traits<Function>
00032   : public SymbolTableListTraits<Function, Module> {
00033 
00034   // createSentinel is used to get hold of the node that marks the end of the
00035   // list... (same trick used here as in ilist_traits<Instruction>)
00036   Function *createSentinel() const {
00037     return static_cast<Function*>(&Sentinel);
00038   }
00039   static void destroySentinel(Function*) {}
00040 
00041   Function *provideInitialHead() const { return createSentinel(); }
00042   Function *ensureHead(Function*) const { return createSentinel(); }
00043   static void noteHead(Function*, Function*) {}
00044 
00045 private:
00046   mutable ilist_node<Function> Sentinel;
00047 };
00048 template<> struct ilist_traits<GlobalVariable>
00049   : public SymbolTableListTraits<GlobalVariable, Module> {
00050   // createSentinel is used to create a node that marks the end of the list.
00051   static GlobalVariable *createSentinel();
00052   static void destroySentinel(GlobalVariable *GV) { delete GV; }
00053 };
00054 template<> struct ilist_traits<GlobalAlias>
00055   : public SymbolTableListTraits<GlobalAlias, Module> {
00056   // createSentinel is used to create a node that marks the end of the list.
00057   static GlobalAlias *createSentinel();
00058   static void destroySentinel(GlobalAlias *GA) { delete GA; }
00059 };
00060 template<> struct ilist_traits<NamedMDNode>
00061   : public SymbolTableListTraits<NamedMDNode, Module> {
00062   // createSentinel is used to get hold of a node that marks the end of
00063   // the list...
00064   NamedMDNode *createSentinel() const {
00065     return static_cast<NamedMDNode*>(&Sentinel);
00066   }
00067   static void destroySentinel(NamedMDNode*) {}
00068 
00069   NamedMDNode *provideInitialHead() const { return createSentinel(); }
00070   NamedMDNode *ensureHead(NamedMDNode*) const { return createSentinel(); }
00071   static void noteHead(NamedMDNode*, NamedMDNode*) {}
00072 private:
00073   mutable ilist_node<NamedMDNode> Sentinel;
00074 };
00075 
00076 /// A Module instance is used to store all the information related to an
00077 /// LLVM module. Modules are the top level container of all other LLVM
00078 /// Intermediate Representation (IR) objects. Each module directly contains a
00079 /// list of globals variables, a list of functions, a list of libraries (or
00080 /// other modules) this module depends on, a symbol table, and various data
00081 /// about the target's characteristics.
00082 ///
00083 /// A module maintains a GlobalValRefMap object that is used to hold all
00084 /// constant references to global variables in the module.  When a global
00085 /// variable is destroyed, it should have no entries in the GlobalValueRefMap.
00086 /// @brief The main container class for the LLVM Intermediate Representation.
00087 class Module {
00088 /// @name Types And Enumerations
00089 /// @{
00090 public:
00091   /// The type for the list of global variables.
00092   typedef iplist<GlobalVariable> GlobalListType;
00093   /// The type for the list of functions.
00094   typedef iplist<Function> FunctionListType;
00095   /// The type for the list of aliases.
00096   typedef iplist<GlobalAlias> AliasListType;
00097   /// The type for the list of named metadata.
00098   typedef iplist<NamedMDNode> NamedMDListType;
00099 
00100   /// The type for the list of dependent libraries.
00101   typedef std::vector<std::string> LibraryListType;
00102 
00103   /// The Global Variable iterator.
00104   typedef GlobalListType::iterator                      global_iterator;
00105   /// The Global Variable constant iterator.
00106   typedef GlobalListType::const_iterator          const_global_iterator;
00107 
00108   /// The Function iterators.
00109   typedef FunctionListType::iterator                           iterator;
00110   /// The Function constant iterator
00111   typedef FunctionListType::const_iterator               const_iterator;
00112 
00113   /// The Global Alias iterators.
00114   typedef AliasListType::iterator                        alias_iterator;
00115   /// The Global Alias constant iterator
00116   typedef AliasListType::const_iterator            const_alias_iterator;
00117 
00118   /// The named metadata iterators.
00119   typedef NamedMDListType::iterator             named_metadata_iterator;
00120   /// The named metadata constant interators.
00121   typedef NamedMDListType::const_iterator const_named_metadata_iterator;
00122   /// The Library list iterator.
00123   typedef LibraryListType::const_iterator lib_iterator;
00124 
00125   /// An enumeration for describing the endianess of the target machine.
00126   enum Endianness  { AnyEndianness, LittleEndian, BigEndian };
00127 
00128   /// An enumeration for describing the size of a pointer on the target machine.
00129   enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
00130 
00131 /// @}
00132 /// @name Member Variables
00133 /// @{
00134 private:
00135   LLVMContext& Context;          ///< The LLVMContext from which types and
00136                                  ///< constants are allocated.
00137   GlobalListType GlobalList;     ///< The Global Variables in the module
00138   FunctionListType FunctionList; ///< The Functions in the module
00139   AliasListType AliasList;       ///< The Aliases in the module
00140   LibraryListType LibraryList;   ///< The Libraries needed by the module
00141   NamedMDListType NamedMDList;   ///< The named metadata in the module
00142   std::string GlobalScopeAsm;    ///< Inline Asm at global scope.
00143   ValueSymbolTable *ValSymTab;   ///< Symbol table for values
00144   TypeSymbolTable *TypeSymTab;   ///< Symbol table for types
00145   std::string ModuleID;          ///< Human readable identifier for the module
00146   std::string TargetTriple;      ///< Platform target triple Module compiled on
00147   std::string DataLayout;        ///< Target data description
00148 
00149   friend class Constant;
00150 
00151 /// @}
00152 /// @name Constructors
00153 /// @{
00154 public:
00155   /// The Module constructor. Note that there is no default constructor. You
00156   /// must provide a name for the module upon construction.
00157   explicit Module(const StringRef &ModuleID, LLVMContext& C);
00158   /// The module destructor. This will dropAllReferences.
00159   ~Module();
00160 
00161 /// @}
00162 /// @name Module Level Accessors
00163 /// @{
00164 public:
00165   /// Get the module identifier which is, essentially, the name of the module.
00166   /// @returns the module identifier as a string
00167   const std::string &getModuleIdentifier() const { return ModuleID; }
00168 
00169   /// Get the data layout string for the module's target platform.  This encodes
00170   /// the type sizes and alignments expected by this module.
00171   /// @returns the data layout as a string
00172   const std::string &getDataLayout() const { return DataLayout; }
00173 
00174   /// Get the target triple which is a string describing the target host.
00175   /// @returns a string containing the target triple.
00176   const std::string &getTargetTriple() const { return TargetTriple; }
00177 
00178   /// Get the target endian information.
00179   /// @returns Endianess - an enumeration for the endianess of the target
00180   Endianness getEndianness() const;
00181 
00182   /// Get the target pointer size.
00183   /// @returns PointerSize - an enumeration for the size of the target's pointer
00184   PointerSize getPointerSize() const;
00185 
00186   /// Get the global data context.
00187   /// @returns LLVMContext - a container for LLVM's global information
00188   LLVMContext& getContext() const { return Context; }
00189 
00190   /// Get any module-scope inline assembly blocks.
00191   /// @returns a string containing the module-scope inline assembly blocks.
00192   const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
00193 /// @}
00194 /// @name Module Level Mutators
00195 /// @{
00196 public:
00197 
00198   /// Set the module identifier.
00199   void setModuleIdentifier(const StringRef &ID) { ModuleID = ID; }
00200 
00201   /// Set the data layout
00202   void setDataLayout(const StringRef &DL) { DataLayout = DL; }
00203 
00204   /// Set the target triple.
00205   void setTargetTriple(const StringRef &T) { TargetTriple = T; }
00206 
00207   /// Set the module-scope inline assembly blocks.
00208   void setModuleInlineAsm(const StringRef &Asm) { GlobalScopeAsm = Asm; }
00209 
00210   /// Append to the module-scope inline assembly blocks, automatically
00211   /// appending a newline to the end.
00212   void appendModuleInlineAsm(const StringRef &Asm) {
00213     GlobalScopeAsm += Asm;
00214     GlobalScopeAsm += '\n';
00215   }
00216 
00217 /// @}
00218 /// @name Generic Value Accessors
00219 /// @{
00220 
00221   /// getNamedValue - Return the first global value in the module with
00222   /// the specified name, of arbitrary type.  This method returns null
00223   /// if a global with the specified name is not found.
00224   GlobalValue *getNamedValue(const StringRef &Name) const;
00225 
00226 /// @}
00227 /// @name Function Accessors
00228 /// @{
00229 public:
00230   /// getOrInsertFunction - Look up the specified function in the module symbol
00231   /// table.  Four possibilities:
00232   ///   1. If it does not exist, add a prototype for the function and return it.
00233   ///   2. If it exists, and has a local linkage, the existing function is
00234   ///      renamed and a new one is inserted.
00235   ///   3. Otherwise, if the existing function has the correct prototype, return
00236   ///      the existing function.
00237   ///   4. Finally, the function exists but has the wrong prototype: return the
00238   ///      function with a constantexpr cast to the right prototype.
00239   Constant *getOrInsertFunction(const StringRef &Name, const FunctionType *T,
00240                                 AttrListPtr AttributeList);
00241 
00242   Constant *getOrInsertFunction(const StringRef &Name, const FunctionType *T);
00243 
00244   /// getOrInsertFunction - Look up the specified function in the module symbol
00245   /// table.  If it does not exist, add a prototype for the function and return
00246   /// it.  This function guarantees to return a constant of pointer to the
00247   /// specified function type or a ConstantExpr BitCast of that type if the
00248   /// named function has a different type.  This version of the method takes a
00249   /// null terminated list of function arguments, which makes it easier for
00250   /// clients to use.
00251   Constant *getOrInsertFunction(const StringRef &Name,
00252                                 AttrListPtr AttributeList,
00253                                 const Type *RetTy, ...)  END_WITH_NULL;
00254 
00255   Constant *getOrInsertFunction(const StringRef &Name, const Type *RetTy, ...)
00256     END_WITH_NULL;
00257 
00258   Constant *getOrInsertTargetIntrinsic(const StringRef &Name,
00259                                        const FunctionType *Ty,
00260                                        AttrListPtr AttributeList);
00261   
00262   /// getFunction - Look up the specified function in the module symbol table.
00263   /// If it does not exist, return null.
00264   Function *getFunction(const StringRef &Name) const;
00265 
00266 /// @}
00267 /// @name Global Variable Accessors
00268 /// @{
00269 public:
00270   /// getGlobalVariable - Look up the specified global variable in the module
00271   /// symbol table.  If it does not exist, return null. If AllowInternal is set
00272   /// to true, this function will return types that have InternalLinkage. By
00273   /// default, these types are not returned.
00274   GlobalVariable *getGlobalVariable(const StringRef &Name,
00275                                     bool AllowInternal = false) const;
00276 
00277   /// getNamedGlobal - Return the first global variable in the module with the
00278   /// specified name, of arbitrary type.  This method returns null if a global
00279   /// with the specified name is not found.
00280   GlobalVariable *getNamedGlobal(const StringRef &Name) const {
00281     return getGlobalVariable(Name, true);
00282   }
00283 
00284   /// getOrInsertGlobal - Look up the specified global in the module symbol
00285   /// table.
00286   ///   1. If it does not exist, add a declaration of the global and return it.
00287   ///   2. Else, the global exists but has the wrong type: return the function
00288   ///      with a constantexpr cast to the right type.
00289   ///   3. Finally, if the existing global is the correct delclaration, return
00290   ///      the existing global.
00291   Constant *getOrInsertGlobal(const StringRef &Name, const Type *Ty);
00292 
00293 /// @}
00294 /// @name Global Alias Accessors
00295 /// @{
00296 public:
00297   /// getNamedAlias - Return the first global alias in the module with the
00298   /// specified name, of arbitrary type.  This method returns null if a global
00299   /// with the specified name is not found.
00300   GlobalAlias *getNamedAlias(const StringRef &Name) const;
00301 
00302 /// @}
00303 /// @name Named Metadata Accessors
00304 /// @{
00305 public:
00306   /// getNamedMetadata - Return the first NamedMDNode in the module with the
00307   /// specified name. This method returns null if a NamedMDNode with the 
00308   /// specified name is not found.
00309   NamedMDNode *getNamedMetadata(const StringRef &Name) const;
00310 
00311   /// getOrInsertNamedMetadata - Return the first named MDNode in the module 
00312   /// with the specified name. This method returns a new NamedMDNode if a 
00313   /// NamedMDNode with the specified name is not found.
00314   NamedMDNode *getOrInsertNamedMetadata(const StringRef &Name);
00315 
00316 /// @}
00317 /// @name Type Accessors
00318 /// @{
00319 public:
00320   /// addTypeName - Insert an entry in the symbol table mapping Str to Type.  If
00321   /// there is already an entry for this name, true is returned and the symbol
00322   /// table is not modified.
00323   bool addTypeName(const StringRef &Name, const Type *Ty);
00324 
00325   /// getTypeName - If there is at least one entry in the symbol table for the
00326   /// specified type, return it.
00327   std::string getTypeName(const Type *Ty) const;
00328 
00329   /// getTypeByName - Return the type with the specified name in this module, or
00330   /// null if there is none by that name.
00331   const Type *getTypeByName(const StringRef &Name) const;
00332 
00333 /// @}
00334 /// @name Direct access to the globals list, functions list, and symbol table
00335 /// @{
00336 public:
00337   /// Get the Module's list of global variables (constant).
00338   const GlobalListType   &getGlobalList() const       { return GlobalList; }
00339   /// Get the Module's list of global variables.
00340   GlobalListType         &getGlobalList()             { return GlobalList; }
00341   static iplist<GlobalVariable> Module::*getSublistAccess(GlobalVariable*) {
00342     return &Module::GlobalList;
00343   }
00344   /// Get the Module's list of functions (constant).
00345   const FunctionListType &getFunctionList() const     { return FunctionList; }
00346   /// Get the Module's list of functions.
00347   FunctionListType       &getFunctionList()           { return FunctionList; }
00348   static iplist<Function> Module::*getSublistAccess(Function*) {
00349     return &Module::FunctionList;
00350   }
00351   /// Get the Module's list of aliases (constant).
00352   const AliasListType    &getAliasList() const        { return AliasList; }
00353   /// Get the Module's list of aliases.
00354   AliasListType          &getAliasList()              { return AliasList; }
00355   static iplist<GlobalAlias> Module::*getSublistAccess(GlobalAlias*) {
00356     return &Module::AliasList;
00357   }
00358   /// Get the Module's list of named metadata (constant).
00359   const NamedMDListType  &getNamedMDList() const      { return NamedMDList; }
00360   /// Get the Module's list of named metadata.
00361   NamedMDListType  &getNamedMDList()                  { return NamedMDList; }
00362   static iplist<NamedMDNode> Module::*getSublistAccess(NamedMDNode *) {
00363     return &Module::NamedMDList;
00364   }
00365   /// Get the symbol table of global variable and function identifiers
00366   const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; }
00367   /// Get the Module's symbol table of global variable and function identifiers.
00368   ValueSymbolTable       &getValueSymbolTable()       { return *ValSymTab; }
00369   /// Get the symbol table of types
00370   const TypeSymbolTable  &getTypeSymbolTable() const  { return *TypeSymTab; }
00371   /// Get the Module's symbol table of types
00372   TypeSymbolTable        &getTypeSymbolTable()        { return *TypeSymTab; }
00373 
00374 /// @}
00375 /// @name Global Variable Iteration
00376 /// @{
00377 public:
00378   /// Get an iterator to the first global variable
00379   global_iterator       global_begin()       { return GlobalList.begin(); }
00380   /// Get a constant iterator to the first global variable
00381   const_global_iterator global_begin() const { return GlobalList.begin(); }
00382   /// Get an iterator to the last global variable
00383   global_iterator       global_end  ()       { return GlobalList.end(); }
00384   /// Get a constant iterator to the last global variable
00385   const_global_iterator global_end  () const { return GlobalList.end(); }
00386   /// Determine if the list of globals is empty.
00387   bool                  global_empty() const { return GlobalList.empty(); }
00388 
00389 /// @}
00390 /// @name Function Iteration
00391 /// @{
00392 public:
00393   /// Get an iterator to the first function.
00394   iterator                begin()       { return FunctionList.begin(); }
00395   /// Get a constant iterator to the first function.
00396   const_iterator          begin() const { return FunctionList.begin(); }
00397   /// Get an iterator to the last function.
00398   iterator                end  ()       { return FunctionList.end();   }
00399   /// Get a constant iterator to the last function.
00400   const_iterator          end  () const { return FunctionList.end();   }
00401   /// Determine how many functions are in the Module's list of functions.
00402   size_t                  size() const  { return FunctionList.size(); }
00403   /// Determine if the list of functions is empty.
00404   bool                    empty() const { return FunctionList.empty(); }
00405 
00406 /// @}
00407 /// @name Dependent Library Iteration
00408 /// @{
00409 public:
00410   /// @brief Get a constant iterator to beginning of dependent library list.
00411   inline lib_iterator lib_begin() const { return LibraryList.begin(); }
00412   /// @brief Get a constant iterator to end of dependent library list.
00413   inline lib_iterator lib_end()   const { return LibraryList.end();   }
00414   /// @brief Returns the number of items in the list of libraries.
00415   inline size_t       lib_size()  const { return LibraryList.size();  }
00416   /// @brief Add a library to the list of dependent libraries
00417   void addLibrary(const StringRef &Lib);
00418   /// @brief Remove a library from the list of dependent libraries
00419   void removeLibrary(const StringRef &Lib);
00420   /// @brief Get all the libraries
00421   inline const LibraryListType& getLibraries() const { return LibraryList; }
00422 
00423 /// @}
00424 /// @name Alias Iteration
00425 /// @{
00426 public:
00427   /// Get an iterator to the first alias.
00428   alias_iterator       alias_begin()            { return AliasList.begin(); }
00429   /// Get a constant iterator to the first alias.
00430   const_alias_iterator alias_begin() const      { return AliasList.begin(); }
00431   /// Get an iterator to the last alias.
00432   alias_iterator       alias_end  ()            { return AliasList.end();   }
00433   /// Get a constant iterator to the last alias.
00434   const_alias_iterator alias_end  () const      { return AliasList.end();   }
00435   /// Determine how many aliases are in the Module's list of aliases.
00436   size_t               alias_size () const      { return AliasList.size();  }
00437   /// Determine if the list of aliases is empty.
00438   bool                 alias_empty() const      { return AliasList.empty(); }
00439 
00440 
00441 /// @}
00442 /// @name Named Metadata Iteration
00443 /// @{
00444 public:
00445   /// Get an iterator to the first named metadata.
00446   named_metadata_iterator       named_metadata_begin()            
00447                                                 { return NamedMDList.begin(); }
00448   /// Get a constant iterator to the first named metadata.
00449   const_named_metadata_iterator named_metadata_begin() const      
00450                                                 { return NamedMDList.begin(); }
00451   /// Get an iterator to the last named metadata.
00452   named_metadata_iterator       named_metadata_end  ()            
00453                                                 { return NamedMDList.end();   }
00454   /// Get a constant iterator to the last named metadata.
00455   const_named_metadata_iterator named_metadata_end  () const      
00456                                                 { return NamedMDList.end();   }
00457   /// Determine how many NamedMDNodes are in the Module's list of named metadata.
00458   size_t                        named_metadata_size () const      
00459                                                 { return NamedMDList.size();  }
00460   /// Determine if the list of named metadata is empty.
00461   bool                          named_metadata_empty() const      
00462                                                 { return NamedMDList.empty(); }
00463 
00464 
00465 /// @}
00466 /// @name Utility functions for printing and dumping Module objects
00467 /// @{
00468 public:
00469   /// Print the module to an output stream with AssemblyAnnotationWriter.
00470   void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const;
00471   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
00472   
00473   /// Dump the module to stderr (for debugging).
00474   void dump() const;
00475   /// This function causes all the subinstructions to "let go" of all references
00476   /// that they are maintaining.  This allows one to 'delete' a whole class at
00477   /// a time, even though there may be circular references... first all
00478   /// references are dropped, and all use counts go to zero.  Then everything
00479   /// is delete'd for real.  Note that no operations are valid on an object
00480   /// that has "dropped all references", except operator delete.
00481   void dropAllReferences();
00482 /// @}
00483 };
00484 
00485 /// An iostream inserter for modules.
00486 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
00487   M.print(O, 0);
00488   return O;
00489 }
00490 inline raw_ostream &operator<<(raw_ostream &O, const Module &M) {
00491   M.print(O, 0);
00492   return O;
00493 }
00494 
00495 } // End llvm namespace
00496 
00497 #endif