blob: 130474408bdac33b53d73896769812cc31f5f4f5 [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"
Chandler Carruth66445382014-01-11 08:16:35 +000017#include "llvm/ADT/StringRef.h"
Chandler Carruthedf59962016-02-18 09:45:17 +000018#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000019#include "llvm/Analysis/CGSCCPassManager.h"
Justin Bognereecc3c82016-02-25 07:23:08 +000020#include "llvm/Analysis/LoopPassManager.h"
Chandler Carruthb7bdfd62014-01-13 07:38:24 +000021#include "llvm/Bitcode/BitcodeWriterPass.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000022#include "llvm/IR/Dominators.h"
Chandler Carruthb353c3f2014-01-13 05:16:45 +000023#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000024#include "llvm/IR/LLVMContext.h"
25#include "llvm/IR/Module.h"
26#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000027#include "llvm/IR/Verifier.h"
Chandler Carruth1ff77242015-03-07 09:02:36 +000028#include "llvm/Passes/PassBuilder.h"
Chandler Carruth66445382014-01-11 08:16:35 +000029#include "llvm/Support/CommandLine.h"
Chandler Carruthb353c3f2014-01-13 05:16:45 +000030#include "llvm/Support/ErrorHandling.h"
Chandler Carruth66445382014-01-11 08:16:35 +000031#include "llvm/Support/ToolOutputFile.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000032#include "llvm/Target/TargetMachine.h"
Chandler Carruth66445382014-01-11 08:16:35 +000033
34using namespace llvm;
Chandler Carruth949282e2014-01-13 03:08:40 +000035using namespace opt_tool;
Chandler Carruth66445382014-01-11 08:16:35 +000036
Chandler Carruth14a759e2015-01-13 22:42:38 +000037static cl::opt<bool>
38 DebugPM("debug-pass-manager", cl::Hidden,
39 cl::desc("Print pass management debugging information"));
40
Chandler Carruthedf59962016-02-18 09:45:17 +000041// This flag specifies a textual description of the alias analysis pipeline to
42// use when querying for aliasing information. It only works in concert with
43// the "passes" flag above.
44static cl::opt<std::string>
45 AAPipeline("aa-pipeline",
46 cl::desc("A textual description of the alias analysis "
47 "pipeline for handling managed aliasing queries"),
48 cl::Hidden);
49
Chandler Carruth66445382014-01-11 08:16:35 +000050bool llvm::runPassPipeline(StringRef Arg0, LLVMContext &Context, Module &M,
Chandler Carruthe0385522015-02-01 10:11:22 +000051 TargetMachine *TM, tool_output_file *Out,
52 StringRef PassPipeline, OutputKind OK,
Duncan P. N. Exon Smith679db332015-04-15 00:34:24 +000053 VerifierKind VK,
Duncan P. N. Exon Smith8a74f682015-04-15 02:38:06 +000054 bool ShouldPreserveAssemblyUseListOrder,
Duncan P. N. Exon Smith679db332015-04-15 00:34:24 +000055 bool ShouldPreserveBitcodeUseListOrder) {
Chandler Carruth1ff77242015-03-07 09:02:36 +000056 PassBuilder PB(TM);
Chandler Carruth2dc92e92015-02-01 07:40:05 +000057
Chandler Carruthedf59962016-02-18 09:45:17 +000058 // Specially handle the alias analysis manager so that we can register
59 // a custom pipeline of AA passes with it.
60 AAManager AA;
61 if (!PB.parseAAPipeline(AA, AAPipeline)) {
62 errs() << Arg0 << ": unable to parse AA pipeline description.\n";
63 return false;
64 }
65
Justin Bognereecc3c82016-02-25 07:23:08 +000066 LoopAnalysisManager LAM(DebugPM);
Chandler Carruth14a759e2015-01-13 22:42:38 +000067 FunctionAnalysisManager FAM(DebugPM);
68 CGSCCAnalysisManager CGAM(DebugPM);
69 ModuleAnalysisManager MAM(DebugPM);
Chandler Carruth4d356312014-01-20 11:34:08 +000070
Chandler Carruthedf59962016-02-18 09:45:17 +000071 // Register the AA manager first so that our version is the one used.
72 FAM.registerPass([&] { return std::move(AA); });
73
Chandler Carruthb70f6732015-01-06 02:21:37 +000074 // Register all the basic analyses with the managers.
Chandler Carruth1ff77242015-03-07 09:02:36 +000075 PB.registerModuleAnalyses(MAM);
76 PB.registerCGSCCAnalyses(CGAM);
77 PB.registerFunctionAnalyses(FAM);
Justin Bognereecc3c82016-02-25 07:23:08 +000078 PB.registerLoopAnalyses(LAM);
Chandler Carruthbf71a342014-02-06 04:37:03 +000079
Chandler Carruthc68d0822014-02-06 04:25:13 +000080 // Cross register the analysis managers through their proxies.
Chandler Carruthedf59962016-02-18 09:45:17 +000081 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
82 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
83 CGAM.registerPass([&] { return FunctionAnalysisManagerCGSCCProxy(FAM); });
84 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
85 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
86 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
Justin Bognereecc3c82016-02-25 07:23:08 +000087 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
88 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
Chandler Carruthc68d0822014-02-06 04:25:13 +000089
Chandler Carruth14a759e2015-01-13 22:42:38 +000090 ModulePassManager MPM(DebugPM);
Chandler Carruth4d356312014-01-20 11:34:08 +000091 if (VK > VK_NoVerifier)
92 MPM.addPass(VerifierPass());
93
Chandler Carruth1ff77242015-03-07 09:02:36 +000094 if (!PB.parsePassPipeline(MPM, PassPipeline, VK == VK_VerifyEachPass,
95 DebugPM)) {
Chandler Carruth66445382014-01-11 08:16:35 +000096 errs() << Arg0 << ": unable to parse pass pipeline description.\n";
97 return false;
98 }
99
Chandler Carruth4d356312014-01-20 11:34:08 +0000100 if (VK > VK_NoVerifier)
101 MPM.addPass(VerifierPass());
102
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000103 // Add any relevant output pass at the end of the pipeline.
104 switch (OK) {
105 case OK_NoOutput:
106 break; // No output pass needed.
107 case OK_OutputAssembly:
Duncan P. N. Exon Smith8a74f682015-04-15 02:38:06 +0000108 MPM.addPass(
109 PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder));
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000110 break;
111 case OK_OutputBitcode:
Duncan P. N. Exon Smith679db332015-04-15 00:34:24 +0000112 MPM.addPass(
113 BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder));
Chandler Carruthb7bdfd62014-01-13 07:38:24 +0000114 break;
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000115 }
116
117 // Before executing passes, print the final values of the LLVM options.
118 cl::PrintOptionValues();
119
Chandler Carruth66445382014-01-11 08:16:35 +0000120 // Now that we have all of the passes ready, run them.
Chandler Carruthb47f8012016-03-11 11:05:24 +0000121 MPM.run(M, MAM);
Chandler Carruth66445382014-01-11 08:16:35 +0000122
123 // Declare success.
Chandler Carruth949282e2014-01-13 03:08:40 +0000124 if (OK != OK_NoOutput)
Chandler Carruth66445382014-01-11 08:16:35 +0000125 Out->keep();
126 return true;
127}