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