blob: 17208c921369242e0e695cb584d55ab63a3d3da0 [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"
Chandler Carruth3a040e62015-12-27 08:41:34 +000050#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Justin Bogner21e15372015-10-30 23:28:12 +000051#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000052#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000053#include "llvm/Transforms/InstrProfiling.h"
Justin Bogner19b67992015-10-30 23:13:18 +000054#include "llvm/Transforms/Scalar/ADCE.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000055#include "llvm/Transforms/Scalar/EarlyCSE.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000056#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Chandler Carruth89c45a12016-03-11 08:50:55 +000057#include "llvm/Transforms/Scalar/GVN.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000058#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000059#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Chandler Carruth58dde8c2016-02-26 12:17:54 +000060#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +000061
62using namespace llvm;
63
Chandler Carruth8b5a74192016-02-28 22:16:03 +000064static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
65
Chandler Carruth66445382014-01-11 08:16:35 +000066namespace {
67
Chandler Carruthd8330982014-01-12 09:34:22 +000068/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000069struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000070 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000071 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000072};
73
Chandler Carruth0b576b32015-01-06 02:50:06 +000074/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +000075class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
76 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +000077 static char PassID;
78
79public:
Chandler Carruth0b576b32015-01-06 02:50:06 +000080 struct Result {};
81 Result run(Module &) { return Result(); }
82 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +000083};
84
Chandler Carruth572e3402014-04-21 11:12:00 +000085/// \brief No-op CGSCC pass which does nothing.
86struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000087 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +000088 return PreservedAnalyses::all();
89 }
90 static StringRef name() { return "NoOpCGSCCPass"; }
91};
92
Chandler Carruth0b576b32015-01-06 02:50:06 +000093/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +000094class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
95 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +000096 static char PassID;
97
98public:
Chandler Carruth0b576b32015-01-06 02:50:06 +000099 struct Result {};
100 Result run(LazyCallGraph::SCC &) { return Result(); }
101 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000102};
103
Chandler Carruthd8330982014-01-12 09:34:22 +0000104/// \brief No-op function pass which does nothing.
105struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000106 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +0000107 static StringRef name() { return "NoOpFunctionPass"; }
108};
109
Chandler Carruth0b576b32015-01-06 02:50:06 +0000110/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000111class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
112 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000113 static char PassID;
114
115public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000116 struct Result {};
117 Result run(Function &) { return Result(); }
118 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000119};
120
Justin Bognereecc3c82016-02-25 07:23:08 +0000121/// \brief No-op loop pass which does nothing.
122struct NoOpLoopPass {
123 PreservedAnalyses run(Loop &L) { return PreservedAnalyses::all(); }
124 static StringRef name() { return "NoOpLoopPass"; }
125};
126
127/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000128class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
129 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000130 static char PassID;
131
132public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000133 struct Result {};
134 Result run(Loop &) { return Result(); }
135 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000136};
137
Chandler Carruthb4faf132016-03-11 10:22:49 +0000138char NoOpModuleAnalysis::PassID;
139char NoOpCGSCCAnalysis::PassID;
140char NoOpFunctionAnalysis::PassID;
141char NoOpLoopAnalysis::PassID;
142
Chandler Carruth66445382014-01-11 08:16:35 +0000143} // End anonymous namespace.
144
Chandler Carruth1ff77242015-03-07 09:02:36 +0000145void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000146#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000147 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000148#include "PassRegistry.def"
149}
150
Chandler Carruth1ff77242015-03-07 09:02:36 +0000151void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000152#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000153 CGAM.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::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000158#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000159 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000160#include "PassRegistry.def"
161}
162
Justin Bognereecc3c82016-02-25 07:23:08 +0000163void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
164#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
165 LAM.registerPass([&] { return CREATE_PASS; });
166#include "PassRegistry.def"
167}
168
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000169void PassBuilder::addPerModuleDefaultPipeline(ModulePassManager &MPM,
170 OptimizationLevel Level,
171 bool DebugLogging) {
172 // FIXME: Finish fleshing this out to match the legacy pipelines.
173 FunctionPassManager EarlyFPM(DebugLogging);
174 EarlyFPM.addPass(SimplifyCFGPass());
175 EarlyFPM.addPass(SROA());
176 EarlyFPM.addPass(EarlyCSEPass());
177 EarlyFPM.addPass(LowerExpectIntrinsicPass());
178
179 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
180}
181
182void PassBuilder::addLTOPreLinkDefaultPipeline(ModulePassManager &MPM,
183 OptimizationLevel Level,
184 bool DebugLogging) {
185 // FIXME: We should use a customized pre-link pipeline!
186 addPerModuleDefaultPipeline(MPM, Level, DebugLogging);
187}
188
189void PassBuilder::addLTODefaultPipeline(ModulePassManager &MPM,
190 OptimizationLevel Level,
191 bool DebugLogging) {
192 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
193 FunctionPassManager LateFPM(DebugLogging);
194 LateFPM.addPass(InstCombinePass());
195 LateFPM.addPass(SimplifyCFGPass());
196
197 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
198}
199
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000200#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000201static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000202 // Manually handle aliases for pre-configured pipeline fragments.
203 if (Name.startswith("default") || Name.startswith("lto"))
204 return DefaultAliasRegex.match(Name);
205
Chandler Carruth58944182014-04-21 08:08:50 +0000206#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000207#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000208 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000209 return true;
210#include "PassRegistry.def"
211
Chandler Carruth66445382014-01-11 08:16:35 +0000212 return false;
213}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000214#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000215
Chandler Carruth572e3402014-04-21 11:12:00 +0000216static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000217#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000218#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000219 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000220 return true;
221#include "PassRegistry.def"
222
Chandler Carruth572e3402014-04-21 11:12:00 +0000223 return false;
224}
225
Chandler Carruthd8330982014-01-12 09:34:22 +0000226static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000227#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000228#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000229 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000230 return true;
231#include "PassRegistry.def"
232
Chandler Carruthd8330982014-01-12 09:34:22 +0000233 return false;
234}
235
Justin Bognereecc3c82016-02-25 07:23:08 +0000236static bool isLoopPassName(StringRef Name) {
237#define LOOP_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
238#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
239 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
240 return true;
241#include "PassRegistry.def"
242
243 return false;
244}
245
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000246bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name,
247 bool DebugLogging) {
248 // Manually handle aliases for pre-configured pipeline fragments.
249 if (Name.startswith("default") || Name.startswith("lto")) {
250 SmallVector<StringRef, 3> Matches;
251 if (!DefaultAliasRegex.match(Name, &Matches))
252 return false;
253 assert(Matches.size() == 3 && "Must capture two matched strings!");
254
255 auto L = StringSwitch<OptimizationLevel>(Matches[2])
256 .Case("O0", O0)
257 .Case("O1", O1)
258 .Case("O2", O2)
259 .Case("O3", O3)
260 .Case("Os", Os)
261 .Case("Oz", Oz);
262
263 if (Matches[1] == "default") {
264 addPerModuleDefaultPipeline(MPM, L, DebugLogging);
265 } else if (Matches[1] == "lto-pre-link") {
266 addLTOPreLinkDefaultPipeline(MPM, L, DebugLogging);
267 } else {
268 assert(Matches[1] == "lto" && "Not one of the matched options!");
269 addLTODefaultPipeline(MPM, L, DebugLogging);
270 }
271 return true;
272 }
273
Chandler Carruth58944182014-04-21 08:08:50 +0000274#define MODULE_PASS(NAME, CREATE_PASS) \
275 if (Name == NAME) { \
276 MPM.addPass(CREATE_PASS); \
277 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000278 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000279#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
280 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000281 MPM.addPass(RequireAnalysisPass< \
282 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000283 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000284 } \
285 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000286 MPM.addPass(InvalidateAnalysisPass< \
287 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000288 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000289 }
290#include "PassRegistry.def"
291
Chandler Carruth66445382014-01-11 08:16:35 +0000292 return false;
293}
294
Chandler Carruth1ff77242015-03-07 09:02:36 +0000295bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000296#define CGSCC_PASS(NAME, CREATE_PASS) \
297 if (Name == NAME) { \
298 CGPM.addPass(CREATE_PASS); \
299 return true; \
300 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000301#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
302 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000303 CGPM.addPass(RequireAnalysisPass< \
304 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000305 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000306 } \
307 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000308 CGPM.addPass(InvalidateAnalysisPass< \
309 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000310 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000311 }
312#include "PassRegistry.def"
313
Chandler Carruth572e3402014-04-21 11:12:00 +0000314 return false;
315}
316
Chandler Carruth1ff77242015-03-07 09:02:36 +0000317bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
318 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000319#define FUNCTION_PASS(NAME, CREATE_PASS) \
320 if (Name == NAME) { \
321 FPM.addPass(CREATE_PASS); \
322 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000323 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000324#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
325 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000326 FPM.addPass(RequireAnalysisPass< \
327 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000328 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000329 } \
330 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000331 FPM.addPass(InvalidateAnalysisPass< \
332 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000333 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000334 }
335#include "PassRegistry.def"
336
Chandler Carruthd8330982014-01-12 09:34:22 +0000337 return false;
338}
339
Justin Bognereecc3c82016-02-25 07:23:08 +0000340bool PassBuilder::parseLoopPassName(LoopPassManager &FPM,
341 StringRef Name) {
342#define LOOP_PASS(NAME, CREATE_PASS) \
343 if (Name == NAME) { \
344 FPM.addPass(CREATE_PASS); \
345 return true; \
346 }
347#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
348 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000349 FPM.addPass(RequireAnalysisPass< \
350 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000351 return true; \
352 } \
353 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000354 FPM.addPass(InvalidateAnalysisPass< \
355 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000356 return true; \
357 }
358#include "PassRegistry.def"
359
360 return false;
361}
362
Chandler Carruthedf59962016-02-18 09:45:17 +0000363bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +0000364#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
365 if (Name == NAME) { \
366 AA.registerModuleAnalysis< \
367 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
368 return true; \
369 }
Chandler Carruthedf59962016-02-18 09:45:17 +0000370#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
371 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000372 AA.registerFunctionAnalysis< \
373 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +0000374 return true; \
375 }
376#include "PassRegistry.def"
377
378 return false;
379}
380
Justin Bognereecc3c82016-02-25 07:23:08 +0000381bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
382 StringRef &PipelineText,
383 bool VerifyEachPass,
384 bool DebugLogging) {
385 for (;;) {
386 // Parse nested pass managers by recursing.
387 if (PipelineText.startswith("loop(")) {
388 LoopPassManager NestedLPM(DebugLogging);
389
390 // Parse the inner pipeline inte the nested manager.
391 PipelineText = PipelineText.substr(strlen("loop("));
392 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
393 DebugLogging) ||
394 PipelineText.empty())
395 return false;
396 assert(PipelineText[0] == ')');
397 PipelineText = PipelineText.substr(1);
398
399 // Add the nested pass manager with the appropriate adaptor.
400 LPM.addPass(std::move(NestedLPM));
401 } else {
402 // Otherwise try to parse a pass name.
403 size_t End = PipelineText.find_first_of(",)");
404 if (!parseLoopPassName(LPM, PipelineText.substr(0, End)))
405 return false;
406 // TODO: Ideally, we would run a LoopVerifierPass() here in the
407 // VerifyEachPass case, but we don't have such a verifier yet.
408
409 PipelineText = PipelineText.substr(End);
410 }
411
412 if (PipelineText.empty() || PipelineText[0] == ')')
413 return true;
414
415 assert(PipelineText[0] == ',');
416 PipelineText = PipelineText.substr(1);
417 }
418}
419
Chandler Carruth1ff77242015-03-07 09:02:36 +0000420bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
421 StringRef &PipelineText,
422 bool VerifyEachPass,
423 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000424 for (;;) {
425 // Parse nested pass managers by recursing.
426 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000427 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000428
429 // Parse the inner pipeline inte the nested manager.
430 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000431 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
432 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000433 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000434 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000435 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000436 PipelineText = PipelineText.substr(1);
437
438 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000439 FPM.addPass(std::move(NestedFPM));
Justin Bognereecc3c82016-02-25 07:23:08 +0000440 } else if (PipelineText.startswith("loop(")) {
441 LoopPassManager NestedLPM(DebugLogging);
442
443 // Parse the inner pipeline inte the nested manager.
444 PipelineText = PipelineText.substr(strlen("loop("));
445 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
446 DebugLogging) ||
447 PipelineText.empty())
448 return false;
449 assert(PipelineText[0] == ')');
450 PipelineText = PipelineText.substr(1);
451
452 // Add the nested pass manager with the appropriate adaptor.
453 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(NestedLPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000454 } else {
455 // Otherwise try to parse a pass name.
456 size_t End = PipelineText.find_first_of(",)");
457 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
458 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000459 if (VerifyEachPass)
460 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000461
462 PipelineText = PipelineText.substr(End);
463 }
464
465 if (PipelineText.empty() || PipelineText[0] == ')')
466 return true;
467
468 assert(PipelineText[0] == ',');
469 PipelineText = PipelineText.substr(1);
470 }
471}
472
Chandler Carruth1ff77242015-03-07 09:02:36 +0000473bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
474 StringRef &PipelineText,
475 bool VerifyEachPass,
476 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000477 for (;;) {
478 // Parse nested pass managers by recursing.
479 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000480 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000481
482 // Parse the inner pipeline into the nested manager.
483 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000484 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
485 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000486 PipelineText.empty())
487 return false;
488 assert(PipelineText[0] == ')');
489 PipelineText = PipelineText.substr(1);
490
491 // Add the nested pass manager with the appropriate adaptor.
492 CGPM.addPass(std::move(NestedCGPM));
493 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000494 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000495
496 // Parse the inner pipeline inte the nested manager.
497 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000498 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
499 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000500 PipelineText.empty())
501 return false;
502 assert(PipelineText[0] == ')');
503 PipelineText = PipelineText.substr(1);
504
505 // Add the nested pass manager with the appropriate adaptor.
506 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
507 } else {
508 // Otherwise try to parse a pass name.
509 size_t End = PipelineText.find_first_of(",)");
510 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
511 return false;
512 // FIXME: No verifier support for CGSCC passes!
513
514 PipelineText = PipelineText.substr(End);
515 }
516
517 if (PipelineText.empty() || PipelineText[0] == ')')
518 return true;
519
520 assert(PipelineText[0] == ',');
521 PipelineText = PipelineText.substr(1);
522 }
523}
524
Chandler Carruth1ff77242015-03-07 09:02:36 +0000525bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
526 StringRef &PipelineText,
527 bool VerifyEachPass,
528 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000529 for (;;) {
530 // Parse nested pass managers by recursing.
531 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000532 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000533
534 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000535 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000536 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
537 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000538 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000539 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000540 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000541 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000542
543 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000544 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000545 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000546 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000547
548 // Parse the inner pipeline inte the nested manager.
549 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000550 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
551 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000552 PipelineText.empty())
553 return false;
554 assert(PipelineText[0] == ')');
555 PipelineText = PipelineText.substr(1);
556
557 // Add the nested pass manager with the appropriate adaptor.
558 MPM.addPass(
559 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000560 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000561 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000562
563 // Parse the inner pipeline inte the nested manager.
564 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000565 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
566 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000567 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000568 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000569 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000570 PipelineText = PipelineText.substr(1);
571
572 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000573 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000574 } else {
575 // Otherwise try to parse a pass name.
576 size_t End = PipelineText.find_first_of(",)");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000577 if (!parseModulePassName(MPM, PipelineText.substr(0, End), DebugLogging))
Chandler Carruth66445382014-01-11 08:16:35 +0000578 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000579 if (VerifyEachPass)
580 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000581
582 PipelineText = PipelineText.substr(End);
583 }
584
585 if (PipelineText.empty() || PipelineText[0] == ')')
586 return true;
587
588 assert(PipelineText[0] == ',');
589 PipelineText = PipelineText.substr(1);
590 }
591}
592
593// Primary pass pipeline description parsing routine.
594// FIXME: Should this routine accept a TargetMachine or require the caller to
595// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000596bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
597 StringRef PipelineText, bool VerifyEachPass,
598 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000599 // By default, try to parse the pipeline as-if it were within an implicit
600 // 'module(...)' pass pipeline. If this will parse at all, it needs to
601 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000602 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000603 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000604
Chandler Carruthea368f12015-01-06 08:37:58 +0000605 // This isn't parsable as a module pipeline, look for the end of a pass name
606 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000607 StringRef FirstName =
608 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000609 assert(!isModulePassName(FirstName) &&
610 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000611
Chandler Carruthea368f12015-01-06 08:37:58 +0000612 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
613 // pipeline.
Justin Bognereecc3c82016-02-25 07:23:08 +0000614 if (PipelineText.startswith("cgscc(") || isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000615 CGSCCPassManager CGPM(DebugLogging);
616 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
617 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000618 !PipelineText.empty())
619 return false;
620 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
621 return true;
622 }
623
Chandler Carruthea368f12015-01-06 08:37:58 +0000624 // Similarly, if this looks like a Function pass, parse the whole thing as
625 // a Function pipelien.
Justin Bognereecc3c82016-02-25 07:23:08 +0000626 if (PipelineText.startswith("function(") || isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000627 FunctionPassManager FPM(DebugLogging);
628 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
629 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000630 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000631 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000632 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000633 return true;
634 }
Chandler Carruth66445382014-01-11 08:16:35 +0000635
Justin Bognereecc3c82016-02-25 07:23:08 +0000636 // If this looks like a Loop pass, parse the whole thing as a Loop pipeline.
637 if (PipelineText.startswith("loop(") || isLoopPassName(FirstName)) {
638 LoopPassManager LPM(DebugLogging);
639 if (!parseLoopPassPipeline(LPM, PipelineText, VerifyEachPass,
640 DebugLogging) ||
641 !PipelineText.empty())
642 return false;
643 FunctionPassManager FPM(DebugLogging);
644 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
645 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
646 return true;
647 }
648
649
Chandler Carruth66445382014-01-11 08:16:35 +0000650 return false;
651}
Chandler Carruthedf59962016-02-18 09:45:17 +0000652
653bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
654 while (!PipelineText.empty()) {
655 StringRef Name;
656 std::tie(Name, PipelineText) = PipelineText.split(',');
657 if (!parseAAPassName(AA, Name))
658 return false;
659 }
660
661 return true;
662}