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