blob: dbe655aca14b65d1963358a2bdeaabdf0d8a8422 [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"
Davide Italiano164b9bc2016-05-05 00:51:09 +000048#include "llvm/Transforms/IPO/ConstantMerge.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000049#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000050#include "llvm/Transforms/IPO/FunctionAttrs.h"
Davide Italiano66228c42016-05-03 19:39:15 +000051#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000052#include "llvm/Transforms/IPO/GlobalOpt.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000053#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Justin Bogner4563a062016-04-26 20:15:52 +000054#include "llvm/Transforms/IPO/Internalize.h"
Justin Bogner21e15372015-10-30 23:28:12 +000055#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000056#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000057#include "llvm/Transforms/InstrProfiling.h"
Justin Bogner19b67992015-10-30 23:13:18 +000058#include "llvm/Transforms/Scalar/ADCE.h"
Justin Bogner395c2122016-04-22 19:40:41 +000059#include "llvm/Transforms/Scalar/DCE.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000060#include "llvm/Transforms/Scalar/EarlyCSE.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000061#include "llvm/Transforms/Scalar/GVN.h"
Justin Bognerd0d23412016-05-03 22:02:31 +000062#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +000063#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000064#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +000065#include "llvm/Transforms/Scalar/Reassociate.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000066#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000067#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +000068#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruth58dde8c2016-02-26 12:17:54 +000069#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +000070
71using namespace llvm;
72
Chandler Carruth8b5a74192016-02-28 22:16:03 +000073static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
74
Chandler Carruth66445382014-01-11 08:16:35 +000075namespace {
76
Chandler Carruthd8330982014-01-12 09:34:22 +000077/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000078struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000079 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000080 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000081};
82
Chandler Carruth0b576b32015-01-06 02:50:06 +000083/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +000084class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
85 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +000086 static char PassID;
87
88public:
Chandler Carruth0b576b32015-01-06 02:50:06 +000089 struct Result {};
90 Result run(Module &) { return Result(); }
91 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +000092};
93
Chandler Carruth572e3402014-04-21 11:12:00 +000094/// \brief No-op CGSCC pass which does nothing.
95struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000096 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +000097 return PreservedAnalyses::all();
98 }
99 static StringRef name() { return "NoOpCGSCCPass"; }
100};
101
Chandler Carruth0b576b32015-01-06 02:50:06 +0000102/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000103class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
104 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000105 static char PassID;
106
107public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000108 struct Result {};
109 Result run(LazyCallGraph::SCC &) { return Result(); }
110 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000111};
112
Chandler Carruthd8330982014-01-12 09:34:22 +0000113/// \brief No-op function pass which does nothing.
114struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000115 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +0000116 static StringRef name() { return "NoOpFunctionPass"; }
117};
118
Chandler Carruth0b576b32015-01-06 02:50:06 +0000119/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000120class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
121 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000122 static char PassID;
123
124public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000125 struct Result {};
126 Result run(Function &) { return Result(); }
127 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000128};
129
Justin Bognereecc3c82016-02-25 07:23:08 +0000130/// \brief No-op loop pass which does nothing.
131struct NoOpLoopPass {
132 PreservedAnalyses run(Loop &L) { return PreservedAnalyses::all(); }
133 static StringRef name() { return "NoOpLoopPass"; }
134};
135
136/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000137class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
138 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000139 static char PassID;
140
141public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000142 struct Result {};
143 Result run(Loop &) { return Result(); }
144 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000145};
146
Chandler Carruthb4faf132016-03-11 10:22:49 +0000147char NoOpModuleAnalysis::PassID;
148char NoOpCGSCCAnalysis::PassID;
149char NoOpFunctionAnalysis::PassID;
150char NoOpLoopAnalysis::PassID;
151
Chandler Carruth66445382014-01-11 08:16:35 +0000152} // End anonymous namespace.
153
Chandler Carruth1ff77242015-03-07 09:02:36 +0000154void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000155#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000156 MAM.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::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000161#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000162 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000163#include "PassRegistry.def"
164}
165
Chandler Carruth1ff77242015-03-07 09:02:36 +0000166void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000167#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000168 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000169#include "PassRegistry.def"
170}
171
Justin Bognereecc3c82016-02-25 07:23:08 +0000172void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
173#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
174 LAM.registerPass([&] { return CREATE_PASS; });
175#include "PassRegistry.def"
176}
177
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000178void PassBuilder::addPerModuleDefaultPipeline(ModulePassManager &MPM,
179 OptimizationLevel Level,
180 bool DebugLogging) {
181 // FIXME: Finish fleshing this out to match the legacy pipelines.
182 FunctionPassManager EarlyFPM(DebugLogging);
183 EarlyFPM.addPass(SimplifyCFGPass());
184 EarlyFPM.addPass(SROA());
185 EarlyFPM.addPass(EarlyCSEPass());
186 EarlyFPM.addPass(LowerExpectIntrinsicPass());
187
188 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
189}
190
191void PassBuilder::addLTOPreLinkDefaultPipeline(ModulePassManager &MPM,
192 OptimizationLevel Level,
193 bool DebugLogging) {
194 // FIXME: We should use a customized pre-link pipeline!
195 addPerModuleDefaultPipeline(MPM, Level, DebugLogging);
196}
197
198void PassBuilder::addLTODefaultPipeline(ModulePassManager &MPM,
199 OptimizationLevel Level,
200 bool DebugLogging) {
201 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
202 FunctionPassManager LateFPM(DebugLogging);
203 LateFPM.addPass(InstCombinePass());
204 LateFPM.addPass(SimplifyCFGPass());
205
206 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
207}
208
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000209#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000210static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000211 // Manually handle aliases for pre-configured pipeline fragments.
212 if (Name.startswith("default") || Name.startswith("lto"))
213 return DefaultAliasRegex.match(Name);
214
Chandler Carruth58944182014-04-21 08:08:50 +0000215#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000216#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000217 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000218 return true;
219#include "PassRegistry.def"
220
Chandler Carruth66445382014-01-11 08:16:35 +0000221 return false;
222}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000223#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000224
Chandler Carruth572e3402014-04-21 11:12:00 +0000225static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000226#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000227#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000228 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000229 return true;
230#include "PassRegistry.def"
231
Chandler Carruth572e3402014-04-21 11:12:00 +0000232 return false;
233}
234
Chandler Carruthd8330982014-01-12 09:34:22 +0000235static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000236#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000237#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000238 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000239 return true;
240#include "PassRegistry.def"
241
Chandler Carruthd8330982014-01-12 09:34:22 +0000242 return false;
243}
244
Justin Bognereecc3c82016-02-25 07:23:08 +0000245static bool isLoopPassName(StringRef Name) {
246#define LOOP_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
247#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
248 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
249 return true;
250#include "PassRegistry.def"
251
252 return false;
253}
254
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000255bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name,
256 bool DebugLogging) {
257 // Manually handle aliases for pre-configured pipeline fragments.
258 if (Name.startswith("default") || Name.startswith("lto")) {
259 SmallVector<StringRef, 3> Matches;
260 if (!DefaultAliasRegex.match(Name, &Matches))
261 return false;
262 assert(Matches.size() == 3 && "Must capture two matched strings!");
263
264 auto L = StringSwitch<OptimizationLevel>(Matches[2])
265 .Case("O0", O0)
266 .Case("O1", O1)
267 .Case("O2", O2)
268 .Case("O3", O3)
269 .Case("Os", Os)
270 .Case("Oz", Oz);
271
272 if (Matches[1] == "default") {
273 addPerModuleDefaultPipeline(MPM, L, DebugLogging);
274 } else if (Matches[1] == "lto-pre-link") {
275 addLTOPreLinkDefaultPipeline(MPM, L, DebugLogging);
276 } else {
277 assert(Matches[1] == "lto" && "Not one of the matched options!");
278 addLTODefaultPipeline(MPM, L, DebugLogging);
279 }
280 return true;
281 }
282
Chandler Carruth58944182014-04-21 08:08:50 +0000283#define MODULE_PASS(NAME, CREATE_PASS) \
284 if (Name == NAME) { \
285 MPM.addPass(CREATE_PASS); \
286 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000287 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000288#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
289 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000290 MPM.addPass(RequireAnalysisPass< \
291 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000292 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000293 } \
294 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000295 MPM.addPass(InvalidateAnalysisPass< \
296 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000297 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000298 }
299#include "PassRegistry.def"
300
Chandler Carruth66445382014-01-11 08:16:35 +0000301 return false;
302}
303
Chandler Carruth1ff77242015-03-07 09:02:36 +0000304bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000305#define CGSCC_PASS(NAME, CREATE_PASS) \
306 if (Name == NAME) { \
307 CGPM.addPass(CREATE_PASS); \
308 return true; \
309 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000310#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
311 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000312 CGPM.addPass(RequireAnalysisPass< \
313 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000314 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000315 } \
316 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000317 CGPM.addPass(InvalidateAnalysisPass< \
318 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000319 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000320 }
321#include "PassRegistry.def"
322
Chandler Carruth572e3402014-04-21 11:12:00 +0000323 return false;
324}
325
Chandler Carruth1ff77242015-03-07 09:02:36 +0000326bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
327 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000328#define FUNCTION_PASS(NAME, CREATE_PASS) \
329 if (Name == NAME) { \
330 FPM.addPass(CREATE_PASS); \
331 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000332 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000333#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
334 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000335 FPM.addPass(RequireAnalysisPass< \
336 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000337 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000338 } \
339 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000340 FPM.addPass(InvalidateAnalysisPass< \
341 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000342 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000343 }
344#include "PassRegistry.def"
345
Chandler Carruthd8330982014-01-12 09:34:22 +0000346 return false;
347}
348
Justin Bognereecc3c82016-02-25 07:23:08 +0000349bool PassBuilder::parseLoopPassName(LoopPassManager &FPM,
350 StringRef Name) {
351#define LOOP_PASS(NAME, CREATE_PASS) \
352 if (Name == NAME) { \
353 FPM.addPass(CREATE_PASS); \
354 return true; \
355 }
356#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
357 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000358 FPM.addPass(RequireAnalysisPass< \
359 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000360 return true; \
361 } \
362 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000363 FPM.addPass(InvalidateAnalysisPass< \
364 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000365 return true; \
366 }
367#include "PassRegistry.def"
368
369 return false;
370}
371
Chandler Carruthedf59962016-02-18 09:45:17 +0000372bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +0000373#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
374 if (Name == NAME) { \
375 AA.registerModuleAnalysis< \
376 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
377 return true; \
378 }
Chandler Carruthedf59962016-02-18 09:45:17 +0000379#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
380 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000381 AA.registerFunctionAnalysis< \
382 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +0000383 return true; \
384 }
385#include "PassRegistry.def"
386
387 return false;
388}
389
Justin Bognereecc3c82016-02-25 07:23:08 +0000390bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
391 StringRef &PipelineText,
392 bool VerifyEachPass,
393 bool DebugLogging) {
394 for (;;) {
395 // Parse nested pass managers by recursing.
396 if (PipelineText.startswith("loop(")) {
397 LoopPassManager NestedLPM(DebugLogging);
398
399 // Parse the inner pipeline inte the nested manager.
400 PipelineText = PipelineText.substr(strlen("loop("));
401 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
402 DebugLogging) ||
403 PipelineText.empty())
404 return false;
405 assert(PipelineText[0] == ')');
406 PipelineText = PipelineText.substr(1);
407
408 // Add the nested pass manager with the appropriate adaptor.
409 LPM.addPass(std::move(NestedLPM));
410 } else {
411 // Otherwise try to parse a pass name.
412 size_t End = PipelineText.find_first_of(",)");
413 if (!parseLoopPassName(LPM, PipelineText.substr(0, End)))
414 return false;
415 // TODO: Ideally, we would run a LoopVerifierPass() here in the
416 // VerifyEachPass case, but we don't have such a verifier yet.
417
418 PipelineText = PipelineText.substr(End);
419 }
420
421 if (PipelineText.empty() || PipelineText[0] == ')')
422 return true;
423
424 assert(PipelineText[0] == ',');
425 PipelineText = PipelineText.substr(1);
426 }
427}
428
Chandler Carruth1ff77242015-03-07 09:02:36 +0000429bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
430 StringRef &PipelineText,
431 bool VerifyEachPass,
432 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000433 for (;;) {
434 // Parse nested pass managers by recursing.
435 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000436 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000437
438 // Parse the inner pipeline inte the nested manager.
439 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000440 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
441 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000442 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000443 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000444 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000445 PipelineText = PipelineText.substr(1);
446
447 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000448 FPM.addPass(std::move(NestedFPM));
Justin Bognereecc3c82016-02-25 07:23:08 +0000449 } else if (PipelineText.startswith("loop(")) {
450 LoopPassManager NestedLPM(DebugLogging);
451
452 // Parse the inner pipeline inte the nested manager.
453 PipelineText = PipelineText.substr(strlen("loop("));
454 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
455 DebugLogging) ||
456 PipelineText.empty())
457 return false;
458 assert(PipelineText[0] == ')');
459 PipelineText = PipelineText.substr(1);
460
461 // Add the nested pass manager with the appropriate adaptor.
462 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(NestedLPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000463 } else {
464 // Otherwise try to parse a pass name.
465 size_t End = PipelineText.find_first_of(",)");
466 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
467 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000468 if (VerifyEachPass)
469 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000470
471 PipelineText = PipelineText.substr(End);
472 }
473
474 if (PipelineText.empty() || PipelineText[0] == ')')
475 return true;
476
477 assert(PipelineText[0] == ',');
478 PipelineText = PipelineText.substr(1);
479 }
480}
481
Chandler Carruth1ff77242015-03-07 09:02:36 +0000482bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
483 StringRef &PipelineText,
484 bool VerifyEachPass,
485 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000486 for (;;) {
487 // Parse nested pass managers by recursing.
488 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000489 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000490
491 // Parse the inner pipeline into the nested manager.
492 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000493 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
494 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000495 PipelineText.empty())
496 return false;
497 assert(PipelineText[0] == ')');
498 PipelineText = PipelineText.substr(1);
499
500 // Add the nested pass manager with the appropriate adaptor.
501 CGPM.addPass(std::move(NestedCGPM));
502 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000503 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000504
505 // Parse the inner pipeline inte the nested manager.
506 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000507 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
508 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000509 PipelineText.empty())
510 return false;
511 assert(PipelineText[0] == ')');
512 PipelineText = PipelineText.substr(1);
513
514 // Add the nested pass manager with the appropriate adaptor.
515 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
516 } else {
517 // Otherwise try to parse a pass name.
518 size_t End = PipelineText.find_first_of(",)");
519 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
520 return false;
521 // FIXME: No verifier support for CGSCC passes!
522
523 PipelineText = PipelineText.substr(End);
524 }
525
526 if (PipelineText.empty() || PipelineText[0] == ')')
527 return true;
528
529 assert(PipelineText[0] == ',');
530 PipelineText = PipelineText.substr(1);
531 }
532}
533
Chandler Carruth1ff77242015-03-07 09:02:36 +0000534bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
535 StringRef &PipelineText,
536 bool VerifyEachPass,
537 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000538 for (;;) {
539 // Parse nested pass managers by recursing.
540 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000541 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000542
543 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000544 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000545 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
546 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000547 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000548 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000549 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000550 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000551
552 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000553 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000554 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000555 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000556
557 // Parse the inner pipeline inte the nested manager.
558 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000559 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
560 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000561 PipelineText.empty())
562 return false;
563 assert(PipelineText[0] == ')');
564 PipelineText = PipelineText.substr(1);
565
566 // Add the nested pass manager with the appropriate adaptor.
567 MPM.addPass(
568 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000569 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000570 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000571
572 // Parse the inner pipeline inte the nested manager.
573 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000574 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
575 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000576 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000577 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000578 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000579 PipelineText = PipelineText.substr(1);
580
581 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000582 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000583 } else {
584 // Otherwise try to parse a pass name.
585 size_t End = PipelineText.find_first_of(",)");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000586 if (!parseModulePassName(MPM, PipelineText.substr(0, End), DebugLogging))
Chandler Carruth66445382014-01-11 08:16:35 +0000587 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000588 if (VerifyEachPass)
589 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000590
591 PipelineText = PipelineText.substr(End);
592 }
593
594 if (PipelineText.empty() || PipelineText[0] == ')')
595 return true;
596
597 assert(PipelineText[0] == ',');
598 PipelineText = PipelineText.substr(1);
599 }
600}
601
602// Primary pass pipeline description parsing routine.
603// FIXME: Should this routine accept a TargetMachine or require the caller to
604// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000605bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
606 StringRef PipelineText, bool VerifyEachPass,
607 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000608 // By default, try to parse the pipeline as-if it were within an implicit
609 // 'module(...)' pass pipeline. If this will parse at all, it needs to
610 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000611 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000612 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000613
Chandler Carruthea368f12015-01-06 08:37:58 +0000614 // This isn't parsable as a module pipeline, look for the end of a pass name
615 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000616 StringRef FirstName =
617 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000618 assert(!isModulePassName(FirstName) &&
619 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000620
Chandler Carruthea368f12015-01-06 08:37:58 +0000621 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
622 // pipeline.
Justin Bognereecc3c82016-02-25 07:23:08 +0000623 if (PipelineText.startswith("cgscc(") || isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000624 CGSCCPassManager CGPM(DebugLogging);
625 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
626 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000627 !PipelineText.empty())
628 return false;
629 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
630 return true;
631 }
632
Chandler Carruthea368f12015-01-06 08:37:58 +0000633 // Similarly, if this looks like a Function pass, parse the whole thing as
634 // a Function pipelien.
Justin Bognereecc3c82016-02-25 07:23:08 +0000635 if (PipelineText.startswith("function(") || isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000636 FunctionPassManager FPM(DebugLogging);
637 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
638 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000639 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000640 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000641 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000642 return true;
643 }
Chandler Carruth66445382014-01-11 08:16:35 +0000644
Justin Bognereecc3c82016-02-25 07:23:08 +0000645 // If this looks like a Loop pass, parse the whole thing as a Loop pipeline.
646 if (PipelineText.startswith("loop(") || isLoopPassName(FirstName)) {
647 LoopPassManager LPM(DebugLogging);
648 if (!parseLoopPassPipeline(LPM, PipelineText, VerifyEachPass,
649 DebugLogging) ||
650 !PipelineText.empty())
651 return false;
652 FunctionPassManager FPM(DebugLogging);
653 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
654 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
655 return true;
656 }
657
658
Chandler Carruth66445382014-01-11 08:16:35 +0000659 return false;
660}
Chandler Carruthedf59962016-02-18 09:45:17 +0000661
662bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
663 while (!PipelineText.empty()) {
664 StringRef Name;
665 std::tie(Name, PipelineText) = PipelineText.split(',');
666 if (!parseAAPassName(AA, Name))
667 return false;
668 }
669
670 return true;
671}