09eba81b4ebeefaf4db4a6bcad252d7a110f2f63
[oota-llvm.git] / include / llvm / Module.h
1 //===-- llvm/Module.h - C++ class to represent a VM module ------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// @file This file contains the declarations for the Module class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_MODULE_H
15 #define LLVM_MODULE_H
16
17 #include "llvm/Function.h"
18 #include "llvm/GlobalVariable.h"
19 #include "llvm/GlobalAlias.h"
20 #include "llvm/Support/DataTypes.h"
21 #include <vector>
22
23 namespace llvm {
24
25 class GlobalValueRefMap;   // Used by ConstantVals.cpp
26 class FunctionType;
27
28 template<> struct ilist_traits<Function>
29   : public SymbolTableListTraits<Function, Module> {
30   // createSentinel is used to create a node that marks the end of the list.
31   static Function *createSentinel();
32   static void destroySentinel(Function *F) { delete F; }
33   static iplist<Function> &getList(Module *M);
34   static inline ValueSymbolTable *getSymTab(Module *M);
35   static int getListOffset();
36 };
37 template<> struct ilist_traits<GlobalVariable>
38   : public SymbolTableListTraits<GlobalVariable, Module> {
39   // createSentinel is used to create a node that marks the end of the list.
40   static GlobalVariable *createSentinel();
41   static void destroySentinel(GlobalVariable *GV) { delete GV; }
42   static iplist<GlobalVariable> &getList(Module *M);
43   static inline ValueSymbolTable *getSymTab(Module *M);
44   static int getListOffset();
45 };
46 template<> struct ilist_traits<GlobalAlias>
47   : public SymbolTableListTraits<GlobalAlias, Module> {
48   // createSentinel is used to create a node that marks the end of the list.
49   static GlobalAlias *createSentinel();
50   static void destroySentinel(GlobalAlias *GA) { delete GA; }
51   static iplist<GlobalAlias> &getList(Module *M);
52   static inline ValueSymbolTable *getSymTab(Module *M);
53   static int getListOffset();
54 };
55
56 /// A Module instance is used to store all the information related to an
57 /// LLVM module. Modules are the top level container of all other LLVM
58 /// Intermediate Representation (IR) objects. Each module directly contains a
59 /// list of globals variables, a list of functions, a list of libraries (or
60 /// other modules) this module depends on, a symbol table, and various data
61 /// about the target's characteristics.
62 ///
63 /// A module maintains a GlobalValRefMap object that is used to hold all
64 /// constant references to global variables in the module.  When a global
65 /// variable is destroyed, it should have no entries in the GlobalValueRefMap.
66 /// @brief The main container class for the LLVM Intermediate Representation.
67 class Module {
68 /// @name Types And Enumerations
69 /// @{
70 public:
71   /// The type for the list of global variables.
72   typedef iplist<GlobalVariable> GlobalListType;
73   /// The type for the list of functions.
74   typedef iplist<Function> FunctionListType;
75   /// The type for the list of aliases.
76   typedef iplist<GlobalAlias> AliasListType;
77
78   /// The type for the list of dependent libraries.
79   typedef std::vector<std::string> LibraryListType;
80
81   /// The Global Variable iterator.
82   typedef GlobalListType::iterator                     global_iterator;
83   /// The Global Variable constant iterator.
84   typedef GlobalListType::const_iterator         const_global_iterator;
85
86   /// The Function iterators.
87   typedef FunctionListType::iterator                          iterator;
88   /// The Function constant iterator
89   typedef FunctionListType::const_iterator              const_iterator;
90
91   /// The Global Alias iterators.
92   typedef AliasListType::iterator                       alias_iterator;
93   /// The Global Alias constant iterator
94   typedef AliasListType::const_iterator           const_alias_iterator;
95
96   /// The Library list iterator.
97   typedef LibraryListType::const_iterator lib_iterator;
98
99   /// An enumeration for describing the endianess of the target machine.
100   enum Endianness  { AnyEndianness, LittleEndian, BigEndian };
101
102   /// An enumeration for describing the size of a pointer on the target machine.
103   enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
104
105 /// @}
106 /// @name Member Variables
107 /// @{
108 private:
109   GlobalListType GlobalList;     ///< The Global Variables in the module
110   FunctionListType FunctionList; ///< The Functions in the module
111   AliasListType AliasList;       ///< The Aliases in the module
112   LibraryListType LibraryList;   ///< The Libraries needed by the module
113   std::string GlobalScopeAsm;    ///< Inline Asm at global scope.
114   ValueSymbolTable *ValSymTab;   ///< Symbol table for values
115   TypeSymbolTable *TypeSymTab;   ///< Symbol table for types
116   std::string ModuleID;          ///< Human readable identifier for the module
117   std::string TargetTriple;      ///< Platform target triple Module compiled on
118   std::string DataLayout;        ///< Target data description
119
120   friend class Constant;
121
122 /// @}
123 /// @name Constructors
124 /// @{
125 public:
126   /// The Module constructor. Note that there is no default constructor. You
127   /// must provide a name for the module upon construction.
128   explicit Module(const std::string &ModuleID);
129   /// The module destructor. This will dropAllReferences.
130   ~Module();
131
132 /// @}
133 /// @name Module Level Accessors
134 /// @{
135 public:
136   /// Get the module identifier which is, essentially, the name of the module.
137   /// @returns the module identifier as a string
138   const std::string &getModuleIdentifier() const { return ModuleID; }
139
140   /// Get the data layout string for the module's target platform.  This encodes
141   /// the type sizes and alignments expected by this module.
142   /// @returns the data layout as a string
143   const std::string& getDataLayout() const { return DataLayout; }
144
145   /// Get the target triple which is a string describing the target host.
146   /// @returns a string containing the target triple.
147   const std::string &getTargetTriple() const { return TargetTriple; }
148
149   /// Get the target endian information.
150   /// @returns Endianess - an enumeration for the endianess of the target
151   Endianness getEndianness() const;
152
153   /// Get the target pointer size.
154   /// @returns PointerSize - an enumeration for the size of the target's pointer
155   PointerSize getPointerSize() const;
156
157   /// Get any module-scope inline assembly blocks.
158   /// @returns a string containing the module-scope inline assembly blocks.
159   const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
160 /// @}
161 /// @name Module Level Mutators
162 /// @{
163 public:
164
165   /// Set the module identifier.
166   void setModuleIdentifier(const std::string &ID) { ModuleID = ID; }
167
168   /// Set the data layout
169   void setDataLayout(const std::string& DL) { DataLayout = DL; }
170
171   /// Set the target triple.
172   void setTargetTriple(const std::string &T) { TargetTriple = T; }
173
174   /// Set the module-scope inline assembly blocks.
175   void setModuleInlineAsm(const std::string &Asm) { GlobalScopeAsm = Asm; }
176
177   /// Append to the module-scope inline assembly blocks, automatically
178   /// appending a newline to the end.
179   void appendModuleInlineAsm(const std::string &Asm) {
180     GlobalScopeAsm += Asm;
181     GlobalScopeAsm += '\n';
182   }
183
184 /// @}
185 /// @name Function Accessors
186 /// @{
187 public:
188   /// getOrInsertFunction - Look up the specified function in the module symbol
189   /// table.  Four possibilities:
190   ///   1. If it does not exist, add a prototype for the function and return it.
191   ///   2. If it exists, and has internal linkage, the existing function is
192   ///      renamed and a new one is inserted.
193   ///   3. Otherwise, if the existing function has the correct prototype, return
194   ///      the existing function.
195   ///   4. Finally, the function exists but has the wrong prototype: return the
196   ///      function with a constantexpr cast to the right prototype.
197   Constant *getOrInsertFunction(const std::string &Name, const FunctionType *T);
198
199   /// getOrInsertFunction - Look up the specified function in the module symbol
200   /// table.  If it does not exist, add a prototype for the function and return
201   /// it.  This function guarantees to return a constant of pointer to the
202   /// specified function type or a ConstantExpr BitCast of that type if the
203   /// named function has a different type.  This version of the method takes a
204   /// null terminated list of function arguments, which makes it easier for
205   /// clients to use.
206   Constant *getOrInsertFunction(const std::string &Name, const Type *RetTy,...)
207     END_WITH_NULL;
208
209   /// getFunction - Look up the specified function in the module symbol table.
210   /// If it does not exist, return null.
211   Function *getFunction(const std::string &Name) const;
212   Function *getFunction(const char *Name) const;
213
214 /// @}
215 /// @name Global Variable Accessors
216 /// @{
217 public:
218   /// getGlobalVariable - Look up the specified global variable in the module
219   /// symbol table.  If it does not exist, return null. If AllowInternal is set
220   /// to true, this function will return types that have InternalLinkage. By
221   /// default, these types are not returned.
222   GlobalVariable *getGlobalVariable(const std::string &Name,
223                                     bool AllowInternal = false) const;
224
225   /// getNamedGlobal - Return the first global variable in the module with the
226   /// specified name, of arbitrary type.  This method returns null if a global
227   /// with the specified name is not found.
228   GlobalVariable *getNamedGlobal(const std::string &Name) const {
229     return getGlobalVariable(Name, true);
230   }
231
232   /// getOrInsertGlobal - Look up the specified global in the module symbol
233   /// table.
234   ///   1. If it does not exist, add a declaration of the global and return it.
235   ///   2. Else, the global exists but has the wrong type: return the function
236   ///      with a constantexpr cast to the right type.
237   ///   3. Finally, if the existing global is the correct delclaration, return
238   ///      the existing global.
239   Constant *getOrInsertGlobal(const std::string &Name, const Type *Ty);
240
241 /// @}
242 /// @name Global Alias Accessors
243 /// @{
244 public:
245   /// getNamedAlias - Return the first global alias in the module with the
246   /// specified name, of arbitrary type.  This method returns null if a global
247   /// with the specified name is not found.
248   GlobalAlias *getNamedAlias(const std::string &Name) const;
249
250 /// @}
251 /// @name Type Accessors
252 /// @{
253 public:
254   /// addTypeName - Insert an entry in the symbol table mapping Str to Type.  If
255   /// there is already an entry for this name, true is returned and the symbol
256   /// table is not modified.
257   bool addTypeName(const std::string &Name, const Type *Ty);
258
259   /// getTypeName - If there is at least one entry in the symbol table for the
260   /// specified type, return it.
261   std::string getTypeName(const Type *Ty) const;
262
263   /// getTypeByName - Return the type with the specified name in this module, or
264   /// null if there is none by that name.
265   const Type *getTypeByName(const std::string &Name) const;
266
267 /// @}
268 /// @name Direct access to the globals list, functions list, and symbol table
269 /// @{
270 public:
271   /// Get the Module's list of global variables (constant).
272   const GlobalListType   &getGlobalList() const       { return GlobalList; }
273   /// Get the Module's list of global variables.
274   GlobalListType         &getGlobalList()             { return GlobalList; }
275   /// Get the Module's list of functions (constant).
276   const FunctionListType &getFunctionList() const     { return FunctionList; }
277   /// Get the Module's list of functions.
278   FunctionListType       &getFunctionList()           { return FunctionList; }
279   /// Get the Module's list of aliases (constant).
280   const AliasListType    &getAliasList() const        { return AliasList; }
281   /// Get the Module's list of aliases.
282   AliasListType          &getAliasList()              { return AliasList; }
283   /// Get the symbol table of global variable and function identifiers
284   const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; }
285   /// Get the Module's symbol table of global variable and function identifiers.
286   ValueSymbolTable       &getValueSymbolTable()       { return *ValSymTab; }
287   /// Get the symbol table of types
288   const TypeSymbolTable  &getTypeSymbolTable() const  { return *TypeSymTab; }
289   /// Get the Module's symbol table of types
290   TypeSymbolTable        &getTypeSymbolTable()        { return *TypeSymTab; }
291
292 /// @}
293 /// @name Global Variable Iteration
294 /// @{
295 public:
296   /// Get an iterator to the first global variable
297   global_iterator       global_begin()       { return GlobalList.begin(); }
298   /// Get a constant iterator to the first global variable
299   const_global_iterator global_begin() const { return GlobalList.begin(); }
300   /// Get an iterator to the last global variable
301   global_iterator       global_end  ()       { return GlobalList.end(); }
302   /// Get a constant iterator to the last global variable
303   const_global_iterator global_end  () const { return GlobalList.end(); }
304   /// Determine if the list of globals is empty.
305   bool                  global_empty() const { return GlobalList.empty(); }
306
307 /// @}
308 /// @name Function Iteration
309 /// @{
310 public:
311   /// Get an iterator to the first function.
312   iterator                begin()       { return FunctionList.begin(); }
313   /// Get a constant iterator to the first function.
314   const_iterator          begin() const { return FunctionList.begin(); }
315   /// Get an iterator to the last function.
316   iterator                end  ()       { return FunctionList.end();   }
317   /// Get a constant iterator to the last function.
318   const_iterator          end  () const { return FunctionList.end();   }
319   /// Determine how many functions are in the Module's list of functions.
320   size_t                  size() const  { return FunctionList.size(); }
321   /// Determine if the list of functions is empty.
322   bool                    empty() const { return FunctionList.empty(); }
323
324 /// @}
325 /// @name Dependent Library Iteration
326 /// @{
327 public:
328   /// @brief Get a constant iterator to beginning of dependent library list.
329   inline lib_iterator lib_begin() const { return LibraryList.begin(); }
330   /// @brief Get a constant iterator to end of dependent library list.
331   inline lib_iterator lib_end()   const { return LibraryList.end();   }
332   /// @brief Returns the number of items in the list of libraries.
333   inline size_t       lib_size()  const { return LibraryList.size();  }
334   /// @brief Add a library to the list of dependent libraries
335   void addLibrary(const std::string& Lib);
336   /// @brief Remove a library from the list of dependent libraries
337   void removeLibrary(const std::string& Lib);
338   /// @brief Get all the libraries
339   inline const LibraryListType& getLibraries() const { return LibraryList; }
340
341 /// @}
342 /// @name Alias Iteration
343 /// @{
344 public:
345   /// Get an iterator to the first alias.
346   alias_iterator       alias_begin()            { return AliasList.begin(); }
347   /// Get a constant iterator to the first alias.
348   const_alias_iterator alias_begin() const      { return AliasList.begin(); }
349   /// Get an iterator to the last alias.
350   alias_iterator       alias_end  ()            { return AliasList.end();   }
351   /// Get a constant iterator to the last alias.
352   const_alias_iterator alias_end  () const      { return AliasList.end();   }
353   /// Determine how many functions are in the Module's list of aliases.
354   size_t               alias_size () const      { return AliasList.size();  }
355   /// Determine if the list of aliases is empty.
356   bool                 alias_empty() const      { return AliasList.empty(); }
357
358 /// @}
359 /// @name Utility functions for printing and dumping Module objects
360 /// @{
361 public:
362   /// Print the module to an output stream with AssemblyAnnotationWriter.
363   void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const;
364   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
365   
366   /// Dump the module to stderr (for debugging).
367   void dump() const;
368   /// This function causes all the subinstructions to "let go" of all references
369   /// that they are maintaining.  This allows one to 'delete' a whole class at
370   /// a time, even though there may be circular references... first all
371   /// references are dropped, and all use counts go to zero.  Then everything
372   /// is delete'd for real.  Note that no operations are valid on an object
373   /// that has "dropped all references", except operator delete.
374   void dropAllReferences();
375 /// @}
376
377   static unsigned getFunctionListOffset() {
378     Module *Obj = 0;
379     return unsigned(reinterpret_cast<uintptr_t>(&Obj->FunctionList));
380   }
381   static unsigned getGlobalVariableListOffset() {
382     Module *Obj = 0;
383     return unsigned(reinterpret_cast<uintptr_t>(&Obj->GlobalList));
384   }
385   static unsigned getAliasListOffset() {
386     Module *Obj = 0;
387     return unsigned(reinterpret_cast<uintptr_t>(&Obj->AliasList));
388   }
389 };
390
391 /// An iostream inserter for modules.
392 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
393   M.print(O, 0);
394   return O;
395 }
396 inline raw_ostream &operator<<(raw_ostream &O, const Module &M) {
397   M.print(O, 0);
398   return O;
399 }
400   
401
402 inline ValueSymbolTable *
403 ilist_traits<Function>::getSymTab(Module *M) {
404   return M ? &M->getValueSymbolTable() : 0;
405 }
406
407 inline ValueSymbolTable *
408 ilist_traits<GlobalVariable>::getSymTab(Module *M) {
409   return M ? &M->getValueSymbolTable() : 0;
410 }
411
412 inline ValueSymbolTable *
413 ilist_traits<GlobalAlias>::getSymTab(Module *M) {
414   return M ? &M->getValueSymbolTable() : 0;
415 }
416
417 inline int
418 ilist_traits<Function>::getListOffset() {
419   return Module::getFunctionListOffset();
420 }
421
422 inline int
423 ilist_traits<GlobalVariable>::getListOffset() {
424   return Module::getGlobalVariableListOffset();
425 }
426
427 inline int
428 ilist_traits<GlobalAlias>::getListOffset() {
429   return Module::getAliasListOffset();
430 }
431
432 } // End llvm namespace
433
434 #endif