Add support for the link-time pass list to Modules.
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declarations for the Module class that is used to 
11 // maintain all the information related to a VM module.
12 //
13 // A module also maintains a GlobalValRefMap object that is used to hold all
14 // constant references to global variables in the module.  When a global
15 // variable is destroyed, it should have no entries in the GlobalValueRefMap.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_MODULE_H
20 #define LLVM_MODULE_H
21
22 #include "llvm/Function.h"
23 #include "llvm/GlobalVariable.h"
24 #include "llvm/ADT/SetVector.h"
25
26 namespace llvm {
27
28 class GlobalVariable;
29 class GlobalValueRefMap;   // Used by ConstantVals.cpp
30 class ConstantPointerRef;
31 class FunctionType;
32 class SymbolTable;
33
34 template<> struct ilist_traits<Function>
35   : public SymbolTableListTraits<Function, Module, Module> {
36   // createNode is used to create a node that marks the end of the list...
37   static Function *createNode();
38   static iplist<Function> &getList(Module *M);
39 };
40 template<> struct ilist_traits<GlobalVariable>
41   : public SymbolTableListTraits<GlobalVariable, Module, Module> {
42   // createNode is used to create a node that marks the end of the list...
43   static GlobalVariable *createNode();
44   static iplist<GlobalVariable> &getList(Module *M);
45 };
46
47 class Module {
48 public:
49   typedef iplist<GlobalVariable> GlobalListType;
50   typedef iplist<Function> FunctionListType;
51   typedef SetVector<std::string> LibraryListType;
52   typedef std::vector<std::string> PassListType;
53
54   // Global Variable iterators...
55   typedef GlobalListType::iterator                             giterator;
56   typedef GlobalListType::const_iterator                 const_giterator;
57   typedef std::reverse_iterator<giterator>             reverse_giterator;
58   typedef std::reverse_iterator<const_giterator> const_reverse_giterator;
59
60   // Function iterators...
61   typedef FunctionListType::iterator                          iterator;
62   typedef FunctionListType::const_iterator              const_iterator;
63   typedef std::reverse_iterator<iterator>             reverse_iterator;
64   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
65
66   // Library list iterators
67   typedef LibraryListType::const_iterator lib_iterator;
68
69   // Link-time Pass list iterators
70   typedef PassListType::const_iterator pass_iterator;
71
72   enum Endianness  { AnyEndianness, LittleEndian, BigEndian };
73   enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
74
75 private:
76   GlobalListType GlobalList;     // The Global Variables in the module
77   FunctionListType FunctionList; // The Functions in the module
78   LibraryListType LibraryList;   // The Libraries needed by the module
79   PassListType PassList;         // The Passes needed by the module at link time
80   SymbolTable *SymTab;           // Symbol Table for the module
81   std::string ModuleID;          // Human readable identifier for the module
82   std::string TargetTriple;      // Platform target triple Module compiled on
83
84   // These flags are probably not the right long-term way to handle this kind of
85   // target information, but it is sufficient for now.
86   Endianness  Endian;     // True if target is little endian
87   PointerSize PtrSize;    // True if target has 32-bit pointers (false = 64-bit)
88
89   // Accessor for the underlying GVRefMap... only through the Constant class...
90   friend class Constant;
91   friend class ConstantPointerRef;
92   ConstantPointerRef *getConstantPointerRef(GlobalValue *GV);
93   void destroyConstantPointerRef(ConstantPointerRef *CPR);
94
95 public:
96   Module(const std::string &ModuleID);
97   ~Module();
98
99   const std::string& getModuleIdentifier() const { return ModuleID; }
100   const std::string& getTargetTriple() const { return TargetTriple; }
101   void setTargetTriple(const std::string& T) { TargetTriple = T; }
102
103   /// Target endian information...
104   Endianness getEndianness() const { return Endian; }
105   void setEndianness(Endianness E) { Endian = E; }
106
107   /// Target Pointer Size information...
108   PointerSize getPointerSize() const { return PtrSize; }
109   void setPointerSize(PointerSize PS) { PtrSize = PS; }
110
111   //===--------------------------------------------------------------------===//
112   // Methods for easy access to the functions in the module.
113   //
114
115   /// getOrInsertFunction - Look up the specified function in the module symbol
116   /// table.  If it does not exist, add a prototype for the function and return
117   /// it.
118   Function *getOrInsertFunction(const std::string &Name, const FunctionType *T);
119
120   /// getOrInsertFunction - Look up the specified function in the module symbol
121   /// table.  If it does not exist, add a prototype for the function and return
122   /// it.  This version of the method takes a null terminated list of function
123   /// arguments, which makes it easier for clients to use.
124   Function *getOrInsertFunction(const std::string &Name, const Type *RetTy,...);
125
126   /// getFunction - Look up the specified function in the module symbol table.
127   /// If it does not exist, return null.
128   ///
129   Function *getFunction(const std::string &Name, const FunctionType *Ty);
130
131   /// getMainFunction - This function looks up main efficiently.  This is such a
132   /// common case, that it is a method in Module.  If main cannot be found, a
133   /// null pointer is returned.
134   ///
135   Function *getMainFunction();
136
137   /// getNamedFunction - Return the first function in the module with the
138   /// specified name, of arbitrary type.  This method returns null if a function
139   /// with the specified name is not found.
140   ///
141   Function *getNamedFunction(const std::string &Name);
142
143   //===--------------------------------------------------------------------===//
144   // Methods for easy access to the global variables in the module.
145   //
146
147   /// getGlobalVariable - Look up the specified global variable in the module
148   /// symbol table.  If it does not exist, return null.  Note that this only
149   /// returns a global variable if it does not have internal linkage.  The type
150   /// argument should be the underlying type of the global, ie, it should not
151   /// have the top-level PointerType, which represents the address of the
152   /// global.
153   ///
154   GlobalVariable *getGlobalVariable(const std::string &Name, const Type *Ty);
155
156
157   //===--------------------------------------------------------------------===//
158   // Methods for easy access to the types in the module.
159   //
160
161   /// addTypeName - Insert an entry in the symbol table mapping Str to Type.  If
162   /// there is already an entry for this name, true is returned and the symbol
163   /// table is not modified.
164   ///
165   bool addTypeName(const std::string &Name, const Type *Ty);
166
167   /// getTypeName - If there is at least one entry in the symbol table for the
168   /// specified type, return it.
169   ///
170   std::string getTypeName(const Type *Ty) const;
171
172   /// getTypeByName - Return the type with the specified name in this module, or
173   /// null if there is none by that name.
174   const Type *getTypeByName(const std::string &Name) const;
175
176
177   //===--------------------------------------------------------------------===//
178   // Methods for direct access to the globals list, functions list, and symbol
179   // table.
180   //
181
182   /// Get the underlying elements of the Module...
183   inline const GlobalListType &getGlobalList() const  { return GlobalList; }
184   inline       GlobalListType &getGlobalList()        { return GlobalList; }
185   inline const FunctionListType &getFunctionList() const { return FunctionList;}
186   inline       FunctionListType &getFunctionList()       { return FunctionList;}
187
188   /// getSymbolTable() - Get access to the symbol table for the module, where
189   /// global variables and functions are identified.
190   ///
191   inline       SymbolTable &getSymbolTable()       { return *SymTab; }
192   inline const SymbolTable &getSymbolTable() const { return *SymTab; }
193
194
195   //===--------------------------------------------------------------------===//
196   // Module iterator forwarding functions
197   //
198   // Globals list interface
199   inline giterator                gbegin()       { return GlobalList.begin(); }
200   inline const_giterator          gbegin() const { return GlobalList.begin(); }
201   inline giterator                gend  ()       { return GlobalList.end();   }
202   inline const_giterator          gend  () const { return GlobalList.end();   }
203
204   inline reverse_giterator       grbegin()       { return GlobalList.rbegin(); }
205   inline const_reverse_giterator grbegin() const { return GlobalList.rbegin(); }
206   inline reverse_giterator       grend  ()       { return GlobalList.rend();   }
207   inline const_reverse_giterator grend  () const { return GlobalList.rend();   }
208
209   inline unsigned                  gsize() const { return GlobalList.size(); }
210   inline bool                     gempty() const { return GlobalList.empty(); }
211   inline const GlobalVariable    &gfront() const { return GlobalList.front(); }
212   inline       GlobalVariable    &gfront()       { return GlobalList.front(); }
213   inline const GlobalVariable     &gback() const { return GlobalList.back(); }
214   inline       GlobalVariable     &gback()       { return GlobalList.back(); }
215
216   // FunctionList interface
217   inline iterator                begin()       { return FunctionList.begin(); }
218   inline const_iterator          begin() const { return FunctionList.begin(); }
219   inline iterator                end  ()       { return FunctionList.end();   }
220   inline const_iterator          end  () const { return FunctionList.end();   }
221
222   inline reverse_iterator       rbegin()       { return FunctionList.rbegin(); }
223   inline const_reverse_iterator rbegin() const { return FunctionList.rbegin(); }
224   inline reverse_iterator       rend  ()       { return FunctionList.rend();   }
225   inline const_reverse_iterator rend  () const { return FunctionList.rend();   }
226
227   inline unsigned                 size() const { return FunctionList.size(); }
228   inline bool                    empty() const { return FunctionList.empty(); }
229   inline const Function         &front() const { return FunctionList.front(); }
230   inline       Function         &front()       { return FunctionList.front(); }
231   inline const Function          &back() const { return FunctionList.back(); }
232   inline       Function          &back()       { return FunctionList.back(); }
233
234   //===--------------------------------------------------------------------===//
235   // List of dependent library access functions
236
237   /// @brief Get a constant iterator to beginning of dependent library list.
238   inline lib_iterator lib_begin() const { return LibraryList.begin(); }
239
240   /// @brief Get a constant iterator to end of dependent library list.
241   inline lib_iterator lib_end() const { return LibraryList.end(); }
242
243   /// @brief Returns the number of items in the list of libraries.
244   inline unsigned lib_size() const { return LibraryList.size(); }
245
246   /// @brief Add a library to the list of dependent libraries
247   inline void addLibrary(const std::string& Lib){ LibraryList.insert(Lib); }
248
249   /// @brief Remove a library from the list of dependent libraries
250   inline void removeLibrary(const std::string& Lib) { LibraryList.remove(Lib); }
251
252   /// @brief Get all the libraries 
253   inline const LibraryListType& getLibraries() const { return LibraryList; }
254
255   //===--------------------------------------------------------------------===//
256   // Access functions for Link-time pass list
257
258   /// @brief Get a constant iterator to beginning of pass list.
259   inline pass_iterator pass_begin() const { return PassList.begin(); }
260
261   /// @brief Get a constant iterator to end of pass list.
262   inline pass_iterator pass_end() const { return PassList.end(); }
263
264   /// @brief Returns the number of items in the list of passes.
265   inline unsigned pass_size() const { return PassList.size(); }
266
267   /// @brief Add a library to the list of passes
268   inline void addPass(const std::string& Pass){ PassList.push_back(Pass); }
269
270   /// @brief Remove a library from the list of passes
271   void removePass(const std::string& Lib); 
272
273   /// @brief Get all the passes 
274   inline const PassListType& getPasses() const { return PassList; }
275
276   //===--------------------------------------------------------------------===//
277   // Utility functions for printing and dumping Module objects
278
279   void print(std::ostream &OS) const { print(OS, 0); }
280   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
281
282   void dump() const;
283
284   /// dropAllReferences() - This function causes all the subinstructions to "let
285   /// go" of all references that they are maintaining.  This allows one to
286   /// 'delete' a whole class at a time, even though there may be circular
287   /// references... first all references are dropped, and all use counts go to
288   /// zero.  Then everything is delete'd for real.  Note that no operations are
289   /// valid on an object that has "dropped all references", except operator 
290   /// delete.
291   ///
292   void dropAllReferences();
293 };
294
295 inline std::ostream &operator<<(std::ostream &O, const Module *M) {
296   M->print(O);
297   return O;
298 }
299
300 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
301   M.print(O);
302   return O;
303 }
304
305 } // End llvm namespace
306
307 #endif