LTOCodeGenerator.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //===-LTOCodeGenerator.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 LTOCodeGenerator class.
  11. //
  12. // LTO compilation consists of three phases: Pre-IPO, IPO and Post-IPO.
  13. //
  14. // The Pre-IPO phase compiles source code into bitcode file. The resulting
  15. // bitcode files, along with object files and libraries, will be fed to the
  16. // linker to through the IPO and Post-IPO phases. By using obj-file extension,
  17. // the resulting bitcode file disguises itself as an object file, and therefore
  18. // obviates the need of writing a special set of the make-rules only for LTO
  19. // compilation.
  20. //
  21. // The IPO phase perform inter-procedural analyses and optimizations, and
  22. // the Post-IPO consists two sub-phases: intra-procedural scalar optimizations
  23. // (SOPT), and intra-procedural target-dependent code generator (CG).
  24. //
  25. // As of this writing, we don't separate IPO and the Post-IPO SOPT. They
  26. // are intermingled together, and are driven by a single pass manager (see
  27. // PassManagerBuilder::populateLTOPassManager()).
  28. //
  29. // The "LTOCodeGenerator" is the driver for the IPO and Post-IPO stages.
  30. // The "CodeGenerator" here is bit confusing. Don't confuse the "CodeGenerator"
  31. // with the machine specific code generator.
  32. //
  33. //===----------------------------------------------------------------------===//
  34. #ifndef LLVM_LTO_LTOCODEGENERATOR_H
  35. #define LLVM_LTO_LTOCODEGENERATOR_H
  36. #include "llvm-c/lto.h"
  37. #include "llvm/ADT/ArrayRef.h"
  38. #include "llvm/ADT/SmallPtrSet.h"
  39. #include "llvm/ADT/StringMap.h"
  40. #include "llvm/Linker/Linker.h"
  41. #include "llvm/Target/TargetOptions.h"
  42. #include <string>
  43. #include <vector>
  44. namespace llvm {
  45. class LLVMContext;
  46. class DiagnosticInfo;
  47. class GlobalValue;
  48. class Mangler;
  49. class MemoryBuffer;
  50. class TargetLibraryInfo;
  51. class TargetMachine;
  52. class raw_ostream;
  53. class raw_pwrite_stream;
  54. // //
  55. ///////////////////////////////////////////////////////////////////////////////
  56. /// C++ class which implements the opaque lto_code_gen_t type.
  57. ///
  58. struct LTOCodeGenerator {
  59. static const char *getVersionString();
  60. LTOCodeGenerator();
  61. LTOCodeGenerator(std::unique_ptr<LLVMContext> Context);
  62. ~LTOCodeGenerator();
  63. // Merge given module, return true on success.
  64. bool addModule(struct LTOModule *);
  65. // Set the destination module.
  66. void setModule(struct LTOModule *);
  67. void setTargetOptions(TargetOptions options);
  68. void setDebugInfo(lto_debug_model);
  69. void setCodePICModel(lto_codegen_model);
  70. void setCpu(const char *mCpu) { MCpu = mCpu; }
  71. void setAttr(const char *mAttr) { MAttr = mAttr; }
  72. void setOptLevel(unsigned optLevel) { OptLevel = optLevel; }
  73. void setShouldInternalize(bool Value) { ShouldInternalize = Value; }
  74. void setShouldEmbedUselists(bool Value) { ShouldEmbedUselists = Value; }
  75. void addMustPreserveSymbol(StringRef sym) { MustPreserveSymbols[sym] = 1; }
  76. // To pass options to the driver and optimization passes. These options are
  77. // not necessarily for debugging purpose (The function name is misleading).
  78. // This function should be called before LTOCodeGenerator::compilexxx(),
  79. // and LTOCodeGenerator::writeMergedModules().
  80. void setCodeGenDebugOptions(const char *opts);
  81. // Parse the options set in setCodeGenDebugOptions. Like
  82. // setCodeGenDebugOptions, this must be called before
  83. // LTOCodeGenerator::compilexxx() and LTOCodeGenerator::writeMergedModules()
  84. void parseCodeGenDebugOptions();
  85. // Write the merged module to the file specified by the given path.
  86. // Return true on success.
  87. bool writeMergedModules(const char *path, std::string &errMsg);
  88. // Compile the merged module into a *single* object file; the path to object
  89. // file is returned to the caller via argument "name". Return true on
  90. // success.
  91. //
  92. // NOTE that it is up to the linker to remove the intermediate object file.
  93. // Do not try to remove the object file in LTOCodeGenerator's destructor
  94. // as we don't who (LTOCodeGenerator or the obj file) will last longer.
  95. bool compile_to_file(const char **name,
  96. bool disableInline,
  97. bool disableGVNLoadPRE,
  98. bool disableVectorization,
  99. std::string &errMsg);
  100. // As with compile_to_file(), this function compiles the merged module into
  101. // single object file. Instead of returning the object-file-path to the caller
  102. // (linker), it brings the object to a buffer, and return the buffer to the
  103. // caller. This function should delete intermediate object file once its content
  104. // is brought to memory. Return NULL if the compilation was not successful.
  105. std::unique_ptr<MemoryBuffer> compile(bool disableInline,
  106. bool disableGVNLoadPRE,
  107. bool disableVectorization,
  108. std::string &errMsg);
  109. // Optimizes the merged module. Returns true on success.
  110. bool optimize(bool disableInline,
  111. bool disableGVNLoadPRE,
  112. bool disableVectorization,
  113. std::string &errMsg);
  114. // Compiles the merged optimized module into a single object file. It brings
  115. // the object to a buffer, and returns the buffer to the caller. Return NULL
  116. // if the compilation was not successful.
  117. std::unique_ptr<MemoryBuffer> compileOptimized(std::string &errMsg);
  118. void setDiagnosticHandler(lto_diagnostic_handler_t, void *);
  119. LLVMContext &getContext() { return Context; }
  120. private:
  121. void initializeLTOPasses();
  122. bool compileOptimized(raw_pwrite_stream &out, std::string &errMsg);
  123. bool compileOptimizedToFile(const char **name, std::string &errMsg);
  124. void applyScopeRestrictions();
  125. void applyRestriction(GlobalValue &GV, ArrayRef<StringRef> Libcalls,
  126. std::vector<const char *> &MustPreserveList,
  127. SmallPtrSetImpl<GlobalValue *> &AsmUsed,
  128. Mangler &Mangler);
  129. bool determineTarget(std::string &errMsg);
  130. static void DiagnosticHandler(const DiagnosticInfo &DI, void *Context);
  131. void DiagnosticHandler2(const DiagnosticInfo &DI);
  132. typedef StringMap<uint8_t> StringSet;
  133. void destroyMergedModule();
  134. std::unique_ptr<LLVMContext> OwnedContext;
  135. LLVMContext &Context;
  136. Linker IRLinker;
  137. TargetMachine *TargetMach = nullptr;
  138. bool EmitDwarfDebugInfo = false;
  139. bool ScopeRestrictionsDone = false;
  140. lto_codegen_model CodeModel = LTO_CODEGEN_PIC_MODEL_DEFAULT;
  141. StringSet MustPreserveSymbols;
  142. StringSet AsmUndefinedRefs;
  143. std::vector<char *> CodegenOptions;
  144. std::string MCpu;
  145. std::string MAttr;
  146. std::string NativeObjectPath;
  147. TargetOptions Options;
  148. unsigned OptLevel = 2;
  149. lto_diagnostic_handler_t DiagHandler = nullptr;
  150. void *DiagContext = nullptr;
  151. LTOModule *OwnedModule = nullptr;
  152. bool ShouldInternalize = true;
  153. bool ShouldEmbedUselists = false;
  154. };
  155. }
  156. #endif