LLVMTargetMachine.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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/Analysis/Verifier.h"
  17. #include "llvm/Assembly/PrintModulePass.h"
  18. #include "llvm/CodeGen/AsmPrinter.h"
  19. #include "llvm/CodeGen/Passes.h"
  20. #include "llvm/CodeGen/GCStrategy.h"
  21. #include "llvm/CodeGen/MachineFunctionAnalysis.h"
  22. #include "llvm/Target/TargetOptions.h"
  23. #include "llvm/MC/MCAsmInfo.h"
  24. #include "llvm/MC/MCContext.h"
  25. #include "llvm/MC/MCStreamer.h"
  26. #include "llvm/Target/TargetData.h"
  27. #include "llvm/Target/TargetRegistry.h"
  28. #include "llvm/Transforms/Scalar.h"
  29. #include "llvm/ADT/OwningPtr.h"
  30. #include "llvm/Support/CommandLine.h"
  31. #include "llvm/Support/Debug.h"
  32. #include "llvm/Support/FormattedStream.h"
  33. using namespace llvm;
  34. namespace llvm {
  35. bool EnableFastISel;
  36. }
  37. static cl::opt<bool> DisablePostRA("disable-post-ra", cl::Hidden,
  38. cl::desc("Disable Post Regalloc"));
  39. static cl::opt<bool> DisableBranchFold("disable-branch-fold", cl::Hidden,
  40. cl::desc("Disable branch folding"));
  41. static cl::opt<bool> DisableTailDuplicate("disable-tail-duplicate", cl::Hidden,
  42. cl::desc("Disable tail duplication"));
  43. static cl::opt<bool> DisableEarlyTailDup("disable-early-taildup", cl::Hidden,
  44. cl::desc("Disable pre-register allocation tail duplication"));
  45. static cl::opt<bool> DisableCodePlace("disable-code-place", cl::Hidden,
  46. cl::desc("Disable code placement"));
  47. static cl::opt<bool> DisableSSC("disable-ssc", cl::Hidden,
  48. cl::desc("Disable Stack Slot Coloring"));
  49. static cl::opt<bool> DisableMachineLICM("disable-machine-licm", cl::Hidden,
  50. cl::desc("Disable Machine LICM"));
  51. static cl::opt<bool> DisableMachineSink("disable-machine-sink", cl::Hidden,
  52. cl::desc("Disable Machine Sinking"));
  53. static cl::opt<bool> DisableLSR("disable-lsr", cl::Hidden,
  54. cl::desc("Disable Loop Strength Reduction Pass"));
  55. static cl::opt<bool> DisableCGP("disable-cgp", cl::Hidden,
  56. cl::desc("Disable Codegen Prepare"));
  57. static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
  58. cl::desc("Print LLVM IR produced by the loop-reduce pass"));
  59. static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
  60. cl::desc("Print LLVM IR input to isel pass"));
  61. static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
  62. cl::desc("Dump garbage collector data"));
  63. static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
  64. cl::desc("Verify generated machine code"),
  65. cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL));
  66. static cl::opt<cl::boolOrDefault>
  67. AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
  68. cl::init(cl::BOU_UNSET));
  69. static bool getVerboseAsm() {
  70. switch (AsmVerbose) {
  71. default:
  72. case cl::BOU_UNSET: return TargetMachine::getAsmVerbosityDefault();
  73. case cl::BOU_TRUE: return true;
  74. case cl::BOU_FALSE: return false;
  75. }
  76. }
  77. // Enable or disable FastISel. Both options are needed, because
  78. // FastISel is enabled by default with -fast, and we wish to be
  79. // able to enable or disable fast-isel independently from -O0.
  80. static cl::opt<cl::boolOrDefault>
  81. EnableFastISelOption("fast-isel", cl::Hidden,
  82. cl::desc("Enable the \"fast\" instruction selector"));
  83. // Enable or disable an experimental optimization to split GEPs
  84. // and run a special GVN pass which does not examine loads, in
  85. // an effort to factor out redundancy implicit in complex GEPs.
  86. static cl::opt<bool> EnableSplitGEPGVN("split-gep-gvn", cl::Hidden,
  87. cl::desc("Split GEPs and run no-load GVN"));
  88. LLVMTargetMachine::LLVMTargetMachine(const Target &T,
  89. const std::string &TargetTriple)
  90. : TargetMachine(T) {
  91. AsmInfo = T.createAsmInfo(TargetTriple);
  92. }
  93. // Set the default code model for the JIT for a generic target.
  94. // FIXME: Is small right here? or .is64Bit() ? Large : Small?
  95. void
  96. LLVMTargetMachine::setCodeModelForJIT() {
  97. setCodeModel(CodeModel::Small);
  98. }
  99. // Set the default code model for static compilation for a generic target.
  100. void
  101. LLVMTargetMachine::setCodeModelForStatic() {
  102. setCodeModel(CodeModel::Small);
  103. }
  104. bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
  105. formatted_raw_ostream &Out,
  106. CodeGenFileType FileType,
  107. CodeGenOpt::Level OptLevel,
  108. bool DisableVerify) {
  109. // Add common CodeGen passes.
  110. if (addCommonCodeGenPasses(PM, OptLevel, DisableVerify))
  111. return true;
  112. OwningPtr<MCContext> Context(new MCContext());
  113. OwningPtr<MCStreamer> AsmStreamer;
  114. formatted_raw_ostream *LegacyOutput;
  115. switch (FileType) {
  116. default: return true;
  117. case CGFT_AssemblyFile: {
  118. const MCAsmInfo &MAI = *getMCAsmInfo();
  119. MCInstPrinter *InstPrinter =
  120. getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI, Out);
  121. AsmStreamer.reset(createAsmStreamer(*Context, Out, MAI,
  122. getTargetData()->isLittleEndian(),
  123. getVerboseAsm(), InstPrinter,
  124. /*codeemitter*/0));
  125. // Set the AsmPrinter's "O" to the output file.
  126. LegacyOutput = &Out;
  127. break;
  128. }
  129. case CGFT_ObjectFile: {
  130. // Create the code emitter for the target if it exists. If not, .o file
  131. // emission fails.
  132. MCCodeEmitter *MCE = getTarget().createCodeEmitter(*this, *Context);
  133. if (MCE == 0)
  134. return true;
  135. AsmStreamer.reset(createMachOStreamer(*Context, Out, MCE));
  136. // Any output to the asmprinter's "O" stream is bad and needs to be fixed,
  137. // force it to come out stderr.
  138. // FIXME: this is horrible and leaks, eventually remove the raw_ostream from
  139. // asmprinter.
  140. LegacyOutput = new formatted_raw_ostream(errs());
  141. break;
  142. }
  143. case CGFT_Null:
  144. // The Null output is intended for use for performance analysis and testing,
  145. // not real users.
  146. AsmStreamer.reset(createNullStreamer(*Context));
  147. // Any output to the asmprinter's "O" stream is bad and needs to be fixed,
  148. // force it to come out stderr.
  149. // FIXME: this is horrible and leaks, eventually remove the raw_ostream from
  150. // asmprinter.
  151. LegacyOutput = new formatted_raw_ostream(errs());
  152. break;
  153. }
  154. // Create the AsmPrinter, which takes ownership of Context and AsmStreamer
  155. // if successful.
  156. FunctionPass *Printer =
  157. getTarget().createAsmPrinter(*LegacyOutput, *this, *Context, *AsmStreamer,
  158. getMCAsmInfo());
  159. if (Printer == 0)
  160. return true;
  161. // If successful, createAsmPrinter took ownership of AsmStreamer and Context.
  162. Context.take(); AsmStreamer.take();
  163. PM.add(Printer);
  164. // Make sure the code model is set.
  165. setCodeModelForStatic();
  166. PM.add(createGCInfoDeleter());
  167. return false;
  168. }
  169. /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
  170. /// get machine code emitted. This uses a JITCodeEmitter object to handle
  171. /// actually outputting the machine code and resolving things like the address
  172. /// of functions. This method should returns true if machine code emission is
  173. /// not supported.
  174. ///
  175. bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
  176. JITCodeEmitter &JCE,
  177. CodeGenOpt::Level OptLevel,
  178. bool DisableVerify) {
  179. // Make sure the code model is set.
  180. setCodeModelForJIT();
  181. // Add common CodeGen passes.
  182. if (addCommonCodeGenPasses(PM, OptLevel, DisableVerify))
  183. return true;
  184. addCodeEmitter(PM, OptLevel, JCE);
  185. PM.add(createGCInfoDeleter());
  186. return false; // success!
  187. }
  188. static void printAndVerify(PassManagerBase &PM,
  189. const char *Banner,
  190. bool allowDoubleDefs = false) {
  191. if (PrintMachineCode)
  192. PM.add(createMachineFunctionPrinterPass(dbgs(), Banner));
  193. if (VerifyMachineCode)
  194. PM.add(createMachineVerifierPass(allowDoubleDefs));
  195. }
  196. /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for both
  197. /// emitting to assembly files or machine code output.
  198. ///
  199. bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM,
  200. CodeGenOpt::Level OptLevel,
  201. bool DisableVerify) {
  202. // Standard LLVM-Level Passes.
  203. // Before running any passes, run the verifier to determine if the input
  204. // coming from the front-end and/or optimizer is valid.
  205. if (!DisableVerify)
  206. PM.add(createVerifierPass());
  207. // Optionally, tun split-GEPs and no-load GVN.
  208. if (EnableSplitGEPGVN) {
  209. PM.add(createGEPSplitterPass());
  210. PM.add(createGVNPass(/*NoLoads=*/true));
  211. }
  212. // Run loop strength reduction before anything else.
  213. if (OptLevel != CodeGenOpt::None && !DisableLSR) {
  214. PM.add(createLoopStrengthReducePass(getTargetLowering()));
  215. if (PrintLSR)
  216. PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &dbgs()));
  217. }
  218. // Turn exception handling constructs into something the code generators can
  219. // handle.
  220. switch (getMCAsmInfo()->getExceptionHandlingType())
  221. {
  222. case ExceptionHandling::SjLj:
  223. // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
  224. // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise,
  225. // catch info can get misplaced when a selector ends up more than one block
  226. // removed from the parent invoke(s). This could happen when a landing
  227. // pad is shared by multiple invokes and is also a target of a normal
  228. // edge from elsewhere.
  229. PM.add(createSjLjEHPass(getTargetLowering()));
  230. PM.add(createDwarfEHPass(getTargetLowering(), OptLevel==CodeGenOpt::None));
  231. break;
  232. case ExceptionHandling::Dwarf:
  233. PM.add(createDwarfEHPass(getTargetLowering(), OptLevel==CodeGenOpt::None));
  234. break;
  235. case ExceptionHandling::None:
  236. PM.add(createLowerInvokePass(getTargetLowering()));
  237. break;
  238. }
  239. PM.add(createGCLoweringPass());
  240. // Make sure that no unreachable blocks are instruction selected.
  241. PM.add(createUnreachableBlockEliminationPass());
  242. if (OptLevel != CodeGenOpt::None && !DisableCGP)
  243. PM.add(createCodeGenPreparePass(getTargetLowering()));
  244. PM.add(createStackProtectorPass(getTargetLowering()));
  245. if (PrintISelInput)
  246. PM.add(createPrintFunctionPass("\n\n"
  247. "*** Final LLVM Code input to ISel ***\n",
  248. &dbgs()));
  249. // All passes which modify the LLVM IR are now complete; run the verifier
  250. // to ensure that the IR is valid.
  251. if (!DisableVerify)
  252. PM.add(createVerifierPass());
  253. // Standard Lower-Level Passes.
  254. // Set up a MachineFunction for the rest of CodeGen to work on.
  255. PM.add(new MachineFunctionAnalysis(*this, OptLevel));
  256. // Enable FastISel with -fast, but allow that to be overridden.
  257. if (EnableFastISelOption == cl::BOU_TRUE ||
  258. (OptLevel == CodeGenOpt::None && EnableFastISelOption != cl::BOU_FALSE))
  259. EnableFastISel = true;
  260. // Ask the target for an isel.
  261. if (addInstSelector(PM, OptLevel))
  262. return true;
  263. // Print the instruction selected machine code...
  264. printAndVerify(PM, "After Instruction Selection",
  265. /* allowDoubleDefs= */ true);
  266. // Optimize PHIs before DCE: removing dead PHI cycles may make more
  267. // instructions dead.
  268. if (OptLevel != CodeGenOpt::None)
  269. PM.add(createOptimizePHIsPass());
  270. // Delete dead machine instructions regardless of optimization level.
  271. PM.add(createDeadMachineInstructionElimPass());
  272. printAndVerify(PM, "After codegen DCE pass",
  273. /* allowDoubleDefs= */ true);
  274. if (OptLevel != CodeGenOpt::None) {
  275. PM.add(createOptimizeExtsPass());
  276. if (!DisableMachineLICM)
  277. PM.add(createMachineLICMPass());
  278. if (!DisableMachineSink)
  279. PM.add(createMachineSinkingPass());
  280. printAndVerify(PM, "After MachineLICM and MachineSinking",
  281. /* allowDoubleDefs= */ true);
  282. }
  283. // Pre-ra tail duplication.
  284. if (OptLevel != CodeGenOpt::None && !DisableEarlyTailDup) {
  285. PM.add(createTailDuplicatePass(true));
  286. printAndVerify(PM, "After Pre-RegAlloc TailDuplicate",
  287. /* allowDoubleDefs= */ true);
  288. }
  289. // Run pre-ra passes.
  290. if (addPreRegAlloc(PM, OptLevel))
  291. printAndVerify(PM, "After PreRegAlloc passes",
  292. /* allowDoubleDefs= */ true);
  293. // Perform register allocation.
  294. PM.add(createRegisterAllocator());
  295. printAndVerify(PM, "After Register Allocation");
  296. // Perform stack slot coloring.
  297. if (OptLevel != CodeGenOpt::None && !DisableSSC) {
  298. // FIXME: Re-enable coloring with register when it's capable of adding
  299. // kill markers.
  300. PM.add(createStackSlotColoringPass(false));
  301. printAndVerify(PM, "After StackSlotColoring");
  302. }
  303. // Run post-ra passes.
  304. if (addPostRegAlloc(PM, OptLevel))
  305. printAndVerify(PM, "After PostRegAlloc passes");
  306. PM.add(createLowerSubregsPass());
  307. printAndVerify(PM, "After LowerSubregs");
  308. // Insert prolog/epilog code. Eliminate abstract frame index references...
  309. PM.add(createPrologEpilogCodeInserter());
  310. printAndVerify(PM, "After PrologEpilogCodeInserter");
  311. // Run pre-sched2 passes.
  312. if (addPreSched2(PM, OptLevel))
  313. printAndVerify(PM, "After PreSched2 passes");
  314. // Second pass scheduler.
  315. if (OptLevel != CodeGenOpt::None && !DisablePostRA) {
  316. PM.add(createPostRAScheduler(OptLevel));
  317. printAndVerify(PM, "After PostRAScheduler");
  318. }
  319. // Branch folding must be run after regalloc and prolog/epilog insertion.
  320. if (OptLevel != CodeGenOpt::None && !DisableBranchFold) {
  321. PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
  322. printAndVerify(PM, "After BranchFolding");
  323. }
  324. // Tail duplication.
  325. if (OptLevel != CodeGenOpt::None && !DisableTailDuplicate) {
  326. PM.add(createTailDuplicatePass(false));
  327. printAndVerify(PM, "After TailDuplicate");
  328. }
  329. PM.add(createGCMachineCodeAnalysisPass());
  330. if (PrintGCInfo)
  331. PM.add(createGCInfoPrinter(dbgs()));
  332. if (OptLevel != CodeGenOpt::None && !DisableCodePlace) {
  333. PM.add(createCodePlacementOptPass());
  334. printAndVerify(PM, "After CodePlacementOpt");
  335. }
  336. if (addPreEmitPass(PM, OptLevel))
  337. printAndVerify(PM, "After PreEmit passes");
  338. return false;
  339. }