Constify the Optnone checks in IR passes.
[oota-llvm.git] / include / llvm / LTO / LTOModule.h
1 //===-LTOModule.h - LLVM Link Time Optimizer ------------------------------===//
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 // This file declares the LTOModule class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LTO_MODULE_H
15 #define LTO_MODULE_H
16
17 #include "llvm-c/lto.h"
18 #include "llvm/ADT/OwningPtr.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/IR/Mangler.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCObjectFileInfo.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include <string>
26 #include <vector>
27
28 // Forward references to llvm classes.
29 namespace llvm {
30   class Function;
31   class GlobalValue;
32   class MemoryBuffer;
33   class TargetOptions;
34   class Value;
35 }
36
37 //===----------------------------------------------------------------------===//
38 /// LTOModule - C++ class which implements the opaque lto_module_t type.
39 ///
40 struct LTOModule {
41 private:
42   typedef llvm::StringMap<uint8_t> StringSet;
43
44   struct NameAndAttributes {
45     const char        *name;
46     uint32_t           attributes;
47     bool               isFunction;
48     const llvm::GlobalValue *symbol;
49   };
50
51   llvm::OwningPtr<llvm::Module>           _module;
52   llvm::OwningPtr<llvm::TargetMachine>    _target;
53   llvm::MCObjectFileInfo ObjFileInfo;
54   StringSet                               _linkeropt_strings;
55   std::vector<const char *>               _deplibs;
56   std::vector<const char *>               _linkeropts;
57   std::vector<NameAndAttributes>          _symbols;
58
59   // _defines and _undefines only needed to disambiguate tentative definitions
60   StringSet                               _defines;
61   llvm::StringMap<NameAndAttributes>      _undefines;
62   std::vector<const char*>                _asm_undefines;
63   llvm::MCContext                         _context;
64
65   // Use mangler to add GlobalPrefix to names to match linker names.
66   llvm::Mangler                           _mangler;
67
68   LTOModule(llvm::Module *m, llvm::TargetMachine *t);
69 public:
70   /// isBitcodeFile - Returns 'true' if the file or memory contents is LLVM
71   /// bitcode.
72   static bool isBitcodeFile(const void *mem, size_t length);
73   static bool isBitcodeFile(const char *path);
74
75   /// isBitcodeFileForTarget - Returns 'true' if the file or memory contents
76   /// is LLVM bitcode for the specified triple.
77   static bool isBitcodeFileForTarget(const void *mem,
78                                      size_t length,
79                                      const char *triplePrefix);
80   static bool isBitcodeFileForTarget(const char *path,
81                                      const char *triplePrefix);
82
83   /// makeLTOModule - Create an LTOModule. N.B. These methods take ownership
84   /// of the buffer. The caller must have initialized the Targets, the
85   /// TargetMCs, the AsmPrinters, and the AsmParsers by calling:
86   ///
87   /// InitializeAllTargets();
88   /// InitializeAllTargetMCs();
89   /// InitializeAllAsmPrinters();
90   /// InitializeAllAsmParsers();
91   static LTOModule *makeLTOModule(const char* path,
92                                   llvm::TargetOptions options,
93                                   std::string &errMsg);
94   static LTOModule *makeLTOModule(int fd, const char *path,
95                                   size_t size, llvm::TargetOptions options,
96                                   std::string &errMsg);
97   static LTOModule *makeLTOModule(int fd, const char *path,
98                                   size_t map_size,
99                                   off_t offset, llvm::TargetOptions options,
100                                   std::string& errMsg);
101   static LTOModule *makeLTOModule(const void *mem, size_t length,
102                                   llvm::TargetOptions options,
103                                   std::string &errMsg,
104                                   llvm::StringRef path = "");
105
106   /// getTargetTriple - Return the Module's target triple.
107   const char *getTargetTriple() {
108     return _module->getTargetTriple().c_str();
109   }
110
111   /// setTargetTriple - Set the Module's target triple.
112   void setTargetTriple(const char *triple) {
113     _module->setTargetTriple(triple);
114   }
115
116   /// getSymbolCount - Get the number of symbols
117   uint32_t getSymbolCount() {
118     return _symbols.size();
119   }
120
121   /// getSymbolAttributes - Get the attributes for a symbol at the specified
122   /// index.
123   lto_symbol_attributes getSymbolAttributes(uint32_t index) {
124     if (index < _symbols.size())
125       return lto_symbol_attributes(_symbols[index].attributes);
126     return lto_symbol_attributes(0);
127   }
128
129   /// getSymbolName - Get the name of the symbol at the specified index.
130   const char *getSymbolName(uint32_t index) {
131     if (index < _symbols.size())
132       return _symbols[index].name;
133     return NULL;
134   }
135
136   /// getDependentLibraryCount - Get the number of dependent libraries
137   uint32_t getDependentLibraryCount() {
138     return _deplibs.size();
139   }
140
141   /// getDependentLibrary - Get the dependent library at the specified index.
142   const char *getDependentLibrary(uint32_t index) {
143     if (index < _deplibs.size())
144       return _deplibs[index];
145     return NULL;
146   }
147
148   /// getLinkerOptCount - Get the number of linker options
149   uint32_t getLinkerOptCount() {
150     return _linkeropts.size();
151   }
152
153   /// getLinkerOpt - Get the linker option at the specified index.
154   const char *getLinkerOpt(uint32_t index) {
155     if (index < _linkeropts.size())
156       return _linkeropts[index];
157     return NULL;
158   }
159
160   /// getLLVVMModule - Return the Module.
161   llvm::Module *getLLVVMModule() { return _module.get(); }
162
163   /// getAsmUndefinedRefs -
164   const std::vector<const char*> &getAsmUndefinedRefs() {
165     return _asm_undefines;
166   }
167
168 private:
169   /// parseMetadata - Parse metadata from the module
170   // FIXME: it only parses "Linker Options" metadata at the moment
171   void parseMetadata();
172
173   /// parseSymbols - Parse the symbols from the module and model-level ASM and
174   /// add them to either the defined or undefined lists.
175   bool parseSymbols(std::string &errMsg);
176
177   /// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet
178   /// to a list to be resolved later.
179   void addPotentialUndefinedSymbol(const llvm::GlobalValue *dcl, bool isFunc);
180
181   /// addDefinedSymbol - Add a defined symbol to the list.
182   void addDefinedSymbol(const llvm::GlobalValue *def, bool isFunction);
183
184   /// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
185   void addDefinedFunctionSymbol(const llvm::Function *f);
186
187   /// addDefinedDataSymbol - Add a data symbol as defined to the list.
188   void addDefinedDataSymbol(const llvm::GlobalValue *v);
189
190   /// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
191   /// defined or undefined lists.
192   bool addAsmGlobalSymbols(std::string &errMsg);
193
194   /// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
195   /// defined list.
196   void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
197
198   /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to
199   /// the undefined list.
200   void addAsmGlobalSymbolUndef(const char *);
201
202   /// addObjCClass - Parse i386/ppc ObjC class data structure.
203   void addObjCClass(const llvm::GlobalVariable *clgv);
204
205   /// addObjCCategory - Parse i386/ppc ObjC category data structure.
206   void addObjCCategory(const llvm::GlobalVariable *clgv);
207
208   /// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
209   void addObjCClassRef(const llvm::GlobalVariable *clgv);
210
211   /// objcClassNameFromExpression - Get string that the data pointer points
212   /// to.
213   bool objcClassNameFromExpression(const llvm::Constant* c, std::string &name);
214
215   /// isTargetMatch - Returns 'true' if the memory buffer is for the specified
216   /// target triple.
217   static bool isTargetMatch(llvm::MemoryBuffer *memBuffer,
218                             const char *triplePrefix);
219
220   /// makeLTOModule - Create an LTOModule (private version). N.B. This
221   /// method takes ownership of the buffer.
222   static LTOModule *makeLTOModule(llvm::MemoryBuffer *buffer,
223                                   llvm::TargetOptions options,
224                                   std::string &errMsg);
225
226   /// Create a MemoryBuffer from a memory range with an optional name.
227   static llvm::MemoryBuffer *makeBuffer(const void *mem, size_t length,
228                                         llvm::StringRef name = "");
229 };
230
231 #endif // LTO_MODULE_H