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 std::string &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 std::string &ID) { ModuleID = ID; }
178 /// Set the data layout
179 void setDataLayout(const std::string& DL) { DataLayout = DL; }
181 /// Set the target triple.
182 void setTargetTriple(const std::string &T) { TargetTriple = T; }
184 /// Set the module-scope inline assembly blocks.
185 void setModuleInlineAsm(const std::string &Asm) { GlobalScopeAsm = Asm; }
187 /// Append to the module-scope inline assembly blocks, automatically
188 /// appending a newline to the end.
189 void appendModuleInlineAsm(const std::string &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 std::string &Name) const;
202 GlobalValue *getNamedValue(const char *Name) const;
205 /// @name Function Accessors
208 /// getOrInsertFunction - Look up the specified function in the module symbol
209 /// table. Four possibilities:
210 /// 1. If it does not exist, add a prototype for the function and return it.
211 /// 2. If it exists, and has a local linkage, the existing function is
212 /// renamed and a new one is inserted.
213 /// 3. Otherwise, if the existing function has the correct prototype, return
214 /// the existing function.
215 /// 4. Finally, the function exists but has the wrong prototype: return the
216 /// function with a constantexpr cast to the right prototype.
217 Constant *getOrInsertFunction(const std::string &Name, const FunctionType *T,
218 AttrListPtr AttributeList);
220 Constant *getOrInsertFunction(const std::string &Name, const FunctionType *T);
222 /// getOrInsertFunction - Look up the specified function in the module symbol
223 /// table. If it does not exist, add a prototype for the function and return
224 /// it. This function guarantees to return a constant of pointer to the
225 /// specified function type or a ConstantExpr BitCast of that type if the
226 /// named function has a different type. This version of the method takes a
227 /// null terminated list of function arguments, which makes it easier for
229 Constant *getOrInsertFunction(const std::string &Name,
230 AttrListPtr AttributeList,
231 const Type *RetTy, ...) END_WITH_NULL;
233 Constant *getOrInsertFunction(const std::string &Name, const Type *RetTy, ...)
236 Constant *getOrInsertTargetIntrinsic(const std::string &Name,
237 const FunctionType *Ty,
238 AttrListPtr AttributeList);
240 /// getFunction - Look up the specified function in the module symbol table.
241 /// If it does not exist, return null.
242 Function *getFunction(const std::string &Name) const;
243 Function *getFunction(const char *Name) const;
246 /// @name Global Variable Accessors
249 /// getGlobalVariable - Look up the specified global variable in the module
250 /// symbol table. If it does not exist, return null. If AllowInternal is set
251 /// to true, this function will return types that have InternalLinkage. By
252 /// default, these types are not returned.
253 GlobalVariable *getGlobalVariable(const std::string &Name,
254 bool AllowInternal = false) const;
256 /// getNamedGlobal - Return the first global variable in the module with the
257 /// specified name, of arbitrary type. This method returns null if a global
258 /// with the specified name is not found.
259 GlobalVariable *getNamedGlobal(const std::string &Name) const {
260 return getGlobalVariable(Name, true);
263 /// getOrInsertGlobal - Look up the specified global in the module symbol
265 /// 1. If it does not exist, add a declaration of the global and return it.
266 /// 2. Else, the global exists but has the wrong type: return the function
267 /// with a constantexpr cast to the right type.
268 /// 3. Finally, if the existing global is the correct delclaration, return
269 /// the existing global.
270 Constant *getOrInsertGlobal(const std::string &Name, const Type *Ty);
273 /// @name Global Alias Accessors
276 /// getNamedAlias - Return the first global alias in the module with the
277 /// specified name, of arbitrary type. This method returns null if a global
278 /// with the specified name is not found.
279 GlobalAlias *getNamedAlias(const std::string &Name) const;
282 /// @name Type Accessors
285 /// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
286 /// there is already an entry for this name, true is returned and the symbol
287 /// table is not modified.
288 bool addTypeName(const std::string &Name, const Type *Ty);
290 /// getTypeName - If there is at least one entry in the symbol table for the
291 /// specified type, return it.
292 std::string getTypeName(const Type *Ty) const;
294 /// getTypeByName - Return the type with the specified name in this module, or
295 /// null if there is none by that name.
296 const Type *getTypeByName(const std::string &Name) const;
299 /// @name Direct access to the globals list, functions list, and symbol table
302 /// Get the Module's list of global variables (constant).
303 const GlobalListType &getGlobalList() const { return GlobalList; }
304 /// Get the Module's list of global variables.
305 GlobalListType &getGlobalList() { return GlobalList; }
306 static iplist<GlobalVariable> Module::*getSublistAccess(GlobalVariable*) {
307 return &Module::GlobalList;
309 /// Get the Module's list of functions (constant).
310 const FunctionListType &getFunctionList() const { return FunctionList; }
311 /// Get the Module's list of functions.
312 FunctionListType &getFunctionList() { return FunctionList; }
313 static iplist<Function> Module::*getSublistAccess(Function*) {
314 return &Module::FunctionList;
316 /// Get the Module's list of aliases (constant).
317 const AliasListType &getAliasList() const { return AliasList; }
318 /// Get the Module's list of aliases.
319 AliasListType &getAliasList() { return AliasList; }
320 static iplist<GlobalAlias> Module::*getSublistAccess(GlobalAlias*) {
321 return &Module::AliasList;
323 /// Get the symbol table of global variable and function identifiers
324 const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; }
325 /// Get the Module's symbol table of global variable and function identifiers.
326 ValueSymbolTable &getValueSymbolTable() { return *ValSymTab; }
327 /// Get the symbol table of types
328 const TypeSymbolTable &getTypeSymbolTable() const { return *TypeSymTab; }
329 /// Get the Module's symbol table of types
330 TypeSymbolTable &getTypeSymbolTable() { return *TypeSymTab; }
333 /// @name Global Variable Iteration
336 /// Get an iterator to the first global variable
337 global_iterator global_begin() { return GlobalList.begin(); }
338 /// Get a constant iterator to the first global variable
339 const_global_iterator global_begin() const { return GlobalList.begin(); }
340 /// Get an iterator to the last global variable
341 global_iterator global_end () { return GlobalList.end(); }
342 /// Get a constant iterator to the last global variable
343 const_global_iterator global_end () const { return GlobalList.end(); }
344 /// Determine if the list of globals is empty.
345 bool global_empty() const { return GlobalList.empty(); }
348 /// @name Function Iteration
351 /// Get an iterator to the first function.
352 iterator begin() { return FunctionList.begin(); }
353 /// Get a constant iterator to the first function.
354 const_iterator begin() const { return FunctionList.begin(); }
355 /// Get an iterator to the last function.
356 iterator end () { return FunctionList.end(); }
357 /// Get a constant iterator to the last function.
358 const_iterator end () const { return FunctionList.end(); }
359 /// Determine how many functions are in the Module's list of functions.
360 size_t size() const { return FunctionList.size(); }
361 /// Determine if the list of functions is empty.
362 bool empty() const { return FunctionList.empty(); }
365 /// @name Dependent Library Iteration
368 /// @brief Get a constant iterator to beginning of dependent library list.
369 inline lib_iterator lib_begin() const { return LibraryList.begin(); }
370 /// @brief Get a constant iterator to end of dependent library list.
371 inline lib_iterator lib_end() const { return LibraryList.end(); }
372 /// @brief Returns the number of items in the list of libraries.
373 inline size_t lib_size() const { return LibraryList.size(); }
374 /// @brief Add a library to the list of dependent libraries
375 void addLibrary(const std::string& Lib);
376 /// @brief Remove a library from the list of dependent libraries
377 void removeLibrary(const std::string& Lib);
378 /// @brief Get all the libraries
379 inline const LibraryListType& getLibraries() const { return LibraryList; }
382 /// @name Alias Iteration
385 /// Get an iterator to the first alias.
386 alias_iterator alias_begin() { return AliasList.begin(); }
387 /// Get a constant iterator to the first alias.
388 const_alias_iterator alias_begin() const { return AliasList.begin(); }
389 /// Get an iterator to the last alias.
390 alias_iterator alias_end () { return AliasList.end(); }
391 /// Get a constant iterator to the last alias.
392 const_alias_iterator alias_end () const { return AliasList.end(); }
393 /// Determine how many functions are in the Module's list of aliases.
394 size_t alias_size () const { return AliasList.size(); }
395 /// Determine if the list of aliases is empty.
396 bool alias_empty() const { return AliasList.empty(); }
399 /// @name Utility functions for printing and dumping Module objects
402 /// Print the module to an output stream with AssemblyAnnotationWriter.
403 void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const;
404 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
406 /// Dump the module to stderr (for debugging).
408 /// This function causes all the subinstructions to "let go" of all references
409 /// that they are maintaining. This allows one to 'delete' a whole class at
410 /// a time, even though there may be circular references... first all
411 /// references are dropped, and all use counts go to zero. Then everything
412 /// is delete'd for real. Note that no operations are valid on an object
413 /// that has "dropped all references", except operator delete.
414 void dropAllReferences();
418 /// An iostream inserter for modules.
419 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
423 inline raw_ostream &operator<<(raw_ostream &O, const Module &M) {
428 } // End llvm namespace