LLVMTargetMachine.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
  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 implements the LLVMTargetMachine class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Analysis/Passes.h"
  14. #include "llvm/CodeGen/AsmPrinter.h"
  15. #include "llvm/CodeGen/BasicTTIImpl.h"
  16. #include "llvm/CodeGen/MachineModuleInfo.h"
  17. #include "llvm/CodeGen/Passes.h"
  18. #include "llvm/CodeGen/TargetLoweringObjectFile.h"
  19. #include "llvm/CodeGen/TargetPassConfig.h"
  20. #include "llvm/IR/LegacyPassManager.h"
  21. #include "llvm/MC/MCAsmBackend.h"
  22. #include "llvm/MC/MCAsmInfo.h"
  23. #include "llvm/MC/MCCodeEmitter.h"
  24. #include "llvm/MC/MCContext.h"
  25. #include "llvm/MC/MCInstrInfo.h"
  26. #include "llvm/MC/MCStreamer.h"
  27. #include "llvm/MC/MCSubtargetInfo.h"
  28. #include "llvm/Support/CommandLine.h"
  29. #include "llvm/Support/ErrorHandling.h"
  30. #include "llvm/Support/FormattedStream.h"
  31. #include "llvm/Support/TargetRegistry.h"
  32. #include "llvm/Target/TargetMachine.h"
  33. #include "llvm/Target/TargetOptions.h"
  34. using namespace llvm;
  35. void LLVMTargetMachine::initAsmInfo() {
  36. MRI = TheTarget.createMCRegInfo(getTargetTriple().str());
  37. MII = TheTarget.createMCInstrInfo();
  38. // FIXME: Having an MCSubtargetInfo on the target machine is a hack due
  39. // to some backends having subtarget feature dependent module level
  40. // code generation. This is similar to the hack in the AsmPrinter for
  41. // module level assembly etc.
  42. STI = TheTarget.createMCSubtargetInfo(getTargetTriple().str(), getTargetCPU(),
  43. getTargetFeatureString());
  44. MCAsmInfo *TmpAsmInfo =
  45. TheTarget.createMCAsmInfo(*MRI, getTargetTriple().str());
  46. // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
  47. // and if the old one gets included then MCAsmInfo will be NULL and
  48. // we'll crash later.
  49. // Provide the user with a useful error message about what's wrong.
  50. assert(TmpAsmInfo && "MCAsmInfo not initialized. "
  51. "Make sure you include the correct TargetSelect.h"
  52. "and that InitializeAllTargetMCs() is being invoked!");
  53. if (Options.DisableIntegratedAS)
  54. TmpAsmInfo->setUseIntegratedAssembler(false);
  55. TmpAsmInfo->setPreserveAsmComments(Options.MCOptions.PreserveAsmComments);
  56. TmpAsmInfo->setCompressDebugSections(Options.CompressDebugSections);
  57. TmpAsmInfo->setRelaxELFRelocations(Options.RelaxELFRelocations);
  58. if (Options.ExceptionModel != ExceptionHandling::None)
  59. TmpAsmInfo->setExceptionsType(Options.ExceptionModel);
  60. AsmInfo = TmpAsmInfo;
  61. }
  62. LLVMTargetMachine::LLVMTargetMachine(const Target &T,
  63. StringRef DataLayoutString,
  64. const Triple &TT, StringRef CPU,
  65. StringRef FS, const TargetOptions &Options,
  66. Reloc::Model RM, CodeModel::Model CM,
  67. CodeGenOpt::Level OL)
  68. : TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) {
  69. this->RM = RM;
  70. this->CMModel = CM;
  71. this->OptLevel = OL;
  72. }
  73. TargetIRAnalysis LLVMTargetMachine::getTargetIRAnalysis() {
  74. return TargetIRAnalysis([this](const Function &F) {
  75. return TargetTransformInfo(BasicTTIImpl(this, F));
  76. });
  77. }
  78. /// addPassesToX helper drives creation and initialization of TargetPassConfig.
  79. static MCContext *
  80. addPassesToGenerateCode(LLVMTargetMachine *TM, PassManagerBase &PM,
  81. bool DisableVerify, bool &WillCompleteCodeGenPipeline,
  82. raw_pwrite_stream &Out, MachineModuleInfo *MMI) {
  83. // Targets may override createPassConfig to provide a target-specific
  84. // subclass.
  85. TargetPassConfig *PassConfig = TM->createPassConfig(PM);
  86. // Set PassConfig options provided by TargetMachine.
  87. PassConfig->setDisableVerify(DisableVerify);
  88. WillCompleteCodeGenPipeline = PassConfig->willCompleteCodeGenPipeline();
  89. PM.add(PassConfig);
  90. if (!MMI)
  91. MMI = new MachineModuleInfo(TM);
  92. PM.add(MMI);
  93. if (PassConfig->addISelPasses())
  94. return nullptr;
  95. PassConfig->addMachinePasses();
  96. PassConfig->setInitialized();
  97. if (!WillCompleteCodeGenPipeline)
  98. PM.add(createPrintMIRPass(Out));
  99. return &MMI->getContext();
  100. }
  101. bool LLVMTargetMachine::addAsmPrinter(PassManagerBase &PM,
  102. raw_pwrite_stream &Out, CodeGenFileType FileType,
  103. MCContext &Context) {
  104. if (Options.MCOptions.MCSaveTempLabels)
  105. Context.setAllowTemporaryLabels(false);
  106. const MCSubtargetInfo &STI = *getMCSubtargetInfo();
  107. const MCAsmInfo &MAI = *getMCAsmInfo();
  108. const MCRegisterInfo &MRI = *getMCRegisterInfo();
  109. const MCInstrInfo &MII = *getMCInstrInfo();
  110. std::unique_ptr<MCStreamer> AsmStreamer;
  111. switch (FileType) {
  112. case CGFT_AssemblyFile: {
  113. MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter(
  114. getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI);
  115. // Create a code emitter if asked to show the encoding.
  116. MCCodeEmitter *MCE = nullptr;
  117. if (Options.MCOptions.ShowMCEncoding)
  118. MCE = getTarget().createMCCodeEmitter(MII, MRI, Context);
  119. MCAsmBackend *MAB =
  120. getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU,
  121. Options.MCOptions);
  122. auto FOut = llvm::make_unique<formatted_raw_ostream>(Out);
  123. MCStreamer *S = getTarget().createAsmStreamer(
  124. Context, std::move(FOut), Options.MCOptions.AsmVerbose,
  125. Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB,
  126. Options.MCOptions.ShowMCInst);
  127. AsmStreamer.reset(S);
  128. break;
  129. }
  130. case CGFT_ObjectFile: {
  131. // Create the code emitter for the target if it exists. If not, .o file
  132. // emission fails.
  133. MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, Context);
  134. MCAsmBackend *MAB =
  135. getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU,
  136. Options.MCOptions);
  137. if (!MCE || !MAB)
  138. return true;
  139. // Don't waste memory on names of temp labels.
  140. Context.setUseNamesOnTempLabels(false);
  141. Triple T(getTargetTriple().str());
  142. AsmStreamer.reset(getTarget().createMCObjectStreamer(
  143. T, Context, std::unique_ptr<MCAsmBackend>(MAB), Out,
  144. std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll,
  145. Options.MCOptions.MCIncrementalLinkerCompatible,
  146. /*DWARFMustBeAtTheEnd*/ true));
  147. break;
  148. }
  149. case CGFT_Null:
  150. // The Null output is intended for use for performance analysis and testing,
  151. // not real users.
  152. AsmStreamer.reset(getTarget().createNullStreamer(Context));
  153. break;
  154. }
  155. // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
  156. FunctionPass *Printer =
  157. getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
  158. if (!Printer)
  159. return true;
  160. PM.add(Printer);
  161. return false;
  162. }
  163. bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
  164. raw_pwrite_stream &Out,
  165. CodeGenFileType FileType,
  166. bool DisableVerify,
  167. MachineModuleInfo *MMI) {
  168. // Add common CodeGen passes.
  169. bool WillCompleteCodeGenPipeline = true;
  170. MCContext *Context = addPassesToGenerateCode(
  171. this, PM, DisableVerify, WillCompleteCodeGenPipeline, Out, MMI);
  172. if (!Context)
  173. return true;
  174. if (WillCompleteCodeGenPipeline && addAsmPrinter(PM, Out, FileType, *Context))
  175. return true;
  176. PM.add(createFreeMachineFunctionPass());
  177. return false;
  178. }
  179. /// addPassesToEmitMC - Add passes to the specified pass manager to get
  180. /// machine code emitted with the MCJIT. This method returns true if machine
  181. /// code is not supported. It fills the MCContext Ctx pointer which can be
  182. /// used to build custom MCStreamer.
  183. ///
  184. bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
  185. raw_pwrite_stream &Out,
  186. bool DisableVerify) {
  187. // Add common CodeGen passes.
  188. bool WillCompleteCodeGenPipeline = true;
  189. Ctx = addPassesToGenerateCode(this, PM, DisableVerify,
  190. WillCompleteCodeGenPipeline, Out,
  191. /*MachineModuleInfo*/ nullptr);
  192. if (!Ctx)
  193. return true;
  194. assert(WillCompleteCodeGenPipeline && "CodeGen pipeline has been altered");
  195. if (Options.MCOptions.MCSaveTempLabels)
  196. Ctx->setAllowTemporaryLabels(false);
  197. // Create the code emitter for the target if it exists. If not, .o file
  198. // emission fails.
  199. const MCRegisterInfo &MRI = *getMCRegisterInfo();
  200. MCCodeEmitter *MCE =
  201. getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx);
  202. MCAsmBackend *MAB =
  203. getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU,
  204. Options.MCOptions);
  205. if (!MCE || !MAB)
  206. return true;
  207. const Triple &T = getTargetTriple();
  208. const MCSubtargetInfo &STI = *getMCSubtargetInfo();
  209. std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
  210. T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), Out,
  211. std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll,
  212. Options.MCOptions.MCIncrementalLinkerCompatible,
  213. /*DWARFMustBeAtTheEnd*/ true));
  214. // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
  215. FunctionPass *Printer =
  216. getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
  217. if (!Printer)
  218. return true;
  219. PM.add(Printer);
  220. PM.add(createFreeMachineFunctionPass());
  221. return false; // success!
  222. }