]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/opt/NewPMDriver.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r303197, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / opt / NewPMDriver.cpp
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/Scalar/LoopPassManager.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, Module &M,
51                            TargetMachine *TM, tool_output_file *Out,
52                            StringRef PassPipeline, OutputKind OK,
53                            VerifierKind VK,
54                            bool ShouldPreserveAssemblyUseListOrder,
55                            bool ShouldPreserveBitcodeUseListOrder,
56                            bool EmitSummaryIndex, bool EmitModuleHash) {
57   PassBuilder PB(TM);
58
59   // Specially handle the alias analysis manager so that we can register
60   // a custom pipeline of AA passes with it.
61   AAManager AA;
62   if (!PB.parseAAPipeline(AA, AAPipeline)) {
63     errs() << Arg0 << ": unable to parse AA pipeline description.\n";
64     return false;
65   }
66
67   LoopAnalysisManager LAM(DebugPM);
68   FunctionAnalysisManager FAM(DebugPM);
69   CGSCCAnalysisManager CGAM(DebugPM);
70   ModuleAnalysisManager MAM(DebugPM);
71
72   // Register the AA manager first so that our version is the one used.
73   FAM.registerPass([&] { return std::move(AA); });
74
75   // Register all the basic analyses with the managers.
76   PB.registerModuleAnalyses(MAM);
77   PB.registerCGSCCAnalyses(CGAM);
78   PB.registerFunctionAnalyses(FAM);
79   PB.registerLoopAnalyses(LAM);
80   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
81
82   ModulePassManager MPM(DebugPM);
83   if (VK > VK_NoVerifier)
84     MPM.addPass(VerifierPass());
85
86   if (!PB.parsePassPipeline(MPM, PassPipeline, VK == VK_VerifyEachPass,
87                             DebugPM)) {
88     errs() << Arg0 << ": unable to parse pass pipeline description.\n";
89     return false;
90   }
91
92   if (VK > VK_NoVerifier)
93     MPM.addPass(VerifierPass());
94
95   // Add any relevant output pass at the end of the pipeline.
96   switch (OK) {
97   case OK_NoOutput:
98     break; // No output pass needed.
99   case OK_OutputAssembly:
100     MPM.addPass(
101         PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder));
102     break;
103   case OK_OutputBitcode:
104     MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder,
105                                   EmitSummaryIndex, EmitModuleHash));
106     break;
107   }
108
109   // Before executing passes, print the final values of the LLVM options.
110   cl::PrintOptionValues();
111
112   // Now that we have all of the passes ready, run them.
113   MPM.run(M, MAM);
114
115   // Declare success.
116   if (OK != OK_NoOutput)
117     Out->keep();
118   return true;
119 }