blob: adbd61b0d7a21dd02553c6734d2a66c66fa11823 [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 Carruth45a9c202016-03-11 09:15:11 +000028#include "llvm/Analysis/GlobalsModRef.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000029#include "llvm/Analysis/LazyCallGraph.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000030#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth61440d22016-03-10 00:55:30 +000031#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000032#include "llvm/Analysis/PostDominators.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000033#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000034#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000035#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000036#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000037#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000038#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000039#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000040#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000041#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000042#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000043#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000044#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000045#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000046#include "llvm/Target/TargetMachine.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000047#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000048#include "llvm/Transforms/IPO/FunctionAttrs.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000049#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Justin Bogner21e15372015-10-30 23:28:12 +000050#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000051#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000052#include "llvm/Transforms/InstrProfiling.h"
Justin Bogner19b67992015-10-30 23:13:18 +000053#include "llvm/Transforms/Scalar/ADCE.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000054#include "llvm/Transforms/Scalar/EarlyCSE.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000055#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Chandler Carruth89c45a12016-03-11 08:50:55 +000056#include "llvm/Transforms/Scalar/GVN.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000057#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000058#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Chandler Carruth58dde8c2016-02-26 12:17:54 +000059#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +000060
61using namespace llvm;
62
Chandler Carruth8b5a74192016-02-28 22:16:03 +000063static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
64
Chandler Carruth66445382014-01-11 08:16:35 +000065namespace {
66
Chandler Carruthd8330982014-01-12 09:34:22 +000067/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000068struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000069 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000070 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000071};
72
Chandler Carruth0b576b32015-01-06 02:50:06 +000073/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +000074class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
75 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +000076 static char PassID;
77
78public:
Chandler Carruth0b576b32015-01-06 02:50:06 +000079 struct Result {};
80 Result run(Module &) { return Result(); }
81 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +000082};
83
Chandler Carruth572e3402014-04-21 11:12:00 +000084/// \brief No-op CGSCC pass which does nothing.
85struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000086 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +000087 return PreservedAnalyses::all();
88 }
89 static StringRef name() { return "NoOpCGSCCPass"; }
90};
91
Chandler Carruth0b576b32015-01-06 02:50:06 +000092/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +000093class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
94 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +000095 static char PassID;
96
97public:
Chandler Carruth0b576b32015-01-06 02:50:06 +000098 struct Result {};
99 Result run(LazyCallGraph::SCC &) { return Result(); }
100 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000101};
102
Chandler Carruthd8330982014-01-12 09:34:22 +0000103/// \brief No-op function pass which does nothing.
104struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000105 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +0000106 static StringRef name() { return "NoOpFunctionPass"; }
107};
108
Chandler Carruth0b576b32015-01-06 02:50:06 +0000109/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000110class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
111 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000112 static char PassID;
113
114public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000115 struct Result {};
116 Result run(Function &) { return Result(); }
117 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000118};
119
Justin Bognereecc3c82016-02-25 07:23:08 +0000120/// \brief No-op loop pass which does nothing.
121struct NoOpLoopPass {
122 PreservedAnalyses run(Loop &L) { return PreservedAnalyses::all(); }
123 static StringRef name() { return "NoOpLoopPass"; }
124};
125
126/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000127class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
128 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000129 static char PassID;
130
131public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000132 struct Result {};
133 Result run(Loop &) { return Result(); }
134 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000135};
136
Chandler Carruthb4faf132016-03-11 10:22:49 +0000137char NoOpModuleAnalysis::PassID;
138char NoOpCGSCCAnalysis::PassID;
139char NoOpFunctionAnalysis::PassID;
140char NoOpLoopAnalysis::PassID;
141
Chandler Carruth66445382014-01-11 08:16:35 +0000142} // End anonymous namespace.
143
Chandler Carruth1ff77242015-03-07 09:02:36 +0000144void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000145#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000146 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000147#include "PassRegistry.def"
148}
149
Chandler Carruth1ff77242015-03-07 09:02:36 +0000150void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000151#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000152 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000153#include "PassRegistry.def"
154}
155
Chandler Carruth1ff77242015-03-07 09:02:36 +0000156void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000157#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000158 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000159#include "PassRegistry.def"
160}
161
Justin Bognereecc3c82016-02-25 07:23:08 +0000162void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
163#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
164 LAM.registerPass([&] { return CREATE_PASS; });
165#include "PassRegistry.def"
166}
167
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000168void PassBuilder::addPerModuleDefaultPipeline(ModulePassManager &MPM,
169 OptimizationLevel Level,
170 bool DebugLogging) {
171 // FIXME: Finish fleshing this out to match the legacy pipelines.
172 FunctionPassManager EarlyFPM(DebugLogging);
173 EarlyFPM.addPass(SimplifyCFGPass());
174 EarlyFPM.addPass(SROA());
175 EarlyFPM.addPass(EarlyCSEPass());
176 EarlyFPM.addPass(LowerExpectIntrinsicPass());
177
178 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
179}
180
181void PassBuilder::addLTOPreLinkDefaultPipeline(ModulePassManager &MPM,
182 OptimizationLevel Level,
183 bool DebugLogging) {
184 // FIXME: We should use a customized pre-link pipeline!
185 addPerModuleDefaultPipeline(MPM, Level, DebugLogging);
186}
187
188void PassBuilder::addLTODefaultPipeline(ModulePassManager &MPM,
189 OptimizationLevel Level,
190 bool DebugLogging) {
191 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
192 FunctionPassManager LateFPM(DebugLogging);
193 LateFPM.addPass(InstCombinePass());
194 LateFPM.addPass(SimplifyCFGPass());
195
196 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
197}
198
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000199#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000200static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000201 // Manually handle aliases for pre-configured pipeline fragments.
202 if (Name.startswith("default") || Name.startswith("lto"))
203 return DefaultAliasRegex.match(Name);
204
Chandler Carruth58944182014-04-21 08:08:50 +0000205#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000206#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000207 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000208 return true;
209#include "PassRegistry.def"
210
Chandler Carruth66445382014-01-11 08:16:35 +0000211 return false;
212}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000213#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000214
Chandler Carruth572e3402014-04-21 11:12:00 +0000215static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000216#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000217#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000218 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000219 return true;
220#include "PassRegistry.def"
221
Chandler Carruth572e3402014-04-21 11:12:00 +0000222 return false;
223}
224
Chandler Carruthd8330982014-01-12 09:34:22 +0000225static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000226#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000227#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000228 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000229 return true;
230#include "PassRegistry.def"
231
Chandler Carruthd8330982014-01-12 09:34:22 +0000232 return false;
233}
234
Justin Bognereecc3c82016-02-25 07:23:08 +0000235static bool isLoopPassName(StringRef Name) {
236#define LOOP_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
237#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
238 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
239 return true;
240#include "PassRegistry.def"
241
242 return false;
243}
244
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000245bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name,
246 bool DebugLogging) {
247 // Manually handle aliases for pre-configured pipeline fragments.
248 if (Name.startswith("default") || Name.startswith("lto")) {
249 SmallVector<StringRef, 3> Matches;
250 if (!DefaultAliasRegex.match(Name, &Matches))
251 return false;
252 assert(Matches.size() == 3 && "Must capture two matched strings!");
253
254 auto L = StringSwitch<OptimizationLevel>(Matches[2])
255 .Case("O0", O0)
256 .Case("O1", O1)
257 .Case("O2", O2)
258 .Case("O3", O3)
259 .Case("Os", Os)
260 .Case("Oz", Oz);
261
262 if (Matches[1] == "default") {
263 addPerModuleDefaultPipeline(MPM, L, DebugLogging);
264 } else if (Matches[1] == "lto-pre-link") {
265 addLTOPreLinkDefaultPipeline(MPM, L, DebugLogging);
266 } else {
267 assert(Matches[1] == "lto" && "Not one of the matched options!");
268 addLTODefaultPipeline(MPM, L, DebugLogging);
269 }
270 return true;
271 }
272
Chandler Carruth58944182014-04-21 08:08:50 +0000273#define MODULE_PASS(NAME, CREATE_PASS) \
274 if (Name == NAME) { \
275 MPM.addPass(CREATE_PASS); \
276 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000277 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000278#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
279 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000280 MPM.addPass(RequireAnalysisPass< \
281 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000282 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000283 } \
284 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000285 MPM.addPass(InvalidateAnalysisPass< \
286 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000287 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000288 }
289#include "PassRegistry.def"
290
Chandler Carruth66445382014-01-11 08:16:35 +0000291 return false;
292}
293
Chandler Carruth1ff77242015-03-07 09:02:36 +0000294bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000295#define CGSCC_PASS(NAME, CREATE_PASS) \
296 if (Name == NAME) { \
297 CGPM.addPass(CREATE_PASS); \
298 return true; \
299 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000300#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
301 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000302 CGPM.addPass(RequireAnalysisPass< \
303 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000304 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000305 } \
306 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000307 CGPM.addPass(InvalidateAnalysisPass< \
308 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000309 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000310 }
311#include "PassRegistry.def"
312
Chandler Carruth572e3402014-04-21 11:12:00 +0000313 return false;
314}
315
Chandler Carruth1ff77242015-03-07 09:02:36 +0000316bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
317 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000318#define FUNCTION_PASS(NAME, CREATE_PASS) \
319 if (Name == NAME) { \
320 FPM.addPass(CREATE_PASS); \
321 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000322 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000323#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
324 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000325 FPM.addPass(RequireAnalysisPass< \
326 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000327 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000328 } \
329 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000330 FPM.addPass(InvalidateAnalysisPass< \
331 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000332 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000333 }
334#include "PassRegistry.def"
335
Chandler Carruthd8330982014-01-12 09:34:22 +0000336 return false;
337}
338
Justin Bognereecc3c82016-02-25 07:23:08 +0000339bool PassBuilder::parseLoopPassName(LoopPassManager &FPM,
340 StringRef Name) {
341#define LOOP_PASS(NAME, CREATE_PASS) \
342 if (Name == NAME) { \
343 FPM.addPass(CREATE_PASS); \
344 return true; \
345 }
346#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
347 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000348 FPM.addPass(RequireAnalysisPass< \
349 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000350 return true; \
351 } \
352 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000353 FPM.addPass(InvalidateAnalysisPass< \
354 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000355 return true; \
356 }
357#include "PassRegistry.def"
358
359 return false;
360}
361
Chandler Carruthedf59962016-02-18 09:45:17 +0000362bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +0000363#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
364 if (Name == NAME) { \
365 AA.registerModuleAnalysis< \
366 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
367 return true; \
368 }
Chandler Carruthedf59962016-02-18 09:45:17 +0000369#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
370 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000371 AA.registerFunctionAnalysis< \
372 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +0000373 return true; \
374 }
375#include "PassRegistry.def"
376
377 return false;
378}
379
Justin Bognereecc3c82016-02-25 07:23:08 +0000380bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
381 StringRef &PipelineText,
382 bool VerifyEachPass,
383 bool DebugLogging) {
384 for (;;) {
385 // Parse nested pass managers by recursing.
386 if (PipelineText.startswith("loop(")) {
387 LoopPassManager NestedLPM(DebugLogging);
388
389 // Parse the inner pipeline inte the nested manager.
390 PipelineText = PipelineText.substr(strlen("loop("));
391 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
392 DebugLogging) ||
393 PipelineText.empty())
394 return false;
395 assert(PipelineText[0] == ')');
396 PipelineText = PipelineText.substr(1);
397
398 // Add the nested pass manager with the appropriate adaptor.
399 LPM.addPass(std::move(NestedLPM));
400 } else {
401 // Otherwise try to parse a pass name.
402 size_t End = PipelineText.find_first_of(",)");
403 if (!parseLoopPassName(LPM, PipelineText.substr(0, End)))
404 return false;
405 // TODO: Ideally, we would run a LoopVerifierPass() here in the
406 // VerifyEachPass case, but we don't have such a verifier yet.
407
408 PipelineText = PipelineText.substr(End);
409 }
410
411 if (PipelineText.empty() || PipelineText[0] == ')')
412 return true;
413
414 assert(PipelineText[0] == ',');
415 PipelineText = PipelineText.substr(1);
416 }
417}
418
Chandler Carruth1ff77242015-03-07 09:02:36 +0000419bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
420 StringRef &PipelineText,
421 bool VerifyEachPass,
422 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000423 for (;;) {
424 // Parse nested pass managers by recursing.
425 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000426 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000427
428 // Parse the inner pipeline inte the nested manager.
429 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000430 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
431 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000432 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000433 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000434 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000435 PipelineText = PipelineText.substr(1);
436
437 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000438 FPM.addPass(std::move(NestedFPM));
Justin Bognereecc3c82016-02-25 07:23:08 +0000439 } else if (PipelineText.startswith("loop(")) {
440 LoopPassManager NestedLPM(DebugLogging);
441
442 // Parse the inner pipeline inte the nested manager.
443 PipelineText = PipelineText.substr(strlen("loop("));
444 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
445 DebugLogging) ||
446 PipelineText.empty())
447 return false;
448 assert(PipelineText[0] == ')');
449 PipelineText = PipelineText.substr(1);
450
451 // Add the nested pass manager with the appropriate adaptor.
452 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(NestedLPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000453 } else {
454 // Otherwise try to parse a pass name.
455 size_t End = PipelineText.find_first_of(",)");
456 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
457 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000458 if (VerifyEachPass)
459 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000460
461 PipelineText = PipelineText.substr(End);
462 }
463
464 if (PipelineText.empty() || PipelineText[0] == ')')
465 return true;
466
467 assert(PipelineText[0] == ',');
468 PipelineText = PipelineText.substr(1);
469 }
470}
471
Chandler Carruth1ff77242015-03-07 09:02:36 +0000472bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
473 StringRef &PipelineText,
474 bool VerifyEachPass,
475 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000476 for (;;) {
477 // Parse nested pass managers by recursing.
478 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000479 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000480
481 // Parse the inner pipeline into the nested manager.
482 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000483 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
484 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000485 PipelineText.empty())
486 return false;
487 assert(PipelineText[0] == ')');
488 PipelineText = PipelineText.substr(1);
489
490 // Add the nested pass manager with the appropriate adaptor.
491 CGPM.addPass(std::move(NestedCGPM));
492 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000493 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000494
495 // Parse the inner pipeline inte the nested manager.
496 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000497 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
498 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000499 PipelineText.empty())
500 return false;
501 assert(PipelineText[0] == ')');
502 PipelineText = PipelineText.substr(1);
503
504 // Add the nested pass manager with the appropriate adaptor.
505 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
506 } else {
507 // Otherwise try to parse a pass name.
508 size_t End = PipelineText.find_first_of(",)");
509 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
510 return false;
511 // FIXME: No verifier support for CGSCC passes!
512
513 PipelineText = PipelineText.substr(End);
514 }
515
516 if (PipelineText.empty() || PipelineText[0] == ')')
517 return true;
518
519 assert(PipelineText[0] == ',');
520 PipelineText = PipelineText.substr(1);
521 }
522}
523
Chandler Carruth1ff77242015-03-07 09:02:36 +0000524bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
525 StringRef &PipelineText,
526 bool VerifyEachPass,
527 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000528 for (;;) {
529 // Parse nested pass managers by recursing.
530 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000531 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000532
533 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000534 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000535 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
536 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000537 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000538 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000539 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000540 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000541
542 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000543 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000544 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000545 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000546
547 // Parse the inner pipeline inte the nested manager.
548 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000549 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
550 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000551 PipelineText.empty())
552 return false;
553 assert(PipelineText[0] == ')');
554 PipelineText = PipelineText.substr(1);
555
556 // Add the nested pass manager with the appropriate adaptor.
557 MPM.addPass(
558 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000559 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000560 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000561
562 // Parse the inner pipeline inte the nested manager.
563 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000564 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
565 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000566 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000567 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000568 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000569 PipelineText = PipelineText.substr(1);
570
571 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000572 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000573 } else {
574 // Otherwise try to parse a pass name.
575 size_t End = PipelineText.find_first_of(",)");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000576 if (!parseModulePassName(MPM, PipelineText.substr(0, End), DebugLogging))
Chandler Carruth66445382014-01-11 08:16:35 +0000577 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000578 if (VerifyEachPass)
579 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000580
581 PipelineText = PipelineText.substr(End);
582 }
583
584 if (PipelineText.empty() || PipelineText[0] == ')')
585 return true;
586
587 assert(PipelineText[0] == ',');
588 PipelineText = PipelineText.substr(1);
589 }
590}
591
592// Primary pass pipeline description parsing routine.
593// FIXME: Should this routine accept a TargetMachine or require the caller to
594// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000595bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
596 StringRef PipelineText, bool VerifyEachPass,
597 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000598 // By default, try to parse the pipeline as-if it were within an implicit
599 // 'module(...)' pass pipeline. If this will parse at all, it needs to
600 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000601 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000602 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000603
Chandler Carruthea368f12015-01-06 08:37:58 +0000604 // This isn't parsable as a module pipeline, look for the end of a pass name
605 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000606 StringRef FirstName =
607 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000608 assert(!isModulePassName(FirstName) &&
609 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000610
Chandler Carruthea368f12015-01-06 08:37:58 +0000611 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
612 // pipeline.
Justin Bognereecc3c82016-02-25 07:23:08 +0000613 if (PipelineText.startswith("cgscc(") || isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000614 CGSCCPassManager CGPM(DebugLogging);
615 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
616 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000617 !PipelineText.empty())
618 return false;
619 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
620 return true;
621 }
622
Chandler Carruthea368f12015-01-06 08:37:58 +0000623 // Similarly, if this looks like a Function pass, parse the whole thing as
624 // a Function pipelien.
Justin Bognereecc3c82016-02-25 07:23:08 +0000625 if (PipelineText.startswith("function(") || isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000626 FunctionPassManager FPM(DebugLogging);
627 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
628 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000629 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000630 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000631 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000632 return true;
633 }
Chandler Carruth66445382014-01-11 08:16:35 +0000634
Justin Bognereecc3c82016-02-25 07:23:08 +0000635 // If this looks like a Loop pass, parse the whole thing as a Loop pipeline.
636 if (PipelineText.startswith("loop(") || isLoopPassName(FirstName)) {
637 LoopPassManager LPM(DebugLogging);
638 if (!parseLoopPassPipeline(LPM, PipelineText, VerifyEachPass,
639 DebugLogging) ||
640 !PipelineText.empty())
641 return false;
642 FunctionPassManager FPM(DebugLogging);
643 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
644 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
645 return true;
646 }
647
648
Chandler Carruth66445382014-01-11 08:16:35 +0000649 return false;
650}
Chandler Carruthedf59962016-02-18 09:45:17 +0000651
652bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
653 while (!PipelineText.empty()) {
654 StringRef Name;
655 std::tie(Name, PipelineText) = PipelineText.split(',');
656 if (!parseAAPassName(AA, Name))
657 return false;
658 }
659
660 return true;
661}