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 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LTO_LTOMODULE_H
15 #define LLVM_LTO_LTOMODULE_H
17 #include "llvm-c/lto.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/StringSet.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCObjectFileInfo.h"
23 #include "llvm/Object/IRObjectFile.h"
24 #include "llvm/Target/TargetMachine.h"
28 // Forward references to llvm classes.
36 //===----------------------------------------------------------------------===//
37 /// C++ class which implements the opaque lto_module_t type.
41 struct NameAndAttributes {
45 const GlobalValue *symbol;
48 std::unique_ptr<LLVMContext> OwnedContext;
50 std::string LinkerOpts;
52 std::unique_ptr<object::IRObjectFile> IRFile;
53 std::unique_ptr<TargetMachine> _target;
54 std::vector<NameAndAttributes> _symbols;
56 // _defines and _undefines only needed to disambiguate tentative definitions
58 StringMap<NameAndAttributes> _undefines;
59 std::vector<const char*> _asm_undefines;
61 LTOModule(std::unique_ptr<object::IRObjectFile> Obj, TargetMachine *TM);
62 LTOModule(std::unique_ptr<object::IRObjectFile> Obj, TargetMachine *TM,
63 std::unique_ptr<LLVMContext> Context);
68 /// Returns 'true' if the file or memory contents is LLVM bitcode.
69 static bool isBitcodeFile(const void *mem, size_t length);
70 static bool isBitcodeFile(const char *path);
72 /// Returns 'true' if the memory buffer is LLVM bitcode for the specified
74 static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
75 StringRef triplePrefix);
77 /// Create a MemoryBuffer from a memory range with an optional name.
78 static std::unique_ptr<MemoryBuffer>
79 makeBuffer(const void *mem, size_t length, StringRef name = "");
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:
85 /// InitializeAllTargets();
86 /// InitializeAllTargetMCs();
87 /// InitializeAllAsmPrinters();
88 /// InitializeAllAsmParsers();
89 static LTOModule *createFromFile(const char *path, TargetOptions options,
91 static LTOModule *createFromOpenFile(int fd, const char *path, size_t size,
92 TargetOptions options,
94 static LTOModule *createFromOpenFileSlice(int fd, const char *path,
95 size_t map_size, off_t offset,
96 TargetOptions options,
98 static LTOModule *createFromBuffer(const void *mem, size_t length,
99 TargetOptions options, std::string &errMsg,
100 StringRef path = "");
102 static LTOModule *createInLocalContext(const void *mem, size_t length,
103 TargetOptions options,
104 std::string &errMsg, StringRef path);
105 static LTOModule *createInContext(const void *mem, size_t length,
106 TargetOptions options, std::string &errMsg,
107 StringRef path, LLVMContext *Context);
109 const Module &getModule() const {
110 return const_cast<LTOModule*>(this)->getModule();
112 Module &getModule() {
113 return IRFile->getModule();
116 std::unique_ptr<Module> takeModule() { return IRFile->takeModule(); }
118 /// Return the Module's target triple.
119 const std::string &getTargetTriple() {
120 return getModule().getTargetTriple();
123 /// Set the Module's target triple.
124 void setTargetTriple(StringRef Triple) {
125 getModule().setTargetTriple(Triple);
128 /// Get the number of symbols
129 uint32_t getSymbolCount() {
130 return _symbols.size();
133 /// Get the attributes for a symbol at the specified index.
134 lto_symbol_attributes getSymbolAttributes(uint32_t index) {
135 if (index < _symbols.size())
136 return lto_symbol_attributes(_symbols[index].attributes);
137 return lto_symbol_attributes(0);
140 /// Get the name of the symbol at the specified index.
141 const char *getSymbolName(uint32_t index) {
142 if (index < _symbols.size())
143 return _symbols[index].name;
147 const GlobalValue *getSymbolGV(uint32_t index) {
148 if (index < _symbols.size())
149 return _symbols[index].symbol;
153 const char *getLinkerOpts() {
154 return LinkerOpts.c_str();
157 const std::vector<const char*> &getAsmUndefinedRefs() {
158 return _asm_undefines;
162 /// Parse metadata from the module
163 // FIXME: it only parses "Linker Options" metadata at the moment
164 void parseMetadata();
166 /// Parse the symbols from the module and model-level ASM and add them to
167 /// either the defined or undefined lists.
168 bool parseSymbols(std::string &errMsg);
170 /// Add a symbol which isn't defined just yet to a list to be resolved later.
171 void addPotentialUndefinedSymbol(const object::BasicSymbolRef &Sym,
174 /// Add a defined symbol to the list.
175 void addDefinedSymbol(const char *Name, const GlobalValue *def,
178 /// Add a data symbol as defined to the list.
179 void addDefinedDataSymbol(const object::BasicSymbolRef &Sym);
180 void addDefinedDataSymbol(const char*Name, const GlobalValue *v);
182 /// Add a function symbol as defined to the list.
183 void addDefinedFunctionSymbol(const object::BasicSymbolRef &Sym);
184 void addDefinedFunctionSymbol(const char *Name, const Function *F);
186 /// Add a global symbol from module-level ASM to the defined list.
187 void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
189 /// Add a global symbol from module-level ASM to the undefined list.
190 void addAsmGlobalSymbolUndef(const char *);
192 /// Parse i386/ppc ObjC class data structure.
193 void addObjCClass(const GlobalVariable *clgv);
195 /// Parse i386/ppc ObjC category data structure.
196 void addObjCCategory(const GlobalVariable *clgv);
198 /// Parse i386/ppc ObjC class list data structure.
199 void addObjCClassRef(const GlobalVariable *clgv);
201 /// Get string that the data pointer points to.
202 bool objcClassNameFromExpression(const Constant *c, std::string &name);
204 /// Create an LTOModule (private version).
205 static LTOModule *makeLTOModule(MemoryBufferRef Buffer, TargetOptions options,
206 std::string &errMsg, LLVMContext *Context);