blob: f16981296d00db85df23ea256a8693dedd204c36 [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);
Philip Pfaffe2d4effb2018-11-12 11:17:07 +000098static cl::opt<std::string> OptimizerLastEPPipeline(
99 "passes-ep-optimizer-last",
100 cl::desc("A textual description of the function pass pipeline inserted at "
101 "the OptimizerLast extension point into default pipelines"),
102 cl::Hidden);
103
Wei Mic876e3d2019-01-16 23:19:02 +0000104extern cl::opt<PGOKind> PGOKindFlag;
105extern cl::opt<std::string> ProfileFile;
Richard Smith6c676622018-10-10 23:13:47 +0000106static cl::opt<std::string>
107 ProfileRemappingFile("profile-remapping-file",
108 cl::desc("Path to the profile remapping file."),
109 cl::Hidden);
Dehao Chene90d0152017-07-26 15:01:20 +0000110static cl::opt<bool> DebugInfoForProfiling(
111 "new-pm-debug-info-for-profiling", cl::init(false), cl::Hidden,
112 cl::desc("Emit special debug info to enable PGO profile generation."));
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000113/// @}}
114
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000115template <typename PassManagerT>
Fedor Sergeevbd6b2132018-10-17 10:36:23 +0000116bool tryParsePipelineText(PassBuilder &PB,
117 const cl::opt<std::string> &PipelineOpt) {
118 if (PipelineOpt.empty())
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000119 return false;
120
121 // Verify the pipeline is parseable:
122 PassManagerT PM;
Fedor Sergeevbd6b2132018-10-17 10:36:23 +0000123 if (auto Err = PB.parsePassPipeline(PM, PipelineOpt)) {
124 errs() << "Could not parse -" << PipelineOpt.ArgStr
125 << " pipeline: " << toString(std::move(Err))
126 << "... I'm going to ignore it.\n";
127 return false;
128 }
129 return true;
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000130}
131
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000132/// If one of the EPPipeline command line options was given, register callbacks
133/// for parsing and inserting the given pipeline
134static void registerEPCallbacks(PassBuilder &PB, bool VerifyEachPass,
135 bool DebugLogging) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000136 if (tryParsePipelineText<FunctionPassManager>(PB, PeepholeEPPipeline))
Fedor Sergeevbd6b2132018-10-17 10:36:23 +0000137 PB.registerPeepholeEPCallback(
138 [&PB, VerifyEachPass, DebugLogging](
139 FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
140 ExitOnError Err("Unable to parse PeepholeEP pipeline: ");
141 Err(PB.parsePassPipeline(PM, PeepholeEPPipeline, VerifyEachPass,
142 DebugLogging));
143 });
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000144 if (tryParsePipelineText<LoopPassManager>(PB,
145 LateLoopOptimizationsEPPipeline))
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000146 PB.registerLateLoopOptimizationsEPCallback(
Philip Pfaffea3b84162017-07-10 12:48:51 +0000147 [&PB, VerifyEachPass, DebugLogging](
148 LoopPassManager &PM, PassBuilder::OptimizationLevel Level) {
Fedor Sergeevbd6b2132018-10-17 10:36:23 +0000149 ExitOnError Err("Unable to parse LateLoopOptimizationsEP pipeline: ");
150 Err(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))
Fedor Sergeevbd6b2132018-10-17 10:36:23 +0000154 PB.registerLoopOptimizerEndEPCallback(
155 [&PB, VerifyEachPass, DebugLogging](
156 LoopPassManager &PM, PassBuilder::OptimizationLevel Level) {
157 ExitOnError Err("Unable to parse LoopOptimizerEndEP pipeline: ");
158 Err(PB.parsePassPipeline(PM, LoopOptimizerEndEPPipeline,
159 VerifyEachPass, DebugLogging));
160 });
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000161 if (tryParsePipelineText<FunctionPassManager>(PB,
162 ScalarOptimizerLateEPPipeline))
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000163 PB.registerScalarOptimizerLateEPCallback(
Philip Pfaffea3b84162017-07-10 12:48:51 +0000164 [&PB, VerifyEachPass, DebugLogging](
165 FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
Fedor Sergeevbd6b2132018-10-17 10:36:23 +0000166 ExitOnError Err("Unable to parse ScalarOptimizerLateEP pipeline: ");
167 Err(PB.parsePassPipeline(PM, ScalarOptimizerLateEPPipeline,
168 VerifyEachPass, DebugLogging));
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000169 });
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000170 if (tryParsePipelineText<CGSCCPassManager>(PB, CGSCCOptimizerLateEPPipeline))
Fedor Sergeevbd6b2132018-10-17 10:36:23 +0000171 PB.registerCGSCCOptimizerLateEPCallback(
172 [&PB, VerifyEachPass, DebugLogging](
173 CGSCCPassManager &PM, PassBuilder::OptimizationLevel Level) {
174 ExitOnError Err("Unable to parse CGSCCOptimizerLateEP pipeline: ");
175 Err(PB.parsePassPipeline(PM, CGSCCOptimizerLateEPPipeline,
176 VerifyEachPass, DebugLogging));
177 });
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000178 if (tryParsePipelineText<FunctionPassManager>(PB, VectorizerStartEPPipeline))
Fedor Sergeevbd6b2132018-10-17 10:36:23 +0000179 PB.registerVectorizerStartEPCallback(
180 [&PB, VerifyEachPass, DebugLogging](
181 FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
182 ExitOnError Err("Unable to parse VectorizerStartEP pipeline: ");
183 Err(PB.parsePassPipeline(PM, VectorizerStartEPPipeline,
184 VerifyEachPass, DebugLogging));
185 });
David Blaikie0c64f5a2018-01-23 01:25:20 +0000186 if (tryParsePipelineText<ModulePassManager>(PB, PipelineStartEPPipeline))
187 PB.registerPipelineStartEPCallback(
188 [&PB, VerifyEachPass, DebugLogging](ModulePassManager &PM) {
Fedor Sergeevbd6b2132018-10-17 10:36:23 +0000189 ExitOnError Err("Unable to parse PipelineStartEP pipeline: ");
190 Err(PB.parsePassPipeline(PM, PipelineStartEPPipeline, VerifyEachPass,
191 DebugLogging));
David Blaikie0c64f5a2018-01-23 01:25:20 +0000192 });
Philip Pfaffe2d4effb2018-11-12 11:17:07 +0000193 if (tryParsePipelineText<FunctionPassManager>(PB, OptimizerLastEPPipeline))
194 PB.registerOptimizerLastEPCallback(
195 [&PB, VerifyEachPass, DebugLogging](FunctionPassManager &PM,
196 PassBuilder::OptimizationLevel) {
Philip Pfaffee194c022018-11-12 12:27:58 +0000197 ExitOnError Err("Unable to parse OptimizerLastEP pipeline: ");
198 Err(PB.parsePassPipeline(PM, OptimizerLastEPPipeline, VerifyEachPass,
199 DebugLogging));
Philip Pfaffe2d4effb2018-11-12 11:17:07 +0000200 });
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000201}
202
Philip Pfaffe59d690b2017-08-04 09:28:09 +0000203#ifdef LINK_POLLY_INTO_TOOLS
204namespace polly {
205void RegisterPollyPasses(PassBuilder &);
206}
207#endif
208
Tim Shen6b4114182017-06-01 01:02:12 +0000209bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
Reid Kleckner3fc649c2017-09-23 01:03:17 +0000210 ToolOutputFile *Out, ToolOutputFile *ThinLTOLinkOut,
211 ToolOutputFile *OptRemarkFile,
Chandler Carruthe0385522015-02-01 10:11:22 +0000212 StringRef PassPipeline, OutputKind OK,
Duncan P. N. Exon Smith679db332015-04-15 00:34:24 +0000213 VerifierKind VK,
Duncan P. N. Exon Smith8a74f682015-04-15 02:38:06 +0000214 bool ShouldPreserveAssemblyUseListOrder,
Teresa Johnsonf93b2462016-08-12 13:53:02 +0000215 bool ShouldPreserveBitcodeUseListOrder,
Vedant Kumar775c7af2018-02-15 21:14:36 +0000216 bool EmitSummaryIndex, bool EmitModuleHash,
217 bool EnableDebugify) {
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000218 bool VerifyEachPass = VK == VK_VerifyEachPass;
Dehao Chen7b05a272017-07-26 02:00:43 +0000219
220 Optional<PGOOptions> P;
221 switch (PGOKindFlag) {
222 case InstrGen:
Richard Smith6c676622018-10-10 23:13:47 +0000223 P = PGOOptions(ProfileFile, "", "", "", true);
Dehao Chen7b05a272017-07-26 02:00:43 +0000224 break;
225 case InstrUse:
Richard Smith6c676622018-10-10 23:13:47 +0000226 P = PGOOptions("", ProfileFile, "", ProfileRemappingFile, false);
Dehao Chen7b05a272017-07-26 02:00:43 +0000227 break;
228 case SampleUse:
Richard Smith6c676622018-10-10 23:13:47 +0000229 P = PGOOptions("", "", ProfileFile, ProfileRemappingFile, false);
Dehao Chen7b05a272017-07-26 02:00:43 +0000230 break;
231 case NoPGO:
Dehao Chene90d0152017-07-26 15:01:20 +0000232 if (DebugInfoForProfiling)
Richard Smith6c676622018-10-10 23:13:47 +0000233 P = PGOOptions("", "", "", "", false, true);
Dehao Chene90d0152017-07-26 15:01:20 +0000234 else
235 P = None;
Dehao Chen7b05a272017-07-26 02:00:43 +0000236 }
Fedor Sergeev662e5682018-09-24 16:08:15 +0000237 PassInstrumentationCallbacks PIC;
238 StandardInstrumentations SI;
239 SI.registerCallbacks(PIC);
240
241 PassBuilder PB(TM, P, &PIC);
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000242 registerEPCallbacks(PB, VerifyEachPass, DebugPM);
Chandler Carruth2dc92e92015-02-01 07:40:05 +0000243
Philip Pfaffe131fb972018-04-05 15:04:13 +0000244 // Load requested pass plugins and let them register pass builder callbacks
245 for (auto &PluginFN : PassPlugins) {
246 auto PassPlugin = PassPlugin::Load(PluginFN);
247 if (!PassPlugin) {
248 errs() << "Failed to load passes from '" << PluginFN
249 << "'. Request ignored.\n";
250 continue;
251 }
252
253 PassPlugin->registerPassBuilderCallbacks(PB);
254 }
255
Vedant Kumar775c7af2018-02-15 21:14:36 +0000256 // Register a callback that creates the debugify passes as needed.
257 PB.registerPipelineParsingCallback(
258 [](StringRef Name, ModulePassManager &MPM,
259 ArrayRef<PassBuilder::PipelineElement>) {
260 if (Name == "debugify") {
261 MPM.addPass(NewPMDebugifyPass());
262 return true;
263 } else if (Name == "check-debugify") {
264 MPM.addPass(NewPMCheckDebugifyPass());
265 return true;
266 }
267 return false;
268 });
269
Philip Pfaffe59d690b2017-08-04 09:28:09 +0000270#ifdef LINK_POLLY_INTO_TOOLS
271 polly::RegisterPollyPasses(PB);
272#endif
273
Chandler Carruthedf59962016-02-18 09:45:17 +0000274 // Specially handle the alias analysis manager so that we can register
275 // a custom pipeline of AA passes with it.
276 AAManager AA;
Fedor Sergeevbd6b2132018-10-17 10:36:23 +0000277 if (auto Err = PB.parseAAPipeline(AA, AAPipeline)) {
278 errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
Chandler Carruthedf59962016-02-18 09:45:17 +0000279 return false;
280 }
281
Justin Bognereecc3c82016-02-25 07:23:08 +0000282 LoopAnalysisManager LAM(DebugPM);
Chandler Carruth14a759e2015-01-13 22:42:38 +0000283 FunctionAnalysisManager FAM(DebugPM);
284 CGSCCAnalysisManager CGAM(DebugPM);
285 ModuleAnalysisManager MAM(DebugPM);
Chandler Carruth4d356312014-01-20 11:34:08 +0000286
Chandler Carruthedf59962016-02-18 09:45:17 +0000287 // Register the AA manager first so that our version is the one used.
288 FAM.registerPass([&] { return std::move(AA); });
289
Chandler Carruthb70f6732015-01-06 02:21:37 +0000290 // Register all the basic analyses with the managers.
Chandler Carruth1ff77242015-03-07 09:02:36 +0000291 PB.registerModuleAnalyses(MAM);
292 PB.registerCGSCCAnalyses(CGAM);
293 PB.registerFunctionAnalyses(FAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000294 PB.registerLoopAnalyses(LAM);
Davide Italianob6ccd6b2016-05-14 23:21:50 +0000295 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
Chandler Carruthc68d0822014-02-06 04:25:13 +0000296
Chandler Carruth14a759e2015-01-13 22:42:38 +0000297 ModulePassManager MPM(DebugPM);
Chandler Carruth4d356312014-01-20 11:34:08 +0000298 if (VK > VK_NoVerifier)
299 MPM.addPass(VerifierPass());
Vedant Kumar775c7af2018-02-15 21:14:36 +0000300 if (EnableDebugify)
301 MPM.addPass(NewPMDebugifyPass());
Chandler Carruth4d356312014-01-20 11:34:08 +0000302
Fedor Sergeevbd6b2132018-10-17 10:36:23 +0000303 if (auto Err =
304 PB.parsePassPipeline(MPM, PassPipeline, VerifyEachPass, DebugPM)) {
305 errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
Chandler Carruth66445382014-01-11 08:16:35 +0000306 return false;
307 }
308
Chandler Carruth4d356312014-01-20 11:34:08 +0000309 if (VK > VK_NoVerifier)
310 MPM.addPass(VerifierPass());
Vedant Kumar775c7af2018-02-15 21:14:36 +0000311 if (EnableDebugify)
312 MPM.addPass(NewPMCheckDebugifyPass());
Chandler Carruth4d356312014-01-20 11:34:08 +0000313
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000314 // Add any relevant output pass at the end of the pipeline.
315 switch (OK) {
316 case OK_NoOutput:
317 break; // No output pass needed.
318 case OK_OutputAssembly:
Duncan P. N. Exon Smith8a74f682015-04-15 02:38:06 +0000319 MPM.addPass(
320 PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder));
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000321 break;
322 case OK_OutputBitcode:
Teresa Johnsonf93b2462016-08-12 13:53:02 +0000323 MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder,
324 EmitSummaryIndex, EmitModuleHash));
Chandler Carruthb7bdfd62014-01-13 07:38:24 +0000325 break;
Tim Shen6b4114182017-06-01 01:02:12 +0000326 case OK_OutputThinLTOBitcode:
327 MPM.addPass(ThinLTOBitcodeWriterPass(
328 Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr));
329 break;
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000330 }
331
332 // Before executing passes, print the final values of the LLVM options.
333 cl::PrintOptionValues();
334
Chandler Carruth66445382014-01-11 08:16:35 +0000335 // Now that we have all of the passes ready, run them.
Chandler Carruthb47f8012016-03-11 11:05:24 +0000336 MPM.run(M, MAM);
Chandler Carruth66445382014-01-11 08:16:35 +0000337
338 // Declare success.
Tim Shen6b4114182017-06-01 01:02:12 +0000339 if (OK != OK_NoOutput) {
Chandler Carruth66445382014-01-11 08:16:35 +0000340 Out->keep();
Tim Shen6b4114182017-06-01 01:02:12 +0000341 if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut)
342 ThinLTOLinkOut->keep();
343 }
Sam Elliottb0c97532017-08-20 01:30:45 +0000344
345 if (OptRemarkFile)
346 OptRemarkFile->keep();
347
Chandler Carruth66445382014-01-11 08:16:35 +0000348 return true;
349}