66ce88d959c945fc31e3c188248b2e9d05fdca18
[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 /// 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   /// Returns 'true' if the file or memory contents is LLVM bitcode.
70   static bool isBitcodeFile(const void *mem, size_t length);
71   static bool isBitcodeFile(const char *path);
72
73   /// Returns 'true' if the file or memory contents is LLVM bitcode for the
74   /// specified triple.
75   static bool isBitcodeFileForTarget(const void *mem,
76                                      size_t length,
77                                      const char *triplePrefix);
78   static bool isBitcodeFileForTarget(const char *path,
79                                      const char *triplePrefix);
80
81   /// Create an LTOModule. N.B. These methods take ownership of the buffer. The
82   /// caller must have initialized the Targets, the TargetMCs, the AsmPrinters,
83   /// and the AsmParsers by calling:
84   ///
85   /// InitializeAllTargets();
86   /// InitializeAllTargetMCs();
87   /// InitializeAllAsmPrinters();
88   /// InitializeAllAsmParsers();
89   static LTOModule *makeLTOModule(const char* path,
90                                   llvm::TargetOptions options,
91                                   std::string &errMsg);
92   static LTOModule *makeLTOModule(int fd, const char *path,
93                                   size_t size, llvm::TargetOptions options,
94                                   std::string &errMsg);
95   static LTOModule *makeLTOModule(int fd, const char *path,
96                                   size_t map_size,
97                                   off_t offset, llvm::TargetOptions options,
98                                   std::string& errMsg);
99   static LTOModule *makeLTOModule(const void *mem, size_t length,
100                                   llvm::TargetOptions options,
101                                   std::string &errMsg,
102                                   llvm::StringRef path = "");
103
104   /// Return the Module's target triple.
105   const char *getTargetTriple() {
106     return _module->getTargetTriple().c_str();
107   }
108
109   /// Set the Module's target triple.
110   void setTargetTriple(const char *triple) {
111     _module->setTargetTriple(triple);
112   }
113
114   /// Get the number of symbols
115   uint32_t getSymbolCount() {
116     return _symbols.size();
117   }
118
119   /// Get the attributes for a symbol at the specified index.
120   lto_symbol_attributes getSymbolAttributes(uint32_t index) {
121     if (index < _symbols.size())
122       return lto_symbol_attributes(_symbols[index].attributes);
123     return lto_symbol_attributes(0);
124   }
125
126   /// Get the name of the symbol at the specified index.
127   const char *getSymbolName(uint32_t index) {
128     if (index < _symbols.size())
129       return _symbols[index].name;
130     return nullptr;
131   }
132
133   /// Get the number of dependent libraries
134   uint32_t getDependentLibraryCount() {
135     return _deplibs.size();
136   }
137
138   /// Get the dependent library at the specified index.
139   const char *getDependentLibrary(uint32_t index) {
140     if (index < _deplibs.size())
141       return _deplibs[index];
142     return nullptr;
143   }
144
145   /// Get the number of linker options
146   uint32_t getLinkerOptCount() {
147     return _linkeropts.size();
148   }
149
150   /// Get the linker option at the specified index.
151   const char *getLinkerOpt(uint32_t index) {
152     if (index < _linkeropts.size())
153       return _linkeropts[index];
154     return nullptr;
155   }
156
157   /// Return the Module.
158   llvm::Module *getLLVVMModule() { return _module.get(); }
159
160   const std::vector<const char*> &getAsmUndefinedRefs() {
161     return _asm_undefines;
162   }
163
164 private:
165   /// Parse metadata from the module
166   // FIXME: it only parses "Linker Options" metadata at the moment
167   void parseMetadata();
168
169   /// Parse the symbols from the module and model-level ASM and add them to
170   /// either the defined or undefined lists.
171   bool parseSymbols(std::string &errMsg);
172
173   /// Add a symbol which isn't defined just yet to a list to be resolved later.
174   void addPotentialUndefinedSymbol(const llvm::GlobalValue *dcl, bool isFunc);
175
176   /// Add a defined symbol to the list.
177   void addDefinedSymbol(const llvm::GlobalValue *def, bool isFunction);
178
179   /// Add a function symbol as defined to the list.
180   void addDefinedFunctionSymbol(const llvm::Function *f);
181
182   /// Add a data symbol as defined to the list.
183   void addDefinedDataSymbol(const llvm::GlobalValue *v);
184
185   /// Add global symbols from module-level ASM to the defined or undefined
186   /// lists.
187   bool addAsmGlobalSymbols(std::string &errMsg);
188
189   /// Add a global symbol from module-level ASM to the defined list.
190   void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
191
192   /// Add a global symbol from module-level ASM to the undefined list.
193   void addAsmGlobalSymbolUndef(const char *);
194
195   /// Parse i386/ppc ObjC class data structure.
196   void addObjCClass(const llvm::GlobalVariable *clgv);
197
198   /// Parse i386/ppc ObjC category data structure.
199   void addObjCCategory(const llvm::GlobalVariable *clgv);
200
201   /// Parse i386/ppc ObjC class list data structure.
202   void addObjCClassRef(const llvm::GlobalVariable *clgv);
203
204   /// Get string that the data pointer points to.
205   bool objcClassNameFromExpression(const llvm::Constant* c, std::string &name);
206
207   /// Returns 'true' if the memory buffer is for the specified target triple.
208   static bool isTargetMatch(llvm::MemoryBuffer *memBuffer,
209                             const char *triplePrefix);
210
211   /// Create an LTOModule (private version). N.B. This method takes ownership of
212   /// the buffer.
213   static LTOModule *makeLTOModule(llvm::MemoryBuffer *buffer,
214                                   llvm::TargetOptions options,
215                                   std::string &errMsg);
216
217   /// Create a MemoryBuffer from a memory range with an optional name.
218   static llvm::MemoryBuffer *makeBuffer(const void *mem, size_t length,
219                                         llvm::StringRef name = "");
220 };
221
222 #endif // LTO_MODULE_H