xref: /freebsd/contrib/llvm-project/llvm/tools/opt/NewPMDriver.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
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"
160b57cec5SDimitry Andric #include "PassPrinters.h"
175ffd83dbSDimitry Andric #include "llvm/ADT/SmallVector.h"
180b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
190b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
200b57cec5SDimitry Andric #include "llvm/Analysis/CGSCCPassManager.h"
21e8d8bef9SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
220b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeWriterPass.h"
230b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
240b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
250b57cec5SDimitry Andric #include "llvm/IR/IRPrintingPasses.h"
260b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h"
270b57cec5SDimitry Andric #include "llvm/IR/Module.h"
280b57cec5SDimitry Andric #include "llvm/IR/PassManager.h"
290b57cec5SDimitry Andric #include "llvm/IR/Verifier.h"
300b57cec5SDimitry Andric #include "llvm/Passes/PassBuilder.h"
310b57cec5SDimitry Andric #include "llvm/Passes/PassPlugin.h"
320b57cec5SDimitry Andric #include "llvm/Passes/StandardInstrumentations.h"
330b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
340b57cec5SDimitry Andric #include "llvm/Support/ToolOutputFile.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"));
53e8d8bef9SDimitry Andric } // namespace llvm
54e8d8bef9SDimitry Andric 
55*fe6060f1SDimitry Andric enum class DebugLogging { None, Normal, Verbose, Quiet };
56*fe6060f1SDimitry Andric 
57*fe6060f1SDimitry Andric static cl::opt<DebugLogging> DebugPM(
58*fe6060f1SDimitry Andric     "debug-pass-manager", cl::Hidden, cl::ValueOptional,
59*fe6060f1SDimitry Andric     cl::desc("Print pass management debugging information"),
60*fe6060f1SDimitry Andric     cl::init(DebugLogging::None),
61*fe6060f1SDimitry Andric     cl::values(
62*fe6060f1SDimitry Andric         clEnumValN(DebugLogging::Normal, "", ""),
63*fe6060f1SDimitry Andric         clEnumValN(DebugLogging::Quiet, "quiet",
64*fe6060f1SDimitry Andric                    "Skip printing info about analyses"),
65*fe6060f1SDimitry Andric         clEnumValN(
66*fe6060f1SDimitry Andric             DebugLogging::Verbose, "verbose",
67*fe6060f1SDimitry Andric             "Print extra information about adaptors and pass managers")));
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric static cl::list<std::string>
700b57cec5SDimitry Andric     PassPlugins("load-pass-plugin",
710b57cec5SDimitry Andric                 cl::desc("Load passes from plugin library"));
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric // This flag specifies a textual description of the alias analysis pipeline to
740b57cec5SDimitry Andric // use when querying for aliasing information. It only works in concert with
750b57cec5SDimitry Andric // the "passes" flag above.
760b57cec5SDimitry Andric static cl::opt<std::string>
770b57cec5SDimitry Andric     AAPipeline("aa-pipeline",
780b57cec5SDimitry Andric                cl::desc("A textual description of the alias analysis "
790b57cec5SDimitry Andric                         "pipeline for handling managed aliasing queries"),
80e8d8bef9SDimitry Andric                cl::Hidden, cl::init("default"));
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric /// {{@ These options accept textual pipeline descriptions which will be
830b57cec5SDimitry Andric /// inserted into default pipelines at the respective extension points
840b57cec5SDimitry Andric static cl::opt<std::string> PeepholeEPPipeline(
850b57cec5SDimitry Andric     "passes-ep-peephole",
860b57cec5SDimitry Andric     cl::desc("A textual description of the function pass pipeline inserted at "
870b57cec5SDimitry Andric              "the Peephole extension points into default pipelines"),
880b57cec5SDimitry Andric     cl::Hidden);
890b57cec5SDimitry Andric static cl::opt<std::string> LateLoopOptimizationsEPPipeline(
900b57cec5SDimitry Andric     "passes-ep-late-loop-optimizations",
910b57cec5SDimitry Andric     cl::desc(
920b57cec5SDimitry Andric         "A textual description of the loop pass pipeline inserted at "
930b57cec5SDimitry Andric         "the LateLoopOptimizations extension point into default pipelines"),
940b57cec5SDimitry Andric     cl::Hidden);
950b57cec5SDimitry Andric static cl::opt<std::string> LoopOptimizerEndEPPipeline(
960b57cec5SDimitry Andric     "passes-ep-loop-optimizer-end",
970b57cec5SDimitry Andric     cl::desc("A textual description of the loop pass pipeline inserted at "
980b57cec5SDimitry Andric              "the LoopOptimizerEnd extension point into default pipelines"),
990b57cec5SDimitry Andric     cl::Hidden);
1000b57cec5SDimitry Andric static cl::opt<std::string> ScalarOptimizerLateEPPipeline(
1010b57cec5SDimitry Andric     "passes-ep-scalar-optimizer-late",
1020b57cec5SDimitry Andric     cl::desc("A textual description of the function pass pipeline inserted at "
1030b57cec5SDimitry Andric              "the ScalarOptimizerLate extension point into default pipelines"),
1040b57cec5SDimitry Andric     cl::Hidden);
1050b57cec5SDimitry Andric static cl::opt<std::string> CGSCCOptimizerLateEPPipeline(
1060b57cec5SDimitry Andric     "passes-ep-cgscc-optimizer-late",
1070b57cec5SDimitry Andric     cl::desc("A textual description of the cgscc pass pipeline inserted at "
1080b57cec5SDimitry Andric              "the CGSCCOptimizerLate extension point into default pipelines"),
1090b57cec5SDimitry Andric     cl::Hidden);
1100b57cec5SDimitry Andric static cl::opt<std::string> VectorizerStartEPPipeline(
1110b57cec5SDimitry Andric     "passes-ep-vectorizer-start",
1120b57cec5SDimitry Andric     cl::desc("A textual description of the function pass pipeline inserted at "
1130b57cec5SDimitry Andric              "the VectorizerStart extension point into default pipelines"),
1140b57cec5SDimitry Andric     cl::Hidden);
1150b57cec5SDimitry Andric static cl::opt<std::string> PipelineStartEPPipeline(
1160b57cec5SDimitry Andric     "passes-ep-pipeline-start",
117e8d8bef9SDimitry Andric     cl::desc("A textual description of the module pass pipeline inserted at "
1180b57cec5SDimitry Andric              "the PipelineStart extension point into default pipelines"),
1190b57cec5SDimitry Andric     cl::Hidden);
120e8d8bef9SDimitry Andric static cl::opt<std::string> PipelineEarlySimplificationEPPipeline(
121e8d8bef9SDimitry Andric     "passes-ep-pipeline-early-simplification",
122e8d8bef9SDimitry Andric     cl::desc("A textual description of the module pass pipeline inserted at "
123e8d8bef9SDimitry Andric              "the EarlySimplification extension point into default pipelines"),
124e8d8bef9SDimitry 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);
1300b57cec5SDimitry Andric 
1315ffd83dbSDimitry Andric // Individual pipeline tuning options.
1325ffd83dbSDimitry Andric extern cl::opt<bool> DisableLoopUnrolling;
1335ffd83dbSDimitry Andric 
134*fe6060f1SDimitry Andric namespace llvm {
1350b57cec5SDimitry Andric extern cl::opt<PGOKind> PGOKindFlag;
1360b57cec5SDimitry Andric extern cl::opt<std::string> ProfileFile;
1370b57cec5SDimitry Andric extern cl::opt<CSPGOKind> CSPGOKindFlag;
1380b57cec5SDimitry Andric extern cl::opt<std::string> CSProfileGenFile;
139e8d8bef9SDimitry Andric extern cl::opt<bool> DisableBasicAA;
140*fe6060f1SDimitry Andric } // namespace llvm
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric static cl::opt<std::string>
1430b57cec5SDimitry Andric     ProfileRemappingFile("profile-remapping-file",
1440b57cec5SDimitry Andric                          cl::desc("Path to the profile remapping file."),
1450b57cec5SDimitry Andric                          cl::Hidden);
1460b57cec5SDimitry Andric static cl::opt<bool> DebugInfoForProfiling(
1470b57cec5SDimitry Andric     "new-pm-debug-info-for-profiling", cl::init(false), cl::Hidden,
1480b57cec5SDimitry Andric     cl::desc("Emit special debug info to enable PGO profile generation."));
149e8d8bef9SDimitry Andric static cl::opt<bool> PseudoProbeForProfiling(
150e8d8bef9SDimitry Andric     "new-pm-pseudo-probe-for-profiling", cl::init(false), cl::Hidden,
151e8d8bef9SDimitry Andric     cl::desc("Emit pseudo probes to enable PGO profile generation."));
1520b57cec5SDimitry Andric /// @}}
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric template <typename PassManagerT>
1550b57cec5SDimitry Andric bool tryParsePipelineText(PassBuilder &PB,
1560b57cec5SDimitry Andric                           const cl::opt<std::string> &PipelineOpt) {
1570b57cec5SDimitry Andric   if (PipelineOpt.empty())
1580b57cec5SDimitry Andric     return false;
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric   // Verify the pipeline is parseable:
1610b57cec5SDimitry Andric   PassManagerT PM;
1620b57cec5SDimitry Andric   if (auto Err = PB.parsePassPipeline(PM, PipelineOpt)) {
1630b57cec5SDimitry Andric     errs() << "Could not parse -" << PipelineOpt.ArgStr
1640b57cec5SDimitry Andric            << " pipeline: " << toString(std::move(Err))
1650b57cec5SDimitry Andric            << "... I'm going to ignore it.\n";
1660b57cec5SDimitry Andric     return false;
1670b57cec5SDimitry Andric   }
1680b57cec5SDimitry Andric   return true;
1690b57cec5SDimitry Andric }
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric /// If one of the EPPipeline command line options was given, register callbacks
1720b57cec5SDimitry Andric /// for parsing and inserting the given pipeline
173e8d8bef9SDimitry Andric static void registerEPCallbacks(PassBuilder &PB) {
1740b57cec5SDimitry Andric   if (tryParsePipelineText<FunctionPassManager>(PB, PeepholeEPPipeline))
1750b57cec5SDimitry Andric     PB.registerPeepholeEPCallback(
176e8d8bef9SDimitry Andric         [&PB](FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
1770b57cec5SDimitry Andric           ExitOnError Err("Unable to parse PeepholeEP pipeline: ");
178e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, PeepholeEPPipeline));
1790b57cec5SDimitry Andric         });
1800b57cec5SDimitry Andric   if (tryParsePipelineText<LoopPassManager>(PB,
1810b57cec5SDimitry Andric                                             LateLoopOptimizationsEPPipeline))
1820b57cec5SDimitry Andric     PB.registerLateLoopOptimizationsEPCallback(
183e8d8bef9SDimitry Andric         [&PB](LoopPassManager &PM, PassBuilder::OptimizationLevel Level) {
1840b57cec5SDimitry Andric           ExitOnError Err("Unable to parse LateLoopOptimizationsEP pipeline: ");
185e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, LateLoopOptimizationsEPPipeline));
1860b57cec5SDimitry Andric         });
1870b57cec5SDimitry Andric   if (tryParsePipelineText<LoopPassManager>(PB, LoopOptimizerEndEPPipeline))
1880b57cec5SDimitry Andric     PB.registerLoopOptimizerEndEPCallback(
189e8d8bef9SDimitry Andric         [&PB](LoopPassManager &PM, PassBuilder::OptimizationLevel Level) {
1900b57cec5SDimitry Andric           ExitOnError Err("Unable to parse LoopOptimizerEndEP pipeline: ");
191e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, LoopOptimizerEndEPPipeline));
1920b57cec5SDimitry Andric         });
1930b57cec5SDimitry Andric   if (tryParsePipelineText<FunctionPassManager>(PB,
1940b57cec5SDimitry Andric                                                 ScalarOptimizerLateEPPipeline))
1950b57cec5SDimitry Andric     PB.registerScalarOptimizerLateEPCallback(
196e8d8bef9SDimitry Andric         [&PB](FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
1970b57cec5SDimitry Andric           ExitOnError Err("Unable to parse ScalarOptimizerLateEP pipeline: ");
198e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, ScalarOptimizerLateEPPipeline));
1990b57cec5SDimitry Andric         });
2000b57cec5SDimitry Andric   if (tryParsePipelineText<CGSCCPassManager>(PB, CGSCCOptimizerLateEPPipeline))
2010b57cec5SDimitry Andric     PB.registerCGSCCOptimizerLateEPCallback(
202e8d8bef9SDimitry Andric         [&PB](CGSCCPassManager &PM, PassBuilder::OptimizationLevel Level) {
2030b57cec5SDimitry Andric           ExitOnError Err("Unable to parse CGSCCOptimizerLateEP pipeline: ");
204e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, CGSCCOptimizerLateEPPipeline));
2050b57cec5SDimitry Andric         });
2060b57cec5SDimitry Andric   if (tryParsePipelineText<FunctionPassManager>(PB, VectorizerStartEPPipeline))
2070b57cec5SDimitry Andric     PB.registerVectorizerStartEPCallback(
208e8d8bef9SDimitry Andric         [&PB](FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
2090b57cec5SDimitry Andric           ExitOnError Err("Unable to parse VectorizerStartEP pipeline: ");
210e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, VectorizerStartEPPipeline));
2110b57cec5SDimitry Andric         });
2120b57cec5SDimitry Andric   if (tryParsePipelineText<ModulePassManager>(PB, PipelineStartEPPipeline))
2130b57cec5SDimitry Andric     PB.registerPipelineStartEPCallback(
214e8d8bef9SDimitry Andric         [&PB](ModulePassManager &PM, PassBuilder::OptimizationLevel) {
2150b57cec5SDimitry Andric           ExitOnError Err("Unable to parse PipelineStartEP pipeline: ");
216e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, PipelineStartEPPipeline));
217e8d8bef9SDimitry Andric         });
218e8d8bef9SDimitry Andric   if (tryParsePipelineText<ModulePassManager>(
219e8d8bef9SDimitry Andric           PB, PipelineEarlySimplificationEPPipeline))
220e8d8bef9SDimitry Andric     PB.registerPipelineEarlySimplificationEPCallback(
221e8d8bef9SDimitry Andric         [&PB](ModulePassManager &PM, PassBuilder::OptimizationLevel) {
222e8d8bef9SDimitry Andric           ExitOnError Err("Unable to parse EarlySimplification pipeline: ");
223e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, PipelineEarlySimplificationEPPipeline));
2240b57cec5SDimitry Andric         });
2250b57cec5SDimitry Andric   if (tryParsePipelineText<FunctionPassManager>(PB, OptimizerLastEPPipeline))
2260b57cec5SDimitry Andric     PB.registerOptimizerLastEPCallback(
227e8d8bef9SDimitry Andric         [&PB](ModulePassManager &PM, PassBuilder::OptimizationLevel) {
2280b57cec5SDimitry Andric           ExitOnError Err("Unable to parse OptimizerLastEP pipeline: ");
229e8d8bef9SDimitry Andric           Err(PB.parsePassPipeline(PM, OptimizerLastEPPipeline));
2300b57cec5SDimitry Andric         });
2310b57cec5SDimitry Andric }
2320b57cec5SDimitry Andric 
233480093f4SDimitry Andric #define HANDLE_EXTENSION(Ext)                                                  \
234480093f4SDimitry Andric   llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
235480093f4SDimitry Andric #include "llvm/Support/Extension.def"
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
238e8d8bef9SDimitry Andric                            TargetLibraryInfoImpl *TLII, ToolOutputFile *Out,
239e8d8bef9SDimitry Andric                            ToolOutputFile *ThinLTOLinkOut,
2400b57cec5SDimitry Andric                            ToolOutputFile *OptRemarkFile,
2415ffd83dbSDimitry Andric                            StringRef PassPipeline, ArrayRef<StringRef> Passes,
2425ffd83dbSDimitry Andric                            OutputKind OK, VerifierKind VK,
2430b57cec5SDimitry Andric                            bool ShouldPreserveAssemblyUseListOrder,
2440b57cec5SDimitry Andric                            bool ShouldPreserveBitcodeUseListOrder,
2450b57cec5SDimitry Andric                            bool EmitSummaryIndex, bool EmitModuleHash,
246*fe6060f1SDimitry Andric                            bool EnableDebugify) {
2470b57cec5SDimitry Andric   bool VerifyEachPass = VK == VK_VerifyEachPass;
2480b57cec5SDimitry Andric 
2490b57cec5SDimitry Andric   Optional<PGOOptions> P;
2500b57cec5SDimitry Andric   switch (PGOKindFlag) {
2510b57cec5SDimitry Andric   case InstrGen:
2520b57cec5SDimitry Andric     P = PGOOptions(ProfileFile, "", "", PGOOptions::IRInstr);
2530b57cec5SDimitry Andric     break;
2540b57cec5SDimitry Andric   case InstrUse:
2550b57cec5SDimitry Andric     P = PGOOptions(ProfileFile, "", ProfileRemappingFile, PGOOptions::IRUse);
2560b57cec5SDimitry Andric     break;
2570b57cec5SDimitry Andric   case SampleUse:
2580b57cec5SDimitry Andric     P = PGOOptions(ProfileFile, "", ProfileRemappingFile,
2590b57cec5SDimitry Andric                    PGOOptions::SampleUse);
2600b57cec5SDimitry Andric     break;
2610b57cec5SDimitry Andric   case NoPGO:
2620b57cec5SDimitry Andric     if (DebugInfoForProfiling)
2630b57cec5SDimitry Andric       P = PGOOptions("", "", "", PGOOptions::NoAction, PGOOptions::NoCSAction,
2640b57cec5SDimitry Andric                      true);
265e8d8bef9SDimitry Andric     else if (PseudoProbeForProfiling)
266e8d8bef9SDimitry Andric       P = PGOOptions("", "", "", PGOOptions::NoAction, PGOOptions::NoCSAction,
267e8d8bef9SDimitry Andric                      false, true);
2680b57cec5SDimitry Andric     else
2690b57cec5SDimitry Andric       P = None;
2700b57cec5SDimitry Andric   }
2710b57cec5SDimitry Andric   if (CSPGOKindFlag != NoCSPGO) {
2720b57cec5SDimitry Andric     if (P && (P->Action == PGOOptions::IRInstr ||
2730b57cec5SDimitry Andric               P->Action == PGOOptions::SampleUse))
2740b57cec5SDimitry Andric       errs() << "CSPGOKind cannot be used with IRInstr or SampleUse";
2750b57cec5SDimitry Andric     if (CSPGOKindFlag == CSInstrGen) {
2760b57cec5SDimitry Andric       if (CSProfileGenFile.empty())
2770b57cec5SDimitry Andric         errs() << "CSInstrGen needs to specify CSProfileGenFile";
2780b57cec5SDimitry Andric       if (P) {
2790b57cec5SDimitry Andric         P->CSAction = PGOOptions::CSIRInstr;
2800b57cec5SDimitry Andric         P->CSProfileGenFile = CSProfileGenFile;
2810b57cec5SDimitry Andric       } else
2820b57cec5SDimitry Andric         P = PGOOptions("", CSProfileGenFile, ProfileRemappingFile,
2830b57cec5SDimitry Andric                        PGOOptions::NoAction, PGOOptions::CSIRInstr);
2840b57cec5SDimitry Andric     } else /* CSPGOKindFlag == CSInstrUse */ {
2850b57cec5SDimitry Andric       if (!P)
2860b57cec5SDimitry Andric         errs() << "CSInstrUse needs to be together with InstrUse";
2870b57cec5SDimitry Andric       P->CSAction = PGOOptions::CSIRUse;
2880b57cec5SDimitry Andric     }
2890b57cec5SDimitry Andric   }
290*fe6060f1SDimitry Andric   LoopAnalysisManager LAM;
291*fe6060f1SDimitry Andric   FunctionAnalysisManager FAM;
292*fe6060f1SDimitry Andric   CGSCCAnalysisManager CGAM;
293*fe6060f1SDimitry Andric   ModuleAnalysisManager MAM;
294*fe6060f1SDimitry Andric 
2950b57cec5SDimitry Andric   PassInstrumentationCallbacks PIC;
296*fe6060f1SDimitry Andric   PrintPassOptions PrintPassOpts;
297*fe6060f1SDimitry Andric   PrintPassOpts.Verbose = DebugPM == DebugLogging::Verbose;
298*fe6060f1SDimitry Andric   PrintPassOpts.SkipAnalyses = DebugPM == DebugLogging::Quiet;
299*fe6060f1SDimitry Andric   StandardInstrumentations SI(DebugPM != DebugLogging::None, VerifyEachPass,
300*fe6060f1SDimitry Andric                               PrintPassOpts);
301*fe6060f1SDimitry Andric   SI.registerCallbacks(PIC, &FAM);
302e8d8bef9SDimitry Andric   DebugifyEachInstrumentation Debugify;
303e8d8bef9SDimitry Andric   if (DebugifyEach)
304e8d8bef9SDimitry Andric     Debugify.registerCallbacks(PIC);
3050b57cec5SDimitry Andric 
3065ffd83dbSDimitry Andric   PipelineTuningOptions PTO;
3075ffd83dbSDimitry Andric   // LoopUnrolling defaults on to true and DisableLoopUnrolling is initialized
3085ffd83dbSDimitry Andric   // to false above so we shouldn't necessarily need to check whether or not the
3095ffd83dbSDimitry Andric   // option has been enabled.
3105ffd83dbSDimitry Andric   PTO.LoopUnrolling = !DisableLoopUnrolling;
311*fe6060f1SDimitry Andric   PassBuilder PB(TM, PTO, P, &PIC);
312e8d8bef9SDimitry Andric   registerEPCallbacks(PB);
3130b57cec5SDimitry Andric 
3140b57cec5SDimitry Andric   // Load requested pass plugins and let them register pass builder callbacks
3150b57cec5SDimitry Andric   for (auto &PluginFN : PassPlugins) {
3160b57cec5SDimitry Andric     auto PassPlugin = PassPlugin::Load(PluginFN);
3170b57cec5SDimitry Andric     if (!PassPlugin) {
3180b57cec5SDimitry Andric       errs() << "Failed to load passes from '" << PluginFN
3190b57cec5SDimitry Andric              << "'. Request ignored.\n";
3200b57cec5SDimitry Andric       continue;
3210b57cec5SDimitry Andric     }
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric     PassPlugin->registerPassBuilderCallbacks(PB);
3240b57cec5SDimitry Andric   }
3250b57cec5SDimitry Andric 
3260b57cec5SDimitry Andric   // Register a callback that creates the debugify passes as needed.
3270b57cec5SDimitry Andric   PB.registerPipelineParsingCallback(
3280b57cec5SDimitry Andric       [](StringRef Name, ModulePassManager &MPM,
3290b57cec5SDimitry Andric          ArrayRef<PassBuilder::PipelineElement>) {
3300b57cec5SDimitry Andric         if (Name == "debugify") {
3310b57cec5SDimitry Andric           MPM.addPass(NewPMDebugifyPass());
3320b57cec5SDimitry Andric           return true;
3330b57cec5SDimitry Andric         } else if (Name == "check-debugify") {
3340b57cec5SDimitry Andric           MPM.addPass(NewPMCheckDebugifyPass());
3350b57cec5SDimitry Andric           return true;
3360b57cec5SDimitry Andric         }
3370b57cec5SDimitry Andric         return false;
3380b57cec5SDimitry Andric       });
339e8d8bef9SDimitry Andric   PB.registerPipelineParsingCallback(
340e8d8bef9SDimitry Andric       [](StringRef Name, ModulePassManager &MPM,
341e8d8bef9SDimitry Andric          ArrayRef<PassBuilder::PipelineElement>) {
342e8d8bef9SDimitry Andric         if (Name == "asan-pipeline") {
343e8d8bef9SDimitry Andric           MPM.addPass(
344e8d8bef9SDimitry Andric               RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
345e8d8bef9SDimitry Andric           MPM.addPass(
346e8d8bef9SDimitry Andric               createModuleToFunctionPassAdaptor(AddressSanitizerPass()));
347e8d8bef9SDimitry Andric           MPM.addPass(ModuleAddressSanitizerPass());
348e8d8bef9SDimitry Andric           return true;
349e8d8bef9SDimitry Andric         } else if (Name == "asan-function-pipeline") {
350e8d8bef9SDimitry Andric           MPM.addPass(
351e8d8bef9SDimitry Andric               RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
352e8d8bef9SDimitry Andric           MPM.addPass(
353e8d8bef9SDimitry Andric               createModuleToFunctionPassAdaptor(AddressSanitizerPass()));
354e8d8bef9SDimitry Andric           return true;
355e8d8bef9SDimitry Andric         }
356e8d8bef9SDimitry Andric         return false;
357e8d8bef9SDimitry Andric       });
3580b57cec5SDimitry Andric 
359480093f4SDimitry Andric #define HANDLE_EXTENSION(Ext)                                                  \
360480093f4SDimitry Andric   get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
361480093f4SDimitry Andric #include "llvm/Support/Extension.def"
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric   // Specially handle the alias analysis manager so that we can register
3640b57cec5SDimitry Andric   // a custom pipeline of AA passes with it.
3650b57cec5SDimitry Andric   AAManager AA;
366e8d8bef9SDimitry Andric   if (Passes.empty()) {
3670b57cec5SDimitry Andric     if (auto Err = PB.parseAAPipeline(AA, AAPipeline)) {
3680b57cec5SDimitry Andric       errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
3690b57cec5SDimitry Andric       return false;
3700b57cec5SDimitry Andric     }
3715ffd83dbSDimitry Andric   }
372e8d8bef9SDimitry Andric 
373e8d8bef9SDimitry Andric   // For compatibility with the legacy PM AA pipeline.
374e8d8bef9SDimitry Andric   // AAResultsWrapperPass by default provides basic-aa in the legacy PM
375e8d8bef9SDimitry Andric   // unless -disable-basic-aa is specified.
376e8d8bef9SDimitry Andric   // TODO: remove this once tests implicitly requiring basic-aa use -passes= and
377e8d8bef9SDimitry Andric   // -aa-pipeline=basic-aa.
378e8d8bef9SDimitry Andric   if (!Passes.empty() && !DisableBasicAA) {
379e8d8bef9SDimitry Andric     if (auto Err = PB.parseAAPipeline(AA, "basic-aa")) {
380e8d8bef9SDimitry Andric       errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
381e8d8bef9SDimitry Andric       return false;
382e8d8bef9SDimitry Andric     }
383e8d8bef9SDimitry Andric   }
384e8d8bef9SDimitry Andric 
3855ffd83dbSDimitry Andric   // For compatibility with legacy pass manager.
3865ffd83dbSDimitry Andric   // Alias analyses are not specially specified when using the legacy PM.
3875ffd83dbSDimitry Andric   for (auto PassName : Passes) {
3885ffd83dbSDimitry Andric     if (PB.isAAPassName(PassName)) {
3895ffd83dbSDimitry Andric       if (auto Err = PB.parseAAPipeline(AA, PassName)) {
3905ffd83dbSDimitry Andric         errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
3915ffd83dbSDimitry Andric         return false;
3925ffd83dbSDimitry Andric       }
3935ffd83dbSDimitry Andric     }
3945ffd83dbSDimitry Andric   }
3950b57cec5SDimitry Andric 
3960b57cec5SDimitry Andric   // Register the AA manager first so that our version is the one used.
3970b57cec5SDimitry Andric   FAM.registerPass([&] { return std::move(AA); });
398e8d8bef9SDimitry Andric   // Register our TargetLibraryInfoImpl.
399e8d8bef9SDimitry Andric   FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
4000b57cec5SDimitry Andric 
4010b57cec5SDimitry Andric   // Register all the basic analyses with the managers.
4020b57cec5SDimitry Andric   PB.registerModuleAnalyses(MAM);
4030b57cec5SDimitry Andric   PB.registerCGSCCAnalyses(CGAM);
4040b57cec5SDimitry Andric   PB.registerFunctionAnalyses(FAM);
4050b57cec5SDimitry Andric   PB.registerLoopAnalyses(LAM);
4060b57cec5SDimitry Andric   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
4070b57cec5SDimitry Andric 
408*fe6060f1SDimitry Andric   ModulePassManager MPM;
4090b57cec5SDimitry Andric   if (VK > VK_NoVerifier)
4100b57cec5SDimitry Andric     MPM.addPass(VerifierPass());
4110b57cec5SDimitry Andric   if (EnableDebugify)
4120b57cec5SDimitry Andric     MPM.addPass(NewPMDebugifyPass());
4130b57cec5SDimitry Andric 
4145ffd83dbSDimitry Andric   if (!PassPipeline.empty()) {
4155ffd83dbSDimitry Andric     assert(Passes.empty() &&
4165ffd83dbSDimitry Andric            "PassPipeline and Passes should not both contain passes");
417e8d8bef9SDimitry Andric     if (auto Err = PB.parsePassPipeline(MPM, PassPipeline)) {
4180b57cec5SDimitry Andric       errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
4190b57cec5SDimitry Andric       return false;
4200b57cec5SDimitry Andric     }
4215ffd83dbSDimitry Andric   }
422e8d8bef9SDimitry Andric   for (auto PassName : Passes) {
4235ffd83dbSDimitry Andric     std::string ModifiedPassName(PassName.begin(), PassName.end());
4245ffd83dbSDimitry Andric     if (PB.isAnalysisPassName(PassName))
4255ffd83dbSDimitry Andric       ModifiedPassName = "require<" + ModifiedPassName + ">";
426e8d8bef9SDimitry Andric     if (auto Err = PB.parsePassPipeline(MPM, ModifiedPassName)) {
4275ffd83dbSDimitry Andric       errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
4285ffd83dbSDimitry Andric       return false;
4295ffd83dbSDimitry Andric     }
4305ffd83dbSDimitry Andric   }
4310b57cec5SDimitry Andric 
4320b57cec5SDimitry Andric   if (VK > VK_NoVerifier)
4330b57cec5SDimitry Andric     MPM.addPass(VerifierPass());
4340b57cec5SDimitry Andric   if (EnableDebugify)
4350b57cec5SDimitry Andric     MPM.addPass(NewPMCheckDebugifyPass());
4360b57cec5SDimitry Andric 
4370b57cec5SDimitry Andric   // Add any relevant output pass at the end of the pipeline.
4380b57cec5SDimitry Andric   switch (OK) {
4390b57cec5SDimitry Andric   case OK_NoOutput:
4400b57cec5SDimitry Andric     break; // No output pass needed.
4410b57cec5SDimitry Andric   case OK_OutputAssembly:
4420b57cec5SDimitry Andric     MPM.addPass(
4430b57cec5SDimitry Andric         PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder));
4440b57cec5SDimitry Andric     break;
4450b57cec5SDimitry Andric   case OK_OutputBitcode:
4460b57cec5SDimitry Andric     MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder,
4470b57cec5SDimitry Andric                                   EmitSummaryIndex, EmitModuleHash));
4480b57cec5SDimitry Andric     break;
4490b57cec5SDimitry Andric   case OK_OutputThinLTOBitcode:
4500b57cec5SDimitry Andric     MPM.addPass(ThinLTOBitcodeWriterPass(
4510b57cec5SDimitry Andric         Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr));
4520b57cec5SDimitry Andric     break;
4530b57cec5SDimitry Andric   }
4540b57cec5SDimitry Andric 
4550b57cec5SDimitry Andric   // Before executing passes, print the final values of the LLVM options.
4560b57cec5SDimitry Andric   cl::PrintOptionValues();
4570b57cec5SDimitry Andric 
4580b57cec5SDimitry Andric   // Now that we have all of the passes ready, run them.
4590b57cec5SDimitry Andric   MPM.run(M, MAM);
4600b57cec5SDimitry Andric 
4610b57cec5SDimitry Andric   // Declare success.
4620b57cec5SDimitry Andric   if (OK != OK_NoOutput) {
4630b57cec5SDimitry Andric     Out->keep();
4640b57cec5SDimitry Andric     if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut)
4650b57cec5SDimitry Andric       ThinLTOLinkOut->keep();
4660b57cec5SDimitry Andric   }
4670b57cec5SDimitry Andric 
4680b57cec5SDimitry Andric   if (OptRemarkFile)
4690b57cec5SDimitry Andric     OptRemarkFile->keep();
4700b57cec5SDimitry Andric 
471e8d8bef9SDimitry Andric   if (DebugifyEach && !DebugifyExport.empty())
472e8d8bef9SDimitry Andric     exportDebugifyStats(DebugifyExport, Debugify.StatsMap);
473e8d8bef9SDimitry Andric 
4740b57cec5SDimitry Andric   return true;
4750b57cec5SDimitry Andric }
476*fe6060f1SDimitry Andric 
477*fe6060f1SDimitry Andric void llvm::printPasses(raw_ostream &OS) {
478*fe6060f1SDimitry Andric   PassBuilder PB;
479*fe6060f1SDimitry Andric   PB.printPassNames(OS);
480*fe6060f1SDimitry Andric }
481