[PM] Wire the analysis passes (such as they are) into the registry, and
[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/LazyCallGraph.h"
20 #include "llvm/Bitcode/BitcodeWriterPass.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/Support/CommandLine.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/ToolOutputFile.h"
29
30 using namespace llvm;
31 using namespace opt_tool;
32
33 bool llvm::runPassPipeline(StringRef Arg0, LLVMContext &Context, Module &M,
34                            tool_output_file *Out, StringRef PassPipeline,
35                            OutputKind OK, VerifierKind VK) {
36   FunctionAnalysisManager FAM;
37   ModuleAnalysisManager MAM;
38
39 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
40   MAM.registerPass(CREATE_PASS);
41 #include "PassRegistry.def"
42
43 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
44   FAM.registerPass(CREATE_PASS);
45 #include "PassRegistry.def"
46
47   // Cross register the analysis managers through their proxies.
48   MAM.registerPass(FunctionAnalysisManagerModuleProxy(FAM));
49   FAM.registerPass(ModuleAnalysisManagerFunctionProxy(MAM));
50
51   ModulePassManager MPM;
52   if (VK > VK_NoVerifier)
53     MPM.addPass(VerifierPass());
54
55   if (!parsePassPipeline(MPM, PassPipeline, VK == VK_VerifyEachPass)) {
56     errs() << Arg0 << ": unable to parse pass pipeline description.\n";
57     return false;
58   }
59
60   if (VK > VK_NoVerifier)
61     MPM.addPass(VerifierPass());
62
63   // Add any relevant output pass at the end of the pipeline.
64   switch (OK) {
65   case OK_NoOutput:
66     break; // No output pass needed.
67   case OK_OutputAssembly:
68     MPM.addPass(PrintModulePass(Out->os()));
69     break;
70   case OK_OutputBitcode:
71     MPM.addPass(BitcodeWriterPass(Out->os()));
72     break;
73   }
74
75   // Before executing passes, print the final values of the LLVM options.
76   cl::PrintOptionValues();
77
78   // Now that we have all of the passes ready, run them.
79   MPM.run(&M, &MAM);
80
81   // Declare success.
82   if (OK != OK_NoOutput)
83     Out->keep();
84   return true;
85 }