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/Support/DataTypes.h"
26 class GlobalValueRefMap; // Used by ConstantVals.cpp
29 template<> struct ilist_traits<Function>
30 : public SymbolTableListTraits<Function, Module> {
32 // createSentinel is used to get hold of the node that marks the end of the
33 // list... (same trick used here as in ilist_traits<Instruction>)
34 Function *createSentinel() const {
35 return static_cast<Function*>(&Sentinel);
37 static void destroySentinel(Function*) {}
39 Function *provideInitialHead() const { return createSentinel(); }
40 Function *ensureHead(Function*) const { return createSentinel(); }
41 static void noteHead(Function*, Function*) {}
44 mutable ilist_node<Function> Sentinel;
46 template<> struct ilist_traits<GlobalVariable>
47 : public SymbolTableListTraits<GlobalVariable, Module> {
48 // createSentinel is used to create a node that marks the end of the list.
49 static GlobalVariable *createSentinel();
50 static void destroySentinel(GlobalVariable *GV) { delete GV; }
52 template<> struct ilist_traits<GlobalAlias>
53 : public SymbolTableListTraits<GlobalAlias, Module> {
54 // createSentinel is used to create a node that marks the end of the list.
55 static GlobalAlias *createSentinel();
56 static void destroySentinel(GlobalAlias *GA) { delete GA; }
59 /// A Module instance is used to store all the information related to an
60 /// LLVM module. Modules are the top level container of all other LLVM
61 /// Intermediate Representation (IR) objects. Each module directly contains a
62 /// list of globals variables, a list of functions, a list of libraries (or
63 /// other modules) this module depends on, a symbol table, and various data
64 /// about the target's characteristics.
66 /// A module maintains a GlobalValRefMap object that is used to hold all
67 /// constant references to global variables in the module. When a global
68 /// variable is destroyed, it should have no entries in the GlobalValueRefMap.
69 /// @brief The main container class for the LLVM Intermediate Representation.
71 /// @name Types And Enumerations
74 /// The type for the list of global variables.
75 typedef iplist<GlobalVariable> GlobalListType;
76 /// The type for the list of functions.
77 typedef iplist<Function> FunctionListType;
78 /// The type for the list of aliases.
79 typedef iplist<GlobalAlias> AliasListType;
81 /// The type for the list of dependent libraries.
82 typedef std::vector<std::string> LibraryListType;
84 /// The Global Variable iterator.
85 typedef GlobalListType::iterator global_iterator;
86 /// The Global Variable constant iterator.
87 typedef GlobalListType::const_iterator const_global_iterator;
89 /// The Function iterators.
90 typedef FunctionListType::iterator iterator;
91 /// The Function constant iterator
92 typedef FunctionListType::const_iterator const_iterator;
94 /// The Global Alias iterators.
95 typedef AliasListType::iterator alias_iterator;
96 /// The Global Alias constant iterator
97 typedef AliasListType::const_iterator const_alias_iterator;
99 /// The Library list iterator.
100 typedef LibraryListType::const_iterator lib_iterator;
102 /// An enumeration for describing the endianess of the target machine.
103 enum Endianness { AnyEndianness, LittleEndian, BigEndian };
105 /// An enumeration for describing the size of a pointer on the target machine.
106 enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
109 /// @name Member Variables
112 GlobalListType GlobalList; ///< The Global Variables in the module
113 FunctionListType FunctionList; ///< The Functions in the module
114 AliasListType AliasList; ///< The Aliases in the module
115 LibraryListType LibraryList; ///< The Libraries needed by the module
116 std::string GlobalScopeAsm; ///< Inline Asm at global scope.
117 ValueSymbolTable *ValSymTab; ///< Symbol table for values
118 TypeSymbolTable *TypeSymTab; ///< Symbol table for types
119 std::string ModuleID; ///< Human readable identifier for the module
120 std::string TargetTriple; ///< Platform target triple Module compiled on
121 std::string DataLayout; ///< Target data description
123 friend class Constant;
126 /// @name Constructors
129 /// The Module constructor. Note that there is no default constructor. You
130 /// must provide a name for the module upon construction.
131 explicit Module(const std::string &ModuleID);
132 /// The module destructor. This will dropAllReferences.
136 /// @name Module Level Accessors
139 /// Get the module identifier which is, essentially, the name of the module.
140 /// @returns the module identifier as a string
141 const std::string &getModuleIdentifier() const { return ModuleID; }
143 /// Get the data layout string for the module's target platform. This encodes
144 /// the type sizes and alignments expected by this module.
145 /// @returns the data layout as a string
146 const std::string& getDataLayout() const { return DataLayout; }
148 /// Get the target triple which is a string describing the target host.
149 /// @returns a string containing the target triple.
150 const std::string &getTargetTriple() const { return TargetTriple; }
152 /// Get the target endian information.
153 /// @returns Endianess - an enumeration for the endianess of the target
154 Endianness getEndianness() const;
156 /// Get the target pointer size.
157 /// @returns PointerSize - an enumeration for the size of the target's pointer
158 PointerSize getPointerSize() const;
160 /// Get any module-scope inline assembly blocks.
161 /// @returns a string containing the module-scope inline assembly blocks.
162 const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
164 /// @name Module Level Mutators
168 /// Set the module identifier.
169 void setModuleIdentifier(const std::string &ID) { ModuleID = ID; }
171 /// Set the data layout
172 void setDataLayout(const std::string& DL) { DataLayout = DL; }
174 /// Set the target triple.
175 void setTargetTriple(const std::string &T) { TargetTriple = T; }
177 /// Set the module-scope inline assembly blocks.
178 void setModuleInlineAsm(const std::string &Asm) { GlobalScopeAsm = Asm; }
180 /// Append to the module-scope inline assembly blocks, automatically
181 /// appending a newline to the end.
182 void appendModuleInlineAsm(const std::string &Asm) {
183 GlobalScopeAsm += Asm;
184 GlobalScopeAsm += '\n';
188 /// @name Generic Value Accessors
191 /// getNamedValue - Return the first global value in the module with
192 /// the specified name, of arbitrary type. This method returns null
193 /// if a global with the specified name is not found.
194 GlobalValue *getNamedValue(const std::string &Name) const;
195 GlobalValue *getNamedValue(const char *Name) const;
198 /// @name Function Accessors
201 /// getOrInsertFunction - Look up the specified function in the module symbol
202 /// table. Four possibilities:
203 /// 1. If it does not exist, add a prototype for the function and return it.
204 /// 2. If it exists, and has a local linkage, the existing function is
205 /// renamed and a new one is inserted.
206 /// 3. Otherwise, if the existing function has the correct prototype, return
207 /// the existing function.
208 /// 4. Finally, the function exists but has the wrong prototype: return the
209 /// function with a constantexpr cast to the right prototype.
210 Constant *getOrInsertFunction(const std::string &Name, const FunctionType *T,
211 AttrListPtr AttributeList);
213 Constant *getOrInsertFunction(const std::string &Name, const FunctionType *T);
215 /// getOrInsertFunction - Look up the specified function in the module symbol
216 /// table. If it does not exist, add a prototype for the function and return
217 /// it. This function guarantees to return a constant of pointer to the
218 /// specified function type or a ConstantExpr BitCast of that type if the
219 /// named function has a different type. This version of the method takes a
220 /// null terminated list of function arguments, which makes it easier for
222 Constant *getOrInsertFunction(const std::string &Name,
223 AttrListPtr AttributeList,
224 const Type *RetTy, ...) END_WITH_NULL;
226 Constant *getOrInsertFunction(const std::string &Name, const Type *RetTy, ...)
229 Constant *getOrInsertTargetIntrinsic(const std::string &Name,
230 const FunctionType *Ty,
231 AttrListPtr AttributeList);
233 /// getFunction - Look up the specified function in the module symbol table.
234 /// If it does not exist, return null.
235 Function *getFunction(const std::string &Name) const;
236 Function *getFunction(const char *Name) const;
239 /// @name Global Variable Accessors
242 /// getGlobalVariable - Look up the specified global variable in the module
243 /// symbol table. If it does not exist, return null. If AllowInternal is set
244 /// to true, this function will return types that have InternalLinkage. By
245 /// default, these types are not returned.
246 GlobalVariable *getGlobalVariable(const std::string &Name,
247 bool AllowInternal = false) const;
249 /// getNamedGlobal - Return the first global variable in the module with the
250 /// specified name, of arbitrary type. This method returns null if a global
251 /// with the specified name is not found.
252 GlobalVariable *getNamedGlobal(const std::string &Name) const {
253 return getGlobalVariable(Name, true);
256 /// getOrInsertGlobal - Look up the specified global in the module symbol
258 /// 1. If it does not exist, add a declaration of the global and return it.
259 /// 2. Else, the global exists but has the wrong type: return the function
260 /// with a constantexpr cast to the right type.
261 /// 3. Finally, if the existing global is the correct delclaration, return
262 /// the existing global.
263 Constant *getOrInsertGlobal(const std::string &Name, const Type *Ty);
266 /// @name Global Alias Accessors
269 /// getNamedAlias - Return the first global alias in the module with the
270 /// specified name, of arbitrary type. This method returns null if a global
271 /// with the specified name is not found.
272 GlobalAlias *getNamedAlias(const std::string &Name) const;
275 /// @name Type Accessors
278 /// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
279 /// there is already an entry for this name, true is returned and the symbol
280 /// table is not modified.
281 bool addTypeName(const std::string &Name, const Type *Ty);
283 /// getTypeName - If there is at least one entry in the symbol table for the
284 /// specified type, return it.
285 std::string getTypeName(const Type *Ty) const;
287 /// getTypeByName - Return the type with the specified name in this module, or
288 /// null if there is none by that name.
289 const Type *getTypeByName(const std::string &Name) const;
292 /// @name Direct access to the globals list, functions list, and symbol table
295 /// Get the Module's list of global variables (constant).
296 const GlobalListType &getGlobalList() const { return GlobalList; }
297 /// Get the Module's list of global variables.
298 GlobalListType &getGlobalList() { return GlobalList; }
299 static iplist<GlobalVariable> Module::*getSublistAccess(GlobalVariable*) {
300 return &Module::GlobalList;
302 /// Get the Module's list of functions (constant).
303 const FunctionListType &getFunctionList() const { return FunctionList; }
304 /// Get the Module's list of functions.
305 FunctionListType &getFunctionList() { return FunctionList; }
306 static iplist<Function> Module::*getSublistAccess(Function*) {
307 return &Module::FunctionList;
309 /// Get the Module's list of aliases (constant).
310 const AliasListType &getAliasList() const { return AliasList; }
311 /// Get the Module's list of aliases.
312 AliasListType &getAliasList() { return AliasList; }
313 static iplist<GlobalAlias> Module::*getSublistAccess(GlobalAlias*) {
314 return &Module::AliasList;
316 /// Get the symbol table of global variable and function identifiers
317 const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; }
318 /// Get the Module's symbol table of global variable and function identifiers.
319 ValueSymbolTable &getValueSymbolTable() { return *ValSymTab; }
320 /// Get the symbol table of types
321 const TypeSymbolTable &getTypeSymbolTable() const { return *TypeSymTab; }
322 /// Get the Module's symbol table of types
323 TypeSymbolTable &getTypeSymbolTable() { return *TypeSymTab; }
326 /// @name Global Variable Iteration
329 /// Get an iterator to the first global variable
330 global_iterator global_begin() { return GlobalList.begin(); }
331 /// Get a constant iterator to the first global variable
332 const_global_iterator global_begin() const { return GlobalList.begin(); }
333 /// Get an iterator to the last global variable
334 global_iterator global_end () { return GlobalList.end(); }
335 /// Get a constant iterator to the last global variable
336 const_global_iterator global_end () const { return GlobalList.end(); }
337 /// Determine if the list of globals is empty.
338 bool global_empty() const { return GlobalList.empty(); }
341 /// @name Function Iteration
344 /// Get an iterator to the first function.
345 iterator begin() { return FunctionList.begin(); }
346 /// Get a constant iterator to the first function.
347 const_iterator begin() const { return FunctionList.begin(); }
348 /// Get an iterator to the last function.
349 iterator end () { return FunctionList.end(); }
350 /// Get a constant iterator to the last function.
351 const_iterator end () const { return FunctionList.end(); }
352 /// Determine how many functions are in the Module's list of functions.
353 size_t size() const { return FunctionList.size(); }
354 /// Determine if the list of functions is empty.
355 bool empty() const { return FunctionList.empty(); }
358 /// @name Dependent Library Iteration
361 /// @brief Get a constant iterator to beginning of dependent library list.
362 inline lib_iterator lib_begin() const { return LibraryList.begin(); }
363 /// @brief Get a constant iterator to end of dependent library list.
364 inline lib_iterator lib_end() const { return LibraryList.end(); }
365 /// @brief Returns the number of items in the list of libraries.
366 inline size_t lib_size() const { return LibraryList.size(); }
367 /// @brief Add a library to the list of dependent libraries
368 void addLibrary(const std::string& Lib);
369 /// @brief Remove a library from the list of dependent libraries
370 void removeLibrary(const std::string& Lib);
371 /// @brief Get all the libraries
372 inline const LibraryListType& getLibraries() const { return LibraryList; }
375 /// @name Alias Iteration
378 /// Get an iterator to the first alias.
379 alias_iterator alias_begin() { return AliasList.begin(); }
380 /// Get a constant iterator to the first alias.
381 const_alias_iterator alias_begin() const { return AliasList.begin(); }
382 /// Get an iterator to the last alias.
383 alias_iterator alias_end () { return AliasList.end(); }
384 /// Get a constant iterator to the last alias.
385 const_alias_iterator alias_end () const { return AliasList.end(); }
386 /// Determine how many functions are in the Module's list of aliases.
387 size_t alias_size () const { return AliasList.size(); }
388 /// Determine if the list of aliases is empty.
389 bool alias_empty() const { return AliasList.empty(); }
392 /// @name Utility functions for printing and dumping Module objects
395 /// Print the module to an output stream with AssemblyAnnotationWriter.
396 void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const;
397 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
399 /// Dump the module to stderr (for debugging).
401 /// This function causes all the subinstructions to "let go" of all references
402 /// that they are maintaining. This allows one to 'delete' a whole class at
403 /// a time, even though there may be circular references... first all
404 /// references are dropped, and all use counts go to zero. Then everything
405 /// is delete'd for real. Note that no operations are valid on an object
406 /// that has "dropped all references", except operator delete.
407 void dropAllReferences();
411 /// An iostream inserter for modules.
412 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
416 inline raw_ostream &operator<<(raw_ostream &O, const Module &M) {
421 } // End llvm namespace