blob: 049b8fba39228849fe129f174b4bd7d9cc283ca6 [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"
Michael Kupersteinde16b442016-04-18 23:55:01 +000027#include "llvm/Analysis/DemandedBits.h"
Hongbin Zheng751337f2016-02-25 17:54:15 +000028#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruth45a9c202016-03-11 09:15:11 +000029#include "llvm/Analysis/GlobalsModRef.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000030#include "llvm/Analysis/LazyCallGraph.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000031#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth61440d22016-03-10 00:55:30 +000032#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000033#include "llvm/Analysis/PostDominators.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000034#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000035#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000036#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000037#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000038#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000039#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000040#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000041#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000042#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000043#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000044#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000045#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000046#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000047#include "llvm/Target/TargetMachine.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000048#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000049#include "llvm/Transforms/IPO/FunctionAttrs.h"
Davide Italiano66228c42016-05-03 19:39:15 +000050#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000051#include "llvm/Transforms/IPO/GlobalOpt.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000052#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Justin Bogner4563a062016-04-26 20:15:52 +000053#include "llvm/Transforms/IPO/Internalize.h"
Justin Bogner21e15372015-10-30 23:28:12 +000054#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000055#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000056#include "llvm/Transforms/InstrProfiling.h"
Justin Bogner19b67992015-10-30 23:13:18 +000057#include "llvm/Transforms/Scalar/ADCE.h"
Justin Bogner395c2122016-04-22 19:40:41 +000058#include "llvm/Transforms/Scalar/DCE.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000059#include "llvm/Transforms/Scalar/EarlyCSE.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000060#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Chandler Carruth89c45a12016-03-11 08:50:55 +000061#include "llvm/Transforms/Scalar/GVN.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +000062#include "llvm/Transforms/Scalar/Reassociate.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000063#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000064#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +000065#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruth58dde8c2016-02-26 12:17:54 +000066#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +000067
68using namespace llvm;
69
Chandler Carruth8b5a74192016-02-28 22:16:03 +000070static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
71
Chandler Carruth66445382014-01-11 08:16:35 +000072namespace {
73
Chandler Carruthd8330982014-01-12 09:34:22 +000074/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000075struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000076 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000077 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000078};
79
Chandler Carruth0b576b32015-01-06 02:50:06 +000080/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +000081class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
82 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +000083 static char PassID;
84
85public:
Chandler Carruth0b576b32015-01-06 02:50:06 +000086 struct Result {};
87 Result run(Module &) { return Result(); }
88 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +000089};
90
Chandler Carruth572e3402014-04-21 11:12:00 +000091/// \brief No-op CGSCC pass which does nothing.
92struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000093 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +000094 return PreservedAnalyses::all();
95 }
96 static StringRef name() { return "NoOpCGSCCPass"; }
97};
98
Chandler Carruth0b576b32015-01-06 02:50:06 +000099/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000100class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
101 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000102 static char PassID;
103
104public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000105 struct Result {};
106 Result run(LazyCallGraph::SCC &) { return Result(); }
107 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000108};
109
Chandler Carruthd8330982014-01-12 09:34:22 +0000110/// \brief No-op function pass which does nothing.
111struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000112 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +0000113 static StringRef name() { return "NoOpFunctionPass"; }
114};
115
Chandler Carruth0b576b32015-01-06 02:50:06 +0000116/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000117class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
118 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000119 static char PassID;
120
121public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000122 struct Result {};
123 Result run(Function &) { return Result(); }
124 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000125};
126
Justin Bognereecc3c82016-02-25 07:23:08 +0000127/// \brief No-op loop pass which does nothing.
128struct NoOpLoopPass {
129 PreservedAnalyses run(Loop &L) { return PreservedAnalyses::all(); }
130 static StringRef name() { return "NoOpLoopPass"; }
131};
132
133/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000134class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
135 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000136 static char PassID;
137
138public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000139 struct Result {};
140 Result run(Loop &) { return Result(); }
141 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000142};
143
Chandler Carruthb4faf132016-03-11 10:22:49 +0000144char NoOpModuleAnalysis::PassID;
145char NoOpCGSCCAnalysis::PassID;
146char NoOpFunctionAnalysis::PassID;
147char NoOpLoopAnalysis::PassID;
148
Chandler Carruth66445382014-01-11 08:16:35 +0000149} // End anonymous namespace.
150
Chandler Carruth1ff77242015-03-07 09:02:36 +0000151void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000152#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000153 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000154#include "PassRegistry.def"
155}
156
Chandler Carruth1ff77242015-03-07 09:02:36 +0000157void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000158#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000159 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000160#include "PassRegistry.def"
161}
162
Chandler Carruth1ff77242015-03-07 09:02:36 +0000163void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000164#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000165 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000166#include "PassRegistry.def"
167}
168
Justin Bognereecc3c82016-02-25 07:23:08 +0000169void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
170#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
171 LAM.registerPass([&] { return CREATE_PASS; });
172#include "PassRegistry.def"
173}
174
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000175void PassBuilder::addPerModuleDefaultPipeline(ModulePassManager &MPM,
176 OptimizationLevel Level,
177 bool DebugLogging) {
178 // FIXME: Finish fleshing this out to match the legacy pipelines.
179 FunctionPassManager EarlyFPM(DebugLogging);
180 EarlyFPM.addPass(SimplifyCFGPass());
181 EarlyFPM.addPass(SROA());
182 EarlyFPM.addPass(EarlyCSEPass());
183 EarlyFPM.addPass(LowerExpectIntrinsicPass());
184
185 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
186}
187
188void PassBuilder::addLTOPreLinkDefaultPipeline(ModulePassManager &MPM,
189 OptimizationLevel Level,
190 bool DebugLogging) {
191 // FIXME: We should use a customized pre-link pipeline!
192 addPerModuleDefaultPipeline(MPM, Level, DebugLogging);
193}
194
195void PassBuilder::addLTODefaultPipeline(ModulePassManager &MPM,
196 OptimizationLevel Level,
197 bool DebugLogging) {
198 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
199 FunctionPassManager LateFPM(DebugLogging);
200 LateFPM.addPass(InstCombinePass());
201 LateFPM.addPass(SimplifyCFGPass());
202
203 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
204}
205
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000206#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000207static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000208 // Manually handle aliases for pre-configured pipeline fragments.
209 if (Name.startswith("default") || Name.startswith("lto"))
210 return DefaultAliasRegex.match(Name);
211
Chandler Carruth58944182014-04-21 08:08:50 +0000212#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000213#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000214 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000215 return true;
216#include "PassRegistry.def"
217
Chandler Carruth66445382014-01-11 08:16:35 +0000218 return false;
219}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000220#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000221
Chandler Carruth572e3402014-04-21 11:12:00 +0000222static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000223#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000224#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000225 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000226 return true;
227#include "PassRegistry.def"
228
Chandler Carruth572e3402014-04-21 11:12:00 +0000229 return false;
230}
231
Chandler Carruthd8330982014-01-12 09:34:22 +0000232static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000233#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000234#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000235 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000236 return true;
237#include "PassRegistry.def"
238
Chandler Carruthd8330982014-01-12 09:34:22 +0000239 return false;
240}
241
Justin Bognereecc3c82016-02-25 07:23:08 +0000242static bool isLoopPassName(StringRef Name) {
243#define LOOP_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
244#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
245 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
246 return true;
247#include "PassRegistry.def"
248
249 return false;
250}
251
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000252bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name,
253 bool DebugLogging) {
254 // Manually handle aliases for pre-configured pipeline fragments.
255 if (Name.startswith("default") || Name.startswith("lto")) {
256 SmallVector<StringRef, 3> Matches;
257 if (!DefaultAliasRegex.match(Name, &Matches))
258 return false;
259 assert(Matches.size() == 3 && "Must capture two matched strings!");
260
261 auto L = StringSwitch<OptimizationLevel>(Matches[2])
262 .Case("O0", O0)
263 .Case("O1", O1)
264 .Case("O2", O2)
265 .Case("O3", O3)
266 .Case("Os", Os)
267 .Case("Oz", Oz);
268
269 if (Matches[1] == "default") {
270 addPerModuleDefaultPipeline(MPM, L, DebugLogging);
271 } else if (Matches[1] == "lto-pre-link") {
272 addLTOPreLinkDefaultPipeline(MPM, L, DebugLogging);
273 } else {
274 assert(Matches[1] == "lto" && "Not one of the matched options!");
275 addLTODefaultPipeline(MPM, L, DebugLogging);
276 }
277 return true;
278 }
279
Chandler Carruth58944182014-04-21 08:08:50 +0000280#define MODULE_PASS(NAME, CREATE_PASS) \
281 if (Name == NAME) { \
282 MPM.addPass(CREATE_PASS); \
283 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000284 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000285#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
286 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000287 MPM.addPass(RequireAnalysisPass< \
288 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000289 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000290 } \
291 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000292 MPM.addPass(InvalidateAnalysisPass< \
293 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000294 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000295 }
296#include "PassRegistry.def"
297
Chandler Carruth66445382014-01-11 08:16:35 +0000298 return false;
299}
300
Chandler Carruth1ff77242015-03-07 09:02:36 +0000301bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000302#define CGSCC_PASS(NAME, CREATE_PASS) \
303 if (Name == NAME) { \
304 CGPM.addPass(CREATE_PASS); \
305 return true; \
306 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000307#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
308 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000309 CGPM.addPass(RequireAnalysisPass< \
310 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000311 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000312 } \
313 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000314 CGPM.addPass(InvalidateAnalysisPass< \
315 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000316 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000317 }
318#include "PassRegistry.def"
319
Chandler Carruth572e3402014-04-21 11:12:00 +0000320 return false;
321}
322
Chandler Carruth1ff77242015-03-07 09:02:36 +0000323bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
324 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000325#define FUNCTION_PASS(NAME, CREATE_PASS) \
326 if (Name == NAME) { \
327 FPM.addPass(CREATE_PASS); \
328 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000329 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000330#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
331 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000332 FPM.addPass(RequireAnalysisPass< \
333 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000334 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000335 } \
336 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000337 FPM.addPass(InvalidateAnalysisPass< \
338 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000339 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000340 }
341#include "PassRegistry.def"
342
Chandler Carruthd8330982014-01-12 09:34:22 +0000343 return false;
344}
345
Justin Bognereecc3c82016-02-25 07:23:08 +0000346bool PassBuilder::parseLoopPassName(LoopPassManager &FPM,
347 StringRef Name) {
348#define LOOP_PASS(NAME, CREATE_PASS) \
349 if (Name == NAME) { \
350 FPM.addPass(CREATE_PASS); \
351 return true; \
352 }
353#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
354 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000355 FPM.addPass(RequireAnalysisPass< \
356 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000357 return true; \
358 } \
359 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000360 FPM.addPass(InvalidateAnalysisPass< \
361 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000362 return true; \
363 }
364#include "PassRegistry.def"
365
366 return false;
367}
368
Chandler Carruthedf59962016-02-18 09:45:17 +0000369bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +0000370#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
371 if (Name == NAME) { \
372 AA.registerModuleAnalysis< \
373 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
374 return true; \
375 }
Chandler Carruthedf59962016-02-18 09:45:17 +0000376#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
377 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000378 AA.registerFunctionAnalysis< \
379 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +0000380 return true; \
381 }
382#include "PassRegistry.def"
383
384 return false;
385}
386
Justin Bognereecc3c82016-02-25 07:23:08 +0000387bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
388 StringRef &PipelineText,
389 bool VerifyEachPass,
390 bool DebugLogging) {
391 for (;;) {
392 // Parse nested pass managers by recursing.
393 if (PipelineText.startswith("loop(")) {
394 LoopPassManager NestedLPM(DebugLogging);
395
396 // Parse the inner pipeline inte the nested manager.
397 PipelineText = PipelineText.substr(strlen("loop("));
398 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
399 DebugLogging) ||
400 PipelineText.empty())
401 return false;
402 assert(PipelineText[0] == ')');
403 PipelineText = PipelineText.substr(1);
404
405 // Add the nested pass manager with the appropriate adaptor.
406 LPM.addPass(std::move(NestedLPM));
407 } else {
408 // Otherwise try to parse a pass name.
409 size_t End = PipelineText.find_first_of(",)");
410 if (!parseLoopPassName(LPM, PipelineText.substr(0, End)))
411 return false;
412 // TODO: Ideally, we would run a LoopVerifierPass() here in the
413 // VerifyEachPass case, but we don't have such a verifier yet.
414
415 PipelineText = PipelineText.substr(End);
416 }
417
418 if (PipelineText.empty() || PipelineText[0] == ')')
419 return true;
420
421 assert(PipelineText[0] == ',');
422 PipelineText = PipelineText.substr(1);
423 }
424}
425
Chandler Carruth1ff77242015-03-07 09:02:36 +0000426bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
427 StringRef &PipelineText,
428 bool VerifyEachPass,
429 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000430 for (;;) {
431 // Parse nested pass managers by recursing.
432 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000433 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000434
435 // Parse the inner pipeline inte the nested manager.
436 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000437 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
438 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000439 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000440 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000441 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000442 PipelineText = PipelineText.substr(1);
443
444 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000445 FPM.addPass(std::move(NestedFPM));
Justin Bognereecc3c82016-02-25 07:23:08 +0000446 } else if (PipelineText.startswith("loop(")) {
447 LoopPassManager NestedLPM(DebugLogging);
448
449 // Parse the inner pipeline inte the nested manager.
450 PipelineText = PipelineText.substr(strlen("loop("));
451 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
452 DebugLogging) ||
453 PipelineText.empty())
454 return false;
455 assert(PipelineText[0] == ')');
456 PipelineText = PipelineText.substr(1);
457
458 // Add the nested pass manager with the appropriate adaptor.
459 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(NestedLPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000460 } else {
461 // Otherwise try to parse a pass name.
462 size_t End = PipelineText.find_first_of(",)");
463 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
464 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000465 if (VerifyEachPass)
466 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000467
468 PipelineText = PipelineText.substr(End);
469 }
470
471 if (PipelineText.empty() || PipelineText[0] == ')')
472 return true;
473
474 assert(PipelineText[0] == ',');
475 PipelineText = PipelineText.substr(1);
476 }
477}
478
Chandler Carruth1ff77242015-03-07 09:02:36 +0000479bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
480 StringRef &PipelineText,
481 bool VerifyEachPass,
482 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000483 for (;;) {
484 // Parse nested pass managers by recursing.
485 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000486 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000487
488 // Parse the inner pipeline into the nested manager.
489 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000490 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
491 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000492 PipelineText.empty())
493 return false;
494 assert(PipelineText[0] == ')');
495 PipelineText = PipelineText.substr(1);
496
497 // Add the nested pass manager with the appropriate adaptor.
498 CGPM.addPass(std::move(NestedCGPM));
499 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000500 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000501
502 // Parse the inner pipeline inte the nested manager.
503 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000504 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
505 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000506 PipelineText.empty())
507 return false;
508 assert(PipelineText[0] == ')');
509 PipelineText = PipelineText.substr(1);
510
511 // Add the nested pass manager with the appropriate adaptor.
512 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
513 } else {
514 // Otherwise try to parse a pass name.
515 size_t End = PipelineText.find_first_of(",)");
516 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
517 return false;
518 // FIXME: No verifier support for CGSCC passes!
519
520 PipelineText = PipelineText.substr(End);
521 }
522
523 if (PipelineText.empty() || PipelineText[0] == ')')
524 return true;
525
526 assert(PipelineText[0] == ',');
527 PipelineText = PipelineText.substr(1);
528 }
529}
530
Chandler Carruth1ff77242015-03-07 09:02:36 +0000531bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
532 StringRef &PipelineText,
533 bool VerifyEachPass,
534 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000535 for (;;) {
536 // Parse nested pass managers by recursing.
537 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000538 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000539
540 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000541 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000542 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
543 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000544 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000545 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000546 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000547 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000548
549 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000550 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000551 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000552 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000553
554 // Parse the inner pipeline inte the nested manager.
555 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000556 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
557 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000558 PipelineText.empty())
559 return false;
560 assert(PipelineText[0] == ')');
561 PipelineText = PipelineText.substr(1);
562
563 // Add the nested pass manager with the appropriate adaptor.
564 MPM.addPass(
565 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000566 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000567 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000568
569 // Parse the inner pipeline inte the nested manager.
570 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000571 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
572 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000573 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000574 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000575 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000576 PipelineText = PipelineText.substr(1);
577
578 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000579 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000580 } else {
581 // Otherwise try to parse a pass name.
582 size_t End = PipelineText.find_first_of(",)");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000583 if (!parseModulePassName(MPM, PipelineText.substr(0, End), DebugLogging))
Chandler Carruth66445382014-01-11 08:16:35 +0000584 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000585 if (VerifyEachPass)
586 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000587
588 PipelineText = PipelineText.substr(End);
589 }
590
591 if (PipelineText.empty() || PipelineText[0] == ')')
592 return true;
593
594 assert(PipelineText[0] == ',');
595 PipelineText = PipelineText.substr(1);
596 }
597}
598
599// Primary pass pipeline description parsing routine.
600// FIXME: Should this routine accept a TargetMachine or require the caller to
601// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000602bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
603 StringRef PipelineText, bool VerifyEachPass,
604 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000605 // By default, try to parse the pipeline as-if it were within an implicit
606 // 'module(...)' pass pipeline. If this will parse at all, it needs to
607 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000608 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000609 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000610
Chandler Carruthea368f12015-01-06 08:37:58 +0000611 // This isn't parsable as a module pipeline, look for the end of a pass name
612 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000613 StringRef FirstName =
614 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000615 assert(!isModulePassName(FirstName) &&
616 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000617
Chandler Carruthea368f12015-01-06 08:37:58 +0000618 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
619 // pipeline.
Justin Bognereecc3c82016-02-25 07:23:08 +0000620 if (PipelineText.startswith("cgscc(") || isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000621 CGSCCPassManager CGPM(DebugLogging);
622 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
623 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000624 !PipelineText.empty())
625 return false;
626 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
627 return true;
628 }
629
Chandler Carruthea368f12015-01-06 08:37:58 +0000630 // Similarly, if this looks like a Function pass, parse the whole thing as
631 // a Function pipelien.
Justin Bognereecc3c82016-02-25 07:23:08 +0000632 if (PipelineText.startswith("function(") || isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000633 FunctionPassManager FPM(DebugLogging);
634 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
635 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000636 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000637 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000638 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000639 return true;
640 }
Chandler Carruth66445382014-01-11 08:16:35 +0000641
Justin Bognereecc3c82016-02-25 07:23:08 +0000642 // If this looks like a Loop pass, parse the whole thing as a Loop pipeline.
643 if (PipelineText.startswith("loop(") || isLoopPassName(FirstName)) {
644 LoopPassManager LPM(DebugLogging);
645 if (!parseLoopPassPipeline(LPM, PipelineText, VerifyEachPass,
646 DebugLogging) ||
647 !PipelineText.empty())
648 return false;
649 FunctionPassManager FPM(DebugLogging);
650 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
651 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
652 return true;
653 }
654
655
Chandler Carruth66445382014-01-11 08:16:35 +0000656 return false;
657}
Chandler Carruthedf59962016-02-18 09:45:17 +0000658
659bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
660 while (!PipelineText.empty()) {
661 StringRef Name;
662 std::tie(Name, PipelineText) = PipelineText.split(',');
663 if (!parseAAPassName(AA, Name))
664 return false;
665 }
666
667 return true;
668}