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/Analysis/LoopPassManager.h"
21 #include "llvm/Bitcode/BitcodeWriterPass.h"
22 #include "llvm/IR/Dominators.h"
23 #include "llvm/IR/IRPrintingPasses.h"
24 #include "llvm/IR/LLVMContext.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/IR/PassManager.h"
27 #include "llvm/IR/Verifier.h"
28 #include "llvm/Passes/PassBuilder.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/ToolOutputFile.h"
32 #include "llvm/Target/TargetMachine.h"
33 
34 using namespace llvm;
35 using namespace opt_tool;
36 
37 static cl::opt<bool>
38     DebugPM("debug-pass-manager", cl::Hidden,
39             cl::desc("Print pass management debugging information"));
40 
41 // This flag specifies a textual description of the alias analysis pipeline to
42 // use when querying for aliasing information. It only works in concert with
43 // the "passes" flag above.
44 static cl::opt<std::string>
45     AAPipeline("aa-pipeline",
46                cl::desc("A textual description of the alias analysis "
47                         "pipeline for handling managed aliasing queries"),
48                cl::Hidden);
49 
50 bool llvm::runPassPipeline(StringRef Arg0, LLVMContext &Context, Module &M,
51                            TargetMachine *TM, tool_output_file *Out,
52                            StringRef PassPipeline, OutputKind OK,
53                            VerifierKind VK,
54                            bool ShouldPreserveAssemblyUseListOrder,
55                            bool ShouldPreserveBitcodeUseListOrder) {
56   PassBuilder PB(TM);
57 
58   // Specially handle the alias analysis manager so that we can register
59   // a custom pipeline of AA passes with it.
60   AAManager AA;
61   if (!PB.parseAAPipeline(AA, AAPipeline)) {
62     errs() << Arg0 << ": unable to parse AA pipeline description.\n";
63     return false;
64   }
65 
66   LoopAnalysisManager LAM(DebugPM);
67   FunctionAnalysisManager FAM(DebugPM);
68   CGSCCAnalysisManager CGAM(DebugPM);
69   ModuleAnalysisManager MAM(DebugPM);
70 
71   // Register the AA manager first so that our version is the one used.
72   FAM.registerPass([&] { return std::move(AA); });
73 
74   // Register all the basic analyses with the managers.
75   PB.registerModuleAnalyses(MAM);
76   PB.registerCGSCCAnalyses(CGAM);
77   PB.registerFunctionAnalyses(FAM);
78   PB.registerLoopAnalyses(LAM);
79 
80   // Cross register the analysis managers through their proxies.
81   MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
82   MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
83   CGAM.registerPass([&] { return FunctionAnalysisManagerCGSCCProxy(FAM); });
84   CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
85   FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
86   FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
87   FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
88   LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
89 
90   ModulePassManager MPM(DebugPM);
91   if (VK > VK_NoVerifier)
92     MPM.addPass(VerifierPass());
93 
94   if (!PB.parsePassPipeline(MPM, PassPipeline, VK == VK_VerifyEachPass,
95                             DebugPM)) {
96     errs() << Arg0 << ": unable to parse pass pipeline description.\n";
97     return false;
98   }
99 
100   if (VK > VK_NoVerifier)
101     MPM.addPass(VerifierPass());
102 
103   // Add any relevant output pass at the end of the pipeline.
104   switch (OK) {
105   case OK_NoOutput:
106     break; // No output pass needed.
107   case OK_OutputAssembly:
108     MPM.addPass(
109         PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder));
110     break;
111   case OK_OutputBitcode:
112     MPM.addPass(
113         BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder));
114     break;
115   }
116 
117   // Before executing passes, print the final values of the LLVM options.
118   cl::PrintOptionValues();
119 
120   // Now that we have all of the passes ready, run them.
121   MPM.run(M, MAM);
122 
123   // Declare success.
124   if (OK != OK_NoOutput)
125     Out->keep();
126   return true;
127 }
128