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::unique_ptr<object::IRObjectFile> IRFile;
51 std::unique_ptr<TargetMachine> _target;
52 StringSet<> _linkeropt_strings;
53 std::vector<const char *> _deplibs;
54 std::vector<const char *> _linkeropts;
55 std::vector<NameAndAttributes> _symbols;
57 // _defines and _undefines only needed to disambiguate tentative definitions
59 StringMap<NameAndAttributes> _undefines;
60 std::vector<const char*> _asm_undefines;
62 LTOModule(std::unique_ptr<object::IRObjectFile> Obj, TargetMachine *TM);
63 LTOModule(std::unique_ptr<object::IRObjectFile> Obj, TargetMachine *TM,
64 std::unique_ptr<LLVMContext> Context);
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);
73 /// Returns 'true' if the memory buffer is LLVM bitcode for the specified
75 static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
76 StringRef triplePrefix);
78 /// Create a MemoryBuffer from a memory range with an optional name.
79 static std::unique_ptr<MemoryBuffer>
80 makeBuffer(const void *mem, size_t length, StringRef name = "");
82 /// Create an LTOModule. N.B. These methods take ownership of the buffer. The
83 /// caller must have initialized the Targets, the TargetMCs, the AsmPrinters,
84 /// and the AsmParsers by calling:
86 /// InitializeAllTargets();
87 /// InitializeAllTargetMCs();
88 /// InitializeAllAsmPrinters();
89 /// InitializeAllAsmParsers();
90 static LTOModule *createFromFile(const char *path, TargetOptions options,
92 static LTOModule *createFromOpenFile(int fd, const char *path, size_t size,
93 TargetOptions options,
95 static LTOModule *createFromOpenFileSlice(int fd, const char *path,
96 size_t map_size, off_t offset,
97 TargetOptions options,
99 static LTOModule *createFromBuffer(const void *mem, size_t length,
100 TargetOptions options, std::string &errMsg,
101 StringRef path = "");
103 static LTOModule *createInLocalContext(const void *mem, size_t length,
104 TargetOptions options,
105 std::string &errMsg, StringRef path);
106 static LTOModule *createInContext(const void *mem, size_t length,
107 TargetOptions options, std::string &errMsg,
108 StringRef path, LLVMContext *Context);
110 const Module &getModule() const {
111 return const_cast<LTOModule*>(this)->getModule();
113 Module &getModule() {
114 return IRFile->getModule();
117 /// Return the Module's target triple.
118 const std::string &getTargetTriple() {
119 return getModule().getTargetTriple();
122 /// Set the Module's target triple.
123 void setTargetTriple(StringRef Triple) {
124 getModule().setTargetTriple(Triple);
127 /// Get the number of symbols
128 uint32_t getSymbolCount() {
129 return _symbols.size();
132 /// Get the attributes for a symbol at the specified index.
133 lto_symbol_attributes getSymbolAttributes(uint32_t index) {
134 if (index < _symbols.size())
135 return lto_symbol_attributes(_symbols[index].attributes);
136 return lto_symbol_attributes(0);
139 /// Get the name of the symbol at the specified index.
140 const char *getSymbolName(uint32_t index) {
141 if (index < _symbols.size())
142 return _symbols[index].name;
146 /// Get the number of dependent libraries
147 uint32_t getDependentLibraryCount() {
148 return _deplibs.size();
151 /// Get the dependent library at the specified index.
152 const char *getDependentLibrary(uint32_t index) {
153 if (index < _deplibs.size())
154 return _deplibs[index];
158 /// Get the number of linker options
159 uint32_t getLinkerOptCount() {
160 return _linkeropts.size();
163 /// Get the linker option at the specified index.
164 const char *getLinkerOpt(uint32_t index) {
165 if (index < _linkeropts.size())
166 return _linkeropts[index];
170 const std::vector<const char*> &getAsmUndefinedRefs() {
171 return _asm_undefines;
175 /// Parse metadata from the module
176 // FIXME: it only parses "Linker Options" metadata at the moment
177 void parseMetadata();
179 /// Parse the symbols from the module and model-level ASM and add them to
180 /// either the defined or undefined lists.
181 bool parseSymbols(std::string &errMsg);
183 /// Add a symbol which isn't defined just yet to a list to be resolved later.
184 void addPotentialUndefinedSymbol(const object::BasicSymbolRef &Sym,
187 /// Add a defined symbol to the list.
188 void addDefinedSymbol(const char *Name, const GlobalValue *def,
191 /// Add a data symbol as defined to the list.
192 void addDefinedDataSymbol(const object::BasicSymbolRef &Sym);
193 void addDefinedDataSymbol(const char*Name, const GlobalValue *v);
195 /// Add a function symbol as defined to the list.
196 void addDefinedFunctionSymbol(const object::BasicSymbolRef &Sym);
197 void addDefinedFunctionSymbol(const char *Name, const Function *F);
199 /// Add a global symbol from module-level ASM to the defined list.
200 void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
202 /// Add a global symbol from module-level ASM to the undefined list.
203 void addAsmGlobalSymbolUndef(const char *);
205 /// Parse i386/ppc ObjC class data structure.
206 void addObjCClass(const GlobalVariable *clgv);
208 /// Parse i386/ppc ObjC category data structure.
209 void addObjCCategory(const GlobalVariable *clgv);
211 /// Parse i386/ppc ObjC class list data structure.
212 void addObjCClassRef(const GlobalVariable *clgv);
214 /// Get string that the data pointer points to.
215 bool objcClassNameFromExpression(const Constant *c, std::string &name);
217 /// Create an LTOModule (private version).
218 static LTOModule *makeLTOModule(MemoryBufferRef Buffer, TargetOptions options,
219 std::string &errMsg, LLVMContext *Context);