blob: 9e24f30c1886020e95431b1ed8dcd859ab9fc7ee [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 Carruth572e3402014-04-21 11:12:00 +000019#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruthb7bdfd62014-01-13 07:38:24 +000020#include "llvm/Bitcode/BitcodeWriterPass.h"
Chandler Carruthb353c3f2014-01-13 05:16:45 +000021#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000022#include "llvm/IR/LLVMContext.h"
23#include "llvm/IR/Module.h"
24#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000025#include "llvm/IR/Verifier.h"
Chandler Carruth66445382014-01-11 08:16:35 +000026#include "llvm/Support/CommandLine.h"
Chandler Carruthb353c3f2014-01-13 05:16:45 +000027#include "llvm/Support/ErrorHandling.h"
Chandler Carruth66445382014-01-11 08:16:35 +000028#include "llvm/Support/ToolOutputFile.h"
29
30using namespace llvm;
Chandler Carruth949282e2014-01-13 03:08:40 +000031using namespace opt_tool;
Chandler Carruth66445382014-01-11 08:16:35 +000032
33bool llvm::runPassPipeline(StringRef Arg0, LLVMContext &Context, Module &M,
34 tool_output_file *Out, StringRef PassPipeline,
Chandler Carruth4d356312014-01-20 11:34:08 +000035 OutputKind OK, VerifierKind VK) {
Chandler Carruthc68d0822014-02-06 04:25:13 +000036 FunctionAnalysisManager FAM;
Chandler Carruth572e3402014-04-21 11:12:00 +000037 CGSCCAnalysisManager CGAM;
Chandler Carruthc68d0822014-02-06 04:25:13 +000038 ModuleAnalysisManager MAM;
Chandler Carruth4d356312014-01-20 11:34:08 +000039
Chandler Carruthb70f6732015-01-06 02:21:37 +000040 // Register all the basic analyses with the managers.
41 registerModuleAnalyses(MAM);
42 registerCGSCCAnalyses(CGAM);
43 registerFunctionAnalyses(FAM);
Chandler Carruthbf71a342014-02-06 04:37:03 +000044
Chandler Carruthc68d0822014-02-06 04:25:13 +000045 // Cross register the analysis managers through their proxies.
46 MAM.registerPass(FunctionAnalysisManagerModuleProxy(FAM));
Chandler Carruth572e3402014-04-21 11:12:00 +000047 MAM.registerPass(CGSCCAnalysisManagerModuleProxy(CGAM));
48 CGAM.registerPass(FunctionAnalysisManagerCGSCCProxy(FAM));
49 CGAM.registerPass(ModuleAnalysisManagerCGSCCProxy(MAM));
50 FAM.registerPass(CGSCCAnalysisManagerFunctionProxy(CGAM));
Chandler Carruthc68d0822014-02-06 04:25:13 +000051 FAM.registerPass(ModuleAnalysisManagerFunctionProxy(MAM));
52
53 ModulePassManager MPM;
Chandler Carruth4d356312014-01-20 11:34:08 +000054 if (VK > VK_NoVerifier)
55 MPM.addPass(VerifierPass());
56
57 if (!parsePassPipeline(MPM, PassPipeline, VK == VK_VerifyEachPass)) {
Chandler Carruth66445382014-01-11 08:16:35 +000058 errs() << Arg0 << ": unable to parse pass pipeline description.\n";
59 return false;
60 }
61
Chandler Carruth4d356312014-01-20 11:34:08 +000062 if (VK > VK_NoVerifier)
63 MPM.addPass(VerifierPass());
64
Chandler Carruthb353c3f2014-01-13 05:16:45 +000065 // Add any relevant output pass at the end of the pipeline.
66 switch (OK) {
67 case OK_NoOutput:
68 break; // No output pass needed.
69 case OK_OutputAssembly:
70 MPM.addPass(PrintModulePass(Out->os()));
71 break;
72 case OK_OutputBitcode:
Chandler Carruthb7bdfd62014-01-13 07:38:24 +000073 MPM.addPass(BitcodeWriterPass(Out->os()));
74 break;
Chandler Carruthb353c3f2014-01-13 05:16:45 +000075 }
76
77 // Before executing passes, print the final values of the LLVM options.
78 cl::PrintOptionValues();
79
Chandler Carruth66445382014-01-11 08:16:35 +000080 // Now that we have all of the passes ready, run them.
Chandler Carruthd174ce42015-01-05 02:47:05 +000081 MPM.run(M, &MAM);
Chandler Carruth66445382014-01-11 08:16:35 +000082
83 // Declare success.
Chandler Carruth949282e2014-01-13 03:08:40 +000084 if (OK != OK_NoOutput)
Chandler Carruth66445382014-01-11 08:16:35 +000085 Out->keep();
86 return true;
87}