LLVM  mainline
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   DataLayout DL;                  ///< DataLayout associated with the module
00223 
00224   friend class Constant;
00225 
00226 /// @}
00227 /// @name Constructors
00228 /// @{
00229 public:
00230   /// The Module constructor. Note that there is no default constructor. You
00231   /// must provide a name for the module upon construction.
00232   explicit Module(StringRef ModuleID, LLVMContext& C);
00233   /// The module destructor. This will dropAllReferences.
00234   ~Module();
00235 
00236 /// @}
00237 /// @name Module Level Accessors
00238 /// @{
00239 
00240   /// Get the module identifier which is, essentially, the name of the module.
00241   /// @returns the module identifier as a string
00242   const std::string &getModuleIdentifier() const { return ModuleID; }
00243 
00244   /// \brief Get a short "name" for the module.
00245   ///
00246   /// This is useful for debugging or logging. It is essentially a convenience
00247   /// wrapper around getModuleIdentifier().
00248   StringRef getName() const { return ModuleID; }
00249 
00250   /// Get the data layout string for the module's target platform. This is
00251   /// equivalent to getDataLayout()->getStringRepresentation().
00252   const std::string getDataLayoutStr() const {
00253     return DL.getStringRepresentation();
00254   }
00255 
00256   /// Get the data layout for the module's target platform.
00257   const DataLayout &getDataLayout() const;
00258 
00259   /// Get the target triple which is a string describing the target host.
00260   /// @returns a string containing the target triple.
00261   const std::string &getTargetTriple() const { return TargetTriple; }
00262 
00263   /// Get the global data context.
00264   /// @returns LLVMContext - a container for LLVM's global information
00265   LLVMContext &getContext() const { return Context; }
00266 
00267   /// Get any module-scope inline assembly blocks.
00268   /// @returns a string containing the module-scope inline assembly blocks.
00269   const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
00270 
00271   /// Get a RandomNumberGenerator salted for use with this module. The
00272   /// RNG can be seeded via -rng-seed=<uint64> and is salted with the
00273   /// ModuleID and the provided pass salt. The returned RNG should not
00274   /// be shared across threads or passes.
00275   ///
00276   /// A unique RNG per pass ensures a reproducible random stream even
00277   /// when other randomness consuming passes are added or removed. In
00278   /// addition, the random stream will be reproducible across LLVM
00279   /// versions when the pass does not change.
00280   RandomNumberGenerator *createRNG(const Pass* P) const;
00281 
00282 /// @}
00283 /// @name Module Level Mutators
00284 /// @{
00285 
00286   /// Set the module identifier.
00287   void setModuleIdentifier(StringRef ID) { ModuleID = ID; }
00288 
00289   /// Set the data layout
00290   void setDataLayout(StringRef Desc);
00291   void setDataLayout(const DataLayout &Other);
00292 
00293   /// Set the target triple.
00294   void setTargetTriple(StringRef T) { TargetTriple = T; }
00295 
00296   /// Set the module-scope inline assembly blocks.
00297   /// A trailing newline is added if the input doesn't have one.
00298   void setModuleInlineAsm(StringRef Asm) {
00299     GlobalScopeAsm = Asm;
00300     if (!GlobalScopeAsm.empty() &&
00301         GlobalScopeAsm[GlobalScopeAsm.size()-1] != '\n')
00302       GlobalScopeAsm += '\n';
00303   }
00304 
00305   /// Append to the module-scope inline assembly blocks.
00306   /// A trailing newline is added if the input doesn't have one.
00307   void appendModuleInlineAsm(StringRef Asm) {
00308     GlobalScopeAsm += Asm;
00309     if (!GlobalScopeAsm.empty() &&
00310         GlobalScopeAsm[GlobalScopeAsm.size()-1] != '\n')
00311       GlobalScopeAsm += '\n';
00312   }
00313 
00314 /// @}
00315 /// @name Generic Value Accessors
00316 /// @{
00317 
00318   /// Return the global value in the module with the specified name, of
00319   /// arbitrary type. This method returns null if a global with the specified
00320   /// name is not found.
00321   GlobalValue *getNamedValue(StringRef Name) const;
00322 
00323   /// Return a unique non-zero ID for the specified metadata kind. This ID is
00324   /// uniqued across modules in the current LLVMContext.
00325   unsigned getMDKindID(StringRef Name) const;
00326 
00327   /// Populate client supplied SmallVector with the name for custom metadata IDs
00328   /// registered in this LLVMContext.
00329   void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
00330 
00331   /// Return the type with the specified name, or null if there is none by that
00332   /// name.
00333   StructType *getTypeByName(StringRef Name) const;
00334 
00335   std::vector<StructType *> getIdentifiedStructTypes() const;
00336 
00337 /// @}
00338 /// @name Function Accessors
00339 /// @{
00340 
00341   /// Look up the specified function in the module symbol table. Four
00342   /// possibilities:
00343   ///   1. If it does not exist, add a prototype for the function and return it.
00344   ///   2. If it exists, and has a local linkage, the existing function is
00345   ///      renamed and a new one is inserted.
00346   ///   3. Otherwise, if the existing function has the correct prototype, return
00347   ///      the existing function.
00348   ///   4. Finally, the function exists but has the wrong prototype: return the
00349   ///      function with a constantexpr cast to the right prototype.
00350   Constant *getOrInsertFunction(StringRef Name, FunctionType *T,
00351                                 AttributeSet AttributeList);
00352 
00353   Constant *getOrInsertFunction(StringRef Name, FunctionType *T);
00354 
00355   /// Look up the specified function in the module symbol table. If it does not
00356   /// exist, add a prototype for the function and return it. This function
00357   /// guarantees to return a constant of pointer to the specified function type
00358   /// or a ConstantExpr BitCast of that type if the named function has a
00359   /// different type. This version of the method takes a null terminated list of
00360   /// function arguments, which makes it easier for clients to use.
00361   Constant *getOrInsertFunction(StringRef Name,
00362                                 AttributeSet AttributeList,
00363                                 Type *RetTy, ...) LLVM_END_WITH_NULL;
00364 
00365   /// Same as above, but without the attributes.
00366   Constant *getOrInsertFunction(StringRef Name, Type *RetTy, ...)
00367     LLVM_END_WITH_NULL;
00368 
00369   /// Look up the specified function in the module symbol table. If it does not
00370   /// exist, return null.
00371   Function *getFunction(StringRef Name) const;
00372 
00373 /// @}
00374 /// @name Global Variable Accessors
00375 /// @{
00376 
00377   /// Look up the specified global variable in the module symbol table. If it
00378   /// does not exist, return null. If AllowInternal is set to true, this
00379   /// function will return types that have InternalLinkage. By default, these
00380   /// types are not returned.
00381   GlobalVariable *getGlobalVariable(StringRef Name) const {
00382     return getGlobalVariable(Name, false);
00383   }
00384 
00385   GlobalVariable *getGlobalVariable(StringRef Name, bool AllowInternal) const {
00386     return const_cast<Module *>(this)->getGlobalVariable(Name, AllowInternal);
00387   }
00388 
00389   GlobalVariable *getGlobalVariable(StringRef Name, bool AllowInternal = false);
00390 
00391   /// Return the global variable in the module with the specified name, of
00392   /// arbitrary type. This method returns null if a global with the specified
00393   /// name is not found.
00394   GlobalVariable *getNamedGlobal(StringRef Name) {
00395     return getGlobalVariable(Name, true);
00396   }
00397   const GlobalVariable *getNamedGlobal(StringRef Name) const {
00398     return const_cast<Module *>(this)->getNamedGlobal(Name);
00399   }
00400 
00401   /// Look up the specified global in the module symbol table.
00402   ///   1. If it does not exist, add a declaration of the global and return it.
00403   ///   2. Else, the global exists but has the wrong type: return the function
00404   ///      with a constantexpr cast to the right type.
00405   ///   3. Finally, if the existing global is the correct declaration, return
00406   ///      the existing global.
00407   Constant *getOrInsertGlobal(StringRef Name, Type *Ty);
00408 
00409 /// @}
00410 /// @name Global Alias Accessors
00411 /// @{
00412 
00413   /// Return the global alias in the module with the specified name, of
00414   /// arbitrary type. This method returns null if a global with the specified
00415   /// name is not found.
00416   GlobalAlias *getNamedAlias(StringRef Name) const;
00417 
00418 /// @}
00419 /// @name Named Metadata Accessors
00420 /// @{
00421 
00422   /// Return the first NamedMDNode in the module with the specified name. This
00423   /// method returns null if a NamedMDNode with the specified name is not found.
00424   NamedMDNode *getNamedMetadata(const Twine &Name) const;
00425 
00426   /// Return the named MDNode in the module with the specified name. This method
00427   /// returns a new NamedMDNode if a NamedMDNode with the specified name is not
00428   /// found.
00429   NamedMDNode *getOrInsertNamedMetadata(StringRef Name);
00430 
00431   /// Remove the given NamedMDNode from this module and delete it.
00432   void eraseNamedMetadata(NamedMDNode *NMD);
00433 
00434 /// @}
00435 /// @name Comdat Accessors
00436 /// @{
00437 
00438   /// Return the Comdat in the module with the specified name. It is created
00439   /// if it didn't already exist.
00440   Comdat *getOrInsertComdat(StringRef Name);
00441 
00442 /// @}
00443 /// @name Module Flags Accessors
00444 /// @{
00445 
00446   /// Returns the module flags in the provided vector.
00447   void getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const;
00448 
00449   /// Return the corresponding value if Key appears in module flags, otherwise
00450   /// return null.
00451   Metadata *getModuleFlag(StringRef Key) const;
00452 
00453   /// Returns the NamedMDNode in the module that represents module-level flags.
00454   /// This method returns null if there are no module-level flags.
00455   NamedMDNode *getModuleFlagsMetadata() const;
00456 
00457   /// Returns the NamedMDNode in the module that represents module-level flags.
00458   /// If module-level flags aren't found, it creates the named metadata that
00459   /// contains them.
00460   NamedMDNode *getOrInsertModuleFlagsMetadata();
00461 
00462   /// Add a module-level flag to the module-level flags metadata. It will create
00463   /// the module-level flags named metadata if it doesn't already exist.
00464   void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, Metadata *Val);
00465   void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, Constant *Val);
00466   void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, uint32_t Val);
00467   void addModuleFlag(MDNode *Node);
00468 
00469 /// @}
00470 /// @name Materialization
00471 /// @{
00472 
00473   /// Sets the GVMaterializer to GVM. This module must not yet have a
00474   /// Materializer. To reset the materializer for a module that already has one,
00475   /// call MaterializeAllPermanently first. Destroying this module will destroy
00476   /// its materializer without materializing any more GlobalValues. Without
00477   /// destroying the Module, there is no way to detach or destroy a materializer
00478   /// without materializing all the GVs it controls, to avoid leaving orphan
00479   /// unmaterialized GVs.
00480   void setMaterializer(GVMaterializer *GVM);
00481   /// Retrieves the GVMaterializer, if any, for this Module.
00482   GVMaterializer *getMaterializer() const { return Materializer.get(); }
00483 
00484   /// Returns true if this GV was loaded from this Module's GVMaterializer and
00485   /// the GVMaterializer knows how to dematerialize the GV.
00486   bool isDematerializable(const GlobalValue *GV) const;
00487 
00488   /// Make sure the GlobalValue is fully read. If the module is corrupt, this
00489   /// returns true and fills in the optional string with information about the
00490   /// problem. If successful, this returns false.
00491   std::error_code materialize(GlobalValue *GV);
00492   /// If the GlobalValue is read in, and if the GVMaterializer supports it,
00493   /// release the memory for the function, and set it up to be materialized
00494   /// lazily. If !isDematerializable(), this method is a no-op.
00495   void Dematerialize(GlobalValue *GV);
00496 
00497   /// Make sure all GlobalValues in this Module are fully read.
00498   std::error_code materializeAll();
00499 
00500   /// Make sure all GlobalValues in this Module are fully read and clear the
00501   /// Materializer. If the module is corrupt, this DOES NOT clear the old
00502   /// Materializer.
00503   std::error_code materializeAllPermanently();
00504 
00505   std::error_code materializeMetadata();
00506 
00507 /// @}
00508 /// @name Direct access to the globals list, functions list, and symbol table
00509 /// @{
00510 
00511   /// Get the Module's list of global variables (constant).
00512   const GlobalListType   &getGlobalList() const       { return GlobalList; }
00513   /// Get the Module's list of global variables.
00514   GlobalListType         &getGlobalList()             { return GlobalList; }
00515   static iplist<GlobalVariable> Module::*getSublistAccess(GlobalVariable*) {
00516     return &Module::GlobalList;
00517   }
00518   /// Get the Module's list of functions (constant).
00519   const FunctionListType &getFunctionList() const     { return FunctionList; }
00520   /// Get the Module's list of functions.
00521   FunctionListType       &getFunctionList()           { return FunctionList; }
00522   static iplist<Function> Module::*getSublistAccess(Function*) {
00523     return &Module::FunctionList;
00524   }
00525   /// Get the Module's list of aliases (constant).
00526   const AliasListType    &getAliasList() const        { return AliasList; }
00527   /// Get the Module's list of aliases.
00528   AliasListType          &getAliasList()              { return AliasList; }
00529   static iplist<GlobalAlias> Module::*getSublistAccess(GlobalAlias*) {
00530     return &Module::AliasList;
00531   }
00532   /// Get the Module's list of named metadata (constant).
00533   const NamedMDListType  &getNamedMDList() const      { return NamedMDList; }
00534   /// Get the Module's list of named metadata.
00535   NamedMDListType        &getNamedMDList()            { return NamedMDList; }
00536   static ilist<NamedMDNode> Module::*getSublistAccess(NamedMDNode*) {
00537     return &Module::NamedMDList;
00538   }
00539   /// Get the symbol table of global variable and function identifiers
00540   const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; }
00541   /// Get the Module's symbol table of global variable and function identifiers.
00542   ValueSymbolTable       &getValueSymbolTable()       { return *ValSymTab; }
00543   /// Get the Module's symbol table for COMDATs (constant).
00544   const ComdatSymTabType &getComdatSymbolTable() const { return ComdatSymTab; }
00545   /// Get the Module's symbol table for COMDATs.
00546   ComdatSymTabType &getComdatSymbolTable() { return ComdatSymTab; }
00547 
00548 /// @}
00549 /// @name Global Variable Iteration
00550 /// @{
00551 
00552   global_iterator       global_begin()       { return GlobalList.begin(); }
00553   const_global_iterator global_begin() const { return GlobalList.begin(); }
00554   global_iterator       global_end  ()       { return GlobalList.end(); }
00555   const_global_iterator global_end  () const { return GlobalList.end(); }
00556   bool                  global_empty() const { return GlobalList.empty(); }
00557 
00558   iterator_range<global_iterator> globals() {
00559     return iterator_range<global_iterator>(global_begin(), global_end());
00560   }
00561   iterator_range<const_global_iterator> globals() const {
00562     return iterator_range<const_global_iterator>(global_begin(), global_end());
00563   }
00564 
00565 /// @}
00566 /// @name Function Iteration
00567 /// @{
00568 
00569   iterator                begin()       { return FunctionList.begin(); }
00570   const_iterator          begin() const { return FunctionList.begin(); }
00571   iterator                end  ()       { return FunctionList.end();   }
00572   const_iterator          end  () const { return FunctionList.end();   }
00573   reverse_iterator        rbegin()      { return FunctionList.rbegin(); }
00574   const_reverse_iterator  rbegin() const{ return FunctionList.rbegin(); }
00575   reverse_iterator        rend()        { return FunctionList.rend(); }
00576   const_reverse_iterator  rend() const  { return FunctionList.rend(); }
00577   size_t                  size() const  { return FunctionList.size(); }
00578   bool                    empty() const { return FunctionList.empty(); }
00579 
00580   iterator_range<iterator> functions() {
00581     return iterator_range<iterator>(begin(), end());
00582   }
00583   iterator_range<const_iterator> functions() const {
00584     return iterator_range<const_iterator>(begin(), end());
00585   }
00586 
00587 /// @}
00588 /// @name Alias Iteration
00589 /// @{
00590 
00591   alias_iterator       alias_begin()            { return AliasList.begin(); }
00592   const_alias_iterator alias_begin() const      { return AliasList.begin(); }
00593   alias_iterator       alias_end  ()            { return AliasList.end();   }
00594   const_alias_iterator alias_end  () const      { return AliasList.end();   }
00595   size_t               alias_size () const      { return AliasList.size();  }
00596   bool                 alias_empty() const      { return AliasList.empty(); }
00597 
00598   iterator_range<alias_iterator> aliases() {
00599     return iterator_range<alias_iterator>(alias_begin(), alias_end());
00600   }
00601   iterator_range<const_alias_iterator> aliases() const {
00602     return iterator_range<const_alias_iterator>(alias_begin(), alias_end());
00603   }
00604 
00605 /// @}
00606 /// @name Named Metadata Iteration
00607 /// @{
00608 
00609   named_metadata_iterator named_metadata_begin() { return NamedMDList.begin(); }
00610   const_named_metadata_iterator named_metadata_begin() const {
00611     return NamedMDList.begin();
00612   }
00613 
00614   named_metadata_iterator named_metadata_end() { return NamedMDList.end(); }
00615   const_named_metadata_iterator named_metadata_end() const {
00616     return NamedMDList.end();
00617   }
00618 
00619   size_t named_metadata_size() const { return NamedMDList.size();  }
00620   bool named_metadata_empty() const { return NamedMDList.empty(); }
00621 
00622   iterator_range<named_metadata_iterator> named_metadata() {
00623     return iterator_range<named_metadata_iterator>(named_metadata_begin(),
00624                                                    named_metadata_end());
00625   }
00626   iterator_range<const_named_metadata_iterator> named_metadata() const {
00627     return iterator_range<const_named_metadata_iterator>(named_metadata_begin(),
00628                                                          named_metadata_end());
00629   }
00630 
00631   /// Destroy ConstantArrays in LLVMContext if they are not used.
00632   /// ConstantArrays constructed during linking can cause quadratic memory
00633   /// explosion. Releasing all unused constants can cause a 20% LTO compile-time
00634   /// slowdown for a large application.
00635   ///
00636   /// NOTE: Constants are currently owned by LLVMContext. This can then only
00637   /// be called where all uses of the LLVMContext are understood.
00638   void dropTriviallyDeadConstantArrays();
00639 
00640 /// @}
00641 /// @name Utility functions for printing and dumping Module objects
00642 /// @{
00643 
00644   /// Print the module to an output stream with an optional
00645   /// AssemblyAnnotationWriter.
00646   void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const;
00647 
00648   /// Dump the module to stderr (for debugging).
00649   void dump() const;
00650   
00651   /// This function causes all the subinstructions to "let go" of all references
00652   /// that they are maintaining.  This allows one to 'delete' a whole class at
00653   /// a time, even though there may be circular references... first all
00654   /// references are dropped, and all use counts go to zero.  Then everything
00655   /// is delete'd for real.  Note that no operations are valid on an object
00656   /// that has "dropped all references", except operator delete.
00657   void dropAllReferences();
00658 
00659 /// @}
00660 /// @name Utility functions for querying Debug information.
00661 /// @{
00662 
00663   /// \brief Returns the Dwarf Version by checking module flags.
00664   unsigned getDwarfVersion() const;
00665 
00666 /// @}
00667 /// @name Utility functions for querying and setting PIC level
00668 /// @{
00669 
00670   /// \brief Returns the PIC level (small or large model)
00671   PICLevel::Level getPICLevel() const;
00672 
00673   /// \brief Set the PIC level (small or large model)
00674   void setPICLevel(PICLevel::Level PL);
00675 /// @}
00676 };
00677 
00678 /// An raw_ostream inserter for modules.
00679 inline raw_ostream &operator<<(raw_ostream &O, const Module &M) {
00680   M.print(O, nullptr);
00681   return O;
00682 }
00683 
00684 // Create wrappers for C Binding types (see CBindingWrapping.h).
00685 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Module, LLVMModuleRef)
00686 
00687 /* LLVMModuleProviderRef exists for historical reasons, but now just holds a
00688  * Module.
00689  */
00690 inline Module *unwrap(LLVMModuleProviderRef MP) {
00691   return reinterpret_cast<Module*>(MP);
00692 }
00693   
00694 } // End llvm namespace
00695 
00696 #endif