blob: e63547a79d0ebae53284389aad8ba017178f2813 [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>
121bool tryParsePipelineText(PassBuilder &PB, StringRef PipelineText) {
122 if (PipelineText.empty())
123 return false;
124
125 // Verify the pipeline is parseable:
126 PassManagerT PM;
127 if (PB.parsePassPipeline(PM, PipelineText))
128 return true;
129
130 errs() << "Could not parse pipeline '" << PipelineText
131 << "'. I'm going to igore it.\n";
132 return false;
133}
134
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000135/// If one of the EPPipeline command line options was given, register callbacks
136/// for parsing and inserting the given pipeline
137static void registerEPCallbacks(PassBuilder &PB, bool VerifyEachPass,
138 bool DebugLogging) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000139 if (tryParsePipelineText<FunctionPassManager>(PB, PeepholeEPPipeline))
Philip Pfaffea3b84162017-07-10 12:48:51 +0000140 PB.registerPeepholeEPCallback([&PB, VerifyEachPass, DebugLogging](
141 FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000142 PB.parsePassPipeline(PM, PeepholeEPPipeline, VerifyEachPass,
143 DebugLogging);
Philip Pfaffea3b84162017-07-10 12:48:51 +0000144 });
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000145 if (tryParsePipelineText<LoopPassManager>(PB,
146 LateLoopOptimizationsEPPipeline))
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000147 PB.registerLateLoopOptimizationsEPCallback(
Philip Pfaffea3b84162017-07-10 12:48:51 +0000148 [&PB, VerifyEachPass, DebugLogging](
149 LoopPassManager &PM, PassBuilder::OptimizationLevel Level) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000150 PB.parsePassPipeline(PM, LateLoopOptimizationsEPPipeline,
151 VerifyEachPass, DebugLogging);
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000152 });
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000153 if (tryParsePipelineText<LoopPassManager>(PB, LoopOptimizerEndEPPipeline))
Philip Pfaffea3b84162017-07-10 12:48:51 +0000154 PB.registerLoopOptimizerEndEPCallback([&PB, VerifyEachPass, DebugLogging](
155 LoopPassManager &PM, PassBuilder::OptimizationLevel Level) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000156 PB.parsePassPipeline(PM, LoopOptimizerEndEPPipeline, VerifyEachPass,
157 DebugLogging);
Philip Pfaffea3b84162017-07-10 12:48:51 +0000158 });
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000159 if (tryParsePipelineText<FunctionPassManager>(PB,
160 ScalarOptimizerLateEPPipeline))
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000161 PB.registerScalarOptimizerLateEPCallback(
Philip Pfaffea3b84162017-07-10 12:48:51 +0000162 [&PB, VerifyEachPass, DebugLogging](
163 FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000164 PB.parsePassPipeline(PM, ScalarOptimizerLateEPPipeline,
165 VerifyEachPass, DebugLogging);
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000166 });
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000167 if (tryParsePipelineText<CGSCCPassManager>(PB, CGSCCOptimizerLateEPPipeline))
Philip Pfaffea3b84162017-07-10 12:48:51 +0000168 PB.registerCGSCCOptimizerLateEPCallback([&PB, VerifyEachPass, DebugLogging](
169 CGSCCPassManager &PM, PassBuilder::OptimizationLevel Level) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000170 PB.parsePassPipeline(PM, CGSCCOptimizerLateEPPipeline, VerifyEachPass,
171 DebugLogging);
Philip Pfaffea3b84162017-07-10 12:48:51 +0000172 });
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000173 if (tryParsePipelineText<FunctionPassManager>(PB, VectorizerStartEPPipeline))
Philip Pfaffea3b84162017-07-10 12:48:51 +0000174 PB.registerVectorizerStartEPCallback([&PB, VerifyEachPass, DebugLogging](
175 FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000176 PB.parsePassPipeline(PM, VectorizerStartEPPipeline, VerifyEachPass,
177 DebugLogging);
Philip Pfaffea3b84162017-07-10 12:48:51 +0000178 });
David Blaikie0c64f5a2018-01-23 01:25:20 +0000179 if (tryParsePipelineText<ModulePassManager>(PB, PipelineStartEPPipeline))
180 PB.registerPipelineStartEPCallback(
181 [&PB, VerifyEachPass, DebugLogging](ModulePassManager &PM) {
182 PB.parsePassPipeline(PM, PipelineStartEPPipeline, VerifyEachPass,
183 DebugLogging);
184 });
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000185}
186
Philip Pfaffe59d690b2017-08-04 09:28:09 +0000187#ifdef LINK_POLLY_INTO_TOOLS
188namespace polly {
189void RegisterPollyPasses(PassBuilder &);
190}
191#endif
192
Tim Shen6b4114182017-06-01 01:02:12 +0000193bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
Reid Kleckner3fc649c2017-09-23 01:03:17 +0000194 ToolOutputFile *Out, ToolOutputFile *ThinLTOLinkOut,
195 ToolOutputFile *OptRemarkFile,
Chandler Carruthe0385522015-02-01 10:11:22 +0000196 StringRef PassPipeline, OutputKind OK,
Duncan P. N. Exon Smith679db332015-04-15 00:34:24 +0000197 VerifierKind VK,
Duncan P. N. Exon Smith8a74f682015-04-15 02:38:06 +0000198 bool ShouldPreserveAssemblyUseListOrder,
Teresa Johnsonf93b2462016-08-12 13:53:02 +0000199 bool ShouldPreserveBitcodeUseListOrder,
Vedant Kumar775c7af2018-02-15 21:14:36 +0000200 bool EmitSummaryIndex, bool EmitModuleHash,
201 bool EnableDebugify) {
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000202 bool VerifyEachPass = VK == VK_VerifyEachPass;
Dehao Chen7b05a272017-07-26 02:00:43 +0000203
204 Optional<PGOOptions> P;
205 switch (PGOKindFlag) {
206 case InstrGen:
Richard Smith6c676622018-10-10 23:13:47 +0000207 P = PGOOptions(ProfileFile, "", "", "", true);
Dehao Chen7b05a272017-07-26 02:00:43 +0000208 break;
209 case InstrUse:
Richard Smith6c676622018-10-10 23:13:47 +0000210 P = PGOOptions("", ProfileFile, "", ProfileRemappingFile, false);
Dehao Chen7b05a272017-07-26 02:00:43 +0000211 break;
212 case SampleUse:
Richard Smith6c676622018-10-10 23:13:47 +0000213 P = PGOOptions("", "", ProfileFile, ProfileRemappingFile, false);
Dehao Chen7b05a272017-07-26 02:00:43 +0000214 break;
215 case NoPGO:
Dehao Chene90d0152017-07-26 15:01:20 +0000216 if (DebugInfoForProfiling)
Richard Smith6c676622018-10-10 23:13:47 +0000217 P = PGOOptions("", "", "", "", false, true);
Dehao Chene90d0152017-07-26 15:01:20 +0000218 else
219 P = None;
Dehao Chen7b05a272017-07-26 02:00:43 +0000220 }
Fedor Sergeev662e5682018-09-24 16:08:15 +0000221 PassInstrumentationCallbacks PIC;
222 StandardInstrumentations SI;
223 SI.registerCallbacks(PIC);
224
225 PassBuilder PB(TM, P, &PIC);
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000226 registerEPCallbacks(PB, VerifyEachPass, DebugPM);
Chandler Carruth2dc92e92015-02-01 07:40:05 +0000227
Philip Pfaffe131fb972018-04-05 15:04:13 +0000228 // Load requested pass plugins and let them register pass builder callbacks
229 for (auto &PluginFN : PassPlugins) {
230 auto PassPlugin = PassPlugin::Load(PluginFN);
231 if (!PassPlugin) {
232 errs() << "Failed to load passes from '" << PluginFN
233 << "'. Request ignored.\n";
234 continue;
235 }
236
237 PassPlugin->registerPassBuilderCallbacks(PB);
238 }
239
Vedant Kumar775c7af2018-02-15 21:14:36 +0000240 // Register a callback that creates the debugify passes as needed.
241 PB.registerPipelineParsingCallback(
242 [](StringRef Name, ModulePassManager &MPM,
243 ArrayRef<PassBuilder::PipelineElement>) {
244 if (Name == "debugify") {
245 MPM.addPass(NewPMDebugifyPass());
246 return true;
247 } else if (Name == "check-debugify") {
248 MPM.addPass(NewPMCheckDebugifyPass());
249 return true;
250 }
251 return false;
252 });
253
Philip Pfaffe59d690b2017-08-04 09:28:09 +0000254#ifdef LINK_POLLY_INTO_TOOLS
255 polly::RegisterPollyPasses(PB);
256#endif
257
Chandler Carruthedf59962016-02-18 09:45:17 +0000258 // Specially handle the alias analysis manager so that we can register
259 // a custom pipeline of AA passes with it.
260 AAManager AA;
261 if (!PB.parseAAPipeline(AA, AAPipeline)) {
262 errs() << Arg0 << ": unable to parse AA pipeline description.\n";
263 return false;
264 }
265
Justin Bognereecc3c82016-02-25 07:23:08 +0000266 LoopAnalysisManager LAM(DebugPM);
Chandler Carruth14a759e2015-01-13 22:42:38 +0000267 FunctionAnalysisManager FAM(DebugPM);
268 CGSCCAnalysisManager CGAM(DebugPM);
269 ModuleAnalysisManager MAM(DebugPM);
Chandler Carruth4d356312014-01-20 11:34:08 +0000270
Chandler Carruthedf59962016-02-18 09:45:17 +0000271 // Register the AA manager first so that our version is the one used.
272 FAM.registerPass([&] { return std::move(AA); });
273
Chandler Carruthb70f6732015-01-06 02:21:37 +0000274 // Register all the basic analyses with the managers.
Chandler Carruth1ff77242015-03-07 09:02:36 +0000275 PB.registerModuleAnalyses(MAM);
276 PB.registerCGSCCAnalyses(CGAM);
277 PB.registerFunctionAnalyses(FAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000278 PB.registerLoopAnalyses(LAM);
Davide Italianob6ccd6b2016-05-14 23:21:50 +0000279 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
Chandler Carruthc68d0822014-02-06 04:25:13 +0000280
Chandler Carruth14a759e2015-01-13 22:42:38 +0000281 ModulePassManager MPM(DebugPM);
Chandler Carruth4d356312014-01-20 11:34:08 +0000282 if (VK > VK_NoVerifier)
283 MPM.addPass(VerifierPass());
Vedant Kumar775c7af2018-02-15 21:14:36 +0000284 if (EnableDebugify)
285 MPM.addPass(NewPMDebugifyPass());
Chandler Carruth4d356312014-01-20 11:34:08 +0000286
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000287 if (!PB.parsePassPipeline(MPM, PassPipeline, VerifyEachPass, DebugPM)) {
Chandler Carruth66445382014-01-11 08:16:35 +0000288 errs() << Arg0 << ": unable to parse pass pipeline description.\n";
289 return false;
290 }
291
Chandler Carruth4d356312014-01-20 11:34:08 +0000292 if (VK > VK_NoVerifier)
293 MPM.addPass(VerifierPass());
Vedant Kumar775c7af2018-02-15 21:14:36 +0000294 if (EnableDebugify)
295 MPM.addPass(NewPMCheckDebugifyPass());
Chandler Carruth4d356312014-01-20 11:34:08 +0000296
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000297 // Add any relevant output pass at the end of the pipeline.
298 switch (OK) {
299 case OK_NoOutput:
300 break; // No output pass needed.
301 case OK_OutputAssembly:
Duncan P. N. Exon Smith8a74f682015-04-15 02:38:06 +0000302 MPM.addPass(
303 PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder));
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000304 break;
305 case OK_OutputBitcode:
Teresa Johnsonf93b2462016-08-12 13:53:02 +0000306 MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder,
307 EmitSummaryIndex, EmitModuleHash));
Chandler Carruthb7bdfd62014-01-13 07:38:24 +0000308 break;
Tim Shen6b4114182017-06-01 01:02:12 +0000309 case OK_OutputThinLTOBitcode:
310 MPM.addPass(ThinLTOBitcodeWriterPass(
311 Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr));
312 break;
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000313 }
314
315 // Before executing passes, print the final values of the LLVM options.
316 cl::PrintOptionValues();
317
Chandler Carruth66445382014-01-11 08:16:35 +0000318 // Now that we have all of the passes ready, run them.
Chandler Carruthb47f8012016-03-11 11:05:24 +0000319 MPM.run(M, MAM);
Chandler Carruth66445382014-01-11 08:16:35 +0000320
321 // Declare success.
Tim Shen6b4114182017-06-01 01:02:12 +0000322 if (OK != OK_NoOutput) {
Chandler Carruth66445382014-01-11 08:16:35 +0000323 Out->keep();
Tim Shen6b4114182017-06-01 01:02:12 +0000324 if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut)
325 ThinLTOLinkOut->keep();
326 }
Sam Elliottb0c97532017-08-20 01:30:45 +0000327
328 if (OptRemarkFile)
329 OptRemarkFile->keep();
330
Chandler Carruth66445382014-01-11 08:16:35 +0000331 return true;
332}