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" |
Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 17 | #include "Debugify.h" |
Vedant Kumar | 775c7af | 2018-02-15 21:14:36 +0000 | [diff] [blame] | 18 | #include "PassPrinters.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringRef.h" |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/AliasAnalysis.h" |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/CGSCCPassManager.h" |
Chandler Carruth | b7bdfd6 | 2014-01-13 07:38:24 +0000 | [diff] [blame] | 22 | #include "llvm/Bitcode/BitcodeWriterPass.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 23 | #include "llvm/Config/llvm-config.h" |
Chandler Carruth | 64764b4 | 2015-01-14 10:19:28 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 25 | #include "llvm/IR/IRPrintingPasses.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 26 | #include "llvm/IR/LLVMContext.h" |
| 27 | #include "llvm/IR/Module.h" |
| 28 | #include "llvm/IR/PassManager.h" |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Verifier.h" |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 30 | #include "llvm/Passes/PassBuilder.h" |
Philip Pfaffe | 131fb97 | 2018-04-05 15:04:13 +0000 | [diff] [blame] | 31 | #include "llvm/Passes/PassPlugin.h" |
Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 32 | #include "llvm/Passes/StandardInstrumentations.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 34 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ToolOutputFile.h" |
Chandler Carruth | e038552 | 2015-02-01 10:11:22 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetMachine.h" |
Tim Shen | 6b411418 | 2017-06-01 01:02:12 +0000 | [diff] [blame] | 37 | #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h" |
Chandler Carruth | 3bab7e1 | 2017-01-11 09:43:56 +0000 | [diff] [blame] | 38 | #include "llvm/Transforms/Scalar/LoopPassManager.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace llvm; |
Chandler Carruth | 949282e | 2014-01-13 03:08:40 +0000 | [diff] [blame] | 41 | using namespace opt_tool; |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 42 | |
Chandler Carruth | 14a759e | 2015-01-13 22:42:38 +0000 | [diff] [blame] | 43 | static cl::opt<bool> |
| 44 | DebugPM("debug-pass-manager", cl::Hidden, |
| 45 | cl::desc("Print pass management debugging information")); |
| 46 | |
Philip Pfaffe | 131fb97 | 2018-04-05 15:04:13 +0000 | [diff] [blame] | 47 | static cl::list<std::string> |
| 48 | PassPlugins("load-pass-plugin", |
| 49 | cl::desc("Load passes from plugin library")); |
| 50 | |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 51 | // This flag specifies a textual description of the alias analysis pipeline to |
| 52 | // use when querying for aliasing information. It only works in concert with |
| 53 | // the "passes" flag above. |
| 54 | static cl::opt<std::string> |
| 55 | AAPipeline("aa-pipeline", |
| 56 | cl::desc("A textual description of the alias analysis " |
| 57 | "pipeline for handling managed aliasing queries"), |
| 58 | cl::Hidden); |
| 59 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 60 | /// {{@ These options accept textual pipeline descriptions which will be |
| 61 | /// inserted into default pipelines at the respective extension points |
| 62 | static cl::opt<std::string> PeepholeEPPipeline( |
| 63 | "passes-ep-peephole", |
| 64 | cl::desc("A textual description of the function pass pipeline inserted at " |
| 65 | "the Peephole extension points into default pipelines"), |
| 66 | cl::Hidden); |
| 67 | static cl::opt<std::string> LateLoopOptimizationsEPPipeline( |
| 68 | "passes-ep-late-loop-optimizations", |
| 69 | cl::desc( |
| 70 | "A textual description of the loop pass pipeline inserted at " |
| 71 | "the LateLoopOptimizations extension point into default pipelines"), |
| 72 | cl::Hidden); |
| 73 | static cl::opt<std::string> LoopOptimizerEndEPPipeline( |
| 74 | "passes-ep-loop-optimizer-end", |
| 75 | cl::desc("A textual description of the loop pass pipeline inserted at " |
| 76 | "the LoopOptimizerEnd extension point into default pipelines"), |
| 77 | cl::Hidden); |
| 78 | static cl::opt<std::string> ScalarOptimizerLateEPPipeline( |
| 79 | "passes-ep-scalar-optimizer-late", |
| 80 | cl::desc("A textual description of the function pass pipeline inserted at " |
| 81 | "the ScalarOptimizerLate extension point into default pipelines"), |
| 82 | cl::Hidden); |
| 83 | static cl::opt<std::string> CGSCCOptimizerLateEPPipeline( |
| 84 | "passes-ep-cgscc-optimizer-late", |
| 85 | cl::desc("A textual description of the cgscc pass pipeline inserted at " |
| 86 | "the CGSCCOptimizerLate extension point into default pipelines"), |
| 87 | cl::Hidden); |
| 88 | static cl::opt<std::string> VectorizerStartEPPipeline( |
| 89 | "passes-ep-vectorizer-start", |
| 90 | cl::desc("A textual description of the function pass pipeline inserted at " |
| 91 | "the VectorizerStart extension point into default pipelines"), |
| 92 | cl::Hidden); |
David Blaikie | 0c64f5a | 2018-01-23 01:25:20 +0000 | [diff] [blame] | 93 | static cl::opt<std::string> PipelineStartEPPipeline( |
| 94 | "passes-ep-pipeline-start", |
| 95 | cl::desc("A textual description of the function pass pipeline inserted at " |
| 96 | "the PipelineStart extension point into default pipelines"), |
| 97 | cl::Hidden); |
Dehao Chen | 7b05a27 | 2017-07-26 02:00:43 +0000 | [diff] [blame] | 98 | enum PGOKind { NoPGO, InstrGen, InstrUse, SampleUse }; |
| 99 | static cl::opt<PGOKind> PGOKindFlag( |
| 100 | "pgo-kind", cl::init(NoPGO), cl::Hidden, |
| 101 | cl::desc("The kind of profile guided optimization"), |
| 102 | cl::values(clEnumValN(NoPGO, "nopgo", "Do not use PGO."), |
| 103 | clEnumValN(InstrGen, "new-pm-pgo-instr-gen-pipeline", |
| 104 | "Instrument the IR to generate profile."), |
| 105 | clEnumValN(InstrUse, "new-pm-pgo-instr-use-pipeline", |
| 106 | "Use instrumented profile to guide PGO."), |
| 107 | clEnumValN(SampleUse, "new-pm-pgo-sample-use-pipeline", |
| 108 | "Use sampled profile to guide PGO."))); |
| 109 | static cl::opt<std::string> ProfileFile( |
| 110 | "profile-file", cl::desc("Path to the profile."), cl::Hidden); |
Richard Smith | 6c67662 | 2018-10-10 23:13:47 +0000 | [diff] [blame] | 111 | static cl::opt<std::string> |
| 112 | ProfileRemappingFile("profile-remapping-file", |
| 113 | cl::desc("Path to the profile remapping file."), |
| 114 | cl::Hidden); |
Dehao Chen | e90d015 | 2017-07-26 15:01:20 +0000 | [diff] [blame] | 115 | static cl::opt<bool> DebugInfoForProfiling( |
| 116 | "new-pm-debug-info-for-profiling", cl::init(false), cl::Hidden, |
| 117 | cl::desc("Emit special debug info to enable PGO profile generation.")); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 118 | /// @}} |
| 119 | |
Philip Pfaffe | b4d500f | 2017-07-11 11:17:44 +0000 | [diff] [blame] | 120 | template <typename PassManagerT> |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame^] | 121 | bool tryParsePipelineText(PassBuilder &PB, |
| 122 | const cl::opt<std::string> &PipelineOpt) { |
| 123 | if (PipelineOpt.empty()) |
Philip Pfaffe | b4d500f | 2017-07-11 11:17:44 +0000 | [diff] [blame] | 124 | return false; |
| 125 | |
| 126 | // Verify the pipeline is parseable: |
| 127 | PassManagerT PM; |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame^] | 128 | if (auto Err = PB.parsePassPipeline(PM, PipelineOpt)) { |
| 129 | errs() << "Could not parse -" << PipelineOpt.ArgStr |
| 130 | << " pipeline: " << toString(std::move(Err)) |
| 131 | << "... I'm going to ignore it.\n"; |
| 132 | return false; |
| 133 | } |
| 134 | return true; |
Philip Pfaffe | b4d500f | 2017-07-11 11:17:44 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 137 | /// If one of the EPPipeline command line options was given, register callbacks |
| 138 | /// for parsing and inserting the given pipeline |
| 139 | static void registerEPCallbacks(PassBuilder &PB, bool VerifyEachPass, |
| 140 | bool DebugLogging) { |
Philip Pfaffe | b4d500f | 2017-07-11 11:17:44 +0000 | [diff] [blame] | 141 | if (tryParsePipelineText<FunctionPassManager>(PB, PeepholeEPPipeline)) |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame^] | 142 | PB.registerPeepholeEPCallback( |
| 143 | [&PB, VerifyEachPass, DebugLogging]( |
| 144 | FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) { |
| 145 | ExitOnError Err("Unable to parse PeepholeEP pipeline: "); |
| 146 | Err(PB.parsePassPipeline(PM, PeepholeEPPipeline, VerifyEachPass, |
| 147 | DebugLogging)); |
| 148 | }); |
Philip Pfaffe | b4d500f | 2017-07-11 11:17:44 +0000 | [diff] [blame] | 149 | if (tryParsePipelineText<LoopPassManager>(PB, |
| 150 | LateLoopOptimizationsEPPipeline)) |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 151 | PB.registerLateLoopOptimizationsEPCallback( |
Philip Pfaffe | a3b8416 | 2017-07-10 12:48:51 +0000 | [diff] [blame] | 152 | [&PB, VerifyEachPass, DebugLogging]( |
| 153 | LoopPassManager &PM, PassBuilder::OptimizationLevel Level) { |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame^] | 154 | ExitOnError Err("Unable to parse LateLoopOptimizationsEP pipeline: "); |
| 155 | Err(PB.parsePassPipeline(PM, LateLoopOptimizationsEPPipeline, |
| 156 | VerifyEachPass, DebugLogging)); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 157 | }); |
Philip Pfaffe | b4d500f | 2017-07-11 11:17:44 +0000 | [diff] [blame] | 158 | if (tryParsePipelineText<LoopPassManager>(PB, LoopOptimizerEndEPPipeline)) |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame^] | 159 | PB.registerLoopOptimizerEndEPCallback( |
| 160 | [&PB, VerifyEachPass, DebugLogging]( |
| 161 | LoopPassManager &PM, PassBuilder::OptimizationLevel Level) { |
| 162 | ExitOnError Err("Unable to parse LoopOptimizerEndEP pipeline: "); |
| 163 | Err(PB.parsePassPipeline(PM, LoopOptimizerEndEPPipeline, |
| 164 | VerifyEachPass, DebugLogging)); |
| 165 | }); |
Philip Pfaffe | b4d500f | 2017-07-11 11:17:44 +0000 | [diff] [blame] | 166 | if (tryParsePipelineText<FunctionPassManager>(PB, |
| 167 | ScalarOptimizerLateEPPipeline)) |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 168 | PB.registerScalarOptimizerLateEPCallback( |
Philip Pfaffe | a3b8416 | 2017-07-10 12:48:51 +0000 | [diff] [blame] | 169 | [&PB, VerifyEachPass, DebugLogging]( |
| 170 | FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) { |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame^] | 171 | ExitOnError Err("Unable to parse ScalarOptimizerLateEP pipeline: "); |
| 172 | Err(PB.parsePassPipeline(PM, ScalarOptimizerLateEPPipeline, |
| 173 | VerifyEachPass, DebugLogging)); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 174 | }); |
Philip Pfaffe | b4d500f | 2017-07-11 11:17:44 +0000 | [diff] [blame] | 175 | if (tryParsePipelineText<CGSCCPassManager>(PB, CGSCCOptimizerLateEPPipeline)) |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame^] | 176 | PB.registerCGSCCOptimizerLateEPCallback( |
| 177 | [&PB, VerifyEachPass, DebugLogging]( |
| 178 | CGSCCPassManager &PM, PassBuilder::OptimizationLevel Level) { |
| 179 | ExitOnError Err("Unable to parse CGSCCOptimizerLateEP pipeline: "); |
| 180 | Err(PB.parsePassPipeline(PM, CGSCCOptimizerLateEPPipeline, |
| 181 | VerifyEachPass, DebugLogging)); |
| 182 | }); |
Philip Pfaffe | b4d500f | 2017-07-11 11:17:44 +0000 | [diff] [blame] | 183 | if (tryParsePipelineText<FunctionPassManager>(PB, VectorizerStartEPPipeline)) |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame^] | 184 | PB.registerVectorizerStartEPCallback( |
| 185 | [&PB, VerifyEachPass, DebugLogging]( |
| 186 | FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) { |
| 187 | ExitOnError Err("Unable to parse VectorizerStartEP pipeline: "); |
| 188 | Err(PB.parsePassPipeline(PM, VectorizerStartEPPipeline, |
| 189 | VerifyEachPass, DebugLogging)); |
| 190 | }); |
David Blaikie | 0c64f5a | 2018-01-23 01:25:20 +0000 | [diff] [blame] | 191 | if (tryParsePipelineText<ModulePassManager>(PB, PipelineStartEPPipeline)) |
| 192 | PB.registerPipelineStartEPCallback( |
| 193 | [&PB, VerifyEachPass, DebugLogging](ModulePassManager &PM) { |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame^] | 194 | ExitOnError Err("Unable to parse PipelineStartEP pipeline: "); |
| 195 | Err(PB.parsePassPipeline(PM, PipelineStartEPPipeline, VerifyEachPass, |
| 196 | DebugLogging)); |
David Blaikie | 0c64f5a | 2018-01-23 01:25:20 +0000 | [diff] [blame] | 197 | }); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Philip Pfaffe | 59d690b | 2017-08-04 09:28:09 +0000 | [diff] [blame] | 200 | #ifdef LINK_POLLY_INTO_TOOLS |
| 201 | namespace polly { |
| 202 | void RegisterPollyPasses(PassBuilder &); |
| 203 | } |
| 204 | #endif |
| 205 | |
Tim Shen | 6b411418 | 2017-06-01 01:02:12 +0000 | [diff] [blame] | 206 | bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM, |
Reid Kleckner | 3fc649c | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 207 | ToolOutputFile *Out, ToolOutputFile *ThinLTOLinkOut, |
| 208 | ToolOutputFile *OptRemarkFile, |
Chandler Carruth | e038552 | 2015-02-01 10:11:22 +0000 | [diff] [blame] | 209 | StringRef PassPipeline, OutputKind OK, |
Duncan P. N. Exon Smith | 679db33 | 2015-04-15 00:34:24 +0000 | [diff] [blame] | 210 | VerifierKind VK, |
Duncan P. N. Exon Smith | 8a74f68 | 2015-04-15 02:38:06 +0000 | [diff] [blame] | 211 | bool ShouldPreserveAssemblyUseListOrder, |
Teresa Johnson | f93b246 | 2016-08-12 13:53:02 +0000 | [diff] [blame] | 212 | bool ShouldPreserveBitcodeUseListOrder, |
Vedant Kumar | 775c7af | 2018-02-15 21:14:36 +0000 | [diff] [blame] | 213 | bool EmitSummaryIndex, bool EmitModuleHash, |
| 214 | bool EnableDebugify) { |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 215 | bool VerifyEachPass = VK == VK_VerifyEachPass; |
Dehao Chen | 7b05a27 | 2017-07-26 02:00:43 +0000 | [diff] [blame] | 216 | |
| 217 | Optional<PGOOptions> P; |
| 218 | switch (PGOKindFlag) { |
| 219 | case InstrGen: |
Richard Smith | 6c67662 | 2018-10-10 23:13:47 +0000 | [diff] [blame] | 220 | P = PGOOptions(ProfileFile, "", "", "", true); |
Dehao Chen | 7b05a27 | 2017-07-26 02:00:43 +0000 | [diff] [blame] | 221 | break; |
| 222 | case InstrUse: |
Richard Smith | 6c67662 | 2018-10-10 23:13:47 +0000 | [diff] [blame] | 223 | P = PGOOptions("", ProfileFile, "", ProfileRemappingFile, false); |
Dehao Chen | 7b05a27 | 2017-07-26 02:00:43 +0000 | [diff] [blame] | 224 | break; |
| 225 | case SampleUse: |
Richard Smith | 6c67662 | 2018-10-10 23:13:47 +0000 | [diff] [blame] | 226 | P = PGOOptions("", "", ProfileFile, ProfileRemappingFile, false); |
Dehao Chen | 7b05a27 | 2017-07-26 02:00:43 +0000 | [diff] [blame] | 227 | break; |
| 228 | case NoPGO: |
Dehao Chen | e90d015 | 2017-07-26 15:01:20 +0000 | [diff] [blame] | 229 | if (DebugInfoForProfiling) |
Richard Smith | 6c67662 | 2018-10-10 23:13:47 +0000 | [diff] [blame] | 230 | P = PGOOptions("", "", "", "", false, true); |
Dehao Chen | e90d015 | 2017-07-26 15:01:20 +0000 | [diff] [blame] | 231 | else |
| 232 | P = None; |
Dehao Chen | 7b05a27 | 2017-07-26 02:00:43 +0000 | [diff] [blame] | 233 | } |
Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 234 | PassInstrumentationCallbacks PIC; |
| 235 | StandardInstrumentations SI; |
| 236 | SI.registerCallbacks(PIC); |
| 237 | |
| 238 | PassBuilder PB(TM, P, &PIC); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 239 | registerEPCallbacks(PB, VerifyEachPass, DebugPM); |
Chandler Carruth | 2dc92e9 | 2015-02-01 07:40:05 +0000 | [diff] [blame] | 240 | |
Philip Pfaffe | 131fb97 | 2018-04-05 15:04:13 +0000 | [diff] [blame] | 241 | // Load requested pass plugins and let them register pass builder callbacks |
| 242 | for (auto &PluginFN : PassPlugins) { |
| 243 | auto PassPlugin = PassPlugin::Load(PluginFN); |
| 244 | if (!PassPlugin) { |
| 245 | errs() << "Failed to load passes from '" << PluginFN |
| 246 | << "'. Request ignored.\n"; |
| 247 | continue; |
| 248 | } |
| 249 | |
| 250 | PassPlugin->registerPassBuilderCallbacks(PB); |
| 251 | } |
| 252 | |
Vedant Kumar | 775c7af | 2018-02-15 21:14:36 +0000 | [diff] [blame] | 253 | // Register a callback that creates the debugify passes as needed. |
| 254 | PB.registerPipelineParsingCallback( |
| 255 | [](StringRef Name, ModulePassManager &MPM, |
| 256 | ArrayRef<PassBuilder::PipelineElement>) { |
| 257 | if (Name == "debugify") { |
| 258 | MPM.addPass(NewPMDebugifyPass()); |
| 259 | return true; |
| 260 | } else if (Name == "check-debugify") { |
| 261 | MPM.addPass(NewPMCheckDebugifyPass()); |
| 262 | return true; |
| 263 | } |
| 264 | return false; |
| 265 | }); |
| 266 | |
Philip Pfaffe | 59d690b | 2017-08-04 09:28:09 +0000 | [diff] [blame] | 267 | #ifdef LINK_POLLY_INTO_TOOLS |
| 268 | polly::RegisterPollyPasses(PB); |
| 269 | #endif |
| 270 | |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 271 | // Specially handle the alias analysis manager so that we can register |
| 272 | // a custom pipeline of AA passes with it. |
| 273 | AAManager AA; |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame^] | 274 | if (auto Err = PB.parseAAPipeline(AA, AAPipeline)) { |
| 275 | errs() << Arg0 << ": " << toString(std::move(Err)) << "\n"; |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 276 | return false; |
| 277 | } |
| 278 | |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 279 | LoopAnalysisManager LAM(DebugPM); |
Chandler Carruth | 14a759e | 2015-01-13 22:42:38 +0000 | [diff] [blame] | 280 | FunctionAnalysisManager FAM(DebugPM); |
| 281 | CGSCCAnalysisManager CGAM(DebugPM); |
| 282 | ModuleAnalysisManager MAM(DebugPM); |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 283 | |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 284 | // Register the AA manager first so that our version is the one used. |
| 285 | FAM.registerPass([&] { return std::move(AA); }); |
| 286 | |
Chandler Carruth | b70f673 | 2015-01-06 02:21:37 +0000 | [diff] [blame] | 287 | // Register all the basic analyses with the managers. |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 288 | PB.registerModuleAnalyses(MAM); |
| 289 | PB.registerCGSCCAnalyses(CGAM); |
| 290 | PB.registerFunctionAnalyses(FAM); |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 291 | PB.registerLoopAnalyses(LAM); |
Davide Italiano | b6ccd6b | 2016-05-14 23:21:50 +0000 | [diff] [blame] | 292 | PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); |
Chandler Carruth | c68d082 | 2014-02-06 04:25:13 +0000 | [diff] [blame] | 293 | |
Chandler Carruth | 14a759e | 2015-01-13 22:42:38 +0000 | [diff] [blame] | 294 | ModulePassManager MPM(DebugPM); |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 295 | if (VK > VK_NoVerifier) |
| 296 | MPM.addPass(VerifierPass()); |
Vedant Kumar | 775c7af | 2018-02-15 21:14:36 +0000 | [diff] [blame] | 297 | if (EnableDebugify) |
| 298 | MPM.addPass(NewPMDebugifyPass()); |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 299 | |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame^] | 300 | if (auto Err = |
| 301 | PB.parsePassPipeline(MPM, PassPipeline, VerifyEachPass, DebugPM)) { |
| 302 | errs() << Arg0 << ": " << toString(std::move(Err)) << "\n"; |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 303 | return false; |
| 304 | } |
| 305 | |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 306 | if (VK > VK_NoVerifier) |
| 307 | MPM.addPass(VerifierPass()); |
Vedant Kumar | 775c7af | 2018-02-15 21:14:36 +0000 | [diff] [blame] | 308 | if (EnableDebugify) |
| 309 | MPM.addPass(NewPMCheckDebugifyPass()); |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 310 | |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 311 | // Add any relevant output pass at the end of the pipeline. |
| 312 | switch (OK) { |
| 313 | case OK_NoOutput: |
| 314 | break; // No output pass needed. |
| 315 | case OK_OutputAssembly: |
Duncan P. N. Exon Smith | 8a74f68 | 2015-04-15 02:38:06 +0000 | [diff] [blame] | 316 | MPM.addPass( |
| 317 | PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder)); |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 318 | break; |
| 319 | case OK_OutputBitcode: |
Teresa Johnson | f93b246 | 2016-08-12 13:53:02 +0000 | [diff] [blame] | 320 | MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder, |
| 321 | EmitSummaryIndex, EmitModuleHash)); |
Chandler Carruth | b7bdfd6 | 2014-01-13 07:38:24 +0000 | [diff] [blame] | 322 | break; |
Tim Shen | 6b411418 | 2017-06-01 01:02:12 +0000 | [diff] [blame] | 323 | case OK_OutputThinLTOBitcode: |
| 324 | MPM.addPass(ThinLTOBitcodeWriterPass( |
| 325 | Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr)); |
| 326 | break; |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | // Before executing passes, print the final values of the LLVM options. |
| 330 | cl::PrintOptionValues(); |
| 331 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 332 | // Now that we have all of the passes ready, run them. |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 333 | MPM.run(M, MAM); |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 334 | |
| 335 | // Declare success. |
Tim Shen | 6b411418 | 2017-06-01 01:02:12 +0000 | [diff] [blame] | 336 | if (OK != OK_NoOutput) { |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 337 | Out->keep(); |
Tim Shen | 6b411418 | 2017-06-01 01:02:12 +0000 | [diff] [blame] | 338 | if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut) |
| 339 | ThinLTOLinkOut->keep(); |
| 340 | } |
Sam Elliott | b0c9753 | 2017-08-20 01:30:45 +0000 | [diff] [blame] | 341 | |
| 342 | if (OptRemarkFile) |
| 343 | OptRemarkFile->keep(); |
| 344 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 345 | return true; |
| 346 | } |