blob: f2acd9af831f03d17470c9a695f16e984d9f6665 [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"
Justin Bogner1a075012016-04-26 00:28:01 +000050#include "llvm/Transforms/IPO/GlobalOpt.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000051#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Justin Bogner21e15372015-10-30 23:28:12 +000052#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000053#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000054#include "llvm/Transforms/InstrProfiling.h"
Justin Bogner19b67992015-10-30 23:13:18 +000055#include "llvm/Transforms/Scalar/ADCE.h"
Justin Bogner395c2122016-04-22 19:40:41 +000056#include "llvm/Transforms/Scalar/DCE.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000057#include "llvm/Transforms/Scalar/EarlyCSE.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000058#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Chandler Carruth89c45a12016-03-11 08:50:55 +000059#include "llvm/Transforms/Scalar/GVN.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000060#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000061#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +000062#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruth58dde8c2016-02-26 12:17:54 +000063#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +000064
65using namespace llvm;
66
Chandler Carruth8b5a74192016-02-28 22:16:03 +000067static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
68
Chandler Carruth66445382014-01-11 08:16:35 +000069namespace {
70
Chandler Carruthd8330982014-01-12 09:34:22 +000071/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000072struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000073 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000074 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000075};
76
Chandler Carruth0b576b32015-01-06 02:50:06 +000077/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +000078class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
79 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +000080 static char PassID;
81
82public:
Chandler Carruth0b576b32015-01-06 02:50:06 +000083 struct Result {};
84 Result run(Module &) { return Result(); }
85 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +000086};
87
Chandler Carruth572e3402014-04-21 11:12:00 +000088/// \brief No-op CGSCC pass which does nothing.
89struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000090 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +000091 return PreservedAnalyses::all();
92 }
93 static StringRef name() { return "NoOpCGSCCPass"; }
94};
95
Chandler Carruth0b576b32015-01-06 02:50:06 +000096/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +000097class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
98 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +000099 static char PassID;
100
101public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000102 struct Result {};
103 Result run(LazyCallGraph::SCC &) { return Result(); }
104 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000105};
106
Chandler Carruthd8330982014-01-12 09:34:22 +0000107/// \brief No-op function pass which does nothing.
108struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000109 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +0000110 static StringRef name() { return "NoOpFunctionPass"; }
111};
112
Chandler Carruth0b576b32015-01-06 02:50:06 +0000113/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000114class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
115 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000116 static char PassID;
117
118public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000119 struct Result {};
120 Result run(Function &) { return Result(); }
121 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000122};
123
Justin Bognereecc3c82016-02-25 07:23:08 +0000124/// \brief No-op loop pass which does nothing.
125struct NoOpLoopPass {
126 PreservedAnalyses run(Loop &L) { return PreservedAnalyses::all(); }
127 static StringRef name() { return "NoOpLoopPass"; }
128};
129
130/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000131class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
132 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000133 static char PassID;
134
135public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000136 struct Result {};
137 Result run(Loop &) { return Result(); }
138 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000139};
140
Chandler Carruthb4faf132016-03-11 10:22:49 +0000141char NoOpModuleAnalysis::PassID;
142char NoOpCGSCCAnalysis::PassID;
143char NoOpFunctionAnalysis::PassID;
144char NoOpLoopAnalysis::PassID;
145
Chandler Carruth66445382014-01-11 08:16:35 +0000146} // End anonymous namespace.
147
Chandler Carruth1ff77242015-03-07 09:02:36 +0000148void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000149#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000150 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000151#include "PassRegistry.def"
152}
153
Chandler Carruth1ff77242015-03-07 09:02:36 +0000154void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000155#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000156 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000157#include "PassRegistry.def"
158}
159
Chandler Carruth1ff77242015-03-07 09:02:36 +0000160void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000161#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000162 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000163#include "PassRegistry.def"
164}
165
Justin Bognereecc3c82016-02-25 07:23:08 +0000166void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
167#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
168 LAM.registerPass([&] { return CREATE_PASS; });
169#include "PassRegistry.def"
170}
171
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000172void PassBuilder::addPerModuleDefaultPipeline(ModulePassManager &MPM,
173 OptimizationLevel Level,
174 bool DebugLogging) {
175 // FIXME: Finish fleshing this out to match the legacy pipelines.
176 FunctionPassManager EarlyFPM(DebugLogging);
177 EarlyFPM.addPass(SimplifyCFGPass());
178 EarlyFPM.addPass(SROA());
179 EarlyFPM.addPass(EarlyCSEPass());
180 EarlyFPM.addPass(LowerExpectIntrinsicPass());
181
182 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
183}
184
185void PassBuilder::addLTOPreLinkDefaultPipeline(ModulePassManager &MPM,
186 OptimizationLevel Level,
187 bool DebugLogging) {
188 // FIXME: We should use a customized pre-link pipeline!
189 addPerModuleDefaultPipeline(MPM, Level, DebugLogging);
190}
191
192void PassBuilder::addLTODefaultPipeline(ModulePassManager &MPM,
193 OptimizationLevel Level,
194 bool DebugLogging) {
195 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
196 FunctionPassManager LateFPM(DebugLogging);
197 LateFPM.addPass(InstCombinePass());
198 LateFPM.addPass(SimplifyCFGPass());
199
200 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
201}
202
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000203#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000204static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000205 // Manually handle aliases for pre-configured pipeline fragments.
206 if (Name.startswith("default") || Name.startswith("lto"))
207 return DefaultAliasRegex.match(Name);
208
Chandler Carruth58944182014-04-21 08:08:50 +0000209#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000210#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000211 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000212 return true;
213#include "PassRegistry.def"
214
Chandler Carruth66445382014-01-11 08:16:35 +0000215 return false;
216}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000217#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000218
Chandler Carruth572e3402014-04-21 11:12:00 +0000219static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000220#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000221#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000222 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000223 return true;
224#include "PassRegistry.def"
225
Chandler Carruth572e3402014-04-21 11:12:00 +0000226 return false;
227}
228
Chandler Carruthd8330982014-01-12 09:34:22 +0000229static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000230#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000231#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000232 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000233 return true;
234#include "PassRegistry.def"
235
Chandler Carruthd8330982014-01-12 09:34:22 +0000236 return false;
237}
238
Justin Bognereecc3c82016-02-25 07:23:08 +0000239static bool isLoopPassName(StringRef Name) {
240#define LOOP_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
241#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
242 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
243 return true;
244#include "PassRegistry.def"
245
246 return false;
247}
248
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000249bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name,
250 bool DebugLogging) {
251 // Manually handle aliases for pre-configured pipeline fragments.
252 if (Name.startswith("default") || Name.startswith("lto")) {
253 SmallVector<StringRef, 3> Matches;
254 if (!DefaultAliasRegex.match(Name, &Matches))
255 return false;
256 assert(Matches.size() == 3 && "Must capture two matched strings!");
257
258 auto L = StringSwitch<OptimizationLevel>(Matches[2])
259 .Case("O0", O0)
260 .Case("O1", O1)
261 .Case("O2", O2)
262 .Case("O3", O3)
263 .Case("Os", Os)
264 .Case("Oz", Oz);
265
266 if (Matches[1] == "default") {
267 addPerModuleDefaultPipeline(MPM, L, DebugLogging);
268 } else if (Matches[1] == "lto-pre-link") {
269 addLTOPreLinkDefaultPipeline(MPM, L, DebugLogging);
270 } else {
271 assert(Matches[1] == "lto" && "Not one of the matched options!");
272 addLTODefaultPipeline(MPM, L, DebugLogging);
273 }
274 return true;
275 }
276
Chandler Carruth58944182014-04-21 08:08:50 +0000277#define MODULE_PASS(NAME, CREATE_PASS) \
278 if (Name == NAME) { \
279 MPM.addPass(CREATE_PASS); \
280 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000281 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000282#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
283 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000284 MPM.addPass(RequireAnalysisPass< \
285 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000286 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000287 } \
288 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000289 MPM.addPass(InvalidateAnalysisPass< \
290 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000291 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000292 }
293#include "PassRegistry.def"
294
Chandler Carruth66445382014-01-11 08:16:35 +0000295 return false;
296}
297
Chandler Carruth1ff77242015-03-07 09:02:36 +0000298bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000299#define CGSCC_PASS(NAME, CREATE_PASS) \
300 if (Name == NAME) { \
301 CGPM.addPass(CREATE_PASS); \
302 return true; \
303 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000304#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
305 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000306 CGPM.addPass(RequireAnalysisPass< \
307 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000308 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000309 } \
310 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000311 CGPM.addPass(InvalidateAnalysisPass< \
312 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000313 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000314 }
315#include "PassRegistry.def"
316
Chandler Carruth572e3402014-04-21 11:12:00 +0000317 return false;
318}
319
Chandler Carruth1ff77242015-03-07 09:02:36 +0000320bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
321 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000322#define FUNCTION_PASS(NAME, CREATE_PASS) \
323 if (Name == NAME) { \
324 FPM.addPass(CREATE_PASS); \
325 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000326 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000327#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
328 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000329 FPM.addPass(RequireAnalysisPass< \
330 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000331 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000332 } \
333 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000334 FPM.addPass(InvalidateAnalysisPass< \
335 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000336 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000337 }
338#include "PassRegistry.def"
339
Chandler Carruthd8330982014-01-12 09:34:22 +0000340 return false;
341}
342
Justin Bognereecc3c82016-02-25 07:23:08 +0000343bool PassBuilder::parseLoopPassName(LoopPassManager &FPM,
344 StringRef Name) {
345#define LOOP_PASS(NAME, CREATE_PASS) \
346 if (Name == NAME) { \
347 FPM.addPass(CREATE_PASS); \
348 return true; \
349 }
350#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
351 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000352 FPM.addPass(RequireAnalysisPass< \
353 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000354 return true; \
355 } \
356 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000357 FPM.addPass(InvalidateAnalysisPass< \
358 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000359 return true; \
360 }
361#include "PassRegistry.def"
362
363 return false;
364}
365
Chandler Carruthedf59962016-02-18 09:45:17 +0000366bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +0000367#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
368 if (Name == NAME) { \
369 AA.registerModuleAnalysis< \
370 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
371 return true; \
372 }
Chandler Carruthedf59962016-02-18 09:45:17 +0000373#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
374 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000375 AA.registerFunctionAnalysis< \
376 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +0000377 return true; \
378 }
379#include "PassRegistry.def"
380
381 return false;
382}
383
Justin Bognereecc3c82016-02-25 07:23:08 +0000384bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
385 StringRef &PipelineText,
386 bool VerifyEachPass,
387 bool DebugLogging) {
388 for (;;) {
389 // Parse nested pass managers by recursing.
390 if (PipelineText.startswith("loop(")) {
391 LoopPassManager NestedLPM(DebugLogging);
392
393 // Parse the inner pipeline inte the nested manager.
394 PipelineText = PipelineText.substr(strlen("loop("));
395 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
396 DebugLogging) ||
397 PipelineText.empty())
398 return false;
399 assert(PipelineText[0] == ')');
400 PipelineText = PipelineText.substr(1);
401
402 // Add the nested pass manager with the appropriate adaptor.
403 LPM.addPass(std::move(NestedLPM));
404 } else {
405 // Otherwise try to parse a pass name.
406 size_t End = PipelineText.find_first_of(",)");
407 if (!parseLoopPassName(LPM, PipelineText.substr(0, End)))
408 return false;
409 // TODO: Ideally, we would run a LoopVerifierPass() here in the
410 // VerifyEachPass case, but we don't have such a verifier yet.
411
412 PipelineText = PipelineText.substr(End);
413 }
414
415 if (PipelineText.empty() || PipelineText[0] == ')')
416 return true;
417
418 assert(PipelineText[0] == ',');
419 PipelineText = PipelineText.substr(1);
420 }
421}
422
Chandler Carruth1ff77242015-03-07 09:02:36 +0000423bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
424 StringRef &PipelineText,
425 bool VerifyEachPass,
426 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000427 for (;;) {
428 // Parse nested pass managers by recursing.
429 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000430 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000431
432 // Parse the inner pipeline inte the nested manager.
433 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000434 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
435 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000436 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000437 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000438 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000439 PipelineText = PipelineText.substr(1);
440
441 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000442 FPM.addPass(std::move(NestedFPM));
Justin Bognereecc3c82016-02-25 07:23:08 +0000443 } else if (PipelineText.startswith("loop(")) {
444 LoopPassManager NestedLPM(DebugLogging);
445
446 // Parse the inner pipeline inte the nested manager.
447 PipelineText = PipelineText.substr(strlen("loop("));
448 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
449 DebugLogging) ||
450 PipelineText.empty())
451 return false;
452 assert(PipelineText[0] == ')');
453 PipelineText = PipelineText.substr(1);
454
455 // Add the nested pass manager with the appropriate adaptor.
456 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(NestedLPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000457 } else {
458 // Otherwise try to parse a pass name.
459 size_t End = PipelineText.find_first_of(",)");
460 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
461 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000462 if (VerifyEachPass)
463 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000464
465 PipelineText = PipelineText.substr(End);
466 }
467
468 if (PipelineText.empty() || PipelineText[0] == ')')
469 return true;
470
471 assert(PipelineText[0] == ',');
472 PipelineText = PipelineText.substr(1);
473 }
474}
475
Chandler Carruth1ff77242015-03-07 09:02:36 +0000476bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
477 StringRef &PipelineText,
478 bool VerifyEachPass,
479 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000480 for (;;) {
481 // Parse nested pass managers by recursing.
482 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000483 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000484
485 // Parse the inner pipeline into the nested manager.
486 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000487 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
488 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000489 PipelineText.empty())
490 return false;
491 assert(PipelineText[0] == ')');
492 PipelineText = PipelineText.substr(1);
493
494 // Add the nested pass manager with the appropriate adaptor.
495 CGPM.addPass(std::move(NestedCGPM));
496 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000497 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000498
499 // Parse the inner pipeline inte the nested manager.
500 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000501 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
502 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000503 PipelineText.empty())
504 return false;
505 assert(PipelineText[0] == ')');
506 PipelineText = PipelineText.substr(1);
507
508 // Add the nested pass manager with the appropriate adaptor.
509 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
510 } else {
511 // Otherwise try to parse a pass name.
512 size_t End = PipelineText.find_first_of(",)");
513 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
514 return false;
515 // FIXME: No verifier support for CGSCC passes!
516
517 PipelineText = PipelineText.substr(End);
518 }
519
520 if (PipelineText.empty() || PipelineText[0] == ')')
521 return true;
522
523 assert(PipelineText[0] == ',');
524 PipelineText = PipelineText.substr(1);
525 }
526}
527
Chandler Carruth1ff77242015-03-07 09:02:36 +0000528bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
529 StringRef &PipelineText,
530 bool VerifyEachPass,
531 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000532 for (;;) {
533 // Parse nested pass managers by recursing.
534 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000535 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000536
537 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000538 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000539 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
540 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000541 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000542 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000543 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000544 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000545
546 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000547 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000548 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000549 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000550
551 // Parse the inner pipeline inte the nested manager.
552 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000553 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
554 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000555 PipelineText.empty())
556 return false;
557 assert(PipelineText[0] == ')');
558 PipelineText = PipelineText.substr(1);
559
560 // Add the nested pass manager with the appropriate adaptor.
561 MPM.addPass(
562 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000563 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000564 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000565
566 // Parse the inner pipeline inte the nested manager.
567 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000568 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
569 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000570 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000571 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000572 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000573 PipelineText = PipelineText.substr(1);
574
575 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000576 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000577 } else {
578 // Otherwise try to parse a pass name.
579 size_t End = PipelineText.find_first_of(",)");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000580 if (!parseModulePassName(MPM, PipelineText.substr(0, End), DebugLogging))
Chandler Carruth66445382014-01-11 08:16:35 +0000581 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000582 if (VerifyEachPass)
583 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000584
585 PipelineText = PipelineText.substr(End);
586 }
587
588 if (PipelineText.empty() || PipelineText[0] == ')')
589 return true;
590
591 assert(PipelineText[0] == ',');
592 PipelineText = PipelineText.substr(1);
593 }
594}
595
596// Primary pass pipeline description parsing routine.
597// FIXME: Should this routine accept a TargetMachine or require the caller to
598// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000599bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
600 StringRef PipelineText, bool VerifyEachPass,
601 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000602 // By default, try to parse the pipeline as-if it were within an implicit
603 // 'module(...)' pass pipeline. If this will parse at all, it needs to
604 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000605 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000606 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000607
Chandler Carruthea368f12015-01-06 08:37:58 +0000608 // This isn't parsable as a module pipeline, look for the end of a pass name
609 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000610 StringRef FirstName =
611 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000612 assert(!isModulePassName(FirstName) &&
613 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000614
Chandler Carruthea368f12015-01-06 08:37:58 +0000615 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
616 // pipeline.
Justin Bognereecc3c82016-02-25 07:23:08 +0000617 if (PipelineText.startswith("cgscc(") || isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000618 CGSCCPassManager CGPM(DebugLogging);
619 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
620 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000621 !PipelineText.empty())
622 return false;
623 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
624 return true;
625 }
626
Chandler Carruthea368f12015-01-06 08:37:58 +0000627 // Similarly, if this looks like a Function pass, parse the whole thing as
628 // a Function pipelien.
Justin Bognereecc3c82016-02-25 07:23:08 +0000629 if (PipelineText.startswith("function(") || isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000630 FunctionPassManager FPM(DebugLogging);
631 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
632 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000633 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000634 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000635 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000636 return true;
637 }
Chandler Carruth66445382014-01-11 08:16:35 +0000638
Justin Bognereecc3c82016-02-25 07:23:08 +0000639 // If this looks like a Loop pass, parse the whole thing as a Loop pipeline.
640 if (PipelineText.startswith("loop(") || isLoopPassName(FirstName)) {
641 LoopPassManager LPM(DebugLogging);
642 if (!parseLoopPassPipeline(LPM, PipelineText, VerifyEachPass,
643 DebugLogging) ||
644 !PipelineText.empty())
645 return false;
646 FunctionPassManager FPM(DebugLogging);
647 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
648 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
649 return true;
650 }
651
652
Chandler Carruth66445382014-01-11 08:16:35 +0000653 return false;
654}
Chandler Carruthedf59962016-02-18 09:45:17 +0000655
656bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
657 while (!PipelineText.empty()) {
658 StringRef Name;
659 std::tie(Name, PipelineText) = PipelineText.split(',');
660 if (!parseAAPassName(AA, Name))
661 return false;
662 }
663
664 return true;
665}