LTO: rename the various makeLTOModule overloads.
[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 /// C++ class which implements the opaque lto_module_t type.
37 ///
38 struct LTOModule {
39 private:
40   typedef StringMap<uint8_t> StringSet;
41
42   struct NameAndAttributes {
43     const char        *name;
44     uint32_t           attributes;
45     bool               isFunction;
46     const GlobalValue *symbol;
47   };
48
49   std::unique_ptr<Module> _module;
50   std::unique_ptr<TargetMachine> _target;
51   MCObjectFileInfo ObjFileInfo;
52   StringSet                               _linkeropt_strings;
53   std::vector<const char *>               _deplibs;
54   std::vector<const char *>               _linkeropts;
55   std::vector<NameAndAttributes>          _symbols;
56
57   // _defines and _undefines only needed to disambiguate tentative definitions
58   StringSet                               _defines;
59   StringMap<NameAndAttributes> _undefines;
60   std::vector<const char*>                _asm_undefines;
61   MCContext _context;
62
63   // Use mangler to add GlobalPrefix to names to match linker names.
64   Mangler _mangler;
65
66   LTOModule(std::unique_ptr<Module> M, TargetMachine *TM);
67
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 *createFromFile(const char *path, TargetOptions options,
90                                    std::string &errMsg);
91   static LTOModule *createFromOpenFile(int fd, const char *path, size_t size,
92                                        TargetOptions options,
93                                        std::string &errMsg);
94   static LTOModule *createFromOpenFileSlice(int fd, const char *path,
95                                             size_t map_size, off_t offset,
96                                             TargetOptions options,
97                                             std::string &errMsg);
98   static LTOModule *createFromBuffer(const void *mem, size_t length,
99                                      TargetOptions options, std::string &errMsg,
100                                      StringRef path = "");
101
102   /// Return the Module's target triple.
103   const char *getTargetTriple() {
104     return _module->getTargetTriple().c_str();
105   }
106
107   /// Set the Module's target triple.
108   void setTargetTriple(const char *triple) {
109     _module->setTargetTriple(triple);
110   }
111
112   /// Get the number of symbols
113   uint32_t getSymbolCount() {
114     return _symbols.size();
115   }
116
117   /// Get the attributes for a symbol at the specified index.
118   lto_symbol_attributes getSymbolAttributes(uint32_t index) {
119     if (index < _symbols.size())
120       return lto_symbol_attributes(_symbols[index].attributes);
121     return lto_symbol_attributes(0);
122   }
123
124   /// Get the name of the symbol at the specified index.
125   const char *getSymbolName(uint32_t index) {
126     if (index < _symbols.size())
127       return _symbols[index].name;
128     return nullptr;
129   }
130
131   /// Get the number of dependent libraries
132   uint32_t getDependentLibraryCount() {
133     return _deplibs.size();
134   }
135
136   /// Get the dependent library at the specified index.
137   const char *getDependentLibrary(uint32_t index) {
138     if (index < _deplibs.size())
139       return _deplibs[index];
140     return nullptr;
141   }
142
143   /// Get the number of linker options
144   uint32_t getLinkerOptCount() {
145     return _linkeropts.size();
146   }
147
148   /// Get the linker option at the specified index.
149   const char *getLinkerOpt(uint32_t index) {
150     if (index < _linkeropts.size())
151       return _linkeropts[index];
152     return nullptr;
153   }
154
155   /// Return the Module.
156   Module *getLLVVMModule() { return _module.get(); }
157
158   const std::vector<const char*> &getAsmUndefinedRefs() {
159     return _asm_undefines;
160   }
161
162 private:
163   /// Parse metadata from the module
164   // FIXME: it only parses "Linker Options" metadata at the moment
165   void parseMetadata();
166
167   /// Parse the symbols from the module and model-level ASM and add them to
168   /// either the defined or undefined lists.
169   bool parseSymbols(std::string &errMsg);
170
171   /// Add a symbol which isn't defined just yet to a list to be resolved later.
172   void addPotentialUndefinedSymbol(const GlobalValue *dcl, bool isFunc);
173
174   /// Add a defined symbol to the list.
175   void addDefinedSymbol(const GlobalValue *def, bool isFunction);
176
177   /// Add a function symbol as defined to the list.
178   void addDefinedFunctionSymbol(const Function *f);
179
180   /// Add a data symbol as defined to the list.
181   void addDefinedDataSymbol(const GlobalValue *v);
182
183   /// Add global symbols from module-level ASM to the defined or undefined
184   /// lists.
185   bool addAsmGlobalSymbols(std::string &errMsg);
186
187   /// Add a global symbol from module-level ASM to the defined list.
188   void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
189
190   /// Add a global symbol from module-level ASM to the undefined list.
191   void addAsmGlobalSymbolUndef(const char *);
192
193   /// Parse i386/ppc ObjC class data structure.
194   void addObjCClass(const GlobalVariable *clgv);
195
196   /// Parse i386/ppc ObjC category data structure.
197   void addObjCCategory(const GlobalVariable *clgv);
198
199   /// Parse i386/ppc ObjC class list data structure.
200   void addObjCClassRef(const GlobalVariable *clgv);
201
202   /// Get string that the data pointer points to.
203   bool objcClassNameFromExpression(const Constant *c, std::string &name);
204
205   /// Returns 'true' if the memory buffer is for the specified target triple.
206   static bool isTargetMatch(MemoryBuffer *memBuffer, const char *triplePrefix);
207
208   /// Create an LTOModule (private version). N.B. This method takes ownership of
209   /// the buffer.
210   static LTOModule *makeLTOModule(std::unique_ptr<MemoryBuffer> Buffer,
211                                   TargetOptions options, std::string &errMsg);
212
213   /// Create a MemoryBuffer from a memory range with an optional name.
214   static MemoryBuffer *makeBuffer(const void *mem, size_t length,
215                                   StringRef name = "");
216 };
217 }
218 #endif // LTO_MODULE_H