blob: a73750dd4920c50dcccec8f751c847a44abb2a55 [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 Carruth64764b42015-01-14 10:19:28 +000021#include "llvm/IR/Dominators.h"
Chandler Carruthb353c3f2014-01-13 05:16:45 +000022#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000023#include "llvm/IR/LLVMContext.h"
24#include "llvm/IR/Module.h"
25#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000026#include "llvm/IR/Verifier.h"
Chandler Carruth66445382014-01-11 08:16:35 +000027#include "llvm/Support/CommandLine.h"
Chandler Carruthb353c3f2014-01-13 05:16:45 +000028#include "llvm/Support/ErrorHandling.h"
Chandler Carruth66445382014-01-11 08:16:35 +000029#include "llvm/Support/ToolOutputFile.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000030#include "llvm/Target/TargetMachine.h"
Chandler Carruth66445382014-01-11 08:16:35 +000031
32using namespace llvm;
Chandler Carruth949282e2014-01-13 03:08:40 +000033using namespace opt_tool;
Chandler Carruth66445382014-01-11 08:16:35 +000034
Chandler Carruth14a759e2015-01-13 22:42:38 +000035static cl::opt<bool>
36 DebugPM("debug-pass-manager", cl::Hidden,
37 cl::desc("Print pass management debugging information"));
38
Chandler Carruth66445382014-01-11 08:16:35 +000039bool llvm::runPassPipeline(StringRef Arg0, LLVMContext &Context, Module &M,
Chandler Carruthe0385522015-02-01 10:11:22 +000040 TargetMachine *TM, tool_output_file *Out,
41 StringRef PassPipeline, OutputKind OK,
42 VerifierKind VK) {
43 Passes P(TM);
Chandler Carruth2dc92e92015-02-01 07:40:05 +000044
Chandler Carruth14a759e2015-01-13 22:42:38 +000045 FunctionAnalysisManager FAM(DebugPM);
46 CGSCCAnalysisManager CGAM(DebugPM);
47 ModuleAnalysisManager MAM(DebugPM);
Chandler Carruth4d356312014-01-20 11:34:08 +000048
Chandler Carruthb70f6732015-01-06 02:21:37 +000049 // Register all the basic analyses with the managers.
Chandler Carruth2dc92e92015-02-01 07:40:05 +000050 P.registerModuleAnalyses(MAM);
51 P.registerCGSCCAnalyses(CGAM);
52 P.registerFunctionAnalyses(FAM);
Chandler Carruthbf71a342014-02-06 04:37:03 +000053
Chandler Carruthc68d0822014-02-06 04:25:13 +000054 // Cross register the analysis managers through their proxies.
55 MAM.registerPass(FunctionAnalysisManagerModuleProxy(FAM));
Chandler Carruth572e3402014-04-21 11:12:00 +000056 MAM.registerPass(CGSCCAnalysisManagerModuleProxy(CGAM));
57 CGAM.registerPass(FunctionAnalysisManagerCGSCCProxy(FAM));
58 CGAM.registerPass(ModuleAnalysisManagerCGSCCProxy(MAM));
59 FAM.registerPass(CGSCCAnalysisManagerFunctionProxy(CGAM));
Chandler Carruthc68d0822014-02-06 04:25:13 +000060 FAM.registerPass(ModuleAnalysisManagerFunctionProxy(MAM));
61
Chandler Carruth14a759e2015-01-13 22:42:38 +000062 ModulePassManager MPM(DebugPM);
Chandler Carruth4d356312014-01-20 11:34:08 +000063 if (VK > VK_NoVerifier)
64 MPM.addPass(VerifierPass());
65
Chandler Carruth2dc92e92015-02-01 07:40:05 +000066 if (!P.parsePassPipeline(MPM, PassPipeline, VK == VK_VerifyEachPass,
67 DebugPM)) {
Chandler Carruth66445382014-01-11 08:16:35 +000068 errs() << Arg0 << ": unable to parse pass pipeline description.\n";
69 return false;
70 }
71
Chandler Carruth4d356312014-01-20 11:34:08 +000072 if (VK > VK_NoVerifier)
73 MPM.addPass(VerifierPass());
74
Chandler Carruthb353c3f2014-01-13 05:16:45 +000075 // Add any relevant output pass at the end of the pipeline.
76 switch (OK) {
77 case OK_NoOutput:
78 break; // No output pass needed.
79 case OK_OutputAssembly:
80 MPM.addPass(PrintModulePass(Out->os()));
81 break;
82 case OK_OutputBitcode:
Chandler Carruthb7bdfd62014-01-13 07:38:24 +000083 MPM.addPass(BitcodeWriterPass(Out->os()));
84 break;
Chandler Carruthb353c3f2014-01-13 05:16:45 +000085 }
86
87 // Before executing passes, print the final values of the LLVM options.
88 cl::PrintOptionValues();
89
Chandler Carruth66445382014-01-11 08:16:35 +000090 // Now that we have all of the passes ready, run them.
Chandler Carruthd174ce42015-01-05 02:47:05 +000091 MPM.run(M, &MAM);
Chandler Carruth66445382014-01-11 08:16:35 +000092
93 // Declare success.
Chandler Carruth949282e2014-01-13 03:08:40 +000094 if (OK != OK_NoOutput)
Chandler Carruth66445382014-01-11 08:16:35 +000095 Out->keep();
96 return true;
97}