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"
27 class GlobalValueRefMap; // Used by ConstantVals.cpp
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
149 friend class Constant;
152 /// @name Constructors
155 /// The Module constructor. Note that there is no default constructor. You
156 /// must provide a name for the module upon construction.
157 explicit Module(const StringRef &ModuleID, LLVMContext& C);
158 /// The module destructor. This will dropAllReferences.
162 /// @name Module Level Accessors
165 /// Get the module identifier which is, essentially, the name of the module.
166 /// @returns the module identifier as a string
167 const std::string &getModuleIdentifier() const { return ModuleID; }
169 /// Get the data layout string for the module's target platform. This encodes
170 /// the type sizes and alignments expected by this module.
171 /// @returns the data layout as a string
172 const std::string &getDataLayout() const { return DataLayout; }
174 /// Get the target triple which is a string describing the target host.
175 /// @returns a string containing the target triple.
176 const std::string &getTargetTriple() const { return TargetTriple; }
178 /// Get the target endian information.
179 /// @returns Endianess - an enumeration for the endianess of the target
180 Endianness getEndianness() const;
182 /// Get the target pointer size.
183 /// @returns PointerSize - an enumeration for the size of the target's pointer
184 PointerSize getPointerSize() const;
186 /// Get the global data context.
187 /// @returns LLVMContext - a container for LLVM's global information
188 LLVMContext& getContext() const { return Context; }
190 /// Get any module-scope inline assembly blocks.
191 /// @returns a string containing the module-scope inline assembly blocks.
192 const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
194 /// @name Module Level Mutators
198 /// Set the module identifier.
199 void setModuleIdentifier(const StringRef &ID) { ModuleID = ID; }
201 /// Set the data layout
202 void setDataLayout(const StringRef &DL) { DataLayout = DL; }
204 /// Set the target triple.
205 void setTargetTriple(const StringRef &T) { TargetTriple = T; }
207 /// Set the module-scope inline assembly blocks.
208 void setModuleInlineAsm(const StringRef &Asm) { GlobalScopeAsm = Asm; }
210 /// Append to the module-scope inline assembly blocks, automatically
211 /// appending a newline to the end.
212 void appendModuleInlineAsm(const StringRef &Asm) {
213 GlobalScopeAsm += Asm;
214 GlobalScopeAsm += '\n';
218 /// @name Generic Value Accessors
221 /// getNamedValue - Return the first global value in the module with
222 /// the specified name, of arbitrary type. This method returns null
223 /// if a global with the specified name is not found.
224 GlobalValue *getNamedValue(const StringRef &Name) const;
227 /// @name Function Accessors
230 /// getOrInsertFunction - Look up the specified function in the module symbol
231 /// table. Four possibilities:
232 /// 1. If it does not exist, add a prototype for the function and return it.
233 /// 2. If it exists, and has a local linkage, the existing function is
234 /// renamed and a new one is inserted.
235 /// 3. Otherwise, if the existing function has the correct prototype, return
236 /// the existing function.
237 /// 4. Finally, the function exists but has the wrong prototype: return the
238 /// function with a constantexpr cast to the right prototype.
239 Constant *getOrInsertFunction(const StringRef &Name, const FunctionType *T,
240 AttrListPtr AttributeList);
242 Constant *getOrInsertFunction(const StringRef &Name, const FunctionType *T);
244 /// getOrInsertFunction - Look up the specified function in the module symbol
245 /// table. If it does not exist, add a prototype for the function and return
246 /// it. This function guarantees to return a constant of pointer to the
247 /// specified function type or a ConstantExpr BitCast of that type if the
248 /// named function has a different type. This version of the method takes a
249 /// null terminated list of function arguments, which makes it easier for
251 Constant *getOrInsertFunction(const StringRef &Name,
252 AttrListPtr AttributeList,
253 const Type *RetTy, ...) END_WITH_NULL;
255 /// getOrInsertFunction - Same as above, but without the attributes.
256 Constant *getOrInsertFunction(const StringRef &Name, const Type *RetTy, ...)
259 Constant *getOrInsertTargetIntrinsic(const StringRef &Name,
260 const FunctionType *Ty,
261 AttrListPtr AttributeList);
263 /// getFunction - Look up the specified function in the module symbol table.
264 /// If it does not exist, return null.
265 Function *getFunction(const StringRef &Name) const;
268 /// @name Global Variable Accessors
271 /// getGlobalVariable - Look up the specified global variable in the module
272 /// symbol table. If it does not exist, return null. If AllowInternal is set
273 /// to true, this function will return types that have InternalLinkage. By
274 /// default, these types are not returned.
275 GlobalVariable *getGlobalVariable(const StringRef &Name,
276 bool AllowInternal = false) const;
278 /// getNamedGlobal - Return the first global variable in the module with the
279 /// specified name, of arbitrary type. This method returns null if a global
280 /// with the specified name is not found.
281 GlobalVariable *getNamedGlobal(const StringRef &Name) const {
282 return getGlobalVariable(Name, true);
285 /// getOrInsertGlobal - Look up the specified global in the module symbol
287 /// 1. If it does not exist, add a declaration of the global and return it.
288 /// 2. Else, the global exists but has the wrong type: return the function
289 /// with a constantexpr cast to the right type.
290 /// 3. Finally, if the existing global is the correct delclaration, return
291 /// the existing global.
292 Constant *getOrInsertGlobal(const StringRef &Name, const Type *Ty);
295 /// @name Global Alias Accessors
298 /// getNamedAlias - Return the first global alias in the module with the
299 /// specified name, of arbitrary type. This method returns null if a global
300 /// with the specified name is not found.
301 GlobalAlias *getNamedAlias(const StringRef &Name) const;
304 /// @name Named Metadata Accessors
307 /// getNamedMetadata - Return the first NamedMDNode in the module with the
308 /// specified name. This method returns null if a NamedMDNode with the
309 /// specified name is not found.
310 NamedMDNode *getNamedMetadata(const StringRef &Name) const;
312 /// getOrInsertNamedMetadata - Return the first named MDNode in the module
313 /// with the specified name. This method returns a new NamedMDNode if a
314 /// NamedMDNode with the specified name is not found.
315 NamedMDNode *getOrInsertNamedMetadata(const StringRef &Name);
318 /// @name Type Accessors
321 /// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
322 /// there is already an entry for this name, true is returned and the symbol
323 /// table is not modified.
324 bool addTypeName(const StringRef &Name, const Type *Ty);
326 /// getTypeName - If there is at least one entry in the symbol table for the
327 /// specified type, return it.
328 std::string getTypeName(const Type *Ty) const;
330 /// getTypeByName - Return the type with the specified name in this module, or
331 /// null if there is none by that name.
332 const Type *getTypeByName(const StringRef &Name) const;
335 /// @name Direct access to the globals list, functions list, and symbol table
338 /// Get the Module's list of global variables (constant).
339 const GlobalListType &getGlobalList() const { return GlobalList; }
340 /// Get the Module's list of global variables.
341 GlobalListType &getGlobalList() { return GlobalList; }
342 static iplist<GlobalVariable> Module::*getSublistAccess(GlobalVariable*) {
343 return &Module::GlobalList;
345 /// Get the Module's list of functions (constant).
346 const FunctionListType &getFunctionList() const { return FunctionList; }
347 /// Get the Module's list of functions.
348 FunctionListType &getFunctionList() { return FunctionList; }
349 static iplist<Function> Module::*getSublistAccess(Function*) {
350 return &Module::FunctionList;
352 /// Get the Module's list of aliases (constant).
353 const AliasListType &getAliasList() const { return AliasList; }
354 /// Get the Module's list of aliases.
355 AliasListType &getAliasList() { return AliasList; }
356 static iplist<GlobalAlias> Module::*getSublistAccess(GlobalAlias*) {
357 return &Module::AliasList;
359 /// Get the Module's list of named metadata (constant).
360 const NamedMDListType &getNamedMDList() const { return NamedMDList; }
361 /// Get the Module's list of named metadata.
362 NamedMDListType &getNamedMDList() { return NamedMDList; }
363 static iplist<NamedMDNode> Module::*getSublistAccess(NamedMDNode *) {
364 return &Module::NamedMDList;
366 /// Get the symbol table of global variable and function identifiers
367 const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; }
368 /// Get the Module's symbol table of global variable and function identifiers.
369 ValueSymbolTable &getValueSymbolTable() { return *ValSymTab; }
370 /// Get the symbol table of types
371 const TypeSymbolTable &getTypeSymbolTable() const { return *TypeSymTab; }
372 /// Get the Module's symbol table of types
373 TypeSymbolTable &getTypeSymbolTable() { return *TypeSymTab; }
376 /// @name Global Variable Iteration
379 /// Get an iterator to the first global variable
380 global_iterator global_begin() { return GlobalList.begin(); }
381 /// Get a constant iterator to the first global variable
382 const_global_iterator global_begin() const { return GlobalList.begin(); }
383 /// Get an iterator to the last global variable
384 global_iterator global_end () { return GlobalList.end(); }
385 /// Get a constant iterator to the last global variable
386 const_global_iterator global_end () const { return GlobalList.end(); }
387 /// Determine if the list of globals is empty.
388 bool global_empty() const { return GlobalList.empty(); }
391 /// @name Function Iteration
394 /// Get an iterator to the first function.
395 iterator begin() { return FunctionList.begin(); }
396 /// Get a constant iterator to the first function.
397 const_iterator begin() const { return FunctionList.begin(); }
398 /// Get an iterator to the last function.
399 iterator end () { return FunctionList.end(); }
400 /// Get a constant iterator to the last function.
401 const_iterator end () const { return FunctionList.end(); }
402 /// Determine how many functions are in the Module's list of functions.
403 size_t size() const { return FunctionList.size(); }
404 /// Determine if the list of functions is empty.
405 bool empty() const { return FunctionList.empty(); }
408 /// @name Dependent Library Iteration
411 /// @brief Get a constant iterator to beginning of dependent library list.
412 inline lib_iterator lib_begin() const { return LibraryList.begin(); }
413 /// @brief Get a constant iterator to end of dependent library list.
414 inline lib_iterator lib_end() const { return LibraryList.end(); }
415 /// @brief Returns the number of items in the list of libraries.
416 inline size_t lib_size() const { return LibraryList.size(); }
417 /// @brief Add a library to the list of dependent libraries
418 void addLibrary(const StringRef &Lib);
419 /// @brief Remove a library from the list of dependent libraries
420 void removeLibrary(const StringRef &Lib);
421 /// @brief Get all the libraries
422 inline const LibraryListType& getLibraries() const { return LibraryList; }
425 /// @name Alias Iteration
428 /// Get an iterator to the first alias.
429 alias_iterator alias_begin() { return AliasList.begin(); }
430 /// Get a constant iterator to the first alias.
431 const_alias_iterator alias_begin() const { return AliasList.begin(); }
432 /// Get an iterator to the last alias.
433 alias_iterator alias_end () { return AliasList.end(); }
434 /// Get a constant iterator to the last alias.
435 const_alias_iterator alias_end () const { return AliasList.end(); }
436 /// Determine how many aliases are in the Module's list of aliases.
437 size_t alias_size () const { return AliasList.size(); }
438 /// Determine if the list of aliases is empty.
439 bool alias_empty() const { return AliasList.empty(); }
443 /// @name Named Metadata Iteration
446 /// Get an iterator to the first named metadata.
447 named_metadata_iterator named_metadata_begin()
448 { return NamedMDList.begin(); }
449 /// Get a constant iterator to the first named metadata.
450 const_named_metadata_iterator named_metadata_begin() const
451 { return NamedMDList.begin(); }
452 /// Get an iterator to the last named metadata.
453 named_metadata_iterator named_metadata_end ()
454 { return NamedMDList.end(); }
455 /// Get a constant iterator to the last named metadata.
456 const_named_metadata_iterator named_metadata_end () const
457 { return NamedMDList.end(); }
458 /// Determine how many NamedMDNodes are in the Module's list of named metadata.
459 size_t named_metadata_size () const
460 { return NamedMDList.size(); }
461 /// Determine if the list of named metadata is empty.
462 bool named_metadata_empty() const
463 { return NamedMDList.empty(); }
467 /// @name Utility functions for printing and dumping Module objects
470 /// Print the module to an output stream with AssemblyAnnotationWriter.
471 void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const;
473 /// Dump the module to stderr (for debugging).
475 /// This function causes all the subinstructions to "let go" of all references
476 /// that they are maintaining. This allows one to 'delete' a whole class at
477 /// a time, even though there may be circular references... first all
478 /// references are dropped, and all use counts go to zero. Then everything
479 /// is delete'd for real. Note that no operations are valid on an object
480 /// that has "dropped all references", except operator delete.
481 void dropAllReferences();
485 /// An raw_ostream inserter for modules.
486 inline raw_ostream &operator<<(raw_ostream &O, const Module &M) {
491 } // End llvm namespace