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