blob: 32790a630868157f6657ca1af2eec71e84297a29 [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"
Justin Bognerab6a5132016-05-03 21:47:32 +000060#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000061#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Chandler Carruth89c45a12016-03-11 08:50:55 +000062#include "llvm/Transforms/Scalar/GVN.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +000063#include "llvm/Transforms/Scalar/Reassociate.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000064#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000065#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +000066#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruth58dde8c2016-02-26 12:17:54 +000067#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +000068
69using namespace llvm;
70
Chandler Carruth8b5a74192016-02-28 22:16:03 +000071static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
72
Chandler Carruth66445382014-01-11 08:16:35 +000073namespace {
74
Chandler Carruthd8330982014-01-12 09:34:22 +000075/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000076struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000077 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000078 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000079};
80
Chandler Carruth0b576b32015-01-06 02:50:06 +000081/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +000082class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
83 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +000084 static char PassID;
85
86public:
Chandler Carruth0b576b32015-01-06 02:50:06 +000087 struct Result {};
88 Result run(Module &) { return Result(); }
89 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +000090};
91
Chandler Carruth572e3402014-04-21 11:12:00 +000092/// \brief No-op CGSCC pass which does nothing.
93struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000094 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +000095 return PreservedAnalyses::all();
96 }
97 static StringRef name() { return "NoOpCGSCCPass"; }
98};
99
Chandler Carruth0b576b32015-01-06 02:50:06 +0000100/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000101class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
102 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000103 static char PassID;
104
105public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000106 struct Result {};
107 Result run(LazyCallGraph::SCC &) { return Result(); }
108 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000109};
110
Chandler Carruthd8330982014-01-12 09:34:22 +0000111/// \brief No-op function pass which does nothing.
112struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000113 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +0000114 static StringRef name() { return "NoOpFunctionPass"; }
115};
116
Chandler Carruth0b576b32015-01-06 02:50:06 +0000117/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000118class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
119 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000120 static char PassID;
121
122public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000123 struct Result {};
124 Result run(Function &) { return Result(); }
125 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000126};
127
Justin Bognereecc3c82016-02-25 07:23:08 +0000128/// \brief No-op loop pass which does nothing.
129struct NoOpLoopPass {
130 PreservedAnalyses run(Loop &L) { return PreservedAnalyses::all(); }
131 static StringRef name() { return "NoOpLoopPass"; }
132};
133
134/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000135class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
136 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000137 static char PassID;
138
139public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000140 struct Result {};
141 Result run(Loop &) { return Result(); }
142 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000143};
144
Chandler Carruthb4faf132016-03-11 10:22:49 +0000145char NoOpModuleAnalysis::PassID;
146char NoOpCGSCCAnalysis::PassID;
147char NoOpFunctionAnalysis::PassID;
148char NoOpLoopAnalysis::PassID;
149
Chandler Carruth66445382014-01-11 08:16:35 +0000150} // End anonymous namespace.
151
Chandler Carruth1ff77242015-03-07 09:02:36 +0000152void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000153#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000154 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000155#include "PassRegistry.def"
156}
157
Chandler Carruth1ff77242015-03-07 09:02:36 +0000158void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000159#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000160 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000161#include "PassRegistry.def"
162}
163
Chandler Carruth1ff77242015-03-07 09:02:36 +0000164void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000165#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000166 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000167#include "PassRegistry.def"
168}
169
Justin Bognereecc3c82016-02-25 07:23:08 +0000170void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
171#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
172 LAM.registerPass([&] { return CREATE_PASS; });
173#include "PassRegistry.def"
174}
175
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000176void PassBuilder::addPerModuleDefaultPipeline(ModulePassManager &MPM,
177 OptimizationLevel Level,
178 bool DebugLogging) {
179 // FIXME: Finish fleshing this out to match the legacy pipelines.
180 FunctionPassManager EarlyFPM(DebugLogging);
181 EarlyFPM.addPass(SimplifyCFGPass());
182 EarlyFPM.addPass(SROA());
183 EarlyFPM.addPass(EarlyCSEPass());
184 EarlyFPM.addPass(LowerExpectIntrinsicPass());
185
186 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
187}
188
189void PassBuilder::addLTOPreLinkDefaultPipeline(ModulePassManager &MPM,
190 OptimizationLevel Level,
191 bool DebugLogging) {
192 // FIXME: We should use a customized pre-link pipeline!
193 addPerModuleDefaultPipeline(MPM, Level, DebugLogging);
194}
195
196void PassBuilder::addLTODefaultPipeline(ModulePassManager &MPM,
197 OptimizationLevel Level,
198 bool DebugLogging) {
199 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
200 FunctionPassManager LateFPM(DebugLogging);
201 LateFPM.addPass(InstCombinePass());
202 LateFPM.addPass(SimplifyCFGPass());
203
204 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
205}
206
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000207#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000208static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000209 // Manually handle aliases for pre-configured pipeline fragments.
210 if (Name.startswith("default") || Name.startswith("lto"))
211 return DefaultAliasRegex.match(Name);
212
Chandler Carruth58944182014-04-21 08:08:50 +0000213#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000214#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000215 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000216 return true;
217#include "PassRegistry.def"
218
Chandler Carruth66445382014-01-11 08:16:35 +0000219 return false;
220}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000221#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000222
Chandler Carruth572e3402014-04-21 11:12:00 +0000223static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000224#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000225#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000226 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000227 return true;
228#include "PassRegistry.def"
229
Chandler Carruth572e3402014-04-21 11:12:00 +0000230 return false;
231}
232
Chandler Carruthd8330982014-01-12 09:34:22 +0000233static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000234#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000235#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000236 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000237 return true;
238#include "PassRegistry.def"
239
Chandler Carruthd8330982014-01-12 09:34:22 +0000240 return false;
241}
242
Justin Bognereecc3c82016-02-25 07:23:08 +0000243static bool isLoopPassName(StringRef Name) {
244#define LOOP_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
245#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
246 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
247 return true;
248#include "PassRegistry.def"
249
250 return false;
251}
252
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000253bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name,
254 bool DebugLogging) {
255 // Manually handle aliases for pre-configured pipeline fragments.
256 if (Name.startswith("default") || Name.startswith("lto")) {
257 SmallVector<StringRef, 3> Matches;
258 if (!DefaultAliasRegex.match(Name, &Matches))
259 return false;
260 assert(Matches.size() == 3 && "Must capture two matched strings!");
261
262 auto L = StringSwitch<OptimizationLevel>(Matches[2])
263 .Case("O0", O0)
264 .Case("O1", O1)
265 .Case("O2", O2)
266 .Case("O3", O3)
267 .Case("Os", Os)
268 .Case("Oz", Oz);
269
270 if (Matches[1] == "default") {
271 addPerModuleDefaultPipeline(MPM, L, DebugLogging);
272 } else if (Matches[1] == "lto-pre-link") {
273 addLTOPreLinkDefaultPipeline(MPM, L, DebugLogging);
274 } else {
275 assert(Matches[1] == "lto" && "Not one of the matched options!");
276 addLTODefaultPipeline(MPM, L, DebugLogging);
277 }
278 return true;
279 }
280
Chandler Carruth58944182014-04-21 08:08:50 +0000281#define MODULE_PASS(NAME, CREATE_PASS) \
282 if (Name == NAME) { \
283 MPM.addPass(CREATE_PASS); \
284 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000285 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000286#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
287 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000288 MPM.addPass(RequireAnalysisPass< \
289 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000290 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000291 } \
292 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000293 MPM.addPass(InvalidateAnalysisPass< \
294 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000295 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000296 }
297#include "PassRegistry.def"
298
Chandler Carruth66445382014-01-11 08:16:35 +0000299 return false;
300}
301
Chandler Carruth1ff77242015-03-07 09:02:36 +0000302bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000303#define CGSCC_PASS(NAME, CREATE_PASS) \
304 if (Name == NAME) { \
305 CGPM.addPass(CREATE_PASS); \
306 return true; \
307 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000308#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
309 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000310 CGPM.addPass(RequireAnalysisPass< \
311 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000312 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000313 } \
314 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000315 CGPM.addPass(InvalidateAnalysisPass< \
316 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000317 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000318 }
319#include "PassRegistry.def"
320
Chandler Carruth572e3402014-04-21 11:12:00 +0000321 return false;
322}
323
Chandler Carruth1ff77242015-03-07 09:02:36 +0000324bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
325 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000326#define FUNCTION_PASS(NAME, CREATE_PASS) \
327 if (Name == NAME) { \
328 FPM.addPass(CREATE_PASS); \
329 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000330 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000331#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
332 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000333 FPM.addPass(RequireAnalysisPass< \
334 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000335 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000336 } \
337 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000338 FPM.addPass(InvalidateAnalysisPass< \
339 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000340 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000341 }
342#include "PassRegistry.def"
343
Chandler Carruthd8330982014-01-12 09:34:22 +0000344 return false;
345}
346
Justin Bognereecc3c82016-02-25 07:23:08 +0000347bool PassBuilder::parseLoopPassName(LoopPassManager &FPM,
348 StringRef Name) {
349#define LOOP_PASS(NAME, CREATE_PASS) \
350 if (Name == NAME) { \
351 FPM.addPass(CREATE_PASS); \
352 return true; \
353 }
354#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
355 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000356 FPM.addPass(RequireAnalysisPass< \
357 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000358 return true; \
359 } \
360 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000361 FPM.addPass(InvalidateAnalysisPass< \
362 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000363 return true; \
364 }
365#include "PassRegistry.def"
366
367 return false;
368}
369
Chandler Carruthedf59962016-02-18 09:45:17 +0000370bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +0000371#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
372 if (Name == NAME) { \
373 AA.registerModuleAnalysis< \
374 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
375 return true; \
376 }
Chandler Carruthedf59962016-02-18 09:45:17 +0000377#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
378 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000379 AA.registerFunctionAnalysis< \
380 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +0000381 return true; \
382 }
383#include "PassRegistry.def"
384
385 return false;
386}
387
Justin Bognereecc3c82016-02-25 07:23:08 +0000388bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
389 StringRef &PipelineText,
390 bool VerifyEachPass,
391 bool DebugLogging) {
392 for (;;) {
393 // Parse nested pass managers by recursing.
394 if (PipelineText.startswith("loop(")) {
395 LoopPassManager NestedLPM(DebugLogging);
396
397 // Parse the inner pipeline inte the nested manager.
398 PipelineText = PipelineText.substr(strlen("loop("));
399 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
400 DebugLogging) ||
401 PipelineText.empty())
402 return false;
403 assert(PipelineText[0] == ')');
404 PipelineText = PipelineText.substr(1);
405
406 // Add the nested pass manager with the appropriate adaptor.
407 LPM.addPass(std::move(NestedLPM));
408 } else {
409 // Otherwise try to parse a pass name.
410 size_t End = PipelineText.find_first_of(",)");
411 if (!parseLoopPassName(LPM, PipelineText.substr(0, End)))
412 return false;
413 // TODO: Ideally, we would run a LoopVerifierPass() here in the
414 // VerifyEachPass case, but we don't have such a verifier yet.
415
416 PipelineText = PipelineText.substr(End);
417 }
418
419 if (PipelineText.empty() || PipelineText[0] == ')')
420 return true;
421
422 assert(PipelineText[0] == ',');
423 PipelineText = PipelineText.substr(1);
424 }
425}
426
Chandler Carruth1ff77242015-03-07 09:02:36 +0000427bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
428 StringRef &PipelineText,
429 bool VerifyEachPass,
430 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000431 for (;;) {
432 // Parse nested pass managers by recursing.
433 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000434 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000435
436 // Parse the inner pipeline inte the nested manager.
437 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000438 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
439 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000440 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000441 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000442 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000443 PipelineText = PipelineText.substr(1);
444
445 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000446 FPM.addPass(std::move(NestedFPM));
Justin Bognereecc3c82016-02-25 07:23:08 +0000447 } else if (PipelineText.startswith("loop(")) {
448 LoopPassManager NestedLPM(DebugLogging);
449
450 // Parse the inner pipeline inte the nested manager.
451 PipelineText = PipelineText.substr(strlen("loop("));
452 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
453 DebugLogging) ||
454 PipelineText.empty())
455 return false;
456 assert(PipelineText[0] == ')');
457 PipelineText = PipelineText.substr(1);
458
459 // Add the nested pass manager with the appropriate adaptor.
460 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(NestedLPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000461 } else {
462 // Otherwise try to parse a pass name.
463 size_t End = PipelineText.find_first_of(",)");
464 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
465 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000466 if (VerifyEachPass)
467 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000468
469 PipelineText = PipelineText.substr(End);
470 }
471
472 if (PipelineText.empty() || PipelineText[0] == ')')
473 return true;
474
475 assert(PipelineText[0] == ',');
476 PipelineText = PipelineText.substr(1);
477 }
478}
479
Chandler Carruth1ff77242015-03-07 09:02:36 +0000480bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
481 StringRef &PipelineText,
482 bool VerifyEachPass,
483 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000484 for (;;) {
485 // Parse nested pass managers by recursing.
486 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000487 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000488
489 // Parse the inner pipeline into the nested manager.
490 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000491 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
492 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000493 PipelineText.empty())
494 return false;
495 assert(PipelineText[0] == ')');
496 PipelineText = PipelineText.substr(1);
497
498 // Add the nested pass manager with the appropriate adaptor.
499 CGPM.addPass(std::move(NestedCGPM));
500 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000501 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000502
503 // Parse the inner pipeline inte the nested manager.
504 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000505 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
506 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000507 PipelineText.empty())
508 return false;
509 assert(PipelineText[0] == ')');
510 PipelineText = PipelineText.substr(1);
511
512 // Add the nested pass manager with the appropriate adaptor.
513 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
514 } else {
515 // Otherwise try to parse a pass name.
516 size_t End = PipelineText.find_first_of(",)");
517 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
518 return false;
519 // FIXME: No verifier support for CGSCC passes!
520
521 PipelineText = PipelineText.substr(End);
522 }
523
524 if (PipelineText.empty() || PipelineText[0] == ')')
525 return true;
526
527 assert(PipelineText[0] == ',');
528 PipelineText = PipelineText.substr(1);
529 }
530}
531
Chandler Carruth1ff77242015-03-07 09:02:36 +0000532bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
533 StringRef &PipelineText,
534 bool VerifyEachPass,
535 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000536 for (;;) {
537 // Parse nested pass managers by recursing.
538 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000539 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000540
541 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000542 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000543 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
544 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000545 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000546 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000547 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000548 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000549
550 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000551 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000552 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000553 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000554
555 // Parse the inner pipeline inte the nested manager.
556 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000557 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
558 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000559 PipelineText.empty())
560 return false;
561 assert(PipelineText[0] == ')');
562 PipelineText = PipelineText.substr(1);
563
564 // Add the nested pass manager with the appropriate adaptor.
565 MPM.addPass(
566 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000567 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000568 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000569
570 // Parse the inner pipeline inte the nested manager.
571 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000572 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
573 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000574 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000575 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000576 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000577 PipelineText = PipelineText.substr(1);
578
579 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000580 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000581 } else {
582 // Otherwise try to parse a pass name.
583 size_t End = PipelineText.find_first_of(",)");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000584 if (!parseModulePassName(MPM, PipelineText.substr(0, End), DebugLogging))
Chandler Carruth66445382014-01-11 08:16:35 +0000585 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000586 if (VerifyEachPass)
587 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000588
589 PipelineText = PipelineText.substr(End);
590 }
591
592 if (PipelineText.empty() || PipelineText[0] == ')')
593 return true;
594
595 assert(PipelineText[0] == ',');
596 PipelineText = PipelineText.substr(1);
597 }
598}
599
600// Primary pass pipeline description parsing routine.
601// FIXME: Should this routine accept a TargetMachine or require the caller to
602// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000603bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
604 StringRef PipelineText, bool VerifyEachPass,
605 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000606 // By default, try to parse the pipeline as-if it were within an implicit
607 // 'module(...)' pass pipeline. If this will parse at all, it needs to
608 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000609 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000610 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000611
Chandler Carruthea368f12015-01-06 08:37:58 +0000612 // This isn't parsable as a module pipeline, look for the end of a pass name
613 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000614 StringRef FirstName =
615 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000616 assert(!isModulePassName(FirstName) &&
617 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000618
Chandler Carruthea368f12015-01-06 08:37:58 +0000619 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
620 // pipeline.
Justin Bognereecc3c82016-02-25 07:23:08 +0000621 if (PipelineText.startswith("cgscc(") || isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000622 CGSCCPassManager CGPM(DebugLogging);
623 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
624 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000625 !PipelineText.empty())
626 return false;
627 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
628 return true;
629 }
630
Chandler Carruthea368f12015-01-06 08:37:58 +0000631 // Similarly, if this looks like a Function pass, parse the whole thing as
632 // a Function pipelien.
Justin Bognereecc3c82016-02-25 07:23:08 +0000633 if (PipelineText.startswith("function(") || isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000634 FunctionPassManager FPM(DebugLogging);
635 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
636 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000637 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000638 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000639 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000640 return true;
641 }
Chandler Carruth66445382014-01-11 08:16:35 +0000642
Justin Bognereecc3c82016-02-25 07:23:08 +0000643 // If this looks like a Loop pass, parse the whole thing as a Loop pipeline.
644 if (PipelineText.startswith("loop(") || isLoopPassName(FirstName)) {
645 LoopPassManager LPM(DebugLogging);
646 if (!parseLoopPassPipeline(LPM, PipelineText, VerifyEachPass,
647 DebugLogging) ||
648 !PipelineText.empty())
649 return false;
650 FunctionPassManager FPM(DebugLogging);
651 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
652 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
653 return true;
654 }
655
656
Chandler Carruth66445382014-01-11 08:16:35 +0000657 return false;
658}
Chandler Carruthedf59962016-02-18 09:45:17 +0000659
660bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
661 while (!PipelineText.empty()) {
662 StringRef Name;
663 std::tie(Name, PipelineText) = PipelineText.split(',');
664 if (!parseAAPassName(AA, Name))
665 return false;
666 }
667
668 return true;
669}