NewPMDriver.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //===- NewPMDriver.cpp - Driver for opt with new PM -----------------------===//
  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. /// \file
  10. ///
  11. /// This file is just a split of the code that logically belongs in opt.cpp but
  12. /// that includes the new pass manager headers.
  13. ///
  14. //===----------------------------------------------------------------------===//
  15. #include "NewPMDriver.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/Analysis/CGSCCPassManager.h"
  18. #include "llvm/Bitcode/BitcodeWriterPass.h"
  19. #include "llvm/IR/Dominators.h"
  20. #include "llvm/IR/IRPrintingPasses.h"
  21. #include "llvm/IR/LLVMContext.h"
  22. #include "llvm/IR/Module.h"
  23. #include "llvm/IR/PassManager.h"
  24. #include "llvm/IR/Verifier.h"
  25. #include "llvm/Passes/PassBuilder.h"
  26. #include "llvm/Support/CommandLine.h"
  27. #include "llvm/Support/ErrorHandling.h"
  28. #include "llvm/Support/ToolOutputFile.h"
  29. #include "llvm/Target/TargetMachine.h"
  30. using namespace llvm;
  31. using namespace opt_tool;
  32. static cl::opt<bool>
  33. DebugPM("debug-pass-manager", cl::Hidden,
  34. cl::desc("Print pass management debugging information"));
  35. bool llvm::runPassPipeline(StringRef Arg0, LLVMContext &Context, Module &M,
  36. TargetMachine *TM, tool_output_file *Out,
  37. StringRef PassPipeline, OutputKind OK,
  38. VerifierKind VK,
  39. bool ShouldPreserveAssemblyUseListOrder,
  40. bool ShouldPreserveBitcodeUseListOrder) {
  41. PassBuilder PB(TM);
  42. FunctionAnalysisManager FAM(DebugPM);
  43. CGSCCAnalysisManager CGAM(DebugPM);
  44. ModuleAnalysisManager MAM(DebugPM);
  45. // Register all the basic analyses with the managers.
  46. PB.registerModuleAnalyses(MAM);
  47. PB.registerCGSCCAnalyses(CGAM);
  48. PB.registerFunctionAnalyses(FAM);
  49. // Cross register the analysis managers through their proxies.
  50. MAM.registerPass(FunctionAnalysisManagerModuleProxy(FAM));
  51. MAM.registerPass(CGSCCAnalysisManagerModuleProxy(CGAM));
  52. CGAM.registerPass(FunctionAnalysisManagerCGSCCProxy(FAM));
  53. CGAM.registerPass(ModuleAnalysisManagerCGSCCProxy(MAM));
  54. FAM.registerPass(CGSCCAnalysisManagerFunctionProxy(CGAM));
  55. FAM.registerPass(ModuleAnalysisManagerFunctionProxy(MAM));
  56. ModulePassManager MPM(DebugPM);
  57. if (VK > VK_NoVerifier)
  58. MPM.addPass(VerifierPass());
  59. if (!PB.parsePassPipeline(MPM, PassPipeline, VK == VK_VerifyEachPass,
  60. DebugPM)) {
  61. errs() << Arg0 << ": unable to parse pass pipeline description.\n";
  62. return false;
  63. }
  64. if (VK > VK_NoVerifier)
  65. MPM.addPass(VerifierPass());
  66. // Add any relevant output pass at the end of the pipeline.
  67. switch (OK) {
  68. case OK_NoOutput:
  69. break; // No output pass needed.
  70. case OK_OutputAssembly:
  71. MPM.addPass(
  72. PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder));
  73. break;
  74. case OK_OutputBitcode:
  75. MPM.addPass(
  76. BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder));
  77. break;
  78. }
  79. // Before executing passes, print the final values of the LLVM options.
  80. cl::PrintOptionValues();
  81. // Now that we have all of the passes ready, run them.
  82. MPM.run(M, &MAM);
  83. // Declare success.
  84. if (OK != OK_NoOutput)
  85. Out->keep();
  86. return true;
  87. }