LLVMTargetMachine.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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/Target/TargetMachine.h"
  14. #include "llvm/Analysis/JumpInstrTableInfo.h"
  15. #include "llvm/Analysis/Passes.h"
  16. #include "llvm/CodeGen/AsmPrinter.h"
  17. #include "llvm/CodeGen/ForwardControlFlowIntegrity.h"
  18. #include "llvm/CodeGen/JumpInstrTables.h"
  19. #include "llvm/CodeGen/MachineFunctionAnalysis.h"
  20. #include "llvm/CodeGen/MachineModuleInfo.h"
  21. #include "llvm/CodeGen/Passes.h"
  22. #include "llvm/IR/IRPrintingPasses.h"
  23. #include "llvm/IR/Verifier.h"
  24. #include "llvm/MC/MCAsmInfo.h"
  25. #include "llvm/MC/MCContext.h"
  26. #include "llvm/MC/MCInstrInfo.h"
  27. #include "llvm/MC/MCStreamer.h"
  28. #include "llvm/MC/MCSubtargetInfo.h"
  29. #include "llvm/PassManager.h"
  30. #include "llvm/Support/CommandLine.h"
  31. #include "llvm/Support/ErrorHandling.h"
  32. #include "llvm/Support/FormattedStream.h"
  33. #include "llvm/Support/TargetRegistry.h"
  34. #include "llvm/Target/TargetInstrInfo.h"
  35. #include "llvm/Target/TargetLowering.h"
  36. #include "llvm/Target/TargetLoweringObjectFile.h"
  37. #include "llvm/Target/TargetOptions.h"
  38. #include "llvm/Target/TargetRegisterInfo.h"
  39. #include "llvm/Target/TargetSubtargetInfo.h"
  40. #include "llvm/Transforms/Scalar.h"
  41. using namespace llvm;
  42. // Enable or disable FastISel. Both options are needed, because
  43. // FastISel is enabled by default with -fast, and we wish to be
  44. // able to enable or disable fast-isel independently from -O0.
  45. static cl::opt<cl::boolOrDefault>
  46. EnableFastISelOption("fast-isel", cl::Hidden,
  47. cl::desc("Enable the \"fast\" instruction selector"));
  48. void LLVMTargetMachine::initAsmInfo() {
  49. MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo(
  50. *getSubtargetImpl()->getRegisterInfo(), getTargetTriple());
  51. // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
  52. // and if the old one gets included then MCAsmInfo will be NULL and
  53. // we'll crash later.
  54. // Provide the user with a useful error message about what's wrong.
  55. assert(TmpAsmInfo && "MCAsmInfo not initialized. "
  56. "Make sure you include the correct TargetSelect.h"
  57. "and that InitializeAllTargetMCs() is being invoked!");
  58. if (Options.DisableIntegratedAS)
  59. TmpAsmInfo->setUseIntegratedAssembler(false);
  60. if (Options.CompressDebugSections)
  61. TmpAsmInfo->setCompressDebugSections(true);
  62. AsmInfo = TmpAsmInfo;
  63. }
  64. LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
  65. StringRef CPU, StringRef FS,
  66. TargetOptions Options,
  67. Reloc::Model RM, CodeModel::Model CM,
  68. CodeGenOpt::Level OL)
  69. : TargetMachine(T, Triple, CPU, FS, Options) {
  70. CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
  71. }
  72. void LLVMTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
  73. PM.add(createBasicTargetTransformInfoPass(this));
  74. }
  75. /// addPassesToX helper drives creation and initialization of TargetPassConfig.
  76. static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
  77. PassManagerBase &PM,
  78. bool DisableVerify,
  79. AnalysisID StartAfter,
  80. AnalysisID StopAfter) {
  81. // Add internal analysis passes from the target machine.
  82. TM->addAnalysisPasses(PM);
  83. // Targets may override createPassConfig to provide a target-specific
  84. // subclass.
  85. TargetPassConfig *PassConfig = TM->createPassConfig(PM);
  86. PassConfig->setStartStopPasses(StartAfter, StopAfter);
  87. // Set PassConfig options provided by TargetMachine.
  88. PassConfig->setDisableVerify(DisableVerify);
  89. PM.add(PassConfig);
  90. PassConfig->addIRPasses();
  91. PassConfig->addCodeGenPrepare();
  92. PassConfig->addPassesToHandleExceptions();
  93. PassConfig->addISelPrepare();
  94. // Install a MachineModuleInfo class, which is an immutable pass that holds
  95. // all the per-module stuff we're generating, including MCContext.
  96. MachineModuleInfo *MMI = new MachineModuleInfo(
  97. *TM->getMCAsmInfo(), *TM->getSubtargetImpl()->getRegisterInfo(),
  98. &TM->getSubtargetImpl()->getTargetLowering()->getObjFileLowering());
  99. PM.add(MMI);
  100. // Set up a MachineFunction for the rest of CodeGen to work on.
  101. PM.add(new MachineFunctionAnalysis(*TM));
  102. // Enable FastISel with -fast, but allow that to be overridden.
  103. if (EnableFastISelOption == cl::BOU_TRUE ||
  104. (TM->getOptLevel() == CodeGenOpt::None &&
  105. EnableFastISelOption != cl::BOU_FALSE))
  106. TM->setFastISel(true);
  107. // Ask the target for an isel.
  108. if (PassConfig->addInstSelector())
  109. return nullptr;
  110. PassConfig->addMachinePasses();
  111. PassConfig->setInitialized();
  112. return &MMI->getContext();
  113. }
  114. bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
  115. formatted_raw_ostream &Out,
  116. CodeGenFileType FileType,
  117. bool DisableVerify,
  118. AnalysisID StartAfter,
  119. AnalysisID StopAfter) {
  120. // Passes to handle jumptable function annotations. These can't be handled at
  121. // JIT time, so we don't add them directly to addPassesToGenerateCode.
  122. PM.add(createJumpInstrTableInfoPass(
  123. getSubtargetImpl()->getInstrInfo()->getJumpInstrTableEntryBound()));
  124. PM.add(createJumpInstrTablesPass(Options.JTType));
  125. if (Options.FCFI)
  126. PM.add(createForwardControlFlowIntegrityPass(
  127. Options.JTType, Options.CFIType, Options.CFIEnforcing,
  128. Options.getCFIFuncName()));
  129. // Add common CodeGen passes.
  130. MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify,
  131. StartAfter, StopAfter);
  132. if (!Context)
  133. return true;
  134. if (StopAfter) {
  135. // FIXME: The intent is that this should eventually write out a YAML file,
  136. // containing the LLVM IR, the machine-level IR (when stopping after a
  137. // machine-level pass), and whatever other information is needed to
  138. // deserialize the code and resume compilation. For now, just write the
  139. // LLVM IR.
  140. PM.add(createPrintModulePass(Out));
  141. return false;
  142. }
  143. if (Options.MCOptions.MCSaveTempLabels)
  144. Context->setAllowTemporaryLabels(false);
  145. const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
  146. const MCAsmInfo &MAI = *getMCAsmInfo();
  147. const MCRegisterInfo &MRI = *getSubtargetImpl()->getRegisterInfo();
  148. const MCInstrInfo &MII = *getSubtargetImpl()->getInstrInfo();
  149. std::unique_ptr<MCStreamer> AsmStreamer;
  150. switch (FileType) {
  151. case CGFT_AssemblyFile: {
  152. MCInstPrinter *InstPrinter =
  153. getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI,
  154. MII, MRI, STI);
  155. // Create a code emitter if asked to show the encoding.
  156. MCCodeEmitter *MCE = nullptr;
  157. if (Options.MCOptions.ShowMCEncoding)
  158. MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, *Context);
  159. MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
  160. TargetCPU);
  161. MCStreamer *S = getTarget().createAsmStreamer(
  162. *Context, Out, Options.MCOptions.AsmVerbose,
  163. Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB,
  164. Options.MCOptions.ShowMCInst);
  165. AsmStreamer.reset(S);
  166. break;
  167. }
  168. case CGFT_ObjectFile: {
  169. // Create the code emitter for the target if it exists. If not, .o file
  170. // emission fails.
  171. MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, STI,
  172. *Context);
  173. MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
  174. TargetCPU);
  175. if (!MCE || !MAB)
  176. return true;
  177. AsmStreamer.reset(
  178. getTarget()
  179. .createMCObjectStreamer(getTargetTriple(), *Context, *MAB, Out, MCE,
  180. STI, Options.MCOptions.MCRelaxAll));
  181. break;
  182. }
  183. case CGFT_Null:
  184. // The Null output is intended for use for performance analysis and testing,
  185. // not real users.
  186. AsmStreamer.reset(getTarget().createNullStreamer(*Context));
  187. break;
  188. }
  189. // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
  190. FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
  191. if (!Printer)
  192. return true;
  193. // If successful, createAsmPrinter took ownership of AsmStreamer.
  194. AsmStreamer.release();
  195. PM.add(Printer);
  196. return false;
  197. }
  198. /// addPassesToEmitMC - Add passes to the specified pass manager to get
  199. /// machine code emitted with the MCJIT. This method returns true if machine
  200. /// code is not supported. It fills the MCContext Ctx pointer which can be
  201. /// used to build custom MCStreamer.
  202. ///
  203. bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
  204. MCContext *&Ctx,
  205. raw_ostream &Out,
  206. bool DisableVerify) {
  207. // Add common CodeGen passes.
  208. Ctx = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, nullptr);
  209. if (!Ctx)
  210. return true;
  211. if (Options.MCOptions.MCSaveTempLabels)
  212. Ctx->setAllowTemporaryLabels(false);
  213. // Create the code emitter for the target if it exists. If not, .o file
  214. // emission fails.
  215. const MCRegisterInfo &MRI = *getSubtargetImpl()->getRegisterInfo();
  216. const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
  217. MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(
  218. *getSubtargetImpl()->getInstrInfo(), MRI, STI, *Ctx);
  219. MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
  220. TargetCPU);
  221. if (!MCE || !MAB)
  222. return true;
  223. std::unique_ptr<MCStreamer> AsmStreamer;
  224. AsmStreamer.reset(getTarget()
  225. .createMCObjectStreamer(getTargetTriple(), *Ctx, *MAB,
  226. Out, MCE, STI,
  227. Options.MCOptions.MCRelaxAll));
  228. // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
  229. FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
  230. if (!Printer)
  231. return true;
  232. // If successful, createAsmPrinter took ownership of AsmStreamer.
  233. AsmStreamer.release();
  234. PM.add(Printer);
  235. return false; // success!
  236. }