1 //===- NewPMDriver.cpp - Driver for opt with new PM -----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 /// \file
10 ///
11 /// This file is just a split of the code that logically belongs in opt.cpp but
12 /// that includes the new pass manager headers.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "NewPMDriver.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Analysis/AliasAnalysis.h"
19 #include "llvm/Analysis/CGSCCPassManager.h"
20 #include "llvm/Bitcode/BitcodeWriterPass.h"
21 #include "llvm/IR/Dominators.h"
22 #include "llvm/IR/IRPrintingPasses.h"
23 #include "llvm/IR/LLVMContext.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/IR/PassManager.h"
26 #include "llvm/IR/Verifier.h"
27 #include "llvm/Passes/PassBuilder.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/ToolOutputFile.h"
31 #include "llvm/Target/TargetMachine.h"
32 #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
33 #include "llvm/Transforms/Scalar/LoopPassManager.h"
34 
35 using namespace llvm;
36 using namespace opt_tool;
37 
38 static cl::opt<bool>
39     DebugPM("debug-pass-manager", cl::Hidden,
40             cl::desc("Print pass management debugging information"));
41 
42 // This flag specifies a textual description of the alias analysis pipeline to
43 // use when querying for aliasing information. It only works in concert with
44 // the "passes" flag above.
45 static cl::opt<std::string>
46     AAPipeline("aa-pipeline",
47                cl::desc("A textual description of the alias analysis "
48                         "pipeline for handling managed aliasing queries"),
49                cl::Hidden);
50 
51 /// {{@ These options accept textual pipeline descriptions which will be
52 /// inserted into default pipelines at the respective extension points
53 static cl::opt<std::string> PeepholeEPPipeline(
54     "passes-ep-peephole",
55     cl::desc("A textual description of the function pass pipeline inserted at "
56              "the Peephole extension points into default pipelines"),
57     cl::Hidden);
58 static cl::opt<std::string> LateLoopOptimizationsEPPipeline(
59     "passes-ep-late-loop-optimizations",
60     cl::desc(
61         "A textual description of the loop pass pipeline inserted at "
62         "the LateLoopOptimizations extension point into default pipelines"),
63     cl::Hidden);
64 static cl::opt<std::string> LoopOptimizerEndEPPipeline(
65     "passes-ep-loop-optimizer-end",
66     cl::desc("A textual description of the loop pass pipeline inserted at "
67              "the LoopOptimizerEnd extension point into default pipelines"),
68     cl::Hidden);
69 static cl::opt<std::string> ScalarOptimizerLateEPPipeline(
70     "passes-ep-scalar-optimizer-late",
71     cl::desc("A textual description of the function pass pipeline inserted at "
72              "the ScalarOptimizerLate extension point into default pipelines"),
73     cl::Hidden);
74 static cl::opt<std::string> CGSCCOptimizerLateEPPipeline(
75     "passes-ep-cgscc-optimizer-late",
76     cl::desc("A textual description of the cgscc pass pipeline inserted at "
77              "the CGSCCOptimizerLate extension point into default pipelines"),
78     cl::Hidden);
79 static cl::opt<std::string> VectorizerStartEPPipeline(
80     "passes-ep-vectorizer-start",
81     cl::desc("A textual description of the function pass pipeline inserted at "
82              "the VectorizerStart extension point into default pipelines"),
83     cl::Hidden);
84 /// @}}
85 
86 /// If one of the EPPipeline command line options was given, register callbacks
87 /// for parsing and inserting the given pipeline
88 static void registerEPCallbacks(PassBuilder &PB, bool VerifyEachPass,
89                                 bool DebugLogging) {
90   if (!PeepholeEPPipeline.empty())
91     PB.registerPeepholeEPCallback([&PB, VerifyEachPass, DebugLogging](
92         FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
93       return PB.parsePassPipeline(PM, PeepholeEPPipeline, VerifyEachPass,
94                                   DebugLogging);
95     });
96   if (!LateLoopOptimizationsEPPipeline.empty())
97     PB.registerLateLoopOptimizationsEPCallback(
98         [&PB, VerifyEachPass, DebugLogging](
99             LoopPassManager &PM, PassBuilder::OptimizationLevel Level) {
100           return PB.parsePassPipeline(PM, LateLoopOptimizationsEPPipeline,
101                                       VerifyEachPass, DebugLogging);
102         });
103   if (!LoopOptimizerEndEPPipeline.empty())
104     PB.registerLoopOptimizerEndEPCallback([&PB, VerifyEachPass, DebugLogging](
105         LoopPassManager &PM, PassBuilder::OptimizationLevel Level) {
106       return PB.parsePassPipeline(PM, LoopOptimizerEndEPPipeline,
107                                   VerifyEachPass, DebugLogging);
108     });
109   if (!ScalarOptimizerLateEPPipeline.empty())
110     PB.registerScalarOptimizerLateEPCallback(
111         [&PB, VerifyEachPass, DebugLogging](
112             FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
113           return PB.parsePassPipeline(PM, ScalarOptimizerLateEPPipeline,
114                                       VerifyEachPass, DebugLogging);
115         });
116   if (!CGSCCOptimizerLateEPPipeline.empty())
117     PB.registerCGSCCOptimizerLateEPCallback([&PB, VerifyEachPass, DebugLogging](
118         CGSCCPassManager &PM, PassBuilder::OptimizationLevel Level) {
119       return PB.parsePassPipeline(PM, CGSCCOptimizerLateEPPipeline,
120                                   VerifyEachPass, DebugLogging);
121     });
122   if (!VectorizerStartEPPipeline.empty())
123     PB.registerVectorizerStartEPCallback([&PB, VerifyEachPass, DebugLogging](
124         FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
125       return PB.parsePassPipeline(PM, VectorizerStartEPPipeline, VerifyEachPass,
126                                   DebugLogging);
127     });
128 }
129 
130 bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
131                            tool_output_file *Out,
132                            tool_output_file *ThinLTOLinkOut,
133                            StringRef PassPipeline, OutputKind OK,
134                            VerifierKind VK,
135                            bool ShouldPreserveAssemblyUseListOrder,
136                            bool ShouldPreserveBitcodeUseListOrder,
137                            bool EmitSummaryIndex, bool EmitModuleHash) {
138   bool VerifyEachPass = VK == VK_VerifyEachPass;
139   PassBuilder PB(TM);
140   registerEPCallbacks(PB, VerifyEachPass, DebugPM);
141 
142   // Specially handle the alias analysis manager so that we can register
143   // a custom pipeline of AA passes with it.
144   AAManager AA;
145   if (!PB.parseAAPipeline(AA, AAPipeline)) {
146     errs() << Arg0 << ": unable to parse AA pipeline description.\n";
147     return false;
148   }
149 
150   LoopAnalysisManager LAM(DebugPM);
151   FunctionAnalysisManager FAM(DebugPM);
152   CGSCCAnalysisManager CGAM(DebugPM);
153   ModuleAnalysisManager MAM(DebugPM);
154 
155   // Register the AA manager first so that our version is the one used.
156   FAM.registerPass([&] { return std::move(AA); });
157 
158   // Register all the basic analyses with the managers.
159   PB.registerModuleAnalyses(MAM);
160   PB.registerCGSCCAnalyses(CGAM);
161   PB.registerFunctionAnalyses(FAM);
162   PB.registerLoopAnalyses(LAM);
163   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
164 
165   ModulePassManager MPM(DebugPM);
166   if (VK > VK_NoVerifier)
167     MPM.addPass(VerifierPass());
168 
169   if (!PB.parsePassPipeline(MPM, PassPipeline, VerifyEachPass, DebugPM)) {
170     errs() << Arg0 << ": unable to parse pass pipeline description.\n";
171     return false;
172   }
173 
174   if (VK > VK_NoVerifier)
175     MPM.addPass(VerifierPass());
176 
177   // Add any relevant output pass at the end of the pipeline.
178   switch (OK) {
179   case OK_NoOutput:
180     break; // No output pass needed.
181   case OK_OutputAssembly:
182     MPM.addPass(
183         PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder));
184     break;
185   case OK_OutputBitcode:
186     MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder,
187                                   EmitSummaryIndex, EmitModuleHash));
188     break;
189   case OK_OutputThinLTOBitcode:
190     MPM.addPass(ThinLTOBitcodeWriterPass(
191         Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr));
192     break;
193   }
194 
195   // Before executing passes, print the final values of the LLVM options.
196   cl::PrintOptionValues();
197 
198   // Now that we have all of the passes ready, run them.
199   MPM.run(M, MAM);
200 
201   // Declare success.
202   if (OK != OK_NoOutput) {
203     Out->keep();
204     if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut)
205       ThinLTOLinkOut->keep();
206   }
207   return true;
208 }
209