blob: 80cd918c2ccec595f49ee661cd7277bdd693c543 [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 Li28a93272016-05-05 21:13:27 +000024#include "llvm/Analysis/BlockFrequencyInfo.h"
25#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
Xinliang David Li6e5dd412016-05-05 02:59:57 +000026#include "llvm/Analysis/BranchProbabilityInfo.h"
Chandler Carruth342c6712016-02-20 03:52:02 +000027#include "llvm/Analysis/CFLAliasAnalysis.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000028#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruth4c660f72016-03-10 11:24:11 +000029#include "llvm/Analysis/CallGraph.h"
Michael Kupersteinde16b442016-04-18 23:55:01 +000030#include "llvm/Analysis/DemandedBits.h"
Chandler Carruth49c22192016-05-12 22:19:39 +000031#include "llvm/Analysis/DependenceAnalysis.h"
Hongbin Zheng751337f2016-02-25 17:54:15 +000032#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruth45a9c202016-03-11 09:15:11 +000033#include "llvm/Analysis/GlobalsModRef.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000034#include "llvm/Analysis/LazyCallGraph.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000035#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth61440d22016-03-10 00:55:30 +000036#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000037#include "llvm/Analysis/PostDominators.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000038#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000039#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000040#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000041#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000042#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000043#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000044#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000045#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000046#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000047#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000048#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000049#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000050#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000051#include "llvm/Target/TargetMachine.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000052#include "llvm/Transforms/IPO/ConstantMerge.h"
Davide Italiano344e8382016-05-05 02:37:32 +000053#include "llvm/Transforms/IPO/ElimAvailExtern.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000054#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000055#include "llvm/Transforms/IPO/FunctionAttrs.h"
Davide Italiano66228c42016-05-03 19:39:15 +000056#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000057#include "llvm/Transforms/IPO/GlobalOpt.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000058#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Justin Bogner4563a062016-04-26 20:15:52 +000059#include "llvm/Transforms/IPO/Internalize.h"
Davide Italianof54f2f02016-05-05 21:05:36 +000060#include "llvm/Transforms/IPO/SCCP.h"
Justin Bogner21e15372015-10-30 23:28:12 +000061#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000062#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000063#include "llvm/Transforms/InstrProfiling.h"
Xinliang David Li8aebf442016-05-06 05:49:19 +000064#include "llvm/Transforms/PGOInstrumentation.h"
Justin Bogner19b67992015-10-30 23:13:18 +000065#include "llvm/Transforms/Scalar/ADCE.h"
Davide Italiano655a1452016-05-25 01:57:04 +000066#include "llvm/Transforms/Scalar/BDCE.h"
Justin Bogner395c2122016-04-22 19:40:41 +000067#include "llvm/Transforms/Scalar/DCE.h"
Justin Bogner594e07b2016-05-17 21:38:13 +000068#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000069#include "llvm/Transforms/Scalar/EarlyCSE.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000070#include "llvm/Transforms/Scalar/GVN.h"
Davide Italiano655a1452016-05-25 01:57:04 +000071#include "llvm/Transforms/Scalar/GuardWidening.h"
Justin Bognerd0d23412016-05-03 22:02:31 +000072#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +000073#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Davide Italiano99223442016-05-13 22:52:35 +000074#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000075#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Davide Italiano1021c682016-05-25 23:38:53 +000076#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +000077#include "llvm/Transforms/Scalar/Reassociate.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +000078#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000079#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000080#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +000081#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruth58dde8c2016-02-26 12:17:54 +000082#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +000083
84using namespace llvm;
85
Chandler Carruth8b5a74192016-02-28 22:16:03 +000086static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
87
Chandler Carruth66445382014-01-11 08:16:35 +000088namespace {
89
Chandler Carruthd8330982014-01-12 09:34:22 +000090/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000091struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000092 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000093 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000094};
95
Chandler Carruth0b576b32015-01-06 02:50:06 +000096/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +000097class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
98 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +000099 static char PassID;
100
101public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000102 struct Result {};
103 Result run(Module &) { return Result(); }
104 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000105};
106
Chandler Carruth572e3402014-04-21 11:12:00 +0000107/// \brief No-op CGSCC pass which does nothing.
108struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000109 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000110 return PreservedAnalyses::all();
111 }
112 static StringRef name() { return "NoOpCGSCCPass"; }
113};
114
Chandler Carruth0b576b32015-01-06 02:50:06 +0000115/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000116class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
117 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000118 static char PassID;
119
120public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000121 struct Result {};
122 Result run(LazyCallGraph::SCC &) { return Result(); }
123 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000124};
125
Chandler Carruthd8330982014-01-12 09:34:22 +0000126/// \brief No-op function pass which does nothing.
127struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000128 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +0000129 static StringRef name() { return "NoOpFunctionPass"; }
130};
131
Chandler Carruth0b576b32015-01-06 02:50:06 +0000132/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000133class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
134 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000135 static char PassID;
136
137public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000138 struct Result {};
139 Result run(Function &) { return Result(); }
140 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000141};
142
Justin Bognereecc3c82016-02-25 07:23:08 +0000143/// \brief No-op loop pass which does nothing.
144struct NoOpLoopPass {
145 PreservedAnalyses run(Loop &L) { return PreservedAnalyses::all(); }
146 static StringRef name() { return "NoOpLoopPass"; }
147};
148
149/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000150class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
151 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000152 static char PassID;
153
154public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000155 struct Result {};
156 Result run(Loop &) { return Result(); }
157 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000158};
159
Chandler Carruthb4faf132016-03-11 10:22:49 +0000160char NoOpModuleAnalysis::PassID;
161char NoOpCGSCCAnalysis::PassID;
162char NoOpFunctionAnalysis::PassID;
163char NoOpLoopAnalysis::PassID;
164
Chandler Carruth66445382014-01-11 08:16:35 +0000165} // End anonymous namespace.
166
Chandler Carruth1ff77242015-03-07 09:02:36 +0000167void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000168#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000169 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000170#include "PassRegistry.def"
171}
172
Chandler Carruth1ff77242015-03-07 09:02:36 +0000173void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000174#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000175 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000176#include "PassRegistry.def"
177}
178
Chandler Carruth1ff77242015-03-07 09:02:36 +0000179void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000180#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000181 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000182#include "PassRegistry.def"
183}
184
Justin Bognereecc3c82016-02-25 07:23:08 +0000185void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
186#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
187 LAM.registerPass([&] { return CREATE_PASS; });
188#include "PassRegistry.def"
189}
190
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000191void PassBuilder::addPerModuleDefaultPipeline(ModulePassManager &MPM,
192 OptimizationLevel Level,
193 bool DebugLogging) {
194 // FIXME: Finish fleshing this out to match the legacy pipelines.
195 FunctionPassManager EarlyFPM(DebugLogging);
196 EarlyFPM.addPass(SimplifyCFGPass());
197 EarlyFPM.addPass(SROA());
198 EarlyFPM.addPass(EarlyCSEPass());
199 EarlyFPM.addPass(LowerExpectIntrinsicPass());
200
201 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
202}
203
204void PassBuilder::addLTOPreLinkDefaultPipeline(ModulePassManager &MPM,
205 OptimizationLevel Level,
206 bool DebugLogging) {
207 // FIXME: We should use a customized pre-link pipeline!
208 addPerModuleDefaultPipeline(MPM, Level, DebugLogging);
209}
210
211void PassBuilder::addLTODefaultPipeline(ModulePassManager &MPM,
212 OptimizationLevel Level,
213 bool DebugLogging) {
214 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
215 FunctionPassManager LateFPM(DebugLogging);
216 LateFPM.addPass(InstCombinePass());
217 LateFPM.addPass(SimplifyCFGPass());
218
219 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
220}
221
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000222#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000223static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000224 // Manually handle aliases for pre-configured pipeline fragments.
225 if (Name.startswith("default") || Name.startswith("lto"))
226 return DefaultAliasRegex.match(Name);
227
Chandler Carruth58944182014-04-21 08:08:50 +0000228#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000229#define MODULE_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 Carruth66445382014-01-11 08:16:35 +0000234 return false;
235}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000236#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000237
Chandler Carruth572e3402014-04-21 11:12:00 +0000238static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000239#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000240#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000241 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000242 return true;
243#include "PassRegistry.def"
244
Chandler Carruth572e3402014-04-21 11:12:00 +0000245 return false;
246}
247
Chandler Carruthd8330982014-01-12 09:34:22 +0000248static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000249#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000250#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000251 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000252 return true;
253#include "PassRegistry.def"
254
Chandler Carruthd8330982014-01-12 09:34:22 +0000255 return false;
256}
257
Justin Bognereecc3c82016-02-25 07:23:08 +0000258static bool isLoopPassName(StringRef Name) {
259#define LOOP_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
260#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
261 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
262 return true;
263#include "PassRegistry.def"
264
265 return false;
266}
267
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000268bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name,
269 bool DebugLogging) {
270 // Manually handle aliases for pre-configured pipeline fragments.
271 if (Name.startswith("default") || Name.startswith("lto")) {
272 SmallVector<StringRef, 3> Matches;
273 if (!DefaultAliasRegex.match(Name, &Matches))
274 return false;
275 assert(Matches.size() == 3 && "Must capture two matched strings!");
276
277 auto L = StringSwitch<OptimizationLevel>(Matches[2])
278 .Case("O0", O0)
279 .Case("O1", O1)
280 .Case("O2", O2)
281 .Case("O3", O3)
282 .Case("Os", Os)
283 .Case("Oz", Oz);
284
285 if (Matches[1] == "default") {
286 addPerModuleDefaultPipeline(MPM, L, DebugLogging);
287 } else if (Matches[1] == "lto-pre-link") {
288 addLTOPreLinkDefaultPipeline(MPM, L, DebugLogging);
289 } else {
290 assert(Matches[1] == "lto" && "Not one of the matched options!");
291 addLTODefaultPipeline(MPM, L, DebugLogging);
292 }
293 return true;
294 }
295
Chandler Carruth58944182014-04-21 08:08:50 +0000296#define MODULE_PASS(NAME, CREATE_PASS) \
297 if (Name == NAME) { \
298 MPM.addPass(CREATE_PASS); \
299 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000300 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000301#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
302 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000303 MPM.addPass(RequireAnalysisPass< \
304 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000305 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000306 } \
307 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000308 MPM.addPass(InvalidateAnalysisPass< \
309 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000310 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000311 }
312#include "PassRegistry.def"
313
Chandler Carruth66445382014-01-11 08:16:35 +0000314 return false;
315}
316
Chandler Carruth1ff77242015-03-07 09:02:36 +0000317bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000318#define CGSCC_PASS(NAME, CREATE_PASS) \
319 if (Name == NAME) { \
320 CGPM.addPass(CREATE_PASS); \
321 return true; \
322 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000323#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
324 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000325 CGPM.addPass(RequireAnalysisPass< \
326 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000327 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000328 } \
329 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000330 CGPM.addPass(InvalidateAnalysisPass< \
331 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000332 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000333 }
334#include "PassRegistry.def"
335
Chandler Carruth572e3402014-04-21 11:12:00 +0000336 return false;
337}
338
Chandler Carruth1ff77242015-03-07 09:02:36 +0000339bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
340 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000341#define FUNCTION_PASS(NAME, CREATE_PASS) \
342 if (Name == NAME) { \
343 FPM.addPass(CREATE_PASS); \
344 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000345 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000346#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
347 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000348 FPM.addPass(RequireAnalysisPass< \
349 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000350 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000351 } \
352 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000353 FPM.addPass(InvalidateAnalysisPass< \
354 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000355 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000356 }
357#include "PassRegistry.def"
358
Chandler Carruthd8330982014-01-12 09:34:22 +0000359 return false;
360}
361
Justin Bognereecc3c82016-02-25 07:23:08 +0000362bool PassBuilder::parseLoopPassName(LoopPassManager &FPM,
363 StringRef Name) {
364#define LOOP_PASS(NAME, CREATE_PASS) \
365 if (Name == NAME) { \
366 FPM.addPass(CREATE_PASS); \
367 return true; \
368 }
369#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
370 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000371 FPM.addPass(RequireAnalysisPass< \
372 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000373 return true; \
374 } \
375 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000376 FPM.addPass(InvalidateAnalysisPass< \
377 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000378 return true; \
379 }
380#include "PassRegistry.def"
381
382 return false;
383}
384
Chandler Carruthedf59962016-02-18 09:45:17 +0000385bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +0000386#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
387 if (Name == NAME) { \
388 AA.registerModuleAnalysis< \
389 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
390 return true; \
391 }
Chandler Carruthedf59962016-02-18 09:45:17 +0000392#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
393 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000394 AA.registerFunctionAnalysis< \
395 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +0000396 return true; \
397 }
398#include "PassRegistry.def"
399
400 return false;
401}
402
Justin Bognereecc3c82016-02-25 07:23:08 +0000403bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
404 StringRef &PipelineText,
405 bool VerifyEachPass,
406 bool DebugLogging) {
407 for (;;) {
408 // Parse nested pass managers by recursing.
409 if (PipelineText.startswith("loop(")) {
410 LoopPassManager NestedLPM(DebugLogging);
411
412 // Parse the inner pipeline inte the nested manager.
413 PipelineText = PipelineText.substr(strlen("loop("));
414 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
415 DebugLogging) ||
416 PipelineText.empty())
417 return false;
418 assert(PipelineText[0] == ')');
419 PipelineText = PipelineText.substr(1);
420
421 // Add the nested pass manager with the appropriate adaptor.
422 LPM.addPass(std::move(NestedLPM));
423 } else {
424 // Otherwise try to parse a pass name.
425 size_t End = PipelineText.find_first_of(",)");
426 if (!parseLoopPassName(LPM, PipelineText.substr(0, End)))
427 return false;
428 // TODO: Ideally, we would run a LoopVerifierPass() here in the
429 // VerifyEachPass case, but we don't have such a verifier yet.
430
431 PipelineText = PipelineText.substr(End);
432 }
433
434 if (PipelineText.empty() || PipelineText[0] == ')')
435 return true;
436
437 assert(PipelineText[0] == ',');
438 PipelineText = PipelineText.substr(1);
439 }
440}
441
Chandler Carruth1ff77242015-03-07 09:02:36 +0000442bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
443 StringRef &PipelineText,
444 bool VerifyEachPass,
445 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000446 for (;;) {
447 // Parse nested pass managers by recursing.
448 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000449 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000450
451 // Parse the inner pipeline inte the nested manager.
452 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000453 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
454 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000455 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000456 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000457 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000458 PipelineText = PipelineText.substr(1);
459
460 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000461 FPM.addPass(std::move(NestedFPM));
Justin Bognereecc3c82016-02-25 07:23:08 +0000462 } else if (PipelineText.startswith("loop(")) {
463 LoopPassManager NestedLPM(DebugLogging);
464
465 // Parse the inner pipeline inte the nested manager.
466 PipelineText = PipelineText.substr(strlen("loop("));
467 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
468 DebugLogging) ||
469 PipelineText.empty())
470 return false;
471 assert(PipelineText[0] == ')');
472 PipelineText = PipelineText.substr(1);
473
474 // Add the nested pass manager with the appropriate adaptor.
475 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(NestedLPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000476 } else {
477 // Otherwise try to parse a pass name.
478 size_t End = PipelineText.find_first_of(",)");
479 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
480 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000481 if (VerifyEachPass)
482 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000483
484 PipelineText = PipelineText.substr(End);
485 }
486
487 if (PipelineText.empty() || PipelineText[0] == ')')
488 return true;
489
490 assert(PipelineText[0] == ',');
491 PipelineText = PipelineText.substr(1);
492 }
493}
494
Chandler Carruth1ff77242015-03-07 09:02:36 +0000495bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
496 StringRef &PipelineText,
497 bool VerifyEachPass,
498 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000499 for (;;) {
500 // Parse nested pass managers by recursing.
501 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000502 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000503
504 // Parse the inner pipeline into the nested manager.
505 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000506 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
507 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000508 PipelineText.empty())
509 return false;
510 assert(PipelineText[0] == ')');
511 PipelineText = PipelineText.substr(1);
512
513 // Add the nested pass manager with the appropriate adaptor.
514 CGPM.addPass(std::move(NestedCGPM));
515 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000516 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000517
518 // Parse the inner pipeline inte the nested manager.
519 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000520 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
521 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000522 PipelineText.empty())
523 return false;
524 assert(PipelineText[0] == ')');
525 PipelineText = PipelineText.substr(1);
526
527 // Add the nested pass manager with the appropriate adaptor.
528 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
529 } else {
530 // Otherwise try to parse a pass name.
531 size_t End = PipelineText.find_first_of(",)");
532 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
533 return false;
534 // FIXME: No verifier support for CGSCC passes!
535
536 PipelineText = PipelineText.substr(End);
537 }
538
539 if (PipelineText.empty() || PipelineText[0] == ')')
540 return true;
541
542 assert(PipelineText[0] == ',');
543 PipelineText = PipelineText.substr(1);
544 }
545}
546
NAKAMURA Takumia42f5282016-05-16 10:13:37 +0000547void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
548 FunctionAnalysisManager &FAM,
549 CGSCCAnalysisManager &CGAM,
550 ModuleAnalysisManager &MAM) {
551 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
552 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
553 CGAM.registerPass([&] { return FunctionAnalysisManagerCGSCCProxy(FAM); });
554 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
555 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
556 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
557 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
558 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
559}
560
Chandler Carruth1ff77242015-03-07 09:02:36 +0000561bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
562 StringRef &PipelineText,
563 bool VerifyEachPass,
564 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000565 for (;;) {
566 // Parse nested pass managers by recursing.
567 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000568 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000569
570 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000571 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000572 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
573 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000574 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000575 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000576 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000577 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000578
579 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000580 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000581 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000582 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000583
584 // Parse the inner pipeline inte the nested manager.
585 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000586 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
587 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000588 PipelineText.empty())
589 return false;
590 assert(PipelineText[0] == ')');
591 PipelineText = PipelineText.substr(1);
592
593 // Add the nested pass manager with the appropriate adaptor.
594 MPM.addPass(
595 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000596 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000597 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000598
599 // Parse the inner pipeline inte the nested manager.
600 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000601 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
602 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000603 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000604 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000605 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000606 PipelineText = PipelineText.substr(1);
607
608 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000609 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000610 } else {
611 // Otherwise try to parse a pass name.
612 size_t End = PipelineText.find_first_of(",)");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000613 if (!parseModulePassName(MPM, PipelineText.substr(0, End), DebugLogging))
Chandler Carruth66445382014-01-11 08:16:35 +0000614 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000615 if (VerifyEachPass)
616 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000617
618 PipelineText = PipelineText.substr(End);
619 }
620
621 if (PipelineText.empty() || PipelineText[0] == ')')
622 return true;
623
624 assert(PipelineText[0] == ',');
625 PipelineText = PipelineText.substr(1);
626 }
627}
628
629// Primary pass pipeline description parsing routine.
630// FIXME: Should this routine accept a TargetMachine or require the caller to
631// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000632bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
633 StringRef PipelineText, bool VerifyEachPass,
634 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000635 // By default, try to parse the pipeline as-if it were within an implicit
636 // 'module(...)' pass pipeline. If this will parse at all, it needs to
637 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000638 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000639 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000640
Chandler Carruthea368f12015-01-06 08:37:58 +0000641 // This isn't parsable as a module pipeline, look for the end of a pass name
642 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000643 StringRef FirstName =
644 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000645 assert(!isModulePassName(FirstName) &&
646 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000647
Chandler Carruthea368f12015-01-06 08:37:58 +0000648 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
649 // pipeline.
Justin Bognereecc3c82016-02-25 07:23:08 +0000650 if (PipelineText.startswith("cgscc(") || isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000651 CGSCCPassManager CGPM(DebugLogging);
652 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
653 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000654 !PipelineText.empty())
655 return false;
656 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
657 return true;
658 }
659
Chandler Carruthea368f12015-01-06 08:37:58 +0000660 // Similarly, if this looks like a Function pass, parse the whole thing as
661 // a Function pipelien.
Justin Bognereecc3c82016-02-25 07:23:08 +0000662 if (PipelineText.startswith("function(") || isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000663 FunctionPassManager FPM(DebugLogging);
664 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
665 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000666 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000667 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000668 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000669 return true;
670 }
Chandler Carruth66445382014-01-11 08:16:35 +0000671
Justin Bognereecc3c82016-02-25 07:23:08 +0000672 // If this looks like a Loop pass, parse the whole thing as a Loop pipeline.
673 if (PipelineText.startswith("loop(") || isLoopPassName(FirstName)) {
674 LoopPassManager LPM(DebugLogging);
675 if (!parseLoopPassPipeline(LPM, PipelineText, VerifyEachPass,
676 DebugLogging) ||
677 !PipelineText.empty())
678 return false;
679 FunctionPassManager FPM(DebugLogging);
680 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
681 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
682 return true;
683 }
684
685
Chandler Carruth66445382014-01-11 08:16:35 +0000686 return false;
687}
Chandler Carruthedf59962016-02-18 09:45:17 +0000688
689bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
690 while (!PipelineText.empty()) {
691 StringRef Name;
692 std::tie(Name, PipelineText) = PipelineText.split(',');
693 if (!parseAAPassName(AA, Name))
694 return false;
695 }
696
697 return true;
698}