[PM] Wire up support for writing bitcode with new PM.
[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/Support/CommandLine.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/ToolOutputFile.h"
27
28 using namespace llvm;
29 using namespace opt_tool;
30
31 bool llvm::runPassPipeline(StringRef Arg0, LLVMContext &Context, Module &M,
32                            tool_output_file *Out, StringRef PassPipeline,
33                            OutputKind OK) {
34   ModulePassManager MPM;
35   if (!parsePassPipeline(MPM, PassPipeline)) {
36     errs() << Arg0 << ": unable to parse pass pipeline description.\n";
37     return false;
38   }
39
40   // Add any relevant output pass at the end of the pipeline.
41   switch (OK) {
42   case OK_NoOutput:
43     break; // No output pass needed.
44   case OK_OutputAssembly:
45     MPM.addPass(PrintModulePass(Out->os()));
46     break;
47   case OK_OutputBitcode:
48     MPM.addPass(BitcodeWriterPass(Out->os()));
49     break;
50   }
51
52   // Before executing passes, print the final values of the LLVM options.
53   cl::PrintOptionValues();
54
55   // Now that we have all of the passes ready, run them.
56   MPM.run(&M);
57
58   // Declare success.
59   if (OK != OK_NoOutput)
60     Out->keep();
61   return true;
62 }