92f58699cad83a5f8c5424e5fe79f5f77672a60e
[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
11 /// Module.h This file contains the declarations for the Module class.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_MODULE_H
16 #define LLVM_MODULE_H
17
18 #include "llvm/Function.h"
19 #include "llvm/GlobalVariable.h"
20 #include "llvm/GlobalAlias.h"
21 #include "llvm/Support/DataTypes.h"
22 #include <vector>
23
24 namespace llvm {
25
26 class GlobalValueRefMap;   // Used by ConstantVals.cpp
27 class FunctionType;
28
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; }
34   static iplist<Function> &getList(Module *M);
35   static inline ValueSymbolTable *getSymTab(Module *M);
36   static int getListOffset();
37 };
38 template<> struct ilist_traits<GlobalVariable>
39   : public SymbolTableListTraits<GlobalVariable, Module> {
40   // createSentinel is used to create a node that marks the end of the list.
41   static GlobalVariable *createSentinel();
42   static void destroySentinel(GlobalVariable *GV) { delete GV; }
43   static iplist<GlobalVariable> &getList(Module *M);
44   static inline ValueSymbolTable *getSymTab(Module *M);
45   static int getListOffset();
46 };
47 template<> struct ilist_traits<GlobalAlias>
48   : public SymbolTableListTraits<GlobalAlias, Module> {
49   // createSentinel is used to create a node that marks the end of the list.
50   static GlobalAlias *createSentinel();
51   static void destroySentinel(GlobalAlias *GA) { delete GA; }
52   static iplist<GlobalAlias> &getList(Module *M);
53   static inline ValueSymbolTable *getSymTab(Module *M);
54   static int getListOffset();
55 };
56
57 /// A Module instance is used to store all the information related to an
58 /// LLVM module. Modules are the top level container of all other LLVM
59 /// Intermediate Representation (IR) objects. Each module directly contains a
60 /// list of globals variables, a list of functions, a list of libraries (or
61 /// other modules) this module depends on, a symbol table, and various data
62 /// about the target's characteristics.
63 ///
64 /// A module maintains a GlobalValRefMap object that is used to hold all
65 /// constant references to global variables in the module.  When a global
66 /// variable is destroyed, it should have no entries in the GlobalValueRefMap.
67 /// @brief The main container class for the LLVM Intermediate Representation.
68 class Module {
69 /// @name Types And Enumerations
70 /// @{
71 public:
72   /// The type for the list of global variables.
73   typedef iplist<GlobalVariable> GlobalListType;
74   /// The type for the list of functions.
75   typedef iplist<Function> FunctionListType;
76   /// The type for the list of aliases.
77   typedef iplist<GlobalAlias> AliasListType;
78
79   /// The type for the list of dependent libraries.
80   typedef std::vector<std::string> LibraryListType;
81
82   /// The Global Variable iterator.
83   typedef GlobalListType::iterator                     global_iterator;
84   /// The Global Variable constant iterator.
85   typedef GlobalListType::const_iterator         const_global_iterator;
86
87   /// The Function iterators.
88   typedef FunctionListType::iterator                          iterator;
89   /// The Function constant iterator
90   typedef FunctionListType::const_iterator              const_iterator;
91
92   /// The Global Alias iterators.
93   typedef AliasListType::iterator                       alias_iterator;
94   /// The Global Alias constant iterator
95   typedef AliasListType::const_iterator           const_alias_iterator;
96
97   /// The Library list iterator.
98   typedef LibraryListType::const_iterator lib_iterator;
99
100   /// An enumeration for describing the endianess of the target machine.
101   enum Endianness  { AnyEndianness, LittleEndian, BigEndian };
102
103   /// An enumeration for describing the size of a pointer on the target machine.
104   enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
105
106 /// @}
107 /// @name Member Variables
108 /// @{
109 private:
110   GlobalListType GlobalList;     ///< The Global Variables in the module
111   FunctionListType FunctionList; ///< The Functions in the module
112   AliasListType AliasList;       ///< The Aliases in the module
113   LibraryListType LibraryList;   ///< The Libraries needed by the module
114   std::string GlobalScopeAsm;    ///< Inline Asm at global scope.
115   ValueSymbolTable *ValSymTab;   ///< Symbol table for values
116   TypeSymbolTable *TypeSymTab;   ///< Symbol table for types
117   std::string ModuleID;          ///< Human readable identifier for the module
118   std::string TargetTriple;      ///< Platform target triple Module compiled on
119   std::string DataLayout;        ///< Target data description
120
121   friend class Constant;
122
123 /// @}
124 /// @name Constructors
125 /// @{
126 public:
127   /// The Module constructor. Note that there is no default constructor. You
128   /// must provide a name for the module upon construction.
129   explicit Module(const std::string &ModuleID);
130   /// The module destructor. This will dropAllReferences.
131   ~Module();
132
133 /// @}
134 /// @name Module Level Accessors
135 /// @{
136 public:
137   /// Get the module identifier which is, essentially, the name of the module.
138   /// @returns the module identifier as a string
139   const std::string &getModuleIdentifier() const { return ModuleID; }
140
141   /// Get the data layout string for the module's target platform.  This encodes
142   /// the type sizes and alignments expected by this module.
143   /// @returns the data layout as a string
144   const std::string& getDataLayout() const { return DataLayout; }
145
146   /// Get the target triple which is a string describing the target host.
147   /// @returns a string containing the target triple.
148   const std::string &getTargetTriple() const { return TargetTriple; }
149
150   /// Get the target endian information.
151   /// @returns Endianess - an enumeration for the endianess of the target
152   Endianness getEndianness() const;
153
154   /// Get the target pointer size.
155   /// @returns PointerSize - an enumeration for the size of the target's pointer
156   PointerSize getPointerSize() const;
157
158   /// Get any module-scope inline assembly blocks.
159   /// @returns a string containing the module-scope inline assembly blocks.
160   const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
161 /// @}
162 /// @name Module Level Mutators
163 /// @{
164 public:
165
166   /// Set the module identifier.
167   void setModuleIdentifier(const std::string &ID) { ModuleID = ID; }
168
169   /// Set the data layout
170   void setDataLayout(const std::string& DL) { DataLayout = DL; }
171
172   /// Set the target triple.
173   void setTargetTriple(const std::string &T) { TargetTriple = T; }
174
175   /// Set the module-scope inline assembly blocks.
176   void setModuleInlineAsm(const std::string &Asm) { GlobalScopeAsm = Asm; }
177
178   /// Append to the module-scope inline assembly blocks, automatically
179   /// appending a newline to the end.
180   void appendModuleInlineAsm(const std::string &Asm) {
181     GlobalScopeAsm += Asm;
182     GlobalScopeAsm += '\n';
183   }
184
185 /// @}
186 /// @name Generic Value Accessors
187 /// @{
188
189   /// getNamedValue - Return the first global value in the module with
190   /// the specified name, of arbitrary type.  This method returns null
191   /// if a global with the specified name is not found.
192   GlobalValue *getNamedValue(const std::string &Name) const;
193   GlobalValue *getNamedValue(const char *Name) const;
194
195 /// @}
196 /// @name Function Accessors
197 /// @{
198 public:
199   /// getOrInsertFunction - Look up the specified function in the module symbol
200   /// table.  Four possibilities:
201   ///   1. If it does not exist, add a prototype for the function and return it.
202   ///   2. If it exists, and has a local linkage, the existing function is
203   ///      renamed and a new one is inserted.
204   ///   3. Otherwise, if the existing function has the correct prototype, return
205   ///      the existing function.
206   ///   4. Finally, the function exists but has the wrong prototype: return the
207   ///      function with a constantexpr cast to the right prototype.
208   Constant *getOrInsertFunction(const std::string &Name, const FunctionType *T,
209                                 AttrListPtr AttributeList);
210
211   Constant *getOrInsertFunction(const std::string &Name, const FunctionType *T);
212
213   /// getOrInsertFunction - Look up the specified function in the module symbol
214   /// table.  If it does not exist, add a prototype for the function and return
215   /// it.  This function guarantees to return a constant of pointer to the
216   /// specified function type or a ConstantExpr BitCast of that type if the
217   /// named function has a different type.  This version of the method takes a
218   /// null terminated list of function arguments, which makes it easier for
219   /// clients to use.
220   Constant *getOrInsertFunction(const std::string &Name,
221                                 AttrListPtr AttributeList,
222                                 const Type *RetTy, ...)  END_WITH_NULL;
223
224   Constant *getOrInsertFunction(const std::string &Name, const Type *RetTy, ...)
225     END_WITH_NULL;
226
227   Constant *getOrInsertTargetIntrinsic(const std::string &Name,
228                                        const FunctionType *Ty,
229                                        AttrListPtr AttributeList);
230   
231   /// getFunction - Look up the specified function in the module symbol table.
232   /// If it does not exist, return null.
233   Function *getFunction(const std::string &Name) const;
234   Function *getFunction(const char *Name) const;
235
236 /// @}
237 /// @name Global Variable Accessors
238 /// @{
239 public:
240   /// getGlobalVariable - Look up the specified global variable in the module
241   /// symbol table.  If it does not exist, return null. If AllowInternal is set
242   /// to true, this function will return types that have InternalLinkage. By
243   /// default, these types are not returned.
244   GlobalVariable *getGlobalVariable(const std::string &Name,
245                                     bool AllowInternal = false) const;
246
247   /// getNamedGlobal - Return the first global variable in the module with the
248   /// specified name, of arbitrary type.  This method returns null if a global
249   /// with the specified name is not found.
250   GlobalVariable *getNamedGlobal(const std::string &Name) const {
251     return getGlobalVariable(Name, true);
252   }
253
254   /// getOrInsertGlobal - Look up the specified global in the module symbol
255   /// table.
256   ///   1. If it does not exist, add a declaration of the global and return it.
257   ///   2. Else, the global exists but has the wrong type: return the function
258   ///      with a constantexpr cast to the right type.
259   ///   3. Finally, if the existing global is the correct delclaration, return
260   ///      the existing global.
261   Constant *getOrInsertGlobal(const std::string &Name, const Type *Ty);
262
263 /// @}
264 /// @name Global Alias Accessors
265 /// @{
266 public:
267   /// getNamedAlias - Return the first global alias in the module with the
268   /// specified name, of arbitrary type.  This method returns null if a global
269   /// with the specified name is not found.
270   GlobalAlias *getNamedAlias(const std::string &Name) const;
271
272 /// @}
273 /// @name Type Accessors
274 /// @{
275 public:
276   /// addTypeName - Insert an entry in the symbol table mapping Str to Type.  If
277   /// there is already an entry for this name, true is returned and the symbol
278   /// table is not modified.
279   bool addTypeName(const std::string &Name, const Type *Ty);
280
281   /// getTypeName - If there is at least one entry in the symbol table for the
282   /// specified type, return it.
283   std::string getTypeName(const Type *Ty) const;
284
285   /// getTypeByName - Return the type with the specified name in this module, or
286   /// null if there is none by that name.
287   const Type *getTypeByName(const std::string &Name) const;
288
289 /// @}
290 /// @name Direct access to the globals list, functions list, and symbol table
291 /// @{
292 public:
293   /// Get the Module's list of global variables (constant).
294   const GlobalListType   &getGlobalList() const       { return GlobalList; }
295   /// Get the Module's list of global variables.
296   GlobalListType         &getGlobalList()             { return GlobalList; }
297   /// Get the Module's list of functions (constant).
298   const FunctionListType &getFunctionList() const     { return FunctionList; }
299   /// Get the Module's list of functions.
300   FunctionListType       &getFunctionList()           { return FunctionList; }
301   /// Get the Module's list of aliases (constant).
302   const AliasListType    &getAliasList() const        { return AliasList; }
303   /// Get the Module's list of aliases.
304   AliasListType          &getAliasList()              { return 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; }
313
314 /// @}
315 /// @name Global Variable Iteration
316 /// @{
317 public:
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(); }
328
329 /// @}
330 /// @name Function Iteration
331 /// @{
332 public:
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(); }
345
346 /// @}
347 /// @name Dependent Library Iteration
348 /// @{
349 public:
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; }
362
363 /// @}
364 /// @name Alias Iteration
365 /// @{
366 public:
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(); }
379
380 /// @}
381 /// @name Utility functions for printing and dumping Module objects
382 /// @{
383 public:
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;
387   
388   /// Dump the module to stderr (for debugging).
389   void dump() const;
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();
397 /// @}
398
399   static unsigned getFunctionListOffset() {
400     Module *Obj = 0;
401     return unsigned(reinterpret_cast<uintptr_t>(&Obj->FunctionList));
402   }
403   static unsigned getGlobalVariableListOffset() {
404     Module *Obj = 0;
405     return unsigned(reinterpret_cast<uintptr_t>(&Obj->GlobalList));
406   }
407   static unsigned getAliasListOffset() {
408     Module *Obj = 0;
409     return unsigned(reinterpret_cast<uintptr_t>(&Obj->AliasList));
410   }
411 };
412
413 /// An iostream inserter for modules.
414 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
415   M.print(O, 0);
416   return O;
417 }
418 inline raw_ostream &operator<<(raw_ostream &O, const Module &M) {
419   M.print(O, 0);
420   return O;
421 }
422   
423
424 inline ValueSymbolTable *
425 ilist_traits<Function>::getSymTab(Module *M) {
426   return M ? &M->getValueSymbolTable() : 0;
427 }
428
429 inline ValueSymbolTable *
430 ilist_traits<GlobalVariable>::getSymTab(Module *M) {
431   return M ? &M->getValueSymbolTable() : 0;
432 }
433
434 inline ValueSymbolTable *
435 ilist_traits<GlobalAlias>::getSymTab(Module *M) {
436   return M ? &M->getValueSymbolTable() : 0;
437 }
438
439 inline int
440 ilist_traits<Function>::getListOffset() {
441   return Module::getFunctionListOffset();
442 }
443
444 inline int
445 ilist_traits<GlobalVariable>::getListOffset() {
446   return Module::getGlobalVariableListOffset();
447 }
448
449 inline int
450 ilist_traits<GlobalAlias>::getListOffset() {
451   return Module::getAliasListOffset();
452 }
453
454 } // End llvm namespace
455
456 #endif