Fix the xfail I added a couple of patches back. The issue
[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/Metadata.h"
22 #include "llvm/ADT/OwningPtr.h"
23 #include "llvm/System/DataTypes.h"
24 #include <vector>
25
26 namespace llvm {
27
28 class FunctionType;
29 class GVMaterializer;
30 class LLVMContext;
31 class MDSymbolTable;
32
33 template<> struct ilist_traits<Function>
34   : public SymbolTableListTraits<Function, Module> {
35
36   // createSentinel is used to get hold of the node that marks the end of the
37   // list... (same trick used here as in ilist_traits<Instruction>)
38   Function *createSentinel() const {
39     return static_cast<Function*>(&Sentinel);
40   }
41   static void destroySentinel(Function*) {}
42
43   Function *provideInitialHead() const { return createSentinel(); }
44   Function *ensureHead(Function*) const { return createSentinel(); }
45   static void noteHead(Function*, Function*) {}
46
47 private:
48   mutable ilist_node<Function> Sentinel;
49 };
50 template<> struct ilist_traits<GlobalVariable>
51   : public SymbolTableListTraits<GlobalVariable, Module> {
52   // createSentinel is used to create a node that marks the end of the list.
53   static GlobalVariable *createSentinel();
54   static void destroySentinel(GlobalVariable *GV) { delete GV; }
55 };
56 template<> struct ilist_traits<GlobalAlias>
57   : public SymbolTableListTraits<GlobalAlias, Module> {
58   // createSentinel is used to create a node that marks the end of the list.
59   static GlobalAlias *createSentinel();
60   static void destroySentinel(GlobalAlias *GA) { delete GA; }
61 };
62
63 template<> struct ilist_traits<NamedMDNode>
64   : public SymbolTableListTraits<NamedMDNode, Module> {
65   // createSentinel is used to get hold of a node that marks the end of
66   // the list...
67   NamedMDNode *createSentinel() const {
68     return static_cast<NamedMDNode*>(&Sentinel);
69   }
70   static void destroySentinel(NamedMDNode*) {}
71
72   NamedMDNode *provideInitialHead() const { return createSentinel(); }
73   NamedMDNode *ensureHead(NamedMDNode*) const { return createSentinel(); }
74   static void noteHead(NamedMDNode*, NamedMDNode*) {}
75   void addNodeToList(NamedMDNode *N);
76   void removeNodeFromList(NamedMDNode *N);
77 private:
78   mutable ilist_node<NamedMDNode> Sentinel;
79 };
80
81 /// A Module instance is used to store all the information related to an
82 /// LLVM module. Modules are the top level container of all other LLVM
83 /// Intermediate Representation (IR) objects. Each module directly contains a
84 /// list of globals variables, a list of functions, a list of libraries (or
85 /// other modules) this module depends on, a symbol table, and various data
86 /// about the target's characteristics.
87 ///
88 /// A module maintains a GlobalValRefMap object that is used to hold all
89 /// constant references to global variables in the module.  When a global
90 /// variable is destroyed, it should have no entries in the GlobalValueRefMap.
91 /// @brief The main container class for the LLVM Intermediate Representation.
92 class Module {
93 /// @name Types And Enumerations
94 /// @{
95 public:
96   /// The type for the list of global variables.
97   typedef iplist<GlobalVariable> GlobalListType;
98   /// The type for the list of functions.
99   typedef iplist<Function> FunctionListType;
100   /// The type for the list of aliases.
101   typedef iplist<GlobalAlias> AliasListType;
102   /// The type for the list of named metadata.
103   typedef iplist<NamedMDNode> NamedMDListType;
104
105   /// The type for the list of dependent libraries.
106   typedef std::vector<std::string> LibraryListType;
107
108   /// The Global Variable iterator.
109   typedef GlobalListType::iterator                      global_iterator;
110   /// The Global Variable constant iterator.
111   typedef GlobalListType::const_iterator          const_global_iterator;
112
113   /// The Function iterators.
114   typedef FunctionListType::iterator                           iterator;
115   /// The Function constant iterator
116   typedef FunctionListType::const_iterator               const_iterator;
117
118   /// The Global Alias iterators.
119   typedef AliasListType::iterator                        alias_iterator;
120   /// The Global Alias constant iterator
121   typedef AliasListType::const_iterator            const_alias_iterator;
122
123   /// The named metadata iterators.
124   typedef NamedMDListType::iterator             named_metadata_iterator;
125   /// The named metadata constant interators.
126   typedef NamedMDListType::const_iterator const_named_metadata_iterator;
127   /// The Library list iterator.
128   typedef LibraryListType::const_iterator lib_iterator;
129
130   /// An enumeration for describing the endianess of the target machine.
131   enum Endianness  { AnyEndianness, LittleEndian, BigEndian };
132
133   /// An enumeration for describing the size of a pointer on the target machine.
134   enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
135
136 /// @}
137 /// @name Member Variables
138 /// @{
139 private:
140   LLVMContext &Context;           ///< The LLVMContext from which types and
141                                   ///< constants are allocated.
142   GlobalListType GlobalList;      ///< The Global Variables in the module
143   FunctionListType FunctionList;  ///< The Functions in the module
144   AliasListType AliasList;        ///< The Aliases in the module
145   LibraryListType LibraryList;    ///< The Libraries needed by the module
146   NamedMDListType NamedMDList;    ///< The named metadata in the module
147   std::string GlobalScopeAsm;     ///< Inline Asm at global scope.
148   ValueSymbolTable *ValSymTab;    ///< Symbol table for values
149   TypeSymbolTable *TypeSymTab;    ///< Symbol table for types
150   OwningPtr<GVMaterializer> Materializer;  ///< Used to materialize GlobalValues
151   std::string ModuleID;           ///< Human readable identifier for the module
152   std::string TargetTriple;       ///< Platform target triple Module compiled on
153   std::string DataLayout;         ///< Target data description
154   MDSymbolTable *NamedMDSymTab;   ///< NamedMDNode names.
155
156   friend class Constant;
157
158 /// @}
159 /// @name Constructors
160 /// @{
161 public:
162   /// The Module constructor. Note that there is no default constructor. You
163   /// must provide a name for the module upon construction.
164   explicit Module(StringRef ModuleID, LLVMContext& C);
165   /// The module destructor. This will dropAllReferences.
166   ~Module();
167
168 /// @}
169 /// @name Module Level Accessors
170 /// @{
171
172   /// Get the module identifier which is, essentially, the name of the module.
173   /// @returns the module identifier as a string
174   const std::string &getModuleIdentifier() const { return ModuleID; }
175
176   /// Get the data layout string for the module's target platform.  This encodes
177   /// the type sizes and alignments expected by this module.
178   /// @returns the data layout as a string
179   const std::string &getDataLayout() const { return DataLayout; }
180
181   /// Get the target triple which is a string describing the target host.
182   /// @returns a string containing the target triple.
183   const std::string &getTargetTriple() const { return TargetTriple; }
184
185   /// Get the target endian information.
186   /// @returns Endianess - an enumeration for the endianess of the target
187   Endianness getEndianness() const;
188
189   /// Get the target pointer size.
190   /// @returns PointerSize - an enumeration for the size of the target's pointer
191   PointerSize getPointerSize() const;
192
193   /// Get the global data context.
194   /// @returns LLVMContext - a container for LLVM's global information
195   LLVMContext &getContext() const { return Context; }
196
197   /// Get any module-scope inline assembly blocks.
198   /// @returns a string containing the module-scope inline assembly blocks.
199   const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
200   
201 /// @}
202 /// @name Module Level Mutators
203 /// @{
204   
205   /// Set the module identifier.
206   void setModuleIdentifier(StringRef ID) { ModuleID = ID; }
207
208   /// Set the data layout
209   void setDataLayout(StringRef DL) { DataLayout = DL; }
210
211   /// Set the target triple.
212   void setTargetTriple(StringRef T) { TargetTriple = T; }
213
214   /// Set the module-scope inline assembly blocks.
215   void setModuleInlineAsm(StringRef Asm) { GlobalScopeAsm = Asm; }
216
217   /// Append to the module-scope inline assembly blocks, automatically inserting
218   /// a separating newline if necessary.
219   void appendModuleInlineAsm(StringRef Asm) {
220     if (!GlobalScopeAsm.empty() &&
221         GlobalScopeAsm[GlobalScopeAsm.size()-1] != '\n')
222       GlobalScopeAsm += '\n';
223     GlobalScopeAsm += Asm;
224   }
225
226 /// @}
227 /// @name Generic Value Accessors
228 /// @{
229
230   /// getNamedValue - Return the first global value in the module with
231   /// the specified name, of arbitrary type.  This method returns null
232   /// if a global with the specified name is not found.
233   GlobalValue *getNamedValue(StringRef Name) const;
234
235   /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
236   /// This ID is uniqued across modules in the current LLVMContext.
237   unsigned getMDKindID(StringRef Name) const;
238   
239   /// getMDKindNames - Populate client supplied SmallVector with the name for
240   /// custom metadata IDs registered in this LLVMContext.   ID #0 is not used,
241   /// so it is filled in as an empty string.
242   void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
243   
244 /// @}
245 /// @name Function Accessors
246 /// @{
247
248   /// getOrInsertFunction - Look up the specified function in the module symbol
249   /// table.  Four possibilities:
250   ///   1. If it does not exist, add a prototype for the function and return it.
251   ///   2. If it exists, and has a local linkage, the existing function is
252   ///      renamed and a new one is inserted.
253   ///   3. Otherwise, if the existing function has the correct prototype, return
254   ///      the existing function.
255   ///   4. Finally, the function exists but has the wrong prototype: return the
256   ///      function with a constantexpr cast to the right prototype.
257   Constant *getOrInsertFunction(StringRef Name, const FunctionType *T,
258                                 AttrListPtr AttributeList);
259
260   Constant *getOrInsertFunction(StringRef Name, const FunctionType *T);
261
262   /// getOrInsertFunction - Look up the specified function in the module symbol
263   /// table.  If it does not exist, add a prototype for the function and return
264   /// it.  This function guarantees to return a constant of pointer to the
265   /// specified function type or a ConstantExpr BitCast of that type if the
266   /// named function has a different type.  This version of the method takes a
267   /// null terminated list of function arguments, which makes it easier for
268   /// clients to use.
269   Constant *getOrInsertFunction(StringRef Name,
270                                 AttrListPtr AttributeList,
271                                 const Type *RetTy, ...)  END_WITH_NULL;
272
273   /// getOrInsertFunction - Same as above, but without the attributes.
274   Constant *getOrInsertFunction(StringRef Name, const Type *RetTy, ...)
275     END_WITH_NULL;
276
277   Constant *getOrInsertTargetIntrinsic(StringRef Name,
278                                        const FunctionType *Ty,
279                                        AttrListPtr AttributeList);
280   
281   /// getFunction - Look up the specified function in the module symbol table.
282   /// If it does not exist, return null.
283   Function *getFunction(StringRef Name) const;
284
285 /// @}
286 /// @name Global Variable Accessors
287 /// @{
288
289   /// getGlobalVariable - Look up the specified global variable in the module
290   /// symbol table.  If it does not exist, return null. If AllowInternal is set
291   /// to true, this function will return types that have InternalLinkage. By
292   /// default, these types are not returned.
293   GlobalVariable *getGlobalVariable(StringRef Name,
294                                     bool AllowInternal = false) const;
295
296   /// getNamedGlobal - Return the first global variable in the module with the
297   /// specified name, of arbitrary type.  This method returns null if a global
298   /// with the specified name is not found.
299   GlobalVariable *getNamedGlobal(StringRef Name) const {
300     return getGlobalVariable(Name, true);
301   }
302
303   /// getOrInsertGlobal - Look up the specified global in the module symbol
304   /// table.
305   ///   1. If it does not exist, add a declaration of the global and return it.
306   ///   2. Else, the global exists but has the wrong type: return the function
307   ///      with a constantexpr cast to the right type.
308   ///   3. Finally, if the existing global is the correct delclaration, return
309   ///      the existing global.
310   Constant *getOrInsertGlobal(StringRef Name, const Type *Ty);
311
312 /// @}
313 /// @name Global Alias Accessors
314 /// @{
315
316   /// getNamedAlias - Return the first global alias in the module with the
317   /// specified name, of arbitrary type.  This method returns null if a global
318   /// with the specified name is not found.
319   GlobalAlias *getNamedAlias(StringRef Name) const;
320
321 /// @}
322 /// @name Named Metadata Accessors
323 /// @{
324   
325   /// getNamedMetadata - Return the first NamedMDNode in the module with the
326   /// specified name. This method returns null if a NamedMDNode with the 
327   /// specified name is not found.
328   NamedMDNode *getNamedMetadata(StringRef Name) const;
329
330   /// getOrInsertNamedMetadata - Return the first named MDNode in the module 
331   /// with the specified name. This method returns a new NamedMDNode if a 
332   /// NamedMDNode with the specified name is not found.
333   NamedMDNode *getOrInsertNamedMetadata(StringRef Name);
334
335 /// @}
336 /// @name Type Accessors
337 /// @{
338
339   /// addTypeName - Insert an entry in the symbol table mapping Str to Type.  If
340   /// there is already an entry for this name, true is returned and the symbol
341   /// table is not modified.
342   bool addTypeName(StringRef Name, const Type *Ty);
343
344   /// getTypeName - If there is at least one entry in the symbol table for the
345   /// specified type, return it.
346   std::string getTypeName(const Type *Ty) const;
347
348   /// getTypeByName - Return the type with the specified name in this module, or
349   /// null if there is none by that name.
350   const Type *getTypeByName(StringRef Name) const;
351
352 /// @}
353 /// @name Materialization
354 /// @{
355
356   /// setMaterializer - Sets the GVMaterializer to GVM.  This module must not
357   /// yet have a Materializer.  To reset the materializer for a module that
358   /// already has one, call MaterializeAllPermanently first.  Destroying this
359   /// module will destroy its materializer without materializing any more
360   /// GlobalValues.  Without destroying the Module, there is no way to detach or
361   /// destroy a materializer without materializing all the GVs it controls, to
362   /// avoid leaving orphan unmaterialized GVs.
363   void setMaterializer(GVMaterializer *GVM);
364   /// getMaterializer - Retrieves the GVMaterializer, if any, for this Module.
365   GVMaterializer *getMaterializer() const { return Materializer.get(); }
366
367   /// isMaterializable - True if the definition of GV has yet to be materialized
368   /// from the GVMaterializer.
369   bool isMaterializable(const GlobalValue *GV) const;
370   /// isDematerializable - Returns true if this GV was loaded from this Module's
371   /// GVMaterializer and the GVMaterializer knows how to dematerialize the GV.
372   bool isDematerializable(const GlobalValue *GV) const;
373
374   /// Materialize - Make sure the GlobalValue is fully read.  If the module is
375   /// corrupt, this returns true and fills in the optional string with
376   /// information about the problem.  If successful, this returns false.
377   bool Materialize(GlobalValue *GV, std::string *ErrInfo = 0);
378   /// Dematerialize - If the GlobalValue is read in, and if the GVMaterializer
379   /// supports it, release the memory for the function, and set it up to be
380   /// materialized lazily.  If !isDematerializable(), this method is a noop.
381   void Dematerialize(GlobalValue *GV);
382
383   /// MaterializeAll - Make sure all GlobalValues in this Module are fully read.
384   /// If the module is corrupt, this returns true and fills in the optional
385   /// string with information about the problem.  If successful, this returns
386   /// false.
387   bool MaterializeAll(std::string *ErrInfo = 0);
388
389   /// MaterializeAllPermanently - Make sure all GlobalValues in this Module are
390   /// fully read and clear the Materializer.  If the module is corrupt, this
391   /// returns true, fills in the optional string with information about the
392   /// problem, and DOES NOT clear the old Materializer.  If successful, this
393   /// returns false.
394   bool MaterializeAllPermanently(std::string *ErrInfo = 0);
395
396 /// @}
397 /// @name Direct access to the globals list, functions list, and symbol table
398 /// @{
399
400   /// Get the Module's list of global variables (constant).
401   const GlobalListType   &getGlobalList() const       { return GlobalList; }
402   /// Get the Module's list of global variables.
403   GlobalListType         &getGlobalList()             { return GlobalList; }
404   static iplist<GlobalVariable> Module::*getSublistAccess(GlobalVariable*) {
405     return &Module::GlobalList;
406   }
407   /// Get the Module's list of functions (constant).
408   const FunctionListType &getFunctionList() const     { return FunctionList; }
409   /// Get the Module's list of functions.
410   FunctionListType       &getFunctionList()           { return FunctionList; }
411   static iplist<Function> Module::*getSublistAccess(Function*) {
412     return &Module::FunctionList;
413   }
414   /// Get the Module's list of aliases (constant).
415   const AliasListType    &getAliasList() const        { return AliasList; }
416   /// Get the Module's list of aliases.
417   AliasListType          &getAliasList()              { return AliasList; }
418   static iplist<GlobalAlias> Module::*getSublistAccess(GlobalAlias*) {
419     return &Module::AliasList;
420   }
421   /// Get the Module's list of named metadata (constant).
422   const NamedMDListType  &getNamedMDList() const      { return NamedMDList; }
423   /// Get the Module's list of named metadata.
424   NamedMDListType  &getNamedMDList()                  { return NamedMDList; }
425   static iplist<NamedMDNode> Module::*getSublistAccess(NamedMDNode *) {
426     return &Module::NamedMDList;
427   }
428   /// Get the symbol table of global variable and function identifiers
429   const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; }
430   /// Get the Module's symbol table of global variable and function identifiers.
431   ValueSymbolTable       &getValueSymbolTable()       { return *ValSymTab; }
432   /// Get the symbol table of types
433   const TypeSymbolTable  &getTypeSymbolTable() const  { return *TypeSymTab; }
434   /// Get the Module's symbol table of types
435   TypeSymbolTable        &getTypeSymbolTable()        { return *TypeSymTab; }
436   /// Get the symbol table of named metadata
437   const MDSymbolTable  &getMDSymbolTable() const      { return *NamedMDSymTab; }
438   /// Get the Module's symbol table of named metadata
439   MDSymbolTable        &getMDSymbolTable()            { return *NamedMDSymTab; }
440
441 /// @}
442 /// @name Global Variable Iteration
443 /// @{
444
445   /// Get an iterator to the first global variable
446   global_iterator       global_begin()       { return GlobalList.begin(); }
447   /// Get a constant iterator to the first global variable
448   const_global_iterator global_begin() const { return GlobalList.begin(); }
449   /// Get an iterator to the last global variable
450   global_iterator       global_end  ()       { return GlobalList.end(); }
451   /// Get a constant iterator to the last global variable
452   const_global_iterator global_end  () const { return GlobalList.end(); }
453   /// Determine if the list of globals is empty.
454   bool                  global_empty() const { return GlobalList.empty(); }
455
456 /// @}
457 /// @name Function Iteration
458 /// @{
459
460   /// Get an iterator to the first function.
461   iterator                begin()       { return FunctionList.begin(); }
462   /// Get a constant iterator to the first function.
463   const_iterator          begin() const { return FunctionList.begin(); }
464   /// Get an iterator to the last function.
465   iterator                end  ()       { return FunctionList.end();   }
466   /// Get a constant iterator to the last function.
467   const_iterator          end  () const { return FunctionList.end();   }
468   /// Determine how many functions are in the Module's list of functions.
469   size_t                  size() const  { return FunctionList.size(); }
470   /// Determine if the list of functions is empty.
471   bool                    empty() const { return FunctionList.empty(); }
472
473 /// @}
474 /// @name Dependent Library Iteration
475 /// @{
476
477   /// @brief Get a constant iterator to beginning of dependent library list.
478   inline lib_iterator lib_begin() const { return LibraryList.begin(); }
479   /// @brief Get a constant iterator to end of dependent library list.
480   inline lib_iterator lib_end()   const { return LibraryList.end();   }
481   /// @brief Returns the number of items in the list of libraries.
482   inline size_t       lib_size()  const { return LibraryList.size();  }
483   /// @brief Add a library to the list of dependent libraries
484   void addLibrary(StringRef Lib);
485   /// @brief Remove a library from the list of dependent libraries
486   void removeLibrary(StringRef Lib);
487   /// @brief Get all the libraries
488   inline const LibraryListType& getLibraries() const { return LibraryList; }
489
490 /// @}
491 /// @name Alias Iteration
492 /// @{
493
494   /// Get an iterator to the first alias.
495   alias_iterator       alias_begin()            { return AliasList.begin(); }
496   /// Get a constant iterator to the first alias.
497   const_alias_iterator alias_begin() const      { return AliasList.begin(); }
498   /// Get an iterator to the last alias.
499   alias_iterator       alias_end  ()            { return AliasList.end();   }
500   /// Get a constant iterator to the last alias.
501   const_alias_iterator alias_end  () const      { return AliasList.end();   }
502   /// Determine how many aliases are in the Module's list of aliases.
503   size_t               alias_size () const      { return AliasList.size();  }
504   /// Determine if the list of aliases is empty.
505   bool                 alias_empty() const      { return AliasList.empty(); }
506
507
508 /// @}
509 /// @name Named Metadata Iteration
510 /// @{
511
512   /// Get an iterator to the first named metadata.
513   named_metadata_iterator named_metadata_begin() { return NamedMDList.begin(); }
514   /// Get a constant iterator to the first named metadata.
515   const_named_metadata_iterator named_metadata_begin() const {
516     return NamedMDList.begin();
517   }
518   
519   /// Get an iterator to the last named metadata.
520   named_metadata_iterator named_metadata_end() { return NamedMDList.end(); }
521   /// Get a constant iterator to the last named metadata.
522   const_named_metadata_iterator named_metadata_end() const {
523     return NamedMDList.end();
524   }
525   
526   /// Determine how many NamedMDNodes are in the Module's list of named metadata.
527   size_t named_metadata_size() const { return NamedMDList.size();  }
528   /// Determine if the list of named metadata is empty.
529   bool named_metadata_empty() const { return NamedMDList.empty(); }
530
531
532 /// @}
533 /// @name Utility functions for printing and dumping Module objects
534 /// @{
535
536   /// Print the module to an output stream with AssemblyAnnotationWriter.
537   void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const;
538   
539   /// Dump the module to stderr (for debugging).
540   void dump() const;
541   /// This function causes all the subinstructions to "let go" of all references
542   /// that they are maintaining.  This allows one to 'delete' a whole class at
543   /// a time, even though there may be circular references... first all
544   /// references are dropped, and all use counts go to zero.  Then everything
545   /// is delete'd for real.  Note that no operations are valid on an object
546   /// that has "dropped all references", except operator delete.
547   void dropAllReferences();
548 /// @}
549 };
550
551 /// An raw_ostream inserter for modules.
552 inline raw_ostream &operator<<(raw_ostream &O, const Module &M) {
553   M.print(O, 0);
554   return O;
555 }
556
557 } // End llvm namespace
558
559 #endif