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