blob: bc03cc1059ac37fe611efe344d6862cd0c13f681 [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"
Xinliang David Li6e5dd412016-05-05 02:59:57 +000024#include "llvm/Analysis/BranchProbabilityInfo.h"
Chandler Carruth342c6712016-02-20 03:52:02 +000025#include "llvm/Analysis/CFLAliasAnalysis.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000026#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruth4c660f72016-03-10 11:24:11 +000027#include "llvm/Analysis/CallGraph.h"
Michael Kupersteinde16b442016-04-18 23:55:01 +000028#include "llvm/Analysis/DemandedBits.h"
Hongbin Zheng751337f2016-02-25 17:54:15 +000029#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruth45a9c202016-03-11 09:15:11 +000030#include "llvm/Analysis/GlobalsModRef.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000031#include "llvm/Analysis/LazyCallGraph.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000032#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth61440d22016-03-10 00:55:30 +000033#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000034#include "llvm/Analysis/PostDominators.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000035#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000036#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000037#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000038#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000039#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000040#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000041#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000042#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000043#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000044#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000045#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000046#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000047#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000048#include "llvm/Target/TargetMachine.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000049#include "llvm/Transforms/IPO/ConstantMerge.h"
Davide Italiano344e8382016-05-05 02:37:32 +000050#include "llvm/Transforms/IPO/ElimAvailExtern.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000051#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000052#include "llvm/Transforms/IPO/FunctionAttrs.h"
Davide Italiano66228c42016-05-03 19:39:15 +000053#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000054#include "llvm/Transforms/IPO/GlobalOpt.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000055#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Justin Bogner4563a062016-04-26 20:15:52 +000056#include "llvm/Transforms/IPO/Internalize.h"
Justin Bogner21e15372015-10-30 23:28:12 +000057#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000058#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000059#include "llvm/Transforms/InstrProfiling.h"
Justin Bogner19b67992015-10-30 23:13:18 +000060#include "llvm/Transforms/Scalar/ADCE.h"
Justin Bogner395c2122016-04-22 19:40:41 +000061#include "llvm/Transforms/Scalar/DCE.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000062#include "llvm/Transforms/Scalar/EarlyCSE.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000063#include "llvm/Transforms/Scalar/GVN.h"
Justin Bognerd0d23412016-05-03 22:02:31 +000064#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +000065#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000066#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +000067#include "llvm/Transforms/Scalar/Reassociate.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000068#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000069#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +000070#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruth58dde8c2016-02-26 12:17:54 +000071#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +000072
73using namespace llvm;
74
Chandler Carruth8b5a74192016-02-28 22:16:03 +000075static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
76
Chandler Carruth66445382014-01-11 08:16:35 +000077namespace {
78
Chandler Carruthd8330982014-01-12 09:34:22 +000079/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000080struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000081 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000082 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000083};
84
Chandler Carruth0b576b32015-01-06 02:50:06 +000085/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +000086class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
87 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +000088 static char PassID;
89
90public:
Chandler Carruth0b576b32015-01-06 02:50:06 +000091 struct Result {};
92 Result run(Module &) { return Result(); }
93 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +000094};
95
Chandler Carruth572e3402014-04-21 11:12:00 +000096/// \brief No-op CGSCC pass which does nothing.
97struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000098 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +000099 return PreservedAnalyses::all();
100 }
101 static StringRef name() { return "NoOpCGSCCPass"; }
102};
103
Chandler Carruth0b576b32015-01-06 02:50:06 +0000104/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000105class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
106 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000107 static char PassID;
108
109public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000110 struct Result {};
111 Result run(LazyCallGraph::SCC &) { return Result(); }
112 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000113};
114
Chandler Carruthd8330982014-01-12 09:34:22 +0000115/// \brief No-op function pass which does nothing.
116struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000117 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +0000118 static StringRef name() { return "NoOpFunctionPass"; }
119};
120
Chandler Carruth0b576b32015-01-06 02:50:06 +0000121/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000122class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
123 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000124 static char PassID;
125
126public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000127 struct Result {};
128 Result run(Function &) { return Result(); }
129 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000130};
131
Justin Bognereecc3c82016-02-25 07:23:08 +0000132/// \brief No-op loop pass which does nothing.
133struct NoOpLoopPass {
134 PreservedAnalyses run(Loop &L) { return PreservedAnalyses::all(); }
135 static StringRef name() { return "NoOpLoopPass"; }
136};
137
138/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000139class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
140 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000141 static char PassID;
142
143public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000144 struct Result {};
145 Result run(Loop &) { return Result(); }
146 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000147};
148
Chandler Carruthb4faf132016-03-11 10:22:49 +0000149char NoOpModuleAnalysis::PassID;
150char NoOpCGSCCAnalysis::PassID;
151char NoOpFunctionAnalysis::PassID;
152char NoOpLoopAnalysis::PassID;
153
Chandler Carruth66445382014-01-11 08:16:35 +0000154} // End anonymous namespace.
155
Chandler Carruth1ff77242015-03-07 09:02:36 +0000156void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000157#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000158 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000159#include "PassRegistry.def"
160}
161
Chandler Carruth1ff77242015-03-07 09:02:36 +0000162void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000163#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000164 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000165#include "PassRegistry.def"
166}
167
Chandler Carruth1ff77242015-03-07 09:02:36 +0000168void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000169#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000170 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000171#include "PassRegistry.def"
172}
173
Justin Bognereecc3c82016-02-25 07:23:08 +0000174void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
175#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
176 LAM.registerPass([&] { return CREATE_PASS; });
177#include "PassRegistry.def"
178}
179
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000180void PassBuilder::addPerModuleDefaultPipeline(ModulePassManager &MPM,
181 OptimizationLevel Level,
182 bool DebugLogging) {
183 // FIXME: Finish fleshing this out to match the legacy pipelines.
184 FunctionPassManager EarlyFPM(DebugLogging);
185 EarlyFPM.addPass(SimplifyCFGPass());
186 EarlyFPM.addPass(SROA());
187 EarlyFPM.addPass(EarlyCSEPass());
188 EarlyFPM.addPass(LowerExpectIntrinsicPass());
189
190 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
191}
192
193void PassBuilder::addLTOPreLinkDefaultPipeline(ModulePassManager &MPM,
194 OptimizationLevel Level,
195 bool DebugLogging) {
196 // FIXME: We should use a customized pre-link pipeline!
197 addPerModuleDefaultPipeline(MPM, Level, DebugLogging);
198}
199
200void PassBuilder::addLTODefaultPipeline(ModulePassManager &MPM,
201 OptimizationLevel Level,
202 bool DebugLogging) {
203 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
204 FunctionPassManager LateFPM(DebugLogging);
205 LateFPM.addPass(InstCombinePass());
206 LateFPM.addPass(SimplifyCFGPass());
207
208 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
209}
210
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000211#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000212static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000213 // Manually handle aliases for pre-configured pipeline fragments.
214 if (Name.startswith("default") || Name.startswith("lto"))
215 return DefaultAliasRegex.match(Name);
216
Chandler Carruth58944182014-04-21 08:08:50 +0000217#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000218#define MODULE_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 Carruth66445382014-01-11 08:16:35 +0000223 return false;
224}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000225#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000226
Chandler Carruth572e3402014-04-21 11:12:00 +0000227static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000228#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000229#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000230 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000231 return true;
232#include "PassRegistry.def"
233
Chandler Carruth572e3402014-04-21 11:12:00 +0000234 return false;
235}
236
Chandler Carruthd8330982014-01-12 09:34:22 +0000237static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000238#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000239#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000240 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000241 return true;
242#include "PassRegistry.def"
243
Chandler Carruthd8330982014-01-12 09:34:22 +0000244 return false;
245}
246
Justin Bognereecc3c82016-02-25 07:23:08 +0000247static bool isLoopPassName(StringRef Name) {
248#define LOOP_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
249#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
250 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
251 return true;
252#include "PassRegistry.def"
253
254 return false;
255}
256
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000257bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name,
258 bool DebugLogging) {
259 // Manually handle aliases for pre-configured pipeline fragments.
260 if (Name.startswith("default") || Name.startswith("lto")) {
261 SmallVector<StringRef, 3> Matches;
262 if (!DefaultAliasRegex.match(Name, &Matches))
263 return false;
264 assert(Matches.size() == 3 && "Must capture two matched strings!");
265
266 auto L = StringSwitch<OptimizationLevel>(Matches[2])
267 .Case("O0", O0)
268 .Case("O1", O1)
269 .Case("O2", O2)
270 .Case("O3", O3)
271 .Case("Os", Os)
272 .Case("Oz", Oz);
273
274 if (Matches[1] == "default") {
275 addPerModuleDefaultPipeline(MPM, L, DebugLogging);
276 } else if (Matches[1] == "lto-pre-link") {
277 addLTOPreLinkDefaultPipeline(MPM, L, DebugLogging);
278 } else {
279 assert(Matches[1] == "lto" && "Not one of the matched options!");
280 addLTODefaultPipeline(MPM, L, DebugLogging);
281 }
282 return true;
283 }
284
Chandler Carruth58944182014-04-21 08:08:50 +0000285#define MODULE_PASS(NAME, CREATE_PASS) \
286 if (Name == NAME) { \
287 MPM.addPass(CREATE_PASS); \
288 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000289 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000290#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
291 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000292 MPM.addPass(RequireAnalysisPass< \
293 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000294 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000295 } \
296 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000297 MPM.addPass(InvalidateAnalysisPass< \
298 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000299 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000300 }
301#include "PassRegistry.def"
302
Chandler Carruth66445382014-01-11 08:16:35 +0000303 return false;
304}
305
Chandler Carruth1ff77242015-03-07 09:02:36 +0000306bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000307#define CGSCC_PASS(NAME, CREATE_PASS) \
308 if (Name == NAME) { \
309 CGPM.addPass(CREATE_PASS); \
310 return true; \
311 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000312#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
313 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000314 CGPM.addPass(RequireAnalysisPass< \
315 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000316 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000317 } \
318 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000319 CGPM.addPass(InvalidateAnalysisPass< \
320 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000321 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000322 }
323#include "PassRegistry.def"
324
Chandler Carruth572e3402014-04-21 11:12:00 +0000325 return false;
326}
327
Chandler Carruth1ff77242015-03-07 09:02:36 +0000328bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
329 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000330#define FUNCTION_PASS(NAME, CREATE_PASS) \
331 if (Name == NAME) { \
332 FPM.addPass(CREATE_PASS); \
333 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000334 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000335#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
336 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000337 FPM.addPass(RequireAnalysisPass< \
338 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000339 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000340 } \
341 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000342 FPM.addPass(InvalidateAnalysisPass< \
343 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000344 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000345 }
346#include "PassRegistry.def"
347
Chandler Carruthd8330982014-01-12 09:34:22 +0000348 return false;
349}
350
Justin Bognereecc3c82016-02-25 07:23:08 +0000351bool PassBuilder::parseLoopPassName(LoopPassManager &FPM,
352 StringRef Name) {
353#define LOOP_PASS(NAME, CREATE_PASS) \
354 if (Name == NAME) { \
355 FPM.addPass(CREATE_PASS); \
356 return true; \
357 }
358#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
359 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000360 FPM.addPass(RequireAnalysisPass< \
361 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000362 return true; \
363 } \
364 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000365 FPM.addPass(InvalidateAnalysisPass< \
366 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000367 return true; \
368 }
369#include "PassRegistry.def"
370
371 return false;
372}
373
Chandler Carruthedf59962016-02-18 09:45:17 +0000374bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +0000375#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
376 if (Name == NAME) { \
377 AA.registerModuleAnalysis< \
378 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
379 return true; \
380 }
Chandler Carruthedf59962016-02-18 09:45:17 +0000381#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
382 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000383 AA.registerFunctionAnalysis< \
384 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +0000385 return true; \
386 }
387#include "PassRegistry.def"
388
389 return false;
390}
391
Justin Bognereecc3c82016-02-25 07:23:08 +0000392bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
393 StringRef &PipelineText,
394 bool VerifyEachPass,
395 bool DebugLogging) {
396 for (;;) {
397 // Parse nested pass managers by recursing.
398 if (PipelineText.startswith("loop(")) {
399 LoopPassManager NestedLPM(DebugLogging);
400
401 // Parse the inner pipeline inte the nested manager.
402 PipelineText = PipelineText.substr(strlen("loop("));
403 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
404 DebugLogging) ||
405 PipelineText.empty())
406 return false;
407 assert(PipelineText[0] == ')');
408 PipelineText = PipelineText.substr(1);
409
410 // Add the nested pass manager with the appropriate adaptor.
411 LPM.addPass(std::move(NestedLPM));
412 } else {
413 // Otherwise try to parse a pass name.
414 size_t End = PipelineText.find_first_of(",)");
415 if (!parseLoopPassName(LPM, PipelineText.substr(0, End)))
416 return false;
417 // TODO: Ideally, we would run a LoopVerifierPass() here in the
418 // VerifyEachPass case, but we don't have such a verifier yet.
419
420 PipelineText = PipelineText.substr(End);
421 }
422
423 if (PipelineText.empty() || PipelineText[0] == ')')
424 return true;
425
426 assert(PipelineText[0] == ',');
427 PipelineText = PipelineText.substr(1);
428 }
429}
430
Chandler Carruth1ff77242015-03-07 09:02:36 +0000431bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
432 StringRef &PipelineText,
433 bool VerifyEachPass,
434 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000435 for (;;) {
436 // Parse nested pass managers by recursing.
437 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000438 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000439
440 // Parse the inner pipeline inte the nested manager.
441 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000442 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
443 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000444 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000445 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000446 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000447 PipelineText = PipelineText.substr(1);
448
449 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000450 FPM.addPass(std::move(NestedFPM));
Justin Bognereecc3c82016-02-25 07:23:08 +0000451 } else if (PipelineText.startswith("loop(")) {
452 LoopPassManager NestedLPM(DebugLogging);
453
454 // Parse the inner pipeline inte the nested manager.
455 PipelineText = PipelineText.substr(strlen("loop("));
456 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
457 DebugLogging) ||
458 PipelineText.empty())
459 return false;
460 assert(PipelineText[0] == ')');
461 PipelineText = PipelineText.substr(1);
462
463 // Add the nested pass manager with the appropriate adaptor.
464 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(NestedLPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000465 } else {
466 // Otherwise try to parse a pass name.
467 size_t End = PipelineText.find_first_of(",)");
468 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
469 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000470 if (VerifyEachPass)
471 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000472
473 PipelineText = PipelineText.substr(End);
474 }
475
476 if (PipelineText.empty() || PipelineText[0] == ')')
477 return true;
478
479 assert(PipelineText[0] == ',');
480 PipelineText = PipelineText.substr(1);
481 }
482}
483
Chandler Carruth1ff77242015-03-07 09:02:36 +0000484bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
485 StringRef &PipelineText,
486 bool VerifyEachPass,
487 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000488 for (;;) {
489 // Parse nested pass managers by recursing.
490 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000491 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000492
493 // Parse the inner pipeline into the nested manager.
494 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000495 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
496 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000497 PipelineText.empty())
498 return false;
499 assert(PipelineText[0] == ')');
500 PipelineText = PipelineText.substr(1);
501
502 // Add the nested pass manager with the appropriate adaptor.
503 CGPM.addPass(std::move(NestedCGPM));
504 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000505 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000506
507 // Parse the inner pipeline inte the nested manager.
508 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000509 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
510 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000511 PipelineText.empty())
512 return false;
513 assert(PipelineText[0] == ')');
514 PipelineText = PipelineText.substr(1);
515
516 // Add the nested pass manager with the appropriate adaptor.
517 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
518 } else {
519 // Otherwise try to parse a pass name.
520 size_t End = PipelineText.find_first_of(",)");
521 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
522 return false;
523 // FIXME: No verifier support for CGSCC passes!
524
525 PipelineText = PipelineText.substr(End);
526 }
527
528 if (PipelineText.empty() || PipelineText[0] == ')')
529 return true;
530
531 assert(PipelineText[0] == ',');
532 PipelineText = PipelineText.substr(1);
533 }
534}
535
Chandler Carruth1ff77242015-03-07 09:02:36 +0000536bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
537 StringRef &PipelineText,
538 bool VerifyEachPass,
539 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000540 for (;;) {
541 // Parse nested pass managers by recursing.
542 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000543 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000544
545 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000546 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000547 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
548 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000549 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000550 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000551 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000552 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000553
554 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000555 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000556 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000557 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000558
559 // Parse the inner pipeline inte the nested manager.
560 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000561 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
562 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000563 PipelineText.empty())
564 return false;
565 assert(PipelineText[0] == ')');
566 PipelineText = PipelineText.substr(1);
567
568 // Add the nested pass manager with the appropriate adaptor.
569 MPM.addPass(
570 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000571 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000572 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000573
574 // Parse the inner pipeline inte the nested manager.
575 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000576 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
577 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000578 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000579 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000580 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000581 PipelineText = PipelineText.substr(1);
582
583 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000584 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000585 } else {
586 // Otherwise try to parse a pass name.
587 size_t End = PipelineText.find_first_of(",)");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000588 if (!parseModulePassName(MPM, PipelineText.substr(0, End), DebugLogging))
Chandler Carruth66445382014-01-11 08:16:35 +0000589 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000590 if (VerifyEachPass)
591 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000592
593 PipelineText = PipelineText.substr(End);
594 }
595
596 if (PipelineText.empty() || PipelineText[0] == ')')
597 return true;
598
599 assert(PipelineText[0] == ',');
600 PipelineText = PipelineText.substr(1);
601 }
602}
603
604// Primary pass pipeline description parsing routine.
605// FIXME: Should this routine accept a TargetMachine or require the caller to
606// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000607bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
608 StringRef PipelineText, bool VerifyEachPass,
609 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000610 // By default, try to parse the pipeline as-if it were within an implicit
611 // 'module(...)' pass pipeline. If this will parse at all, it needs to
612 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000613 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000614 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000615
Chandler Carruthea368f12015-01-06 08:37:58 +0000616 // This isn't parsable as a module pipeline, look for the end of a pass name
617 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000618 StringRef FirstName =
619 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000620 assert(!isModulePassName(FirstName) &&
621 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000622
Chandler Carruthea368f12015-01-06 08:37:58 +0000623 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
624 // pipeline.
Justin Bognereecc3c82016-02-25 07:23:08 +0000625 if (PipelineText.startswith("cgscc(") || isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000626 CGSCCPassManager CGPM(DebugLogging);
627 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
628 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000629 !PipelineText.empty())
630 return false;
631 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
632 return true;
633 }
634
Chandler Carruthea368f12015-01-06 08:37:58 +0000635 // Similarly, if this looks like a Function pass, parse the whole thing as
636 // a Function pipelien.
Justin Bognereecc3c82016-02-25 07:23:08 +0000637 if (PipelineText.startswith("function(") || isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000638 FunctionPassManager FPM(DebugLogging);
639 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
640 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000641 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000642 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000643 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000644 return true;
645 }
Chandler Carruth66445382014-01-11 08:16:35 +0000646
Justin Bognereecc3c82016-02-25 07:23:08 +0000647 // If this looks like a Loop pass, parse the whole thing as a Loop pipeline.
648 if (PipelineText.startswith("loop(") || isLoopPassName(FirstName)) {
649 LoopPassManager LPM(DebugLogging);
650 if (!parseLoopPassPipeline(LPM, PipelineText, VerifyEachPass,
651 DebugLogging) ||
652 !PipelineText.empty())
653 return false;
654 FunctionPassManager FPM(DebugLogging);
655 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
656 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
657 return true;
658 }
659
660
Chandler Carruth66445382014-01-11 08:16:35 +0000661 return false;
662}
Chandler Carruthedf59962016-02-18 09:45:17 +0000663
664bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
665 while (!PipelineText.empty()) {
666 StringRef Name;
667 std::tie(Name, PipelineText) = PipelineText.split(',');
668 if (!parseAAPassName(AA, Name))
669 return false;
670 }
671
672 return true;
673}