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> {
31 // createSentinel is used to create a node that marks the end of the list.
32 static Function *createSentinel();
33 static void destroySentinel(Function *F) { delete F; }
34 static inline ValueSymbolTable *getSymTab(Module *M);
36 template<> struct ilist_traits<GlobalVariable>
37 : public SymbolTableListTraits<GlobalVariable, Module> {
38 // createSentinel is used to create a node that marks the end of the list.
39 static GlobalVariable *createSentinel();
40 static void destroySentinel(GlobalVariable *GV) { delete GV; }
41 static inline ValueSymbolTable *getSymTab(Module *M);
43 template<> struct ilist_traits<GlobalAlias>
44 : public SymbolTableListTraits<GlobalAlias, Module> {
45 // createSentinel is used to create a node that marks the end of the list.
46 static GlobalAlias *createSentinel();
47 static void destroySentinel(GlobalAlias *GA) { delete GA; }
48 static inline ValueSymbolTable *getSymTab(Module *M);
51 /// A Module instance is used to store all the information related to an
52 /// LLVM module. Modules are the top level container of all other LLVM
53 /// Intermediate Representation (IR) objects. Each module directly contains a
54 /// list of globals variables, a list of functions, a list of libraries (or
55 /// other modules) this module depends on, a symbol table, and various data
56 /// about the target's characteristics.
58 /// A module maintains a GlobalValRefMap object that is used to hold all
59 /// constant references to global variables in the module. When a global
60 /// variable is destroyed, it should have no entries in the GlobalValueRefMap.
61 /// @brief The main container class for the LLVM Intermediate Representation.
63 /// @name Types And Enumerations
66 /// The type for the list of global variables.
67 typedef iplist<GlobalVariable> GlobalListType;
68 /// The type for the list of functions.
69 typedef iplist<Function> FunctionListType;
70 /// The type for the list of aliases.
71 typedef iplist<GlobalAlias> AliasListType;
73 /// The type for the list of dependent libraries.
74 typedef std::vector<std::string> LibraryListType;
76 /// The Global Variable iterator.
77 typedef GlobalListType::iterator global_iterator;
78 /// The Global Variable constant iterator.
79 typedef GlobalListType::const_iterator const_global_iterator;
81 /// The Function iterators.
82 typedef FunctionListType::iterator iterator;
83 /// The Function constant iterator
84 typedef FunctionListType::const_iterator const_iterator;
86 /// The Global Alias iterators.
87 typedef AliasListType::iterator alias_iterator;
88 /// The Global Alias constant iterator
89 typedef AliasListType::const_iterator const_alias_iterator;
91 /// The Library list iterator.
92 typedef LibraryListType::const_iterator lib_iterator;
94 /// An enumeration for describing the endianess of the target machine.
95 enum Endianness { AnyEndianness, LittleEndian, BigEndian };
97 /// An enumeration for describing the size of a pointer on the target machine.
98 enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
101 /// @name Member Variables
104 GlobalListType GlobalList; ///< The Global Variables in the module
105 FunctionListType FunctionList; ///< The Functions in the module
106 AliasListType AliasList; ///< The Aliases in the module
107 LibraryListType LibraryList; ///< The Libraries needed by the module
108 std::string GlobalScopeAsm; ///< Inline Asm at global scope.
109 ValueSymbolTable *ValSymTab; ///< Symbol table for values
110 TypeSymbolTable *TypeSymTab; ///< Symbol table for types
111 std::string ModuleID; ///< Human readable identifier for the module
112 std::string TargetTriple; ///< Platform target triple Module compiled on
113 std::string DataLayout; ///< Target data description
115 friend class Constant;
118 /// @name Constructors
121 /// The Module constructor. Note that there is no default constructor. You
122 /// must provide a name for the module upon construction.
123 explicit Module(const std::string &ModuleID);
124 /// The module destructor. This will dropAllReferences.
128 /// @name Module Level Accessors
131 /// Get the module identifier which is, essentially, the name of the module.
132 /// @returns the module identifier as a string
133 const std::string &getModuleIdentifier() const { return ModuleID; }
135 /// Get the data layout string for the module's target platform. This encodes
136 /// the type sizes and alignments expected by this module.
137 /// @returns the data layout as a string
138 const std::string& getDataLayout() const { return DataLayout; }
140 /// Get the target triple which is a string describing the target host.
141 /// @returns a string containing the target triple.
142 const std::string &getTargetTriple() const { return TargetTriple; }
144 /// Get the target endian information.
145 /// @returns Endianess - an enumeration for the endianess of the target
146 Endianness getEndianness() const;
148 /// Get the target pointer size.
149 /// @returns PointerSize - an enumeration for the size of the target's pointer
150 PointerSize getPointerSize() const;
152 /// Get any module-scope inline assembly blocks.
153 /// @returns a string containing the module-scope inline assembly blocks.
154 const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
156 /// @name Module Level Mutators
160 /// Set the module identifier.
161 void setModuleIdentifier(const std::string &ID) { ModuleID = ID; }
163 /// Set the data layout
164 void setDataLayout(const std::string& DL) { DataLayout = DL; }
166 /// Set the target triple.
167 void setTargetTriple(const std::string &T) { TargetTriple = T; }
169 /// Set the module-scope inline assembly blocks.
170 void setModuleInlineAsm(const std::string &Asm) { GlobalScopeAsm = Asm; }
172 /// Append to the module-scope inline assembly blocks, automatically
173 /// appending a newline to the end.
174 void appendModuleInlineAsm(const std::string &Asm) {
175 GlobalScopeAsm += Asm;
176 GlobalScopeAsm += '\n';
180 /// @name Generic Value Accessors
183 /// getNamedValue - Return the first global value in the module with
184 /// the specified name, of arbitrary type. This method returns null
185 /// if a global with the specified name is not found.
186 GlobalValue *getNamedValue(const std::string &Name) const;
187 GlobalValue *getNamedValue(const char *Name) const;
190 /// @name Function Accessors
193 /// getOrInsertFunction - Look up the specified function in the module symbol
194 /// table. Four possibilities:
195 /// 1. If it does not exist, add a prototype for the function and return it.
196 /// 2. If it exists, and has a local linkage, the existing function is
197 /// renamed and a new one is inserted.
198 /// 3. Otherwise, if the existing function has the correct prototype, return
199 /// the existing function.
200 /// 4. Finally, the function exists but has the wrong prototype: return the
201 /// function with a constantexpr cast to the right prototype.
202 Constant *getOrInsertFunction(const std::string &Name, const FunctionType *T,
203 AttrListPtr AttributeList);
205 Constant *getOrInsertFunction(const std::string &Name, const FunctionType *T);
207 /// getOrInsertFunction - Look up the specified function in the module symbol
208 /// table. If it does not exist, add a prototype for the function and return
209 /// it. This function guarantees to return a constant of pointer to the
210 /// specified function type or a ConstantExpr BitCast of that type if the
211 /// named function has a different type. This version of the method takes a
212 /// null terminated list of function arguments, which makes it easier for
214 Constant *getOrInsertFunction(const std::string &Name,
215 AttrListPtr AttributeList,
216 const Type *RetTy, ...) END_WITH_NULL;
218 Constant *getOrInsertFunction(const std::string &Name, const Type *RetTy, ...)
221 Constant *getOrInsertTargetIntrinsic(const std::string &Name,
222 const FunctionType *Ty,
223 AttrListPtr AttributeList);
225 /// getFunction - Look up the specified function in the module symbol table.
226 /// If it does not exist, return null.
227 Function *getFunction(const std::string &Name) const;
228 Function *getFunction(const char *Name) const;
231 /// @name Global Variable Accessors
234 /// getGlobalVariable - Look up the specified global variable in the module
235 /// symbol table. If it does not exist, return null. If AllowInternal is set
236 /// to true, this function will return types that have InternalLinkage. By
237 /// default, these types are not returned.
238 GlobalVariable *getGlobalVariable(const std::string &Name,
239 bool AllowInternal = false) const;
241 /// getNamedGlobal - Return the first global variable in the module with the
242 /// specified name, of arbitrary type. This method returns null if a global
243 /// with the specified name is not found.
244 GlobalVariable *getNamedGlobal(const std::string &Name) const {
245 return getGlobalVariable(Name, true);
248 /// getOrInsertGlobal - Look up the specified global in the module symbol
250 /// 1. If it does not exist, add a declaration of the global and return it.
251 /// 2. Else, the global exists but has the wrong type: return the function
252 /// with a constantexpr cast to the right type.
253 /// 3. Finally, if the existing global is the correct delclaration, return
254 /// the existing global.
255 Constant *getOrInsertGlobal(const std::string &Name, const Type *Ty);
258 /// @name Global Alias Accessors
261 /// getNamedAlias - Return the first global alias in the module with the
262 /// specified name, of arbitrary type. This method returns null if a global
263 /// with the specified name is not found.
264 GlobalAlias *getNamedAlias(const std::string &Name) const;
267 /// @name Type Accessors
270 /// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
271 /// there is already an entry for this name, true is returned and the symbol
272 /// table is not modified.
273 bool addTypeName(const std::string &Name, const Type *Ty);
275 /// getTypeName - If there is at least one entry in the symbol table for the
276 /// specified type, return it.
277 std::string getTypeName(const Type *Ty) const;
279 /// getTypeByName - Return the type with the specified name in this module, or
280 /// null if there is none by that name.
281 const Type *getTypeByName(const std::string &Name) const;
284 /// @name Direct access to the globals list, functions list, and symbol table
287 /// Get the Module's list of global variables (constant).
288 const GlobalListType &getGlobalList() const { return GlobalList; }
289 /// Get the Module's list of global variables.
290 GlobalListType &getGlobalList() { return GlobalList; }
291 static iplist<GlobalVariable> Module::*getSublistAccess(GlobalVariable*) {
292 return &Module::GlobalList;
294 /// Get the Module's list of functions (constant).
295 const FunctionListType &getFunctionList() const { return FunctionList; }
296 /// Get the Module's list of functions.
297 FunctionListType &getFunctionList() { return FunctionList; }
298 static iplist<Function> Module::*getSublistAccess(Function*) {
299 return &Module::FunctionList;
301 /// Get the Module's list of aliases (constant).
302 const AliasListType &getAliasList() const { return AliasList; }
303 /// Get the Module's list of aliases.
304 AliasListType &getAliasList() { return AliasList; }
305 static iplist<GlobalAlias> Module::*getSublistAccess(GlobalAlias*) {
306 return &Module::AliasList;
308 /// Get the symbol table of global variable and function identifiers
309 const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; }
310 /// Get the Module's symbol table of global variable and function identifiers.
311 ValueSymbolTable &getValueSymbolTable() { return *ValSymTab; }
312 /// Get the symbol table of types
313 const TypeSymbolTable &getTypeSymbolTable() const { return *TypeSymTab; }
314 /// Get the Module's symbol table of types
315 TypeSymbolTable &getTypeSymbolTable() { return *TypeSymTab; }
318 /// @name Global Variable Iteration
321 /// Get an iterator to the first global variable
322 global_iterator global_begin() { return GlobalList.begin(); }
323 /// Get a constant iterator to the first global variable
324 const_global_iterator global_begin() const { return GlobalList.begin(); }
325 /// Get an iterator to the last global variable
326 global_iterator global_end () { return GlobalList.end(); }
327 /// Get a constant iterator to the last global variable
328 const_global_iterator global_end () const { return GlobalList.end(); }
329 /// Determine if the list of globals is empty.
330 bool global_empty() const { return GlobalList.empty(); }
333 /// @name Function Iteration
336 /// Get an iterator to the first function.
337 iterator begin() { return FunctionList.begin(); }
338 /// Get a constant iterator to the first function.
339 const_iterator begin() const { return FunctionList.begin(); }
340 /// Get an iterator to the last function.
341 iterator end () { return FunctionList.end(); }
342 /// Get a constant iterator to the last function.
343 const_iterator end () const { return FunctionList.end(); }
344 /// Determine how many functions are in the Module's list of functions.
345 size_t size() const { return FunctionList.size(); }
346 /// Determine if the list of functions is empty.
347 bool empty() const { return FunctionList.empty(); }
350 /// @name Dependent Library Iteration
353 /// @brief Get a constant iterator to beginning of dependent library list.
354 inline lib_iterator lib_begin() const { return LibraryList.begin(); }
355 /// @brief Get a constant iterator to end of dependent library list.
356 inline lib_iterator lib_end() const { return LibraryList.end(); }
357 /// @brief Returns the number of items in the list of libraries.
358 inline size_t lib_size() const { return LibraryList.size(); }
359 /// @brief Add a library to the list of dependent libraries
360 void addLibrary(const std::string& Lib);
361 /// @brief Remove a library from the list of dependent libraries
362 void removeLibrary(const std::string& Lib);
363 /// @brief Get all the libraries
364 inline const LibraryListType& getLibraries() const { return LibraryList; }
367 /// @name Alias Iteration
370 /// Get an iterator to the first alias.
371 alias_iterator alias_begin() { return AliasList.begin(); }
372 /// Get a constant iterator to the first alias.
373 const_alias_iterator alias_begin() const { return AliasList.begin(); }
374 /// Get an iterator to the last alias.
375 alias_iterator alias_end () { return AliasList.end(); }
376 /// Get a constant iterator to the last alias.
377 const_alias_iterator alias_end () const { return AliasList.end(); }
378 /// Determine how many functions are in the Module's list of aliases.
379 size_t alias_size () const { return AliasList.size(); }
380 /// Determine if the list of aliases is empty.
381 bool alias_empty() const { return AliasList.empty(); }
384 /// @name Utility functions for printing and dumping Module objects
387 /// Print the module to an output stream with AssemblyAnnotationWriter.
388 void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const;
389 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
391 /// Dump the module to stderr (for debugging).
393 /// This function causes all the subinstructions to "let go" of all references
394 /// that they are maintaining. This allows one to 'delete' a whole class at
395 /// a time, even though there may be circular references... first all
396 /// references are dropped, and all use counts go to zero. Then everything
397 /// is delete'd for real. Note that no operations are valid on an object
398 /// that has "dropped all references", except operator delete.
399 void dropAllReferences();
403 /// An iostream inserter for modules.
404 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
408 inline raw_ostream &operator<<(raw_ostream &O, const Module &M) {
414 inline ValueSymbolTable *
415 ilist_traits<Function>::getSymTab(Module *M) {
416 return M ? &M->getValueSymbolTable() : 0;
419 inline ValueSymbolTable *
420 ilist_traits<GlobalVariable>::getSymTab(Module *M) {
421 return M ? &M->getValueSymbolTable() : 0;
424 inline ValueSymbolTable *
425 ilist_traits<GlobalAlias>::getSymTab(Module *M) {
426 return M ? &M->getValueSymbolTable() : 0;
429 } // End llvm namespace