Avoid angle brackets in comment. [-Wdocumentation-html]
[oota-llvm.git] / include / llvm / IR / 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_IR_MODULE_H
16 #define LLVM_IR_MODULE_H
17
18 #include "llvm/ADT/iterator_range.h"
19 #include "llvm/IR/Comdat.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/GlobalAlias.h"
23 #include "llvm/IR/GlobalVariable.h"
24 #include "llvm/IR/Metadata.h"
25 #include "llvm/Support/CBindingWrapping.h"
26 #include "llvm/Support/CodeGen.h"
27 #include "llvm/Support/DataTypes.h"
28 #include <system_error>
29
30 namespace llvm {
31 class FunctionType;
32 class GVMaterializer;
33 class LLVMContext;
34 class RandomNumberGenerator;
35 class StructType;
36 template<typename T> struct DenseMapInfo;
37 template<typename KeyT, typename ValueT, typename KeyInfoT> class DenseMap;
38
39 template<> struct ilist_traits<Function>
40   : public SymbolTableListTraits<Function, Module> {
41
42   // createSentinel is used to get hold of the node that marks the end of the
43   // list... (same trick used here as in ilist_traits<Instruction>)
44   Function *createSentinel() const {
45     return static_cast<Function*>(&Sentinel);
46   }
47   static void destroySentinel(Function*) {}
48
49   Function *provideInitialHead() const { return createSentinel(); }
50   Function *ensureHead(Function*) const { return createSentinel(); }
51   static void noteHead(Function*, Function*) {}
52
53 private:
54   mutable ilist_node<Function> Sentinel;
55 };
56
57 template<> struct ilist_traits<GlobalVariable>
58   : public SymbolTableListTraits<GlobalVariable, Module> {
59   // createSentinel is used to create a node that marks the end of the list.
60   GlobalVariable *createSentinel() const {
61     return static_cast<GlobalVariable*>(&Sentinel);
62   }
63   static void destroySentinel(GlobalVariable*) {}
64
65   GlobalVariable *provideInitialHead() const { return createSentinel(); }
66   GlobalVariable *ensureHead(GlobalVariable*) const { return createSentinel(); }
67   static void noteHead(GlobalVariable*, GlobalVariable*) {}
68 private:
69   mutable ilist_node<GlobalVariable> Sentinel;
70 };
71
72 template<> struct ilist_traits<GlobalAlias>
73   : public SymbolTableListTraits<GlobalAlias, Module> {
74   // createSentinel is used to create a node that marks the end of the list.
75   GlobalAlias *createSentinel() const {
76     return static_cast<GlobalAlias*>(&Sentinel);
77   }
78   static void destroySentinel(GlobalAlias*) {}
79
80   GlobalAlias *provideInitialHead() const { return createSentinel(); }
81   GlobalAlias *ensureHead(GlobalAlias*) const { return createSentinel(); }
82   static void noteHead(GlobalAlias*, GlobalAlias*) {}
83 private:
84   mutable ilist_node<GlobalAlias> Sentinel;
85 };
86
87 template<> struct ilist_traits<NamedMDNode>
88   : public ilist_default_traits<NamedMDNode> {
89   // createSentinel is used to get hold of a node that marks the end of
90   // the list...
91   NamedMDNode *createSentinel() const {
92     return static_cast<NamedMDNode*>(&Sentinel);
93   }
94   static void destroySentinel(NamedMDNode*) {}
95
96   NamedMDNode *provideInitialHead() const { return createSentinel(); }
97   NamedMDNode *ensureHead(NamedMDNode*) const { return createSentinel(); }
98   static void noteHead(NamedMDNode*, NamedMDNode*) {}
99   void addNodeToList(NamedMDNode *) {}
100   void removeNodeFromList(NamedMDNode *) {}
101 private:
102   mutable ilist_node<NamedMDNode> Sentinel;
103 };
104
105 /// A Module instance is used to store all the information related to an
106 /// LLVM module. Modules are the top level container of all other LLVM
107 /// Intermediate Representation (IR) objects. Each module directly contains a
108 /// list of globals variables, a list of functions, a list of libraries (or
109 /// other modules) this module depends on, a symbol table, and various data
110 /// about the target's characteristics.
111 ///
112 /// A module maintains a GlobalValRefMap object that is used to hold all
113 /// constant references to global variables in the module.  When a global
114 /// variable is destroyed, it should have no entries in the GlobalValueRefMap.
115 /// @brief The main container class for the LLVM Intermediate Representation.
116 class Module {
117 /// @name Types And Enumerations
118 /// @{
119 public:
120   /// The type for the list of global variables.
121   typedef iplist<GlobalVariable> GlobalListType;
122   /// The type for the list of functions.
123   typedef iplist<Function> FunctionListType;
124   /// The type for the list of aliases.
125   typedef iplist<GlobalAlias> AliasListType;
126   /// The type for the list of named metadata.
127   typedef ilist<NamedMDNode> NamedMDListType;
128   /// The type of the comdat "symbol" table.
129   typedef StringMap<Comdat> ComdatSymTabType;
130
131   /// The Global Variable iterator.
132   typedef GlobalListType::iterator                      global_iterator;
133   /// The Global Variable constant iterator.
134   typedef GlobalListType::const_iterator          const_global_iterator;
135
136   /// The Function iterators.
137   typedef FunctionListType::iterator                           iterator;
138   /// The Function constant iterator
139   typedef FunctionListType::const_iterator               const_iterator;
140
141   /// The Function reverse iterator.
142   typedef FunctionListType::reverse_iterator             reverse_iterator;
143   /// The Function constant reverse iterator.
144   typedef FunctionListType::const_reverse_iterator const_reverse_iterator;
145
146   /// The Global Alias iterators.
147   typedef AliasListType::iterator                        alias_iterator;
148   /// The Global Alias constant iterator
149   typedef AliasListType::const_iterator            const_alias_iterator;
150
151   /// The named metadata iterators.
152   typedef NamedMDListType::iterator             named_metadata_iterator;
153   /// The named metadata constant iterators.
154   typedef NamedMDListType::const_iterator const_named_metadata_iterator;
155
156   /// This enumeration defines the supported behaviors of module flags.
157   enum ModFlagBehavior {
158     /// Emits an error if two values disagree, otherwise the resulting value is
159     /// that of the operands.
160     Error = 1,
161
162     /// Emits a warning if two values disagree. The result value will be the
163     /// operand for the flag from the first module being linked.
164     Warning = 2,
165
166     /// Adds a requirement that another module flag be present and have a
167     /// specified value after linking is performed. The value must be a metadata
168     /// pair, where the first element of the pair is the ID of the module flag
169     /// to be restricted, and the second element of the pair is the value the
170     /// module flag should be restricted to. This behavior can be used to
171     /// restrict the allowable results (via triggering of an error) of linking
172     /// IDs with the **Override** behavior.
173     Require = 3,
174
175     /// Uses the specified value, regardless of the behavior or value of the
176     /// other module. If both modules specify **Override**, but the values
177     /// differ, an error will be emitted.
178     Override = 4,
179
180     /// Appends the two values, which are required to be metadata nodes.
181     Append = 5,
182
183     /// Appends the two values, which are required to be metadata
184     /// nodes. However, duplicate entries in the second list are dropped
185     /// during the append operation.
186     AppendUnique = 6,
187
188     // Markers:
189     ModFlagBehaviorFirstVal = Error,
190     ModFlagBehaviorLastVal = AppendUnique
191   };
192
193   /// Checks if Value represents a valid ModFlagBehavior, and stores the
194   /// converted result in MFB.
195   static bool isValidModFlagBehavior(Value *V, ModFlagBehavior &MFB);
196
197   struct ModuleFlagEntry {
198     ModFlagBehavior Behavior;
199     MDString *Key;
200     Value *Val;
201     ModuleFlagEntry(ModFlagBehavior B, MDString *K, Value *V)
202       : Behavior(B), Key(K), Val(V) {}
203   };
204
205 /// @}
206 /// @name Member Variables
207 /// @{
208 private:
209   LLVMContext &Context;           ///< The LLVMContext from which types and
210                                   ///< constants are allocated.
211   GlobalListType GlobalList;      ///< The Global Variables in the module
212   FunctionListType FunctionList;  ///< The Functions in the module
213   AliasListType AliasList;        ///< The Aliases in the module
214   NamedMDListType NamedMDList;    ///< The named metadata in the module
215   std::string GlobalScopeAsm;     ///< Inline Asm at global scope.
216   ValueSymbolTable *ValSymTab;    ///< Symbol table for values
217   ComdatSymTabType ComdatSymTab;  ///< Symbol table for COMDATs
218   std::unique_ptr<GVMaterializer>
219   Materializer;                   ///< Used to materialize GlobalValues
220   std::string ModuleID;           ///< Human readable identifier for the module
221   std::string TargetTriple;       ///< Platform target triple Module compiled on
222                                   ///< Format: (arch)(sub)-(vendor)-(sys0-(abi)
223   void *NamedMDSymTab;            ///< NamedMDNode names.
224   // Allow lazy initialization in const method.
225   mutable RandomNumberGenerator *RNG; ///< The random number generator for this module.
226
227   // We need to keep the string because the C API expects us to own the string
228   // representation.
229   // Since we have it, we also use an empty string to represent a module without
230   // a DataLayout. If it has a DataLayout, these variables are in sync and the
231   // string is just a cache of getDataLayout()->getStringRepresentation().
232   std::string DataLayoutStr;
233   DataLayout DL;
234
235   friend class Constant;
236
237 /// @}
238 /// @name Constructors
239 /// @{
240 public:
241   /// The Module constructor. Note that there is no default constructor. You
242   /// must provide a name for the module upon construction.
243   explicit Module(StringRef ModuleID, LLVMContext& C);
244   /// The module destructor. This will dropAllReferences.
245   ~Module();
246
247 /// @}
248 /// @name Module Level Accessors
249 /// @{
250
251   /// Get the module identifier which is, essentially, the name of the module.
252   /// @returns the module identifier as a string
253   const std::string &getModuleIdentifier() const { return ModuleID; }
254
255   /// Get the data layout string for the module's target platform. This is
256   /// equivalent to getDataLayout()->getStringRepresentation().
257   const std::string &getDataLayoutStr() const { return DataLayoutStr; }
258
259   /// Get the data layout for the module's target platform.
260   const DataLayout *getDataLayout() const;
261
262   /// Get the target triple which is a string describing the target host.
263   /// @returns a string containing the target triple.
264   const std::string &getTargetTriple() const { return TargetTriple; }
265
266   /// Get the global data context.
267   /// @returns LLVMContext - a container for LLVM's global information
268   LLVMContext &getContext() const { return Context; }
269
270   /// Get any module-scope inline assembly blocks.
271   /// @returns a string containing the module-scope inline assembly blocks.
272   const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
273
274   /// Get the RandomNumberGenerator for this module. The RNG can be
275   /// seeded via -rng-seed=<uint64> and is salted with the ModuleID.
276   /// The returned RNG should not be shared across threads.
277   RandomNumberGenerator &getRNG() const;
278
279 /// @}
280 /// @name Module Level Mutators
281 /// @{
282
283   /// Set the module identifier.
284   void setModuleIdentifier(StringRef ID) { ModuleID = ID; }
285
286   /// Set the data layout
287   void setDataLayout(StringRef Desc);
288   void setDataLayout(const DataLayout *Other);
289
290   /// Set the target triple.
291   void setTargetTriple(StringRef T) { TargetTriple = T; }
292
293   /// Set the module-scope inline assembly blocks.
294   void setModuleInlineAsm(StringRef Asm) {
295     GlobalScopeAsm = Asm;
296     if (!GlobalScopeAsm.empty() &&
297         GlobalScopeAsm[GlobalScopeAsm.size()-1] != '\n')
298       GlobalScopeAsm += '\n';
299   }
300
301   /// Append to the module-scope inline assembly blocks, automatically inserting
302   /// a separating newline if necessary.
303   void appendModuleInlineAsm(StringRef Asm) {
304     GlobalScopeAsm += Asm;
305     if (!GlobalScopeAsm.empty() &&
306         GlobalScopeAsm[GlobalScopeAsm.size()-1] != '\n')
307       GlobalScopeAsm += '\n';
308   }
309
310 /// @}
311 /// @name Generic Value Accessors
312 /// @{
313
314   /// Return the global value in the module with the specified name, of
315   /// arbitrary type. This method returns null if a global with the specified
316   /// name is not found.
317   GlobalValue *getNamedValue(StringRef Name) const;
318
319   /// Return a unique non-zero ID for the specified metadata kind. This ID is
320   /// uniqued across modules in the current LLVMContext.
321   unsigned getMDKindID(StringRef Name) const;
322
323   /// Populate client supplied SmallVector with the name for custom metadata IDs
324   /// registered in this LLVMContext.
325   void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
326
327   /// Return the type with the specified name, or null if there is none by that
328   /// name.
329   StructType *getTypeByName(StringRef Name) const;
330
331   std::vector<StructType *> getIdentifiedStructTypes() const;
332
333 /// @}
334 /// @name Function Accessors
335 /// @{
336
337   /// Look up the specified function in the module symbol table. Four
338   /// possibilities:
339   ///   1. If it does not exist, add a prototype for the function and return it.
340   ///   2. If it exists, and has a local linkage, the existing function is
341   ///      renamed and a new one is inserted.
342   ///   3. Otherwise, if the existing function has the correct prototype, return
343   ///      the existing function.
344   ///   4. Finally, the function exists but has the wrong prototype: return the
345   ///      function with a constantexpr cast to the right prototype.
346   Constant *getOrInsertFunction(StringRef Name, FunctionType *T,
347                                 AttributeSet AttributeList);
348
349   Constant *getOrInsertFunction(StringRef Name, FunctionType *T);
350
351   /// Look up the specified function in the module symbol table. If it does not
352   /// exist, add a prototype for the function and return it. This function
353   /// guarantees to return a constant of pointer to the specified function type
354   /// or a ConstantExpr BitCast of that type if the named function has a
355   /// different type. This version of the method takes a null terminated list of
356   /// function arguments, which makes it easier for clients to use.
357   Constant *getOrInsertFunction(StringRef Name,
358                                 AttributeSet AttributeList,
359                                 Type *RetTy, ...) LLVM_END_WITH_NULL;
360
361   /// Same as above, but without the attributes.
362   Constant *getOrInsertFunction(StringRef Name, Type *RetTy, ...)
363     LLVM_END_WITH_NULL;
364
365   /// Look up the specified function in the module symbol table. If it does not
366   /// exist, return null.
367   Function *getFunction(StringRef Name) const;
368
369 /// @}
370 /// @name Global Variable Accessors
371 /// @{
372
373   /// Look up the specified global variable in the module symbol table. If it
374   /// does not exist, return null. If AllowInternal is set to true, this
375   /// function will return types that have InternalLinkage. By default, these
376   /// types are not returned.
377   GlobalVariable *getGlobalVariable(StringRef Name) const {
378     return getGlobalVariable(Name, false);
379   }
380
381   GlobalVariable *getGlobalVariable(StringRef Name, bool AllowInternal) const {
382     return const_cast<Module *>(this)->getGlobalVariable(Name, AllowInternal);
383   }
384
385   GlobalVariable *getGlobalVariable(StringRef Name, bool AllowInternal = false);
386
387   /// Return the global variable in the module with the specified name, of
388   /// arbitrary type. This method returns null if a global with the specified
389   /// name is not found.
390   GlobalVariable *getNamedGlobal(StringRef Name) {
391     return getGlobalVariable(Name, true);
392   }
393   const GlobalVariable *getNamedGlobal(StringRef Name) const {
394     return const_cast<Module *>(this)->getNamedGlobal(Name);
395   }
396
397   /// Look up the specified global in the module symbol table.
398   ///   1. If it does not exist, add a declaration of the global and return it.
399   ///   2. Else, the global exists but has the wrong type: return the function
400   ///      with a constantexpr cast to the right type.
401   ///   3. Finally, if the existing global is the correct declaration, return
402   ///      the existing global.
403   Constant *getOrInsertGlobal(StringRef Name, Type *Ty);
404
405 /// @}
406 /// @name Global Alias Accessors
407 /// @{
408
409   /// Return the global alias in the module with the specified name, of
410   /// arbitrary type. This method returns null if a global with the specified
411   /// name is not found.
412   GlobalAlias *getNamedAlias(StringRef Name) const;
413
414 /// @}
415 /// @name Named Metadata Accessors
416 /// @{
417
418   /// Return the first NamedMDNode in the module with the specified name. This
419   /// method returns null if a NamedMDNode with the specified name is not found.
420   NamedMDNode *getNamedMetadata(const Twine &Name) const;
421
422   /// Return the named MDNode in the module with the specified name. This method
423   /// returns a new NamedMDNode if a NamedMDNode with the specified name is not
424   /// found.
425   NamedMDNode *getOrInsertNamedMetadata(StringRef Name);
426
427   /// Remove the given NamedMDNode from this module and delete it.
428   void eraseNamedMetadata(NamedMDNode *NMD);
429
430 /// @}
431 /// @name Comdat Accessors
432 /// @{
433
434   /// Return the Comdat in the module with the specified name. It is created
435   /// if it didn't already exist.
436   Comdat *getOrInsertComdat(StringRef Name);
437
438 /// @}
439 /// @name Module Flags Accessors
440 /// @{
441
442   /// Returns the module flags in the provided vector.
443   void getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const;
444
445   /// Return the corresponding value if Key appears in module flags, otherwise
446   /// return null.
447   Value *getModuleFlag(StringRef Key) const;
448
449   /// Returns the NamedMDNode in the module that represents module-level flags.
450   /// This method returns null if there are no module-level flags.
451   NamedMDNode *getModuleFlagsMetadata() const;
452
453   /// Returns the NamedMDNode in the module that represents module-level flags.
454   /// If module-level flags aren't found, it creates the named metadata that
455   /// contains them.
456   NamedMDNode *getOrInsertModuleFlagsMetadata();
457
458   /// Add a module-level flag to the module-level flags metadata. It will create
459   /// the module-level flags named metadata if it doesn't already exist.
460   void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, Value *Val);
461   void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, uint32_t Val);
462   void addModuleFlag(MDNode *Node);
463
464 /// @}
465 /// @name Materialization
466 /// @{
467
468   /// Sets the GVMaterializer to GVM. This module must not yet have a
469   /// Materializer. To reset the materializer for a module that already has one,
470   /// call MaterializeAllPermanently first. Destroying this module will destroy
471   /// its materializer without materializing any more GlobalValues. Without
472   /// destroying the Module, there is no way to detach or destroy a materializer
473   /// without materializing all the GVs it controls, to avoid leaving orphan
474   /// unmaterialized GVs.
475   void setMaterializer(GVMaterializer *GVM);
476   /// Retrieves the GVMaterializer, if any, for this Module.
477   GVMaterializer *getMaterializer() const { return Materializer.get(); }
478
479   /// Returns true if this GV was loaded from this Module's GVMaterializer and
480   /// the GVMaterializer knows how to dematerialize the GV.
481   bool isDematerializable(const GlobalValue *GV) const;
482
483   /// Make sure the GlobalValue is fully read. If the module is corrupt, this
484   /// returns true and fills in the optional string with information about the
485   /// problem. If successful, this returns false.
486   std::error_code materialize(GlobalValue *GV);
487   /// If the GlobalValue is read in, and if the GVMaterializer supports it,
488   /// release the memory for the function, and set it up to be materialized
489   /// lazily. If !isDematerializable(), this method is a no-op.
490   void Dematerialize(GlobalValue *GV);
491
492   /// Make sure all GlobalValues in this Module are fully read.
493   std::error_code materializeAll();
494
495   /// Make sure all GlobalValues in this Module are fully read and clear the
496   /// Materializer. If the module is corrupt, this DOES NOT clear the old
497   /// Materializer.
498   std::error_code materializeAllPermanently();
499
500 /// @}
501 /// @name Direct access to the globals list, functions list, and symbol table
502 /// @{
503
504   /// Get the Module's list of global variables (constant).
505   const GlobalListType   &getGlobalList() const       { return GlobalList; }
506   /// Get the Module's list of global variables.
507   GlobalListType         &getGlobalList()             { return GlobalList; }
508   static iplist<GlobalVariable> Module::*getSublistAccess(GlobalVariable*) {
509     return &Module::GlobalList;
510   }
511   /// Get the Module's list of functions (constant).
512   const FunctionListType &getFunctionList() const     { return FunctionList; }
513   /// Get the Module's list of functions.
514   FunctionListType       &getFunctionList()           { return FunctionList; }
515   static iplist<Function> Module::*getSublistAccess(Function*) {
516     return &Module::FunctionList;
517   }
518   /// Get the Module's list of aliases (constant).
519   const AliasListType    &getAliasList() const        { return AliasList; }
520   /// Get the Module's list of aliases.
521   AliasListType          &getAliasList()              { return AliasList; }
522   static iplist<GlobalAlias> Module::*getSublistAccess(GlobalAlias*) {
523     return &Module::AliasList;
524   }
525   /// Get the Module's list of named metadata (constant).
526   const NamedMDListType  &getNamedMDList() const      { return NamedMDList; }
527   /// Get the Module's list of named metadata.
528   NamedMDListType        &getNamedMDList()            { return NamedMDList; }
529   static ilist<NamedMDNode> Module::*getSublistAccess(NamedMDNode*) {
530     return &Module::NamedMDList;
531   }
532   /// Get the symbol table of global variable and function identifiers
533   const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; }
534   /// Get the Module's symbol table of global variable and function identifiers.
535   ValueSymbolTable       &getValueSymbolTable()       { return *ValSymTab; }
536   /// Get the Module's symbol table for COMDATs (constant).
537   const ComdatSymTabType &getComdatSymbolTable() const { return ComdatSymTab; }
538   /// Get the Module's symbol table for COMDATs.
539   ComdatSymTabType &getComdatSymbolTable() { return ComdatSymTab; }
540
541 /// @}
542 /// @name Global Variable Iteration
543 /// @{
544
545   global_iterator       global_begin()       { return GlobalList.begin(); }
546   const_global_iterator global_begin() const { return GlobalList.begin(); }
547   global_iterator       global_end  ()       { return GlobalList.end(); }
548   const_global_iterator global_end  () const { return GlobalList.end(); }
549   bool                  global_empty() const { return GlobalList.empty(); }
550
551   iterator_range<global_iterator> globals() {
552     return iterator_range<global_iterator>(global_begin(), global_end());
553   }
554   iterator_range<const_global_iterator> globals() const {
555     return iterator_range<const_global_iterator>(global_begin(), global_end());
556   }
557
558 /// @}
559 /// @name Function Iteration
560 /// @{
561
562   iterator                begin()       { return FunctionList.begin(); }
563   const_iterator          begin() const { return FunctionList.begin(); }
564   iterator                end  ()       { return FunctionList.end();   }
565   const_iterator          end  () const { return FunctionList.end();   }
566   reverse_iterator        rbegin()      { return FunctionList.rbegin(); }
567   const_reverse_iterator  rbegin() const{ return FunctionList.rbegin(); }
568   reverse_iterator        rend()        { return FunctionList.rend(); }
569   const_reverse_iterator  rend() const  { return FunctionList.rend(); }
570   size_t                  size() const  { return FunctionList.size(); }
571   bool                    empty() const { return FunctionList.empty(); }
572
573   iterator_range<iterator> functions() {
574     return iterator_range<iterator>(begin(), end());
575   }
576   iterator_range<const_iterator> functions() const {
577     return iterator_range<const_iterator>(begin(), end());
578   }
579
580 /// @}
581 /// @name Alias Iteration
582 /// @{
583
584   alias_iterator       alias_begin()            { return AliasList.begin(); }
585   const_alias_iterator alias_begin() const      { return AliasList.begin(); }
586   alias_iterator       alias_end  ()            { return AliasList.end();   }
587   const_alias_iterator alias_end  () const      { return AliasList.end();   }
588   size_t               alias_size () const      { return AliasList.size();  }
589   bool                 alias_empty() const      { return AliasList.empty(); }
590
591   iterator_range<alias_iterator> aliases() {
592     return iterator_range<alias_iterator>(alias_begin(), alias_end());
593   }
594   iterator_range<const_alias_iterator> aliases() const {
595     return iterator_range<const_alias_iterator>(alias_begin(), alias_end());
596   }
597
598 /// @}
599 /// @name Named Metadata Iteration
600 /// @{
601
602   named_metadata_iterator named_metadata_begin() { return NamedMDList.begin(); }
603   const_named_metadata_iterator named_metadata_begin() const {
604     return NamedMDList.begin();
605   }
606
607   named_metadata_iterator named_metadata_end() { return NamedMDList.end(); }
608   const_named_metadata_iterator named_metadata_end() const {
609     return NamedMDList.end();
610   }
611
612   size_t named_metadata_size() const { return NamedMDList.size();  }
613   bool named_metadata_empty() const { return NamedMDList.empty(); }
614
615   iterator_range<named_metadata_iterator> named_metadata() {
616     return iterator_range<named_metadata_iterator>(named_metadata_begin(),
617                                                    named_metadata_end());
618   }
619   iterator_range<const_named_metadata_iterator> named_metadata() const {
620     return iterator_range<const_named_metadata_iterator>(named_metadata_begin(),
621                                                          named_metadata_end());
622   }
623
624 /// @}
625 /// @name Utility functions for printing and dumping Module objects
626 /// @{
627
628   /// Print the module to an output stream with an optional
629   /// AssemblyAnnotationWriter.
630   void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const;
631
632   /// Dump the module to stderr (for debugging).
633   void dump() const;
634   
635   /// This function causes all the subinstructions to "let go" of all references
636   /// that they are maintaining.  This allows one to 'delete' a whole class at
637   /// a time, even though there may be circular references... first all
638   /// references are dropped, and all use counts go to zero.  Then everything
639   /// is delete'd for real.  Note that no operations are valid on an object
640   /// that has "dropped all references", except operator delete.
641   void dropAllReferences();
642
643 /// @}
644 /// @name Utility functions for querying Debug information.
645 /// @{
646
647   /// \brief Returns the Dwarf Version by checking module flags.
648   unsigned getDwarfVersion() const;
649
650 /// @}
651 /// @name Utility functions for querying and setting PIC level
652 /// @{
653
654   /// \brief Returns the PIC level (small or large model)
655   PICLevel::Level getPICLevel() const;
656
657   /// \brief Set the PIC level (small or large model)
658   void setPICLevel(PICLevel::Level PL);
659 /// @}
660 };
661
662 /// An raw_ostream inserter for modules.
663 inline raw_ostream &operator<<(raw_ostream &O, const Module &M) {
664   M.print(O, nullptr);
665   return O;
666 }
667
668 // Create wrappers for C Binding types (see CBindingWrapping.h).
669 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Module, LLVMModuleRef)
670
671 /* LLVMModuleProviderRef exists for historical reasons, but now just holds a
672  * Module.
673  */
674 inline Module *unwrap(LLVMModuleProviderRef MP) {
675   return reinterpret_cast<Module*>(MP);
676 }
677   
678 } // End llvm namespace
679
680 #endif