Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 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 | |
| 16 | #include "NewPMDriver.h" |
| 17 | #include "Passes.h" |
| 18 | #include "llvm/ADT/StringRef.h" |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/CGSCCPassManager.h" |
Chandler Carruth | b7bdfd6 | 2014-01-13 07:38:24 +0000 | [diff] [blame] | 20 | #include "llvm/Bitcode/BitcodeWriterPass.h" |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 21 | #include "llvm/IR/IRPrintingPasses.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 22 | #include "llvm/IR/LLVMContext.h" |
| 23 | #include "llvm/IR/Module.h" |
| 24 | #include "llvm/IR/PassManager.h" |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Verifier.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ToolOutputFile.h" |
| 29 | |
| 30 | using namespace llvm; |
Chandler Carruth | 949282e | 2014-01-13 03:08:40 +0000 | [diff] [blame] | 31 | using namespace opt_tool; |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 32 | |
Chandler Carruth | 14a759e | 2015-01-13 22:42:38 +0000 | [diff] [blame^] | 33 | static cl::opt<bool> |
| 34 | DebugPM("debug-pass-manager", cl::Hidden, |
| 35 | cl::desc("Print pass management debugging information")); |
| 36 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 37 | bool llvm::runPassPipeline(StringRef Arg0, LLVMContext &Context, Module &M, |
| 38 | tool_output_file *Out, StringRef PassPipeline, |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 39 | OutputKind OK, VerifierKind VK) { |
Chandler Carruth | 14a759e | 2015-01-13 22:42:38 +0000 | [diff] [blame^] | 40 | FunctionAnalysisManager FAM(DebugPM); |
| 41 | CGSCCAnalysisManager CGAM(DebugPM); |
| 42 | ModuleAnalysisManager MAM(DebugPM); |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 43 | |
Chandler Carruth | b70f673 | 2015-01-06 02:21:37 +0000 | [diff] [blame] | 44 | // Register all the basic analyses with the managers. |
| 45 | registerModuleAnalyses(MAM); |
| 46 | registerCGSCCAnalyses(CGAM); |
| 47 | registerFunctionAnalyses(FAM); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 48 | |
Chandler Carruth | c68d082 | 2014-02-06 04:25:13 +0000 | [diff] [blame] | 49 | // Cross register the analysis managers through their proxies. |
| 50 | MAM.registerPass(FunctionAnalysisManagerModuleProxy(FAM)); |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 51 | MAM.registerPass(CGSCCAnalysisManagerModuleProxy(CGAM)); |
| 52 | CGAM.registerPass(FunctionAnalysisManagerCGSCCProxy(FAM)); |
| 53 | CGAM.registerPass(ModuleAnalysisManagerCGSCCProxy(MAM)); |
| 54 | FAM.registerPass(CGSCCAnalysisManagerFunctionProxy(CGAM)); |
Chandler Carruth | c68d082 | 2014-02-06 04:25:13 +0000 | [diff] [blame] | 55 | FAM.registerPass(ModuleAnalysisManagerFunctionProxy(MAM)); |
| 56 | |
Chandler Carruth | 14a759e | 2015-01-13 22:42:38 +0000 | [diff] [blame^] | 57 | ModulePassManager MPM(DebugPM); |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 58 | if (VK > VK_NoVerifier) |
| 59 | MPM.addPass(VerifierPass()); |
| 60 | |
Chandler Carruth | 14a759e | 2015-01-13 22:42:38 +0000 | [diff] [blame^] | 61 | if (!parsePassPipeline(MPM, PassPipeline, VK == VK_VerifyEachPass, DebugPM)) { |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 62 | errs() << Arg0 << ": unable to parse pass pipeline description.\n"; |
| 63 | return false; |
| 64 | } |
| 65 | |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 66 | if (VK > VK_NoVerifier) |
| 67 | MPM.addPass(VerifierPass()); |
| 68 | |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 69 | // Add any relevant output pass at the end of the pipeline. |
| 70 | switch (OK) { |
| 71 | case OK_NoOutput: |
| 72 | break; // No output pass needed. |
| 73 | case OK_OutputAssembly: |
| 74 | MPM.addPass(PrintModulePass(Out->os())); |
| 75 | break; |
| 76 | case OK_OutputBitcode: |
Chandler Carruth | b7bdfd6 | 2014-01-13 07:38:24 +0000 | [diff] [blame] | 77 | MPM.addPass(BitcodeWriterPass(Out->os())); |
| 78 | break; |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // Before executing passes, print the final values of the LLVM options. |
| 82 | cl::PrintOptionValues(); |
| 83 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 84 | // Now that we have all of the passes ready, run them. |
Chandler Carruth | d174ce4 | 2015-01-05 02:47:05 +0000 | [diff] [blame] | 85 | MPM.run(M, &MAM); |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 86 | |
| 87 | // Declare success. |
Chandler Carruth | 949282e | 2014-01-13 03:08:40 +0000 | [diff] [blame] | 88 | if (OK != OK_NoOutput) |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 89 | Out->keep(); |
| 90 | return true; |
| 91 | } |