1 //===-- llvm/Module.h - C++ class to represent a VM module ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// Module.h This file contains the declarations for the Module class.
13 //===----------------------------------------------------------------------===//
18 #include "llvm/Function.h"
19 #include "llvm/GlobalVariable.h"
20 #include "llvm/GlobalAlias.h"
21 #include "llvm/Metadata.h"
22 #include "llvm/ADT/OwningPtr.h"
23 #include "llvm/System/DataTypes.h"
33 template<> struct ilist_traits<Function>
34 : public SymbolTableListTraits<Function, Module> {
36 // createSentinel is used to get hold of the node that marks the end of the
37 // list... (same trick used here as in ilist_traits<Instruction>)
38 Function *createSentinel() const {
39 return static_cast<Function*>(&Sentinel);
41 static void destroySentinel(Function*) {}
43 Function *provideInitialHead() const { return createSentinel(); }
44 Function *ensureHead(Function*) const { return createSentinel(); }
45 static void noteHead(Function*, Function*) {}
48 mutable ilist_node<Function> Sentinel;
50 template<> struct ilist_traits<GlobalVariable>
51 : public SymbolTableListTraits<GlobalVariable, Module> {
52 // createSentinel is used to create a node that marks the end of the list.
53 static GlobalVariable *createSentinel();
54 static void destroySentinel(GlobalVariable *GV) { delete GV; }
56 template<> struct ilist_traits<GlobalAlias>
57 : public SymbolTableListTraits<GlobalAlias, Module> {
58 // createSentinel is used to create a node that marks the end of the list.
59 static GlobalAlias *createSentinel();
60 static void destroySentinel(GlobalAlias *GA) { delete GA; }
63 template<> struct ilist_traits<NamedMDNode>
64 : public SymbolTableListTraits<NamedMDNode, Module> {
65 // createSentinel is used to get hold of a node that marks the end of
67 NamedMDNode *createSentinel() const {
68 return static_cast<NamedMDNode*>(&Sentinel);
70 static void destroySentinel(NamedMDNode*) {}
72 NamedMDNode *provideInitialHead() const { return createSentinel(); }
73 NamedMDNode *ensureHead(NamedMDNode*) const { return createSentinel(); }
74 static void noteHead(NamedMDNode*, NamedMDNode*) {}
75 void addNodeToList(NamedMDNode *N);
76 void removeNodeFromList(NamedMDNode *N);
78 mutable ilist_node<NamedMDNode> Sentinel;
81 /// A Module instance is used to store all the information related to an
82 /// LLVM module. Modules are the top level container of all other LLVM
83 /// Intermediate Representation (IR) objects. Each module directly contains a
84 /// list of globals variables, a list of functions, a list of libraries (or
85 /// other modules) this module depends on, a symbol table, and various data
86 /// about the target's characteristics.
88 /// A module maintains a GlobalValRefMap object that is used to hold all
89 /// constant references to global variables in the module. When a global
90 /// variable is destroyed, it should have no entries in the GlobalValueRefMap.
91 /// @brief The main container class for the LLVM Intermediate Representation.
93 /// @name Types And Enumerations
96 /// The type for the list of global variables.
97 typedef iplist<GlobalVariable> GlobalListType;
98 /// The type for the list of functions.
99 typedef iplist<Function> FunctionListType;
100 /// The type for the list of aliases.
101 typedef iplist<GlobalAlias> AliasListType;
102 /// The type for the list of named metadata.
103 typedef iplist<NamedMDNode> NamedMDListType;
105 /// The type for the list of dependent libraries.
106 typedef std::vector<std::string> LibraryListType;
108 /// The Global Variable iterator.
109 typedef GlobalListType::iterator global_iterator;
110 /// The Global Variable constant iterator.
111 typedef GlobalListType::const_iterator const_global_iterator;
113 /// The Function iterators.
114 typedef FunctionListType::iterator iterator;
115 /// The Function constant iterator
116 typedef FunctionListType::const_iterator const_iterator;
118 /// The Global Alias iterators.
119 typedef AliasListType::iterator alias_iterator;
120 /// The Global Alias constant iterator
121 typedef AliasListType::const_iterator const_alias_iterator;
123 /// The named metadata iterators.
124 typedef NamedMDListType::iterator named_metadata_iterator;
125 /// The named metadata constant interators.
126 typedef NamedMDListType::const_iterator const_named_metadata_iterator;
127 /// The Library list iterator.
128 typedef LibraryListType::const_iterator lib_iterator;
130 /// An enumeration for describing the endianess of the target machine.
131 enum Endianness { AnyEndianness, LittleEndian, BigEndian };
133 /// An enumeration for describing the size of a pointer on the target machine.
134 enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
137 /// @name Member Variables
140 LLVMContext &Context; ///< The LLVMContext from which types and
141 ///< constants are allocated.
142 GlobalListType GlobalList; ///< The Global Variables in the module
143 FunctionListType FunctionList; ///< The Functions in the module
144 AliasListType AliasList; ///< The Aliases in the module
145 LibraryListType LibraryList; ///< The Libraries needed by the module
146 NamedMDListType NamedMDList; ///< The named metadata in the module
147 std::string GlobalScopeAsm; ///< Inline Asm at global scope.
148 ValueSymbolTable *ValSymTab; ///< Symbol table for values
149 TypeSymbolTable *TypeSymTab; ///< Symbol table for types
150 OwningPtr<GVMaterializer> Materializer; ///< Used to materialize GlobalValues
151 std::string ModuleID; ///< Human readable identifier for the module
152 std::string TargetTriple; ///< Platform target triple Module compiled on
153 std::string DataLayout; ///< Target data description
154 MDSymbolTable *NamedMDSymTab; ///< NamedMDNode names.
156 friend class Constant;
159 /// @name Constructors
162 /// The Module constructor. Note that there is no default constructor. You
163 /// must provide a name for the module upon construction.
164 explicit Module(StringRef ModuleID, LLVMContext& C);
165 /// The module destructor. This will dropAllReferences.
169 /// @name Module Level Accessors
172 /// Get the module identifier which is, essentially, the name of the module.
173 /// @returns the module identifier as a string
174 const std::string &getModuleIdentifier() const { return ModuleID; }
176 /// Get the data layout string for the module's target platform. This encodes
177 /// the type sizes and alignments expected by this module.
178 /// @returns the data layout as a string
179 const std::string &getDataLayout() const { return DataLayout; }
181 /// Get the target triple which is a string describing the target host.
182 /// @returns a string containing the target triple.
183 const std::string &getTargetTriple() const { return TargetTriple; }
185 /// Get the target endian information.
186 /// @returns Endianess - an enumeration for the endianess of the target
187 Endianness getEndianness() const;
189 /// Get the target pointer size.
190 /// @returns PointerSize - an enumeration for the size of the target's pointer
191 PointerSize getPointerSize() const;
193 /// Get the global data context.
194 /// @returns LLVMContext - a container for LLVM's global information
195 LLVMContext &getContext() const { return Context; }
197 /// Get any module-scope inline assembly blocks.
198 /// @returns a string containing the module-scope inline assembly blocks.
199 const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
202 /// @name Module Level Mutators
205 /// Set the module identifier.
206 void setModuleIdentifier(StringRef ID) { ModuleID = ID; }
208 /// Set the data layout
209 void setDataLayout(StringRef DL) { DataLayout = DL; }
211 /// Set the target triple.
212 void setTargetTriple(StringRef T) { TargetTriple = T; }
214 /// Set the module-scope inline assembly blocks.
215 void setModuleInlineAsm(StringRef Asm) { GlobalScopeAsm = Asm; }
217 /// Append to the module-scope inline assembly blocks, automatically inserting
218 /// a separating newline if necessary.
219 void appendModuleInlineAsm(StringRef Asm) {
220 if (!GlobalScopeAsm.empty() &&
221 GlobalScopeAsm[GlobalScopeAsm.size()-1] != '\n')
222 GlobalScopeAsm += '\n';
223 GlobalScopeAsm += Asm;
227 /// @name Generic Value Accessors
230 /// getNamedValue - Return the first global value in the module with
231 /// the specified name, of arbitrary type. This method returns null
232 /// if a global with the specified name is not found.
233 GlobalValue *getNamedValue(StringRef Name) const;
235 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
236 /// This ID is uniqued across modules in the current LLVMContext.
237 unsigned getMDKindID(StringRef Name) const;
239 /// getMDKindNames - Populate client supplied SmallVector with the name for
240 /// custom metadata IDs registered in this LLVMContext. ID #0 is not used,
241 /// so it is filled in as an empty string.
242 void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
245 /// @name Function Accessors
248 /// getOrInsertFunction - Look up the specified function in the module symbol
249 /// table. Four possibilities:
250 /// 1. If it does not exist, add a prototype for the function and return it.
251 /// 2. If it exists, and has a local linkage, the existing function is
252 /// renamed and a new one is inserted.
253 /// 3. Otherwise, if the existing function has the correct prototype, return
254 /// the existing function.
255 /// 4. Finally, the function exists but has the wrong prototype: return the
256 /// function with a constantexpr cast to the right prototype.
257 Constant *getOrInsertFunction(StringRef Name, const FunctionType *T,
258 AttrListPtr AttributeList);
260 Constant *getOrInsertFunction(StringRef Name, const FunctionType *T);
262 /// getOrInsertFunction - Look up the specified function in the module symbol
263 /// table. If it does not exist, add a prototype for the function and return
264 /// it. This function guarantees to return a constant of pointer to the
265 /// specified function type or a ConstantExpr BitCast of that type if the
266 /// named function has a different type. This version of the method takes a
267 /// null terminated list of function arguments, which makes it easier for
269 Constant *getOrInsertFunction(StringRef Name,
270 AttrListPtr AttributeList,
271 const Type *RetTy, ...) END_WITH_NULL;
273 /// getOrInsertFunction - Same as above, but without the attributes.
274 Constant *getOrInsertFunction(StringRef Name, const Type *RetTy, ...)
277 Constant *getOrInsertTargetIntrinsic(StringRef Name,
278 const FunctionType *Ty,
279 AttrListPtr AttributeList);
281 /// getFunction - Look up the specified function in the module symbol table.
282 /// If it does not exist, return null.
283 Function *getFunction(StringRef Name) const;
286 /// @name Global Variable Accessors
289 /// getGlobalVariable - Look up the specified global variable in the module
290 /// symbol table. If it does not exist, return null. If AllowInternal is set
291 /// to true, this function will return types that have InternalLinkage. By
292 /// default, these types are not returned.
293 GlobalVariable *getGlobalVariable(StringRef Name,
294 bool AllowInternal = false) const;
296 /// getNamedGlobal - Return the first global variable in the module with the
297 /// specified name, of arbitrary type. This method returns null if a global
298 /// with the specified name is not found.
299 GlobalVariable *getNamedGlobal(StringRef Name) const {
300 return getGlobalVariable(Name, true);
303 /// getOrInsertGlobal - Look up the specified global in the module symbol
305 /// 1. If it does not exist, add a declaration of the global and return it.
306 /// 2. Else, the global exists but has the wrong type: return the function
307 /// with a constantexpr cast to the right type.
308 /// 3. Finally, if the existing global is the correct delclaration, return
309 /// the existing global.
310 Constant *getOrInsertGlobal(StringRef Name, const Type *Ty);
313 /// @name Global Alias Accessors
316 /// getNamedAlias - Return the first global alias in the module with the
317 /// specified name, of arbitrary type. This method returns null if a global
318 /// with the specified name is not found.
319 GlobalAlias *getNamedAlias(StringRef Name) const;
322 /// @name Named Metadata Accessors
325 /// getNamedMetadata - Return the first NamedMDNode in the module with the
326 /// specified name. This method returns null if a NamedMDNode with the
327 /// specified name is not found.
328 NamedMDNode *getNamedMetadata(StringRef Name) const;
329 NamedMDNode *getNamedMetadataUsingTwine(Twine Name) const;
331 /// getOrInsertNamedMetadata - Return the first named MDNode in the module
332 /// with the specified name. This method returns a new NamedMDNode if a
333 /// NamedMDNode with the specified name is not found.
334 NamedMDNode *getOrInsertNamedMetadata(StringRef Name);
337 /// @name Type Accessors
340 /// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
341 /// there is already an entry for this name, true is returned and the symbol
342 /// table is not modified.
343 bool addTypeName(StringRef Name, const Type *Ty);
345 /// getTypeName - If there is at least one entry in the symbol table for the
346 /// specified type, return it.
347 std::string getTypeName(const Type *Ty) const;
349 /// getTypeByName - Return the type with the specified name in this module, or
350 /// null if there is none by that name.
351 const Type *getTypeByName(StringRef Name) const;
354 /// @name Materialization
357 /// setMaterializer - Sets the GVMaterializer to GVM. This module must not
358 /// yet have a Materializer. To reset the materializer for a module that
359 /// already has one, call MaterializeAllPermanently first. Destroying this
360 /// module will destroy its materializer without materializing any more
361 /// GlobalValues. Without destroying the Module, there is no way to detach or
362 /// destroy a materializer without materializing all the GVs it controls, to
363 /// avoid leaving orphan unmaterialized GVs.
364 void setMaterializer(GVMaterializer *GVM);
365 /// getMaterializer - Retrieves the GVMaterializer, if any, for this Module.
366 GVMaterializer *getMaterializer() const { return Materializer.get(); }
368 /// isMaterializable - True if the definition of GV has yet to be materialized
369 /// from the GVMaterializer.
370 bool isMaterializable(const GlobalValue *GV) const;
371 /// isDematerializable - Returns true if this GV was loaded from this Module's
372 /// GVMaterializer and the GVMaterializer knows how to dematerialize the GV.
373 bool isDematerializable(const GlobalValue *GV) const;
375 /// Materialize - Make sure the GlobalValue is fully read. If the module is
376 /// corrupt, this returns true and fills in the optional string with
377 /// information about the problem. If successful, this returns false.
378 bool Materialize(GlobalValue *GV, std::string *ErrInfo = 0);
379 /// Dematerialize - If the GlobalValue is read in, and if the GVMaterializer
380 /// supports it, release the memory for the function, and set it up to be
381 /// materialized lazily. If !isDematerializable(), this method is a noop.
382 void Dematerialize(GlobalValue *GV);
384 /// MaterializeAll - Make sure all GlobalValues in this Module are fully read.
385 /// If the module is corrupt, this returns true and fills in the optional
386 /// string with information about the problem. If successful, this returns
388 bool MaterializeAll(std::string *ErrInfo = 0);
390 /// MaterializeAllPermanently - Make sure all GlobalValues in this Module are
391 /// fully read and clear the Materializer. If the module is corrupt, this
392 /// returns true, fills in the optional string with information about the
393 /// problem, and DOES NOT clear the old Materializer. If successful, this
395 bool MaterializeAllPermanently(std::string *ErrInfo = 0);
398 /// @name Direct access to the globals list, functions list, and symbol table
401 /// Get the Module's list of global variables (constant).
402 const GlobalListType &getGlobalList() const { return GlobalList; }
403 /// Get the Module's list of global variables.
404 GlobalListType &getGlobalList() { return GlobalList; }
405 static iplist<GlobalVariable> Module::*getSublistAccess(GlobalVariable*) {
406 return &Module::GlobalList;
408 /// Get the Module's list of functions (constant).
409 const FunctionListType &getFunctionList() const { return FunctionList; }
410 /// Get the Module's list of functions.
411 FunctionListType &getFunctionList() { return FunctionList; }
412 static iplist<Function> Module::*getSublistAccess(Function*) {
413 return &Module::FunctionList;
415 /// Get the Module's list of aliases (constant).
416 const AliasListType &getAliasList() const { return AliasList; }
417 /// Get the Module's list of aliases.
418 AliasListType &getAliasList() { return AliasList; }
419 static iplist<GlobalAlias> Module::*getSublistAccess(GlobalAlias*) {
420 return &Module::AliasList;
422 /// Get the Module's list of named metadata (constant).
423 const NamedMDListType &getNamedMDList() const { return NamedMDList; }
424 /// Get the Module's list of named metadata.
425 NamedMDListType &getNamedMDList() { return NamedMDList; }
426 static iplist<NamedMDNode> Module::*getSublistAccess(NamedMDNode *) {
427 return &Module::NamedMDList;
429 /// Get the symbol table of global variable and function identifiers
430 const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; }
431 /// Get the Module's symbol table of global variable and function identifiers.
432 ValueSymbolTable &getValueSymbolTable() { return *ValSymTab; }
433 /// Get the symbol table of types
434 const TypeSymbolTable &getTypeSymbolTable() const { return *TypeSymTab; }
435 /// Get the Module's symbol table of types
436 TypeSymbolTable &getTypeSymbolTable() { return *TypeSymTab; }
437 /// Get the symbol table of named metadata
438 const MDSymbolTable &getMDSymbolTable() const { return *NamedMDSymTab; }
439 /// Get the Module's symbol table of named metadata
440 MDSymbolTable &getMDSymbolTable() { return *NamedMDSymTab; }
443 /// @name Global Variable Iteration
446 /// Get an iterator to the first global variable
447 global_iterator global_begin() { return GlobalList.begin(); }
448 /// Get a constant iterator to the first global variable
449 const_global_iterator global_begin() const { return GlobalList.begin(); }
450 /// Get an iterator to the last global variable
451 global_iterator global_end () { return GlobalList.end(); }
452 /// Get a constant iterator to the last global variable
453 const_global_iterator global_end () const { return GlobalList.end(); }
454 /// Determine if the list of globals is empty.
455 bool global_empty() const { return GlobalList.empty(); }
458 /// @name Function Iteration
461 /// Get an iterator to the first function.
462 iterator begin() { return FunctionList.begin(); }
463 /// Get a constant iterator to the first function.
464 const_iterator begin() const { return FunctionList.begin(); }
465 /// Get an iterator to the last function.
466 iterator end () { return FunctionList.end(); }
467 /// Get a constant iterator to the last function.
468 const_iterator end () const { return FunctionList.end(); }
469 /// Determine how many functions are in the Module's list of functions.
470 size_t size() const { return FunctionList.size(); }
471 /// Determine if the list of functions is empty.
472 bool empty() const { return FunctionList.empty(); }
475 /// @name Dependent Library Iteration
478 /// @brief Get a constant iterator to beginning of dependent library list.
479 inline lib_iterator lib_begin() const { return LibraryList.begin(); }
480 /// @brief Get a constant iterator to end of dependent library list.
481 inline lib_iterator lib_end() const { return LibraryList.end(); }
482 /// @brief Returns the number of items in the list of libraries.
483 inline size_t lib_size() const { return LibraryList.size(); }
484 /// @brief Add a library to the list of dependent libraries
485 void addLibrary(StringRef Lib);
486 /// @brief Remove a library from the list of dependent libraries
487 void removeLibrary(StringRef Lib);
488 /// @brief Get all the libraries
489 inline const LibraryListType& getLibraries() const { return LibraryList; }
492 /// @name Alias Iteration
495 /// Get an iterator to the first alias.
496 alias_iterator alias_begin() { return AliasList.begin(); }
497 /// Get a constant iterator to the first alias.
498 const_alias_iterator alias_begin() const { return AliasList.begin(); }
499 /// Get an iterator to the last alias.
500 alias_iterator alias_end () { return AliasList.end(); }
501 /// Get a constant iterator to the last alias.
502 const_alias_iterator alias_end () const { return AliasList.end(); }
503 /// Determine how many aliases are in the Module's list of aliases.
504 size_t alias_size () const { return AliasList.size(); }
505 /// Determine if the list of aliases is empty.
506 bool alias_empty() const { return AliasList.empty(); }
510 /// @name Named Metadata Iteration
513 /// Get an iterator to the first named metadata.
514 named_metadata_iterator named_metadata_begin() { return NamedMDList.begin(); }
515 /// Get a constant iterator to the first named metadata.
516 const_named_metadata_iterator named_metadata_begin() const {
517 return NamedMDList.begin();
520 /// Get an iterator to the last named metadata.
521 named_metadata_iterator named_metadata_end() { return NamedMDList.end(); }
522 /// Get a constant iterator to the last named metadata.
523 const_named_metadata_iterator named_metadata_end() const {
524 return NamedMDList.end();
527 /// Determine how many NamedMDNodes are in the Module's list of named metadata.
528 size_t named_metadata_size() const { return NamedMDList.size(); }
529 /// Determine if the list of named metadata is empty.
530 bool named_metadata_empty() const { return NamedMDList.empty(); }
534 /// @name Utility functions for printing and dumping Module objects
537 /// Print the module to an output stream with AssemblyAnnotationWriter.
538 void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const;
540 /// Dump the module to stderr (for debugging).
542 /// This function causes all the subinstructions to "let go" of all references
543 /// that they are maintaining. This allows one to 'delete' a whole class at
544 /// a time, even though there may be circular references... first all
545 /// references are dropped, and all use counts go to zero. Then everything
546 /// is delete'd for real. Note that no operations are valid on an object
547 /// that has "dropped all references", except operator delete.
548 void dropAllReferences();
552 /// An raw_ostream inserter for modules.
553 inline raw_ostream &operator<<(raw_ostream &O, const Module &M) {
558 } // End llvm namespace