xref: /freebsd/contrib/llvm-project/llvm/tools/opt/NewPMDriver.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
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/IRPrintingPasses.h"
250b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h"
260b57cec5SDimitry Andric #include "llvm/IR/Module.h"
270b57cec5SDimitry Andric #include "llvm/IR/PassManager.h"
280b57cec5SDimitry Andric #include "llvm/IR/Verifier.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"
340b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
350b57cec5SDimitry Andric #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
36e8d8bef9SDimitry Andric #include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
370b57cec5SDimitry Andric #include "llvm/Transforms/Scalar/LoopPassManager.h"
38480093f4SDimitry Andric #include "llvm/Transforms/Utils/Debugify.h"
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric using namespace llvm;
410b57cec5SDimitry Andric using namespace opt_tool;
420b57cec5SDimitry Andric 
43e8d8bef9SDimitry Andric namespace llvm {
44e8d8bef9SDimitry Andric cl::opt<bool> DebugifyEach(
45e8d8bef9SDimitry Andric     "debugify-each",
46e8d8bef9SDimitry Andric     cl::desc("Start each pass with debugify and end it with check-debugify"));
47e8d8bef9SDimitry Andric 
48e8d8bef9SDimitry Andric cl::opt<std::string>
49e8d8bef9SDimitry Andric     DebugifyExport("debugify-export",
50e8d8bef9SDimitry Andric                    cl::desc("Export per-pass debugify statistics to this file"),
51e8d8bef9SDimitry Andric                    cl::value_desc("filename"));
52e8d8bef9SDimitry Andric } // namespace llvm
53e8d8bef9SDimitry Andric 
54fe6060f1SDimitry Andric enum class DebugLogging { None, Normal, Verbose, Quiet };
55fe6060f1SDimitry Andric 
56fe6060f1SDimitry Andric static cl::opt<DebugLogging> DebugPM(
57fe6060f1SDimitry Andric     "debug-pass-manager", cl::Hidden, cl::ValueOptional,
58fe6060f1SDimitry Andric     cl::desc("Print pass management debugging information"),
59fe6060f1SDimitry Andric     cl::init(DebugLogging::None),
60fe6060f1SDimitry Andric     cl::values(
61fe6060f1SDimitry Andric         clEnumValN(DebugLogging::Normal, "", ""),
62fe6060f1SDimitry Andric         clEnumValN(DebugLogging::Quiet, "quiet",
63fe6060f1SDimitry Andric                    "Skip printing info about analyses"),
64fe6060f1SDimitry Andric         clEnumValN(
65fe6060f1SDimitry Andric             DebugLogging::Verbose, "verbose",
66fe6060f1SDimitry Andric             "Print extra information about adaptors and pass managers")));
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric // This flag specifies a textual description of the alias analysis pipeline to
690b57cec5SDimitry Andric // use when querying for aliasing information. It only works in concert with
700b57cec5SDimitry Andric // the "passes" flag above.
710b57cec5SDimitry Andric static cl::opt<std::string>
720b57cec5SDimitry Andric     AAPipeline("aa-pipeline",
730b57cec5SDimitry Andric                cl::desc("A textual description of the alias analysis "
740b57cec5SDimitry Andric                         "pipeline for handling managed aliasing queries"),
75e8d8bef9SDimitry Andric                cl::Hidden, cl::init("default"));
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric /// {{@ These options accept textual pipeline descriptions which will be
780b57cec5SDimitry Andric /// inserted into default pipelines at the respective extension points
790b57cec5SDimitry Andric static cl::opt<std::string> PeepholeEPPipeline(
800b57cec5SDimitry Andric     "passes-ep-peephole",
810b57cec5SDimitry Andric     cl::desc("A textual description of the function pass pipeline inserted at "
820b57cec5SDimitry Andric              "the Peephole extension points into default pipelines"),
830b57cec5SDimitry Andric     cl::Hidden);
840b57cec5SDimitry Andric static cl::opt<std::string> LateLoopOptimizationsEPPipeline(
850b57cec5SDimitry Andric     "passes-ep-late-loop-optimizations",
860b57cec5SDimitry Andric     cl::desc(
870b57cec5SDimitry Andric         "A textual description of the loop pass pipeline inserted at "
880b57cec5SDimitry Andric         "the LateLoopOptimizations extension point into default pipelines"),
890b57cec5SDimitry Andric     cl::Hidden);
900b57cec5SDimitry Andric static cl::opt<std::string> LoopOptimizerEndEPPipeline(
910b57cec5SDimitry Andric     "passes-ep-loop-optimizer-end",
920b57cec5SDimitry Andric     cl::desc("A textual description of the loop pass pipeline inserted at "
930b57cec5SDimitry Andric              "the LoopOptimizerEnd extension point into default pipelines"),
940b57cec5SDimitry Andric     cl::Hidden);
950b57cec5SDimitry Andric static cl::opt<std::string> ScalarOptimizerLateEPPipeline(
960b57cec5SDimitry Andric     "passes-ep-scalar-optimizer-late",
970b57cec5SDimitry Andric     cl::desc("A textual description of the function pass pipeline inserted at "
980b57cec5SDimitry Andric              "the ScalarOptimizerLate extension point into default pipelines"),
990b57cec5SDimitry Andric     cl::Hidden);
1000b57cec5SDimitry Andric static cl::opt<std::string> CGSCCOptimizerLateEPPipeline(
1010b57cec5SDimitry Andric     "passes-ep-cgscc-optimizer-late",
1020b57cec5SDimitry Andric     cl::desc("A textual description of the cgscc pass pipeline inserted at "
1030b57cec5SDimitry Andric              "the CGSCCOptimizerLate extension point into default pipelines"),
1040b57cec5SDimitry Andric     cl::Hidden);
1050b57cec5SDimitry Andric static cl::opt<std::string> VectorizerStartEPPipeline(
1060b57cec5SDimitry Andric     "passes-ep-vectorizer-start",
1070b57cec5SDimitry Andric     cl::desc("A textual description of the function pass pipeline inserted at "
1080b57cec5SDimitry Andric              "the VectorizerStart extension point into default pipelines"),
1090b57cec5SDimitry Andric     cl::Hidden);
1100b57cec5SDimitry Andric static cl::opt<std::string> PipelineStartEPPipeline(
1110b57cec5SDimitry Andric     "passes-ep-pipeline-start",
112e8d8bef9SDimitry Andric     cl::desc("A textual description of the module pass pipeline inserted at "
1130b57cec5SDimitry Andric              "the PipelineStart extension point into default pipelines"),
1140b57cec5SDimitry Andric     cl::Hidden);
115e8d8bef9SDimitry Andric static cl::opt<std::string> PipelineEarlySimplificationEPPipeline(
116e8d8bef9SDimitry Andric     "passes-ep-pipeline-early-simplification",
117e8d8bef9SDimitry Andric     cl::desc("A textual description of the module pass pipeline inserted at "
118e8d8bef9SDimitry Andric              "the EarlySimplification extension point into default pipelines"),
119e8d8bef9SDimitry Andric     cl::Hidden);
120*81ad6265SDimitry Andric static cl::opt<std::string> OptimizerEarlyEPPipeline(
121*81ad6265SDimitry Andric     "passes-ep-optimizer-early",
122*81ad6265SDimitry Andric     cl::desc("A textual description of the module pass pipeline inserted at "
123*81ad6265SDimitry Andric              "the OptimizerEarly extension point into default pipelines"),
124*81ad6265SDimitry Andric     cl::Hidden);
1250b57cec5SDimitry Andric static cl::opt<std::string> OptimizerLastEPPipeline(
1260b57cec5SDimitry Andric     "passes-ep-optimizer-last",
127e8d8bef9SDimitry Andric     cl::desc("A textual description of the module pass pipeline inserted at "
1280b57cec5SDimitry Andric              "the OptimizerLast extension point into default pipelines"),
1290b57cec5SDimitry Andric     cl::Hidden);
130*81ad6265SDimitry Andric static cl::opt<std::string> FullLinkTimeOptimizationEarlyEPPipeline(
131*81ad6265SDimitry Andric     "passes-ep-full-link-time-optimization-early",
132*81ad6265SDimitry Andric     cl::desc("A textual description of the module pass pipeline inserted at "
133*81ad6265SDimitry Andric              "the FullLinkTimeOptimizationEarly extension point into default "
134*81ad6265SDimitry Andric              "pipelines"),
135*81ad6265SDimitry Andric     cl::Hidden);
136*81ad6265SDimitry Andric static cl::opt<std::string> FullLinkTimeOptimizationLastEPPipeline(
137*81ad6265SDimitry Andric     "passes-ep-full-link-time-optimization-last",
138*81ad6265SDimitry Andric     cl::desc("A textual description of the module pass pipeline inserted at "
139*81ad6265SDimitry Andric              "the FullLinkTimeOptimizationLast extension point into default "
140*81ad6265SDimitry Andric              "pipelines"),
141*81ad6265SDimitry Andric     cl::Hidden);
1420b57cec5SDimitry Andric 
1435ffd83dbSDimitry Andric // Individual pipeline tuning options.
1445ffd83dbSDimitry Andric extern cl::opt<bool> DisableLoopUnrolling;
1455ffd83dbSDimitry Andric 
146fe6060f1SDimitry Andric namespace llvm {
1470b57cec5SDimitry Andric extern cl::opt<PGOKind> PGOKindFlag;
1480b57cec5SDimitry Andric extern cl::opt<std::string> ProfileFile;
1490b57cec5SDimitry Andric extern cl::opt<CSPGOKind> CSPGOKindFlag;
1500b57cec5SDimitry Andric extern cl::opt<std::string> CSProfileGenFile;
151e8d8bef9SDimitry Andric extern cl::opt<bool> DisableBasicAA;
152349cc55cSDimitry Andric extern cl::opt<bool> PrintPipelinePasses;
153fe6060f1SDimitry Andric } // namespace llvm
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric static cl::opt<std::string>
1560b57cec5SDimitry Andric     ProfileRemappingFile("profile-remapping-file",
1570b57cec5SDimitry Andric                          cl::desc("Path to the profile remapping file."),
1580b57cec5SDimitry Andric                          cl::Hidden);
1590b57cec5SDimitry Andric static cl::opt<bool> DebugInfoForProfiling(
1600b57cec5SDimitry Andric     "new-pm-debug-info-for-profiling", cl::init(false), cl::Hidden,
1610b57cec5SDimitry Andric     cl::desc("Emit special debug info to enable PGO profile generation."));
162e8d8bef9SDimitry Andric static cl::opt<bool> PseudoProbeForProfiling(
163e8d8bef9SDimitry Andric     "new-pm-pseudo-probe-for-profiling", cl::init(false), cl::Hidden,
164e8d8bef9SDimitry Andric     cl::desc("Emit pseudo probes to enable PGO profile generation."));
1650b57cec5SDimitry Andric /// @}}
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric template <typename PassManagerT>
1680b57cec5SDimitry Andric bool tryParsePipelineText(PassBuilder &PB,
1690b57cec5SDimitry Andric                           const cl::opt<std::string> &PipelineOpt) {
1700b57cec5SDimitry Andric   if (PipelineOpt.empty())
1710b57cec5SDimitry Andric     return false;
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric   // Verify the pipeline is parseable:
1740b57cec5SDimitry Andric   PassManagerT PM;
1750b57cec5SDimitry Andric   if (auto Err = PB.parsePassPipeline(PM, PipelineOpt)) {
1760b57cec5SDimitry Andric     errs() << "Could not parse -" << PipelineOpt.ArgStr
1770b57cec5SDimitry Andric            << " pipeline: " << toString(std::move(Err))
1780b57cec5SDimitry Andric            << "... I'm going to ignore it.\n";
1790b57cec5SDimitry Andric     return false;
1800b57cec5SDimitry Andric   }
1810b57cec5SDimitry Andric   return true;
1820b57cec5SDimitry Andric }
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric /// If one of the EPPipeline command line options was given, register callbacks
1850b57cec5SDimitry Andric /// for parsing and inserting the given pipeline
186e8d8bef9SDimitry Andric static void registerEPCallbacks(PassBuilder &PB) {
1870b57cec5SDimitry Andric   if (tryParsePipelineText<FunctionPassManager>(PB, PeepholeEPPipeline))
1880b57cec5SDimitry Andric     PB.registerPeepholeEPCallback(
189349cc55cSDimitry Andric         [&PB](FunctionPassManager &PM, OptimizationLevel Level) {
1900b57cec5SDimitry Andric           ExitOnError Err("Unable to parse PeepholeEP pipeline: ");
191e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, PeepholeEPPipeline));
1920b57cec5SDimitry Andric         });
1930b57cec5SDimitry Andric   if (tryParsePipelineText<LoopPassManager>(PB,
1940b57cec5SDimitry Andric                                             LateLoopOptimizationsEPPipeline))
1950b57cec5SDimitry Andric     PB.registerLateLoopOptimizationsEPCallback(
196349cc55cSDimitry Andric         [&PB](LoopPassManager &PM, OptimizationLevel Level) {
1970b57cec5SDimitry Andric           ExitOnError Err("Unable to parse LateLoopOptimizationsEP pipeline: ");
198e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, LateLoopOptimizationsEPPipeline));
1990b57cec5SDimitry Andric         });
2000b57cec5SDimitry Andric   if (tryParsePipelineText<LoopPassManager>(PB, LoopOptimizerEndEPPipeline))
2010b57cec5SDimitry Andric     PB.registerLoopOptimizerEndEPCallback(
202349cc55cSDimitry Andric         [&PB](LoopPassManager &PM, OptimizationLevel Level) {
2030b57cec5SDimitry Andric           ExitOnError Err("Unable to parse LoopOptimizerEndEP pipeline: ");
204e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, LoopOptimizerEndEPPipeline));
2050b57cec5SDimitry Andric         });
2060b57cec5SDimitry Andric   if (tryParsePipelineText<FunctionPassManager>(PB,
2070b57cec5SDimitry Andric                                                 ScalarOptimizerLateEPPipeline))
2080b57cec5SDimitry Andric     PB.registerScalarOptimizerLateEPCallback(
209349cc55cSDimitry Andric         [&PB](FunctionPassManager &PM, OptimizationLevel Level) {
2100b57cec5SDimitry Andric           ExitOnError Err("Unable to parse ScalarOptimizerLateEP pipeline: ");
211e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, ScalarOptimizerLateEPPipeline));
2120b57cec5SDimitry Andric         });
2130b57cec5SDimitry Andric   if (tryParsePipelineText<CGSCCPassManager>(PB, CGSCCOptimizerLateEPPipeline))
2140b57cec5SDimitry Andric     PB.registerCGSCCOptimizerLateEPCallback(
215349cc55cSDimitry Andric         [&PB](CGSCCPassManager &PM, OptimizationLevel Level) {
2160b57cec5SDimitry Andric           ExitOnError Err("Unable to parse CGSCCOptimizerLateEP pipeline: ");
217e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, CGSCCOptimizerLateEPPipeline));
2180b57cec5SDimitry Andric         });
2190b57cec5SDimitry Andric   if (tryParsePipelineText<FunctionPassManager>(PB, VectorizerStartEPPipeline))
2200b57cec5SDimitry Andric     PB.registerVectorizerStartEPCallback(
221349cc55cSDimitry Andric         [&PB](FunctionPassManager &PM, OptimizationLevel Level) {
2220b57cec5SDimitry Andric           ExitOnError Err("Unable to parse VectorizerStartEP pipeline: ");
223e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, VectorizerStartEPPipeline));
2240b57cec5SDimitry Andric         });
2250b57cec5SDimitry Andric   if (tryParsePipelineText<ModulePassManager>(PB, PipelineStartEPPipeline))
2260b57cec5SDimitry Andric     PB.registerPipelineStartEPCallback(
227349cc55cSDimitry Andric         [&PB](ModulePassManager &PM, OptimizationLevel) {
2280b57cec5SDimitry Andric           ExitOnError Err("Unable to parse PipelineStartEP pipeline: ");
229e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, PipelineStartEPPipeline));
230e8d8bef9SDimitry Andric         });
231e8d8bef9SDimitry Andric   if (tryParsePipelineText<ModulePassManager>(
232e8d8bef9SDimitry Andric           PB, PipelineEarlySimplificationEPPipeline))
233e8d8bef9SDimitry Andric     PB.registerPipelineEarlySimplificationEPCallback(
234349cc55cSDimitry Andric         [&PB](ModulePassManager &PM, OptimizationLevel) {
235e8d8bef9SDimitry Andric           ExitOnError Err("Unable to parse EarlySimplification pipeline: ");
236e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, PipelineEarlySimplificationEPPipeline));
2370b57cec5SDimitry Andric         });
238*81ad6265SDimitry Andric   if (tryParsePipelineText<ModulePassManager>(PB, OptimizerEarlyEPPipeline))
239*81ad6265SDimitry Andric     PB.registerOptimizerEarlyEPCallback(
240*81ad6265SDimitry Andric         [&PB](ModulePassManager &PM, OptimizationLevel) {
241*81ad6265SDimitry Andric           ExitOnError Err("Unable to parse OptimizerEarlyEP pipeline: ");
242*81ad6265SDimitry Andric           Err(PB.parsePassPipeline(PM, OptimizerEarlyEPPipeline));
243*81ad6265SDimitry Andric         });
244*81ad6265SDimitry Andric   if (tryParsePipelineText<ModulePassManager>(PB, OptimizerLastEPPipeline))
2450b57cec5SDimitry Andric     PB.registerOptimizerLastEPCallback(
246349cc55cSDimitry Andric         [&PB](ModulePassManager &PM, OptimizationLevel) {
2470b57cec5SDimitry Andric           ExitOnError Err("Unable to parse OptimizerLastEP pipeline: ");
248e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, OptimizerLastEPPipeline));
2490b57cec5SDimitry Andric         });
250*81ad6265SDimitry Andric   if (tryParsePipelineText<ModulePassManager>(
251*81ad6265SDimitry Andric           PB, FullLinkTimeOptimizationEarlyEPPipeline))
252*81ad6265SDimitry Andric     PB.registerFullLinkTimeOptimizationEarlyEPCallback(
253*81ad6265SDimitry Andric         [&PB](ModulePassManager &PM, OptimizationLevel) {
254*81ad6265SDimitry Andric           ExitOnError Err(
255*81ad6265SDimitry Andric               "Unable to parse FullLinkTimeOptimizationEarlyEP pipeline: ");
256*81ad6265SDimitry Andric           Err(PB.parsePassPipeline(PM,
257*81ad6265SDimitry Andric                                    FullLinkTimeOptimizationEarlyEPPipeline));
258*81ad6265SDimitry Andric         });
259*81ad6265SDimitry Andric   if (tryParsePipelineText<ModulePassManager>(
260*81ad6265SDimitry Andric           PB, FullLinkTimeOptimizationLastEPPipeline))
261*81ad6265SDimitry Andric     PB.registerFullLinkTimeOptimizationLastEPCallback(
262*81ad6265SDimitry Andric         [&PB](ModulePassManager &PM, OptimizationLevel) {
263*81ad6265SDimitry Andric           ExitOnError Err(
264*81ad6265SDimitry Andric               "Unable to parse FullLinkTimeOptimizationLastEP pipeline: ");
265*81ad6265SDimitry Andric           Err(PB.parsePassPipeline(PM, FullLinkTimeOptimizationLastEPPipeline));
266*81ad6265SDimitry Andric         });
2670b57cec5SDimitry Andric }
2680b57cec5SDimitry Andric 
269480093f4SDimitry Andric #define HANDLE_EXTENSION(Ext)                                                  \
270480093f4SDimitry Andric   llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
271480093f4SDimitry Andric #include "llvm/Support/Extension.def"
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
274e8d8bef9SDimitry Andric                            TargetLibraryInfoImpl *TLII, ToolOutputFile *Out,
275e8d8bef9SDimitry Andric                            ToolOutputFile *ThinLTOLinkOut,
2760b57cec5SDimitry Andric                            ToolOutputFile *OptRemarkFile,
2775ffd83dbSDimitry Andric                            StringRef PassPipeline, ArrayRef<StringRef> Passes,
278*81ad6265SDimitry Andric                            ArrayRef<PassPlugin> PassPlugins,
2795ffd83dbSDimitry Andric                            OutputKind OK, VerifierKind VK,
2800b57cec5SDimitry Andric                            bool ShouldPreserveAssemblyUseListOrder,
2810b57cec5SDimitry Andric                            bool ShouldPreserveBitcodeUseListOrder,
2820b57cec5SDimitry Andric                            bool EmitSummaryIndex, bool EmitModuleHash,
283fe6060f1SDimitry Andric                            bool EnableDebugify) {
2840b57cec5SDimitry Andric   bool VerifyEachPass = VK == VK_VerifyEachPass;
2850b57cec5SDimitry Andric 
2860b57cec5SDimitry Andric   Optional<PGOOptions> P;
2870b57cec5SDimitry Andric   switch (PGOKindFlag) {
2880b57cec5SDimitry Andric   case InstrGen:
2890b57cec5SDimitry Andric     P = PGOOptions(ProfileFile, "", "", PGOOptions::IRInstr);
2900b57cec5SDimitry Andric     break;
2910b57cec5SDimitry Andric   case InstrUse:
2920b57cec5SDimitry Andric     P = PGOOptions(ProfileFile, "", ProfileRemappingFile, PGOOptions::IRUse);
2930b57cec5SDimitry Andric     break;
2940b57cec5SDimitry Andric   case SampleUse:
2950b57cec5SDimitry Andric     P = PGOOptions(ProfileFile, "", ProfileRemappingFile,
2960b57cec5SDimitry Andric                    PGOOptions::SampleUse);
2970b57cec5SDimitry Andric     break;
2980b57cec5SDimitry Andric   case NoPGO:
299349cc55cSDimitry Andric     if (DebugInfoForProfiling || PseudoProbeForProfiling)
3000b57cec5SDimitry Andric       P = PGOOptions("", "", "", PGOOptions::NoAction, PGOOptions::NoCSAction,
301349cc55cSDimitry Andric                      DebugInfoForProfiling, PseudoProbeForProfiling);
3020b57cec5SDimitry Andric     else
3030b57cec5SDimitry Andric       P = None;
3040b57cec5SDimitry Andric   }
3050b57cec5SDimitry Andric   if (CSPGOKindFlag != NoCSPGO) {
3060b57cec5SDimitry Andric     if (P && (P->Action == PGOOptions::IRInstr ||
3070b57cec5SDimitry Andric               P->Action == PGOOptions::SampleUse))
3080b57cec5SDimitry Andric       errs() << "CSPGOKind cannot be used with IRInstr or SampleUse";
3090b57cec5SDimitry Andric     if (CSPGOKindFlag == CSInstrGen) {
3100b57cec5SDimitry Andric       if (CSProfileGenFile.empty())
3110b57cec5SDimitry Andric         errs() << "CSInstrGen needs to specify CSProfileGenFile";
3120b57cec5SDimitry Andric       if (P) {
3130b57cec5SDimitry Andric         P->CSAction = PGOOptions::CSIRInstr;
3140b57cec5SDimitry Andric         P->CSProfileGenFile = CSProfileGenFile;
3150b57cec5SDimitry Andric       } else
3160b57cec5SDimitry Andric         P = PGOOptions("", CSProfileGenFile, ProfileRemappingFile,
3170b57cec5SDimitry Andric                        PGOOptions::NoAction, PGOOptions::CSIRInstr);
3180b57cec5SDimitry Andric     } else /* CSPGOKindFlag == CSInstrUse */ {
3190b57cec5SDimitry Andric       if (!P)
3200b57cec5SDimitry Andric         errs() << "CSInstrUse needs to be together with InstrUse";
3210b57cec5SDimitry Andric       P->CSAction = PGOOptions::CSIRUse;
3220b57cec5SDimitry Andric     }
3230b57cec5SDimitry Andric   }
324349cc55cSDimitry Andric   if (TM)
325349cc55cSDimitry Andric     TM->setPGOOption(P);
326349cc55cSDimitry Andric 
327fe6060f1SDimitry Andric   LoopAnalysisManager LAM;
328fe6060f1SDimitry Andric   FunctionAnalysisManager FAM;
329fe6060f1SDimitry Andric   CGSCCAnalysisManager CGAM;
330fe6060f1SDimitry Andric   ModuleAnalysisManager MAM;
331fe6060f1SDimitry Andric 
3320b57cec5SDimitry Andric   PassInstrumentationCallbacks PIC;
333fe6060f1SDimitry Andric   PrintPassOptions PrintPassOpts;
334fe6060f1SDimitry Andric   PrintPassOpts.Verbose = DebugPM == DebugLogging::Verbose;
335fe6060f1SDimitry Andric   PrintPassOpts.SkipAnalyses = DebugPM == DebugLogging::Quiet;
336fe6060f1SDimitry Andric   StandardInstrumentations SI(DebugPM != DebugLogging::None, VerifyEachPass,
337fe6060f1SDimitry Andric                               PrintPassOpts);
338fe6060f1SDimitry Andric   SI.registerCallbacks(PIC, &FAM);
339e8d8bef9SDimitry Andric   DebugifyEachInstrumentation Debugify;
340e8d8bef9SDimitry Andric   if (DebugifyEach)
341e8d8bef9SDimitry Andric     Debugify.registerCallbacks(PIC);
3420b57cec5SDimitry Andric 
3435ffd83dbSDimitry Andric   PipelineTuningOptions PTO;
3445ffd83dbSDimitry Andric   // LoopUnrolling defaults on to true and DisableLoopUnrolling is initialized
3455ffd83dbSDimitry Andric   // to false above so we shouldn't necessarily need to check whether or not the
3465ffd83dbSDimitry Andric   // option has been enabled.
3475ffd83dbSDimitry Andric   PTO.LoopUnrolling = !DisableLoopUnrolling;
348fe6060f1SDimitry Andric   PassBuilder PB(TM, PTO, P, &PIC);
349e8d8bef9SDimitry Andric   registerEPCallbacks(PB);
3500b57cec5SDimitry Andric 
351*81ad6265SDimitry Andric   // For any loaded plugins, let them register pass builder callbacks.
352*81ad6265SDimitry Andric   for (auto &PassPlugin : PassPlugins)
353*81ad6265SDimitry Andric     PassPlugin.registerPassBuilderCallbacks(PB);
3540b57cec5SDimitry Andric 
355e8d8bef9SDimitry Andric   PB.registerPipelineParsingCallback(
356e8d8bef9SDimitry Andric       [](StringRef Name, ModulePassManager &MPM,
357e8d8bef9SDimitry Andric          ArrayRef<PassBuilder::PipelineElement>) {
358349cc55cSDimitry Andric         AddressSanitizerOptions Opts;
359e8d8bef9SDimitry Andric         if (Name == "asan-pipeline") {
360349cc55cSDimitry Andric           MPM.addPass(ModuleAddressSanitizerPass(Opts));
361e8d8bef9SDimitry Andric           return true;
362e8d8bef9SDimitry Andric         }
363e8d8bef9SDimitry Andric         return false;
364e8d8bef9SDimitry Andric       });
3650b57cec5SDimitry Andric 
366480093f4SDimitry Andric #define HANDLE_EXTENSION(Ext)                                                  \
367480093f4SDimitry Andric   get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
368480093f4SDimitry Andric #include "llvm/Support/Extension.def"
3690b57cec5SDimitry Andric 
3700b57cec5SDimitry Andric   // Specially handle the alias analysis manager so that we can register
3710b57cec5SDimitry Andric   // a custom pipeline of AA passes with it.
3720b57cec5SDimitry Andric   AAManager AA;
373e8d8bef9SDimitry Andric   if (Passes.empty()) {
3740b57cec5SDimitry Andric     if (auto Err = PB.parseAAPipeline(AA, AAPipeline)) {
3750b57cec5SDimitry Andric       errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
3760b57cec5SDimitry Andric       return false;
3770b57cec5SDimitry Andric     }
3785ffd83dbSDimitry Andric   }
379e8d8bef9SDimitry Andric 
380e8d8bef9SDimitry Andric   // For compatibility with the legacy PM AA pipeline.
381e8d8bef9SDimitry Andric   // AAResultsWrapperPass by default provides basic-aa in the legacy PM
382e8d8bef9SDimitry Andric   // unless -disable-basic-aa is specified.
383e8d8bef9SDimitry Andric   // TODO: remove this once tests implicitly requiring basic-aa use -passes= and
384e8d8bef9SDimitry Andric   // -aa-pipeline=basic-aa.
385e8d8bef9SDimitry Andric   if (!Passes.empty() && !DisableBasicAA) {
386e8d8bef9SDimitry Andric     if (auto Err = PB.parseAAPipeline(AA, "basic-aa")) {
387e8d8bef9SDimitry Andric       errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
388e8d8bef9SDimitry Andric       return false;
389e8d8bef9SDimitry Andric     }
390e8d8bef9SDimitry Andric   }
391e8d8bef9SDimitry Andric 
3925ffd83dbSDimitry Andric   // For compatibility with legacy pass manager.
3935ffd83dbSDimitry Andric   // Alias analyses are not specially specified when using the legacy PM.
3945ffd83dbSDimitry Andric   for (auto PassName : Passes) {
3955ffd83dbSDimitry Andric     if (PB.isAAPassName(PassName)) {
3965ffd83dbSDimitry Andric       if (auto Err = PB.parseAAPipeline(AA, PassName)) {
3975ffd83dbSDimitry Andric         errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
3985ffd83dbSDimitry Andric         return false;
3995ffd83dbSDimitry Andric       }
4005ffd83dbSDimitry Andric     }
4015ffd83dbSDimitry Andric   }
4020b57cec5SDimitry Andric 
4030b57cec5SDimitry Andric   // Register the AA manager first so that our version is the one used.
4040b57cec5SDimitry Andric   FAM.registerPass([&] { return std::move(AA); });
405e8d8bef9SDimitry Andric   // Register our TargetLibraryInfoImpl.
406e8d8bef9SDimitry Andric   FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
4070b57cec5SDimitry Andric 
4080b57cec5SDimitry Andric   // Register all the basic analyses with the managers.
4090b57cec5SDimitry Andric   PB.registerModuleAnalyses(MAM);
4100b57cec5SDimitry Andric   PB.registerCGSCCAnalyses(CGAM);
4110b57cec5SDimitry Andric   PB.registerFunctionAnalyses(FAM);
4120b57cec5SDimitry Andric   PB.registerLoopAnalyses(LAM);
4130b57cec5SDimitry Andric   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
4140b57cec5SDimitry Andric 
415fe6060f1SDimitry Andric   ModulePassManager MPM;
4160b57cec5SDimitry Andric   if (VK > VK_NoVerifier)
4170b57cec5SDimitry Andric     MPM.addPass(VerifierPass());
4180b57cec5SDimitry Andric   if (EnableDebugify)
4190b57cec5SDimitry Andric     MPM.addPass(NewPMDebugifyPass());
4200b57cec5SDimitry Andric 
421349cc55cSDimitry Andric   // Add passes according to the -passes options.
4225ffd83dbSDimitry Andric   if (!PassPipeline.empty()) {
4235ffd83dbSDimitry Andric     assert(Passes.empty() &&
4245ffd83dbSDimitry Andric            "PassPipeline and Passes should not both contain passes");
425e8d8bef9SDimitry Andric     if (auto Err = PB.parsePassPipeline(MPM, PassPipeline)) {
4260b57cec5SDimitry Andric       errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
4270b57cec5SDimitry Andric       return false;
4280b57cec5SDimitry Andric     }
4295ffd83dbSDimitry Andric   }
430349cc55cSDimitry Andric   // Add passes specified using the legacy PM syntax (i.e. not using
431349cc55cSDimitry Andric   // -passes). This should be removed later when such support has been
432349cc55cSDimitry Andric   // deprecated, i.e. when all lit tests running opt (and not using
433349cc55cSDimitry Andric   // -enable-new-pm=0) have been updated to use -passes.
434e8d8bef9SDimitry Andric   for (auto PassName : Passes) {
4355ffd83dbSDimitry Andric     std::string ModifiedPassName(PassName.begin(), PassName.end());
4365ffd83dbSDimitry Andric     if (PB.isAnalysisPassName(PassName))
4375ffd83dbSDimitry Andric       ModifiedPassName = "require<" + ModifiedPassName + ">";
438349cc55cSDimitry Andric     // FIXME: These translations are supposed to be removed when lit tests that
439349cc55cSDimitry Andric     // use these names have been updated to use the -passes syntax (and when the
440349cc55cSDimitry Andric     // support for using the old syntax to specify passes is considered as
441349cc55cSDimitry Andric     // deprecated for the new PM).
442349cc55cSDimitry Andric     if (ModifiedPassName == "early-cse-memssa")
443349cc55cSDimitry Andric       ModifiedPassName = "early-cse<memssa>";
444349cc55cSDimitry Andric     else if (ModifiedPassName == "post-inline-ee-instrument")
445349cc55cSDimitry Andric       ModifiedPassName = "ee-instrument<post-inline>";
446349cc55cSDimitry Andric     else if (ModifiedPassName == "loop-extract-single")
447349cc55cSDimitry Andric       ModifiedPassName = "loop-extract<single>";
448349cc55cSDimitry Andric     else if (ModifiedPassName == "lower-matrix-intrinsics-minimal")
449349cc55cSDimitry Andric       ModifiedPassName = "lower-matrix-intrinsics<minimal>";
450e8d8bef9SDimitry Andric     if (auto Err = PB.parsePassPipeline(MPM, ModifiedPassName)) {
4515ffd83dbSDimitry Andric       errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
4525ffd83dbSDimitry Andric       return false;
4535ffd83dbSDimitry Andric     }
4545ffd83dbSDimitry Andric   }
4550b57cec5SDimitry Andric 
4560b57cec5SDimitry Andric   if (VK > VK_NoVerifier)
4570b57cec5SDimitry Andric     MPM.addPass(VerifierPass());
4580b57cec5SDimitry Andric   if (EnableDebugify)
4590b57cec5SDimitry Andric     MPM.addPass(NewPMCheckDebugifyPass());
4600b57cec5SDimitry Andric 
4610b57cec5SDimitry Andric   // Add any relevant output pass at the end of the pipeline.
4620b57cec5SDimitry Andric   switch (OK) {
4630b57cec5SDimitry Andric   case OK_NoOutput:
4640b57cec5SDimitry Andric     break; // No output pass needed.
4650b57cec5SDimitry Andric   case OK_OutputAssembly:
4660b57cec5SDimitry Andric     MPM.addPass(
4670b57cec5SDimitry Andric         PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder));
4680b57cec5SDimitry Andric     break;
4690b57cec5SDimitry Andric   case OK_OutputBitcode:
4700b57cec5SDimitry Andric     MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder,
4710b57cec5SDimitry Andric                                   EmitSummaryIndex, EmitModuleHash));
4720b57cec5SDimitry Andric     break;
4730b57cec5SDimitry Andric   case OK_OutputThinLTOBitcode:
4740b57cec5SDimitry Andric     MPM.addPass(ThinLTOBitcodeWriterPass(
4750b57cec5SDimitry Andric         Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr));
4760b57cec5SDimitry Andric     break;
4770b57cec5SDimitry Andric   }
4780b57cec5SDimitry Andric 
4790b57cec5SDimitry Andric   // Before executing passes, print the final values of the LLVM options.
4800b57cec5SDimitry Andric   cl::PrintOptionValues();
4810b57cec5SDimitry Andric 
482349cc55cSDimitry Andric   // Print a textual, '-passes=' compatible, representation of pipeline if
483349cc55cSDimitry Andric   // requested.
484349cc55cSDimitry Andric   if (PrintPipelinePasses) {
485349cc55cSDimitry Andric     MPM.printPipeline(outs(), [&PIC](StringRef ClassName) {
486349cc55cSDimitry Andric       auto PassName = PIC.getPassNameForClassName(ClassName);
487349cc55cSDimitry Andric       return PassName.empty() ? ClassName : PassName;
488349cc55cSDimitry Andric     });
489349cc55cSDimitry Andric     outs() << "\n";
490349cc55cSDimitry Andric     return true;
491349cc55cSDimitry Andric   }
492349cc55cSDimitry Andric 
4930b57cec5SDimitry Andric   // Now that we have all of the passes ready, run them.
4940b57cec5SDimitry Andric   MPM.run(M, MAM);
4950b57cec5SDimitry Andric 
4960b57cec5SDimitry Andric   // Declare success.
4970b57cec5SDimitry Andric   if (OK != OK_NoOutput) {
4980b57cec5SDimitry Andric     Out->keep();
4990b57cec5SDimitry Andric     if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut)
5000b57cec5SDimitry Andric       ThinLTOLinkOut->keep();
5010b57cec5SDimitry Andric   }
5020b57cec5SDimitry Andric 
5030b57cec5SDimitry Andric   if (OptRemarkFile)
5040b57cec5SDimitry Andric     OptRemarkFile->keep();
5050b57cec5SDimitry Andric 
506e8d8bef9SDimitry Andric   if (DebugifyEach && !DebugifyExport.empty())
507e8d8bef9SDimitry Andric     exportDebugifyStats(DebugifyExport, Debugify.StatsMap);
508e8d8bef9SDimitry Andric 
5090b57cec5SDimitry Andric   return true;
5100b57cec5SDimitry Andric }
511fe6060f1SDimitry Andric 
512fe6060f1SDimitry Andric void llvm::printPasses(raw_ostream &OS) {
513fe6060f1SDimitry Andric   PassBuilder PB;
514fe6060f1SDimitry Andric   PB.printPassNames(OS);
515fe6060f1SDimitry Andric }
516