LLVMTargetMachine.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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/PassManager.h"
  15. #include "llvm/Pass.h"
  16. #include "llvm/Assembly/PrintModulePass.h"
  17. #include "llvm/Analysis/LoopPass.h"
  18. #include "llvm/CodeGen/Passes.h"
  19. #include "llvm/CodeGen/GCStrategy.h"
  20. #include "llvm/Target/TargetOptions.h"
  21. #include "llvm/Target/TargetAsmInfo.h"
  22. #include "llvm/Transforms/Scalar.h"
  23. #include "llvm/Support/CommandLine.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. using namespace llvm;
  26. namespace llvm {
  27. bool EnableFastISel;
  28. }
  29. static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
  30. cl::desc("Print LLVM IR produced by the loop-reduce pass"));
  31. static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
  32. cl::desc("Print LLVM IR input to isel pass"));
  33. static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
  34. cl::desc("Dump emitter generated instructions as assembly"));
  35. static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
  36. cl::desc("Dump garbage collector data"));
  37. // When this works it will be on by default.
  38. static cl::opt<bool>
  39. DisablePostRAScheduler("disable-post-RA-scheduler",
  40. cl::desc("Disable scheduling after register allocation"),
  41. cl::init(true));
  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 -fast.
  45. static cl::opt<cl::boolOrDefault>
  46. EnableFastISelOption("fast-isel", cl::Hidden,
  47. cl::desc("Enable the experimental \"fast\" instruction selector"));
  48. FileModel::Model
  49. LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
  50. raw_ostream &Out,
  51. CodeGenFileType FileType,
  52. bool Fast) {
  53. // Add common CodeGen passes.
  54. if (addCommonCodeGenPasses(PM, Fast))
  55. return FileModel::Error;
  56. // Fold redundant debug labels.
  57. PM.add(createDebugLabelFoldingPass());
  58. if (PrintMachineCode)
  59. PM.add(createMachineFunctionPrinterPass(cerr));
  60. if (addPreEmitPass(PM, Fast) && PrintMachineCode)
  61. PM.add(createMachineFunctionPrinterPass(cerr));
  62. if (!Fast)
  63. PM.add(createLoopAlignerPass());
  64. switch (FileType) {
  65. default:
  66. break;
  67. case TargetMachine::AssemblyFile:
  68. if (addAssemblyEmitter(PM, Fast, getAsmVerbosityDefault(), Out))
  69. return FileModel::Error;
  70. return FileModel::AsmFile;
  71. case TargetMachine::ObjectFile:
  72. if (getMachOWriterInfo())
  73. return FileModel::MachOFile;
  74. else if (getELFWriterInfo())
  75. return FileModel::ElfFile;
  76. }
  77. return FileModel::Error;
  78. }
  79. /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
  80. /// be split up (e.g., to add an object writer pass), this method can be used to
  81. /// finish up adding passes to emit the file, if necessary.
  82. bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
  83. MachineCodeEmitter *MCE,
  84. bool Fast) {
  85. if (MCE)
  86. addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
  87. PM.add(createGCInfoDeleter());
  88. // Delete machine code for this function
  89. PM.add(createMachineCodeDeleter());
  90. return false; // success!
  91. }
  92. /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
  93. /// get machine code emitted. This uses a MachineCodeEmitter object to handle
  94. /// actually outputting the machine code and resolving things like the address
  95. /// of functions. This method should returns true if machine code emission is
  96. /// not supported.
  97. ///
  98. bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
  99. MachineCodeEmitter &MCE,
  100. bool Fast) {
  101. // Add common CodeGen passes.
  102. if (addCommonCodeGenPasses(PM, Fast))
  103. return true;
  104. if (addPreEmitPass(PM, Fast) && PrintMachineCode)
  105. PM.add(createMachineFunctionPrinterPass(cerr));
  106. addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
  107. PM.add(createGCInfoDeleter());
  108. // Delete machine code for this function
  109. PM.add(createMachineCodeDeleter());
  110. return false; // success!
  111. }
  112. /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
  113. /// both emitting to assembly files or machine code output.
  114. ///
  115. bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM, bool Fast) {
  116. // Standard LLVM-Level Passes.
  117. // Run loop strength reduction before anything else.
  118. if (!Fast) {
  119. PM.add(createLoopStrengthReducePass(getTargetLowering()));
  120. if (PrintLSR)
  121. PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs()));
  122. }
  123. PM.add(createGCLoweringPass());
  124. if (!getTargetAsmInfo()->doesSupportExceptionHandling())
  125. PM.add(createLowerInvokePass(getTargetLowering()));
  126. // Make sure that no unreachable blocks are instruction selected.
  127. PM.add(createUnreachableBlockEliminationPass());
  128. if (!Fast)
  129. PM.add(createCodeGenPreparePass(getTargetLowering()));
  130. PM.add(createStackProtectorPass(getTargetLowering()));
  131. if (PrintISelInput)
  132. PM.add(createPrintFunctionPass("\n\n"
  133. "*** Final LLVM Code input to ISel ***\n",
  134. &errs()));
  135. // Standard Lower-Level Passes.
  136. // Enable FastISel with -fast, but allow that to be overridden.
  137. if (EnableFastISelOption == cl::BOU_TRUE ||
  138. (Fast && EnableFastISelOption != cl::BOU_FALSE))
  139. EnableFastISel = true;
  140. // Ask the target for an isel.
  141. if (addInstSelector(PM, Fast))
  142. return true;
  143. // Print the instruction selected machine code...
  144. if (PrintMachineCode)
  145. PM.add(createMachineFunctionPrinterPass(cerr));
  146. if (!Fast) {
  147. PM.add(createMachineLICMPass());
  148. PM.add(createMachineSinkingPass());
  149. }
  150. // Run pre-ra passes.
  151. if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
  152. PM.add(createMachineFunctionPrinterPass(cerr));
  153. // Perform register allocation.
  154. PM.add(createRegisterAllocator());
  155. // Perform stack slot coloring.
  156. if (!Fast)
  157. PM.add(createStackSlotColoringPass());
  158. if (PrintMachineCode) // Print the register-allocated code
  159. PM.add(createMachineFunctionPrinterPass(cerr));
  160. // Run post-ra passes.
  161. if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
  162. PM.add(createMachineFunctionPrinterPass(cerr));
  163. if (PrintMachineCode)
  164. PM.add(createMachineFunctionPrinterPass(cerr));
  165. PM.add(createLowerSubregsPass());
  166. if (PrintMachineCode) // Print the subreg lowered code
  167. PM.add(createMachineFunctionPrinterPass(cerr));
  168. // Insert prolog/epilog code. Eliminate abstract frame index references...
  169. PM.add(createPrologEpilogCodeInserter());
  170. if (PrintMachineCode)
  171. PM.add(createMachineFunctionPrinterPass(cerr));
  172. // Second pass scheduler.
  173. if (!Fast && !DisablePostRAScheduler) {
  174. PM.add(createPostRAScheduler());
  175. if (PrintMachineCode)
  176. PM.add(createMachineFunctionPrinterPass(cerr));
  177. }
  178. // Branch folding must be run after regalloc and prolog/epilog insertion.
  179. if (!Fast)
  180. PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
  181. if (PrintMachineCode)
  182. PM.add(createMachineFunctionPrinterPass(cerr));
  183. PM.add(createGCMachineCodeAnalysisPass());
  184. if (PrintMachineCode)
  185. PM.add(createMachineFunctionPrinterPass(cerr));
  186. if (PrintGCInfo)
  187. PM.add(createGCInfoPrinter(*cerr));
  188. return false;
  189. }