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" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringRef.h" |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/AliasAnalysis.h" |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/CGSCCPassManager.h" |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/LoopPassManager.h" |
Chandler Carruth | b7bdfd6 | 2014-01-13 07:38:24 +0000 | [diff] [blame] | 21 | #include "llvm/Bitcode/BitcodeWriterPass.h" |
Chandler Carruth | 64764b4 | 2015-01-14 10:19:28 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 23 | #include "llvm/IR/IRPrintingPasses.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 24 | #include "llvm/IR/LLVMContext.h" |
| 25 | #include "llvm/IR/Module.h" |
| 26 | #include "llvm/IR/PassManager.h" |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Verifier.h" |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 28 | #include "llvm/Passes/PassBuilder.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 31 | #include "llvm/Support/ToolOutputFile.h" |
Chandler Carruth | e038552 | 2015-02-01 10:11:22 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetMachine.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 33 | |
| 34 | using namespace llvm; |
Chandler Carruth | 949282e | 2014-01-13 03:08:40 +0000 | [diff] [blame] | 35 | using namespace opt_tool; |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 36 | |
Chandler Carruth | 14a759e | 2015-01-13 22:42:38 +0000 | [diff] [blame] | 37 | static cl::opt<bool> |
| 38 | DebugPM("debug-pass-manager", cl::Hidden, |
| 39 | cl::desc("Print pass management debugging information")); |
| 40 | |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 41 | // This flag specifies a textual description of the alias analysis pipeline to |
| 42 | // use when querying for aliasing information. It only works in concert with |
| 43 | // the "passes" flag above. |
| 44 | static cl::opt<std::string> |
| 45 | AAPipeline("aa-pipeline", |
| 46 | cl::desc("A textual description of the alias analysis " |
| 47 | "pipeline for handling managed aliasing queries"), |
| 48 | cl::Hidden); |
| 49 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 50 | bool llvm::runPassPipeline(StringRef Arg0, LLVMContext &Context, Module &M, |
Chandler Carruth | e038552 | 2015-02-01 10:11:22 +0000 | [diff] [blame] | 51 | TargetMachine *TM, tool_output_file *Out, |
| 52 | StringRef PassPipeline, OutputKind OK, |
Duncan P. N. Exon Smith | 679db33 | 2015-04-15 00:34:24 +0000 | [diff] [blame] | 53 | VerifierKind VK, |
Duncan P. N. Exon Smith | 8a74f68 | 2015-04-15 02:38:06 +0000 | [diff] [blame] | 54 | bool ShouldPreserveAssemblyUseListOrder, |
Duncan P. N. Exon Smith | 679db33 | 2015-04-15 00:34:24 +0000 | [diff] [blame] | 55 | bool ShouldPreserveBitcodeUseListOrder) { |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 56 | PassBuilder PB(TM); |
Chandler Carruth | 2dc92e9 | 2015-02-01 07:40:05 +0000 | [diff] [blame] | 57 | |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 58 | // Specially handle the alias analysis manager so that we can register |
| 59 | // a custom pipeline of AA passes with it. |
| 60 | AAManager AA; |
| 61 | if (!PB.parseAAPipeline(AA, AAPipeline)) { |
| 62 | errs() << Arg0 << ": unable to parse AA pipeline description.\n"; |
| 63 | return false; |
| 64 | } |
| 65 | |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 66 | LoopAnalysisManager LAM(DebugPM); |
Chandler Carruth | 14a759e | 2015-01-13 22:42:38 +0000 | [diff] [blame] | 67 | FunctionAnalysisManager FAM(DebugPM); |
| 68 | CGSCCAnalysisManager CGAM(DebugPM); |
| 69 | ModuleAnalysisManager MAM(DebugPM); |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 70 | |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 71 | // Register the AA manager first so that our version is the one used. |
| 72 | FAM.registerPass([&] { return std::move(AA); }); |
| 73 | |
Chandler Carruth | b70f673 | 2015-01-06 02:21:37 +0000 | [diff] [blame] | 74 | // Register all the basic analyses with the managers. |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 75 | PB.registerModuleAnalyses(MAM); |
| 76 | PB.registerCGSCCAnalyses(CGAM); |
| 77 | PB.registerFunctionAnalyses(FAM); |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 78 | PB.registerLoopAnalyses(LAM); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 79 | |
Chandler Carruth | c68d082 | 2014-02-06 04:25:13 +0000 | [diff] [blame] | 80 | // Cross register the analysis managers through their proxies. |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 81 | MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); }); |
| 82 | MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); }); |
| 83 | CGAM.registerPass([&] { return FunctionAnalysisManagerCGSCCProxy(FAM); }); |
| 84 | CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); }); |
| 85 | FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); }); |
| 86 | FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); }); |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 87 | FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); }); |
| 88 | LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); }); |
Chandler Carruth | c68d082 | 2014-02-06 04:25:13 +0000 | [diff] [blame] | 89 | |
Chandler Carruth | 14a759e | 2015-01-13 22:42:38 +0000 | [diff] [blame] | 90 | ModulePassManager MPM(DebugPM); |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 91 | if (VK > VK_NoVerifier) |
| 92 | MPM.addPass(VerifierPass()); |
| 93 | |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 94 | if (!PB.parsePassPipeline(MPM, PassPipeline, VK == VK_VerifyEachPass, |
| 95 | DebugPM)) { |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 96 | errs() << Arg0 << ": unable to parse pass pipeline description.\n"; |
| 97 | return false; |
| 98 | } |
| 99 | |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 100 | if (VK > VK_NoVerifier) |
| 101 | MPM.addPass(VerifierPass()); |
| 102 | |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 103 | // Add any relevant output pass at the end of the pipeline. |
| 104 | switch (OK) { |
| 105 | case OK_NoOutput: |
| 106 | break; // No output pass needed. |
| 107 | case OK_OutputAssembly: |
Duncan P. N. Exon Smith | 8a74f68 | 2015-04-15 02:38:06 +0000 | [diff] [blame] | 108 | MPM.addPass( |
| 109 | PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder)); |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 110 | break; |
| 111 | case OK_OutputBitcode: |
Duncan P. N. Exon Smith | 679db33 | 2015-04-15 00:34:24 +0000 | [diff] [blame] | 112 | MPM.addPass( |
| 113 | BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder)); |
Chandler Carruth | b7bdfd6 | 2014-01-13 07:38:24 +0000 | [diff] [blame] | 114 | break; |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // Before executing passes, print the final values of the LLVM options. |
| 118 | cl::PrintOptionValues(); |
| 119 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 120 | // Now that we have all of the passes ready, run them. |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame^] | 121 | MPM.run(M, MAM); |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 122 | |
| 123 | // Declare success. |
Chandler Carruth | 949282e | 2014-01-13 03:08:40 +0000 | [diff] [blame] | 124 | if (OK != OK_NoOutput) |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 125 | Out->keep(); |
| 126 | return true; |
| 127 | } |