blob: f46d37ee8c7af21d94b1477e51395e221a287456 [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"
Chandler Carruthb7bdfd62014-01-13 07:38:24 +000020#include "llvm/Bitcode/BitcodeWriterPass.h"
Philip Pfaffe59d690b2017-08-04 09:28:09 +000021#include "llvm/Config/config.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"
Tim Shen6b4114182017-06-01 01:02:12 +000033#include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +000034#include "llvm/Transforms/Scalar/LoopPassManager.h"
Chandler Carruth66445382014-01-11 08:16:35 +000035
36using namespace llvm;
Chandler Carruth949282e2014-01-13 03:08:40 +000037using namespace opt_tool;
Chandler Carruth66445382014-01-11 08:16:35 +000038
Chandler Carruth14a759e2015-01-13 22:42:38 +000039static cl::opt<bool>
40 DebugPM("debug-pass-manager", cl::Hidden,
41 cl::desc("Print pass management debugging information"));
42
Chandler Carruthedf59962016-02-18 09:45:17 +000043// This flag specifies a textual description of the alias analysis pipeline to
44// use when querying for aliasing information. It only works in concert with
45// the "passes" flag above.
46static cl::opt<std::string>
47 AAPipeline("aa-pipeline",
48 cl::desc("A textual description of the alias analysis "
49 "pipeline for handling managed aliasing queries"),
50 cl::Hidden);
51
Philip Pfaffe730f2f92017-07-10 10:57:55 +000052/// {{@ These options accept textual pipeline descriptions which will be
53/// inserted into default pipelines at the respective extension points
54static cl::opt<std::string> PeepholeEPPipeline(
55 "passes-ep-peephole",
56 cl::desc("A textual description of the function pass pipeline inserted at "
57 "the Peephole extension points into default pipelines"),
58 cl::Hidden);
59static cl::opt<std::string> LateLoopOptimizationsEPPipeline(
60 "passes-ep-late-loop-optimizations",
61 cl::desc(
62 "A textual description of the loop pass pipeline inserted at "
63 "the LateLoopOptimizations extension point into default pipelines"),
64 cl::Hidden);
65static cl::opt<std::string> LoopOptimizerEndEPPipeline(
66 "passes-ep-loop-optimizer-end",
67 cl::desc("A textual description of the loop pass pipeline inserted at "
68 "the LoopOptimizerEnd extension point into default pipelines"),
69 cl::Hidden);
70static cl::opt<std::string> ScalarOptimizerLateEPPipeline(
71 "passes-ep-scalar-optimizer-late",
72 cl::desc("A textual description of the function pass pipeline inserted at "
73 "the ScalarOptimizerLate extension point into default pipelines"),
74 cl::Hidden);
75static cl::opt<std::string> CGSCCOptimizerLateEPPipeline(
76 "passes-ep-cgscc-optimizer-late",
77 cl::desc("A textual description of the cgscc pass pipeline inserted at "
78 "the CGSCCOptimizerLate extension point into default pipelines"),
79 cl::Hidden);
80static cl::opt<std::string> VectorizerStartEPPipeline(
81 "passes-ep-vectorizer-start",
82 cl::desc("A textual description of the function pass pipeline inserted at "
83 "the VectorizerStart extension point into default pipelines"),
84 cl::Hidden);
Dehao Chen7b05a272017-07-26 02:00:43 +000085enum PGOKind { NoPGO, InstrGen, InstrUse, SampleUse };
86static cl::opt<PGOKind> PGOKindFlag(
87 "pgo-kind", cl::init(NoPGO), cl::Hidden,
88 cl::desc("The kind of profile guided optimization"),
89 cl::values(clEnumValN(NoPGO, "nopgo", "Do not use PGO."),
90 clEnumValN(InstrGen, "new-pm-pgo-instr-gen-pipeline",
91 "Instrument the IR to generate profile."),
92 clEnumValN(InstrUse, "new-pm-pgo-instr-use-pipeline",
93 "Use instrumented profile to guide PGO."),
94 clEnumValN(SampleUse, "new-pm-pgo-sample-use-pipeline",
95 "Use sampled profile to guide PGO.")));
96static cl::opt<std::string> ProfileFile(
97 "profile-file", cl::desc("Path to the profile."), cl::Hidden);
Dehao Chene90d0152017-07-26 15:01:20 +000098static cl::opt<bool> DebugInfoForProfiling(
99 "new-pm-debug-info-for-profiling", cl::init(false), cl::Hidden,
100 cl::desc("Emit special debug info to enable PGO profile generation."));
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000101/// @}}
102
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000103template <typename PassManagerT>
104bool tryParsePipelineText(PassBuilder &PB, StringRef PipelineText) {
105 if (PipelineText.empty())
106 return false;
107
108 // Verify the pipeline is parseable:
109 PassManagerT PM;
110 if (PB.parsePassPipeline(PM, PipelineText))
111 return true;
112
113 errs() << "Could not parse pipeline '" << PipelineText
114 << "'. I'm going to igore it.\n";
115 return false;
116}
117
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000118/// If one of the EPPipeline command line options was given, register callbacks
119/// for parsing and inserting the given pipeline
120static void registerEPCallbacks(PassBuilder &PB, bool VerifyEachPass,
121 bool DebugLogging) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000122 if (tryParsePipelineText<FunctionPassManager>(PB, PeepholeEPPipeline))
Philip Pfaffea3b84162017-07-10 12:48:51 +0000123 PB.registerPeepholeEPCallback([&PB, VerifyEachPass, DebugLogging](
124 FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000125 PB.parsePassPipeline(PM, PeepholeEPPipeline, VerifyEachPass,
126 DebugLogging);
Philip Pfaffea3b84162017-07-10 12:48:51 +0000127 });
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000128 if (tryParsePipelineText<LoopPassManager>(PB,
129 LateLoopOptimizationsEPPipeline))
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000130 PB.registerLateLoopOptimizationsEPCallback(
Philip Pfaffea3b84162017-07-10 12:48:51 +0000131 [&PB, VerifyEachPass, DebugLogging](
132 LoopPassManager &PM, PassBuilder::OptimizationLevel Level) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000133 PB.parsePassPipeline(PM, LateLoopOptimizationsEPPipeline,
134 VerifyEachPass, DebugLogging);
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000135 });
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000136 if (tryParsePipelineText<LoopPassManager>(PB, LoopOptimizerEndEPPipeline))
Philip Pfaffea3b84162017-07-10 12:48:51 +0000137 PB.registerLoopOptimizerEndEPCallback([&PB, VerifyEachPass, DebugLogging](
138 LoopPassManager &PM, PassBuilder::OptimizationLevel Level) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000139 PB.parsePassPipeline(PM, LoopOptimizerEndEPPipeline, VerifyEachPass,
140 DebugLogging);
Philip Pfaffea3b84162017-07-10 12:48:51 +0000141 });
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000142 if (tryParsePipelineText<FunctionPassManager>(PB,
143 ScalarOptimizerLateEPPipeline))
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000144 PB.registerScalarOptimizerLateEPCallback(
Philip Pfaffea3b84162017-07-10 12:48:51 +0000145 [&PB, VerifyEachPass, DebugLogging](
146 FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000147 PB.parsePassPipeline(PM, ScalarOptimizerLateEPPipeline,
148 VerifyEachPass, DebugLogging);
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000149 });
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000150 if (tryParsePipelineText<CGSCCPassManager>(PB, CGSCCOptimizerLateEPPipeline))
Philip Pfaffea3b84162017-07-10 12:48:51 +0000151 PB.registerCGSCCOptimizerLateEPCallback([&PB, VerifyEachPass, DebugLogging](
152 CGSCCPassManager &PM, PassBuilder::OptimizationLevel Level) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000153 PB.parsePassPipeline(PM, CGSCCOptimizerLateEPPipeline, VerifyEachPass,
154 DebugLogging);
Philip Pfaffea3b84162017-07-10 12:48:51 +0000155 });
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000156 if (tryParsePipelineText<FunctionPassManager>(PB, VectorizerStartEPPipeline))
Philip Pfaffea3b84162017-07-10 12:48:51 +0000157 PB.registerVectorizerStartEPCallback([&PB, VerifyEachPass, DebugLogging](
158 FunctionPassManager &PM, PassBuilder::OptimizationLevel Level) {
Philip Pfaffeb4d500f2017-07-11 11:17:44 +0000159 PB.parsePassPipeline(PM, VectorizerStartEPPipeline, VerifyEachPass,
160 DebugLogging);
Philip Pfaffea3b84162017-07-10 12:48:51 +0000161 });
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000162}
163
Philip Pfaffe59d690b2017-08-04 09:28:09 +0000164#ifdef LINK_POLLY_INTO_TOOLS
165namespace polly {
166void RegisterPollyPasses(PassBuilder &);
167}
168#endif
169
Tim Shen6b4114182017-06-01 01:02:12 +0000170bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
171 tool_output_file *Out,
172 tool_output_file *ThinLTOLinkOut,
Chandler Carruthe0385522015-02-01 10:11:22 +0000173 StringRef PassPipeline, OutputKind OK,
Duncan P. N. Exon Smith679db332015-04-15 00:34:24 +0000174 VerifierKind VK,
Duncan P. N. Exon Smith8a74f682015-04-15 02:38:06 +0000175 bool ShouldPreserveAssemblyUseListOrder,
Teresa Johnsonf93b2462016-08-12 13:53:02 +0000176 bool ShouldPreserveBitcodeUseListOrder,
177 bool EmitSummaryIndex, bool EmitModuleHash) {
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000178 bool VerifyEachPass = VK == VK_VerifyEachPass;
Dehao Chen7b05a272017-07-26 02:00:43 +0000179
180 Optional<PGOOptions> P;
181 switch (PGOKindFlag) {
182 case InstrGen:
183 P = PGOOptions(ProfileFile, "", "", true);
184 break;
185 case InstrUse:
186 P = PGOOptions("", ProfileFile, "", false);
187 break;
188 case SampleUse:
189 P = PGOOptions("", "", ProfileFile, false);
190 break;
191 case NoPGO:
Dehao Chene90d0152017-07-26 15:01:20 +0000192 if (DebugInfoForProfiling)
193 P = PGOOptions("", "", "", false, true);
194 else
195 P = None;
Dehao Chen7b05a272017-07-26 02:00:43 +0000196 }
197 PassBuilder PB(TM, P);
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000198 registerEPCallbacks(PB, VerifyEachPass, DebugPM);
Chandler Carruth2dc92e92015-02-01 07:40:05 +0000199
Philip Pfaffe59d690b2017-08-04 09:28:09 +0000200#ifdef LINK_POLLY_INTO_TOOLS
201 polly::RegisterPollyPasses(PB);
202#endif
203
Chandler Carruthedf59962016-02-18 09:45:17 +0000204 // Specially handle the alias analysis manager so that we can register
205 // a custom pipeline of AA passes with it.
206 AAManager AA;
207 if (!PB.parseAAPipeline(AA, AAPipeline)) {
208 errs() << Arg0 << ": unable to parse AA pipeline description.\n";
209 return false;
210 }
211
Justin Bognereecc3c82016-02-25 07:23:08 +0000212 LoopAnalysisManager LAM(DebugPM);
Chandler Carruth14a759e2015-01-13 22:42:38 +0000213 FunctionAnalysisManager FAM(DebugPM);
214 CGSCCAnalysisManager CGAM(DebugPM);
215 ModuleAnalysisManager MAM(DebugPM);
Chandler Carruth4d356312014-01-20 11:34:08 +0000216
Chandler Carruthedf59962016-02-18 09:45:17 +0000217 // Register the AA manager first so that our version is the one used.
218 FAM.registerPass([&] { return std::move(AA); });
219
Chandler Carruthb70f6732015-01-06 02:21:37 +0000220 // Register all the basic analyses with the managers.
Chandler Carruth1ff77242015-03-07 09:02:36 +0000221 PB.registerModuleAnalyses(MAM);
222 PB.registerCGSCCAnalyses(CGAM);
223 PB.registerFunctionAnalyses(FAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000224 PB.registerLoopAnalyses(LAM);
Davide Italianob6ccd6b2016-05-14 23:21:50 +0000225 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
Chandler Carruthc68d0822014-02-06 04:25:13 +0000226
Chandler Carruth14a759e2015-01-13 22:42:38 +0000227 ModulePassManager MPM(DebugPM);
Chandler Carruth4d356312014-01-20 11:34:08 +0000228 if (VK > VK_NoVerifier)
229 MPM.addPass(VerifierPass());
230
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000231 if (!PB.parsePassPipeline(MPM, PassPipeline, VerifyEachPass, DebugPM)) {
Chandler Carruth66445382014-01-11 08:16:35 +0000232 errs() << Arg0 << ": unable to parse pass pipeline description.\n";
233 return false;
234 }
235
Chandler Carruth4d356312014-01-20 11:34:08 +0000236 if (VK > VK_NoVerifier)
237 MPM.addPass(VerifierPass());
238
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000239 // Add any relevant output pass at the end of the pipeline.
240 switch (OK) {
241 case OK_NoOutput:
242 break; // No output pass needed.
243 case OK_OutputAssembly:
Duncan P. N. Exon Smith8a74f682015-04-15 02:38:06 +0000244 MPM.addPass(
245 PrintModulePass(Out->os(), "", ShouldPreserveAssemblyUseListOrder));
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000246 break;
247 case OK_OutputBitcode:
Teresa Johnsonf93b2462016-08-12 13:53:02 +0000248 MPM.addPass(BitcodeWriterPass(Out->os(), ShouldPreserveBitcodeUseListOrder,
249 EmitSummaryIndex, EmitModuleHash));
Chandler Carruthb7bdfd62014-01-13 07:38:24 +0000250 break;
Tim Shen6b4114182017-06-01 01:02:12 +0000251 case OK_OutputThinLTOBitcode:
252 MPM.addPass(ThinLTOBitcodeWriterPass(
253 Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr));
254 break;
Chandler Carruthb353c3f2014-01-13 05:16:45 +0000255 }
256
257 // Before executing passes, print the final values of the LLVM options.
258 cl::PrintOptionValues();
259
Chandler Carruth66445382014-01-11 08:16:35 +0000260 // Now that we have all of the passes ready, run them.
Chandler Carruthb47f8012016-03-11 11:05:24 +0000261 MPM.run(M, MAM);
Chandler Carruth66445382014-01-11 08:16:35 +0000262
263 // Declare success.
Tim Shen6b4114182017-06-01 01:02:12 +0000264 if (OK != OK_NoOutput) {
Chandler Carruth66445382014-01-11 08:16:35 +0000265 Out->keep();
Tim Shen6b4114182017-06-01 01:02:12 +0000266 if (OK == OK_OutputThinLTOBitcode && ThinLTOLinkOut)
267 ThinLTOLinkOut->keep();
268 }
Chandler Carruth66445382014-01-11 08:16:35 +0000269 return true;
270}