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" |
Chandler Carruth | b7bdfd6 | 2014-01-13 07:38:24 +0000 | [diff] [blame] | 20 | #include "llvm/Bitcode/BitcodeWriterPass.h" |
Chandler Carruth | 64764b4 | 2015-01-14 10:19:28 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 22 | #include "llvm/IR/IRPrintingPasses.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 23 | #include "llvm/IR/LLVMContext.h" |
| 24 | #include "llvm/IR/Module.h" |
| 25 | #include "llvm/IR/PassManager.h" |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Verifier.h" |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 27 | #include "llvm/Passes/PassBuilder.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ToolOutputFile.h" |
Chandler Carruth | e038552 | 2015-02-01 10:11:22 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetMachine.h" |
Tim Shen | 6b411418 | 2017-06-01 01:02:12 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h" |
Chandler Carruth | 3bab7e1 | 2017-01-11 09:43:56 +0000 | [diff] [blame] | 33 | #include "llvm/Transforms/Scalar/LoopPassManager.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 34 | |
| 35 | using namespace llvm; |
Chandler Carruth | 949282e | 2014-01-13 03:08:40 +0000 | [diff] [blame] | 36 | using namespace opt_tool; |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 37 | |
Chandler Carruth | 14a759e | 2015-01-13 22:42:38 +0000 | [diff] [blame] | 38 | static cl::opt<bool> |
| 39 | DebugPM("debug-pass-manager", cl::Hidden, |
| 40 | cl::desc("Print pass management debugging information")); |
| 41 | |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 42 | // This flag specifies a textual description of the alias analysis pipeline to |
| 43 | // use when querying for aliasing information. It only works in concert with |
| 44 | // the "passes" flag above. |
| 45 | static cl::opt<std::string> |
| 46 | AAPipeline("aa-pipeline", |
| 47 | cl::desc("A textual description of the alias analysis " |
| 48 | "pipeline for handling managed aliasing queries"), |
| 49 | cl::Hidden); |
| 50 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 51 | /// {{@ These options accept textual pipeline descriptions which will be |
| 52 | /// inserted into default pipelines at the respective extension points |
| 53 | static cl::opt<std::string> PeepholeEPPipeline( |
| 54 | "passes-ep-peephole", |
| 55 | cl::desc("A textual description of the function pass pipeline inserted at " |
| 56 | "the Peephole extension points into default pipelines"), |
| 57 | cl::Hidden); |
| 58 | static cl::opt<std::string> LateLoopOptimizationsEPPipeline( |
| 59 | "passes-ep-late-loop-optimizations", |
| 60 | cl::desc( |
| 61 | "A textual description of the loop pass pipeline inserted at " |
| 62 | "the LateLoopOptimizations extension point into default pipelines"), |
| 63 | cl::Hidden); |
| 64 | static cl::opt<std::string> LoopOptimizerEndEPPipeline( |
| 65 | "passes-ep-loop-optimizer-end", |
| 66 | cl::desc("A textual description of the loop pass pipeline inserted at " |
| 67 | "the LoopOptimizerEnd extension point into default pipelines"), |
| 68 | cl::Hidden); |
| 69 | static cl::opt<std::string> ScalarOptimizerLateEPPipeline( |
| 70 | "passes-ep-scalar-optimizer-late", |
| 71 | cl::desc("A textual description of the function pass pipeline inserted at " |
| 72 | "the ScalarOptimizerLate extension point into default pipelines"), |
| 73 | cl::Hidden); |
| 74 | static cl::opt<std::string> CGSCCOptimizerLateEPPipeline( |
| 75 | "passes-ep-cgscc-optimizer-late", |
| 76 | cl::desc("A textual description of the cgscc pass pipeline inserted at " |
| 77 | "the CGSCCOptimizerLate extension point into default pipelines"), |
| 78 | cl::Hidden); |
| 79 | static cl::opt<std::string> VectorizerStartEPPipeline( |
| 80 | "passes-ep-vectorizer-start", |
| 81 | cl::desc("A textual description of the function pass pipeline inserted at " |
| 82 | "the VectorizerStart extension point into default pipelines"), |
| 83 | cl::Hidden); |
| 84 | /// @}} |
| 85 | |
| 86 | /// If one of the EPPipeline command line options was given, register callbacks |
| 87 | /// for parsing and inserting the given pipeline |
| 88 | static void registerEPCallbacks(PassBuilder &PB, bool VerifyEachPass, |
| 89 | bool DebugLogging) { |
| 90 | if (!PeepholeEPPipeline.empty()) |
Philip Pfaffe | a3b8416 | 2017-07-10 12:48:51 +0000 | [diff] [blame^] | 91 | PB.registerPeepholeEPCallback([&PB, VerifyEachPass, DebugLogging]( |
| 92 | FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) { |
| 93 | return PB.parsePassPipeline(PM, PeepholeEPPipeline, VerifyEachPass, |
| 94 | DebugPM); |
| 95 | }); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 96 | if (!LateLoopOptimizationsEPPipeline.empty()) |
| 97 | PB.registerLateLoopOptimizationsEPCallback( |
Philip Pfaffe | a3b8416 | 2017-07-10 12:48:51 +0000 | [diff] [blame^] | 98 | [&PB, VerifyEachPass, DebugLogging]( |
| 99 | LoopPassManager &PM, PassBuilder::OptimizationLevel Level) { |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 100 | return PB.parsePassPipeline(PM, LateLoopOptimizationsEPPipeline, |
| 101 | VerifyEachPass, DebugPM); |
| 102 | }); |
| 103 | if (!LoopOptimizerEndEPPipeline.empty()) |
Philip Pfaffe | a3b8416 | 2017-07-10 12:48:51 +0000 | [diff] [blame^] | 104 | PB.registerLoopOptimizerEndEPCallback([&PB, VerifyEachPass, DebugLogging]( |
| 105 | LoopPassManager &PM, PassBuilder::OptimizationLevel Level) { |
| 106 | return PB.parsePassPipeline(PM, LoopOptimizerEndEPPipeline, |
| 107 | VerifyEachPass, DebugPM); |
| 108 | }); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 109 | if (!ScalarOptimizerLateEPPipeline.empty()) |
| 110 | PB.registerScalarOptimizerLateEPCallback( |
Philip Pfaffe | a3b8416 | 2017-07-10 12:48:51 +0000 | [diff] [blame^] | 111 | [&PB, VerifyEachPass, DebugLogging]( |
| 112 | FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) { |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 113 | return PB.parsePassPipeline(PM, ScalarOptimizerLateEPPipeline, |
| 114 | VerifyEachPass, DebugPM); |
| 115 | }); |
| 116 | if (!CGSCCOptimizerLateEPPipeline.empty()) |
Philip Pfaffe | a3b8416 | 2017-07-10 12:48:51 +0000 | [diff] [blame^] | 117 | PB.registerCGSCCOptimizerLateEPCallback([&PB, VerifyEachPass, DebugLogging]( |
| 118 | CGSCCPassManager &PM, PassBuilder::OptimizationLevel Level) { |
| 119 | return PB.parsePassPipeline(PM, CGSCCOptimizerLateEPPipeline, |
| 120 | VerifyEachPass, DebugPM); |
| 121 | }); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 122 | if (!VectorizerStartEPPipeline.empty()) |
Philip Pfaffe | a3b8416 | 2017-07-10 12:48:51 +0000 | [diff] [blame^] | 123 | PB.registerVectorizerStartEPCallback([&PB, VerifyEachPass, DebugLogging]( |
| 124 | FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) { |
| 125 | return PB.parsePassPipeline(PM, VectorizerStartEPPipeline, VerifyEachPass, |
| 126 | DebugPM); |
| 127 | }); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Tim Shen | 6b411418 | 2017-06-01 01:02:12 +0000 | [diff] [blame] | 130 | bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM, |
| 131 | tool_output_file *Out, |
| 132 | tool_output_file *ThinLTOLinkOut, |
Chandler Carruth | e038552 | 2015-02-01 10:11:22 +0000 | [diff] [blame] | 133 | StringRef PassPipeline, OutputKind OK, |
Duncan P. N. Exon Smith | 679db33 | 2015-04-15 00:34:24 +0000 | [diff] [blame] | 134 | VerifierKind VK, |
Duncan P. N. Exon Smith | 8a74f68 | 2015-04-15 02:38:06 +0000 | [diff] [blame] | 135 | bool ShouldPreserveAssemblyUseListOrder, |
Teresa Johnson | f93b246 | 2016-08-12 13:53:02 +0000 | [diff] [blame] | 136 | bool ShouldPreserveBitcodeUseListOrder, |
| 137 | bool EmitSummaryIndex, bool EmitModuleHash) { |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 138 | bool VerifyEachPass = VK == VK_VerifyEachPass; |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 139 | PassBuilder PB(TM); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 140 | registerEPCallbacks(PB, VerifyEachPass, DebugPM); |
Chandler Carruth | 2dc92e9 | 2015-02-01 07:40:05 +0000 | [diff] [blame] | 141 | |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 142 | // Specially handle the alias analysis manager so that we can register |
| 143 | // a custom pipeline of AA passes with it. |
| 144 | AAManager AA; |
| 145 | if (!PB.parseAAPipeline(AA, AAPipeline)) { |
| 146 | errs() << Arg0 << ": unable to parse AA pipeline description.\n"; |
| 147 | return false; |
| 148 | } |
| 149 | |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 150 | LoopAnalysisManager LAM(DebugPM); |
Chandler Carruth | 14a759e | 2015-01-13 22:42:38 +0000 | [diff] [blame] | 151 | FunctionAnalysisManager FAM(DebugPM); |
| 152 | CGSCCAnalysisManager CGAM(DebugPM); |
| 153 | ModuleAnalysisManager MAM(DebugPM); |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 154 | |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 155 | // Register the AA manager first so that our version is the one used. |
| 156 | FAM.registerPass([&] { return std::move(AA); }); |
| 157 | |
Chandler Carruth | b70f673 | 2015-01-06 02:21:37 +0000 | [diff] [blame] | 158 | // Register all the basic analyses with the managers. |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 159 | PB.registerModuleAnalyses(MAM); |
| 160 | PB.registerCGSCCAnalyses(CGAM); |
| 161 | PB.registerFunctionAnalyses(FAM); |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 162 | PB.registerLoopAnalyses(LAM); |
Davide Italiano | b6ccd6b | 2016-05-14 23:21:50 +0000 | [diff] [blame] | 163 | PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); |
Chandler Carruth | c68d082 | 2014-02-06 04:25:13 +0000 | [diff] [blame] | 164 | |
Chandler Carruth | 14a759e | 2015-01-13 22:42:38 +0000 | [diff] [blame] | 165 | ModulePassManager MPM(DebugPM); |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 166 | if (VK > VK_NoVerifier) |
| 167 | MPM.addPass(VerifierPass()); |
| 168 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 169 | if (!PB.parsePassPipeline(MPM, PassPipeline, VerifyEachPass, DebugPM)) { |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 170 | errs() << Arg0 << ": unable to parse pass pipeline description.\n"; |
| 171 | return false; |
| 172 | } |
| 173 | |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 174 | if (VK > VK_NoVerifier) |
| 175 | MPM.addPass(VerifierPass()); |
| 176 | |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 177 | // Add any relevant output pass at the end of the pipeline. |
| 178 | switch (OK) { |
| 179 | case OK_NoOutput: |
| 180 | break; // No output pass needed. |
| 181 | case OK_OutputAssembly: |
Duncan P. N. Exon Smith | 8a74f68 | 2015-04-15 02:38:06 +0000 | [diff] [blame] | 182 | MPM.addPass( |
| 183 | PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder)); |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 184 | break; |
| 185 | case OK_OutputBitcode: |
Teresa Johnson | f93b246 | 2016-08-12 13:53:02 +0000 | [diff] [blame] | 186 | MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder, |
| 187 | EmitSummaryIndex, EmitModuleHash)); |
Chandler Carruth | b7bdfd6 | 2014-01-13 07:38:24 +0000 | [diff] [blame] | 188 | break; |
Tim Shen | 6b411418 | 2017-06-01 01:02:12 +0000 | [diff] [blame] | 189 | case OK_OutputThinLTOBitcode: |
| 190 | MPM.addPass(ThinLTOBitcodeWriterPass( |
| 191 | Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr)); |
| 192 | break; |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // Before executing passes, print the final values of the LLVM options. |
| 196 | cl::PrintOptionValues(); |
| 197 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 198 | // Now that we have all of the passes ready, run them. |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 199 | MPM.run(M, MAM); |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 200 | |
| 201 | // Declare success. |
Tim Shen | 6b411418 | 2017-06-01 01:02:12 +0000 | [diff] [blame] | 202 | if (OK != OK_NoOutput) { |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 203 | Out->keep(); |
Tim Shen | 6b411418 | 2017-06-01 01:02:12 +0000 | [diff] [blame] | 204 | if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut) |
| 205 | ThinLTOLinkOut->keep(); |
| 206 | } |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 207 | return true; |
| 208 | } |