1 //===-- llvm/Module.h - C++ class to represent a VM module ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// @file This file contains the declarations for the Module class.
12 //===----------------------------------------------------------------------===//
17 #include "llvm/Function.h"
18 #include "llvm/GlobalVariable.h"
19 #include "llvm/Support/DataTypes.h"
24 class GlobalValueRefMap; // Used by ConstantVals.cpp
27 template<> struct ilist_traits<Function>
28 : public SymbolTableListTraits<Function, Module, Module> {
29 // createSentinel is used to create a node that marks the end of the list.
30 static Function *createSentinel();
31 static void destroySentinel(Function *F) { delete F; }
32 static iplist<Function> &getList(Module *M);
34 template<> struct ilist_traits<GlobalVariable>
35 : public SymbolTableListTraits<GlobalVariable, Module, Module> {
36 // createSentinel is used to create a node that marks the end of the list.
37 static GlobalVariable *createSentinel();
38 static void destroySentinel(GlobalVariable *GV) { delete GV; }
39 static iplist<GlobalVariable> &getList(Module *M);
42 /// A Module instance is used to store all the information related to an
43 /// LLVM module. Modules are the top level container of all other LLVM
44 /// Intermediate Representation (IR) objects. Each module directly contains a
45 /// list of globals variables, a list of functions, a list of libraries (or
46 /// other modules) this module depends on, a symbol table, and various data
47 /// about the target's characteristics.
49 /// A module maintains a GlobalValRefMap object that is used to hold all
50 /// constant references to global variables in the module. When a global
51 /// variable is destroyed, it should have no entries in the GlobalValueRefMap.
52 /// @brief The main container class for the LLVM Intermediate Representation.
54 /// @name Types And Enumerations
57 /// The type for the list of global variables.
58 typedef iplist<GlobalVariable> GlobalListType;
59 /// The type for the list of functions.
60 typedef iplist<Function> FunctionListType;
62 /// The type for the list of dependent libraries.
63 typedef std::vector<std::string> LibraryListType;
65 /// The Global Variable iterator.
66 typedef GlobalListType::iterator global_iterator;
67 /// The Global Variable constant iterator.
68 typedef GlobalListType::const_iterator const_global_iterator;
70 /// The Function iterators.
71 typedef FunctionListType::iterator iterator;
72 /// The Function constant iterator
73 typedef FunctionListType::const_iterator const_iterator;
75 /// The Library list iterator.
76 typedef LibraryListType::const_iterator lib_iterator;
78 /// An enumeration for describing the endianess of the target machine.
79 enum Endianness { AnyEndianness, LittleEndian, BigEndian };
81 /// An enumeration for describing the size of a pointer on the target machine.
82 enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
85 /// @name Member Variables
88 GlobalListType GlobalList; ///< The Global Variables in the module
89 FunctionListType FunctionList; ///< The Functions in the module
90 LibraryListType LibraryList; ///< The Libraries needed by the module
91 std::string GlobalScopeAsm; ///< Inline Asm at global scope.
92 ValueSymbolTable *ValSymTab; ///< Symbol table for values
93 TypeSymbolTable *TypeSymTab; ///< Symbol table for types
94 std::string ModuleID; ///< Human readable identifier for the module
95 std::string TargetTriple; ///< Platform target triple Module compiled on
96 std::string DataLayout; ///< Target data description
98 friend class Constant;
101 /// @name Constructors
104 /// The Module constructor. Note that there is no default constructor. You
105 /// must provide a name for the module upon construction.
106 Module(const std::string &ModuleID);
107 /// The module destructor. This will dropAllReferences.
111 /// @name Module Level Accessors
114 /// Get the module identifier which is, essentially, the name of the module.
115 /// @returns the module identifier as a string
116 const std::string &getModuleIdentifier() const { return ModuleID; }
118 /// Get the data layout string for the module's target platform. This encodes
119 /// the type sizes and alignments expected by this module.
120 /// @returns the data layout as a string
121 const std::string& getDataLayout() const { return DataLayout; }
123 /// Get the target triple which is a string describing the target host.
124 /// @returns a string containing the target triple.
125 const std::string &getTargetTriple() const { return TargetTriple; }
127 /// Get the target endian information.
128 /// @returns Endianess - an enumeration for the endianess of the target
129 Endianness getEndianness() const;
131 /// Get the target pointer size.
132 /// @returns PointerSize - an enumeration for the size of the target's pointer
133 PointerSize getPointerSize() const;
135 /// Get any module-scope inline assembly blocks.
136 /// @returns a string containing the module-scope inline assembly blocks.
137 const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
139 /// @name Module Level Mutators
143 /// Set the module identifier.
144 void setModuleIdentifier(const std::string &ID) { ModuleID = ID; }
146 /// Set the data layout
147 void setDataLayout(const std::string& DL) { DataLayout = DL; }
149 /// Set the target triple.
150 void setTargetTriple(const std::string &T) { TargetTriple = T; }
152 /// Set the module-scope inline assembly blocks.
153 void setModuleInlineAsm(const std::string &Asm) { GlobalScopeAsm = Asm; }
156 /// @name Function Accessors
159 /// getOrInsertFunction - Look up the specified function in the module symbol
160 /// table. Four possibilities:
161 /// 1. If it does not exist, add a prototype for the function and return it.
162 /// 2. If it exists, and has internal linkage, the existing function is
163 /// renamed and a new one is inserted.
164 /// 3. Otherwise, if the existing function has the correct prototype, return
165 /// the existing function.
166 /// 4. Finally, the function exists but has the wrong prototype: return the
167 /// function with a constantexpr cast to the right prototype.
168 Constant *getOrInsertFunction(const std::string &Name, const FunctionType *T);
170 /// getOrInsertFunction - Look up the specified function in the module symbol
171 /// table. If it does not exist, add a prototype for the function and return
172 /// it. This function guarantees to return a constant of pointer to the
173 /// specified function type or a ConstantExpr BitCast of that type if the
174 /// named /// function has a different type. This version of the method
175 /// takes a null terminated list of function arguments, which makes it
176 /// easier for clients to use.
177 Constant *getOrInsertFunction(const std::string &Name, const Type *RetTy,...)
180 /// getFunction - Look up the specified function in the module symbol table.
181 /// If it does not exist, return null.
182 Function *getFunction(const std::string &Name) const;
185 /// @name Global Variable Accessors
188 /// getGlobalVariable - Look up the specified global variable in the module
189 /// symbol table. If it does not exist, return null. The type argument
190 /// should be the underlying type of the global, i.e., it should not have
191 /// the top-level PointerType, which represents the address of the global.
192 /// If AllowInternal is set to true, this function will return types that
193 /// have InternalLinkage. By default, these types are not returned.
194 GlobalVariable *getGlobalVariable(const std::string &Name,
195 bool AllowInternal = false) const;
197 /// getNamedGlobal - Return the first global variable in the module with the
198 /// specified name, of arbitrary type. This method returns null if a global
199 /// with the specified name is not found.
200 GlobalVariable *getNamedGlobal(const std::string &Name) const {
201 return getGlobalVariable(Name, true);
205 /// @name Type Accessors
208 /// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
209 /// there is already an entry for this name, true is returned and the symbol
210 /// table is not modified.
211 bool addTypeName(const std::string &Name, const Type *Ty);
213 /// getTypeName - If there is at least one entry in the symbol table for the
214 /// specified type, return it.
215 std::string getTypeName(const Type *Ty) const;
217 /// getTypeByName - Return the type with the specified name in this module, or
218 /// null if there is none by that name.
219 const Type *getTypeByName(const std::string &Name) const;
222 /// @name Direct access to the globals list, functions list, and symbol table
225 /// Get the Module's list of global variables (constant).
226 const GlobalListType &getGlobalList() const { return GlobalList; }
227 /// Get the Module's list of global variables.
228 GlobalListType &getGlobalList() { return GlobalList; }
229 /// Get the Module's list of functions (constant).
230 const FunctionListType &getFunctionList() const { return FunctionList; }
231 /// Get the Module's list of functions.
232 FunctionListType &getFunctionList() { return FunctionList; }
233 /// Get the symbol table of global variable and function identifiers
234 const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; }
235 /// Get the Module's symbol table of global variable and function identifiers.
236 ValueSymbolTable &getValueSymbolTable() { return *ValSymTab; }
237 /// Get the symbol table of types
238 const TypeSymbolTable &getTypeSymbolTable() const { return *TypeSymTab; }
239 /// Get the Module's symbol table of types
240 TypeSymbolTable &getTypeSymbolTable() { return *TypeSymTab; }
243 /// @name Global Variable Iteration
246 /// Get an iterator to the first global variable
247 global_iterator global_begin() { return GlobalList.begin(); }
248 /// Get a constant iterator to the first global variable
249 const_global_iterator global_begin() const { return GlobalList.begin(); }
250 /// Get an iterator to the last global variable
251 global_iterator global_end () { return GlobalList.end(); }
252 /// Get a constant iterator to the last global variable
253 const_global_iterator global_end () const { return GlobalList.end(); }
254 /// Determine if the list of globals is empty.
255 bool global_empty() const { return GlobalList.empty(); }
258 /// @name Function Iteration
261 /// Get an iterator to the first function.
262 iterator begin() { return FunctionList.begin(); }
263 /// Get a constant iterator to the first function.
264 const_iterator begin() const { return FunctionList.begin(); }
265 /// Get an iterator to the last function.
266 iterator end () { return FunctionList.end(); }
267 /// Get a constant iterator to the last function.
268 const_iterator end () const { return FunctionList.end(); }
269 /// Determine how many functions are in the Module's list of functions.
270 size_t size() const { return FunctionList.size(); }
271 /// Determine if the list of functions is empty.
272 bool empty() const { return FunctionList.empty(); }
275 /// @name Dependent Library Iteration
278 /// @brief Get a constant iterator to beginning of dependent library list.
279 inline lib_iterator lib_begin() const { return LibraryList.begin(); }
280 /// @brief Get a constant iterator to end of dependent library list.
281 inline lib_iterator lib_end() const { return LibraryList.end(); }
282 /// @brief Returns the number of items in the list of libraries.
283 inline size_t lib_size() const { return LibraryList.size(); }
284 /// @brief Add a library to the list of dependent libraries
285 void addLibrary(const std::string& Lib);
286 /// @brief Remove a library from the list of dependent libraries
287 void removeLibrary(const std::string& Lib);
288 /// @brief Get all the libraries
289 inline const LibraryListType& getLibraries() const { return LibraryList; }
292 /// @name Utility functions for printing and dumping Module objects
295 /// Print the module to an output stream
296 void print(std::ostream &OS) const { print(OS, 0); }
297 void print(std::ostream *OS) const { if (OS) print(*OS); }
298 /// Print the module to an output stream with AssemblyAnnotationWriter.
299 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
300 void print(std::ostream *OS, AssemblyAnnotationWriter *AAW) const {
301 if (OS) print(*OS, AAW);
303 /// Dump the module to std::cerr (for debugging).
305 /// This function causes all the subinstructions to "let go" of all references
306 /// that they are maintaining. This allows one to 'delete' a whole class at
307 /// a time, even though there may be circular references... first all
308 /// references are dropped, and all use counts go to zero. Then everything
309 /// is delete'd for real. Note that no operations are valid on an object
310 /// that has "dropped all references", except operator delete.
311 void dropAllReferences();
315 /// An iostream inserter for modules.
316 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
321 } // End llvm namespace