blob: 38e1dd00661da8146fb5d06635f9e162ebe201e2 [file] [log] [blame]
Chandler Carruth1ff77242015-03-07 09:02:36 +00001//===- Parsing, selection, and construction of pass pipelines -------------===//
Chandler Carruth66445382014-01-11 08:16:35 +00002//
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///
Chandler Carruth1ff77242015-03-07 09:02:36 +000011/// This file provides the implementation of the PassBuilder based on our
12/// static pass registry as well as related functionality. It also provides
13/// helpers to aid in analyzing, debugging, and testing passes and pass
14/// pipelines.
Chandler Carruth66445382014-01-11 08:16:35 +000015///
16//===----------------------------------------------------------------------===//
17
Chandler Carruth1ff77242015-03-07 09:02:36 +000018#include "llvm/Passes/PassBuilder.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000019#include "llvm/ADT/StringSwitch.h"
Chandler Carruth6f5770b102016-02-13 23:32:00 +000020#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth4f846a52016-02-20 03:46:03 +000021#include "llvm/Analysis/AliasAnalysisEvaluator.h"
Chandler Carruthdf8b2232015-01-22 21:53:09 +000022#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthbece8d52016-02-13 23:46:24 +000023#include "llvm/Analysis/BasicAliasAnalysis.h"
Chandler Carruth342c6712016-02-20 03:52:02 +000024#include "llvm/Analysis/CFLAliasAnalysis.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000025#include "llvm/Analysis/CGSCCPassManager.h"
Hongbin Zheng751337f2016-02-25 17:54:15 +000026#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000027#include "llvm/Analysis/LazyCallGraph.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000028#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth61440d22016-03-10 00:55:30 +000029#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000030#include "llvm/Analysis/PostDominators.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000031#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000032#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000033#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000034#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000035#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000036#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000037#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000038#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000039#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000040#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000041#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000042#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000043#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000044#include "llvm/Target/TargetMachine.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000045#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000046#include "llvm/Transforms/IPO/FunctionAttrs.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000047#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Justin Bogner21e15372015-10-30 23:28:12 +000048#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000049#include "llvm/Transforms/InstCombine/InstCombine.h"
Justin Bogner19b67992015-10-30 23:13:18 +000050#include "llvm/Transforms/Scalar/ADCE.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000051#include "llvm/Transforms/Scalar/EarlyCSE.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000052#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000053#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000054#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Chandler Carruth58dde8c2016-02-26 12:17:54 +000055#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +000056
57using namespace llvm;
58
Chandler Carruth8b5a74192016-02-28 22:16:03 +000059static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
60
Chandler Carruth66445382014-01-11 08:16:35 +000061namespace {
62
Chandler Carruthd8330982014-01-12 09:34:22 +000063/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000064struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000065 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000066 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000067};
68
Chandler Carruth0b576b32015-01-06 02:50:06 +000069/// \brief No-op module analysis.
Chandler Carruth3a634352016-02-26 11:44:45 +000070struct NoOpModuleAnalysis : AnalysisBase<NoOpModuleAnalysis> {
Chandler Carruth0b576b32015-01-06 02:50:06 +000071 struct Result {};
72 Result run(Module &) { return Result(); }
73 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +000074};
75
Chandler Carruth572e3402014-04-21 11:12:00 +000076/// \brief No-op CGSCC pass which does nothing.
77struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000078 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +000079 return PreservedAnalyses::all();
80 }
81 static StringRef name() { return "NoOpCGSCCPass"; }
82};
83
Chandler Carruth0b576b32015-01-06 02:50:06 +000084/// \brief No-op CGSCC analysis.
Chandler Carruth3a634352016-02-26 11:44:45 +000085struct NoOpCGSCCAnalysis : AnalysisBase<NoOpCGSCCAnalysis> {
Chandler Carruth0b576b32015-01-06 02:50:06 +000086 struct Result {};
87 Result run(LazyCallGraph::SCC &) { return Result(); }
88 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +000089};
90
Chandler Carruthd8330982014-01-12 09:34:22 +000091/// \brief No-op function pass which does nothing.
92struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000093 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +000094 static StringRef name() { return "NoOpFunctionPass"; }
95};
96
Chandler Carruth0b576b32015-01-06 02:50:06 +000097/// \brief No-op function analysis.
Chandler Carruth3a634352016-02-26 11:44:45 +000098struct NoOpFunctionAnalysis : AnalysisBase<NoOpFunctionAnalysis> {
Chandler Carruth0b576b32015-01-06 02:50:06 +000099 struct Result {};
100 Result run(Function &) { return Result(); }
101 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000102};
103
Justin Bognereecc3c82016-02-25 07:23:08 +0000104/// \brief No-op loop pass which does nothing.
105struct NoOpLoopPass {
106 PreservedAnalyses run(Loop &L) { return PreservedAnalyses::all(); }
107 static StringRef name() { return "NoOpLoopPass"; }
108};
109
110/// \brief No-op loop analysis.
Chandler Carruth3a634352016-02-26 11:44:45 +0000111struct NoOpLoopAnalysis : AnalysisBase<NoOpLoopAnalysis> {
Justin Bognereecc3c82016-02-25 07:23:08 +0000112 struct Result {};
113 Result run(Loop &) { return Result(); }
114 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000115};
116
Chandler Carruth66445382014-01-11 08:16:35 +0000117} // End anonymous namespace.
118
Chandler Carruth1ff77242015-03-07 09:02:36 +0000119void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000120#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000121 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000122#include "PassRegistry.def"
123}
124
Chandler Carruth1ff77242015-03-07 09:02:36 +0000125void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000126#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000127 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000128#include "PassRegistry.def"
129}
130
Chandler Carruth1ff77242015-03-07 09:02:36 +0000131void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000132#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000133 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000134#include "PassRegistry.def"
135}
136
Justin Bognereecc3c82016-02-25 07:23:08 +0000137void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
138#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
139 LAM.registerPass([&] { return CREATE_PASS; });
140#include "PassRegistry.def"
141}
142
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000143void PassBuilder::addPerModuleDefaultPipeline(ModulePassManager &MPM,
144 OptimizationLevel Level,
145 bool DebugLogging) {
146 // FIXME: Finish fleshing this out to match the legacy pipelines.
147 FunctionPassManager EarlyFPM(DebugLogging);
148 EarlyFPM.addPass(SimplifyCFGPass());
149 EarlyFPM.addPass(SROA());
150 EarlyFPM.addPass(EarlyCSEPass());
151 EarlyFPM.addPass(LowerExpectIntrinsicPass());
152
153 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
154}
155
156void PassBuilder::addLTOPreLinkDefaultPipeline(ModulePassManager &MPM,
157 OptimizationLevel Level,
158 bool DebugLogging) {
159 // FIXME: We should use a customized pre-link pipeline!
160 addPerModuleDefaultPipeline(MPM, Level, DebugLogging);
161}
162
163void PassBuilder::addLTODefaultPipeline(ModulePassManager &MPM,
164 OptimizationLevel Level,
165 bool DebugLogging) {
166 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
167 FunctionPassManager LateFPM(DebugLogging);
168 LateFPM.addPass(InstCombinePass());
169 LateFPM.addPass(SimplifyCFGPass());
170
171 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
172}
173
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000174#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000175static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000176 // Manually handle aliases for pre-configured pipeline fragments.
177 if (Name.startswith("default") || Name.startswith("lto"))
178 return DefaultAliasRegex.match(Name);
179
Chandler Carruth58944182014-04-21 08:08:50 +0000180#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000181#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000182 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000183 return true;
184#include "PassRegistry.def"
185
Chandler Carruth66445382014-01-11 08:16:35 +0000186 return false;
187}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000188#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000189
Chandler Carruth572e3402014-04-21 11:12:00 +0000190static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000191#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000192#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000193 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000194 return true;
195#include "PassRegistry.def"
196
Chandler Carruth572e3402014-04-21 11:12:00 +0000197 return false;
198}
199
Chandler Carruthd8330982014-01-12 09:34:22 +0000200static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000201#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000202#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000203 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000204 return true;
205#include "PassRegistry.def"
206
Chandler Carruthd8330982014-01-12 09:34:22 +0000207 return false;
208}
209
Justin Bognereecc3c82016-02-25 07:23:08 +0000210static bool isLoopPassName(StringRef Name) {
211#define LOOP_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
212#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
213 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
214 return true;
215#include "PassRegistry.def"
216
217 return false;
218}
219
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000220bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name,
221 bool DebugLogging) {
222 // Manually handle aliases for pre-configured pipeline fragments.
223 if (Name.startswith("default") || Name.startswith("lto")) {
224 SmallVector<StringRef, 3> Matches;
225 if (!DefaultAliasRegex.match(Name, &Matches))
226 return false;
227 assert(Matches.size() == 3 && "Must capture two matched strings!");
228
229 auto L = StringSwitch<OptimizationLevel>(Matches[2])
230 .Case("O0", O0)
231 .Case("O1", O1)
232 .Case("O2", O2)
233 .Case("O3", O3)
234 .Case("Os", Os)
235 .Case("Oz", Oz);
236
237 if (Matches[1] == "default") {
238 addPerModuleDefaultPipeline(MPM, L, DebugLogging);
239 } else if (Matches[1] == "lto-pre-link") {
240 addLTOPreLinkDefaultPipeline(MPM, L, DebugLogging);
241 } else {
242 assert(Matches[1] == "lto" && "Not one of the matched options!");
243 addLTODefaultPipeline(MPM, L, DebugLogging);
244 }
245 return true;
246 }
247
Chandler Carruth58944182014-04-21 08:08:50 +0000248#define MODULE_PASS(NAME, CREATE_PASS) \
249 if (Name == NAME) { \
250 MPM.addPass(CREATE_PASS); \
251 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000252 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000253#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
254 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000255 MPM.addPass(RequireAnalysisPass< \
256 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000257 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000258 } \
259 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000260 MPM.addPass(InvalidateAnalysisPass< \
261 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000262 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000263 }
264#include "PassRegistry.def"
265
Chandler Carruth66445382014-01-11 08:16:35 +0000266 return false;
267}
268
Chandler Carruth1ff77242015-03-07 09:02:36 +0000269bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000270#define CGSCC_PASS(NAME, CREATE_PASS) \
271 if (Name == NAME) { \
272 CGPM.addPass(CREATE_PASS); \
273 return true; \
274 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000275#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
276 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000277 CGPM.addPass(RequireAnalysisPass< \
278 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000279 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000280 } \
281 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000282 CGPM.addPass(InvalidateAnalysisPass< \
283 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000284 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000285 }
286#include "PassRegistry.def"
287
Chandler Carruth572e3402014-04-21 11:12:00 +0000288 return false;
289}
290
Chandler Carruth1ff77242015-03-07 09:02:36 +0000291bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
292 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000293#define FUNCTION_PASS(NAME, CREATE_PASS) \
294 if (Name == NAME) { \
295 FPM.addPass(CREATE_PASS); \
296 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000297 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000298#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
299 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000300 FPM.addPass(RequireAnalysisPass< \
301 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000302 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000303 } \
304 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000305 FPM.addPass(InvalidateAnalysisPass< \
306 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000307 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000308 }
309#include "PassRegistry.def"
310
Chandler Carruthd8330982014-01-12 09:34:22 +0000311 return false;
312}
313
Justin Bognereecc3c82016-02-25 07:23:08 +0000314bool PassBuilder::parseLoopPassName(LoopPassManager &FPM,
315 StringRef Name) {
316#define LOOP_PASS(NAME, CREATE_PASS) \
317 if (Name == NAME) { \
318 FPM.addPass(CREATE_PASS); \
319 return true; \
320 }
321#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
322 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000323 FPM.addPass(RequireAnalysisPass< \
324 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000325 return true; \
326 } \
327 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000328 FPM.addPass(InvalidateAnalysisPass< \
329 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000330 return true; \
331 }
332#include "PassRegistry.def"
333
334 return false;
335}
336
Chandler Carruthedf59962016-02-18 09:45:17 +0000337bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
338#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
339 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000340 AA.registerFunctionAnalysis< \
341 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +0000342 return true; \
343 }
344#include "PassRegistry.def"
345
346 return false;
347}
348
Justin Bognereecc3c82016-02-25 07:23:08 +0000349bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
350 StringRef &PipelineText,
351 bool VerifyEachPass,
352 bool DebugLogging) {
353 for (;;) {
354 // Parse nested pass managers by recursing.
355 if (PipelineText.startswith("loop(")) {
356 LoopPassManager NestedLPM(DebugLogging);
357
358 // Parse the inner pipeline inte the nested manager.
359 PipelineText = PipelineText.substr(strlen("loop("));
360 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
361 DebugLogging) ||
362 PipelineText.empty())
363 return false;
364 assert(PipelineText[0] == ')');
365 PipelineText = PipelineText.substr(1);
366
367 // Add the nested pass manager with the appropriate adaptor.
368 LPM.addPass(std::move(NestedLPM));
369 } else {
370 // Otherwise try to parse a pass name.
371 size_t End = PipelineText.find_first_of(",)");
372 if (!parseLoopPassName(LPM, PipelineText.substr(0, End)))
373 return false;
374 // TODO: Ideally, we would run a LoopVerifierPass() here in the
375 // VerifyEachPass case, but we don't have such a verifier yet.
376
377 PipelineText = PipelineText.substr(End);
378 }
379
380 if (PipelineText.empty() || PipelineText[0] == ')')
381 return true;
382
383 assert(PipelineText[0] == ',');
384 PipelineText = PipelineText.substr(1);
385 }
386}
387
Chandler Carruth1ff77242015-03-07 09:02:36 +0000388bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
389 StringRef &PipelineText,
390 bool VerifyEachPass,
391 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000392 for (;;) {
393 // Parse nested pass managers by recursing.
394 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000395 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000396
397 // Parse the inner pipeline inte the nested manager.
398 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000399 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
400 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000401 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000402 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000403 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000404 PipelineText = PipelineText.substr(1);
405
406 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000407 FPM.addPass(std::move(NestedFPM));
Justin Bognereecc3c82016-02-25 07:23:08 +0000408 } else if (PipelineText.startswith("loop(")) {
409 LoopPassManager NestedLPM(DebugLogging);
410
411 // Parse the inner pipeline inte the nested manager.
412 PipelineText = PipelineText.substr(strlen("loop("));
413 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
414 DebugLogging) ||
415 PipelineText.empty())
416 return false;
417 assert(PipelineText[0] == ')');
418 PipelineText = PipelineText.substr(1);
419
420 // Add the nested pass manager with the appropriate adaptor.
421 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(NestedLPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000422 } else {
423 // Otherwise try to parse a pass name.
424 size_t End = PipelineText.find_first_of(",)");
425 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
426 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000427 if (VerifyEachPass)
428 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000429
430 PipelineText = PipelineText.substr(End);
431 }
432
433 if (PipelineText.empty() || PipelineText[0] == ')')
434 return true;
435
436 assert(PipelineText[0] == ',');
437 PipelineText = PipelineText.substr(1);
438 }
439}
440
Chandler Carruth1ff77242015-03-07 09:02:36 +0000441bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
442 StringRef &PipelineText,
443 bool VerifyEachPass,
444 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000445 for (;;) {
446 // Parse nested pass managers by recursing.
447 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000448 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000449
450 // Parse the inner pipeline into the nested manager.
451 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000452 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
453 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000454 PipelineText.empty())
455 return false;
456 assert(PipelineText[0] == ')');
457 PipelineText = PipelineText.substr(1);
458
459 // Add the nested pass manager with the appropriate adaptor.
460 CGPM.addPass(std::move(NestedCGPM));
461 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000462 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000463
464 // Parse the inner pipeline inte the nested manager.
465 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000466 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
467 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000468 PipelineText.empty())
469 return false;
470 assert(PipelineText[0] == ')');
471 PipelineText = PipelineText.substr(1);
472
473 // Add the nested pass manager with the appropriate adaptor.
474 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
475 } else {
476 // Otherwise try to parse a pass name.
477 size_t End = PipelineText.find_first_of(",)");
478 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
479 return false;
480 // FIXME: No verifier support for CGSCC passes!
481
482 PipelineText = PipelineText.substr(End);
483 }
484
485 if (PipelineText.empty() || PipelineText[0] == ')')
486 return true;
487
488 assert(PipelineText[0] == ',');
489 PipelineText = PipelineText.substr(1);
490 }
491}
492
Chandler Carruth1ff77242015-03-07 09:02:36 +0000493bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
494 StringRef &PipelineText,
495 bool VerifyEachPass,
496 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000497 for (;;) {
498 // Parse nested pass managers by recursing.
499 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000500 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000501
502 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000503 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000504 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
505 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000506 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000507 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000508 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000509 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000510
511 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000512 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000513 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000514 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000515
516 // Parse the inner pipeline inte the nested manager.
517 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000518 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
519 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000520 PipelineText.empty())
521 return false;
522 assert(PipelineText[0] == ')');
523 PipelineText = PipelineText.substr(1);
524
525 // Add the nested pass manager with the appropriate adaptor.
526 MPM.addPass(
527 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000528 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000529 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000530
531 // Parse the inner pipeline inte the nested manager.
532 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000533 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
534 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000535 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000536 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000537 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000538 PipelineText = PipelineText.substr(1);
539
540 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000541 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000542 } else {
543 // Otherwise try to parse a pass name.
544 size_t End = PipelineText.find_first_of(",)");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000545 if (!parseModulePassName(MPM, PipelineText.substr(0, End), DebugLogging))
Chandler Carruth66445382014-01-11 08:16:35 +0000546 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000547 if (VerifyEachPass)
548 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000549
550 PipelineText = PipelineText.substr(End);
551 }
552
553 if (PipelineText.empty() || PipelineText[0] == ')')
554 return true;
555
556 assert(PipelineText[0] == ',');
557 PipelineText = PipelineText.substr(1);
558 }
559}
560
561// Primary pass pipeline description parsing routine.
562// FIXME: Should this routine accept a TargetMachine or require the caller to
563// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000564bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
565 StringRef PipelineText, bool VerifyEachPass,
566 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000567 // By default, try to parse the pipeline as-if it were within an implicit
568 // 'module(...)' pass pipeline. If this will parse at all, it needs to
569 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000570 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000571 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000572
Chandler Carruthea368f12015-01-06 08:37:58 +0000573 // This isn't parsable as a module pipeline, look for the end of a pass name
574 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000575 StringRef FirstName =
576 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000577 assert(!isModulePassName(FirstName) &&
578 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000579
Chandler Carruthea368f12015-01-06 08:37:58 +0000580 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
581 // pipeline.
Justin Bognereecc3c82016-02-25 07:23:08 +0000582 if (PipelineText.startswith("cgscc(") || isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000583 CGSCCPassManager CGPM(DebugLogging);
584 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
585 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000586 !PipelineText.empty())
587 return false;
588 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
589 return true;
590 }
591
Chandler Carruthea368f12015-01-06 08:37:58 +0000592 // Similarly, if this looks like a Function pass, parse the whole thing as
593 // a Function pipelien.
Justin Bognereecc3c82016-02-25 07:23:08 +0000594 if (PipelineText.startswith("function(") || isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000595 FunctionPassManager FPM(DebugLogging);
596 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
597 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000598 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000599 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000600 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000601 return true;
602 }
Chandler Carruth66445382014-01-11 08:16:35 +0000603
Justin Bognereecc3c82016-02-25 07:23:08 +0000604 // If this looks like a Loop pass, parse the whole thing as a Loop pipeline.
605 if (PipelineText.startswith("loop(") || isLoopPassName(FirstName)) {
606 LoopPassManager LPM(DebugLogging);
607 if (!parseLoopPassPipeline(LPM, PipelineText, VerifyEachPass,
608 DebugLogging) ||
609 !PipelineText.empty())
610 return false;
611 FunctionPassManager FPM(DebugLogging);
612 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
613 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
614 return true;
615 }
616
617
Chandler Carruth66445382014-01-11 08:16:35 +0000618 return false;
619}
Chandler Carruthedf59962016-02-18 09:45:17 +0000620
621bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
622 while (!PipelineText.empty()) {
623 StringRef Name;
624 std::tie(Name, PipelineText) = PipelineText.split(',');
625 if (!parseAAPassName(AA, Name))
626 return false;
627 }
628
629 return true;
630}