1 //===-LTOModule.h - LLVM Link Time Optimizer ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the LTOModule class.
12 //===----------------------------------------------------------------------===//
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"
27 // Forward references to llvm classes.
36 //===----------------------------------------------------------------------===//
37 /// LTOModule - C++ class which implements the opaque lto_module_t type.
41 typedef llvm::StringMap<uint8_t> StringSet;
43 struct NameAndAttributes {
47 const llvm::GlobalValue *symbol;
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;
58 // _defines and _undefines only needed to disambiguate tentative definitions
60 llvm::StringMap<NameAndAttributes> _undefines;
61 std::vector<const char*> _asm_undefines;
62 llvm::MCContext _context;
64 // Use mangler to add GlobalPrefix to names to match linker names.
65 llvm::Mangler _mangler;
67 LTOModule(llvm::Module *m, llvm::TargetMachine *t);
69 /// isBitcodeFile - Returns 'true' if the file or memory contents is LLVM
71 static bool isBitcodeFile(const void *mem, size_t length);
72 static bool isBitcodeFile(const char *path);
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,
78 const char *triplePrefix);
79 static bool isBitcodeFileForTarget(const char *path,
80 const char *triplePrefix);
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:
86 /// InitializeAllTargets();
87 /// InitializeAllTargetMCs();
88 /// InitializeAllAsmPrinters();
89 /// InitializeAllAsmParsers();
90 static LTOModule *makeLTOModule(const char* path,
91 llvm::TargetOptions options,
93 static LTOModule *makeLTOModule(int fd, const char *path,
94 size_t size, llvm::TargetOptions options,
96 static LTOModule *makeLTOModule(int fd, const char *path,
98 off_t offset, llvm::TargetOptions options,
100 static LTOModule *makeLTOModule(const void *mem, size_t length,
101 llvm::TargetOptions options,
103 llvm::StringRef path = "");
105 /// getTargetTriple - Return the Module's target triple.
106 const char *getTargetTriple() {
107 return _module->getTargetTriple().c_str();
110 /// setTargetTriple - Set the Module's target triple.
111 void setTargetTriple(const char *triple) {
112 _module->setTargetTriple(triple);
115 /// getSymbolCount - Get the number of symbols
116 uint32_t getSymbolCount() {
117 return _symbols.size();
120 /// getSymbolAttributes - Get the attributes for a symbol at the specified
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);
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;
135 /// getDependentLibraryCount - Get the number of dependent libraries
136 uint32_t getDependentLibraryCount() {
137 return _deplibs.size();
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];
147 /// getLinkerOptCount - Get the number of linker options
148 uint32_t getLinkerOptCount() {
149 return _linkeropts.size();
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];
159 /// getLLVVMModule - Return the Module.
160 llvm::Module *getLLVVMModule() { return _module.get(); }
162 /// getAsmUndefinedRefs -
163 const std::vector<const char*> &getAsmUndefinedRefs() {
164 return _asm_undefines;
168 /// parseMetadata - Parse metadata from the module
169 // FIXME: it only parses "Linker Options" metadata at the moment
170 void parseMetadata();
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);
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);
180 /// addDefinedSymbol - Add a defined symbol to the list.
181 void addDefinedSymbol(const llvm::GlobalValue *def, bool isFunction);
183 /// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
184 void addDefinedFunctionSymbol(const llvm::Function *f);
186 /// addDefinedDataSymbol - Add a data symbol as defined to the list.
187 void addDefinedDataSymbol(const llvm::GlobalValue *v);
189 /// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
190 /// defined or undefined lists.
191 bool addAsmGlobalSymbols(std::string &errMsg);
193 /// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
195 void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
197 /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to
198 /// the undefined list.
199 void addAsmGlobalSymbolUndef(const char *);
201 /// addObjCClass - Parse i386/ppc ObjC class data structure.
202 void addObjCClass(const llvm::GlobalVariable *clgv);
204 /// addObjCCategory - Parse i386/ppc ObjC category data structure.
205 void addObjCCategory(const llvm::GlobalVariable *clgv);
207 /// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
208 void addObjCClassRef(const llvm::GlobalVariable *clgv);
210 /// objcClassNameFromExpression - Get string that the data pointer points
212 bool objcClassNameFromExpression(const llvm::Constant* c, std::string &name);
214 /// isTargetMatch - Returns 'true' if the memory buffer is for the specified
216 static bool isTargetMatch(llvm::MemoryBuffer *memBuffer,
217 const char *triplePrefix);
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);
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 = "");
230 #endif // LTO_MODULE_H