LLVMTargetMachine.cpp 11 KB

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