LCOV - code coverage report
Current view: top level - include/llvm/IR - Module.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 37 37 100.0 %
Date: 2018-07-13 00:08:38 Functions: 30 30 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- llvm/Module.h - C++ class to represent a VM module -------*- C++ -*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : /// @file
      11             : /// Module.h This file contains the declarations for the Module class.
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #ifndef LLVM_IR_MODULE_H
      16             : #define LLVM_IR_MODULE_H
      17             : 
      18             : #include "llvm-c/Types.h"
      19             : #include "llvm/ADT/STLExtras.h"
      20             : #include "llvm/ADT/StringMap.h"
      21             : #include "llvm/ADT/StringRef.h"
      22             : #include "llvm/ADT/iterator_range.h"
      23             : #include "llvm/IR/Attributes.h"
      24             : #include "llvm/IR/Comdat.h"
      25             : #include "llvm/IR/DataLayout.h"
      26             : #include "llvm/IR/Function.h"
      27             : #include "llvm/IR/GlobalAlias.h"
      28             : #include "llvm/IR/GlobalIFunc.h"
      29             : #include "llvm/IR/GlobalVariable.h"
      30             : #include "llvm/IR/Metadata.h"
      31             : #include "llvm/IR/SymbolTableListTraits.h"
      32             : #include "llvm/Support/CBindingWrapping.h"
      33             : #include "llvm/Support/CodeGen.h"
      34             : #include <cstddef>
      35             : #include <cstdint>
      36             : #include <iterator>
      37             : #include <memory>
      38             : #include <string>
      39             : #include <vector>
      40             : 
      41             : namespace llvm {
      42             : 
      43             : class Error;
      44             : class FunctionType;
      45             : class GVMaterializer;
      46             : class LLVMContext;
      47             : class MemoryBuffer;
      48             : class RandomNumberGenerator;
      49             : template <class PtrType> class SmallPtrSetImpl;
      50             : class StructType;
      51             : 
      52             : /// A Module instance is used to store all the information related to an
      53             : /// LLVM module. Modules are the top level container of all other LLVM
      54             : /// Intermediate Representation (IR) objects. Each module directly contains a
      55             : /// list of globals variables, a list of functions, a list of libraries (or
      56             : /// other modules) this module depends on, a symbol table, and various data
      57             : /// about the target's characteristics.
      58             : ///
      59             : /// A module maintains a GlobalValRefMap object that is used to hold all
      60             : /// constant references to global variables in the module.  When a global
      61             : /// variable is destroyed, it should have no entries in the GlobalValueRefMap.
      62             : /// The main container class for the LLVM Intermediate Representation.
      63             : class Module {
      64             : /// @name Types And Enumerations
      65             : /// @{
      66             : public:
      67             :   /// The type for the list of global variables.
      68             :   using GlobalListType = SymbolTableList<GlobalVariable>;
      69             :   /// The type for the list of functions.
      70             :   using FunctionListType = SymbolTableList<Function>;
      71             :   /// The type for the list of aliases.
      72             :   using AliasListType = SymbolTableList<GlobalAlias>;
      73             :   /// The type for the list of ifuncs.
      74             :   using IFuncListType = SymbolTableList<GlobalIFunc>;
      75             :   /// The type for the list of named metadata.
      76             :   using NamedMDListType = ilist<NamedMDNode>;
      77             :   /// The type of the comdat "symbol" table.
      78             :   using ComdatSymTabType = StringMap<Comdat>;
      79             : 
      80             :   /// The Global Variable iterator.
      81             :   using global_iterator = GlobalListType::iterator;
      82             :   /// The Global Variable constant iterator.
      83             :   using const_global_iterator = GlobalListType::const_iterator;
      84             : 
      85             :   /// The Function iterators.
      86             :   using iterator = FunctionListType::iterator;
      87             :   /// The Function constant iterator
      88             :   using const_iterator = FunctionListType::const_iterator;
      89             : 
      90             :   /// The Function reverse iterator.
      91             :   using reverse_iterator = FunctionListType::reverse_iterator;
      92             :   /// The Function constant reverse iterator.
      93             :   using const_reverse_iterator = FunctionListType::const_reverse_iterator;
      94             : 
      95             :   /// The Global Alias iterators.
      96             :   using alias_iterator = AliasListType::iterator;
      97             :   /// The Global Alias constant iterator
      98             :   using const_alias_iterator = AliasListType::const_iterator;
      99             : 
     100             :   /// The Global IFunc iterators.
     101             :   using ifunc_iterator = IFuncListType::iterator;
     102             :   /// The Global IFunc constant iterator
     103             :   using const_ifunc_iterator = IFuncListType::const_iterator;
     104             : 
     105             :   /// The named metadata iterators.
     106             :   using named_metadata_iterator = NamedMDListType::iterator;
     107             :   /// The named metadata constant iterators.
     108             :   using const_named_metadata_iterator = NamedMDListType::const_iterator;
     109             : 
     110             :   /// This enumeration defines the supported behaviors of module flags.
     111             :   enum ModFlagBehavior {
     112             :     /// Emits an error if two values disagree, otherwise the resulting value is
     113             :     /// that of the operands.
     114             :     Error = 1,
     115             : 
     116             :     /// Emits a warning if two values disagree. The result value will be the
     117             :     /// operand for the flag from the first module being linked.
     118             :     Warning = 2,
     119             : 
     120             :     /// Adds a requirement that another module flag be present and have a
     121             :     /// specified value after linking is performed. The value must be a metadata
     122             :     /// pair, where the first element of the pair is the ID of the module flag
     123             :     /// to be restricted, and the second element of the pair is the value the
     124             :     /// module flag should be restricted to. This behavior can be used to
     125             :     /// restrict the allowable results (via triggering of an error) of linking
     126             :     /// IDs with the **Override** behavior.
     127             :     Require = 3,
     128             : 
     129             :     /// Uses the specified value, regardless of the behavior or value of the
     130             :     /// other module. If both modules specify **Override**, but the values
     131             :     /// differ, an error will be emitted.
     132             :     Override = 4,
     133             : 
     134             :     /// Appends the two values, which are required to be metadata nodes.
     135             :     Append = 5,
     136             : 
     137             :     /// Appends the two values, which are required to be metadata
     138             :     /// nodes. However, duplicate entries in the second list are dropped
     139             :     /// during the append operation.
     140             :     AppendUnique = 6,
     141             : 
     142             :     /// Takes the max of the two values, which are required to be integers.
     143             :     Max = 7,
     144             : 
     145             :     // Markers:
     146             :     ModFlagBehaviorFirstVal = Error,
     147             :     ModFlagBehaviorLastVal = Max
     148             :   };
     149             : 
     150             :   /// Checks if Metadata represents a valid ModFlagBehavior, and stores the
     151             :   /// converted result in MFB.
     152             :   static bool isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB);
     153             : 
     154             :   struct ModuleFlagEntry {
     155             :     ModFlagBehavior Behavior;
     156             :     MDString *Key;
     157             :     Metadata *Val;
     158             : 
     159             :     ModuleFlagEntry(ModFlagBehavior B, MDString *K, Metadata *V)
     160     5776963 :         : Behavior(B), Key(K), Val(V) {}
     161             :   };
     162             : 
     163             : /// @}
     164             : /// @name Member Variables
     165             : /// @{
     166             : private:
     167             :   LLVMContext &Context;           ///< The LLVMContext from which types and
     168             :                                   ///< constants are allocated.
     169             :   GlobalListType GlobalList;      ///< The Global Variables in the module
     170             :   FunctionListType FunctionList;  ///< The Functions in the module
     171             :   AliasListType AliasList;        ///< The Aliases in the module
     172             :   IFuncListType IFuncList;        ///< The IFuncs in the module
     173             :   NamedMDListType NamedMDList;    ///< The named metadata in the module
     174             :   std::string GlobalScopeAsm;     ///< Inline Asm at global scope.
     175             :   ValueSymbolTable *ValSymTab;    ///< Symbol table for values
     176             :   ComdatSymTabType ComdatSymTab;  ///< Symbol table for COMDATs
     177             :   std::unique_ptr<MemoryBuffer>
     178             :   OwnedMemoryBuffer;              ///< Memory buffer directly owned by this
     179             :                                   ///< module, for legacy clients only.
     180             :   std::unique_ptr<GVMaterializer>
     181             :   Materializer;                   ///< Used to materialize GlobalValues
     182             :   std::string ModuleID;           ///< Human readable identifier for the module
     183             :   std::string SourceFileName;     ///< Original source file name for module,
     184             :                                   ///< recorded in bitcode.
     185             :   std::string TargetTriple;       ///< Platform target triple Module compiled on
     186             :                                   ///< Format: (arch)(sub)-(vendor)-(sys0-(abi)
     187             :   void *NamedMDSymTab;            ///< NamedMDNode names.
     188             :   DataLayout DL;                  ///< DataLayout associated with the module
     189             : 
     190             :   friend class Constant;
     191             : 
     192             : /// @}
     193             : /// @name Constructors
     194             : /// @{
     195             : public:
     196             :   /// The Module constructor. Note that there is no default constructor. You
     197             :   /// must provide a name for the module upon construction.
     198             :   explicit Module(StringRef ModuleID, LLVMContext& C);
     199             :   /// The module destructor. This will dropAllReferences.
     200             :   ~Module();
     201             : 
     202             : /// @}
     203             : /// @name Module Level Accessors
     204             : /// @{
     205             : 
     206             :   /// Get the module identifier which is, essentially, the name of the module.
     207             :   /// @returns the module identifier as a string
     208       18218 :   const std::string &getModuleIdentifier() const { return ModuleID; }
     209             : 
     210             :   /// Returns the number of non-debug IR instructions in the module.
     211             :   /// This is equivalent to the sum of the IR instruction counts of each
     212             :   /// function contained in the module.
     213             :   unsigned getInstructionCount();
     214             : 
     215             :   /// Get the module's original source file name. When compiling from
     216             :   /// bitcode, this is taken from a bitcode record where it was recorded.
     217             :   /// For other compiles it is the same as the ModuleID, which would
     218             :   /// contain the source file name.
     219             :   const std::string &getSourceFileName() const { return SourceFileName; }
     220             : 
     221             :   /// Get a short "name" for the module.
     222             :   ///
     223             :   /// This is useful for debugging or logging. It is essentially a convenience
     224             :   /// wrapper around getModuleIdentifier().
     225             :   StringRef getName() const { return ModuleID; }
     226             : 
     227             :   /// Get the data layout string for the module's target platform. This is
     228             :   /// equivalent to getDataLayout()->getStringRepresentation().
     229             :   const std::string &getDataLayoutStr() const {
     230             :     return DL.getStringRepresentation();
     231             :   }
     232             : 
     233             :   /// Get the data layout for the module's target platform.
     234             :   const DataLayout &getDataLayout() const;
     235             : 
     236             :   /// Get the target triple which is a string describing the target host.
     237             :   /// @returns a string containing the target triple.
     238      250529 :   const std::string &getTargetTriple() const { return TargetTriple; }
     239             : 
     240             :   /// Get the global data context.
     241             :   /// @returns LLVMContext - a container for LLVM's global information
     242             :   LLVMContext &getContext() const { return Context; }
     243             : 
     244             :   /// Get any module-scope inline assembly blocks.
     245             :   /// @returns a string containing the module-scope inline assembly blocks.
     246          97 :   const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
     247             : 
     248             :   /// Get a RandomNumberGenerator salted for use with this module. The
     249             :   /// RNG can be seeded via -rng-seed=<uint64> and is salted with the
     250             :   /// ModuleID and the provided pass salt. The returned RNG should not
     251             :   /// be shared across threads or passes.
     252             :   ///
     253             :   /// A unique RNG per pass ensures a reproducible random stream even
     254             :   /// when other randomness consuming passes are added or removed. In
     255             :   /// addition, the random stream will be reproducible across LLVM
     256             :   /// versions when the pass does not change.
     257             :   std::unique_ptr<RandomNumberGenerator> createRNG(const Pass* P) const;
     258             : 
     259             : /// @}
     260             : /// @name Module Level Mutators
     261             : /// @{
     262             : 
     263             :   /// Set the module identifier.
     264         108 :   void setModuleIdentifier(StringRef ID) { ModuleID = ID; }
     265             : 
     266             :   /// Set the module's original source file name.
     267       15006 :   void setSourceFileName(StringRef Name) { SourceFileName = Name; }
     268             : 
     269             :   /// Set the data layout
     270             :   void setDataLayout(StringRef Desc);
     271             :   void setDataLayout(const DataLayout &Other);
     272             : 
     273             :   /// Set the target triple.
     274      102069 :   void setTargetTriple(StringRef T) { TargetTriple = T; }
     275             : 
     276             :   /// Set the module-scope inline assembly blocks.
     277             :   /// A trailing newline is added if the input doesn't have one.
     278         451 :   void setModuleInlineAsm(StringRef Asm) {
     279         902 :     GlobalScopeAsm = Asm;
     280         618 :     if (!GlobalScopeAsm.empty() && GlobalScopeAsm.back() != '\n')
     281             :       GlobalScopeAsm += '\n';
     282         451 :   }
     283             : 
     284             :   /// Append to the module-scope inline assembly blocks.
     285             :   /// A trailing newline is added if the input doesn't have one.
     286         367 :   void appendModuleInlineAsm(StringRef Asm) {
     287         367 :     GlobalScopeAsm += Asm;
     288         729 :     if (!GlobalScopeAsm.empty() && GlobalScopeAsm.back() != '\n')
     289             :       GlobalScopeAsm += '\n';
     290         367 :   }
     291             : 
     292             : /// @}
     293             : /// @name Generic Value Accessors
     294             : /// @{
     295             : 
     296             :   /// Return the global value in the module with the specified name, of
     297             :   /// arbitrary type. This method returns null if a global with the specified
     298             :   /// name is not found.
     299             :   GlobalValue *getNamedValue(StringRef Name) const;
     300             : 
     301             :   /// Return a unique non-zero ID for the specified metadata kind. This ID is
     302             :   /// uniqued across modules in the current LLVMContext.
     303             :   unsigned getMDKindID(StringRef Name) const;
     304             : 
     305             :   /// Populate client supplied SmallVector with the name for custom metadata IDs
     306             :   /// registered in this LLVMContext.
     307             :   void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
     308             : 
     309             :   /// Populate client supplied SmallVector with the bundle tags registered in
     310             :   /// this LLVMContext.  The bundle tags are ordered by increasing bundle IDs.
     311             :   /// \see LLVMContext::getOperandBundleTagID
     312             :   void getOperandBundleTags(SmallVectorImpl<StringRef> &Result) const;
     313             : 
     314             :   /// Return the type with the specified name, or null if there is none by that
     315             :   /// name.
     316             :   StructType *getTypeByName(StringRef Name) const;
     317             : 
     318             :   std::vector<StructType *> getIdentifiedStructTypes() const;
     319             : 
     320             : /// @}
     321             : /// @name Function Accessors
     322             : /// @{
     323             : 
     324             :   /// Look up the specified function in the module symbol table. Four
     325             :   /// possibilities:
     326             :   ///   1. If it does not exist, add a prototype for the function and return it.
     327             :   ///   2. If it exists, and has a local linkage, the existing function is
     328             :   ///      renamed and a new one is inserted.
     329             :   ///   3. Otherwise, if the existing function has the correct prototype, return
     330             :   ///      the existing function.
     331             :   ///   4. Finally, the function exists but has the wrong prototype: return the
     332             :   ///      function with a constantexpr cast to the right prototype.
     333             :   Constant *getOrInsertFunction(StringRef Name, FunctionType *T,
     334             :                                 AttributeList AttributeList);
     335             : 
     336             :   Constant *getOrInsertFunction(StringRef Name, FunctionType *T);
     337             : 
     338             :   /// Look up the specified function in the module symbol table. If it does not
     339             :   /// exist, add a prototype for the function and return it. This function
     340             :   /// guarantees to return a constant of pointer to the specified function type
     341             :   /// or a ConstantExpr BitCast of that type if the named function has a
     342             :   /// different type. This version of the method takes a list of
     343             :   /// function arguments, which makes it easier for clients to use.
     344             :   template<typename... ArgsTy>
     345       45934 :   Constant *getOrInsertFunction(StringRef Name,
     346             :                                 AttributeList AttributeList,
     347             :                                 Type *RetTy, ArgsTy... Args)
     348             :   {
     349       86488 :     SmallVector<Type*, sizeof...(ArgsTy)> ArgTys{Args...};
     350             :     return getOrInsertFunction(Name,
     351             :                                FunctionType::get(RetTy, ArgTys, false),
     352       91868 :                                AttributeList);
     353             :   }
     354             : 
     355             :   /// Same as above, but without the attributes.
     356             :   template<typename... ArgsTy>
     357             :   Constant *getOrInsertFunction(StringRef Name, Type *RetTy, ArgsTy... Args) {
     358       15919 :     return getOrInsertFunction(Name, AttributeList{}, RetTy, Args...);
     359             :   }
     360             : 
     361             :   /// Look up the specified function in the module symbol table. If it does not
     362             :   /// exist, return null.
     363             :   Function *getFunction(StringRef Name) const;
     364             : 
     365             : /// @}
     366             : /// @name Global Variable Accessors
     367             : /// @{
     368             : 
     369             :   /// Look up the specified global variable in the module symbol table. If it
     370             :   /// does not exist, return null. If AllowInternal is set to true, this
     371             :   /// function will return types that have InternalLinkage. By default, these
     372             :   /// types are not returned.
     373             :   GlobalVariable *getGlobalVariable(StringRef Name) const {
     374       14014 :     return getGlobalVariable(Name, false);
     375             :   }
     376             : 
     377             :   GlobalVariable *getGlobalVariable(StringRef Name, bool AllowInternal) const;
     378             : 
     379             :   GlobalVariable *getGlobalVariable(StringRef Name,
     380             :                                     bool AllowInternal = false) {
     381             :     return static_cast<const Module *>(this)->getGlobalVariable(Name,
     382       23153 :                                                                 AllowInternal);
     383             :   }
     384             : 
     385             :   /// Return the global variable in the module with the specified name, of
     386             :   /// arbitrary type. This method returns null if a global with the specified
     387             :   /// name is not found.
     388             :   const GlobalVariable *getNamedGlobal(StringRef Name) const {
     389       17062 :     return getGlobalVariable(Name, true);
     390             :   }
     391             :   GlobalVariable *getNamedGlobal(StringRef Name) {
     392             :     return const_cast<GlobalVariable *>(
     393             :                        static_cast<const Module *>(this)->getNamedGlobal(Name));
     394             :   }
     395             : 
     396             :   /// Look up the specified global in the module symbol table.
     397             :   ///   1. If it does not exist, add a declaration of the global and return it.
     398             :   ///   2. Else, the global exists but has the wrong type: return the function
     399             :   ///      with a constantexpr cast to the right type.
     400             :   ///   3. Finally, if the existing global is the correct declaration, return
     401             :   ///      the existing global.
     402             :   Constant *getOrInsertGlobal(StringRef Name, Type *Ty);
     403             : 
     404             : /// @}
     405             : /// @name Global Alias Accessors
     406             : /// @{
     407             : 
     408             :   /// Return the global alias in the module with the specified name, of
     409             :   /// arbitrary type. This method returns null if a global with the specified
     410             :   /// name is not found.
     411             :   GlobalAlias *getNamedAlias(StringRef Name) const;
     412             : 
     413             : /// @}
     414             : /// @name Global IFunc Accessors
     415             : /// @{
     416             : 
     417             :   /// Return the global ifunc in the module with the specified name, of
     418             :   /// arbitrary type. This method returns null if a global with the specified
     419             :   /// name is not found.
     420             :   GlobalIFunc *getNamedIFunc(StringRef Name) const;
     421             : 
     422             : /// @}
     423             : /// @name Named Metadata Accessors
     424             : /// @{
     425             : 
     426             :   /// Return the first NamedMDNode in the module with the specified name. This
     427             :   /// method returns null if a NamedMDNode with the specified name is not found.
     428             :   NamedMDNode *getNamedMetadata(const Twine &Name) const;
     429             : 
     430             :   /// Return the named MDNode in the module with the specified name. This method
     431             :   /// returns a new NamedMDNode if a NamedMDNode with the specified name is not
     432             :   /// found.
     433             :   NamedMDNode *getOrInsertNamedMetadata(StringRef Name);
     434             : 
     435             :   /// Remove the given NamedMDNode from this module and delete it.
     436             :   void eraseNamedMetadata(NamedMDNode *NMD);
     437             : 
     438             : /// @}
     439             : /// @name Comdat Accessors
     440             : /// @{
     441             : 
     442             :   /// Return the Comdat in the module with the specified name. It is created
     443             :   /// if it didn't already exist.
     444             :   Comdat *getOrInsertComdat(StringRef Name);
     445             : 
     446             : /// @}
     447             : /// @name Module Flags Accessors
     448             : /// @{
     449             : 
     450             :   /// Returns the module flags in the provided vector.
     451             :   void getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const;
     452             : 
     453             :   /// Return the corresponding value if Key appears in module flags, otherwise
     454             :   /// return null.
     455             :   Metadata *getModuleFlag(StringRef Key) const;
     456             : 
     457             :   /// Returns the NamedMDNode in the module that represents module-level flags.
     458             :   /// This method returns null if there are no module-level flags.
     459             :   NamedMDNode *getModuleFlagsMetadata() const;
     460             : 
     461             :   /// Returns the NamedMDNode in the module that represents module-level flags.
     462             :   /// If module-level flags aren't found, it creates the named metadata that
     463             :   /// contains them.
     464             :   NamedMDNode *getOrInsertModuleFlagsMetadata();
     465             : 
     466             :   /// Add a module-level flag to the module-level flags metadata. It will create
     467             :   /// the module-level flags named metadata if it doesn't already exist.
     468             :   void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, Metadata *Val);
     469             :   void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, Constant *Val);
     470             :   void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, uint32_t Val);
     471             :   void addModuleFlag(MDNode *Node);
     472             : 
     473             : /// @}
     474             : /// @name Materialization
     475             : /// @{
     476             : 
     477             :   /// Sets the GVMaterializer to GVM. This module must not yet have a
     478             :   /// Materializer. To reset the materializer for a module that already has one,
     479             :   /// call materializeAll first. Destroying this module will destroy
     480             :   /// its materializer without materializing any more GlobalValues. Without
     481             :   /// destroying the Module, there is no way to detach or destroy a materializer
     482             :   /// without materializing all the GVs it controls, to avoid leaving orphan
     483             :   /// unmaterialized GVs.
     484             :   void setMaterializer(GVMaterializer *GVM);
     485             :   /// Retrieves the GVMaterializer, if any, for this Module.
     486             :   GVMaterializer *getMaterializer() const { return Materializer.get(); }
     487             :   bool isMaterialized() const { return !getMaterializer(); }
     488             : 
     489             :   /// Make sure the GlobalValue is fully read.
     490             :   llvm::Error materialize(GlobalValue *GV);
     491             : 
     492             :   /// Make sure all GlobalValues in this Module are fully read and clear the
     493             :   /// Materializer.
     494             :   llvm::Error materializeAll();
     495             : 
     496             :   llvm::Error materializeMetadata();
     497             : 
     498             : /// @}
     499             : /// @name Direct access to the globals list, functions list, and symbol table
     500             : /// @{
     501             : 
     502             :   /// Get the Module's list of global variables (constant).
     503             :   const GlobalListType   &getGlobalList() const       { return GlobalList; }
     504             :   /// Get the Module's list of global variables.
     505             :   GlobalListType         &getGlobalList()             { return GlobalList; }
     506             : 
     507             :   static GlobalListType Module::*getSublistAccess(GlobalVariable*) {
     508             :     return &Module::GlobalList;
     509             :   }
     510             : 
     511             :   /// Get the Module's list of functions (constant).
     512             :   const FunctionListType &getFunctionList() const     { return FunctionList; }
     513             :   /// Get the Module's list of functions.
     514             :   FunctionListType       &getFunctionList()           { return FunctionList; }
     515             :   static FunctionListType Module::*getSublistAccess(Function*) {
     516             :     return &Module::FunctionList;
     517             :   }
     518             : 
     519             :   /// Get the Module's list of aliases (constant).
     520             :   const AliasListType    &getAliasList() const        { return AliasList; }
     521             :   /// Get the Module's list of aliases.
     522             :   AliasListType          &getAliasList()              { return AliasList; }
     523             : 
     524             :   static AliasListType Module::*getSublistAccess(GlobalAlias*) {
     525             :     return &Module::AliasList;
     526             :   }
     527             : 
     528             :   /// Get the Module's list of ifuncs (constant).
     529             :   const IFuncListType    &getIFuncList() const        { return IFuncList; }
     530             :   /// Get the Module's list of ifuncs.
     531             :   IFuncListType          &getIFuncList()              { return IFuncList; }
     532             : 
     533             :   static IFuncListType Module::*getSublistAccess(GlobalIFunc*) {
     534             :     return &Module::IFuncList;
     535             :   }
     536             : 
     537             :   /// Get the Module's list of named metadata (constant).
     538             :   const NamedMDListType  &getNamedMDList() const      { return NamedMDList; }
     539             :   /// Get the Module's list of named metadata.
     540             :   NamedMDListType        &getNamedMDList()            { return NamedMDList; }
     541             : 
     542             :   static NamedMDListType Module::*getSublistAccess(NamedMDNode*) {
     543             :     return &Module::NamedMDList;
     544             :   }
     545             : 
     546             :   /// Get the symbol table of global variable and function identifiers
     547             :   const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; }
     548             :   /// Get the Module's symbol table of global variable and function identifiers.
     549             :   ValueSymbolTable       &getValueSymbolTable()       { return *ValSymTab; }
     550             : 
     551             :   /// Get the Module's symbol table for COMDATs (constant).
     552             :   const ComdatSymTabType &getComdatSymbolTable() const { return ComdatSymTab; }
     553             :   /// Get the Module's symbol table for COMDATs.
     554        1660 :   ComdatSymTabType &getComdatSymbolTable() { return ComdatSymTab; }
     555             : 
     556             : /// @}
     557             : /// @name Global Variable Iteration
     558             : /// @{
     559             : 
     560             :   global_iterator       global_begin()       { return GlobalList.begin(); }
     561             :   const_global_iterator global_begin() const { return GlobalList.begin(); }
     562             :   global_iterator       global_end  ()       { return GlobalList.end(); }
     563             :   const_global_iterator global_end  () const { return GlobalList.end(); }
     564             :   bool                  global_empty() const { return GlobalList.empty(); }
     565             : 
     566             :   iterator_range<global_iterator> globals() {
     567             :     return make_range(global_begin(), global_end());
     568             :   }
     569             :   iterator_range<const_global_iterator> globals() const {
     570             :     return make_range(global_begin(), global_end());
     571             :   }
     572             : 
     573             : /// @}
     574             : /// @name Function Iteration
     575             : /// @{
     576             : 
     577             :   iterator                begin()       { return FunctionList.begin(); }
     578             :   const_iterator          begin() const { return FunctionList.begin(); }
     579             :   iterator                end  ()       { return FunctionList.end();   }
     580             :   const_iterator          end  () const { return FunctionList.end();   }
     581             :   reverse_iterator        rbegin()      { return FunctionList.rbegin(); }
     582             :   const_reverse_iterator  rbegin() const{ return FunctionList.rbegin(); }
     583             :   reverse_iterator        rend()        { return FunctionList.rend(); }
     584             :   const_reverse_iterator  rend() const  { return FunctionList.rend(); }
     585             :   size_t                  size() const  { return FunctionList.size(); }
     586             :   bool                    empty() const { return FunctionList.empty(); }
     587             : 
     588             :   iterator_range<iterator> functions() {
     589             :     return make_range(begin(), end());
     590             :   }
     591             :   iterator_range<const_iterator> functions() const {
     592             :     return make_range(begin(), end());
     593             :   }
     594             : 
     595             : /// @}
     596             : /// @name Alias Iteration
     597             : /// @{
     598             : 
     599             :   alias_iterator       alias_begin()            { return AliasList.begin(); }
     600             :   const_alias_iterator alias_begin() const      { return AliasList.begin(); }
     601             :   alias_iterator       alias_end  ()            { return AliasList.end();   }
     602             :   const_alias_iterator alias_end  () const      { return AliasList.end();   }
     603             :   size_t               alias_size () const      { return AliasList.size();  }
     604             :   bool                 alias_empty() const      { return AliasList.empty(); }
     605             : 
     606             :   iterator_range<alias_iterator> aliases() {
     607             :     return make_range(alias_begin(), alias_end());
     608             :   }
     609             :   iterator_range<const_alias_iterator> aliases() const {
     610             :     return make_range(alias_begin(), alias_end());
     611             :   }
     612             : 
     613             : /// @}
     614             : /// @name IFunc Iteration
     615             : /// @{
     616             : 
     617             :   ifunc_iterator       ifunc_begin()            { return IFuncList.begin(); }
     618             :   const_ifunc_iterator ifunc_begin() const      { return IFuncList.begin(); }
     619             :   ifunc_iterator       ifunc_end  ()            { return IFuncList.end();   }
     620             :   const_ifunc_iterator ifunc_end  () const      { return IFuncList.end();   }
     621             :   size_t               ifunc_size () const      { return IFuncList.size();  }
     622             :   bool                 ifunc_empty() const      { return IFuncList.empty(); }
     623             : 
     624             :   iterator_range<ifunc_iterator> ifuncs() {
     625             :     return make_range(ifunc_begin(), ifunc_end());
     626             :   }
     627             :   iterator_range<const_ifunc_iterator> ifuncs() const {
     628             :     return make_range(ifunc_begin(), ifunc_end());
     629             :   }
     630             : 
     631             :   /// @}
     632             :   /// @name Convenience iterators
     633             :   /// @{
     634             : 
     635             :   using global_object_iterator =
     636             :       concat_iterator<GlobalObject, iterator, global_iterator>;
     637             :   using const_global_object_iterator =
     638             :       concat_iterator<const GlobalObject, const_iterator,
     639             :                       const_global_iterator>;
     640             : 
     641             :   iterator_range<global_object_iterator> global_objects() {
     642             :     return concat<GlobalObject>(functions(), globals());
     643             :   }
     644             :   iterator_range<const_global_object_iterator> global_objects() const {
     645             :     return concat<const GlobalObject>(functions(), globals());
     646             :   }
     647             : 
     648             :   global_object_iterator global_object_begin() {
     649             :     return global_objects().begin();
     650             :   }
     651             :   global_object_iterator global_object_end() { return global_objects().end(); }
     652             : 
     653             :   const_global_object_iterator global_object_begin() const {
     654             :     return global_objects().begin();
     655             :   }
     656             :   const_global_object_iterator global_object_end() const {
     657             :     return global_objects().end();
     658             :   }
     659             : 
     660             :   using global_value_iterator =
     661             :       concat_iterator<GlobalValue, iterator, global_iterator, alias_iterator,
     662             :                       ifunc_iterator>;
     663             :   using const_global_value_iterator =
     664             :       concat_iterator<const GlobalValue, const_iterator, const_global_iterator,
     665             :                       const_alias_iterator, const_ifunc_iterator>;
     666             : 
     667             :   iterator_range<global_value_iterator> global_values() {
     668             :     return concat<GlobalValue>(functions(), globals(), aliases(), ifuncs());
     669             :   }
     670             :   iterator_range<const_global_value_iterator> global_values() const {
     671             :     return concat<const GlobalValue>(functions(), globals(), aliases(),
     672             :                                      ifuncs());
     673             :   }
     674             : 
     675             :   global_value_iterator global_value_begin() { return global_values().begin(); }
     676             :   global_value_iterator global_value_end() { return global_values().end(); }
     677             : 
     678             :   const_global_value_iterator global_value_begin() const {
     679             :     return global_values().begin();
     680             :   }
     681             :   const_global_value_iterator global_value_end() const {
     682             :     return global_values().end();
     683             :   }
     684             : 
     685             :   /// @}
     686             :   /// @name Named Metadata Iteration
     687             :   /// @{
     688             : 
     689             :   named_metadata_iterator named_metadata_begin() { return NamedMDList.begin(); }
     690             :   const_named_metadata_iterator named_metadata_begin() const {
     691             :     return NamedMDList.begin();
     692             :   }
     693             : 
     694             :   named_metadata_iterator named_metadata_end() { return NamedMDList.end(); }
     695             :   const_named_metadata_iterator named_metadata_end() const {
     696             :     return NamedMDList.end();
     697             :   }
     698             : 
     699             :   size_t named_metadata_size() const { return NamedMDList.size();  }
     700             :   bool named_metadata_empty() const { return NamedMDList.empty(); }
     701             : 
     702             :   iterator_range<named_metadata_iterator> named_metadata() {
     703             :     return make_range(named_metadata_begin(), named_metadata_end());
     704             :   }
     705             :   iterator_range<const_named_metadata_iterator> named_metadata() const {
     706             :     return make_range(named_metadata_begin(), named_metadata_end());
     707             :   }
     708             : 
     709             :   /// An iterator for DICompileUnits that skips those marked NoDebug.
     710             :   class debug_compile_units_iterator
     711             :       : public std::iterator<std::input_iterator_tag, DICompileUnit *> {
     712             :     NamedMDNode *CUs;
     713             :     unsigned Idx;
     714             : 
     715             :     void SkipNoDebugCUs();
     716             : 
     717             :   public:
     718             :     explicit debug_compile_units_iterator(NamedMDNode *CUs, unsigned Idx)
     719       91834 :         : CUs(CUs), Idx(Idx) {
     720       91834 :       SkipNoDebugCUs();
     721             :     }
     722             : 
     723             :     debug_compile_units_iterator &operator++() {
     724        7909 :       ++Idx;
     725        7909 :       SkipNoDebugCUs();
     726             :       return *this;
     727             :     }
     728             : 
     729             :     debug_compile_units_iterator operator++(int) {
     730             :       debug_compile_units_iterator T(*this);
     731             :       ++Idx;
     732             :       return T;
     733             :     }
     734             : 
     735             :     bool operator==(const debug_compile_units_iterator &I) const {
     736             :       return Idx == I.Idx;
     737             :     }
     738             : 
     739             :     bool operator!=(const debug_compile_units_iterator &I) const {
     740             :       return Idx != I.Idx;
     741             :     }
     742             : 
     743             :     DICompileUnit *operator*() const;
     744             :     DICompileUnit *operator->() const;
     745             :   };
     746             : 
     747       21789 :   debug_compile_units_iterator debug_compile_units_begin() const {
     748       21789 :     auto *CUs = getNamedMetadata("llvm.dbg.cu");
     749       21789 :     return debug_compile_units_iterator(CUs, 0);
     750             :   }
     751             : 
     752       21789 :   debug_compile_units_iterator debug_compile_units_end() const {
     753       21789 :     auto *CUs = getNamedMetadata("llvm.dbg.cu");
     754       43578 :     return debug_compile_units_iterator(CUs, CUs ? CUs->getNumOperands() : 0);
     755             :   }
     756             : 
     757             :   /// Return an iterator for all DICompileUnits listed in this Module's
     758             :   /// llvm.dbg.cu named metadata node and aren't explicitly marked as
     759             :   /// NoDebug.
     760       24128 :   iterator_range<debug_compile_units_iterator> debug_compile_units() const {
     761       24128 :     auto *CUs = getNamedMetadata("llvm.dbg.cu");
     762             :     return make_range(
     763             :         debug_compile_units_iterator(CUs, 0),
     764       72384 :         debug_compile_units_iterator(CUs, CUs ? CUs->getNumOperands() : 0));
     765             :   }
     766             : /// @}
     767             : 
     768             :   /// Destroy ConstantArrays in LLVMContext if they are not used.
     769             :   /// ConstantArrays constructed during linking can cause quadratic memory
     770             :   /// explosion. Releasing all unused constants can cause a 20% LTO compile-time
     771             :   /// slowdown for a large application.
     772             :   ///
     773             :   /// NOTE: Constants are currently owned by LLVMContext. This can then only
     774             :   /// be called where all uses of the LLVMContext are understood.
     775             :   void dropTriviallyDeadConstantArrays();
     776             : 
     777             : /// @name Utility functions for printing and dumping Module objects
     778             : /// @{
     779             : 
     780             :   /// Print the module to an output stream with an optional
     781             :   /// AssemblyAnnotationWriter.  If \c ShouldPreserveUseListOrder, then include
     782             :   /// uselistorder directives so that use-lists can be recreated when reading
     783             :   /// the assembly.
     784             :   void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW,
     785             :              bool ShouldPreserveUseListOrder = false,
     786             :              bool IsForDebug = false) const;
     787             : 
     788             :   /// Dump the module to stderr (for debugging).
     789             :   void dump() const;
     790             : 
     791             :   /// This function causes all the subinstructions to "let go" of all references
     792             :   /// that they are maintaining.  This allows one to 'delete' a whole class at
     793             :   /// a time, even though there may be circular references... first all
     794             :   /// references are dropped, and all use counts go to zero.  Then everything
     795             :   /// is delete'd for real.  Note that no operations are valid on an object
     796             :   /// that has "dropped all references", except operator delete.
     797             :   void dropAllReferences();
     798             : 
     799             : /// @}
     800             : /// @name Utility functions for querying Debug information.
     801             : /// @{
     802             : 
     803             :   /// Returns the Number of Register ParametersDwarf Version by checking
     804             :   /// module flags.
     805             :   unsigned getNumberRegisterParameters() const;
     806             : 
     807             :   /// Returns the Dwarf Version by checking module flags.
     808             :   unsigned getDwarfVersion() const;
     809             : 
     810             :   /// Returns the CodeView Version by checking module flags.
     811             :   /// Returns zero if not present in module.
     812             :   unsigned getCodeViewFlag() const;
     813             : 
     814             : /// @}
     815             : /// @name Utility functions for querying and setting PIC level
     816             : /// @{
     817             : 
     818             :   /// Returns the PIC level (small or large model)
     819             :   PICLevel::Level getPICLevel() const;
     820             : 
     821             :   /// Set the PIC level (small or large model)
     822             :   void setPICLevel(PICLevel::Level PL);
     823             : /// @}
     824             : 
     825             : /// @}
     826             : /// @name Utility functions for querying and setting PIE level
     827             : /// @{
     828             : 
     829             :   /// Returns the PIE level (small or large model)
     830             :   PIELevel::Level getPIELevel() const;
     831             : 
     832             :   /// Set the PIE level (small or large model)
     833             :   void setPIELevel(PIELevel::Level PL);
     834             : /// @}
     835             : 
     836             :   /// @name Utility functions for querying and setting PGO summary
     837             :   /// @{
     838             : 
     839             :   /// Attach profile summary metadata to this module.
     840             :   void setProfileSummary(Metadata *M);
     841             : 
     842             :   /// Returns profile summary metadata
     843             :   Metadata *getProfileSummary();
     844             :   /// @}
     845             : 
     846             :   /// Returns true if PLT should be avoided for RTLib calls.
     847             :   bool getRtLibUseGOT() const;
     848             : 
     849             :   /// Set that PLT should be avoid for RTLib calls.
     850             :   void setRtLibUseGOT();
     851             : 
     852             : 
     853             :   /// Take ownership of the given memory buffer.
     854             :   void setOwnedMemoryBuffer(std::unique_ptr<MemoryBuffer> MB);
     855             : };
     856             : 
     857             : /// Given "llvm.used" or "llvm.compiler.used" as a global name, collect
     858             : /// the initializer elements of that global in Set and return the global itself.
     859             : GlobalVariable *collectUsedGlobalVariables(const Module &M,
     860             :                                            SmallPtrSetImpl<GlobalValue *> &Set,
     861             :                                            bool CompilerUsed);
     862             : 
     863             : /// An raw_ostream inserter for modules.
     864             : inline raw_ostream &operator<<(raw_ostream &O, const Module &M) {
     865           6 :   M.print(O, nullptr);
     866             :   return O;
     867             : }
     868             : 
     869             : // Create wrappers for C Binding types (see CBindingWrapping.h).
     870             : DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Module, LLVMModuleRef)
     871             : 
     872             : /* LLVMModuleProviderRef exists for historical reasons, but now just holds a
     873             :  * Module.
     874             :  */
     875             : inline Module *unwrap(LLVMModuleProviderRef MP) {
     876             :   return reinterpret_cast<Module*>(MP);
     877             : }
     878             : 
     879             : } // end namespace llvm
     880             : 
     881             : #endif // LLVM_IR_MODULE_H

Generated by: LCOV version 1.13