xref: /aosp_15_r20/external/llvm/tools/opt/NewPMDriver.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- NewPMDriver.cpp - Driver for opt with new PM -----------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker /// \file
10*9880d681SAndroid Build Coastguard Worker ///
11*9880d681SAndroid Build Coastguard Worker /// This file is just a split of the code that logically belongs in opt.cpp but
12*9880d681SAndroid Build Coastguard Worker /// that includes the new pass manager headers.
13*9880d681SAndroid Build Coastguard Worker ///
14*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
15*9880d681SAndroid Build Coastguard Worker 
16*9880d681SAndroid Build Coastguard Worker #include "NewPMDriver.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringRef.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AliasAnalysis.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/CGSCCPassManager.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/LoopPassManager.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Bitcode/BitcodeWriterPass.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Dominators.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IRPrintingPasses.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LLVMContext.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/PassManager.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Verifier.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/Passes/PassBuilder.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ToolOutputFile.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetMachine.h"
33*9880d681SAndroid Build Coastguard Worker 
34*9880d681SAndroid Build Coastguard Worker using namespace llvm;
35*9880d681SAndroid Build Coastguard Worker using namespace opt_tool;
36*9880d681SAndroid Build Coastguard Worker 
37*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
38*9880d681SAndroid Build Coastguard Worker     DebugPM("debug-pass-manager", cl::Hidden,
39*9880d681SAndroid Build Coastguard Worker             cl::desc("Print pass management debugging information"));
40*9880d681SAndroid Build Coastguard Worker 
41*9880d681SAndroid Build Coastguard Worker // This flag specifies a textual description of the alias analysis pipeline to
42*9880d681SAndroid Build Coastguard Worker // use when querying for aliasing information. It only works in concert with
43*9880d681SAndroid Build Coastguard Worker // the "passes" flag above.
44*9880d681SAndroid Build Coastguard Worker static cl::opt<std::string>
45*9880d681SAndroid Build Coastguard Worker     AAPipeline("aa-pipeline",
46*9880d681SAndroid Build Coastguard Worker                cl::desc("A textual description of the alias analysis "
47*9880d681SAndroid Build Coastguard Worker                         "pipeline for handling managed aliasing queries"),
48*9880d681SAndroid Build Coastguard Worker                cl::Hidden);
49*9880d681SAndroid Build Coastguard Worker 
runPassPipeline(StringRef Arg0,LLVMContext & Context,Module & M,TargetMachine * TM,tool_output_file * Out,StringRef PassPipeline,OutputKind OK,VerifierKind VK,bool ShouldPreserveAssemblyUseListOrder,bool ShouldPreserveBitcodeUseListOrder)50*9880d681SAndroid Build Coastguard Worker bool llvm::runPassPipeline(StringRef Arg0, LLVMContext &Context, Module &M,
51*9880d681SAndroid Build Coastguard Worker                            TargetMachine *TM, tool_output_file *Out,
52*9880d681SAndroid Build Coastguard Worker                            StringRef PassPipeline, OutputKind OK,
53*9880d681SAndroid Build Coastguard Worker                            VerifierKind VK,
54*9880d681SAndroid Build Coastguard Worker                            bool ShouldPreserveAssemblyUseListOrder,
55*9880d681SAndroid Build Coastguard Worker                            bool ShouldPreserveBitcodeUseListOrder) {
56*9880d681SAndroid Build Coastguard Worker   PassBuilder PB(TM);
57*9880d681SAndroid Build Coastguard Worker 
58*9880d681SAndroid Build Coastguard Worker   // Specially handle the alias analysis manager so that we can register
59*9880d681SAndroid Build Coastguard Worker   // a custom pipeline of AA passes with it.
60*9880d681SAndroid Build Coastguard Worker   AAManager AA;
61*9880d681SAndroid Build Coastguard Worker   if (!PB.parseAAPipeline(AA, AAPipeline)) {
62*9880d681SAndroid Build Coastguard Worker     errs() << Arg0 << ": unable to parse AA pipeline description.\n";
63*9880d681SAndroid Build Coastguard Worker     return false;
64*9880d681SAndroid Build Coastguard Worker   }
65*9880d681SAndroid Build Coastguard Worker 
66*9880d681SAndroid Build Coastguard Worker   LoopAnalysisManager LAM(DebugPM);
67*9880d681SAndroid Build Coastguard Worker   FunctionAnalysisManager FAM(DebugPM);
68*9880d681SAndroid Build Coastguard Worker   CGSCCAnalysisManager CGAM(DebugPM);
69*9880d681SAndroid Build Coastguard Worker   ModuleAnalysisManager MAM(DebugPM);
70*9880d681SAndroid Build Coastguard Worker 
71*9880d681SAndroid Build Coastguard Worker   // Register the AA manager first so that our version is the one used.
72*9880d681SAndroid Build Coastguard Worker   FAM.registerPass([&] { return std::move(AA); });
73*9880d681SAndroid Build Coastguard Worker 
74*9880d681SAndroid Build Coastguard Worker   // Register all the basic analyses with the managers.
75*9880d681SAndroid Build Coastguard Worker   PB.registerModuleAnalyses(MAM);
76*9880d681SAndroid Build Coastguard Worker   PB.registerCGSCCAnalyses(CGAM);
77*9880d681SAndroid Build Coastguard Worker   PB.registerFunctionAnalyses(FAM);
78*9880d681SAndroid Build Coastguard Worker   PB.registerLoopAnalyses(LAM);
79*9880d681SAndroid Build Coastguard Worker   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
80*9880d681SAndroid Build Coastguard Worker 
81*9880d681SAndroid Build Coastguard Worker   ModulePassManager MPM(DebugPM);
82*9880d681SAndroid Build Coastguard Worker   if (VK > VK_NoVerifier)
83*9880d681SAndroid Build Coastguard Worker     MPM.addPass(VerifierPass());
84*9880d681SAndroid Build Coastguard Worker 
85*9880d681SAndroid Build Coastguard Worker   if (!PB.parsePassPipeline(MPM, PassPipeline, VK == VK_VerifyEachPass,
86*9880d681SAndroid Build Coastguard Worker                             DebugPM)) {
87*9880d681SAndroid Build Coastguard Worker     errs() << Arg0 << ": unable to parse pass pipeline description.\n";
88*9880d681SAndroid Build Coastguard Worker     return false;
89*9880d681SAndroid Build Coastguard Worker   }
90*9880d681SAndroid Build Coastguard Worker 
91*9880d681SAndroid Build Coastguard Worker   if (VK > VK_NoVerifier)
92*9880d681SAndroid Build Coastguard Worker     MPM.addPass(VerifierPass());
93*9880d681SAndroid Build Coastguard Worker 
94*9880d681SAndroid Build Coastguard Worker   // Add any relevant output pass at the end of the pipeline.
95*9880d681SAndroid Build Coastguard Worker   switch (OK) {
96*9880d681SAndroid Build Coastguard Worker   case OK_NoOutput:
97*9880d681SAndroid Build Coastguard Worker     break; // No output pass needed.
98*9880d681SAndroid Build Coastguard Worker   case OK_OutputAssembly:
99*9880d681SAndroid Build Coastguard Worker     MPM.addPass(
100*9880d681SAndroid Build Coastguard Worker         PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder));
101*9880d681SAndroid Build Coastguard Worker     break;
102*9880d681SAndroid Build Coastguard Worker   case OK_OutputBitcode:
103*9880d681SAndroid Build Coastguard Worker     MPM.addPass(
104*9880d681SAndroid Build Coastguard Worker         BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder));
105*9880d681SAndroid Build Coastguard Worker     break;
106*9880d681SAndroid Build Coastguard Worker   }
107*9880d681SAndroid Build Coastguard Worker 
108*9880d681SAndroid Build Coastguard Worker   // Before executing passes, print the final values of the LLVM options.
109*9880d681SAndroid Build Coastguard Worker   cl::PrintOptionValues();
110*9880d681SAndroid Build Coastguard Worker 
111*9880d681SAndroid Build Coastguard Worker   // Now that we have all of the passes ready, run them.
112*9880d681SAndroid Build Coastguard Worker   MPM.run(M, MAM);
113*9880d681SAndroid Build Coastguard Worker 
114*9880d681SAndroid Build Coastguard Worker   // Declare success.
115*9880d681SAndroid Build Coastguard Worker   if (OK != OK_NoOutput)
116*9880d681SAndroid Build Coastguard Worker     Out->keep();
117*9880d681SAndroid Build Coastguard Worker   return true;
118*9880d681SAndroid Build Coastguard Worker }
119