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 LLVM_LTO_LTOMODULE_H 15 #define LLVM_LTO_LTOMODULE_H 16 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/Object/IRObjectFile.h" 22 #include "llvm/Target/TargetMachine.h" 23 #include <string> 24 #include <vector> 25 26 // Forward references to llvm classes. 27 namespace llvm { 28 class Function; 29 class GlobalValue; 30 class MemoryBuffer; 31 class TargetOptions; 32 class Value; 33 34 //===----------------------------------------------------------------------===// 35 /// C++ class which implements the opaque lto_module_t type. 36 /// 37 struct LTOModule { 38 private: 39 struct NameAndAttributes { 40 const char *name; 41 uint32_t attributes; 42 bool isFunction; 43 const GlobalValue *symbol; 44 }; 45 46 std::unique_ptr<LLVMContext> OwnedContext; 47 48 std::string LinkerOpts; 49 50 std::unique_ptr<object::IRObjectFile> IRFile; 51 std::unique_ptr<TargetMachine> _target; 52 std::vector<NameAndAttributes> _symbols; 53 54 // _defines and _undefines only needed to disambiguate tentative definitions 55 StringSet<> _defines; 56 StringMap<NameAndAttributes> _undefines; 57 std::vector<const char*> _asm_undefines; 58 59 LTOModule(std::unique_ptr<object::IRObjectFile> Obj, TargetMachine *TM); 60 61 public: 62 ~LTOModule(); 63 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); 67 68 /// Returns 'true' if the Module is produced for ThinLTO. 69 bool isThinLTO(); 70 71 /// Returns 'true' if the memory buffer is LLVM bitcode for the specified 72 /// triple. 73 static bool isBitcodeForTarget(MemoryBuffer *memBuffer, 74 StringRef triplePrefix); 75 76 /// Returns a string representing the producer identification stored in the 77 /// bitcode, or "" if the bitcode does not contains any. 78 /// 79 static std::string getProducerString(MemoryBuffer *Buffer); 80 81 /// Create a MemoryBuffer from a memory range with an optional name. 82 static std::unique_ptr<MemoryBuffer> 83 makeBuffer(const void *mem, size_t length, StringRef name = ""); 84 85 /// Create an LTOModule. N.B. These methods take ownership of the buffer. The 86 /// caller must have initialized the Targets, the TargetMCs, the AsmPrinters, 87 /// and the AsmParsers by calling: 88 /// 89 /// InitializeAllTargets(); 90 /// InitializeAllTargetMCs(); 91 /// InitializeAllAsmPrinters(); 92 /// InitializeAllAsmParsers(); 93 static ErrorOr<std::unique_ptr<LTOModule>> 94 createFromFile(LLVMContext &Context, const char *path, 95 const TargetOptions &options); 96 static ErrorOr<std::unique_ptr<LTOModule>> 97 createFromOpenFile(LLVMContext &Context, int fd, const char *path, 98 size_t size, const TargetOptions &options); 99 static ErrorOr<std::unique_ptr<LTOModule>> 100 createFromOpenFileSlice(LLVMContext &Context, int fd, const char *path, 101 size_t map_size, off_t offset, 102 const TargetOptions &options); 103 static ErrorOr<std::unique_ptr<LTOModule>> 104 createFromBuffer(LLVMContext &Context, const void *mem, size_t length, 105 const TargetOptions &options, StringRef path = ""); 106 static ErrorOr<std::unique_ptr<LTOModule>> 107 createInLocalContext(std::unique_ptr<LLVMContext> Context, const void *mem, 108 size_t length, const TargetOptions &options, 109 StringRef path); 110 111 const Module &getModule() const { 112 return const_cast<LTOModule*>(this)->getModule(); 113 } 114 Module &getModule() { 115 return IRFile->getModule(); 116 } 117 118 std::unique_ptr<Module> takeModule() { return IRFile->takeModule(); } 119 120 /// Return the Module's target triple. 121 const std::string &getTargetTriple() { 122 return getModule().getTargetTriple(); 123 } 124 125 /// Set the Module's target triple. 126 void setTargetTriple(StringRef Triple) { 127 getModule().setTargetTriple(Triple); 128 } 129 130 /// Get the number of symbols 131 uint32_t getSymbolCount() { 132 return _symbols.size(); 133 } 134 135 /// Get the attributes for a symbol at the specified index. 136 lto_symbol_attributes getSymbolAttributes(uint32_t index) { 137 if (index < _symbols.size()) 138 return lto_symbol_attributes(_symbols[index].attributes); 139 return lto_symbol_attributes(0); 140 } 141 142 /// Get the name of the symbol at the specified index. 143 const char *getSymbolName(uint32_t index) { 144 if (index < _symbols.size()) 145 return _symbols[index].name; 146 return nullptr; 147 } 148 149 const GlobalValue *getSymbolGV(uint32_t index) { 150 if (index < _symbols.size()) 151 return _symbols[index].symbol; 152 return nullptr; 153 } 154 155 const char *getLinkerOpts() { 156 return LinkerOpts.c_str(); 157 } 158 159 const std::vector<const char*> &getAsmUndefinedRefs() { 160 return _asm_undefines; 161 } 162 163 private: 164 /// Parse metadata from the module 165 // FIXME: it only parses "Linker Options" metadata at the moment 166 void parseMetadata(); 167 168 /// Parse the symbols from the module and model-level ASM and add them to 169 /// either the defined or undefined lists. 170 void parseSymbols(); 171 172 /// Add a symbol which isn't defined just yet to a list to be resolved later. 173 void addPotentialUndefinedSymbol(const object::BasicSymbolRef &Sym, 174 bool isFunc); 175 176 /// Add a defined symbol to the list. 177 void addDefinedSymbol(const char *Name, const GlobalValue *def, 178 bool isFunction); 179 180 /// Add a data symbol as defined to the list. 181 void addDefinedDataSymbol(const object::BasicSymbolRef &Sym); 182 void addDefinedDataSymbol(const char*Name, const GlobalValue *v); 183 184 /// Add a function symbol as defined to the list. 185 void addDefinedFunctionSymbol(const object::BasicSymbolRef &Sym); 186 void addDefinedFunctionSymbol(const char *Name, const Function *F); 187 188 /// Add a global symbol from module-level ASM to the defined list. 189 void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope); 190 191 /// Add a global symbol from module-level ASM to the undefined list. 192 void addAsmGlobalSymbolUndef(const char *); 193 194 /// Parse i386/ppc ObjC class data structure. 195 void addObjCClass(const GlobalVariable *clgv); 196 197 /// Parse i386/ppc ObjC category data structure. 198 void addObjCCategory(const GlobalVariable *clgv); 199 200 /// Parse i386/ppc ObjC class list data structure. 201 void addObjCClassRef(const GlobalVariable *clgv); 202 203 /// Get string that the data pointer points to. 204 bool objcClassNameFromExpression(const Constant *c, std::string &name); 205 206 /// Create an LTOModule (private version). 207 static ErrorOr<std::unique_ptr<LTOModule>> 208 makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options, 209 LLVMContext &Context, bool ShouldBeLazy); 210 }; 211 } 212 #endif 213