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