LTOModule.h 7.3 KB

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