[PM] Refactor the analysis registration and pass pipeline parsing to
[oota-llvm.git] / 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 "Passes.h"
18 #include "llvm/ADT/StringRef.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/Support/CommandLine.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/ToolOutputFile.h"
30
31 using namespace llvm;
32 using namespace opt_tool;
33
34 static cl::opt<bool>
35     DebugPM("debug-pass-manager", cl::Hidden,
36             cl::desc("Print pass management debugging information"));
37
38 bool llvm::runPassPipeline(StringRef Arg0, LLVMContext &Context, Module &M,
39                            tool_output_file *Out, StringRef PassPipeline,
40                            OutputKind OK, VerifierKind VK) {
41   Passes P;
42
43   FunctionAnalysisManager FAM(DebugPM);
44   CGSCCAnalysisManager CGAM(DebugPM);
45   ModuleAnalysisManager MAM(DebugPM);
46
47   // Register all the basic analyses with the managers.
48   P.registerModuleAnalyses(MAM);
49   P.registerCGSCCAnalyses(CGAM);
50   P.registerFunctionAnalyses(FAM);
51
52   // Cross register the analysis managers through their proxies.
53   MAM.registerPass(FunctionAnalysisManagerModuleProxy(FAM));
54   MAM.registerPass(CGSCCAnalysisManagerModuleProxy(CGAM));
55   CGAM.registerPass(FunctionAnalysisManagerCGSCCProxy(FAM));
56   CGAM.registerPass(ModuleAnalysisManagerCGSCCProxy(MAM));
57   FAM.registerPass(CGSCCAnalysisManagerFunctionProxy(CGAM));
58   FAM.registerPass(ModuleAnalysisManagerFunctionProxy(MAM));
59
60   ModulePassManager MPM(DebugPM);
61   if (VK > VK_NoVerifier)
62     MPM.addPass(VerifierPass());
63
64   if (!P.parsePassPipeline(MPM, PassPipeline, VK == VK_VerifyEachPass,
65                            DebugPM)) {
66     errs() << Arg0 << ": unable to parse pass pipeline description.\n";
67     return false;
68   }
69
70   if (VK > VK_NoVerifier)
71     MPM.addPass(VerifierPass());
72
73   // Add any relevant output pass at the end of the pipeline.
74   switch (OK) {
75   case OK_NoOutput:
76     break; // No output pass needed.
77   case OK_OutputAssembly:
78     MPM.addPass(PrintModulePass(Out->os()));
79     break;
80   case OK_OutputBitcode:
81     MPM.addPass(BitcodeWriterPass(Out->os()));
82     break;
83   }
84
85   // Before executing passes, print the final values of the LLVM options.
86   cl::PrintOptionValues();
87
88   // Now that we have all of the passes ready, run them.
89   MPM.run(M, &MAM);
90
91   // Declare success.
92   if (OK != OK_NoOutput)
93     Out->keep();
94   return true;
95 }