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