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/System/DataTypes.h"
31 template<> struct ilist_traits<Function>
32 : public SymbolTableListTraits<Function, Module> {
34 // createSentinel is used to get hold of the node that marks the end of the
35 // list... (same trick used here as in ilist_traits<Instruction>)
36 Function *createSentinel() const {
37 return static_cast<Function*>(&Sentinel);
39 static void destroySentinel(Function*) {}
41 Function *provideInitialHead() const { return createSentinel(); }
42 Function *ensureHead(Function*) const { return createSentinel(); }
43 static void noteHead(Function*, Function*) {}
46 mutable ilist_node<Function> Sentinel;
48 template<> struct ilist_traits<GlobalVariable>
49 : public SymbolTableListTraits<GlobalVariable, Module> {
50 // createSentinel is used to create a node that marks the end of the list.
51 static GlobalVariable *createSentinel();
52 static void destroySentinel(GlobalVariable *GV) { delete GV; }
54 template<> struct ilist_traits<GlobalAlias>
55 : public SymbolTableListTraits<GlobalAlias, Module> {
56 // createSentinel is used to create a node that marks the end of the list.
57 static GlobalAlias *createSentinel();
58 static void destroySentinel(GlobalAlias *GA) { delete GA; }
60 template<> struct ilist_traits<NamedMDNode>
61 : public SymbolTableListTraits<NamedMDNode, Module> {
62 // createSentinel is used to get hold of a node that marks the end of
64 NamedMDNode *createSentinel() const {
65 return static_cast<NamedMDNode*>(&Sentinel);
67 static void destroySentinel(NamedMDNode*) {}
69 NamedMDNode *provideInitialHead() const { return createSentinel(); }
70 NamedMDNode *ensureHead(NamedMDNode*) const { return createSentinel(); }
71 static void noteHead(NamedMDNode*, NamedMDNode*) {}
73 mutable ilist_node<NamedMDNode> Sentinel;
76 /// A Module instance is used to store all the information related to an
77 /// LLVM module. Modules are the top level container of all other LLVM
78 /// Intermediate Representation (IR) objects. Each module directly contains a
79 /// list of globals variables, a list of functions, a list of libraries (or
80 /// other modules) this module depends on, a symbol table, and various data
81 /// about the target's characteristics.
83 /// A module maintains a GlobalValRefMap object that is used to hold all
84 /// constant references to global variables in the module. When a global
85 /// variable is destroyed, it should have no entries in the GlobalValueRefMap.
86 /// @brief The main container class for the LLVM Intermediate Representation.
88 /// @name Types And Enumerations
91 /// The type for the list of global variables.
92 typedef iplist<GlobalVariable> GlobalListType;
93 /// The type for the list of functions.
94 typedef iplist<Function> FunctionListType;
95 /// The type for the list of aliases.
96 typedef iplist<GlobalAlias> AliasListType;
97 /// The type for the list of named metadata.
98 typedef iplist<NamedMDNode> NamedMDListType;
100 /// The type for the list of dependent libraries.
101 typedef std::vector<std::string> LibraryListType;
103 /// The Global Variable iterator.
104 typedef GlobalListType::iterator global_iterator;
105 /// The Global Variable constant iterator.
106 typedef GlobalListType::const_iterator const_global_iterator;
108 /// The Function iterators.
109 typedef FunctionListType::iterator iterator;
110 /// The Function constant iterator
111 typedef FunctionListType::const_iterator const_iterator;
113 /// The Global Alias iterators.
114 typedef AliasListType::iterator alias_iterator;
115 /// The Global Alias constant iterator
116 typedef AliasListType::const_iterator const_alias_iterator;
118 /// The named metadata iterators.
119 typedef NamedMDListType::iterator named_metadata_iterator;
120 /// The named metadata constant interators.
121 typedef NamedMDListType::const_iterator const_named_metadata_iterator;
122 /// The Library list iterator.
123 typedef LibraryListType::const_iterator lib_iterator;
125 /// An enumeration for describing the endianess of the target machine.
126 enum Endianness { AnyEndianness, LittleEndian, BigEndian };
128 /// An enumeration for describing the size of a pointer on the target machine.
129 enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
132 /// @name Member Variables
135 LLVMContext &Context; ///< The LLVMContext from which types and
136 ///< constants are allocated.
137 GlobalListType GlobalList; ///< The Global Variables in the module
138 FunctionListType FunctionList; ///< The Functions in the module
139 AliasListType AliasList; ///< The Aliases in the module
140 LibraryListType LibraryList; ///< The Libraries needed by the module
141 NamedMDListType NamedMDList; ///< The named metadata in the module
142 std::string GlobalScopeAsm; ///< Inline Asm at global scope.
143 ValueSymbolTable *ValSymTab; ///< Symbol table for values
144 TypeSymbolTable *TypeSymTab; ///< Symbol table for types
145 std::string ModuleID; ///< Human readable identifier for the module
146 std::string TargetTriple; ///< Platform target triple Module compiled on
147 std::string DataLayout; ///< Target data description
148 MDSymbolTable *NamedMDSymTab; ///< NamedMDNode names.
150 friend class Constant;
153 /// @name Constructors
156 /// The Module constructor. Note that there is no default constructor. You
157 /// must provide a name for the module upon construction.
158 explicit Module(StringRef ModuleID, LLVMContext& C);
159 /// The module destructor. This will dropAllReferences.
163 /// @name Module Level Accessors
166 /// Get the module identifier which is, essentially, the name of the module.
167 /// @returns the module identifier as a string
168 const std::string &getModuleIdentifier() const { return ModuleID; }
170 /// Get the data layout string for the module's target platform. This encodes
171 /// the type sizes and alignments expected by this module.
172 /// @returns the data layout as a string
173 const std::string &getDataLayout() const { return DataLayout; }
175 /// Get the target triple which is a string describing the target host.
176 /// @returns a string containing the target triple.
177 const std::string &getTargetTriple() const { return TargetTriple; }
179 /// Get the target endian information.
180 /// @returns Endianess - an enumeration for the endianess of the target
181 Endianness getEndianness() const;
183 /// Get the target pointer size.
184 /// @returns PointerSize - an enumeration for the size of the target's pointer
185 PointerSize getPointerSize() const;
187 /// Get the global data context.
188 /// @returns LLVMContext - a container for LLVM's global information
189 LLVMContext &getContext() const { return Context; }
191 /// Get any module-scope inline assembly blocks.
192 /// @returns a string containing the module-scope inline assembly blocks.
193 const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
196 /// @name Module Level Mutators
199 /// Set the module identifier.
200 void setModuleIdentifier(StringRef ID) { ModuleID = ID; }
202 /// Set the data layout
203 void setDataLayout(StringRef DL) { DataLayout = DL; }
205 /// Set the target triple.
206 void setTargetTriple(StringRef T) { TargetTriple = T; }
208 /// Set the module-scope inline assembly blocks.
209 void setModuleInlineAsm(StringRef Asm) { GlobalScopeAsm = Asm; }
211 /// Append to the module-scope inline assembly blocks, automatically
212 /// appending a newline to the end.
213 void appendModuleInlineAsm(StringRef Asm) {
214 GlobalScopeAsm += Asm;
215 GlobalScopeAsm += '\n';
219 /// @name Generic Value Accessors
222 /// getNamedValue - Return the first global value in the module with
223 /// the specified name, of arbitrary type. This method returns null
224 /// if a global with the specified name is not found.
225 GlobalValue *getNamedValue(StringRef Name) const;
227 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
228 /// This ID is uniqued across modules in the current LLVMContext.
229 unsigned getMDKindID(StringRef Name) const;
231 /// getMDKindNames - Populate client supplied SmallVector with the name for
232 /// custom metadata IDs registered in this LLVMContext. ID #0 is not used,
233 /// so it is filled in as an empty string.
234 void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
237 /// @name Function Accessors
240 /// getOrInsertFunction - Look up the specified function in the module symbol
241 /// table. Four possibilities:
242 /// 1. If it does not exist, add a prototype for the function and return it.
243 /// 2. If it exists, and has a local linkage, the existing function is
244 /// renamed and a new one is inserted.
245 /// 3. Otherwise, if the existing function has the correct prototype, return
246 /// the existing function.
247 /// 4. Finally, the function exists but has the wrong prototype: return the
248 /// function with a constantexpr cast to the right prototype.
249 Constant *getOrInsertFunction(StringRef Name, const FunctionType *T,
250 AttrListPtr AttributeList);
252 Constant *getOrInsertFunction(StringRef Name, const FunctionType *T);
254 /// getOrInsertFunction - Look up the specified function in the module symbol
255 /// table. If it does not exist, add a prototype for the function and return
256 /// it. This function guarantees to return a constant of pointer to the
257 /// specified function type or a ConstantExpr BitCast of that type if the
258 /// named function has a different type. This version of the method takes a
259 /// null terminated list of function arguments, which makes it easier for
261 Constant *getOrInsertFunction(StringRef Name,
262 AttrListPtr AttributeList,
263 const Type *RetTy, ...) END_WITH_NULL;
265 /// getOrInsertFunction - Same as above, but without the attributes.
266 Constant *getOrInsertFunction(StringRef Name, const Type *RetTy, ...)
269 Constant *getOrInsertTargetIntrinsic(StringRef Name,
270 const FunctionType *Ty,
271 AttrListPtr AttributeList);
273 /// getFunction - Look up the specified function in the module symbol table.
274 /// If it does not exist, return null.
275 Function *getFunction(StringRef Name) const;
278 /// @name Global Variable Accessors
281 /// getGlobalVariable - Look up the specified global variable in the module
282 /// symbol table. If it does not exist, return null. If AllowInternal is set
283 /// to true, this function will return types that have InternalLinkage. By
284 /// default, these types are not returned.
285 GlobalVariable *getGlobalVariable(StringRef Name,
286 bool AllowInternal = false) const;
288 /// getNamedGlobal - Return the first global variable in the module with the
289 /// specified name, of arbitrary type. This method returns null if a global
290 /// with the specified name is not found.
291 GlobalVariable *getNamedGlobal(StringRef Name) const {
292 return getGlobalVariable(Name, true);
295 /// getOrInsertGlobal - Look up the specified global in the module symbol
297 /// 1. If it does not exist, add a declaration of the global and return it.
298 /// 2. Else, the global exists but has the wrong type: return the function
299 /// with a constantexpr cast to the right type.
300 /// 3. Finally, if the existing global is the correct delclaration, return
301 /// the existing global.
302 Constant *getOrInsertGlobal(StringRef Name, const Type *Ty);
305 /// @name Global Alias Accessors
308 /// getNamedAlias - Return the first global alias in the module with the
309 /// specified name, of arbitrary type. This method returns null if a global
310 /// with the specified name is not found.
311 GlobalAlias *getNamedAlias(StringRef Name) const;
314 /// @name Named Metadata Accessors
317 /// getNamedMetadata - Return the first NamedMDNode in the module with the
318 /// specified name. This method returns null if a NamedMDNode with the
319 /// specified name is not found.
320 NamedMDNode *getNamedMetadata(StringRef Name) const;
322 /// getOrInsertNamedMetadata - Return the first named MDNode in the module
323 /// with the specified name. This method returns a new NamedMDNode if a
324 /// NamedMDNode with the specified name is not found.
325 NamedMDNode *getOrInsertNamedMetadata(StringRef Name);
327 /// addMDNodeName - Insert an entry in the NamedMDNode symbol table mapping
329 void addMDNodeName(StringRef Name, NamedMDNode *NMD);
332 /// @name Type Accessors
335 /// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
336 /// there is already an entry for this name, true is returned and the symbol
337 /// table is not modified.
338 bool addTypeName(StringRef Name, const Type *Ty);
340 /// getTypeName - If there is at least one entry in the symbol table for the
341 /// specified type, return it.
342 std::string getTypeName(const Type *Ty) const;
344 /// getTypeByName - Return the type with the specified name in this module, or
345 /// null if there is none by that name.
346 const Type *getTypeByName(StringRef Name) const;
349 /// @name Direct access to the globals list, functions list, and symbol table
352 /// Get the Module's list of global variables (constant).
353 const GlobalListType &getGlobalList() const { return GlobalList; }
354 /// Get the Module's list of global variables.
355 GlobalListType &getGlobalList() { return GlobalList; }
356 static iplist<GlobalVariable> Module::*getSublistAccess(GlobalVariable*) {
357 return &Module::GlobalList;
359 /// Get the Module's list of functions (constant).
360 const FunctionListType &getFunctionList() const { return FunctionList; }
361 /// Get the Module's list of functions.
362 FunctionListType &getFunctionList() { return FunctionList; }
363 static iplist<Function> Module::*getSublistAccess(Function*) {
364 return &Module::FunctionList;
366 /// Get the Module's list of aliases (constant).
367 const AliasListType &getAliasList() const { return AliasList; }
368 /// Get the Module's list of aliases.
369 AliasListType &getAliasList() { return AliasList; }
370 static iplist<GlobalAlias> Module::*getSublistAccess(GlobalAlias*) {
371 return &Module::AliasList;
373 /// Get the Module's list of named metadata (constant).
374 const NamedMDListType &getNamedMDList() const { return NamedMDList; }
375 /// Get the Module's list of named metadata.
376 NamedMDListType &getNamedMDList() { return NamedMDList; }
377 static iplist<NamedMDNode> Module::*getSublistAccess(NamedMDNode *) {
378 return &Module::NamedMDList;
380 /// Get the symbol table of global variable and function identifiers
381 const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; }
382 /// Get the Module's symbol table of global variable and function identifiers.
383 ValueSymbolTable &getValueSymbolTable() { return *ValSymTab; }
384 /// Get the symbol table of types
385 const TypeSymbolTable &getTypeSymbolTable() const { return *TypeSymTab; }
386 /// Get the Module's symbol table of types
387 TypeSymbolTable &getTypeSymbolTable() { return *TypeSymTab; }
388 /// Get the symbol table of named metadata
389 const MDSymbolTable &getMDSymbolTable() const { return *NamedMDSymTab; }
390 /// Get the Module's symbol table of named metadata
391 MDSymbolTable &getMDSymbolTable() { return *NamedMDSymTab; }
394 /// @name Global Variable Iteration
397 /// Get an iterator to the first global variable
398 global_iterator global_begin() { return GlobalList.begin(); }
399 /// Get a constant iterator to the first global variable
400 const_global_iterator global_begin() const { return GlobalList.begin(); }
401 /// Get an iterator to the last global variable
402 global_iterator global_end () { return GlobalList.end(); }
403 /// Get a constant iterator to the last global variable
404 const_global_iterator global_end () const { return GlobalList.end(); }
405 /// Determine if the list of globals is empty.
406 bool global_empty() const { return GlobalList.empty(); }
409 /// @name Function Iteration
412 /// Get an iterator to the first function.
413 iterator begin() { return FunctionList.begin(); }
414 /// Get a constant iterator to the first function.
415 const_iterator begin() const { return FunctionList.begin(); }
416 /// Get an iterator to the last function.
417 iterator end () { return FunctionList.end(); }
418 /// Get a constant iterator to the last function.
419 const_iterator end () const { return FunctionList.end(); }
420 /// Determine how many functions are in the Module's list of functions.
421 size_t size() const { return FunctionList.size(); }
422 /// Determine if the list of functions is empty.
423 bool empty() const { return FunctionList.empty(); }
426 /// @name Dependent Library Iteration
429 /// @brief Get a constant iterator to beginning of dependent library list.
430 inline lib_iterator lib_begin() const { return LibraryList.begin(); }
431 /// @brief Get a constant iterator to end of dependent library list.
432 inline lib_iterator lib_end() const { return LibraryList.end(); }
433 /// @brief Returns the number of items in the list of libraries.
434 inline size_t lib_size() const { return LibraryList.size(); }
435 /// @brief Add a library to the list of dependent libraries
436 void addLibrary(StringRef Lib);
437 /// @brief Remove a library from the list of dependent libraries
438 void removeLibrary(StringRef Lib);
439 /// @brief Get all the libraries
440 inline const LibraryListType& getLibraries() const { return LibraryList; }
443 /// @name Alias Iteration
446 /// Get an iterator to the first alias.
447 alias_iterator alias_begin() { return AliasList.begin(); }
448 /// Get a constant iterator to the first alias.
449 const_alias_iterator alias_begin() const { return AliasList.begin(); }
450 /// Get an iterator to the last alias.
451 alias_iterator alias_end () { return AliasList.end(); }
452 /// Get a constant iterator to the last alias.
453 const_alias_iterator alias_end () const { return AliasList.end(); }
454 /// Determine how many aliases are in the Module's list of aliases.
455 size_t alias_size () const { return AliasList.size(); }
456 /// Determine if the list of aliases is empty.
457 bool alias_empty() const { return AliasList.empty(); }
461 /// @name Named Metadata Iteration
464 /// Get an iterator to the first named metadata.
465 named_metadata_iterator named_metadata_begin() { return NamedMDList.begin(); }
466 /// Get a constant iterator to the first named metadata.
467 const_named_metadata_iterator named_metadata_begin() const {
468 return NamedMDList.begin();
471 /// Get an iterator to the last named metadata.
472 named_metadata_iterator named_metadata_end() { return NamedMDList.end(); }
473 /// Get a constant iterator to the last named metadata.
474 const_named_metadata_iterator named_metadata_end() const {
475 return NamedMDList.end();
478 /// Determine how many NamedMDNodes are in the Module's list of named metadata.
479 size_t named_metadata_size() const { return NamedMDList.size(); }
480 /// Determine if the list of named metadata is empty.
481 bool named_metadata_empty() const { return NamedMDList.empty(); }
485 /// @name Utility functions for printing and dumping Module objects
488 /// Print the module to an output stream with AssemblyAnnotationWriter.
489 void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const;
491 /// Dump the module to stderr (for debugging).
493 /// This function causes all the subinstructions to "let go" of all references
494 /// that they are maintaining. This allows one to 'delete' a whole class at
495 /// a time, even though there may be circular references... first all
496 /// references are dropped, and all use counts go to zero. Then everything
497 /// is delete'd for real. Note that no operations are valid on an object
498 /// that has "dropped all references", except operator delete.
499 void dropAllReferences();
503 /// An raw_ostream inserter for modules.
504 inline raw_ostream &operator<<(raw_ostream &O, const Module &M) {
509 } // End llvm namespace