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