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
30 template<> struct ilist_traits<Function>
31 : public SymbolTableListTraits<Function, Module> {
33 // createSentinel is used to get hold of the node that marks the end of the
34 // list... (same trick used here as in ilist_traits<Instruction>)
35 Function *createSentinel() const {
36 return static_cast<Function*>(&Sentinel);
38 static void destroySentinel(Function*) {}
40 Function *provideInitialHead() const { return createSentinel(); }
41 Function *ensureHead(Function*) const { return createSentinel(); }
42 static void noteHead(Function*, Function*) {}
45 mutable ilist_node<Function> Sentinel;
47 template<> struct ilist_traits<GlobalVariable>
48 : public SymbolTableListTraits<GlobalVariable, Module> {
49 // createSentinel is used to create a node that marks the end of the list.
50 static GlobalVariable *createSentinel();
51 static void destroySentinel(GlobalVariable *GV) { delete GV; }
53 template<> struct ilist_traits<GlobalAlias>
54 : public SymbolTableListTraits<GlobalAlias, Module> {
55 // createSentinel is used to create a node that marks the end of the list.
56 static GlobalAlias *createSentinel();
57 static void destroySentinel(GlobalAlias *GA) { delete GA; }
60 /// A Module instance is used to store all the information related to an
61 /// LLVM module. Modules are the top level container of all other LLVM
62 /// Intermediate Representation (IR) objects. Each module directly contains a
63 /// list of globals variables, a list of functions, a list of libraries (or
64 /// other modules) this module depends on, a symbol table, and various data
65 /// about the target's characteristics.
67 /// A module maintains a GlobalValRefMap object that is used to hold all
68 /// constant references to global variables in the module. When a global
69 /// variable is destroyed, it should have no entries in the GlobalValueRefMap.
70 /// @brief The main container class for the LLVM Intermediate Representation.
72 /// @name Types And Enumerations
75 /// The type for the list of global variables.
76 typedef iplist<GlobalVariable> GlobalListType;
77 /// The type for the list of functions.
78 typedef iplist<Function> FunctionListType;
79 /// The type for the list of aliases.
80 typedef iplist<GlobalAlias> AliasListType;
82 /// The type for the list of dependent libraries.
83 typedef std::vector<std::string> LibraryListType;
85 /// The Global Variable iterator.
86 typedef GlobalListType::iterator global_iterator;
87 /// The Global Variable constant iterator.
88 typedef GlobalListType::const_iterator const_global_iterator;
90 /// The Function iterators.
91 typedef FunctionListType::iterator iterator;
92 /// The Function constant iterator
93 typedef FunctionListType::const_iterator const_iterator;
95 /// The Global Alias iterators.
96 typedef AliasListType::iterator alias_iterator;
97 /// The Global Alias constant iterator
98 typedef AliasListType::const_iterator const_alias_iterator;
100 /// The Library list iterator.
101 typedef LibraryListType::const_iterator lib_iterator;
103 /// An enumeration for describing the endianess of the target machine.
104 enum Endianness { AnyEndianness, LittleEndian, BigEndian };
106 /// An enumeration for describing the size of a pointer on the target machine.
107 enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
110 /// @name Member Variables
113 LLVMContext& Context; ///< The LLVMContext from which types and
114 ///< constants are allocated.
115 GlobalListType GlobalList; ///< The Global Variables in the module
116 FunctionListType FunctionList; ///< The Functions in the module
117 AliasListType AliasList; ///< The Aliases in the module
118 LibraryListType LibraryList; ///< The Libraries needed by the module
119 std::string GlobalScopeAsm; ///< Inline Asm at global scope.
120 ValueSymbolTable *ValSymTab; ///< Symbol table for values
121 TypeSymbolTable *TypeSymTab; ///< Symbol table for types
122 std::string ModuleID; ///< Human readable identifier for the module
123 std::string TargetTriple; ///< Platform target triple Module compiled on
124 std::string DataLayout; ///< Target data description
126 friend class Constant;
129 /// @name Constructors
132 /// The Module constructor. Note that there is no default constructor. You
133 /// must provide a name for the module upon construction.
134 explicit Module(const StringRef &ModuleID, LLVMContext& C);
135 /// The module destructor. This will dropAllReferences.
139 /// @name Module Level Accessors
142 /// Get the module identifier which is, essentially, the name of the module.
143 /// @returns the module identifier as a string
144 const std::string &getModuleIdentifier() const { return ModuleID; }
146 /// Get the data layout string for the module's target platform. This encodes
147 /// the type sizes and alignments expected by this module.
148 /// @returns the data layout as a string
149 const std::string &getDataLayout() const { return DataLayout; }
151 /// Get the target triple which is a string describing the target host.
152 /// @returns a string containing the target triple.
153 const std::string &getTargetTriple() const { return TargetTriple; }
155 /// Get the target endian information.
156 /// @returns Endianess - an enumeration for the endianess of the target
157 Endianness getEndianness() const;
159 /// Get the target pointer size.
160 /// @returns PointerSize - an enumeration for the size of the target's pointer
161 PointerSize getPointerSize() const;
163 /// Get the global data context.
164 /// @returns LLVMContext - a container for LLVM's global information
165 LLVMContext& getContext() const { return Context; }
167 /// Get any module-scope inline assembly blocks.
168 /// @returns a string containing the module-scope inline assembly blocks.
169 const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
171 /// @name Module Level Mutators
175 /// Set the module identifier.
176 void setModuleIdentifier(const StringRef &ID) { ModuleID = ID; }
178 /// Set the data layout
179 void setDataLayout(const StringRef &DL) { DataLayout = DL; }
181 /// Set the target triple.
182 void setTargetTriple(const StringRef &T) { TargetTriple = T; }
184 /// Set the module-scope inline assembly blocks.
185 void setModuleInlineAsm(const StringRef &Asm) { GlobalScopeAsm = Asm; }
187 /// Append to the module-scope inline assembly blocks, automatically
188 /// appending a newline to the end.
189 void appendModuleInlineAsm(const StringRef &Asm) {
190 GlobalScopeAsm += Asm;
191 GlobalScopeAsm += '\n';
195 /// @name Generic Value Accessors
198 /// getNamedValue - Return the first global value in the module with
199 /// the specified name, of arbitrary type. This method returns null
200 /// if a global with the specified name is not found.
201 GlobalValue *getNamedValue(const StringRef &Name) const;
204 /// @name Function Accessors
207 /// getOrInsertFunction - Look up the specified function in the module symbol
208 /// table. Four possibilities:
209 /// 1. If it does not exist, add a prototype for the function and return it.
210 /// 2. If it exists, and has a local linkage, the existing function is
211 /// renamed and a new one is inserted.
212 /// 3. Otherwise, if the existing function has the correct prototype, return
213 /// the existing function.
214 /// 4. Finally, the function exists but has the wrong prototype: return the
215 /// function with a constantexpr cast to the right prototype.
216 Constant *getOrInsertFunction(const StringRef &Name, const FunctionType *T,
217 AttrListPtr AttributeList);
219 Constant *getOrInsertFunction(const StringRef &Name, const FunctionType *T);
221 /// getOrInsertFunction - Look up the specified function in the module symbol
222 /// table. If it does not exist, add a prototype for the function and return
223 /// it. This function guarantees to return a constant of pointer to the
224 /// specified function type or a ConstantExpr BitCast of that type if the
225 /// named function has a different type. This version of the method takes a
226 /// null terminated list of function arguments, which makes it easier for
228 Constant *getOrInsertFunction(const StringRef &Name,
229 AttrListPtr AttributeList,
230 const Type *RetTy, ...) END_WITH_NULL;
232 Constant *getOrInsertFunction(const StringRef &Name, const Type *RetTy, ...)
235 Constant *getOrInsertTargetIntrinsic(const StringRef &Name,
236 const FunctionType *Ty,
237 AttrListPtr AttributeList);
239 /// getFunction - Look up the specified function in the module symbol table.
240 /// If it does not exist, return null.
241 Function *getFunction(const StringRef &Name) const;
244 /// @name Global Variable Accessors
247 /// getGlobalVariable - Look up the specified global variable in the module
248 /// symbol table. If it does not exist, return null. If AllowInternal is set
249 /// to true, this function will return types that have InternalLinkage. By
250 /// default, these types are not returned.
251 GlobalVariable *getGlobalVariable(const StringRef &Name,
252 bool AllowInternal = false) const;
254 /// getNamedGlobal - Return the first global variable in the module with the
255 /// specified name, of arbitrary type. This method returns null if a global
256 /// with the specified name is not found.
257 GlobalVariable *getNamedGlobal(const StringRef &Name) const {
258 return getGlobalVariable(Name, true);
261 /// getOrInsertGlobal - Look up the specified global in the module symbol
263 /// 1. If it does not exist, add a declaration of the global and return it.
264 /// 2. Else, the global exists but has the wrong type: return the function
265 /// with a constantexpr cast to the right type.
266 /// 3. Finally, if the existing global is the correct delclaration, return
267 /// the existing global.
268 Constant *getOrInsertGlobal(const StringRef &Name, const Type *Ty);
271 /// @name Global Alias Accessors
274 /// getNamedAlias - Return the first global alias in the module with the
275 /// specified name, of arbitrary type. This method returns null if a global
276 /// with the specified name is not found.
277 GlobalAlias *getNamedAlias(const StringRef &Name) const;
280 /// @name Type Accessors
283 /// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
284 /// there is already an entry for this name, true is returned and the symbol
285 /// table is not modified.
286 bool addTypeName(const StringRef &Name, const Type *Ty);
288 /// getTypeName - If there is at least one entry in the symbol table for the
289 /// specified type, return it.
290 std::string getTypeName(const Type *Ty) const;
292 /// getTypeByName - Return the type with the specified name in this module, or
293 /// null if there is none by that name.
294 const Type *getTypeByName(const StringRef &Name) const;
297 /// @name Direct access to the globals list, functions list, and symbol table
300 /// Get the Module's list of global variables (constant).
301 const GlobalListType &getGlobalList() const { return GlobalList; }
302 /// Get the Module's list of global variables.
303 GlobalListType &getGlobalList() { return GlobalList; }
304 static iplist<GlobalVariable> Module::*getSublistAccess(GlobalVariable*) {
305 return &Module::GlobalList;
307 /// Get the Module's list of functions (constant).
308 const FunctionListType &getFunctionList() const { return FunctionList; }
309 /// Get the Module's list of functions.
310 FunctionListType &getFunctionList() { return FunctionList; }
311 static iplist<Function> Module::*getSublistAccess(Function*) {
312 return &Module::FunctionList;
314 /// Get the Module's list of aliases (constant).
315 const AliasListType &getAliasList() const { return AliasList; }
316 /// Get the Module's list of aliases.
317 AliasListType &getAliasList() { return AliasList; }
318 static iplist<GlobalAlias> Module::*getSublistAccess(GlobalAlias*) {
319 return &Module::AliasList;
321 /// Get the symbol table of global variable and function identifiers
322 const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; }
323 /// Get the Module's symbol table of global variable and function identifiers.
324 ValueSymbolTable &getValueSymbolTable() { return *ValSymTab; }
325 /// Get the symbol table of types
326 const TypeSymbolTable &getTypeSymbolTable() const { return *TypeSymTab; }
327 /// Get the Module's symbol table of types
328 TypeSymbolTable &getTypeSymbolTable() { return *TypeSymTab; }
331 /// @name Global Variable Iteration
334 /// Get an iterator to the first global variable
335 global_iterator global_begin() { return GlobalList.begin(); }
336 /// Get a constant iterator to the first global variable
337 const_global_iterator global_begin() const { return GlobalList.begin(); }
338 /// Get an iterator to the last global variable
339 global_iterator global_end () { return GlobalList.end(); }
340 /// Get a constant iterator to the last global variable
341 const_global_iterator global_end () const { return GlobalList.end(); }
342 /// Determine if the list of globals is empty.
343 bool global_empty() const { return GlobalList.empty(); }
346 /// @name Function Iteration
349 /// Get an iterator to the first function.
350 iterator begin() { return FunctionList.begin(); }
351 /// Get a constant iterator to the first function.
352 const_iterator begin() const { return FunctionList.begin(); }
353 /// Get an iterator to the last function.
354 iterator end () { return FunctionList.end(); }
355 /// Get a constant iterator to the last function.
356 const_iterator end () const { return FunctionList.end(); }
357 /// Determine how many functions are in the Module's list of functions.
358 size_t size() const { return FunctionList.size(); }
359 /// Determine if the list of functions is empty.
360 bool empty() const { return FunctionList.empty(); }
363 /// @name Dependent Library Iteration
366 /// @brief Get a constant iterator to beginning of dependent library list.
367 inline lib_iterator lib_begin() const { return LibraryList.begin(); }
368 /// @brief Get a constant iterator to end of dependent library list.
369 inline lib_iterator lib_end() const { return LibraryList.end(); }
370 /// @brief Returns the number of items in the list of libraries.
371 inline size_t lib_size() const { return LibraryList.size(); }
372 /// @brief Add a library to the list of dependent libraries
373 void addLibrary(const StringRef &Lib);
374 /// @brief Remove a library from the list of dependent libraries
375 void removeLibrary(const StringRef &Lib);
376 /// @brief Get all the libraries
377 inline const LibraryListType& getLibraries() const { return LibraryList; }
380 /// @name Alias Iteration
383 /// Get an iterator to the first alias.
384 alias_iterator alias_begin() { return AliasList.begin(); }
385 /// Get a constant iterator to the first alias.
386 const_alias_iterator alias_begin() const { return AliasList.begin(); }
387 /// Get an iterator to the last alias.
388 alias_iterator alias_end () { return AliasList.end(); }
389 /// Get a constant iterator to the last alias.
390 const_alias_iterator alias_end () const { return AliasList.end(); }
391 /// Determine how many aliases are in the Module's list of aliases.
392 size_t alias_size () const { return AliasList.size(); }
393 /// Determine if the list of aliases is empty.
394 bool alias_empty() const { return AliasList.empty(); }
397 /// @name Utility functions for printing and dumping Module objects
400 /// Print the module to an output stream with AssemblyAnnotationWriter.
401 void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const;
402 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
404 /// Dump the module to stderr (for debugging).
406 /// This function causes all the subinstructions to "let go" of all references
407 /// that they are maintaining. This allows one to 'delete' a whole class at
408 /// a time, even though there may be circular references... first all
409 /// references are dropped, and all use counts go to zero. Then everything
410 /// is delete'd for real. Note that no operations are valid on an object
411 /// that has "dropped all references", except operator delete.
412 void dropAllReferences();
416 /// An iostream inserter for modules.
417 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
421 inline raw_ostream &operator<<(raw_ostream &O, const Module &M) {
426 } // End llvm namespace