blob: f42241ccf95ec4e58e1c8b74d5e125b4826c9705 [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"
Vedant Kumar775c7af2018-02-15 21:14:36 +000017#include "PassPrinters.h"
Chandler Carruth66445382014-01-11 08:16:35 +000018#include "llvm/ADT/StringRef.h"
Chandler Carruthedf59962016-02-18 09:45:17 +000019#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000020#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruthb7bdfd62014-01-13 07:38:24 +000021#include "llvm/Bitcode/BitcodeWriterPass.h"
Philip Pfaffe59d690b2017-08-04 09:28:09 +000022#include "llvm/Config/config.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000023#include "llvm/IR/Dominators.h"
Chandler Carruthb353c3f2014-01-13 05:16:45 +000024#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000025#include "llvm/IR/LLVMContext.h"
26#include "llvm/IR/Module.h"
27#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000028#include "llvm/IR/Verifier.h"
Chandler Carruth1ff77242015-03-07 09:02:36 +000029#include "llvm/Passes/PassBuilder.h"
Philip Pfaffe131fb972018-04-05 15:04:13 +000030#include "llvm/Passes/PassPlugin.h"
Chandler Carruth66445382014-01-11 08:16:35 +000031#include "llvm/Support/CommandLine.h"
Chandler Carruthb353c3f2014-01-13 05:16:45 +000032#include "llvm/Support/ErrorHandling.h"
Chandler Carruth66445382014-01-11 08:16:35 +000033#include "llvm/Support/ToolOutputFile.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000034#include "llvm/Target/TargetMachine.h"
Tim Shen6b4114182017-06-01 01:02:12 +000035#include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +000036#include "llvm/Transforms/Scalar/LoopPassManager.h"
Chandler Carruth66445382014-01-11 08:16:35 +000037
38using namespace llvm;
Chandler Carruth949282e2014-01-13 03:08:40 +000039using namespace opt_tool;
Chandler Carruth66445382014-01-11 08:16:35 +000040
Chandler Carruth14a759e2015-01-13 22:42:38 +000041static cl::opt<bool>
42 DebugPM("debug-pass-manager", cl::Hidden,
43 cl::desc("Print pass management debugging information"));
44
Philip Pfaffe131fb972018-04-05 15:04:13 +000045static cl::list<std::string>
46 PassPlugins("load-pass-plugin",
47 cl::desc("Load passes from plugin library"));
48
Chandler Carruthedf59962016-02-18 09:45:17 +000049// This flag specifies a textual description of the alias analysis pipeline to
50// use when querying for aliasing information. It only works in concert with
51// the "passes" flag above.
52static cl::opt<std::string>
53 AAPipeline("aa-pipeline",
54 cl::desc("A textual description of the alias analysis "
55 "pipeline for handling managed aliasing queries"),
56 cl::Hidden);
57
Philip Pfaffe730f2f92017-07-10 10:57:55 +000058/// {{@ These options accept textual pipeline descriptions which will be
59/// inserted into default pipelines at the respective extension points
60static cl::opt<std::string> PeepholeEPPipeline(
61 "passes-ep-peephole",
62 cl::desc("A textual description of the function pass pipeline inserted at "
63 "the Peephole extension points into default pipelines"),
64 cl::Hidden);
65static cl::opt<std::string> LateLoopOptimizationsEPPipeline(
66 "passes-ep-late-loop-optimizations",
67 cl::desc(
68 "A textual description of the loop pass pipeline inserted at "
69 "the LateLoopOptimizations extension point into default pipelines"),
70 cl::Hidden);
71static cl::opt<std::string> LoopOptimizerEndEPPipeline(
72 "passes-ep-loop-optimizer-end",
73 cl::desc("A textual description of the loop pass pipeline inserted at "
74 "the LoopOptimizerEnd extension point into default pipelines"),
75 cl::Hidden);
76static cl::opt<std::string> ScalarOptimizerLateEPPipeline(
77 "passes-ep-scalar-optimizer-late",
78 cl::desc("A textual description of the function pass pipeline inserted at "
79 "the ScalarOptimizerLate extension point into default pipelines"),
80 cl::Hidden);
81static cl::opt<std::string> CGSCCOptimizerLateEPPipeline(
82 "passes-ep-cgscc-optimizer-late",
83 cl::desc("A textual description of the cgscc pass pipeline inserted at "
84 "the CGSCCOptimizerLate extension point into default pipelines"),
85 cl::Hidden);
86static cl::opt<std::string> VectorizerStartEPPipeline(
87 "passes-ep-vectorizer-start",
88 cl::desc("A textual description of the function pass pipeline inserted at "
89 "the VectorizerStart extension point into default pipelines"),
90 cl::Hidden);
David Blaikie0c64f5a2018-01-23 01:25:20 +000091static cl::opt<std::string> PipelineStartEPPipeline(
92 "passes-ep-pipeline-start",
93 cl::desc("A textual description of the function pass pipeline inserted at "
94 "the PipelineStart extension point into default pipelines"),
95 cl::Hidden);
Dehao Chen7b05a272017-07-26 02:00:43 +000096enum PGOKind { NoPGO, InstrGen, InstrUse, SampleUse };
97static cl::opt<PGOKind> PGOKindFlag(
98 "pgo-kind", cl::init(NoPGO), cl::Hidden,
99 cl::desc("The kind of profile guided optimization"),
100 cl::values(clEnumValN(NoPGO, "nopgo", "Do not use PGO."),
101 clEnumValN(InstrGen, "new-pm-pgo-instr-gen-pipeline",
102 "Instrument the IR to generate profile."),
103 clEnumValN(InstrUse, "new-pm-pgo-instr-use-pipeline",
104 "Use instrumented profile to guide PGO."),
105 clEnumValN(SampleUse, "new-pm-pgo-sample-use-pipeline",
106 "Use sampled profile to guide PGO.")));
107static cl::opt<std::string> ProfileFile(
108 "profile-file", cl::desc("Path to the profile."), cl::Hidden);
Dehao Chene90d0152017-07-26 15:01:20 +0000109static cl::opt<bool> DebugInfoForProfiling(
110 "new-pm-debug-info-for-profiling", cl::init(false), cl::Hidden,
111 cl::desc("Emit special debug info to enable PGO profile generation."));
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000112/// @}}
113
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000114template <typename PassManagerT>
115bool tryParsePipelineText(PassBuilder &PB, StringRef PipelineText) {
116 if (PipelineText.empty())
117 return false;
118
119 // Verify the pipeline is parseable:
120 PassManagerT PM;
121 if (PB.parsePassPipeline(PM, PipelineText))
122 return true;
123
124 errs() << "Could not parse pipeline '" << PipelineText
125 << "'. I'm going to igore it.\n";
126 return false;
127}
128
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000129/// If one of the EPPipeline command line options was given, register callbacks
130/// for parsing and inserting the given pipeline
131static void registerEPCallbacks(PassBuilder &PB, bool VerifyEachPass,
132 bool DebugLogging) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000133 if (tryParsePipelineText<FunctionPassManager>(PB, PeepholeEPPipeline))
Philip Pfaffea3b84162017-07-10 12:48:51 +0000134 PB.registerPeepholeEPCallback([&PB, VerifyEachPass, DebugLogging](
135 FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000136 PB.parsePassPipeline(PM, PeepholeEPPipeline, VerifyEachPass,
137 DebugLogging);
Philip Pfaffea3b84162017-07-10 12:48:51 +0000138 });
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000139 if (tryParsePipelineText<LoopPassManager>(PB,
140 LateLoopOptimizationsEPPipeline))
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000141 PB.registerLateLoopOptimizationsEPCallback(
Philip Pfaffea3b84162017-07-10 12:48:51 +0000142 [&PB, VerifyEachPass, DebugLogging](
143 LoopPassManager &PM, PassBuilder::OptimizationLevel Level) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000144 PB.parsePassPipeline(PM, LateLoopOptimizationsEPPipeline,
145 VerifyEachPass, DebugLogging);
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000146 });
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000147 if (tryParsePipelineText<LoopPassManager>(PB, LoopOptimizerEndEPPipeline))
Philip Pfaffea3b84162017-07-10 12:48:51 +0000148 PB.registerLoopOptimizerEndEPCallback([&PB, VerifyEachPass, DebugLogging](
149 LoopPassManager &PM, PassBuilder::OptimizationLevel Level) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000150 PB.parsePassPipeline(PM, LoopOptimizerEndEPPipeline, VerifyEachPass,
151 DebugLogging);
Philip Pfaffea3b84162017-07-10 12:48:51 +0000152 });
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000153 if (tryParsePipelineText<FunctionPassManager>(PB,
154 ScalarOptimizerLateEPPipeline))
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000155 PB.registerScalarOptimizerLateEPCallback(
Philip Pfaffea3b84162017-07-10 12:48:51 +0000156 [&PB, VerifyEachPass, DebugLogging](
157 FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000158 PB.parsePassPipeline(PM, ScalarOptimizerLateEPPipeline,
159 VerifyEachPass, DebugLogging);
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000160 });
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000161 if (tryParsePipelineText<CGSCCPassManager>(PB, CGSCCOptimizerLateEPPipeline))
Philip Pfaffea3b84162017-07-10 12:48:51 +0000162 PB.registerCGSCCOptimizerLateEPCallback([&PB, VerifyEachPass, DebugLogging](
163 CGSCCPassManager &PM, PassBuilder::OptimizationLevel Level) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000164 PB.parsePassPipeline(PM, CGSCCOptimizerLateEPPipeline, VerifyEachPass,
165 DebugLogging);
Philip Pfaffea3b84162017-07-10 12:48:51 +0000166 });
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000167 if (tryParsePipelineText<FunctionPassManager>(PB, VectorizerStartEPPipeline))
Philip Pfaffea3b84162017-07-10 12:48:51 +0000168 PB.registerVectorizerStartEPCallback([&PB, VerifyEachPass, DebugLogging](
169 FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000170 PB.parsePassPipeline(PM, VectorizerStartEPPipeline, VerifyEachPass,
171 DebugLogging);
Philip Pfaffea3b84162017-07-10 12:48:51 +0000172 });
David Blaikie0c64f5a2018-01-23 01:25:20 +0000173 if (tryParsePipelineText<ModulePassManager>(PB, PipelineStartEPPipeline))
174 PB.registerPipelineStartEPCallback(
175 [&PB, VerifyEachPass, DebugLogging](ModulePassManager &PM) {
176 PB.parsePassPipeline(PM, PipelineStartEPPipeline, VerifyEachPass,
177 DebugLogging);
178 });
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000179}
180
Philip Pfaffe59d690b2017-08-04 09:28:09 +0000181#ifdef LINK_POLLY_INTO_TOOLS
182namespace polly {
183void RegisterPollyPasses(PassBuilder &);
184}
185#endif
186
Tim Shen6b4114182017-06-01 01:02:12 +0000187bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
Reid Kleckner3fc649c2017-09-23 01:03:17 +0000188 ToolOutputFile *Out, ToolOutputFile *ThinLTOLinkOut,
189 ToolOutputFile *OptRemarkFile,
Chandler Carruthe0385522015-02-01 10:11:22 +0000190 StringRef PassPipeline, OutputKind OK,
Duncan P. N. Exon Smith679db332015-04-15 00:34:24 +0000191 VerifierKind VK,
Duncan P. N. Exon Smith8a74f682015-04-15 02:38:06 +0000192 bool ShouldPreserveAssemblyUseListOrder,
Teresa Johnsonf93b2462016-08-12 13:53:02 +0000193 bool ShouldPreserveBitcodeUseListOrder,
Vedant Kumar775c7af2018-02-15 21:14:36 +0000194 bool EmitSummaryIndex, bool EmitModuleHash,
195 bool EnableDebugify) {
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000196 bool VerifyEachPass = VK == VK_VerifyEachPass;
Dehao Chen7b05a272017-07-26 02:00:43 +0000197
198 Optional<PGOOptions> P;
199 switch (PGOKindFlag) {
200 case InstrGen:
201 P = PGOOptions(ProfileFile, "", "", true);
202 break;
203 case InstrUse:
204 P = PGOOptions("", ProfileFile, "", false);
205 break;
206 case SampleUse:
207 P = PGOOptions("", "", ProfileFile, false);
208 break;
209 case NoPGO:
Dehao Chene90d0152017-07-26 15:01:20 +0000210 if (DebugInfoForProfiling)
211 P = PGOOptions("", "", "", false, true);
212 else
213 P = None;
Dehao Chen7b05a272017-07-26 02:00:43 +0000214 }
215 PassBuilder PB(TM, P);
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000216 registerEPCallbacks(PB, VerifyEachPass, DebugPM);
Chandler Carruth2dc92e92015-02-01 07:40:05 +0000217
Philip Pfaffe131fb972018-04-05 15:04:13 +0000218 // Load requested pass plugins and let them register pass builder callbacks
219 for (auto &PluginFN : PassPlugins) {
220 auto PassPlugin = PassPlugin::Load(PluginFN);
221 if (!PassPlugin) {
222 errs() << "Failed to load passes from '" << PluginFN
223 << "'. Request ignored.\n";
224 continue;
225 }
226
227 PassPlugin->registerPassBuilderCallbacks(PB);
228 }
229
Vedant Kumar775c7af2018-02-15 21:14:36 +0000230 // Register a callback that creates the debugify passes as needed.
231 PB.registerPipelineParsingCallback(
232 [](StringRef Name, ModulePassManager &MPM,
233 ArrayRef<PassBuilder::PipelineElement>) {
234 if (Name == "debugify") {
235 MPM.addPass(NewPMDebugifyPass());
236 return true;
237 } else if (Name == "check-debugify") {
238 MPM.addPass(NewPMCheckDebugifyPass());
239 return true;
240 }
241 return false;
242 });
243
Philip Pfaffe59d690b2017-08-04 09:28:09 +0000244#ifdef LINK_POLLY_INTO_TOOLS
245 polly::RegisterPollyPasses(PB);
246#endif
247
Chandler Carruthedf59962016-02-18 09:45:17 +0000248 // Specially handle the alias analysis manager so that we can register
249 // a custom pipeline of AA passes with it.
250 AAManager AA;
251 if (!PB.parseAAPipeline(AA, AAPipeline)) {
252 errs() << Arg0 << ": unable to parse AA pipeline description.\n";
253 return false;
254 }
255
Justin Bognereecc3c82016-02-25 07:23:08 +0000256 LoopAnalysisManager LAM(DebugPM);
Chandler Carruth14a759e2015-01-13 22:42:38 +0000257 FunctionAnalysisManager FAM(DebugPM);
258 CGSCCAnalysisManager CGAM(DebugPM);
259 ModuleAnalysisManager MAM(DebugPM);
Chandler Carruth4d356312014-01-20 11:34:08 +0000260
Chandler Carruthedf59962016-02-18 09:45:17 +0000261 // Register the AA manager first so that our version is the one used.
262 FAM.registerPass([&] { return std::move(AA); });
263
Chandler Carruthb70f6732015-01-06 02:21:37 +0000264 // Register all the basic analyses with the managers.
Chandler Carruth1ff77242015-03-07 09:02:36 +0000265 PB.registerModuleAnalyses(MAM);
266 PB.registerCGSCCAnalyses(CGAM);
267 PB.registerFunctionAnalyses(FAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000268 PB.registerLoopAnalyses(LAM);
Davide Italianob6ccd6b2016-05-14 23:21:50 +0000269 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
Chandler Carruthc68d0822014-02-06 04:25:13 +0000270
Chandler Carruth14a759e2015-01-13 22:42:38 +0000271 ModulePassManager MPM(DebugPM);
Chandler Carruth4d356312014-01-20 11:34:08 +0000272 if (VK > VK_NoVerifier)
273 MPM.addPass(VerifierPass());
Vedant Kumar775c7af2018-02-15 21:14:36 +0000274 if (EnableDebugify)
275 MPM.addPass(NewPMDebugifyPass());
Chandler Carruth4d356312014-01-20 11:34:08 +0000276
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000277 if (!PB.parsePassPipeline(MPM, PassPipeline, VerifyEachPass, DebugPM)) {
Chandler Carruth66445382014-01-11 08:16:35 +0000278 errs() << Arg0 << ": unable to parse pass pipeline description.\n";
279 return false;
280 }
281
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(NewPMCheckDebugifyPass());
Chandler Carruth4d356312014-01-20 11:34:08 +0000286
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000287 // Add any relevant output pass at the end of the pipeline.
288 switch (OK) {
289 case OK_NoOutput:
290 break; // No output pass needed.
291 case OK_OutputAssembly:
Duncan P. N. Exon Smith8a74f682015-04-15 02:38:06 +0000292 MPM.addPass(
293 PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder));
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000294 break;
295 case OK_OutputBitcode:
Teresa Johnsonf93b2462016-08-12 13:53:02 +0000296 MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder,
297 EmitSummaryIndex, EmitModuleHash));
Chandler Carruthb7bdfd62014-01-13 07:38:24 +0000298 break;
Tim Shen6b4114182017-06-01 01:02:12 +0000299 case OK_OutputThinLTOBitcode:
300 MPM.addPass(ThinLTOBitcodeWriterPass(
301 Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr));
302 break;
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000303 }
304
305 // Before executing passes, print the final values of the LLVM options.
306 cl::PrintOptionValues();
307
Chandler Carruth66445382014-01-11 08:16:35 +0000308 // Now that we have all of the passes ready, run them.
Chandler Carruthb47f8012016-03-11 11:05:24 +0000309 MPM.run(M, MAM);
Chandler Carruth66445382014-01-11 08:16:35 +0000310
311 // Declare success.
Tim Shen6b4114182017-06-01 01:02:12 +0000312 if (OK != OK_NoOutput) {
Chandler Carruth66445382014-01-11 08:16:35 +0000313 Out->keep();
Tim Shen6b4114182017-06-01 01:02:12 +0000314 if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut)
315 ThinLTOLinkOut->keep();
316 }
Sam Elliottb0c97532017-08-20 01:30:45 +0000317
318 if (OptRemarkFile)
319 OptRemarkFile->keep();
320
Chandler Carruth66445382014-01-11 08:16:35 +0000321 return true;
322}