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