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_IR_MODULE_H
00016 #define LLVM_IR_MODULE_H
00017 
00018 #include "llvm/ADT/iterator_range.h"
00019 #include "llvm/IR/Comdat.h"
00020 #include "llvm/IR/DataLayout.h"
00021 #include "llvm/IR/Function.h"
00022 #include "llvm/IR/GlobalAlias.h"
00023 #include "llvm/IR/GlobalVariable.h"
00024 #include "llvm/IR/Metadata.h"
00025 #include "llvm/Support/CBindingWrapping.h"
00026 #include "llvm/Support/CodeGen.h"
00027 #include "llvm/Support/DataTypes.h"
00028 #include <system_error>
00029 
00030 namespace llvm {
00031 class FunctionType;
00032 class GVMaterializer;
00033 class LLVMContext;
00034 class RandomNumberGenerator;
00035 class StructType;
00036 
00037 template<> struct ilist_traits<Function>
00038   : public SymbolTableListTraits<Function, Module> {
00039 
00040   // createSentinel is used to get hold of the node that marks the end of the
00041   // list... (same trick used here as in ilist_traits<Instruction>)
00042   Function *createSentinel() const {
00043     return static_cast<Function*>(&Sentinel);
00044   }
00045   static void destroySentinel(Function*) {}
00046 
00047   Function *provideInitialHead() const { return createSentinel(); }
00048   Function *ensureHead(Function*) const { return createSentinel(); }
00049   static void noteHead(Function*, Function*) {}
00050 
00051 private:
00052   mutable ilist_node<Function> Sentinel;
00053 };
00054 
00055 template<> struct ilist_traits<GlobalVariable>
00056   : public SymbolTableListTraits<GlobalVariable, Module> {
00057   // createSentinel is used to create a node that marks the end of the list.
00058   GlobalVariable *createSentinel() const {
00059     return static_cast<GlobalVariable*>(&Sentinel);
00060   }
00061   static void destroySentinel(GlobalVariable*) {}
00062 
00063   GlobalVariable *provideInitialHead() const { return createSentinel(); }
00064   GlobalVariable *ensureHead(GlobalVariable*) const { return createSentinel(); }
00065   static void noteHead(GlobalVariable*, GlobalVariable*) {}
00066 private:
00067   mutable ilist_node<GlobalVariable> Sentinel;
00068 };
00069 
00070 template<> struct ilist_traits<GlobalAlias>
00071   : public SymbolTableListTraits<GlobalAlias, Module> {
00072   // createSentinel is used to create a node that marks the end of the list.
00073   GlobalAlias *createSentinel() const {
00074     return static_cast<GlobalAlias*>(&Sentinel);
00075   }
00076   static void destroySentinel(GlobalAlias*) {}
00077 
00078   GlobalAlias *provideInitialHead() const { return createSentinel(); }
00079   GlobalAlias *ensureHead(GlobalAlias*) const { return createSentinel(); }
00080   static void noteHead(GlobalAlias*, GlobalAlias*) {}
00081 private:
00082   mutable ilist_node<GlobalAlias> Sentinel;
00083 };
00084 
00085 template<> struct ilist_traits<NamedMDNode>
00086   : public ilist_default_traits<NamedMDNode> {
00087   // createSentinel is used to get hold of a node that marks the end of
00088   // the list...
00089   NamedMDNode *createSentinel() const {
00090     return static_cast<NamedMDNode*>(&Sentinel);
00091   }
00092   static void destroySentinel(NamedMDNode*) {}
00093 
00094   NamedMDNode *provideInitialHead() const { return createSentinel(); }
00095   NamedMDNode *ensureHead(NamedMDNode*) const { return createSentinel(); }
00096   static void noteHead(NamedMDNode*, NamedMDNode*) {}
00097   void addNodeToList(NamedMDNode *) {}
00098   void removeNodeFromList(NamedMDNode *) {}
00099 private:
00100   mutable ilist_node<NamedMDNode> Sentinel;
00101 };
00102 
00103 /// A Module instance is used to store all the information related to an
00104 /// LLVM module. Modules are the top level container of all other LLVM
00105 /// Intermediate Representation (IR) objects. Each module directly contains a
00106 /// list of globals variables, a list of functions, a list of libraries (or
00107 /// other modules) this module depends on, a symbol table, and various data
00108 /// about the target's characteristics.
00109 ///
00110 /// A module maintains a GlobalValRefMap object that is used to hold all
00111 /// constant references to global variables in the module.  When a global
00112 /// variable is destroyed, it should have no entries in the GlobalValueRefMap.
00113 /// @brief The main container class for the LLVM Intermediate Representation.
00114 class Module {
00115 /// @name Types And Enumerations
00116 /// @{
00117 public:
00118   /// The type for the list of global variables.
00119   typedef iplist<GlobalVariable> GlobalListType;
00120   /// The type for the list of functions.
00121   typedef iplist<Function> FunctionListType;
00122   /// The type for the list of aliases.
00123   typedef iplist<GlobalAlias> AliasListType;
00124   /// The type for the list of named metadata.
00125   typedef ilist<NamedMDNode> NamedMDListType;
00126   /// The type of the comdat "symbol" table.
00127   typedef StringMap<Comdat> ComdatSymTabType;
00128 
00129   /// The Global Variable iterator.
00130   typedef GlobalListType::iterator                      global_iterator;
00131   /// The Global Variable constant iterator.
00132   typedef GlobalListType::const_iterator          const_global_iterator;
00133 
00134   /// The Function iterators.
00135   typedef FunctionListType::iterator                           iterator;
00136   /// The Function constant iterator
00137   typedef FunctionListType::const_iterator               const_iterator;
00138 
00139   /// The Function reverse iterator.
00140   typedef FunctionListType::reverse_iterator             reverse_iterator;
00141   /// The Function constant reverse iterator.
00142   typedef FunctionListType::const_reverse_iterator const_reverse_iterator;
00143 
00144   /// The Global Alias iterators.
00145   typedef AliasListType::iterator                        alias_iterator;
00146   /// The Global Alias constant iterator
00147   typedef AliasListType::const_iterator            const_alias_iterator;
00148 
00149   /// The named metadata iterators.
00150   typedef NamedMDListType::iterator             named_metadata_iterator;
00151   /// The named metadata constant iterators.
00152   typedef NamedMDListType::const_iterator const_named_metadata_iterator;
00153 
00154   /// This enumeration defines the supported behaviors of module flags.
00155   enum ModFlagBehavior {
00156     /// Emits an error if two values disagree, otherwise the resulting value is
00157     /// that of the operands.
00158     Error = 1,
00159 
00160     /// Emits a warning if two values disagree. The result value will be the
00161     /// operand for the flag from the first module being linked.
00162     Warning = 2,
00163 
00164     /// Adds a requirement that another module flag be present and have a
00165     /// specified value after linking is performed. The value must be a metadata
00166     /// pair, where the first element of the pair is the ID of the module flag
00167     /// to be restricted, and the second element of the pair is the value the
00168     /// module flag should be restricted to. This behavior can be used to
00169     /// restrict the allowable results (via triggering of an error) of linking
00170     /// IDs with the **Override** behavior.
00171     Require = 3,
00172 
00173     /// Uses the specified value, regardless of the behavior or value of the
00174     /// other module. If both modules specify **Override**, but the values
00175     /// differ, an error will be emitted.
00176     Override = 4,
00177 
00178     /// Appends the two values, which are required to be metadata nodes.
00179     Append = 5,
00180 
00181     /// Appends the two values, which are required to be metadata
00182     /// nodes. However, duplicate entries in the second list are dropped
00183     /// during the append operation.
00184     AppendUnique = 6,
00185 
00186     // Markers:
00187     ModFlagBehaviorFirstVal = Error,
00188     ModFlagBehaviorLastVal = AppendUnique
00189   };
00190 
00191   /// Checks if Metadata represents a valid ModFlagBehavior, and stores the
00192   /// converted result in MFB.
00193   static bool isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB);
00194 
00195   struct ModuleFlagEntry {
00196     ModFlagBehavior Behavior;
00197     MDString *Key;
00198     Metadata *Val;
00199     ModuleFlagEntry(ModFlagBehavior B, MDString *K, Metadata *V)
00200         : Behavior(B), Key(K), Val(V) {}
00201   };
00202 
00203 /// @}
00204 /// @name Member Variables
00205 /// @{
00206 private:
00207   LLVMContext &Context;           ///< The LLVMContext from which types and
00208                                   ///< constants are allocated.
00209   GlobalListType GlobalList;      ///< The Global Variables in the module
00210   FunctionListType FunctionList;  ///< The Functions in the module
00211   AliasListType AliasList;        ///< The Aliases in the module
00212   NamedMDListType NamedMDList;    ///< The named metadata in the module
00213   std::string GlobalScopeAsm;     ///< Inline Asm at global scope.
00214   ValueSymbolTable *ValSymTab;    ///< Symbol table for values
00215   ComdatSymTabType ComdatSymTab;  ///< Symbol table for COMDATs
00216   std::unique_ptr<GVMaterializer>
00217   Materializer;                   ///< Used to materialize GlobalValues
00218   std::string ModuleID;           ///< Human readable identifier for the module
00219   std::string TargetTriple;       ///< Platform target triple Module compiled on
00220                                   ///< Format: (arch)(sub)-(vendor)-(sys0-(abi)
00221   void *NamedMDSymTab;            ///< NamedMDNode names.
00222 
00223   // We need to keep the string because the C API expects us to own the string
00224   // representation.
00225   // Since we have it, we also use an empty string to represent a module without
00226   // a DataLayout. If it has a DataLayout, these variables are in sync and the
00227   // string is just a cache of getDataLayout()->getStringRepresentation().
00228   std::string DataLayoutStr;
00229   DataLayout DL;
00230 
00231   friend class Constant;
00232 
00233 /// @}
00234 /// @name Constructors
00235 /// @{
00236 public:
00237   /// The Module constructor. Note that there is no default constructor. You
00238   /// must provide a name for the module upon construction.
00239   explicit Module(StringRef ModuleID, LLVMContext& C);
00240   /// The module destructor. This will dropAllReferences.
00241   ~Module();
00242 
00243 /// @}
00244 /// @name Module Level Accessors
00245 /// @{
00246 
00247   /// Get the module identifier which is, essentially, the name of the module.
00248   /// @returns the module identifier as a string
00249   const std::string &getModuleIdentifier() const { return ModuleID; }
00250 
00251   /// Get the data layout string for the module's target platform. This is
00252   /// equivalent to getDataLayout()->getStringRepresentation().
00253   const std::string &getDataLayoutStr() const { return DataLayoutStr; }
00254 
00255   /// Get the data layout for the module's target platform.
00256   const DataLayout *getDataLayout() const;
00257 
00258   /// Get the target triple which is a string describing the target host.
00259   /// @returns a string containing the target triple.
00260   const std::string &getTargetTriple() const { return TargetTriple; }
00261 
00262   /// Get the global data context.
00263   /// @returns LLVMContext - a container for LLVM's global information
00264   LLVMContext &getContext() const { return Context; }
00265 
00266   /// Get any module-scope inline assembly blocks.
00267   /// @returns a string containing the module-scope inline assembly blocks.
00268   const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
00269 
00270   /// Get a RandomNumberGenerator salted for use with this module. The
00271   /// RNG can be seeded via -rng-seed=<uint64> and is salted with the
00272   /// ModuleID and the provided pass salt. The returned RNG should not
00273   /// be shared across threads or passes.
00274   ///
00275   /// A unique RNG per pass ensures a reproducible random stream even
00276   /// when other randomness consuming passes are added or removed. In
00277   /// addition, the random stream will be reproducible across LLVM
00278   /// versions when the pass does not change.
00279   RandomNumberGenerator *createRNG(const Pass* P) const;
00280 
00281 /// @}
00282 /// @name Module Level Mutators
00283 /// @{
00284 
00285   /// Set the module identifier.
00286   void setModuleIdentifier(StringRef ID) { ModuleID = ID; }
00287 
00288   /// Set the data layout
00289   void setDataLayout(StringRef Desc);
00290   void setDataLayout(const DataLayout *Other);
00291 
00292   /// Set the target triple.
00293   void setTargetTriple(StringRef T) { TargetTriple = T; }
00294 
00295   /// Set the module-scope inline assembly blocks.
00296   void setModuleInlineAsm(StringRef Asm) {
00297     GlobalScopeAsm = Asm;
00298     if (!GlobalScopeAsm.empty() &&
00299         GlobalScopeAsm[GlobalScopeAsm.size()-1] != '\n')
00300       GlobalScopeAsm += '\n';
00301   }
00302 
00303   /// Append to the module-scope inline assembly blocks, automatically inserting
00304   /// a separating newline if necessary.
00305   void appendModuleInlineAsm(StringRef Asm) {
00306     GlobalScopeAsm += Asm;
00307     if (!GlobalScopeAsm.empty() &&
00308         GlobalScopeAsm[GlobalScopeAsm.size()-1] != '\n')
00309       GlobalScopeAsm += '\n';
00310   }
00311 
00312 /// @}
00313 /// @name Generic Value Accessors
00314 /// @{
00315 
00316   /// Return the global value in the module with the specified name, of
00317   /// arbitrary type. This method returns null if a global with the specified
00318   /// name is not found.
00319   GlobalValue *getNamedValue(StringRef Name) const;
00320 
00321   /// Return a unique non-zero ID for the specified metadata kind. This ID is
00322   /// uniqued across modules in the current LLVMContext.
00323   unsigned getMDKindID(StringRef Name) const;
00324 
00325   /// Populate client supplied SmallVector with the name for custom metadata IDs
00326   /// registered in this LLVMContext.
00327   void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
00328 
00329   /// Return the type with the specified name, or null if there is none by that
00330   /// name.
00331   StructType *getTypeByName(StringRef Name) const;
00332 
00333   std::vector<StructType *> getIdentifiedStructTypes() const;
00334 
00335 /// @}
00336 /// @name Function Accessors
00337 /// @{
00338 
00339   /// Look up the specified function in the module symbol table. Four
00340   /// possibilities:
00341   ///   1. If it does not exist, add a prototype for the function and return it.
00342   ///   2. If it exists, and has a local linkage, the existing function is
00343   ///      renamed and a new one is inserted.
00344   ///   3. Otherwise, if the existing function has the correct prototype, return
00345   ///      the existing function.
00346   ///   4. Finally, the function exists but has the wrong prototype: return the
00347   ///      function with a constantexpr cast to the right prototype.
00348   Constant *getOrInsertFunction(StringRef Name, FunctionType *T,
00349                                 AttributeSet AttributeList);
00350 
00351   Constant *getOrInsertFunction(StringRef Name, FunctionType *T);
00352 
00353   /// Look up the specified function in the module symbol table. If it does not
00354   /// exist, add a prototype for the function and return it. This function
00355   /// guarantees to return a constant of pointer to the specified function type
00356   /// or a ConstantExpr BitCast of that type if the named function has a
00357   /// different type. This version of the method takes a null terminated list of
00358   /// function arguments, which makes it easier for clients to use.
00359   Constant *getOrInsertFunction(StringRef Name,
00360                                 AttributeSet AttributeList,
00361                                 Type *RetTy, ...) LLVM_END_WITH_NULL;
00362 
00363   /// Same as above, but without the attributes.
00364   Constant *getOrInsertFunction(StringRef Name, Type *RetTy, ...)
00365     LLVM_END_WITH_NULL;
00366 
00367   /// Look up the specified function in the module symbol table. If it does not
00368   /// exist, return null.
00369   Function *getFunction(StringRef Name) const;
00370 
00371 /// @}
00372 /// @name Global Variable Accessors
00373 /// @{
00374 
00375   /// Look up the specified global variable in the module symbol table. If it
00376   /// does not exist, return null. If AllowInternal is set to true, this
00377   /// function will return types that have InternalLinkage. By default, these
00378   /// types are not returned.
00379   GlobalVariable *getGlobalVariable(StringRef Name) const {
00380     return getGlobalVariable(Name, false);
00381   }
00382 
00383   GlobalVariable *getGlobalVariable(StringRef Name, bool AllowInternal) const {
00384     return const_cast<Module *>(this)->getGlobalVariable(Name, AllowInternal);
00385   }
00386 
00387   GlobalVariable *getGlobalVariable(StringRef Name, bool AllowInternal = false);
00388 
00389   /// Return the global variable in the module with the specified name, of
00390   /// arbitrary type. This method returns null if a global with the specified
00391   /// name is not found.
00392   GlobalVariable *getNamedGlobal(StringRef Name) {
00393     return getGlobalVariable(Name, true);
00394   }
00395   const GlobalVariable *getNamedGlobal(StringRef Name) const {
00396     return const_cast<Module *>(this)->getNamedGlobal(Name);
00397   }
00398 
00399   /// Look up the specified global in the module symbol table.
00400   ///   1. If it does not exist, add a declaration of the global and return it.
00401   ///   2. Else, the global exists but has the wrong type: return the function
00402   ///      with a constantexpr cast to the right type.
00403   ///   3. Finally, if the existing global is the correct declaration, return
00404   ///      the existing global.
00405   Constant *getOrInsertGlobal(StringRef Name, Type *Ty);
00406 
00407 /// @}
00408 /// @name Global Alias Accessors
00409 /// @{
00410 
00411   /// Return the global alias in the module with the specified name, of
00412   /// arbitrary type. This method returns null if a global with the specified
00413   /// name is not found.
00414   GlobalAlias *getNamedAlias(StringRef Name) const;
00415 
00416 /// @}
00417 /// @name Named Metadata Accessors
00418 /// @{
00419 
00420   /// Return the first NamedMDNode in the module with the specified name. This
00421   /// method returns null if a NamedMDNode with the specified name is not found.
00422   NamedMDNode *getNamedMetadata(const Twine &Name) const;
00423 
00424   /// Return the named MDNode in the module with the specified name. This method
00425   /// returns a new NamedMDNode if a NamedMDNode with the specified name is not
00426   /// found.
00427   NamedMDNode *getOrInsertNamedMetadata(StringRef Name);
00428 
00429   /// Remove the given NamedMDNode from this module and delete it.
00430   void eraseNamedMetadata(NamedMDNode *NMD);
00431 
00432 /// @}
00433 /// @name Comdat Accessors
00434 /// @{
00435 
00436   /// Return the Comdat in the module with the specified name. It is created
00437   /// if it didn't already exist.
00438   Comdat *getOrInsertComdat(StringRef Name);
00439 
00440 /// @}
00441 /// @name Module Flags Accessors
00442 /// @{
00443 
00444   /// Returns the module flags in the provided vector.
00445   void getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const;
00446 
00447   /// Return the corresponding value if Key appears in module flags, otherwise
00448   /// return null.
00449   Metadata *getModuleFlag(StringRef Key) const;
00450 
00451   /// Returns the NamedMDNode in the module that represents module-level flags.
00452   /// This method returns null if there are no module-level flags.
00453   NamedMDNode *getModuleFlagsMetadata() const;
00454 
00455   /// Returns the NamedMDNode in the module that represents module-level flags.
00456   /// If module-level flags aren't found, it creates the named metadata that
00457   /// contains them.
00458   NamedMDNode *getOrInsertModuleFlagsMetadata();
00459 
00460   /// Add a module-level flag to the module-level flags metadata. It will create
00461   /// the module-level flags named metadata if it doesn't already exist.
00462   void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, Metadata *Val);
00463   void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, Constant *Val);
00464   void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, uint32_t Val);
00465   void addModuleFlag(MDNode *Node);
00466 
00467 /// @}
00468 /// @name Materialization
00469 /// @{
00470 
00471   /// Sets the GVMaterializer to GVM. This module must not yet have a
00472   /// Materializer. To reset the materializer for a module that already has one,
00473   /// call MaterializeAllPermanently first. Destroying this module will destroy
00474   /// its materializer without materializing any more GlobalValues. Without
00475   /// destroying the Module, there is no way to detach or destroy a materializer
00476   /// without materializing all the GVs it controls, to avoid leaving orphan
00477   /// unmaterialized GVs.
00478   void setMaterializer(GVMaterializer *GVM);
00479   /// Retrieves the GVMaterializer, if any, for this Module.
00480   GVMaterializer *getMaterializer() const { return Materializer.get(); }
00481 
00482   /// Returns true if this GV was loaded from this Module's GVMaterializer and
00483   /// the GVMaterializer knows how to dematerialize the GV.
00484   bool isDematerializable(const GlobalValue *GV) const;
00485 
00486   /// Make sure the GlobalValue is fully read. If the module is corrupt, this
00487   /// returns true and fills in the optional string with information about the
00488   /// problem. If successful, this returns false.
00489   std::error_code materialize(GlobalValue *GV);
00490   /// If the GlobalValue is read in, and if the GVMaterializer supports it,
00491   /// release the memory for the function, and set it up to be materialized
00492   /// lazily. If !isDematerializable(), this method is a no-op.
00493   void Dematerialize(GlobalValue *GV);
00494 
00495   /// Make sure all GlobalValues in this Module are fully read.
00496   std::error_code materializeAll();
00497 
00498   /// Make sure all GlobalValues in this Module are fully read and clear the
00499   /// Materializer. If the module is corrupt, this DOES NOT clear the old
00500   /// Materializer.
00501   std::error_code materializeAllPermanently();
00502 
00503 /// @}
00504 /// @name Direct access to the globals list, functions list, and symbol table
00505 /// @{
00506 
00507   /// Get the Module's list of global variables (constant).
00508   const GlobalListType   &getGlobalList() const       { return GlobalList; }
00509   /// Get the Module's list of global variables.
00510   GlobalListType         &getGlobalList()             { return GlobalList; }
00511   static iplist<GlobalVariable> Module::*getSublistAccess(GlobalVariable*) {
00512     return &Module::GlobalList;
00513   }
00514   /// Get the Module's list of functions (constant).
00515   const FunctionListType &getFunctionList() const     { return FunctionList; }
00516   /// Get the Module's list of functions.
00517   FunctionListType       &getFunctionList()           { return FunctionList; }
00518   static iplist<Function> Module::*getSublistAccess(Function*) {
00519     return &Module::FunctionList;
00520   }
00521   /// Get the Module's list of aliases (constant).
00522   const AliasListType    &getAliasList() const        { return AliasList; }
00523   /// Get the Module's list of aliases.
00524   AliasListType          &getAliasList()              { return AliasList; }
00525   static iplist<GlobalAlias> Module::*getSublistAccess(GlobalAlias*) {
00526     return &Module::AliasList;
00527   }
00528   /// Get the Module's list of named metadata (constant).
00529   const NamedMDListType  &getNamedMDList() const      { return NamedMDList; }
00530   /// Get the Module's list of named metadata.
00531   NamedMDListType        &getNamedMDList()            { return NamedMDList; }
00532   static ilist<NamedMDNode> Module::*getSublistAccess(NamedMDNode*) {
00533     return &Module::NamedMDList;
00534   }
00535   /// Get the symbol table of global variable and function identifiers
00536   const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; }
00537   /// Get the Module's symbol table of global variable and function identifiers.
00538   ValueSymbolTable       &getValueSymbolTable()       { return *ValSymTab; }
00539   /// Get the Module's symbol table for COMDATs (constant).
00540   const ComdatSymTabType &getComdatSymbolTable() const { return ComdatSymTab; }
00541   /// Get the Module's symbol table for COMDATs.
00542   ComdatSymTabType &getComdatSymbolTable() { return ComdatSymTab; }
00543 
00544 /// @}
00545 /// @name Global Variable Iteration
00546 /// @{
00547 
00548   global_iterator       global_begin()       { return GlobalList.begin(); }
00549   const_global_iterator global_begin() const { return GlobalList.begin(); }
00550   global_iterator       global_end  ()       { return GlobalList.end(); }
00551   const_global_iterator global_end  () const { return GlobalList.end(); }
00552   bool                  global_empty() const { return GlobalList.empty(); }
00553 
00554   iterator_range<global_iterator> globals() {
00555     return iterator_range<global_iterator>(global_begin(), global_end());
00556   }
00557   iterator_range<const_global_iterator> globals() const {
00558     return iterator_range<const_global_iterator>(global_begin(), global_end());
00559   }
00560 
00561 /// @}
00562 /// @name Function Iteration
00563 /// @{
00564 
00565   iterator                begin()       { return FunctionList.begin(); }
00566   const_iterator          begin() const { return FunctionList.begin(); }
00567   iterator                end  ()       { return FunctionList.end();   }
00568   const_iterator          end  () const { return FunctionList.end();   }
00569   reverse_iterator        rbegin()      { return FunctionList.rbegin(); }
00570   const_reverse_iterator  rbegin() const{ return FunctionList.rbegin(); }
00571   reverse_iterator        rend()        { return FunctionList.rend(); }
00572   const_reverse_iterator  rend() const  { return FunctionList.rend(); }
00573   size_t                  size() const  { return FunctionList.size(); }
00574   bool                    empty() const { return FunctionList.empty(); }
00575 
00576   iterator_range<iterator> functions() {
00577     return iterator_range<iterator>(begin(), end());
00578   }
00579   iterator_range<const_iterator> functions() const {
00580     return iterator_range<const_iterator>(begin(), end());
00581   }
00582 
00583 /// @}
00584 /// @name Alias Iteration
00585 /// @{
00586 
00587   alias_iterator       alias_begin()            { return AliasList.begin(); }
00588   const_alias_iterator alias_begin() const      { return AliasList.begin(); }
00589   alias_iterator       alias_end  ()            { return AliasList.end();   }
00590   const_alias_iterator alias_end  () const      { return AliasList.end();   }
00591   size_t               alias_size () const      { return AliasList.size();  }
00592   bool                 alias_empty() const      { return AliasList.empty(); }
00593 
00594   iterator_range<alias_iterator> aliases() {
00595     return iterator_range<alias_iterator>(alias_begin(), alias_end());
00596   }
00597   iterator_range<const_alias_iterator> aliases() const {
00598     return iterator_range<const_alias_iterator>(alias_begin(), alias_end());
00599   }
00600 
00601 /// @}
00602 /// @name Named Metadata Iteration
00603 /// @{
00604 
00605   named_metadata_iterator named_metadata_begin() { return NamedMDList.begin(); }
00606   const_named_metadata_iterator named_metadata_begin() const {
00607     return NamedMDList.begin();
00608   }
00609 
00610   named_metadata_iterator named_metadata_end() { return NamedMDList.end(); }
00611   const_named_metadata_iterator named_metadata_end() const {
00612     return NamedMDList.end();
00613   }
00614 
00615   size_t named_metadata_size() const { return NamedMDList.size();  }
00616   bool named_metadata_empty() const { return NamedMDList.empty(); }
00617 
00618   iterator_range<named_metadata_iterator> named_metadata() {
00619     return iterator_range<named_metadata_iterator>(named_metadata_begin(),
00620                                                    named_metadata_end());
00621   }
00622   iterator_range<const_named_metadata_iterator> named_metadata() const {
00623     return iterator_range<const_named_metadata_iterator>(named_metadata_begin(),
00624                                                          named_metadata_end());
00625   }
00626 
00627 /// @}
00628 /// @name Utility functions for printing and dumping Module objects
00629 /// @{
00630 
00631   /// Print the module to an output stream with an optional
00632   /// AssemblyAnnotationWriter.
00633   void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const;
00634 
00635   /// Dump the module to stderr (for debugging).
00636   void dump() const;
00637   
00638   /// This function causes all the subinstructions to "let go" of all references
00639   /// that they are maintaining.  This allows one to 'delete' a whole class at
00640   /// a time, even though there may be circular references... first all
00641   /// references are dropped, and all use counts go to zero.  Then everything
00642   /// is delete'd for real.  Note that no operations are valid on an object
00643   /// that has "dropped all references", except operator delete.
00644   void dropAllReferences();
00645 
00646 /// @}
00647 /// @name Utility functions for querying Debug information.
00648 /// @{
00649 
00650   /// \brief Returns the Dwarf Version by checking module flags.
00651   unsigned getDwarfVersion() const;
00652 
00653 /// @}
00654 /// @name Utility functions for querying and setting PIC level
00655 /// @{
00656 
00657   /// \brief Returns the PIC level (small or large model)
00658   PICLevel::Level getPICLevel() const;
00659 
00660   /// \brief Set the PIC level (small or large model)
00661   void setPICLevel(PICLevel::Level PL);
00662 /// @}
00663 };
00664 
00665 /// An raw_ostream inserter for modules.
00666 inline raw_ostream &operator<<(raw_ostream &O, const Module &M) {
00667   M.print(O, nullptr);
00668   return O;
00669 }
00670 
00671 // Create wrappers for C Binding types (see CBindingWrapping.h).
00672 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Module, LLVMModuleRef)
00673 
00674 /* LLVMModuleProviderRef exists for historical reasons, but now just holds a
00675  * Module.
00676  */
00677 inline Module *unwrap(LLVMModuleProviderRef MP) {
00678   return reinterpret_cast<Module*>(MP);
00679 }
00680   
00681 } // End llvm namespace
00682 
00683 #endif