blob: e2f9a06523a81c608f6f14c5d5ff5bae361c34ea [file] [log] [blame]
Chandler Carruth66445382014-01-11 08:16:35 +00001//===- 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 Sergeev662e5682018-09-24 16:08:15 +000017#include "Debugify.h"
Vedant Kumar775c7af2018-02-15 21:14:36 +000018#include "PassPrinters.h"
Chandler Carruth66445382014-01-11 08:16:35 +000019#include "llvm/ADT/StringRef.h"
Chandler Carruthedf59962016-02-18 09:45:17 +000020#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000021#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruthb7bdfd62014-01-13 07:38:24 +000022#include "llvm/Bitcode/BitcodeWriterPass.h"
Nico Weber432a3882018-04-30 14:59:11 +000023#include "llvm/Config/llvm-config.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000024#include "llvm/IR/Dominators.h"
Chandler Carruthb353c3f2014-01-13 05:16:45 +000025#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000026#include "llvm/IR/LLVMContext.h"
27#include "llvm/IR/Module.h"
28#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000029#include "llvm/IR/Verifier.h"
Chandler Carruth1ff77242015-03-07 09:02:36 +000030#include "llvm/Passes/PassBuilder.h"
Philip Pfaffe131fb972018-04-05 15:04:13 +000031#include "llvm/Passes/PassPlugin.h"
Fedor Sergeev662e5682018-09-24 16:08:15 +000032#include "llvm/Passes/StandardInstrumentations.h"
Chandler Carruth66445382014-01-11 08:16:35 +000033#include "llvm/Support/CommandLine.h"
Chandler Carruthb353c3f2014-01-13 05:16:45 +000034#include "llvm/Support/ErrorHandling.h"
Chandler Carruth66445382014-01-11 08:16:35 +000035#include "llvm/Support/ToolOutputFile.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000036#include "llvm/Target/TargetMachine.h"
Tim Shen6b4114182017-06-01 01:02:12 +000037#include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +000038#include "llvm/Transforms/Scalar/LoopPassManager.h"
Chandler Carruth66445382014-01-11 08:16:35 +000039
40using namespace llvm;
Chandler Carruth949282e2014-01-13 03:08:40 +000041using namespace opt_tool;
Chandler Carruth66445382014-01-11 08:16:35 +000042
Chandler Carruth14a759e2015-01-13 22:42:38 +000043static cl::opt<bool>
44 DebugPM("debug-pass-manager", cl::Hidden,
45 cl::desc("Print pass management debugging information"));
46
Philip Pfaffe131fb972018-04-05 15:04:13 +000047static cl::list<std::string>
48 PassPlugins("load-pass-plugin",
49 cl::desc("Load passes from plugin library"));
50
Chandler Carruthedf59962016-02-18 09:45:17 +000051// 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.
54static 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 Pfaffe730f2f92017-07-10 10:57:55 +000060/// {{@ These options accept textual pipeline descriptions which will be
61/// inserted into default pipelines at the respective extension points
62static 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);
67static 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);
73static 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);
78static 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);
83static 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);
88static 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 Blaikie0c64f5a2018-01-23 01:25:20 +000093static 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 Chen7b05a272017-07-26 02:00:43 +000098enum PGOKind { NoPGO, InstrGen, InstrUse, SampleUse };
99static 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.")));
109static cl::opt<std::string> ProfileFile(
110 "profile-file", cl::desc("Path to the profile."), cl::Hidden);
Richard Smith6c676622018-10-10 23:13:47 +0000111static cl::opt<std::string>
112 ProfileRemappingFile("profile-remapping-file",
113 cl::desc("Path to the profile remapping file."),
114 cl::Hidden);
Dehao Chene90d0152017-07-26 15:01:20 +0000115static 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 Pfaffe730f2f92017-07-10 10:57:55 +0000118/// @}}
119
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000120template <typename PassManagerT>
Fedor Sergeevbd6b2132018-10-17 10:36:23 +0000121bool tryParsePipelineText(PassBuilder &PB,
122 const cl::opt<std::string> &PipelineOpt) {
123 if (PipelineOpt.empty())
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000124 return false;
125
126 // Verify the pipeline is parseable:
127 PassManagerT PM;
Fedor Sergeevbd6b2132018-10-17 10:36:23 +0000128 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 Pfaffeb4d500f2017-07-11 11:17:44 +0000135}
136
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000137/// If one of the EPPipeline command line options was given, register callbacks
138/// for parsing and inserting the given pipeline
139static void registerEPCallbacks(PassBuilder &PB, bool VerifyEachPass,
140 bool DebugLogging) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000141 if (tryParsePipelineText<FunctionPassManager>(PB, PeepholeEPPipeline))
Fedor Sergeevbd6b2132018-10-17 10:36:23 +0000142 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 Pfaffeb4d500f2017-07-11 11:17:44 +0000149 if (tryParsePipelineText<LoopPassManager>(PB,
150 LateLoopOptimizationsEPPipeline))
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000151 PB.registerLateLoopOptimizationsEPCallback(
Philip Pfaffea3b84162017-07-10 12:48:51 +0000152 [&PB, VerifyEachPass, DebugLogging](
153 LoopPassManager &PM, PassBuilder::OptimizationLevel Level) {
Fedor Sergeevbd6b2132018-10-17 10:36:23 +0000154 ExitOnError Err("Unable to parse LateLoopOptimizationsEP pipeline: ");
155 Err(PB.parsePassPipeline(PM, LateLoopOptimizationsEPPipeline,
156 VerifyEachPass, DebugLogging));
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000157 });
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000158 if (tryParsePipelineText<LoopPassManager>(PB, LoopOptimizerEndEPPipeline))
Fedor Sergeevbd6b2132018-10-17 10:36:23 +0000159 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 Pfaffeb4d500f2017-07-11 11:17:44 +0000166 if (tryParsePipelineText<FunctionPassManager>(PB,
167 ScalarOptimizerLateEPPipeline))
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000168 PB.registerScalarOptimizerLateEPCallback(
Philip Pfaffea3b84162017-07-10 12:48:51 +0000169 [&PB, VerifyEachPass, DebugLogging](
170 FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
Fedor Sergeevbd6b2132018-10-17 10:36:23 +0000171 ExitOnError Err("Unable to parse ScalarOptimizerLateEP pipeline: ");
172 Err(PB.parsePassPipeline(PM, ScalarOptimizerLateEPPipeline,
173 VerifyEachPass, DebugLogging));
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000174 });
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000175 if (tryParsePipelineText<CGSCCPassManager>(PB, CGSCCOptimizerLateEPPipeline))
Fedor Sergeevbd6b2132018-10-17 10:36:23 +0000176 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 Pfaffeb4d500f2017-07-11 11:17:44 +0000183 if (tryParsePipelineText<FunctionPassManager>(PB, VectorizerStartEPPipeline))
Fedor Sergeevbd6b2132018-10-17 10:36:23 +0000184 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 Blaikie0c64f5a2018-01-23 01:25:20 +0000191 if (tryParsePipelineText<ModulePassManager>(PB, PipelineStartEPPipeline))
192 PB.registerPipelineStartEPCallback(
193 [&PB, VerifyEachPass, DebugLogging](ModulePassManager &PM) {
Fedor Sergeevbd6b2132018-10-17 10:36:23 +0000194 ExitOnError Err("Unable to parse PipelineStartEP pipeline: ");
195 Err(PB.parsePassPipeline(PM, PipelineStartEPPipeline, VerifyEachPass,
196 DebugLogging));
David Blaikie0c64f5a2018-01-23 01:25:20 +0000197 });
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000198}
199
Philip Pfaffe59d690b2017-08-04 09:28:09 +0000200#ifdef LINK_POLLY_INTO_TOOLS
201namespace polly {
202void RegisterPollyPasses(PassBuilder &);
203}
204#endif
205
Tim Shen6b4114182017-06-01 01:02:12 +0000206bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
Reid Kleckner3fc649c2017-09-23 01:03:17 +0000207 ToolOutputFile *Out, ToolOutputFile *ThinLTOLinkOut,
208 ToolOutputFile *OptRemarkFile,
Chandler Carruthe0385522015-02-01 10:11:22 +0000209 StringRef PassPipeline, OutputKind OK,
Duncan P. N. Exon Smith679db332015-04-15 00:34:24 +0000210 VerifierKind VK,
Duncan P. N. Exon Smith8a74f682015-04-15 02:38:06 +0000211 bool ShouldPreserveAssemblyUseListOrder,
Teresa Johnsonf93b2462016-08-12 13:53:02 +0000212 bool ShouldPreserveBitcodeUseListOrder,
Vedant Kumar775c7af2018-02-15 21:14:36 +0000213 bool EmitSummaryIndex, bool EmitModuleHash,
214 bool EnableDebugify) {
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000215 bool VerifyEachPass = VK == VK_VerifyEachPass;
Dehao Chen7b05a272017-07-26 02:00:43 +0000216
217 Optional<PGOOptions> P;
218 switch (PGOKindFlag) {
219 case InstrGen:
Richard Smith6c676622018-10-10 23:13:47 +0000220 P = PGOOptions(ProfileFile, "", "", "", true);
Dehao Chen7b05a272017-07-26 02:00:43 +0000221 break;
222 case InstrUse:
Richard Smith6c676622018-10-10 23:13:47 +0000223 P = PGOOptions("", ProfileFile, "", ProfileRemappingFile, false);
Dehao Chen7b05a272017-07-26 02:00:43 +0000224 break;
225 case SampleUse:
Richard Smith6c676622018-10-10 23:13:47 +0000226 P = PGOOptions("", "", ProfileFile, ProfileRemappingFile, false);
Dehao Chen7b05a272017-07-26 02:00:43 +0000227 break;
228 case NoPGO:
Dehao Chene90d0152017-07-26 15:01:20 +0000229 if (DebugInfoForProfiling)
Richard Smith6c676622018-10-10 23:13:47 +0000230 P = PGOOptions("", "", "", "", false, true);
Dehao Chene90d0152017-07-26 15:01:20 +0000231 else
232 P = None;
Dehao Chen7b05a272017-07-26 02:00:43 +0000233 }
Fedor Sergeev662e5682018-09-24 16:08:15 +0000234 PassInstrumentationCallbacks PIC;
235 StandardInstrumentations SI;
236 SI.registerCallbacks(PIC);
237
238 PassBuilder PB(TM, P, &PIC);
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000239 registerEPCallbacks(PB, VerifyEachPass, DebugPM);
Chandler Carruth2dc92e92015-02-01 07:40:05 +0000240
Philip Pfaffe131fb972018-04-05 15:04:13 +0000241 // 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 Kumar775c7af2018-02-15 21:14:36 +0000253 // 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 Pfaffe59d690b2017-08-04 09:28:09 +0000267#ifdef LINK_POLLY_INTO_TOOLS
268 polly::RegisterPollyPasses(PB);
269#endif
270
Chandler Carruthedf59962016-02-18 09:45:17 +0000271 // Specially handle the alias analysis manager so that we can register
272 // a custom pipeline of AA passes with it.
273 AAManager AA;
Fedor Sergeevbd6b2132018-10-17 10:36:23 +0000274 if (auto Err = PB.parseAAPipeline(AA, AAPipeline)) {
275 errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
Chandler Carruthedf59962016-02-18 09:45:17 +0000276 return false;
277 }
278
Justin Bognereecc3c82016-02-25 07:23:08 +0000279 LoopAnalysisManager LAM(DebugPM);
Chandler Carruth14a759e2015-01-13 22:42:38 +0000280 FunctionAnalysisManager FAM(DebugPM);
281 CGSCCAnalysisManager CGAM(DebugPM);
282 ModuleAnalysisManager MAM(DebugPM);
Chandler Carruth4d356312014-01-20 11:34:08 +0000283
Chandler Carruthedf59962016-02-18 09:45:17 +0000284 // Register the AA manager first so that our version is the one used.
285 FAM.registerPass([&] { return std::move(AA); });
286
Chandler Carruthb70f6732015-01-06 02:21:37 +0000287 // Register all the basic analyses with the managers.
Chandler Carruth1ff77242015-03-07 09:02:36 +0000288 PB.registerModuleAnalyses(MAM);
289 PB.registerCGSCCAnalyses(CGAM);
290 PB.registerFunctionAnalyses(FAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000291 PB.registerLoopAnalyses(LAM);
Davide Italianob6ccd6b2016-05-14 23:21:50 +0000292 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
Chandler Carruthc68d0822014-02-06 04:25:13 +0000293
Chandler Carruth14a759e2015-01-13 22:42:38 +0000294 ModulePassManager MPM(DebugPM);
Chandler Carruth4d356312014-01-20 11:34:08 +0000295 if (VK > VK_NoVerifier)
296 MPM.addPass(VerifierPass());
Vedant Kumar775c7af2018-02-15 21:14:36 +0000297 if (EnableDebugify)
298 MPM.addPass(NewPMDebugifyPass());
Chandler Carruth4d356312014-01-20 11:34:08 +0000299
Fedor Sergeevbd6b2132018-10-17 10:36:23 +0000300 if (auto Err =
301 PB.parsePassPipeline(MPM, PassPipeline, VerifyEachPass, DebugPM)) {
302 errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
Chandler Carruth66445382014-01-11 08:16:35 +0000303 return false;
304 }
305
Chandler Carruth4d356312014-01-20 11:34:08 +0000306 if (VK > VK_NoVerifier)
307 MPM.addPass(VerifierPass());
Vedant Kumar775c7af2018-02-15 21:14:36 +0000308 if (EnableDebugify)
309 MPM.addPass(NewPMCheckDebugifyPass());
Chandler Carruth4d356312014-01-20 11:34:08 +0000310
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000311 // 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 Smith8a74f682015-04-15 02:38:06 +0000316 MPM.addPass(
317 PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder));
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000318 break;
319 case OK_OutputBitcode:
Teresa Johnsonf93b2462016-08-12 13:53:02 +0000320 MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder,
321 EmitSummaryIndex, EmitModuleHash));
Chandler Carruthb7bdfd62014-01-13 07:38:24 +0000322 break;
Tim Shen6b4114182017-06-01 01:02:12 +0000323 case OK_OutputThinLTOBitcode:
324 MPM.addPass(ThinLTOBitcodeWriterPass(
325 Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr));
326 break;
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000327 }
328
329 // Before executing passes, print the final values of the LLVM options.
330 cl::PrintOptionValues();
331
Chandler Carruth66445382014-01-11 08:16:35 +0000332 // Now that we have all of the passes ready, run them.
Chandler Carruthb47f8012016-03-11 11:05:24 +0000333 MPM.run(M, MAM);
Chandler Carruth66445382014-01-11 08:16:35 +0000334
335 // Declare success.
Tim Shen6b4114182017-06-01 01:02:12 +0000336 if (OK != OK_NoOutput) {
Chandler Carruth66445382014-01-11 08:16:35 +0000337 Out->keep();
Tim Shen6b4114182017-06-01 01:02:12 +0000338 if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut)
339 ThinLTOLinkOut->keep();
340 }
Sam Elliottb0c97532017-08-20 01:30:45 +0000341
342 if (OptRemarkFile)
343 OptRemarkFile->keep();
344
Chandler Carruth66445382014-01-11 08:16:35 +0000345 return true;
346}