blob: 313db544aba5307651d65eff17545f0fe9ae8284 [file] [log] [blame]
Chandler Carruth66445382014-01-11 08:16:35 +00001//===- 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 Carruthb7bdfd62014-01-13 07:38:24 +000019#include "llvm/Bitcode/BitcodeWriterPass.h"
Chandler Carruthb353c3f2014-01-13 05:16:45 +000020#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000021#include "llvm/IR/LLVMContext.h"
22#include "llvm/IR/Module.h"
23#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000024#include "llvm/IR/Verifier.h"
Chandler Carruth66445382014-01-11 08:16:35 +000025#include "llvm/Support/CommandLine.h"
Chandler Carruthb353c3f2014-01-13 05:16:45 +000026#include "llvm/Support/ErrorHandling.h"
Chandler Carruth66445382014-01-11 08:16:35 +000027#include "llvm/Support/ToolOutputFile.h"
28
29using namespace llvm;
Chandler Carruth949282e2014-01-13 03:08:40 +000030using namespace opt_tool;
Chandler Carruth66445382014-01-11 08:16:35 +000031
32bool llvm::runPassPipeline(StringRef Arg0, LLVMContext &Context, Module &M,
33 tool_output_file *Out, StringRef PassPipeline,
Chandler Carruth4d356312014-01-20 11:34:08 +000034 OutputKind OK, VerifierKind VK) {
Chandler Carruthc68d0822014-02-06 04:25:13 +000035 FunctionAnalysisManager FAM;
36 ModuleAnalysisManager MAM;
Chandler Carruth4d356312014-01-20 11:34:08 +000037
Chandler Carruthc68d0822014-02-06 04:25:13 +000038 // FIXME: Lift this registration of analysis passes into a .def file adjacent
39 // to the one used to associate names with passes.
40 MAM.registerPass(LazyCallGraphAnalysis());
41
42 // Cross register the analysis managers through their proxies.
43 MAM.registerPass(FunctionAnalysisManagerModuleProxy(FAM));
44 FAM.registerPass(ModuleAnalysisManagerFunctionProxy(MAM));
45
46 ModulePassManager MPM;
Chandler Carruth4d356312014-01-20 11:34:08 +000047 if (VK > VK_NoVerifier)
48 MPM.addPass(VerifierPass());
49
50 if (!parsePassPipeline(MPM, PassPipeline, VK == VK_VerifyEachPass)) {
Chandler Carruth66445382014-01-11 08:16:35 +000051 errs() << Arg0 << ": unable to parse pass pipeline description.\n";
52 return false;
53 }
54
Chandler Carruth4d356312014-01-20 11:34:08 +000055 if (VK > VK_NoVerifier)
56 MPM.addPass(VerifierPass());
57
Chandler Carruthb353c3f2014-01-13 05:16:45 +000058 // Add any relevant output pass at the end of the pipeline.
59 switch (OK) {
60 case OK_NoOutput:
61 break; // No output pass needed.
62 case OK_OutputAssembly:
63 MPM.addPass(PrintModulePass(Out->os()));
64 break;
65 case OK_OutputBitcode:
Chandler Carruthb7bdfd62014-01-13 07:38:24 +000066 MPM.addPass(BitcodeWriterPass(Out->os()));
67 break;
Chandler Carruthb353c3f2014-01-13 05:16:45 +000068 }
69
70 // Before executing passes, print the final values of the LLVM options.
71 cl::PrintOptionValues();
72
Chandler Carruth66445382014-01-11 08:16:35 +000073 // Now that we have all of the passes ready, run them.
Chandler Carruthc68d0822014-02-06 04:25:13 +000074 MPM.run(&M, &MAM);
Chandler Carruth66445382014-01-11 08:16:35 +000075
76 // Declare success.
Chandler Carruth949282e2014-01-13 03:08:40 +000077 if (OK != OK_NoOutput)
Chandler Carruth66445382014-01-11 08:16:35 +000078 Out->keep();
79 return true;
80}