xref: /freebsd/contrib/llvm-project/llvm/tools/opt/NewPMDriver.cpp (revision 480093f4440d54b30b3025afeac24b48f2ba7a2e)
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"
170b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
180b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
190b57cec5SDimitry Andric #include "llvm/Analysis/CGSCCPassManager.h"
200b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeWriterPass.h"
210b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
220b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
230b57cec5SDimitry Andric #include "llvm/IR/IRPrintingPasses.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"
280b57cec5SDimitry Andric #include "llvm/Passes/PassBuilder.h"
290b57cec5SDimitry Andric #include "llvm/Passes/PassPlugin.h"
300b57cec5SDimitry Andric #include "llvm/Passes/StandardInstrumentations.h"
310b57cec5SDimitry Andric #include "llvm/Support/CommandLine.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"
360b57cec5SDimitry Andric #include "llvm/Transforms/Scalar/LoopPassManager.h"
37*480093f4SDimitry Andric #include "llvm/Transforms/Utils/Debugify.h"
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric using namespace llvm;
400b57cec5SDimitry Andric using namespace opt_tool;
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric static cl::opt<bool>
430b57cec5SDimitry Andric     DebugPM("debug-pass-manager", cl::Hidden,
440b57cec5SDimitry Andric             cl::desc("Print pass management debugging information"));
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric static cl::list<std::string>
470b57cec5SDimitry Andric     PassPlugins("load-pass-plugin",
480b57cec5SDimitry Andric                 cl::desc("Load passes from plugin library"));
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric // This flag specifies a textual description of the alias analysis pipeline to
510b57cec5SDimitry Andric // use when querying for aliasing information. It only works in concert with
520b57cec5SDimitry Andric // the "passes" flag above.
530b57cec5SDimitry Andric static cl::opt<std::string>
540b57cec5SDimitry Andric     AAPipeline("aa-pipeline",
550b57cec5SDimitry Andric                cl::desc("A textual description of the alias analysis "
560b57cec5SDimitry Andric                         "pipeline for handling managed aliasing queries"),
570b57cec5SDimitry Andric                cl::Hidden);
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric /// {{@ These options accept textual pipeline descriptions which will be
600b57cec5SDimitry Andric /// inserted into default pipelines at the respective extension points
610b57cec5SDimitry Andric static cl::opt<std::string> PeepholeEPPipeline(
620b57cec5SDimitry Andric     "passes-ep-peephole",
630b57cec5SDimitry Andric     cl::desc("A textual description of the function pass pipeline inserted at "
640b57cec5SDimitry Andric              "the Peephole extension points into default pipelines"),
650b57cec5SDimitry Andric     cl::Hidden);
660b57cec5SDimitry Andric static cl::opt<std::string> LateLoopOptimizationsEPPipeline(
670b57cec5SDimitry Andric     "passes-ep-late-loop-optimizations",
680b57cec5SDimitry Andric     cl::desc(
690b57cec5SDimitry Andric         "A textual description of the loop pass pipeline inserted at "
700b57cec5SDimitry Andric         "the LateLoopOptimizations extension point into default pipelines"),
710b57cec5SDimitry Andric     cl::Hidden);
720b57cec5SDimitry Andric static cl::opt<std::string> LoopOptimizerEndEPPipeline(
730b57cec5SDimitry Andric     "passes-ep-loop-optimizer-end",
740b57cec5SDimitry Andric     cl::desc("A textual description of the loop pass pipeline inserted at "
750b57cec5SDimitry Andric              "the LoopOptimizerEnd extension point into default pipelines"),
760b57cec5SDimitry Andric     cl::Hidden);
770b57cec5SDimitry Andric static cl::opt<std::string> ScalarOptimizerLateEPPipeline(
780b57cec5SDimitry Andric     "passes-ep-scalar-optimizer-late",
790b57cec5SDimitry Andric     cl::desc("A textual description of the function pass pipeline inserted at "
800b57cec5SDimitry Andric              "the ScalarOptimizerLate extension point into default pipelines"),
810b57cec5SDimitry Andric     cl::Hidden);
820b57cec5SDimitry Andric static cl::opt<std::string> CGSCCOptimizerLateEPPipeline(
830b57cec5SDimitry Andric     "passes-ep-cgscc-optimizer-late",
840b57cec5SDimitry Andric     cl::desc("A textual description of the cgscc pass pipeline inserted at "
850b57cec5SDimitry Andric              "the CGSCCOptimizerLate extension point into default pipelines"),
860b57cec5SDimitry Andric     cl::Hidden);
870b57cec5SDimitry Andric static cl::opt<std::string> VectorizerStartEPPipeline(
880b57cec5SDimitry Andric     "passes-ep-vectorizer-start",
890b57cec5SDimitry Andric     cl::desc("A textual description of the function pass pipeline inserted at "
900b57cec5SDimitry Andric              "the VectorizerStart extension point into default pipelines"),
910b57cec5SDimitry Andric     cl::Hidden);
920b57cec5SDimitry Andric static cl::opt<std::string> PipelineStartEPPipeline(
930b57cec5SDimitry Andric     "passes-ep-pipeline-start",
940b57cec5SDimitry Andric     cl::desc("A textual description of the function pass pipeline inserted at "
950b57cec5SDimitry Andric              "the PipelineStart extension point into default pipelines"),
960b57cec5SDimitry Andric     cl::Hidden);
970b57cec5SDimitry Andric static cl::opt<std::string> OptimizerLastEPPipeline(
980b57cec5SDimitry Andric     "passes-ep-optimizer-last",
990b57cec5SDimitry Andric     cl::desc("A textual description of the function pass pipeline inserted at "
1000b57cec5SDimitry Andric              "the OptimizerLast extension point into default pipelines"),
1010b57cec5SDimitry Andric     cl::Hidden);
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric extern cl::opt<PGOKind> PGOKindFlag;
1040b57cec5SDimitry Andric extern cl::opt<std::string> ProfileFile;
1050b57cec5SDimitry Andric extern cl::opt<CSPGOKind> CSPGOKindFlag;
1060b57cec5SDimitry Andric extern cl::opt<std::string> CSProfileGenFile;
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric static cl::opt<std::string>
1090b57cec5SDimitry Andric     ProfileRemappingFile("profile-remapping-file",
1100b57cec5SDimitry Andric                          cl::desc("Path to the profile remapping file."),
1110b57cec5SDimitry Andric                          cl::Hidden);
1120b57cec5SDimitry Andric static cl::opt<bool> DebugInfoForProfiling(
1130b57cec5SDimitry Andric     "new-pm-debug-info-for-profiling", cl::init(false), cl::Hidden,
1140b57cec5SDimitry Andric     cl::desc("Emit special debug info to enable PGO profile generation."));
1150b57cec5SDimitry Andric /// @}}
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric template <typename PassManagerT>
1180b57cec5SDimitry Andric bool tryParsePipelineText(PassBuilder &PB,
1190b57cec5SDimitry Andric                           const cl::opt<std::string> &PipelineOpt) {
1200b57cec5SDimitry Andric   if (PipelineOpt.empty())
1210b57cec5SDimitry Andric     return false;
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric   // Verify the pipeline is parseable:
1240b57cec5SDimitry Andric   PassManagerT PM;
1250b57cec5SDimitry Andric   if (auto Err = PB.parsePassPipeline(PM, PipelineOpt)) {
1260b57cec5SDimitry Andric     errs() << "Could not parse -" << PipelineOpt.ArgStr
1270b57cec5SDimitry Andric            << " pipeline: " << toString(std::move(Err))
1280b57cec5SDimitry Andric            << "... I'm going to ignore it.\n";
1290b57cec5SDimitry Andric     return false;
1300b57cec5SDimitry Andric   }
1310b57cec5SDimitry Andric   return true;
1320b57cec5SDimitry Andric }
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric /// If one of the EPPipeline command line options was given, register callbacks
1350b57cec5SDimitry Andric /// for parsing and inserting the given pipeline
1360b57cec5SDimitry Andric static void registerEPCallbacks(PassBuilder &PB, bool VerifyEachPass,
1370b57cec5SDimitry Andric                                 bool DebugLogging) {
1380b57cec5SDimitry Andric   if (tryParsePipelineText<FunctionPassManager>(PB, PeepholeEPPipeline))
1390b57cec5SDimitry Andric     PB.registerPeepholeEPCallback(
1400b57cec5SDimitry Andric         [&PB, VerifyEachPass, DebugLogging](
1410b57cec5SDimitry Andric             FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
1420b57cec5SDimitry Andric           ExitOnError Err("Unable to parse PeepholeEP pipeline: ");
1430b57cec5SDimitry Andric           Err(PB.parsePassPipeline(PM, PeepholeEPPipeline, VerifyEachPass,
1440b57cec5SDimitry Andric                                    DebugLogging));
1450b57cec5SDimitry Andric         });
1460b57cec5SDimitry Andric   if (tryParsePipelineText<LoopPassManager>(PB,
1470b57cec5SDimitry Andric                                             LateLoopOptimizationsEPPipeline))
1480b57cec5SDimitry Andric     PB.registerLateLoopOptimizationsEPCallback(
1490b57cec5SDimitry Andric         [&PB, VerifyEachPass, DebugLogging](
1500b57cec5SDimitry Andric             LoopPassManager &PM, PassBuilder::OptimizationLevel Level) {
1510b57cec5SDimitry Andric           ExitOnError Err("Unable to parse LateLoopOptimizationsEP pipeline: ");
1520b57cec5SDimitry Andric           Err(PB.parsePassPipeline(PM, LateLoopOptimizationsEPPipeline,
1530b57cec5SDimitry Andric                                    VerifyEachPass, DebugLogging));
1540b57cec5SDimitry Andric         });
1550b57cec5SDimitry Andric   if (tryParsePipelineText<LoopPassManager>(PB, LoopOptimizerEndEPPipeline))
1560b57cec5SDimitry Andric     PB.registerLoopOptimizerEndEPCallback(
1570b57cec5SDimitry Andric         [&PB, VerifyEachPass, DebugLogging](
1580b57cec5SDimitry Andric             LoopPassManager &PM, PassBuilder::OptimizationLevel Level) {
1590b57cec5SDimitry Andric           ExitOnError Err("Unable to parse LoopOptimizerEndEP pipeline: ");
1600b57cec5SDimitry Andric           Err(PB.parsePassPipeline(PM, LoopOptimizerEndEPPipeline,
1610b57cec5SDimitry Andric                                    VerifyEachPass, DebugLogging));
1620b57cec5SDimitry Andric         });
1630b57cec5SDimitry Andric   if (tryParsePipelineText<FunctionPassManager>(PB,
1640b57cec5SDimitry Andric                                                 ScalarOptimizerLateEPPipeline))
1650b57cec5SDimitry Andric     PB.registerScalarOptimizerLateEPCallback(
1660b57cec5SDimitry Andric         [&PB, VerifyEachPass, DebugLogging](
1670b57cec5SDimitry Andric             FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
1680b57cec5SDimitry Andric           ExitOnError Err("Unable to parse ScalarOptimizerLateEP pipeline: ");
1690b57cec5SDimitry Andric           Err(PB.parsePassPipeline(PM, ScalarOptimizerLateEPPipeline,
1700b57cec5SDimitry Andric                                    VerifyEachPass, DebugLogging));
1710b57cec5SDimitry Andric         });
1720b57cec5SDimitry Andric   if (tryParsePipelineText<CGSCCPassManager>(PB, CGSCCOptimizerLateEPPipeline))
1730b57cec5SDimitry Andric     PB.registerCGSCCOptimizerLateEPCallback(
1740b57cec5SDimitry Andric         [&PB, VerifyEachPass, DebugLogging](
1750b57cec5SDimitry Andric             CGSCCPassManager &PM, PassBuilder::OptimizationLevel Level) {
1760b57cec5SDimitry Andric           ExitOnError Err("Unable to parse CGSCCOptimizerLateEP pipeline: ");
1770b57cec5SDimitry Andric           Err(PB.parsePassPipeline(PM, CGSCCOptimizerLateEPPipeline,
1780b57cec5SDimitry Andric                                    VerifyEachPass, DebugLogging));
1790b57cec5SDimitry Andric         });
1800b57cec5SDimitry Andric   if (tryParsePipelineText<FunctionPassManager>(PB, VectorizerStartEPPipeline))
1810b57cec5SDimitry Andric     PB.registerVectorizerStartEPCallback(
1820b57cec5SDimitry Andric         [&PB, VerifyEachPass, DebugLogging](
1830b57cec5SDimitry Andric             FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
1840b57cec5SDimitry Andric           ExitOnError Err("Unable to parse VectorizerStartEP pipeline: ");
1850b57cec5SDimitry Andric           Err(PB.parsePassPipeline(PM, VectorizerStartEPPipeline,
1860b57cec5SDimitry Andric                                    VerifyEachPass, DebugLogging));
1870b57cec5SDimitry Andric         });
1880b57cec5SDimitry Andric   if (tryParsePipelineText<ModulePassManager>(PB, PipelineStartEPPipeline))
1890b57cec5SDimitry Andric     PB.registerPipelineStartEPCallback(
1900b57cec5SDimitry Andric         [&PB, VerifyEachPass, DebugLogging](ModulePassManager &PM) {
1910b57cec5SDimitry Andric           ExitOnError Err("Unable to parse PipelineStartEP pipeline: ");
1920b57cec5SDimitry Andric           Err(PB.parsePassPipeline(PM, PipelineStartEPPipeline, VerifyEachPass,
1930b57cec5SDimitry Andric                                    DebugLogging));
1940b57cec5SDimitry Andric         });
1950b57cec5SDimitry Andric   if (tryParsePipelineText<FunctionPassManager>(PB, OptimizerLastEPPipeline))
1960b57cec5SDimitry Andric     PB.registerOptimizerLastEPCallback(
1970b57cec5SDimitry Andric         [&PB, VerifyEachPass, DebugLogging](FunctionPassManager &PM,
1980b57cec5SDimitry Andric                                             PassBuilder::OptimizationLevel) {
1990b57cec5SDimitry Andric           ExitOnError Err("Unable to parse OptimizerLastEP pipeline: ");
2000b57cec5SDimitry Andric           Err(PB.parsePassPipeline(PM, OptimizerLastEPPipeline, VerifyEachPass,
2010b57cec5SDimitry Andric                                    DebugLogging));
2020b57cec5SDimitry Andric         });
2030b57cec5SDimitry Andric }
2040b57cec5SDimitry Andric 
205*480093f4SDimitry Andric #define HANDLE_EXTENSION(Ext)                                                  \
206*480093f4SDimitry Andric   llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
207*480093f4SDimitry Andric #include "llvm/Support/Extension.def"
2080b57cec5SDimitry Andric 
2090b57cec5SDimitry Andric bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
2100b57cec5SDimitry Andric                            ToolOutputFile *Out, ToolOutputFile *ThinLTOLinkOut,
2110b57cec5SDimitry Andric                            ToolOutputFile *OptRemarkFile,
2120b57cec5SDimitry Andric                            StringRef PassPipeline, OutputKind OK,
2130b57cec5SDimitry Andric                            VerifierKind VK,
2140b57cec5SDimitry Andric                            bool ShouldPreserveAssemblyUseListOrder,
2150b57cec5SDimitry Andric                            bool ShouldPreserveBitcodeUseListOrder,
2160b57cec5SDimitry Andric                            bool EmitSummaryIndex, bool EmitModuleHash,
2170b57cec5SDimitry Andric                            bool EnableDebugify) {
2180b57cec5SDimitry Andric   bool VerifyEachPass = VK == VK_VerifyEachPass;
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric   Optional<PGOOptions> P;
2210b57cec5SDimitry Andric   switch (PGOKindFlag) {
2220b57cec5SDimitry Andric     case InstrGen:
2230b57cec5SDimitry Andric       P = PGOOptions(ProfileFile, "", "", PGOOptions::IRInstr);
2240b57cec5SDimitry Andric       break;
2250b57cec5SDimitry Andric     case InstrUse:
2260b57cec5SDimitry Andric       P = PGOOptions(ProfileFile, "", ProfileRemappingFile, PGOOptions::IRUse);
2270b57cec5SDimitry Andric       break;
2280b57cec5SDimitry Andric     case SampleUse:
2290b57cec5SDimitry Andric       P = PGOOptions(ProfileFile, "", ProfileRemappingFile,
2300b57cec5SDimitry Andric                      PGOOptions::SampleUse);
2310b57cec5SDimitry Andric       break;
2320b57cec5SDimitry Andric     case NoPGO:
2330b57cec5SDimitry Andric       if (DebugInfoForProfiling)
2340b57cec5SDimitry Andric         P = PGOOptions("", "", "", PGOOptions::NoAction, PGOOptions::NoCSAction,
2350b57cec5SDimitry Andric                        true);
2360b57cec5SDimitry Andric       else
2370b57cec5SDimitry Andric         P = None;
2380b57cec5SDimitry Andric     }
2390b57cec5SDimitry Andric     if (CSPGOKindFlag != NoCSPGO) {
2400b57cec5SDimitry Andric       if (P && (P->Action == PGOOptions::IRInstr ||
2410b57cec5SDimitry Andric                 P->Action == PGOOptions::SampleUse))
2420b57cec5SDimitry Andric         errs() << "CSPGOKind cannot be used with IRInstr or SampleUse";
2430b57cec5SDimitry Andric       if (CSPGOKindFlag == CSInstrGen) {
2440b57cec5SDimitry Andric         if (CSProfileGenFile.empty())
2450b57cec5SDimitry Andric           errs() << "CSInstrGen needs to specify CSProfileGenFile";
2460b57cec5SDimitry Andric         if (P) {
2470b57cec5SDimitry Andric           P->CSAction = PGOOptions::CSIRInstr;
2480b57cec5SDimitry Andric           P->CSProfileGenFile = CSProfileGenFile;
2490b57cec5SDimitry Andric         } else
2500b57cec5SDimitry Andric           P = PGOOptions("", CSProfileGenFile, ProfileRemappingFile,
2510b57cec5SDimitry Andric                          PGOOptions::NoAction, PGOOptions::CSIRInstr);
2520b57cec5SDimitry Andric       } else /* CSPGOKindFlag == CSInstrUse */ {
2530b57cec5SDimitry Andric         if (!P)
2540b57cec5SDimitry Andric           errs() << "CSInstrUse needs to be together with InstrUse";
2550b57cec5SDimitry Andric         P->CSAction = PGOOptions::CSIRUse;
2560b57cec5SDimitry Andric       }
2570b57cec5SDimitry Andric     }
2580b57cec5SDimitry Andric   PassInstrumentationCallbacks PIC;
2590b57cec5SDimitry Andric   StandardInstrumentations SI;
2600b57cec5SDimitry Andric   SI.registerCallbacks(PIC);
2610b57cec5SDimitry Andric 
2620b57cec5SDimitry Andric   PassBuilder PB(TM, PipelineTuningOptions(), P, &PIC);
2630b57cec5SDimitry Andric   registerEPCallbacks(PB, VerifyEachPass, DebugPM);
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric   // Load requested pass plugins and let them register pass builder callbacks
2660b57cec5SDimitry Andric   for (auto &PluginFN : PassPlugins) {
2670b57cec5SDimitry Andric     auto PassPlugin = PassPlugin::Load(PluginFN);
2680b57cec5SDimitry Andric     if (!PassPlugin) {
2690b57cec5SDimitry Andric       errs() << "Failed to load passes from '" << PluginFN
2700b57cec5SDimitry Andric              << "'. Request ignored.\n";
2710b57cec5SDimitry Andric       continue;
2720b57cec5SDimitry Andric     }
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric     PassPlugin->registerPassBuilderCallbacks(PB);
2750b57cec5SDimitry Andric   }
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric   // Register a callback that creates the debugify passes as needed.
2780b57cec5SDimitry Andric   PB.registerPipelineParsingCallback(
2790b57cec5SDimitry Andric       [](StringRef Name, ModulePassManager &MPM,
2800b57cec5SDimitry Andric          ArrayRef<PassBuilder::PipelineElement>) {
2810b57cec5SDimitry Andric         if (Name == "debugify") {
2820b57cec5SDimitry Andric           MPM.addPass(NewPMDebugifyPass());
2830b57cec5SDimitry Andric           return true;
2840b57cec5SDimitry Andric         } else if (Name == "check-debugify") {
2850b57cec5SDimitry Andric           MPM.addPass(NewPMCheckDebugifyPass());
2860b57cec5SDimitry Andric           return true;
2870b57cec5SDimitry Andric         }
2880b57cec5SDimitry Andric         return false;
2890b57cec5SDimitry Andric       });
2900b57cec5SDimitry Andric 
291*480093f4SDimitry Andric #define HANDLE_EXTENSION(Ext)                                                  \
292*480093f4SDimitry Andric   get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
293*480093f4SDimitry Andric #include "llvm/Support/Extension.def"
2940b57cec5SDimitry Andric 
2950b57cec5SDimitry Andric   // Specially handle the alias analysis manager so that we can register
2960b57cec5SDimitry Andric   // a custom pipeline of AA passes with it.
2970b57cec5SDimitry Andric   AAManager AA;
2980b57cec5SDimitry Andric   if (auto Err = PB.parseAAPipeline(AA, AAPipeline)) {
2990b57cec5SDimitry Andric     errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
3000b57cec5SDimitry Andric     return false;
3010b57cec5SDimitry Andric   }
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric   LoopAnalysisManager LAM(DebugPM);
3040b57cec5SDimitry Andric   FunctionAnalysisManager FAM(DebugPM);
3050b57cec5SDimitry Andric   CGSCCAnalysisManager CGAM(DebugPM);
3060b57cec5SDimitry Andric   ModuleAnalysisManager MAM(DebugPM);
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric   // Register the AA manager first so that our version is the one used.
3090b57cec5SDimitry Andric   FAM.registerPass([&] { return std::move(AA); });
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric   // Register all the basic analyses with the managers.
3120b57cec5SDimitry Andric   PB.registerModuleAnalyses(MAM);
3130b57cec5SDimitry Andric   PB.registerCGSCCAnalyses(CGAM);
3140b57cec5SDimitry Andric   PB.registerFunctionAnalyses(FAM);
3150b57cec5SDimitry Andric   PB.registerLoopAnalyses(LAM);
3160b57cec5SDimitry Andric   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric   ModulePassManager MPM(DebugPM);
3190b57cec5SDimitry Andric   if (VK > VK_NoVerifier)
3200b57cec5SDimitry Andric     MPM.addPass(VerifierPass());
3210b57cec5SDimitry Andric   if (EnableDebugify)
3220b57cec5SDimitry Andric     MPM.addPass(NewPMDebugifyPass());
3230b57cec5SDimitry Andric 
3240b57cec5SDimitry Andric   if (auto Err =
3250b57cec5SDimitry Andric           PB.parsePassPipeline(MPM, PassPipeline, VerifyEachPass, DebugPM)) {
3260b57cec5SDimitry Andric     errs() << Arg0 << ": " << toString(std::move(Err)) << "\n";
3270b57cec5SDimitry Andric     return false;
3280b57cec5SDimitry Andric   }
3290b57cec5SDimitry Andric 
3300b57cec5SDimitry Andric   if (VK > VK_NoVerifier)
3310b57cec5SDimitry Andric     MPM.addPass(VerifierPass());
3320b57cec5SDimitry Andric   if (EnableDebugify)
3330b57cec5SDimitry Andric     MPM.addPass(NewPMCheckDebugifyPass());
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric   // Add any relevant output pass at the end of the pipeline.
3360b57cec5SDimitry Andric   switch (OK) {
3370b57cec5SDimitry Andric   case OK_NoOutput:
3380b57cec5SDimitry Andric     break; // No output pass needed.
3390b57cec5SDimitry Andric   case OK_OutputAssembly:
3400b57cec5SDimitry Andric     MPM.addPass(
3410b57cec5SDimitry Andric         PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder));
3420b57cec5SDimitry Andric     break;
3430b57cec5SDimitry Andric   case OK_OutputBitcode:
3440b57cec5SDimitry Andric     MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder,
3450b57cec5SDimitry Andric                                   EmitSummaryIndex, EmitModuleHash));
3460b57cec5SDimitry Andric     break;
3470b57cec5SDimitry Andric   case OK_OutputThinLTOBitcode:
3480b57cec5SDimitry Andric     MPM.addPass(ThinLTOBitcodeWriterPass(
3490b57cec5SDimitry Andric         Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr));
3500b57cec5SDimitry Andric     break;
3510b57cec5SDimitry Andric   }
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric   // Before executing passes, print the final values of the LLVM options.
3540b57cec5SDimitry Andric   cl::PrintOptionValues();
3550b57cec5SDimitry Andric 
3560b57cec5SDimitry Andric   // Now that we have all of the passes ready, run them.
3570b57cec5SDimitry Andric   MPM.run(M, MAM);
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric   // Declare success.
3600b57cec5SDimitry Andric   if (OK != OK_NoOutput) {
3610b57cec5SDimitry Andric     Out->keep();
3620b57cec5SDimitry Andric     if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut)
3630b57cec5SDimitry Andric       ThinLTOLinkOut->keep();
3640b57cec5SDimitry Andric   }
3650b57cec5SDimitry Andric 
3660b57cec5SDimitry Andric   if (OptRemarkFile)
3670b57cec5SDimitry Andric     OptRemarkFile->keep();
3680b57cec5SDimitry Andric 
3690b57cec5SDimitry Andric   return true;
3700b57cec5SDimitry Andric }
371