Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 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" |
Chandler Carruth | b7bdfd6 | 2014-01-13 07:38:24 +0000 | [diff] [blame] | 19 | #include "llvm/Bitcode/BitcodeWriterPass.h" |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 20 | #include "llvm/IR/IRPrintingPasses.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 21 | #include "llvm/IR/LLVMContext.h" |
| 22 | #include "llvm/IR/Module.h" |
| 23 | #include "llvm/IR/PassManager.h" |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Verifier.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ToolOutputFile.h" |
| 28 | |
| 29 | using namespace llvm; |
Chandler Carruth | 949282e | 2014-01-13 03:08:40 +0000 | [diff] [blame] | 30 | using namespace opt_tool; |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 31 | |
| 32 | bool llvm::runPassPipeline(StringRef Arg0, LLVMContext &Context, Module &M, |
| 33 | tool_output_file *Out, StringRef PassPipeline, |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 34 | OutputKind OK, VerifierKind VK) { |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 35 | ModulePassManager MPM; |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 36 | |
| 37 | if (VK > VK_NoVerifier) |
| 38 | MPM.addPass(VerifierPass()); |
| 39 | |
| 40 | if (!parsePassPipeline(MPM, PassPipeline, VK == VK_VerifyEachPass)) { |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 41 | errs() << Arg0 << ": unable to parse pass pipeline description.\n"; |
| 42 | return false; |
| 43 | } |
| 44 | |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 45 | if (VK > VK_NoVerifier) |
| 46 | MPM.addPass(VerifierPass()); |
| 47 | |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 48 | // Add any relevant output pass at the end of the pipeline. |
| 49 | switch (OK) { |
| 50 | case OK_NoOutput: |
| 51 | break; // No output pass needed. |
| 52 | case OK_OutputAssembly: |
| 53 | MPM.addPass(PrintModulePass(Out->os())); |
| 54 | break; |
| 55 | case OK_OutputBitcode: |
Chandler Carruth | b7bdfd6 | 2014-01-13 07:38:24 +0000 | [diff] [blame] | 56 | MPM.addPass(BitcodeWriterPass(Out->os())); |
| 57 | break; |
Chandler Carruth | b353c3f | 2014-01-13 05:16:45 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // Before executing passes, print the final values of the LLVM options. |
| 61 | cl::PrintOptionValues(); |
| 62 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 63 | // Now that we have all of the passes ready, run them. |
| 64 | MPM.run(&M); |
| 65 | |
| 66 | // Declare success. |
Chandler Carruth | 949282e | 2014-01-13 03:08:40 +0000 | [diff] [blame] | 67 | if (OK != OK_NoOutput) |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 68 | Out->keep(); |
| 69 | return true; |
| 70 | } |