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/IR/Module.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCObjectFileInfo.h"
22 #include "llvm/Object/IRObjectFile.h"
23 #include "llvm/Target/TargetMachine.h"
27 // Forward references to llvm classes.
35 //===----------------------------------------------------------------------===//
36 /// C++ class which implements the opaque lto_module_t type.
40 typedef StringMap<uint8_t> StringSet;
42 struct NameAndAttributes {
46 const GlobalValue *symbol;
49 std::unique_ptr<object::IRObjectFile> IRFile;
50 std::unique_ptr<TargetMachine> _target;
51 StringSet _linkeropt_strings;
52 std::vector<const char *> _deplibs;
53 std::vector<const char *> _linkeropts;
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);
64 /// Returns 'true' if the file or memory contents is LLVM bitcode.
65 static bool isBitcodeFile(const void *mem, size_t length);
66 static bool isBitcodeFile(const char *path);
68 /// Returns 'true' if the memory buffer is LLVM bitcode for the specified
70 static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
71 StringRef triplePrefix);
73 /// Create a MemoryBuffer from a memory range with an optional name.
74 static std::unique_ptr<MemoryBuffer>
75 makeBuffer(const void *mem, size_t length, StringRef name = "");
77 /// Create an LTOModule. N.B. These methods take ownership of the buffer. The
78 /// caller must have initialized the Targets, the TargetMCs, the AsmPrinters,
79 /// and the AsmParsers by calling:
81 /// InitializeAllTargets();
82 /// InitializeAllTargetMCs();
83 /// InitializeAllAsmPrinters();
84 /// InitializeAllAsmParsers();
85 static LTOModule *createFromFile(const char *path, TargetOptions options,
87 static LTOModule *createFromOpenFile(int fd, const char *path, size_t size,
88 TargetOptions options,
90 static LTOModule *createFromOpenFileSlice(int fd, const char *path,
91 size_t map_size, off_t offset,
92 TargetOptions options,
94 static LTOModule *createFromBuffer(const void *mem, size_t length,
95 TargetOptions options, std::string &errMsg,
98 const Module &getModule() const {
99 return const_cast<LTOModule*>(this)->getModule();
101 Module &getModule() {
102 return IRFile->getModule();
105 /// Return the Module's target triple.
106 const std::string &getTargetTriple() {
107 return getModule().getTargetTriple();
110 /// Set the Module's target triple.
111 void setTargetTriple(StringRef Triple) {
112 getModule().setTargetTriple(Triple);
115 /// Get the number of symbols
116 uint32_t getSymbolCount() {
117 return _symbols.size();
120 /// Get the attributes for a symbol at the specified index.
121 lto_symbol_attributes getSymbolAttributes(uint32_t index) {
122 if (index < _symbols.size())
123 return lto_symbol_attributes(_symbols[index].attributes);
124 return lto_symbol_attributes(0);
127 /// Get the name of the symbol at the specified index.
128 const char *getSymbolName(uint32_t index) {
129 if (index < _symbols.size())
130 return _symbols[index].name;
134 /// Get the number of dependent libraries
135 uint32_t getDependentLibraryCount() {
136 return _deplibs.size();
139 /// Get the dependent library at the specified index.
140 const char *getDependentLibrary(uint32_t index) {
141 if (index < _deplibs.size())
142 return _deplibs[index];
146 /// Get the number of linker options
147 uint32_t getLinkerOptCount() {
148 return _linkeropts.size();
151 /// Get the linker option at the specified index.
152 const char *getLinkerOpt(uint32_t index) {
153 if (index < _linkeropts.size())
154 return _linkeropts[index];
158 const std::vector<const char*> &getAsmUndefinedRefs() {
159 return _asm_undefines;
163 /// Parse metadata from the module
164 // FIXME: it only parses "Linker Options" metadata at the moment
165 void parseMetadata();
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);
171 /// Add a symbol which isn't defined just yet to a list to be resolved later.
172 void addPotentialUndefinedSymbol(const object::BasicSymbolRef &Sym,
175 /// Add a defined symbol to the list.
176 void addDefinedSymbol(const char *Name, const GlobalValue *def,
179 /// Add a data symbol as defined to the list.
180 void addDefinedDataSymbol(const object::BasicSymbolRef &Sym);
181 void addDefinedDataSymbol(const char*Name, const GlobalValue *v);
183 /// Add a function symbol as defined to the list.
184 void addDefinedFunctionSymbol(const object::BasicSymbolRef &Sym);
185 void addDefinedFunctionSymbol(const char *Name, const Function *F);
187 /// Add a global symbol from module-level ASM to the defined list.
188 void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
190 /// Add a global symbol from module-level ASM to the undefined list.
191 void addAsmGlobalSymbolUndef(const char *);
193 /// Parse i386/ppc ObjC class data structure.
194 void addObjCClass(const GlobalVariable *clgv);
196 /// Parse i386/ppc ObjC category data structure.
197 void addObjCCategory(const GlobalVariable *clgv);
199 /// Parse i386/ppc ObjC class list data structure.
200 void addObjCClassRef(const GlobalVariable *clgv);
202 /// Get string that the data pointer points to.
203 bool objcClassNameFromExpression(const Constant *c, std::string &name);
205 /// Create an LTOModule (private version).
206 static LTOModule *makeLTOModule(MemoryBufferRef Buffer, TargetOptions options,
207 std::string &errMsg);