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 // This file contains the declarations for the Module class that is used to
11 // maintain all the information related to a VM module.
13 // A module also maintains a GlobalValRefMap object that is used to hold all
14 // constant references to global variables in the module. When a global
15 // variable is destroyed, it should have no entries in the GlobalValueRefMap.
17 //===----------------------------------------------------------------------===//
22 #include "llvm/Function.h"
23 #include "llvm/GlobalVariable.h"
24 #include "llvm/ADT/SetVector.h"
29 class GlobalValueRefMap; // Used by ConstantVals.cpp
33 template<> struct ilist_traits<Function>
34 : public SymbolTableListTraits<Function, Module, Module> {
35 // createSentinel is used to create a node that marks the end of the list.
36 static Function *createSentinel();
37 static void destroySentinel(Function *F) { delete F; }
38 static iplist<Function> &getList(Module *M);
40 template<> struct ilist_traits<GlobalVariable>
41 : public SymbolTableListTraits<GlobalVariable, Module, Module> {
42 // createSentinel is used to create a node that marks the end of the list.
43 static GlobalVariable *createSentinel();
44 static void destroySentinel(GlobalVariable *GV) { delete GV; }
45 static iplist<GlobalVariable> &getList(Module *M);
50 typedef iplist<GlobalVariable> GlobalListType;
51 typedef iplist<Function> FunctionListType;
52 typedef SetVector<std::string> LibraryListType;
54 // Global Variable iterators...
55 typedef GlobalListType::iterator global_iterator;
56 typedef GlobalListType::const_iterator const_global_iterator;
57 typedef std::reverse_iterator<global_iterator> reverse_global_iterator;
58 typedef std::reverse_iterator<const_global_iterator> const_reverse_global_iterator;
59 typedef global_iterator giterator; // these are legacy, deprecated
60 typedef const_global_iterator const_giterator;
61 typedef reverse_global_iterator reverse_giterator;
62 typedef const_reverse_global_iterator const_reverse_giterator;
64 // Function iterators...
65 typedef FunctionListType::iterator iterator;
66 typedef FunctionListType::const_iterator const_iterator;
67 typedef std::reverse_iterator<iterator> reverse_iterator;
68 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
70 // Library list iterators
71 typedef LibraryListType::const_iterator lib_iterator;
73 enum Endianness { AnyEndianness, LittleEndian, BigEndian };
74 enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
77 GlobalListType GlobalList; // The Global Variables in the module
78 FunctionListType FunctionList; // The Functions in the module
79 LibraryListType LibraryList; // The Libraries needed by the module
80 SymbolTable *SymTab; // Symbol Table for the module
81 std::string ModuleID; // Human readable identifier for the module
82 std::string TargetTriple; // Platform target triple Module compiled on
84 // These flags are probably not the right long-term way to handle this kind of
85 // target information, but it is sufficient for now.
86 Endianness Endian; // True if target is little endian
87 PointerSize PtrSize; // True if target has 32-bit pointers (false = 64-bit)
89 friend class Constant;
92 Module(const std::string &ModuleID);
95 const std::string& getModuleIdentifier() const { return ModuleID; }
96 const std::string& getTargetTriple() const { return TargetTriple; }
97 void setTargetTriple(const std::string& T) { TargetTriple = T; }
99 /// Target endian information...
100 Endianness getEndianness() const { return Endian; }
101 void setEndianness(Endianness E) { Endian = E; }
103 /// Target Pointer Size information...
104 PointerSize getPointerSize() const { return PtrSize; }
105 void setPointerSize(PointerSize PS) { PtrSize = PS; }
107 //===--------------------------------------------------------------------===//
108 // Methods for easy access to the functions in the module.
111 /// getOrInsertFunction - Look up the specified function in the module symbol
112 /// table. If it does not exist, add a prototype for the function and return
114 Function *getOrInsertFunction(const std::string &Name, const FunctionType *T);
116 /// getOrInsertFunction - Look up the specified function in the module symbol
117 /// table. If it does not exist, add a prototype for the function and return
118 /// it. This version of the method takes a null terminated list of function
119 /// arguments, which makes it easier for clients to use.
120 Function *getOrInsertFunction(const std::string &Name, const Type *RetTy,...);
122 /// getFunction - Look up the specified function in the module symbol table.
123 /// If it does not exist, return null.
125 Function *getFunction(const std::string &Name, const FunctionType *Ty);
127 /// getMainFunction - This function looks up main efficiently. This is such a
128 /// common case, that it is a method in Module. If main cannot be found, a
129 /// null pointer is returned.
131 Function *getMainFunction();
133 /// getNamedFunction - Return the first function in the module with the
134 /// specified name, of arbitrary type. This method returns null if a function
135 /// with the specified name is not found.
137 Function *getNamedFunction(const std::string &Name);
139 //===--------------------------------------------------------------------===//
140 // Methods for easy access to the global variables in the module.
143 /// getGlobalVariable - Look up the specified global variable in the module
144 /// symbol table. If it does not exist, return null. Note that this only
145 /// returns a global variable if it does not have internal linkage. The type
146 /// argument should be the underlying type of the global, i.e., it should not
147 /// have the top-level PointerType, which represents the address of the
150 GlobalVariable *getGlobalVariable(const std::string &Name, const Type *Ty);
153 //===--------------------------------------------------------------------===//
154 // Methods for easy access to the types in the module.
157 /// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
158 /// there is already an entry for this name, true is returned and the symbol
159 /// table is not modified.
161 bool addTypeName(const std::string &Name, const Type *Ty);
163 /// getTypeName - If there is at least one entry in the symbol table for the
164 /// specified type, return it.
166 std::string getTypeName(const Type *Ty) const;
168 /// getTypeByName - Return the type with the specified name in this module, or
169 /// null if there is none by that name.
170 const Type *getTypeByName(const std::string &Name) const;
173 //===--------------------------------------------------------------------===//
174 // Methods for direct access to the globals list, functions list, and symbol
178 /// Get the underlying elements of the Module...
179 inline const GlobalListType &getGlobalList() const { return GlobalList; }
180 inline GlobalListType &getGlobalList() { return GlobalList; }
181 inline const FunctionListType &getFunctionList() const { return FunctionList;}
182 inline FunctionListType &getFunctionList() { return FunctionList;}
184 /// getSymbolTable() - Get access to the symbol table for the module, where
185 /// global variables and functions are identified.
187 inline SymbolTable &getSymbolTable() { return *SymTab; }
188 inline const SymbolTable &getSymbolTable() const { return *SymTab; }
191 //===--------------------------------------------------------------------===//
192 // Module iterator forwarding functions
194 // Globals list interface
195 inline global_iterator global_begin() { return GlobalList.begin(); }
196 inline const_global_iterator global_begin() const { return GlobalList.begin(); }
197 inline global_iterator global_end () { return GlobalList.end(); }
198 inline const_global_iterator global_end () const { return GlobalList.end(); }
200 inline reverse_global_iterator global_rbegin() { return GlobalList.rbegin(); }
201 inline const_reverse_global_iterator global_rbegin() const { return GlobalList.rbegin(); }
202 inline reverse_global_iterator global_rend () { return GlobalList.rend(); }
203 inline const_reverse_global_iterator global_rend () const { return GlobalList.rend(); }
205 inline size_t global_size () const { return GlobalList.size(); }
206 inline bool global_empty() const { return GlobalList.empty(); }
207 inline const GlobalVariable &global_front() const { return GlobalList.front(); }
208 inline GlobalVariable &global_front() { return GlobalList.front(); }
209 inline const GlobalVariable &global_back () const { return GlobalList.back(); }
210 inline GlobalVariable &global_back () { return GlobalList.back(); }
212 //===--------------------------------------------------------------------===//
213 // Module iterator forwarding functions (legacy, deprecated, will be removed)
215 // Globals list interface
216 inline global_iterator gbegin() { return GlobalList.begin(); }
217 inline const_global_iterator gbegin() const { return GlobalList.begin(); }
218 inline global_iterator gend () { return GlobalList.end(); }
219 inline const_global_iterator gend () const { return GlobalList.end(); }
221 inline reverse_global_iterator grbegin() { return GlobalList.rbegin(); }
222 inline const_reverse_global_iterator grbegin() const { return GlobalList.rbegin(); }
223 inline reverse_global_iterator grend () { return GlobalList.rend(); }
224 inline const_reverse_global_iterator grend () const { return GlobalList.rend(); }
226 inline size_t gsize() const { return GlobalList.size(); }
227 inline bool gempty() const { return GlobalList.empty(); }
228 inline const GlobalVariable &gfront() const { return GlobalList.front(); }
229 inline GlobalVariable &gfront() { return GlobalList.front(); }
230 inline const GlobalVariable &gback() const { return GlobalList.back(); }
231 inline GlobalVariable &gback() { return GlobalList.back(); }
233 // FunctionList interface
234 inline iterator begin() { return FunctionList.begin(); }
235 inline const_iterator begin() const { return FunctionList.begin(); }
236 inline iterator end () { return FunctionList.end(); }
237 inline const_iterator end () const { return FunctionList.end(); }
239 inline reverse_iterator rbegin() { return FunctionList.rbegin(); }
240 inline const_reverse_iterator rbegin() const { return FunctionList.rbegin(); }
241 inline reverse_iterator rend () { return FunctionList.rend(); }
242 inline const_reverse_iterator rend () const { return FunctionList.rend(); }
244 inline size_t size() const { return FunctionList.size(); }
245 inline bool empty() const { return FunctionList.empty(); }
246 inline const Function &front() const { return FunctionList.front(); }
247 inline Function &front() { return FunctionList.front(); }
248 inline const Function &back() const { return FunctionList.back(); }
249 inline Function &back() { return FunctionList.back(); }
251 //===--------------------------------------------------------------------===//
252 // List of dependent library access functions
254 /// @brief Get a constant iterator to beginning of dependent library list.
255 inline lib_iterator lib_begin() const { return LibraryList.begin(); }
257 /// @brief Get a constant iterator to end of dependent library list.
258 inline lib_iterator lib_end() const { return LibraryList.end(); }
260 /// @brief Returns the number of items in the list of libraries.
261 inline size_t lib_size() const { return LibraryList.size(); }
263 /// @brief Add a library to the list of dependent libraries
264 inline void addLibrary(const std::string& Lib){ LibraryList.insert(Lib); }
266 /// @brief Remove a library from the list of dependent libraries
267 inline void removeLibrary(const std::string& Lib) { LibraryList.remove(Lib); }
269 /// @brief Get all the libraries
270 inline const LibraryListType& getLibraries() const { return LibraryList; }
272 //===--------------------------------------------------------------------===//
273 // Utility functions for printing and dumping Module objects
275 void print(std::ostream &OS) const { print(OS, 0); }
276 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
280 /// dropAllReferences() - This function causes all the subinstructions to "let
281 /// go" of all references that they are maintaining. This allows one to
282 /// 'delete' a whole class at a time, even though there may be circular
283 /// references... first all references are dropped, and all use counts go to
284 /// zero. Then everything is delete'd for real. Note that no operations are
285 /// valid on an object that has "dropped all references", except operator
288 void dropAllReferences();
291 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
296 } // End llvm namespace