blob: 7f55353ab5030945315e0d89256ed6663785f9bd [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"
Chandler Carruth4c660f72016-03-10 11:24:11 +000026#include "llvm/Analysis/CallGraph.h"
Hongbin Zheng751337f2016-02-25 17:54:15 +000027#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000028#include "llvm/Analysis/LazyCallGraph.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000029#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth61440d22016-03-10 00:55:30 +000030#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000031#include "llvm/Analysis/PostDominators.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000032#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000033#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000034#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000035#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000036#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000037#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000038#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000039#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000040#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000041#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000042#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000043#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000044#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000045#include "llvm/Target/TargetMachine.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000046#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000047#include "llvm/Transforms/IPO/FunctionAttrs.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000048#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Justin Bogner21e15372015-10-30 23:28:12 +000049#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000050#include "llvm/Transforms/InstCombine/InstCombine.h"
Justin Bogner19b67992015-10-30 23:13:18 +000051#include "llvm/Transforms/Scalar/ADCE.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000052#include "llvm/Transforms/Scalar/EarlyCSE.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000053#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000054#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000055#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Chandler Carruth58dde8c2016-02-26 12:17:54 +000056#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +000057
58using namespace llvm;
59
Chandler Carruth8b5a74192016-02-28 22:16:03 +000060static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
61
Chandler Carruth66445382014-01-11 08:16:35 +000062namespace {
63
Chandler Carruthd8330982014-01-12 09:34:22 +000064/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000065struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000066 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000067 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000068};
69
Chandler Carruth0b576b32015-01-06 02:50:06 +000070/// \brief No-op module analysis.
Chandler Carruth3a634352016-02-26 11:44:45 +000071struct NoOpModuleAnalysis : AnalysisBase<NoOpModuleAnalysis> {
Chandler Carruth0b576b32015-01-06 02:50:06 +000072 struct Result {};
73 Result run(Module &) { return Result(); }
74 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +000075};
76
Chandler Carruth572e3402014-04-21 11:12:00 +000077/// \brief No-op CGSCC pass which does nothing.
78struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000079 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +000080 return PreservedAnalyses::all();
81 }
82 static StringRef name() { return "NoOpCGSCCPass"; }
83};
84
Chandler Carruth0b576b32015-01-06 02:50:06 +000085/// \brief No-op CGSCC analysis.
Chandler Carruth3a634352016-02-26 11:44:45 +000086struct NoOpCGSCCAnalysis : AnalysisBase<NoOpCGSCCAnalysis> {
Chandler Carruth0b576b32015-01-06 02:50:06 +000087 struct Result {};
88 Result run(LazyCallGraph::SCC &) { return Result(); }
89 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +000090};
91
Chandler Carruthd8330982014-01-12 09:34:22 +000092/// \brief No-op function pass which does nothing.
93struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000094 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +000095 static StringRef name() { return "NoOpFunctionPass"; }
96};
97
Chandler Carruth0b576b32015-01-06 02:50:06 +000098/// \brief No-op function analysis.
Chandler Carruth3a634352016-02-26 11:44:45 +000099struct NoOpFunctionAnalysis : AnalysisBase<NoOpFunctionAnalysis> {
Chandler Carruth0b576b32015-01-06 02:50:06 +0000100 struct Result {};
101 Result run(Function &) { return Result(); }
102 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000103};
104
Justin Bognereecc3c82016-02-25 07:23:08 +0000105/// \brief No-op loop pass which does nothing.
106struct NoOpLoopPass {
107 PreservedAnalyses run(Loop &L) { return PreservedAnalyses::all(); }
108 static StringRef name() { return "NoOpLoopPass"; }
109};
110
111/// \brief No-op loop analysis.
Chandler Carruth3a634352016-02-26 11:44:45 +0000112struct NoOpLoopAnalysis : AnalysisBase<NoOpLoopAnalysis> {
Justin Bognereecc3c82016-02-25 07:23:08 +0000113 struct Result {};
114 Result run(Loop &) { return Result(); }
115 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000116};
117
Chandler Carruth66445382014-01-11 08:16:35 +0000118} // End anonymous namespace.
119
Chandler Carruth1ff77242015-03-07 09:02:36 +0000120void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000121#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000122 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000123#include "PassRegistry.def"
124}
125
Chandler Carruth1ff77242015-03-07 09:02:36 +0000126void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000127#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000128 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000129#include "PassRegistry.def"
130}
131
Chandler Carruth1ff77242015-03-07 09:02:36 +0000132void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000133#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000134 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000135#include "PassRegistry.def"
136}
137
Justin Bognereecc3c82016-02-25 07:23:08 +0000138void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
139#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
140 LAM.registerPass([&] { return CREATE_PASS; });
141#include "PassRegistry.def"
142}
143
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000144void PassBuilder::addPerModuleDefaultPipeline(ModulePassManager &MPM,
145 OptimizationLevel Level,
146 bool DebugLogging) {
147 // FIXME: Finish fleshing this out to match the legacy pipelines.
148 FunctionPassManager EarlyFPM(DebugLogging);
149 EarlyFPM.addPass(SimplifyCFGPass());
150 EarlyFPM.addPass(SROA());
151 EarlyFPM.addPass(EarlyCSEPass());
152 EarlyFPM.addPass(LowerExpectIntrinsicPass());
153
154 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
155}
156
157void PassBuilder::addLTOPreLinkDefaultPipeline(ModulePassManager &MPM,
158 OptimizationLevel Level,
159 bool DebugLogging) {
160 // FIXME: We should use a customized pre-link pipeline!
161 addPerModuleDefaultPipeline(MPM, Level, DebugLogging);
162}
163
164void PassBuilder::addLTODefaultPipeline(ModulePassManager &MPM,
165 OptimizationLevel Level,
166 bool DebugLogging) {
167 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
168 FunctionPassManager LateFPM(DebugLogging);
169 LateFPM.addPass(InstCombinePass());
170 LateFPM.addPass(SimplifyCFGPass());
171
172 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
173}
174
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000175#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000176static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000177 // Manually handle aliases for pre-configured pipeline fragments.
178 if (Name.startswith("default") || Name.startswith("lto"))
179 return DefaultAliasRegex.match(Name);
180
Chandler Carruth58944182014-04-21 08:08:50 +0000181#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000182#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000183 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000184 return true;
185#include "PassRegistry.def"
186
Chandler Carruth66445382014-01-11 08:16:35 +0000187 return false;
188}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000189#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000190
Chandler Carruth572e3402014-04-21 11:12:00 +0000191static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000192#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000193#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000194 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000195 return true;
196#include "PassRegistry.def"
197
Chandler Carruth572e3402014-04-21 11:12:00 +0000198 return false;
199}
200
Chandler Carruthd8330982014-01-12 09:34:22 +0000201static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000202#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000203#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000204 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000205 return true;
206#include "PassRegistry.def"
207
Chandler Carruthd8330982014-01-12 09:34:22 +0000208 return false;
209}
210
Justin Bognereecc3c82016-02-25 07:23:08 +0000211static bool isLoopPassName(StringRef Name) {
212#define LOOP_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
213#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
214 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
215 return true;
216#include "PassRegistry.def"
217
218 return false;
219}
220
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000221bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name,
222 bool DebugLogging) {
223 // Manually handle aliases for pre-configured pipeline fragments.
224 if (Name.startswith("default") || Name.startswith("lto")) {
225 SmallVector<StringRef, 3> Matches;
226 if (!DefaultAliasRegex.match(Name, &Matches))
227 return false;
228 assert(Matches.size() == 3 && "Must capture two matched strings!");
229
230 auto L = StringSwitch<OptimizationLevel>(Matches[2])
231 .Case("O0", O0)
232 .Case("O1", O1)
233 .Case("O2", O2)
234 .Case("O3", O3)
235 .Case("Os", Os)
236 .Case("Oz", Oz);
237
238 if (Matches[1] == "default") {
239 addPerModuleDefaultPipeline(MPM, L, DebugLogging);
240 } else if (Matches[1] == "lto-pre-link") {
241 addLTOPreLinkDefaultPipeline(MPM, L, DebugLogging);
242 } else {
243 assert(Matches[1] == "lto" && "Not one of the matched options!");
244 addLTODefaultPipeline(MPM, L, DebugLogging);
245 }
246 return true;
247 }
248
Chandler Carruth58944182014-04-21 08:08:50 +0000249#define MODULE_PASS(NAME, CREATE_PASS) \
250 if (Name == NAME) { \
251 MPM.addPass(CREATE_PASS); \
252 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000253 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000254#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
255 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000256 MPM.addPass(RequireAnalysisPass< \
257 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000258 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000259 } \
260 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000261 MPM.addPass(InvalidateAnalysisPass< \
262 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000263 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000264 }
265#include "PassRegistry.def"
266
Chandler Carruth66445382014-01-11 08:16:35 +0000267 return false;
268}
269
Chandler Carruth1ff77242015-03-07 09:02:36 +0000270bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000271#define CGSCC_PASS(NAME, CREATE_PASS) \
272 if (Name == NAME) { \
273 CGPM.addPass(CREATE_PASS); \
274 return true; \
275 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000276#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
277 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000278 CGPM.addPass(RequireAnalysisPass< \
279 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000280 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000281 } \
282 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000283 CGPM.addPass(InvalidateAnalysisPass< \
284 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000285 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000286 }
287#include "PassRegistry.def"
288
Chandler Carruth572e3402014-04-21 11:12:00 +0000289 return false;
290}
291
Chandler Carruth1ff77242015-03-07 09:02:36 +0000292bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
293 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000294#define FUNCTION_PASS(NAME, CREATE_PASS) \
295 if (Name == NAME) { \
296 FPM.addPass(CREATE_PASS); \
297 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000298 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000299#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
300 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000301 FPM.addPass(RequireAnalysisPass< \
302 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000303 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000304 } \
305 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000306 FPM.addPass(InvalidateAnalysisPass< \
307 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000308 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000309 }
310#include "PassRegistry.def"
311
Chandler Carruthd8330982014-01-12 09:34:22 +0000312 return false;
313}
314
Justin Bognereecc3c82016-02-25 07:23:08 +0000315bool PassBuilder::parseLoopPassName(LoopPassManager &FPM,
316 StringRef Name) {
317#define LOOP_PASS(NAME, CREATE_PASS) \
318 if (Name == NAME) { \
319 FPM.addPass(CREATE_PASS); \
320 return true; \
321 }
322#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
323 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000324 FPM.addPass(RequireAnalysisPass< \
325 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000326 return true; \
327 } \
328 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000329 FPM.addPass(InvalidateAnalysisPass< \
330 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000331 return true; \
332 }
333#include "PassRegistry.def"
334
335 return false;
336}
337
Chandler Carruthedf59962016-02-18 09:45:17 +0000338bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
339#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
340 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000341 AA.registerFunctionAnalysis< \
342 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +0000343 return true; \
344 }
345#include "PassRegistry.def"
346
347 return false;
348}
349
Justin Bognereecc3c82016-02-25 07:23:08 +0000350bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
351 StringRef &PipelineText,
352 bool VerifyEachPass,
353 bool DebugLogging) {
354 for (;;) {
355 // Parse nested pass managers by recursing.
356 if (PipelineText.startswith("loop(")) {
357 LoopPassManager NestedLPM(DebugLogging);
358
359 // Parse the inner pipeline inte the nested manager.
360 PipelineText = PipelineText.substr(strlen("loop("));
361 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
362 DebugLogging) ||
363 PipelineText.empty())
364 return false;
365 assert(PipelineText[0] == ')');
366 PipelineText = PipelineText.substr(1);
367
368 // Add the nested pass manager with the appropriate adaptor.
369 LPM.addPass(std::move(NestedLPM));
370 } else {
371 // Otherwise try to parse a pass name.
372 size_t End = PipelineText.find_first_of(",)");
373 if (!parseLoopPassName(LPM, PipelineText.substr(0, End)))
374 return false;
375 // TODO: Ideally, we would run a LoopVerifierPass() here in the
376 // VerifyEachPass case, but we don't have such a verifier yet.
377
378 PipelineText = PipelineText.substr(End);
379 }
380
381 if (PipelineText.empty() || PipelineText[0] == ')')
382 return true;
383
384 assert(PipelineText[0] == ',');
385 PipelineText = PipelineText.substr(1);
386 }
387}
388
Chandler Carruth1ff77242015-03-07 09:02:36 +0000389bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
390 StringRef &PipelineText,
391 bool VerifyEachPass,
392 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000393 for (;;) {
394 // Parse nested pass managers by recursing.
395 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000396 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000397
398 // Parse the inner pipeline inte the nested manager.
399 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000400 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
401 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000402 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000403 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000404 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000405 PipelineText = PipelineText.substr(1);
406
407 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000408 FPM.addPass(std::move(NestedFPM));
Justin Bognereecc3c82016-02-25 07:23:08 +0000409 } else if (PipelineText.startswith("loop(")) {
410 LoopPassManager NestedLPM(DebugLogging);
411
412 // Parse the inner pipeline inte the nested manager.
413 PipelineText = PipelineText.substr(strlen("loop("));
414 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
415 DebugLogging) ||
416 PipelineText.empty())
417 return false;
418 assert(PipelineText[0] == ')');
419 PipelineText = PipelineText.substr(1);
420
421 // Add the nested pass manager with the appropriate adaptor.
422 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(NestedLPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000423 } else {
424 // Otherwise try to parse a pass name.
425 size_t End = PipelineText.find_first_of(",)");
426 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
427 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000428 if (VerifyEachPass)
429 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000430
431 PipelineText = PipelineText.substr(End);
432 }
433
434 if (PipelineText.empty() || PipelineText[0] == ')')
435 return true;
436
437 assert(PipelineText[0] == ',');
438 PipelineText = PipelineText.substr(1);
439 }
440}
441
Chandler Carruth1ff77242015-03-07 09:02:36 +0000442bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
443 StringRef &PipelineText,
444 bool VerifyEachPass,
445 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000446 for (;;) {
447 // Parse nested pass managers by recursing.
448 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000449 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000450
451 // Parse the inner pipeline into the nested manager.
452 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000453 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
454 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000455 PipelineText.empty())
456 return false;
457 assert(PipelineText[0] == ')');
458 PipelineText = PipelineText.substr(1);
459
460 // Add the nested pass manager with the appropriate adaptor.
461 CGPM.addPass(std::move(NestedCGPM));
462 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000463 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000464
465 // Parse the inner pipeline inte the nested manager.
466 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000467 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
468 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000469 PipelineText.empty())
470 return false;
471 assert(PipelineText[0] == ')');
472 PipelineText = PipelineText.substr(1);
473
474 // Add the nested pass manager with the appropriate adaptor.
475 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
476 } else {
477 // Otherwise try to parse a pass name.
478 size_t End = PipelineText.find_first_of(",)");
479 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
480 return false;
481 // FIXME: No verifier support for CGSCC passes!
482
483 PipelineText = PipelineText.substr(End);
484 }
485
486 if (PipelineText.empty() || PipelineText[0] == ')')
487 return true;
488
489 assert(PipelineText[0] == ',');
490 PipelineText = PipelineText.substr(1);
491 }
492}
493
Chandler Carruth1ff77242015-03-07 09:02:36 +0000494bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
495 StringRef &PipelineText,
496 bool VerifyEachPass,
497 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000498 for (;;) {
499 // Parse nested pass managers by recursing.
500 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000501 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000502
503 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000504 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000505 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
506 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000507 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000508 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000509 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000510 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000511
512 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000513 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000514 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000515 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000516
517 // Parse the inner pipeline inte the nested manager.
518 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000519 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
520 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000521 PipelineText.empty())
522 return false;
523 assert(PipelineText[0] == ')');
524 PipelineText = PipelineText.substr(1);
525
526 // Add the nested pass manager with the appropriate adaptor.
527 MPM.addPass(
528 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000529 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000530 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000531
532 // Parse the inner pipeline inte the nested manager.
533 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000534 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
535 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000536 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000537 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000538 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000539 PipelineText = PipelineText.substr(1);
540
541 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000542 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000543 } else {
544 // Otherwise try to parse a pass name.
545 size_t End = PipelineText.find_first_of(",)");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000546 if (!parseModulePassName(MPM, PipelineText.substr(0, End), DebugLogging))
Chandler Carruth66445382014-01-11 08:16:35 +0000547 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000548 if (VerifyEachPass)
549 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000550
551 PipelineText = PipelineText.substr(End);
552 }
553
554 if (PipelineText.empty() || PipelineText[0] == ')')
555 return true;
556
557 assert(PipelineText[0] == ',');
558 PipelineText = PipelineText.substr(1);
559 }
560}
561
562// Primary pass pipeline description parsing routine.
563// FIXME: Should this routine accept a TargetMachine or require the caller to
564// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000565bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
566 StringRef PipelineText, bool VerifyEachPass,
567 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000568 // By default, try to parse the pipeline as-if it were within an implicit
569 // 'module(...)' pass pipeline. If this will parse at all, it needs to
570 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000571 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000572 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000573
Chandler Carruthea368f12015-01-06 08:37:58 +0000574 // This isn't parsable as a module pipeline, look for the end of a pass name
575 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000576 StringRef FirstName =
577 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000578 assert(!isModulePassName(FirstName) &&
579 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000580
Chandler Carruthea368f12015-01-06 08:37:58 +0000581 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
582 // pipeline.
Justin Bognereecc3c82016-02-25 07:23:08 +0000583 if (PipelineText.startswith("cgscc(") || isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000584 CGSCCPassManager CGPM(DebugLogging);
585 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
586 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000587 !PipelineText.empty())
588 return false;
589 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
590 return true;
591 }
592
Chandler Carruthea368f12015-01-06 08:37:58 +0000593 // Similarly, if this looks like a Function pass, parse the whole thing as
594 // a Function pipelien.
Justin Bognereecc3c82016-02-25 07:23:08 +0000595 if (PipelineText.startswith("function(") || isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000596 FunctionPassManager FPM(DebugLogging);
597 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
598 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000599 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000600 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000601 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000602 return true;
603 }
Chandler Carruth66445382014-01-11 08:16:35 +0000604
Justin Bognereecc3c82016-02-25 07:23:08 +0000605 // If this looks like a Loop pass, parse the whole thing as a Loop pipeline.
606 if (PipelineText.startswith("loop(") || isLoopPassName(FirstName)) {
607 LoopPassManager LPM(DebugLogging);
608 if (!parseLoopPassPipeline(LPM, PipelineText, VerifyEachPass,
609 DebugLogging) ||
610 !PipelineText.empty())
611 return false;
612 FunctionPassManager FPM(DebugLogging);
613 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
614 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
615 return true;
616 }
617
618
Chandler Carruth66445382014-01-11 08:16:35 +0000619 return false;
620}
Chandler Carruthedf59962016-02-18 09:45:17 +0000621
622bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
623 while (!PipelineText.empty()) {
624 StringRef Name;
625 std::tie(Name, PipelineText) = PipelineText.split(',');
626 if (!parseAAPassName(AA, Name))
627 return false;
628 }
629
630 return true;
631}