xref: /freebsd/contrib/llvm-project/llvm/tools/opt/NewPMDriver.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
10b57cec5SDimitry Andric //===- NewPMDriver.cpp - Driver for opt with new PM -----------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric /// \file
90b57cec5SDimitry Andric ///
100b57cec5SDimitry Andric /// This file is just a split of the code that logically belongs in opt.cpp but
110b57cec5SDimitry Andric /// that includes the new pass manager headers.
120b57cec5SDimitry Andric ///
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "NewPMDriver.h"
165ffd83dbSDimitry Andric #include "llvm/ADT/SmallVector.h"
170b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
180b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
190b57cec5SDimitry Andric #include "llvm/Analysis/CGSCCPassManager.h"
20e8d8bef9SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
210b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeWriterPass.h"
220b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
230b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
240b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h"
250b57cec5SDimitry Andric #include "llvm/IR/Module.h"
260b57cec5SDimitry Andric #include "llvm/IR/PassManager.h"
270b57cec5SDimitry Andric #include "llvm/IR/Verifier.h"
28*bdd1243dSDimitry Andric #include "llvm/IRPrinter/IRPrintingPasses.h"
290b57cec5SDimitry Andric #include "llvm/Passes/PassBuilder.h"
300b57cec5SDimitry Andric #include "llvm/Passes/PassPlugin.h"
310b57cec5SDimitry Andric #include "llvm/Passes/StandardInstrumentations.h"
320b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
330b57cec5SDimitry Andric #include "llvm/Support/ToolOutputFile.h"
34*bdd1243dSDimitry Andric #include "llvm/Support/raw_ostream.h"
350b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
360b57cec5SDimitry Andric #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
37e8d8bef9SDimitry Andric #include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
380b57cec5SDimitry Andric #include "llvm/Transforms/Scalar/LoopPassManager.h"
39480093f4SDimitry Andric #include "llvm/Transforms/Utils/Debugify.h"
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric using namespace llvm;
420b57cec5SDimitry Andric using namespace opt_tool;
430b57cec5SDimitry Andric 
44e8d8bef9SDimitry Andric namespace llvm {
45e8d8bef9SDimitry Andric cl::opt<bool> DebugifyEach(
46e8d8bef9SDimitry Andric     "debugify-each",
47e8d8bef9SDimitry Andric     cl::desc("Start each pass with debugify and end it with check-debugify"));
48e8d8bef9SDimitry Andric 
49e8d8bef9SDimitry Andric cl::opt<std::string>
50e8d8bef9SDimitry Andric     DebugifyExport("debugify-export",
51e8d8bef9SDimitry Andric                    cl::desc("Export per-pass debugify statistics to this file"),
52e8d8bef9SDimitry Andric                    cl::value_desc("filename"));
53753f127fSDimitry Andric 
54753f127fSDimitry Andric cl::opt<bool> VerifyEachDebugInfoPreserve(
55753f127fSDimitry Andric     "verify-each-debuginfo-preserve",
56753f127fSDimitry Andric     cl::desc("Start each pass with collecting and end it with checking of "
57753f127fSDimitry Andric              "debug info preservation."));
58753f127fSDimitry Andric 
59753f127fSDimitry Andric cl::opt<std::string>
60753f127fSDimitry Andric     VerifyDIPreserveExport("verify-di-preserve-export",
61753f127fSDimitry Andric                    cl::desc("Export debug info preservation failures into "
62753f127fSDimitry Andric                             "specified (JSON) file (should be abs path as we use"
63753f127fSDimitry Andric                             " append mode to insert new JSON objects)"),
64753f127fSDimitry Andric                    cl::value_desc("filename"), cl::init(""));
65753f127fSDimitry Andric 
66e8d8bef9SDimitry Andric } // namespace llvm
67e8d8bef9SDimitry Andric 
68fe6060f1SDimitry Andric enum class DebugLogging { None, Normal, Verbose, Quiet };
69fe6060f1SDimitry Andric 
70fe6060f1SDimitry Andric static cl::opt<DebugLogging> DebugPM(
71fe6060f1SDimitry Andric     "debug-pass-manager", cl::Hidden, cl::ValueOptional,
72fe6060f1SDimitry Andric     cl::desc("Print pass management debugging information"),
73fe6060f1SDimitry Andric     cl::init(DebugLogging::None),
74fe6060f1SDimitry Andric     cl::values(
75fe6060f1SDimitry Andric         clEnumValN(DebugLogging::Normal, "", ""),
76fe6060f1SDimitry Andric         clEnumValN(DebugLogging::Quiet, "quiet",
77fe6060f1SDimitry Andric                    "Skip printing info about analyses"),
78fe6060f1SDimitry Andric         clEnumValN(
79fe6060f1SDimitry Andric             DebugLogging::Verbose, "verbose",
80fe6060f1SDimitry Andric             "Print extra information about adaptors and pass managers")));
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric // This flag specifies a textual description of the alias analysis pipeline to
830b57cec5SDimitry Andric // use when querying for aliasing information. It only works in concert with
840b57cec5SDimitry Andric // the "passes" flag above.
850b57cec5SDimitry Andric static cl::opt<std::string>
860b57cec5SDimitry Andric     AAPipeline("aa-pipeline",
870b57cec5SDimitry Andric                cl::desc("A textual description of the alias analysis "
880b57cec5SDimitry Andric                         "pipeline for handling managed aliasing queries"),
89e8d8bef9SDimitry Andric                cl::Hidden, cl::init("default"));
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric /// {{@ These options accept textual pipeline descriptions which will be
920b57cec5SDimitry Andric /// inserted into default pipelines at the respective extension points
930b57cec5SDimitry Andric static cl::opt<std::string> PeepholeEPPipeline(
940b57cec5SDimitry Andric     "passes-ep-peephole",
950b57cec5SDimitry Andric     cl::desc("A textual description of the function pass pipeline inserted at "
960b57cec5SDimitry Andric              "the Peephole extension points into default pipelines"),
970b57cec5SDimitry Andric     cl::Hidden);
980b57cec5SDimitry Andric static cl::opt<std::string> LateLoopOptimizationsEPPipeline(
990b57cec5SDimitry Andric     "passes-ep-late-loop-optimizations",
1000b57cec5SDimitry Andric     cl::desc(
1010b57cec5SDimitry Andric         "A textual description of the loop pass pipeline inserted at "
1020b57cec5SDimitry Andric         "the LateLoopOptimizations extension point into default pipelines"),
1030b57cec5SDimitry Andric     cl::Hidden);
1040b57cec5SDimitry Andric static cl::opt<std::string> LoopOptimizerEndEPPipeline(
1050b57cec5SDimitry Andric     "passes-ep-loop-optimizer-end",
1060b57cec5SDimitry Andric     cl::desc("A textual description of the loop pass pipeline inserted at "
1070b57cec5SDimitry Andric              "the LoopOptimizerEnd extension point into default pipelines"),
1080b57cec5SDimitry Andric     cl::Hidden);
1090b57cec5SDimitry Andric static cl::opt<std::string> ScalarOptimizerLateEPPipeline(
1100b57cec5SDimitry Andric     "passes-ep-scalar-optimizer-late",
1110b57cec5SDimitry Andric     cl::desc("A textual description of the function pass pipeline inserted at "
1120b57cec5SDimitry Andric              "the ScalarOptimizerLate extension point into default pipelines"),
1130b57cec5SDimitry Andric     cl::Hidden);
1140b57cec5SDimitry Andric static cl::opt<std::string> CGSCCOptimizerLateEPPipeline(
1150b57cec5SDimitry Andric     "passes-ep-cgscc-optimizer-late",
1160b57cec5SDimitry Andric     cl::desc("A textual description of the cgscc pass pipeline inserted at "
1170b57cec5SDimitry Andric              "the CGSCCOptimizerLate extension point into default pipelines"),
1180b57cec5SDimitry Andric     cl::Hidden);
1190b57cec5SDimitry Andric static cl::opt<std::string> VectorizerStartEPPipeline(
1200b57cec5SDimitry Andric     "passes-ep-vectorizer-start",
1210b57cec5SDimitry Andric     cl::desc("A textual description of the function pass pipeline inserted at "
1220b57cec5SDimitry Andric              "the VectorizerStart extension point into default pipelines"),
1230b57cec5SDimitry Andric     cl::Hidden);
1240b57cec5SDimitry Andric static cl::opt<std::string> PipelineStartEPPipeline(
1250b57cec5SDimitry Andric     "passes-ep-pipeline-start",
126e8d8bef9SDimitry Andric     cl::desc("A textual description of the module pass pipeline inserted at "
1270b57cec5SDimitry Andric              "the PipelineStart extension point into default pipelines"),
1280b57cec5SDimitry Andric     cl::Hidden);
129e8d8bef9SDimitry Andric static cl::opt<std::string> PipelineEarlySimplificationEPPipeline(
130e8d8bef9SDimitry Andric     "passes-ep-pipeline-early-simplification",
131e8d8bef9SDimitry Andric     cl::desc("A textual description of the module pass pipeline inserted at "
132e8d8bef9SDimitry Andric              "the EarlySimplification extension point into default pipelines"),
133e8d8bef9SDimitry Andric     cl::Hidden);
13481ad6265SDimitry Andric static cl::opt<std::string> OptimizerEarlyEPPipeline(
13581ad6265SDimitry Andric     "passes-ep-optimizer-early",
13681ad6265SDimitry Andric     cl::desc("A textual description of the module pass pipeline inserted at "
13781ad6265SDimitry Andric              "the OptimizerEarly extension point into default pipelines"),
13881ad6265SDimitry Andric     cl::Hidden);
1390b57cec5SDimitry Andric static cl::opt<std::string> OptimizerLastEPPipeline(
1400b57cec5SDimitry Andric     "passes-ep-optimizer-last",
141e8d8bef9SDimitry Andric     cl::desc("A textual description of the module pass pipeline inserted at "
1420b57cec5SDimitry Andric              "the OptimizerLast extension point into default pipelines"),
1430b57cec5SDimitry Andric     cl::Hidden);
14481ad6265SDimitry Andric static cl::opt<std::string> FullLinkTimeOptimizationEarlyEPPipeline(
14581ad6265SDimitry Andric     "passes-ep-full-link-time-optimization-early",
14681ad6265SDimitry Andric     cl::desc("A textual description of the module pass pipeline inserted at "
14781ad6265SDimitry Andric              "the FullLinkTimeOptimizationEarly extension point into default "
14881ad6265SDimitry Andric              "pipelines"),
14981ad6265SDimitry Andric     cl::Hidden);
15081ad6265SDimitry Andric static cl::opt<std::string> FullLinkTimeOptimizationLastEPPipeline(
15181ad6265SDimitry Andric     "passes-ep-full-link-time-optimization-last",
15281ad6265SDimitry Andric     cl::desc("A textual description of the module pass pipeline inserted at "
15381ad6265SDimitry Andric              "the FullLinkTimeOptimizationLast extension point into default "
15481ad6265SDimitry Andric              "pipelines"),
15581ad6265SDimitry Andric     cl::Hidden);
156*bdd1243dSDimitry Andric /// @}}
1570b57cec5SDimitry Andric 
158*bdd1243dSDimitry Andric static cl::opt<bool> DisablePipelineVerification(
159*bdd1243dSDimitry Andric     "disable-pipeline-verification",
160*bdd1243dSDimitry Andric     cl::desc("Only has an effect when specified with -print-pipeline-passes. "
161*bdd1243dSDimitry Andric              "Disables verifying that the textual pipeline generated by "
162*bdd1243dSDimitry Andric              "-print-pipeline-passes can be used to create a pipeline."),
163*bdd1243dSDimitry Andric     cl::Hidden);
1645ffd83dbSDimitry Andric 
165*bdd1243dSDimitry Andric 
166*bdd1243dSDimitry Andric static cl::opt<PGOKind>
167*bdd1243dSDimitry Andric     PGOKindFlag("pgo-kind", cl::init(NoPGO), cl::Hidden,
168*bdd1243dSDimitry Andric                 cl::desc("The kind of profile guided optimization"),
169*bdd1243dSDimitry Andric                 cl::values(clEnumValN(NoPGO, "nopgo", "Do not use PGO."),
170*bdd1243dSDimitry Andric                            clEnumValN(InstrGen, "pgo-instr-gen-pipeline",
171*bdd1243dSDimitry Andric                                       "Instrument the IR to generate profile."),
172*bdd1243dSDimitry Andric                            clEnumValN(InstrUse, "pgo-instr-use-pipeline",
173*bdd1243dSDimitry Andric                                       "Use instrumented profile to guide PGO."),
174*bdd1243dSDimitry Andric                            clEnumValN(SampleUse, "pgo-sample-use-pipeline",
175*bdd1243dSDimitry Andric                                       "Use sampled profile to guide PGO.")));
176*bdd1243dSDimitry Andric static cl::opt<std::string> ProfileFile("profile-file",
177*bdd1243dSDimitry Andric                                  cl::desc("Path to the profile."), cl::Hidden);
178*bdd1243dSDimitry Andric 
179*bdd1243dSDimitry Andric static cl::opt<CSPGOKind> CSPGOKindFlag(
180*bdd1243dSDimitry Andric     "cspgo-kind", cl::init(NoCSPGO), cl::Hidden,
181*bdd1243dSDimitry Andric     cl::desc("The kind of context sensitive profile guided optimization"),
182*bdd1243dSDimitry Andric     cl::values(
183*bdd1243dSDimitry Andric         clEnumValN(NoCSPGO, "nocspgo", "Do not use CSPGO."),
184*bdd1243dSDimitry Andric         clEnumValN(
185*bdd1243dSDimitry Andric             CSInstrGen, "cspgo-instr-gen-pipeline",
186*bdd1243dSDimitry Andric             "Instrument (context sensitive) the IR to generate profile."),
187*bdd1243dSDimitry Andric         clEnumValN(
188*bdd1243dSDimitry Andric             CSInstrUse, "cspgo-instr-use-pipeline",
189*bdd1243dSDimitry Andric             "Use instrumented (context sensitive) profile to guide PGO.")));
190*bdd1243dSDimitry Andric 
191*bdd1243dSDimitry Andric static cl::opt<std::string> CSProfileGenFile(
192*bdd1243dSDimitry Andric     "cs-profilegen-file",
193*bdd1243dSDimitry Andric     cl::desc("Path to the instrumented context sensitive profile."),
194*bdd1243dSDimitry Andric     cl::Hidden);
1950b57cec5SDimitry Andric 
1960b57cec5SDimitry Andric static cl::opt<std::string>
1970b57cec5SDimitry Andric     ProfileRemappingFile("profile-remapping-file",
1980b57cec5SDimitry Andric                          cl::desc("Path to the profile remapping file."),
1990b57cec5SDimitry Andric                          cl::Hidden);
200*bdd1243dSDimitry Andric 
2010b57cec5SDimitry Andric static cl::opt<bool> DebugInfoForProfiling(
202*bdd1243dSDimitry Andric     "debug-info-for-profiling", cl::init(false), cl::Hidden,
2030b57cec5SDimitry Andric     cl::desc("Emit special debug info to enable PGO profile generation."));
204*bdd1243dSDimitry Andric 
205e8d8bef9SDimitry Andric static cl::opt<bool> PseudoProbeForProfiling(
206*bdd1243dSDimitry Andric     "pseudo-probe-for-profiling", cl::init(false), cl::Hidden,
207e8d8bef9SDimitry Andric     cl::desc("Emit pseudo probes to enable PGO profile generation."));
208*bdd1243dSDimitry Andric 
209*bdd1243dSDimitry Andric static cl::opt<bool> DisableLoopUnrolling(
210*bdd1243dSDimitry Andric     "disable-loop-unrolling",
211*bdd1243dSDimitry Andric     cl::desc("Disable loop unrolling in all relevant passes"), cl::init(false));
212*bdd1243dSDimitry Andric 
213*bdd1243dSDimitry Andric namespace llvm {
214*bdd1243dSDimitry Andric extern cl::opt<bool> PrintPipelinePasses;
215*bdd1243dSDimitry Andric } // namespace llvm
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric template <typename PassManagerT>
2180b57cec5SDimitry Andric bool tryParsePipelineText(PassBuilder &PB,
2190b57cec5SDimitry Andric                           const cl::opt<std::string> &PipelineOpt) {
2200b57cec5SDimitry Andric   if (PipelineOpt.empty())
2210b57cec5SDimitry Andric     return false;
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric   // Verify the pipeline is parseable:
2240b57cec5SDimitry Andric   PassManagerT PM;
2250b57cec5SDimitry Andric   if (auto Err = PB.parsePassPipeline(PM, PipelineOpt)) {
2260b57cec5SDimitry Andric     errs() << "Could not parse -" << PipelineOpt.ArgStr
2270b57cec5SDimitry Andric            << " pipeline: " << toString(std::move(Err))
2280b57cec5SDimitry Andric            << "... I'm going to ignore it.\n";
2290b57cec5SDimitry Andric     return false;
2300b57cec5SDimitry Andric   }
2310b57cec5SDimitry Andric   return true;
2320b57cec5SDimitry Andric }
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric /// If one of the EPPipeline command line options was given, register callbacks
2350b57cec5SDimitry Andric /// for parsing and inserting the given pipeline
236e8d8bef9SDimitry Andric static void registerEPCallbacks(PassBuilder &PB) {
2370b57cec5SDimitry Andric   if (tryParsePipelineText<FunctionPassManager>(PB, PeepholeEPPipeline))
2380b57cec5SDimitry Andric     PB.registerPeepholeEPCallback(
239349cc55cSDimitry Andric         [&PB](FunctionPassManager &PM, OptimizationLevel Level) {
2400b57cec5SDimitry Andric           ExitOnError Err("Unable to parse PeepholeEP pipeline: ");
241e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, PeepholeEPPipeline));
2420b57cec5SDimitry Andric         });
2430b57cec5SDimitry Andric   if (tryParsePipelineText<LoopPassManager>(PB,
2440b57cec5SDimitry Andric                                             LateLoopOptimizationsEPPipeline))
2450b57cec5SDimitry Andric     PB.registerLateLoopOptimizationsEPCallback(
246349cc55cSDimitry Andric         [&PB](LoopPassManager &PM, OptimizationLevel Level) {
2470b57cec5SDimitry Andric           ExitOnError Err("Unable to parse LateLoopOptimizationsEP pipeline: ");
248e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, LateLoopOptimizationsEPPipeline));
2490b57cec5SDimitry Andric         });
2500b57cec5SDimitry Andric   if (tryParsePipelineText<LoopPassManager>(PB, LoopOptimizerEndEPPipeline))
2510b57cec5SDimitry Andric     PB.registerLoopOptimizerEndEPCallback(
252349cc55cSDimitry Andric         [&PB](LoopPassManager &PM, OptimizationLevel Level) {
2530b57cec5SDimitry Andric           ExitOnError Err("Unable to parse LoopOptimizerEndEP pipeline: ");
254e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, LoopOptimizerEndEPPipeline));
2550b57cec5SDimitry Andric         });
2560b57cec5SDimitry Andric   if (tryParsePipelineText<FunctionPassManager>(PB,
2570b57cec5SDimitry Andric                                                 ScalarOptimizerLateEPPipeline))
2580b57cec5SDimitry Andric     PB.registerScalarOptimizerLateEPCallback(
259349cc55cSDimitry Andric         [&PB](FunctionPassManager &PM, OptimizationLevel Level) {
2600b57cec5SDimitry Andric           ExitOnError Err("Unable to parse ScalarOptimizerLateEP pipeline: ");
261e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, ScalarOptimizerLateEPPipeline));
2620b57cec5SDimitry Andric         });
2630b57cec5SDimitry Andric   if (tryParsePipelineText<CGSCCPassManager>(PB, CGSCCOptimizerLateEPPipeline))
2640b57cec5SDimitry Andric     PB.registerCGSCCOptimizerLateEPCallback(
265349cc55cSDimitry Andric         [&PB](CGSCCPassManager &PM, OptimizationLevel Level) {
2660b57cec5SDimitry Andric           ExitOnError Err("Unable to parse CGSCCOptimizerLateEP pipeline: ");
267e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, CGSCCOptimizerLateEPPipeline));
2680b57cec5SDimitry Andric         });
2690b57cec5SDimitry Andric   if (tryParsePipelineText<FunctionPassManager>(PB, VectorizerStartEPPipeline))
2700b57cec5SDimitry Andric     PB.registerVectorizerStartEPCallback(
271349cc55cSDimitry Andric         [&PB](FunctionPassManager &PM, OptimizationLevel Level) {
2720b57cec5SDimitry Andric           ExitOnError Err("Unable to parse VectorizerStartEP pipeline: ");
273e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, VectorizerStartEPPipeline));
2740b57cec5SDimitry Andric         });
2750b57cec5SDimitry Andric   if (tryParsePipelineText<ModulePassManager>(PB, PipelineStartEPPipeline))
2760b57cec5SDimitry Andric     PB.registerPipelineStartEPCallback(
277349cc55cSDimitry Andric         [&PB](ModulePassManager &PM, OptimizationLevel) {
2780b57cec5SDimitry Andric           ExitOnError Err("Unable to parse PipelineStartEP pipeline: ");
279e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, PipelineStartEPPipeline));
280e8d8bef9SDimitry Andric         });
281e8d8bef9SDimitry Andric   if (tryParsePipelineText<ModulePassManager>(
282e8d8bef9SDimitry Andric           PB, PipelineEarlySimplificationEPPipeline))
283e8d8bef9SDimitry Andric     PB.registerPipelineEarlySimplificationEPCallback(
284349cc55cSDimitry Andric         [&PB](ModulePassManager &PM, OptimizationLevel) {
285e8d8bef9SDimitry Andric           ExitOnError Err("Unable to parse EarlySimplification pipeline: ");
286e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, PipelineEarlySimplificationEPPipeline));
2870b57cec5SDimitry Andric         });
28881ad6265SDimitry Andric   if (tryParsePipelineText<ModulePassManager>(PB, OptimizerEarlyEPPipeline))
28981ad6265SDimitry Andric     PB.registerOptimizerEarlyEPCallback(
29081ad6265SDimitry Andric         [&PB](ModulePassManager &PM, OptimizationLevel) {
29181ad6265SDimitry Andric           ExitOnError Err("Unable to parse OptimizerEarlyEP pipeline: ");
29281ad6265SDimitry Andric           Err(PB.parsePassPipeline(PM, OptimizerEarlyEPPipeline));
29381ad6265SDimitry Andric         });
29481ad6265SDimitry Andric   if (tryParsePipelineText<ModulePassManager>(PB, OptimizerLastEPPipeline))
2950b57cec5SDimitry Andric     PB.registerOptimizerLastEPCallback(
296349cc55cSDimitry Andric         [&PB](ModulePassManager &PM, OptimizationLevel) {
2970b57cec5SDimitry Andric           ExitOnError Err("Unable to parse OptimizerLastEP pipeline: ");
298e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, OptimizerLastEPPipeline));
2990b57cec5SDimitry Andric         });
30081ad6265SDimitry Andric   if (tryParsePipelineText<ModulePassManager>(
30181ad6265SDimitry Andric           PB, FullLinkTimeOptimizationEarlyEPPipeline))
30281ad6265SDimitry Andric     PB.registerFullLinkTimeOptimizationEarlyEPCallback(
30381ad6265SDimitry Andric         [&PB](ModulePassManager &PM, OptimizationLevel) {
30481ad6265SDimitry Andric           ExitOnError Err(
30581ad6265SDimitry Andric               "Unable to parse FullLinkTimeOptimizationEarlyEP pipeline: ");
30681ad6265SDimitry Andric           Err(PB.parsePassPipeline(PM,
30781ad6265SDimitry Andric                                    FullLinkTimeOptimizationEarlyEPPipeline));
30881ad6265SDimitry Andric         });
30981ad6265SDimitry Andric   if (tryParsePipelineText<ModulePassManager>(
31081ad6265SDimitry Andric           PB, FullLinkTimeOptimizationLastEPPipeline))
31181ad6265SDimitry Andric     PB.registerFullLinkTimeOptimizationLastEPCallback(
31281ad6265SDimitry Andric         [&PB](ModulePassManager &PM, OptimizationLevel) {
31381ad6265SDimitry Andric           ExitOnError Err(
31481ad6265SDimitry Andric               "Unable to parse FullLinkTimeOptimizationLastEP pipeline: ");
31581ad6265SDimitry Andric           Err(PB.parsePassPipeline(PM, FullLinkTimeOptimizationLastEPPipeline));
31681ad6265SDimitry Andric         });
3170b57cec5SDimitry Andric }
3180b57cec5SDimitry Andric 
319480093f4SDimitry Andric #define HANDLE_EXTENSION(Ext)                                                  \
320480093f4SDimitry Andric   llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
321480093f4SDimitry Andric #include "llvm/Support/Extension.def"
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
324e8d8bef9SDimitry Andric                            TargetLibraryInfoImpl *TLII, ToolOutputFile *Out,
325e8d8bef9SDimitry Andric                            ToolOutputFile *ThinLTOLinkOut,
3260b57cec5SDimitry Andric                            ToolOutputFile *OptRemarkFile,
327*bdd1243dSDimitry Andric                            StringRef PassPipeline,
32881ad6265SDimitry Andric                            ArrayRef<PassPlugin> PassPlugins,
3295ffd83dbSDimitry Andric                            OutputKind OK, VerifierKind VK,
3300b57cec5SDimitry Andric                            bool ShouldPreserveAssemblyUseListOrder,
3310b57cec5SDimitry Andric                            bool ShouldPreserveBitcodeUseListOrder,
3320b57cec5SDimitry Andric                            bool EmitSummaryIndex, bool EmitModuleHash,
333753f127fSDimitry Andric                            bool EnableDebugify, bool VerifyDIPreserve) {
3340b57cec5SDimitry Andric   bool VerifyEachPass = VK == VK_VerifyEachPass;
3350b57cec5SDimitry Andric 
336*bdd1243dSDimitry Andric   std::optional<PGOOptions> P;
3370b57cec5SDimitry Andric   switch (PGOKindFlag) {
3380b57cec5SDimitry Andric   case InstrGen:
3390b57cec5SDimitry Andric     P = PGOOptions(ProfileFile, "", "", PGOOptions::IRInstr);
3400b57cec5SDimitry Andric     break;
3410b57cec5SDimitry Andric   case InstrUse:
3420b57cec5SDimitry Andric     P = PGOOptions(ProfileFile, "", ProfileRemappingFile, PGOOptions::IRUse);
3430b57cec5SDimitry Andric     break;
3440b57cec5SDimitry Andric   case SampleUse:
3450b57cec5SDimitry Andric     P = PGOOptions(ProfileFile, "", ProfileRemappingFile,
3460b57cec5SDimitry Andric                    PGOOptions::SampleUse);
3470b57cec5SDimitry Andric     break;
3480b57cec5SDimitry Andric   case NoPGO:
349349cc55cSDimitry Andric     if (DebugInfoForProfiling || PseudoProbeForProfiling)
3500b57cec5SDimitry Andric       P = PGOOptions("", "", "", PGOOptions::NoAction, PGOOptions::NoCSAction,
351349cc55cSDimitry Andric                      DebugInfoForProfiling, PseudoProbeForProfiling);
3520b57cec5SDimitry Andric     else
353*bdd1243dSDimitry Andric       P = std::nullopt;
3540b57cec5SDimitry Andric   }
3550b57cec5SDimitry Andric   if (CSPGOKindFlag != NoCSPGO) {
3560b57cec5SDimitry Andric     if (P && (P->Action == PGOOptions::IRInstr ||
357*bdd1243dSDimitry Andric               P->Action == PGOOptions::SampleUse)) {
3580b57cec5SDimitry Andric       errs() << "CSPGOKind cannot be used with IRInstr or SampleUse";
359*bdd1243dSDimitry Andric       return false;
360*bdd1243dSDimitry Andric     }
3610b57cec5SDimitry Andric     if (CSPGOKindFlag == CSInstrGen) {
362*bdd1243dSDimitry Andric       if (CSProfileGenFile.empty()) {
3630b57cec5SDimitry Andric         errs() << "CSInstrGen needs to specify CSProfileGenFile";
364*bdd1243dSDimitry Andric         return false;
365*bdd1243dSDimitry Andric       }
3660b57cec5SDimitry Andric       if (P) {
3670b57cec5SDimitry Andric         P->CSAction = PGOOptions::CSIRInstr;
3680b57cec5SDimitry Andric         P->CSProfileGenFile = CSProfileGenFile;
3690b57cec5SDimitry Andric       } else
3700b57cec5SDimitry Andric         P = PGOOptions("", CSProfileGenFile, ProfileRemappingFile,
3710b57cec5SDimitry Andric                        PGOOptions::NoAction, PGOOptions::CSIRInstr);
3720b57cec5SDimitry Andric     } else /* CSPGOKindFlag == CSInstrUse */ {
373*bdd1243dSDimitry Andric       if (!P) {
3740b57cec5SDimitry Andric         errs() << "CSInstrUse needs to be together with InstrUse";
375*bdd1243dSDimitry Andric         return false;
376*bdd1243dSDimitry Andric       }
3770b57cec5SDimitry Andric       P->CSAction = PGOOptions::CSIRUse;
3780b57cec5SDimitry Andric     }
3790b57cec5SDimitry Andric   }
380349cc55cSDimitry Andric   if (TM)
381349cc55cSDimitry Andric     TM->setPGOOption(P);
382349cc55cSDimitry Andric 
383fe6060f1SDimitry Andric   LoopAnalysisManager LAM;
384fe6060f1SDimitry Andric   FunctionAnalysisManager FAM;
385fe6060f1SDimitry Andric   CGSCCAnalysisManager CGAM;
386fe6060f1SDimitry Andric   ModuleAnalysisManager MAM;
387fe6060f1SDimitry Andric 
3880b57cec5SDimitry Andric   PassInstrumentationCallbacks PIC;
389fe6060f1SDimitry Andric   PrintPassOptions PrintPassOpts;
390fe6060f1SDimitry Andric   PrintPassOpts.Verbose = DebugPM == DebugLogging::Verbose;
391fe6060f1SDimitry Andric   PrintPassOpts.SkipAnalyses = DebugPM == DebugLogging::Quiet;
392*bdd1243dSDimitry Andric   StandardInstrumentations SI(M.getContext(), DebugPM != DebugLogging::None,
393*bdd1243dSDimitry Andric                               VerifyEachPass, PrintPassOpts);
394fe6060f1SDimitry Andric   SI.registerCallbacks(PIC, &FAM);
395e8d8bef9SDimitry Andric   DebugifyEachInstrumentation Debugify;
396753f127fSDimitry Andric   DebugifyStatsMap DIStatsMap;
397753f127fSDimitry Andric   DebugInfoPerPass DebugInfoBeforePass;
398753f127fSDimitry Andric   if (DebugifyEach) {
399753f127fSDimitry Andric     Debugify.setDIStatsMap(DIStatsMap);
400753f127fSDimitry Andric     Debugify.setDebugifyMode(DebugifyMode::SyntheticDebugInfo);
401e8d8bef9SDimitry Andric     Debugify.registerCallbacks(PIC);
402753f127fSDimitry Andric   } else if (VerifyEachDebugInfoPreserve) {
403753f127fSDimitry Andric     Debugify.setDebugInfoBeforePass(DebugInfoBeforePass);
404753f127fSDimitry Andric     Debugify.setDebugifyMode(DebugifyMode::OriginalDebugInfo);
405753f127fSDimitry Andric     Debugify.setOrigDIVerifyBugsReportFilePath(
406753f127fSDimitry Andric       VerifyDIPreserveExport);
407753f127fSDimitry Andric     Debugify.registerCallbacks(PIC);
408753f127fSDimitry Andric   }
4090b57cec5SDimitry Andric 
4105ffd83dbSDimitry Andric   PipelineTuningOptions PTO;
4115ffd83dbSDimitry Andric   // LoopUnrolling defaults on to true and DisableLoopUnrolling is initialized
4125ffd83dbSDimitry Andric   // to false above so we shouldn't necessarily need to check whether or not the
4135ffd83dbSDimitry Andric   // option has been enabled.
4145ffd83dbSDimitry Andric   PTO.LoopUnrolling = !DisableLoopUnrolling;
415fe6060f1SDimitry Andric   PassBuilder PB(TM, PTO, P, &PIC);
416e8d8bef9SDimitry Andric   registerEPCallbacks(PB);
4170b57cec5SDimitry Andric 
41881ad6265SDimitry Andric   // For any loaded plugins, let them register pass builder callbacks.
41981ad6265SDimitry Andric   for (auto &PassPlugin : PassPlugins)
42081ad6265SDimitry Andric     PassPlugin.registerPassBuilderCallbacks(PB);
4210b57cec5SDimitry Andric 
422480093f4SDimitry Andric #define HANDLE_EXTENSION(Ext)                                                  \
423480093f4SDimitry Andric   get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
424480093f4SDimitry Andric #include "llvm/Support/Extension.def"
4250b57cec5SDimitry Andric 
4260b57cec5SDimitry Andric   // Specially handle the alias analysis manager so that we can register
4270b57cec5SDimitry Andric   // a custom pipeline of AA passes with it.
4280b57cec5SDimitry Andric   AAManager AA;
4290b57cec5SDimitry Andric   if (auto Err = PB.parseAAPipeline(AA, AAPipeline)) {
4300b57cec5SDimitry Andric     errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
4310b57cec5SDimitry Andric     return false;
4320b57cec5SDimitry Andric   }
4330b57cec5SDimitry Andric 
4340b57cec5SDimitry Andric   // Register the AA manager first so that our version is the one used.
4350b57cec5SDimitry Andric   FAM.registerPass([&] { return std::move(AA); });
436e8d8bef9SDimitry Andric   // Register our TargetLibraryInfoImpl.
437e8d8bef9SDimitry Andric   FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
4380b57cec5SDimitry Andric 
4390b57cec5SDimitry Andric   // Register all the basic analyses with the managers.
4400b57cec5SDimitry Andric   PB.registerModuleAnalyses(MAM);
4410b57cec5SDimitry Andric   PB.registerCGSCCAnalyses(CGAM);
4420b57cec5SDimitry Andric   PB.registerFunctionAnalyses(FAM);
4430b57cec5SDimitry Andric   PB.registerLoopAnalyses(LAM);
4440b57cec5SDimitry Andric   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
4450b57cec5SDimitry Andric 
446fe6060f1SDimitry Andric   ModulePassManager MPM;
4470b57cec5SDimitry Andric   if (EnableDebugify)
4480b57cec5SDimitry Andric     MPM.addPass(NewPMDebugifyPass());
449753f127fSDimitry Andric   if (VerifyDIPreserve)
450753f127fSDimitry Andric     MPM.addPass(NewPMDebugifyPass(DebugifyMode::OriginalDebugInfo, "",
451753f127fSDimitry Andric                                   &DebugInfoBeforePass));
4520b57cec5SDimitry Andric 
453349cc55cSDimitry Andric   // Add passes according to the -passes options.
4545ffd83dbSDimitry Andric   if (!PassPipeline.empty()) {
455e8d8bef9SDimitry Andric     if (auto Err = PB.parsePassPipeline(MPM, PassPipeline)) {
4560b57cec5SDimitry Andric       errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
4570b57cec5SDimitry Andric       return false;
4580b57cec5SDimitry Andric     }
4595ffd83dbSDimitry Andric   }
4600b57cec5SDimitry Andric 
4610b57cec5SDimitry Andric   if (VK > VK_NoVerifier)
4620b57cec5SDimitry Andric     MPM.addPass(VerifierPass());
4630b57cec5SDimitry Andric   if (EnableDebugify)
464753f127fSDimitry Andric     MPM.addPass(NewPMCheckDebugifyPass(false, "", &DIStatsMap));
465753f127fSDimitry Andric   if (VerifyDIPreserve)
466753f127fSDimitry Andric     MPM.addPass(NewPMCheckDebugifyPass(
467*bdd1243dSDimitry Andric         false, "", nullptr, DebugifyMode::OriginalDebugInfo,
468*bdd1243dSDimitry Andric         &DebugInfoBeforePass, VerifyDIPreserveExport));
4690b57cec5SDimitry Andric 
4700b57cec5SDimitry Andric   // Add any relevant output pass at the end of the pipeline.
4710b57cec5SDimitry Andric   switch (OK) {
4720b57cec5SDimitry Andric   case OK_NoOutput:
4730b57cec5SDimitry Andric     break; // No output pass needed.
4740b57cec5SDimitry Andric   case OK_OutputAssembly:
475*bdd1243dSDimitry Andric     MPM.addPass(PrintModulePass(
476*bdd1243dSDimitry Andric         Out->os(), "", ShouldPreserveAssemblyUseListOrder, EmitSummaryIndex));
4770b57cec5SDimitry Andric     break;
4780b57cec5SDimitry Andric   case OK_OutputBitcode:
4790b57cec5SDimitry Andric     MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder,
4800b57cec5SDimitry Andric                                   EmitSummaryIndex, EmitModuleHash));
4810b57cec5SDimitry Andric     break;
4820b57cec5SDimitry Andric   case OK_OutputThinLTOBitcode:
4830b57cec5SDimitry Andric     MPM.addPass(ThinLTOBitcodeWriterPass(
4840b57cec5SDimitry Andric         Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr));
4850b57cec5SDimitry Andric     break;
4860b57cec5SDimitry Andric   }
4870b57cec5SDimitry Andric 
4880b57cec5SDimitry Andric   // Before executing passes, print the final values of the LLVM options.
4890b57cec5SDimitry Andric   cl::PrintOptionValues();
4900b57cec5SDimitry Andric 
491349cc55cSDimitry Andric   // Print a textual, '-passes=' compatible, representation of pipeline if
492349cc55cSDimitry Andric   // requested.
493349cc55cSDimitry Andric   if (PrintPipelinePasses) {
494*bdd1243dSDimitry Andric     std::string Pipeline;
495*bdd1243dSDimitry Andric     raw_string_ostream SOS(Pipeline);
496*bdd1243dSDimitry Andric     MPM.printPipeline(SOS, [&PIC](StringRef ClassName) {
497349cc55cSDimitry Andric       auto PassName = PIC.getPassNameForClassName(ClassName);
498349cc55cSDimitry Andric       return PassName.empty() ? ClassName : PassName;
499349cc55cSDimitry Andric     });
500*bdd1243dSDimitry Andric     outs() << Pipeline;
501349cc55cSDimitry Andric     outs() << "\n";
502*bdd1243dSDimitry Andric 
503*bdd1243dSDimitry Andric     if (!DisablePipelineVerification) {
504*bdd1243dSDimitry Andric       // Check that we can parse the returned pipeline string as an actual
505*bdd1243dSDimitry Andric       // pipeline.
506*bdd1243dSDimitry Andric       ModulePassManager TempPM;
507*bdd1243dSDimitry Andric       if (auto Err = PB.parsePassPipeline(TempPM, Pipeline)) {
508*bdd1243dSDimitry Andric         errs() << "Could not parse dumped pass pipeline: "
509*bdd1243dSDimitry Andric                << toString(std::move(Err)) << "\n";
510*bdd1243dSDimitry Andric         return false;
511*bdd1243dSDimitry Andric       }
512*bdd1243dSDimitry Andric     }
513*bdd1243dSDimitry Andric 
514349cc55cSDimitry Andric     return true;
515349cc55cSDimitry Andric   }
516349cc55cSDimitry Andric 
5170b57cec5SDimitry Andric   // Now that we have all of the passes ready, run them.
5180b57cec5SDimitry Andric   MPM.run(M, MAM);
5190b57cec5SDimitry Andric 
5200b57cec5SDimitry Andric   // Declare success.
5210b57cec5SDimitry Andric   if (OK != OK_NoOutput) {
5220b57cec5SDimitry Andric     Out->keep();
5230b57cec5SDimitry Andric     if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut)
5240b57cec5SDimitry Andric       ThinLTOLinkOut->keep();
5250b57cec5SDimitry Andric   }
5260b57cec5SDimitry Andric 
5270b57cec5SDimitry Andric   if (OptRemarkFile)
5280b57cec5SDimitry Andric     OptRemarkFile->keep();
5290b57cec5SDimitry Andric 
530e8d8bef9SDimitry Andric   if (DebugifyEach && !DebugifyExport.empty())
531753f127fSDimitry Andric     exportDebugifyStats(DebugifyExport, Debugify.getDebugifyStatsMap());
532e8d8bef9SDimitry Andric 
5330b57cec5SDimitry Andric   return true;
5340b57cec5SDimitry Andric }
535fe6060f1SDimitry Andric 
536fe6060f1SDimitry Andric void llvm::printPasses(raw_ostream &OS) {
537fe6060f1SDimitry Andric   PassBuilder PB;
538fe6060f1SDimitry Andric   PB.printPassNames(OS);
539fe6060f1SDimitry Andric }
540