LLVM API Documentation
00001 //===-LTOModule.h - LLVM Link Time Optimizer ------------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file declares the LTOModule class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_LTO_LTOMODULE_H 00015 #define LLVM_LTO_LTOMODULE_H 00016 00017 #include "llvm-c/lto.h" 00018 #include "llvm/ADT/StringMap.h" 00019 #include "llvm/IR/Module.h" 00020 #include "llvm/MC/MCContext.h" 00021 #include "llvm/MC/MCObjectFileInfo.h" 00022 #include "llvm/Object/IRObjectFile.h" 00023 #include "llvm/Target/TargetMachine.h" 00024 #include <string> 00025 #include <vector> 00026 00027 // Forward references to llvm classes. 00028 namespace llvm { 00029 class Function; 00030 class GlobalValue; 00031 class MemoryBuffer; 00032 class TargetOptions; 00033 class Value; 00034 00035 //===----------------------------------------------------------------------===// 00036 /// C++ class which implements the opaque lto_module_t type. 00037 /// 00038 struct LTOModule { 00039 private: 00040 typedef StringMap<uint8_t> StringSet; 00041 00042 struct NameAndAttributes { 00043 const char *name; 00044 uint32_t attributes; 00045 bool isFunction; 00046 const GlobalValue *symbol; 00047 }; 00048 00049 std::unique_ptr<object::IRObjectFile> IRFile; 00050 std::unique_ptr<TargetMachine> _target; 00051 StringSet _linkeropt_strings; 00052 std::vector<const char *> _deplibs; 00053 std::vector<const char *> _linkeropts; 00054 std::vector<NameAndAttributes> _symbols; 00055 00056 // _defines and _undefines only needed to disambiguate tentative definitions 00057 StringSet _defines; 00058 StringMap<NameAndAttributes> _undefines; 00059 std::vector<const char*> _asm_undefines; 00060 00061 LTOModule(std::unique_ptr<object::IRObjectFile> Obj, TargetMachine *TM); 00062 00063 public: 00064 /// Returns 'true' if the file or memory contents is LLVM bitcode. 00065 static bool isBitcodeFile(const void *mem, size_t length); 00066 static bool isBitcodeFile(const char *path); 00067 00068 /// Returns 'true' if the memory buffer is LLVM bitcode for the specified 00069 /// triple. 00070 static bool isBitcodeForTarget(MemoryBuffer *memBuffer, 00071 StringRef triplePrefix); 00072 00073 /// Create a MemoryBuffer from a memory range with an optional name. 00074 static std::unique_ptr<MemoryBuffer> 00075 makeBuffer(const void *mem, size_t length, StringRef name = ""); 00076 00077 /// Create an LTOModule. N.B. These methods take ownership of the buffer. The 00078 /// caller must have initialized the Targets, the TargetMCs, the AsmPrinters, 00079 /// and the AsmParsers by calling: 00080 /// 00081 /// InitializeAllTargets(); 00082 /// InitializeAllTargetMCs(); 00083 /// InitializeAllAsmPrinters(); 00084 /// InitializeAllAsmParsers(); 00085 static LTOModule *createFromFile(const char *path, TargetOptions options, 00086 std::string &errMsg); 00087 static LTOModule *createFromOpenFile(int fd, const char *path, size_t size, 00088 TargetOptions options, 00089 std::string &errMsg); 00090 static LTOModule *createFromOpenFileSlice(int fd, const char *path, 00091 size_t map_size, off_t offset, 00092 TargetOptions options, 00093 std::string &errMsg); 00094 static LTOModule *createFromBuffer(const void *mem, size_t length, 00095 TargetOptions options, std::string &errMsg, 00096 StringRef path = ""); 00097 00098 const Module &getModule() const { 00099 return const_cast<LTOModule*>(this)->getModule(); 00100 } 00101 Module &getModule() { 00102 return IRFile->getModule(); 00103 } 00104 00105 /// Return the Module's target triple. 00106 const std::string &getTargetTriple() { 00107 return getModule().getTargetTriple(); 00108 } 00109 00110 /// Set the Module's target triple. 00111 void setTargetTriple(StringRef Triple) { 00112 getModule().setTargetTriple(Triple); 00113 } 00114 00115 /// Get the number of symbols 00116 uint32_t getSymbolCount() { 00117 return _symbols.size(); 00118 } 00119 00120 /// Get the attributes for a symbol at the specified index. 00121 lto_symbol_attributes getSymbolAttributes(uint32_t index) { 00122 if (index < _symbols.size()) 00123 return lto_symbol_attributes(_symbols[index].attributes); 00124 return lto_symbol_attributes(0); 00125 } 00126 00127 /// Get the name of the symbol at the specified index. 00128 const char *getSymbolName(uint32_t index) { 00129 if (index < _symbols.size()) 00130 return _symbols[index].name; 00131 return nullptr; 00132 } 00133 00134 /// Get the number of dependent libraries 00135 uint32_t getDependentLibraryCount() { 00136 return _deplibs.size(); 00137 } 00138 00139 /// Get the dependent library at the specified index. 00140 const char *getDependentLibrary(uint32_t index) { 00141 if (index < _deplibs.size()) 00142 return _deplibs[index]; 00143 return nullptr; 00144 } 00145 00146 /// Get the number of linker options 00147 uint32_t getLinkerOptCount() { 00148 return _linkeropts.size(); 00149 } 00150 00151 /// Get the linker option at the specified index. 00152 const char *getLinkerOpt(uint32_t index) { 00153 if (index < _linkeropts.size()) 00154 return _linkeropts[index]; 00155 return nullptr; 00156 } 00157 00158 const std::vector<const char*> &getAsmUndefinedRefs() { 00159 return _asm_undefines; 00160 } 00161 00162 private: 00163 /// Parse metadata from the module 00164 // FIXME: it only parses "Linker Options" metadata at the moment 00165 void parseMetadata(); 00166 00167 /// Parse the symbols from the module and model-level ASM and add them to 00168 /// either the defined or undefined lists. 00169 bool parseSymbols(std::string &errMsg); 00170 00171 /// Add a symbol which isn't defined just yet to a list to be resolved later. 00172 void addPotentialUndefinedSymbol(const object::BasicSymbolRef &Sym, 00173 bool isFunc); 00174 00175 /// Add a defined symbol to the list. 00176 void addDefinedSymbol(const char *Name, const GlobalValue *def, 00177 bool isFunction); 00178 00179 /// Add a data symbol as defined to the list. 00180 void addDefinedDataSymbol(const object::BasicSymbolRef &Sym); 00181 void addDefinedDataSymbol(const char*Name, const GlobalValue *v); 00182 00183 /// Add a function symbol as defined to the list. 00184 void addDefinedFunctionSymbol(const object::BasicSymbolRef &Sym); 00185 void addDefinedFunctionSymbol(const char *Name, const Function *F); 00186 00187 /// Add a global symbol from module-level ASM to the defined list. 00188 void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope); 00189 00190 /// Add a global symbol from module-level ASM to the undefined list. 00191 void addAsmGlobalSymbolUndef(const char *); 00192 00193 /// Parse i386/ppc ObjC class data structure. 00194 void addObjCClass(const GlobalVariable *clgv); 00195 00196 /// Parse i386/ppc ObjC category data structure. 00197 void addObjCCategory(const GlobalVariable *clgv); 00198 00199 /// Parse i386/ppc ObjC class list data structure. 00200 void addObjCClassRef(const GlobalVariable *clgv); 00201 00202 /// Get string that the data pointer points to. 00203 bool objcClassNameFromExpression(const Constant *c, std::string &name); 00204 00205 /// Create an LTOModule (private version). 00206 static LTOModule *makeLTOModule(MemoryBufferRef Buffer, TargetOptions options, 00207 std::string &errMsg); 00208 }; 00209 } 00210 #endif