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