LTOModule.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-LTOModule.h - LLVM Link Time Optimizer ------------------------------===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file declares the LTOModule class.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_LTO_LEGACY_LTOMODULE_H
  18. #define LLVM_LTO_LEGACY_LTOMODULE_H
  19. #include "llvm-c/lto.h"
  20. #include "llvm/ADT/StringMap.h"
  21. #include "llvm/ADT/StringSet.h"
  22. #include "llvm/IR/Module.h"
  23. #include "llvm/LTO/LTO.h"
  24. #include "llvm/Object/IRObjectFile.h"
  25. #include "llvm/Object/ModuleSymbolTable.h"
  26. #include "llvm/Target/TargetMachine.h"
  27. #include <string>
  28. #include <vector>
  29. // Forward references to llvm classes.
  30. namespace llvm {
  31. class Function;
  32. class GlobalValue;
  33. class MemoryBuffer;
  34. class TargetOptions;
  35. class Value;
  36. //===----------------------------------------------------------------------===//
  37. /// C++ class which implements the opaque lto_module_t type.
  38. ///
  39. struct LTOModule {
  40. private:
  41. struct NameAndAttributes {
  42. StringRef name;
  43. uint32_t attributes = 0;
  44. bool isFunction = false;
  45. const GlobalValue *symbol = nullptr;
  46. };
  47. std::unique_ptr<LLVMContext> OwnedContext;
  48. std::string LinkerOpts;
  49. std::unique_ptr<Module> Mod;
  50. MemoryBufferRef MBRef;
  51. ModuleSymbolTable SymTab;
  52. std::unique_ptr<TargetMachine> _target;
  53. std::vector<NameAndAttributes> _symbols;
  54. // _defines and _undefines only needed to disambiguate tentative definitions
  55. StringSet<> _defines;
  56. StringMap<NameAndAttributes> _undefines;
  57. std::vector<StringRef> _asm_undefines;
  58. LTOModule(std::unique_ptr<Module> M, MemoryBufferRef MBRef,
  59. TargetMachine *TM);
  60. public:
  61. ~LTOModule();
  62. /// Returns 'true' if the file or memory contents is LLVM bitcode.
  63. static bool isBitcodeFile(const void *mem, size_t length);
  64. static bool isBitcodeFile(StringRef path);
  65. /// Returns 'true' if the Module is produced for ThinLTO.
  66. bool isThinLTO();
  67. /// Returns 'true' if the memory buffer is LLVM bitcode for the specified
  68. /// triple.
  69. static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
  70. StringRef triplePrefix);
  71. /// Returns a string representing the producer identification stored in the
  72. /// bitcode, or "" if the bitcode does not contains any.
  73. ///
  74. static std::string getProducerString(MemoryBuffer *Buffer);
  75. /// Create a MemoryBuffer from a memory range with an optional name.
  76. static std::unique_ptr<MemoryBuffer>
  77. makeBuffer(const void *mem, size_t length, StringRef name = "");
  78. /// Create an LTOModule. N.B. These methods take ownership of the buffer. The
  79. /// caller must have initialized the Targets, the TargetMCs, the AsmPrinters,
  80. /// and the AsmParsers by calling:
  81. ///
  82. /// InitializeAllTargets();
  83. /// InitializeAllTargetMCs();
  84. /// InitializeAllAsmPrinters();
  85. /// InitializeAllAsmParsers();
  86. static ErrorOr<std::unique_ptr<LTOModule>>
  87. createFromFile(LLVMContext &Context, StringRef path,
  88. const TargetOptions &options);
  89. static ErrorOr<std::unique_ptr<LTOModule>>
  90. createFromOpenFile(LLVMContext &Context, int fd, StringRef path, size_t size,
  91. const TargetOptions &options);
  92. static ErrorOr<std::unique_ptr<LTOModule>>
  93. createFromOpenFileSlice(LLVMContext &Context, int fd, StringRef path,
  94. size_t map_size, off_t offset,
  95. const TargetOptions &options);
  96. static ErrorOr<std::unique_ptr<LTOModule>>
  97. createFromBuffer(LLVMContext &Context, const void *mem, size_t length,
  98. const TargetOptions &options, StringRef path = "");
  99. static ErrorOr<std::unique_ptr<LTOModule>>
  100. createInLocalContext(std::unique_ptr<LLVMContext> Context, const void *mem,
  101. size_t length, const TargetOptions &options,
  102. StringRef path);
  103. const Module &getModule() const { return *Mod; }
  104. Module &getModule() { return *Mod; }
  105. std::unique_ptr<Module> takeModule() { return std::move(Mod); }
  106. /// Return the Module's target triple.
  107. const std::string &getTargetTriple() {
  108. return getModule().getTargetTriple();
  109. }
  110. /// Set the Module's target triple.
  111. void setTargetTriple(StringRef Triple) {
  112. getModule().setTargetTriple(Triple);
  113. }
  114. /// Get the number of symbols
  115. uint32_t getSymbolCount() {
  116. return _symbols.size();
  117. }
  118. /// Get the attributes for a symbol at the specified index.
  119. lto_symbol_attributes getSymbolAttributes(uint32_t index) {
  120. if (index < _symbols.size())
  121. return lto_symbol_attributes(_symbols[index].attributes);
  122. return lto_symbol_attributes(0);
  123. }
  124. /// Get the name of the symbol at the specified index.
  125. StringRef getSymbolName(uint32_t index) {
  126. if (index < _symbols.size())
  127. return _symbols[index].name;
  128. return StringRef();
  129. }
  130. const GlobalValue *getSymbolGV(uint32_t index) {
  131. if (index < _symbols.size())
  132. return _symbols[index].symbol;
  133. return nullptr;
  134. }
  135. StringRef getLinkerOpts() { return LinkerOpts; }
  136. const std::vector<StringRef> &getAsmUndefinedRefs() { return _asm_undefines; }
  137. static lto::InputFile *createInputFile(const void *buffer, size_t buffer_size,
  138. const char *path, std::string &out_error);
  139. static size_t getDependentLibraryCount(lto::InputFile *input);
  140. static const char *getDependentLibrary(lto::InputFile *input, size_t index, size_t *size);
  141. Expected<uint32_t> getMachOCPUType() const;
  142. Expected<uint32_t> getMachOCPUSubType() const;
  143. /// Returns true if the module has either the @llvm.global_ctors or the
  144. /// @llvm.global_dtors symbol. Otherwise returns false.
  145. bool hasCtorDtor() const;
  146. private:
  147. /// Parse metadata from the module
  148. // FIXME: it only parses "llvm.linker.options" metadata at the moment
  149. // FIXME: can't access metadata in lazily loaded modules
  150. void parseMetadata();
  151. /// Parse the symbols from the module and model-level ASM and add them to
  152. /// either the defined or undefined lists.
  153. void parseSymbols();
  154. /// Add a symbol which isn't defined just yet to a list to be resolved later.
  155. void addPotentialUndefinedSymbol(ModuleSymbolTable::Symbol Sym,
  156. bool isFunc);
  157. /// Add a defined symbol to the list.
  158. void addDefinedSymbol(StringRef Name, const GlobalValue *def,
  159. bool isFunction);
  160. /// Add a data symbol as defined to the list.
  161. void addDefinedDataSymbol(ModuleSymbolTable::Symbol Sym);
  162. void addDefinedDataSymbol(StringRef Name, const GlobalValue *v);
  163. /// Add a function symbol as defined to the list.
  164. void addDefinedFunctionSymbol(ModuleSymbolTable::Symbol Sym);
  165. void addDefinedFunctionSymbol(StringRef Name, const Function *F);
  166. /// Add a global symbol from module-level ASM to the defined list.
  167. void addAsmGlobalSymbol(StringRef, lto_symbol_attributes scope);
  168. /// Add a global symbol from module-level ASM to the undefined list.
  169. void addAsmGlobalSymbolUndef(StringRef);
  170. /// Parse i386/ppc ObjC class data structure.
  171. void addObjCClass(const GlobalVariable *clgv);
  172. /// Parse i386/ppc ObjC category data structure.
  173. void addObjCCategory(const GlobalVariable *clgv);
  174. /// Parse i386/ppc ObjC class list data structure.
  175. void addObjCClassRef(const GlobalVariable *clgv);
  176. /// Get string that the data pointer points to.
  177. bool objcClassNameFromExpression(const Constant *c, std::string &name);
  178. /// Create an LTOModule (private version).
  179. static ErrorOr<std::unique_ptr<LTOModule>>
  180. makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
  181. LLVMContext &Context, bool ShouldBeLazy);
  182. };
  183. }
  184. #endif
  185. #ifdef __GNUC__
  186. #pragma GCC diagnostic pop
  187. #endif