blob: 9c7c629887f7222bf42a127c6f76de103c970862 [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"
Justin Bogner395c2122016-04-22 19:40:41 +000066#include "llvm/Transforms/Scalar/DCE.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000067#include "llvm/Transforms/Scalar/EarlyCSE.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000068#include "llvm/Transforms/Scalar/GVN.h"
Justin Bognerd0d23412016-05-03 22:02:31 +000069#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +000070#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Davide Italiano99223442016-05-13 22:52:35 +000071#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000072#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +000073#include "llvm/Transforms/Scalar/Reassociate.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000074#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000075#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +000076#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruth58dde8c2016-02-26 12:17:54 +000077#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +000078
79using namespace llvm;
80
Chandler Carruth8b5a74192016-02-28 22:16:03 +000081static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
82
Chandler Carruth66445382014-01-11 08:16:35 +000083namespace {
84
Chandler Carruthd8330982014-01-12 09:34:22 +000085/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000086struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000087 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000088 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000089};
90
Chandler Carruth0b576b32015-01-06 02:50:06 +000091/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +000092class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
93 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +000094 static char PassID;
95
96public:
Chandler Carruth0b576b32015-01-06 02:50:06 +000097 struct Result {};
98 Result run(Module &) { return Result(); }
99 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000100};
101
Chandler Carruth572e3402014-04-21 11:12:00 +0000102/// \brief No-op CGSCC pass which does nothing.
103struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000104 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000105 return PreservedAnalyses::all();
106 }
107 static StringRef name() { return "NoOpCGSCCPass"; }
108};
109
Chandler Carruth0b576b32015-01-06 02:50:06 +0000110/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000111class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
112 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000113 static char PassID;
114
115public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000116 struct Result {};
117 Result run(LazyCallGraph::SCC &) { return Result(); }
118 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000119};
120
Chandler Carruthd8330982014-01-12 09:34:22 +0000121/// \brief No-op function pass which does nothing.
122struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000123 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +0000124 static StringRef name() { return "NoOpFunctionPass"; }
125};
126
Chandler Carruth0b576b32015-01-06 02:50:06 +0000127/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000128class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
129 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000130 static char PassID;
131
132public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000133 struct Result {};
134 Result run(Function &) { return Result(); }
135 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000136};
137
Justin Bognereecc3c82016-02-25 07:23:08 +0000138/// \brief No-op loop pass which does nothing.
139struct NoOpLoopPass {
140 PreservedAnalyses run(Loop &L) { return PreservedAnalyses::all(); }
141 static StringRef name() { return "NoOpLoopPass"; }
142};
143
144/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000145class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
146 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000147 static char PassID;
148
149public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000150 struct Result {};
151 Result run(Loop &) { return Result(); }
152 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000153};
154
Chandler Carruthb4faf132016-03-11 10:22:49 +0000155char NoOpModuleAnalysis::PassID;
156char NoOpCGSCCAnalysis::PassID;
157char NoOpFunctionAnalysis::PassID;
158char NoOpLoopAnalysis::PassID;
159
Chandler Carruth66445382014-01-11 08:16:35 +0000160} // End anonymous namespace.
161
Chandler Carruth1ff77242015-03-07 09:02:36 +0000162void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000163#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000164 MAM.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::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000169#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000170 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000171#include "PassRegistry.def"
172}
173
Chandler Carruth1ff77242015-03-07 09:02:36 +0000174void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000175#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000176 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000177#include "PassRegistry.def"
178}
179
Justin Bognereecc3c82016-02-25 07:23:08 +0000180void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
181#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
182 LAM.registerPass([&] { return CREATE_PASS; });
183#include "PassRegistry.def"
184}
185
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000186void PassBuilder::addPerModuleDefaultPipeline(ModulePassManager &MPM,
187 OptimizationLevel Level,
188 bool DebugLogging) {
189 // FIXME: Finish fleshing this out to match the legacy pipelines.
190 FunctionPassManager EarlyFPM(DebugLogging);
191 EarlyFPM.addPass(SimplifyCFGPass());
192 EarlyFPM.addPass(SROA());
193 EarlyFPM.addPass(EarlyCSEPass());
194 EarlyFPM.addPass(LowerExpectIntrinsicPass());
195
196 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
197}
198
199void PassBuilder::addLTOPreLinkDefaultPipeline(ModulePassManager &MPM,
200 OptimizationLevel Level,
201 bool DebugLogging) {
202 // FIXME: We should use a customized pre-link pipeline!
203 addPerModuleDefaultPipeline(MPM, Level, DebugLogging);
204}
205
206void PassBuilder::addLTODefaultPipeline(ModulePassManager &MPM,
207 OptimizationLevel Level,
208 bool DebugLogging) {
209 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
210 FunctionPassManager LateFPM(DebugLogging);
211 LateFPM.addPass(InstCombinePass());
212 LateFPM.addPass(SimplifyCFGPass());
213
214 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
215}
216
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000217#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000218static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000219 // Manually handle aliases for pre-configured pipeline fragments.
220 if (Name.startswith("default") || Name.startswith("lto"))
221 return DefaultAliasRegex.match(Name);
222
Chandler Carruth58944182014-04-21 08:08:50 +0000223#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000224#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000225 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000226 return true;
227#include "PassRegistry.def"
228
Chandler Carruth66445382014-01-11 08:16:35 +0000229 return false;
230}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000231#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000232
Chandler Carruth572e3402014-04-21 11:12:00 +0000233static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000234#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000235#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000236 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000237 return true;
238#include "PassRegistry.def"
239
Chandler Carruth572e3402014-04-21 11:12:00 +0000240 return false;
241}
242
Chandler Carruthd8330982014-01-12 09:34:22 +0000243static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000244#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000245#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000246 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000247 return true;
248#include "PassRegistry.def"
249
Chandler Carruthd8330982014-01-12 09:34:22 +0000250 return false;
251}
252
Justin Bognereecc3c82016-02-25 07:23:08 +0000253static bool isLoopPassName(StringRef Name) {
254#define LOOP_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
255#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
256 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
257 return true;
258#include "PassRegistry.def"
259
260 return false;
261}
262
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000263bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name,
264 bool DebugLogging) {
265 // Manually handle aliases for pre-configured pipeline fragments.
266 if (Name.startswith("default") || Name.startswith("lto")) {
267 SmallVector<StringRef, 3> Matches;
268 if (!DefaultAliasRegex.match(Name, &Matches))
269 return false;
270 assert(Matches.size() == 3 && "Must capture two matched strings!");
271
272 auto L = StringSwitch<OptimizationLevel>(Matches[2])
273 .Case("O0", O0)
274 .Case("O1", O1)
275 .Case("O2", O2)
276 .Case("O3", O3)
277 .Case("Os", Os)
278 .Case("Oz", Oz);
279
280 if (Matches[1] == "default") {
281 addPerModuleDefaultPipeline(MPM, L, DebugLogging);
282 } else if (Matches[1] == "lto-pre-link") {
283 addLTOPreLinkDefaultPipeline(MPM, L, DebugLogging);
284 } else {
285 assert(Matches[1] == "lto" && "Not one of the matched options!");
286 addLTODefaultPipeline(MPM, L, DebugLogging);
287 }
288 return true;
289 }
290
Chandler Carruth58944182014-04-21 08:08:50 +0000291#define MODULE_PASS(NAME, CREATE_PASS) \
292 if (Name == NAME) { \
293 MPM.addPass(CREATE_PASS); \
294 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000295 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000296#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
297 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000298 MPM.addPass(RequireAnalysisPass< \
299 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000300 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000301 } \
302 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000303 MPM.addPass(InvalidateAnalysisPass< \
304 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000305 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000306 }
307#include "PassRegistry.def"
308
Chandler Carruth66445382014-01-11 08:16:35 +0000309 return false;
310}
311
Chandler Carruth1ff77242015-03-07 09:02:36 +0000312bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000313#define CGSCC_PASS(NAME, CREATE_PASS) \
314 if (Name == NAME) { \
315 CGPM.addPass(CREATE_PASS); \
316 return true; \
317 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000318#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
319 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000320 CGPM.addPass(RequireAnalysisPass< \
321 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000322 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000323 } \
324 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000325 CGPM.addPass(InvalidateAnalysisPass< \
326 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000327 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000328 }
329#include "PassRegistry.def"
330
Chandler Carruth572e3402014-04-21 11:12:00 +0000331 return false;
332}
333
Chandler Carruth1ff77242015-03-07 09:02:36 +0000334bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
335 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000336#define FUNCTION_PASS(NAME, CREATE_PASS) \
337 if (Name == NAME) { \
338 FPM.addPass(CREATE_PASS); \
339 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000340 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000341#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
342 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000343 FPM.addPass(RequireAnalysisPass< \
344 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000345 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000346 } \
347 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000348 FPM.addPass(InvalidateAnalysisPass< \
349 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000350 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000351 }
352#include "PassRegistry.def"
353
Chandler Carruthd8330982014-01-12 09:34:22 +0000354 return false;
355}
356
Justin Bognereecc3c82016-02-25 07:23:08 +0000357bool PassBuilder::parseLoopPassName(LoopPassManager &FPM,
358 StringRef Name) {
359#define LOOP_PASS(NAME, CREATE_PASS) \
360 if (Name == NAME) { \
361 FPM.addPass(CREATE_PASS); \
362 return true; \
363 }
364#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
365 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000366 FPM.addPass(RequireAnalysisPass< \
367 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000368 return true; \
369 } \
370 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000371 FPM.addPass(InvalidateAnalysisPass< \
372 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000373 return true; \
374 }
375#include "PassRegistry.def"
376
377 return false;
378}
379
Chandler Carruthedf59962016-02-18 09:45:17 +0000380bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +0000381#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
382 if (Name == NAME) { \
383 AA.registerModuleAnalysis< \
384 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
385 return true; \
386 }
Chandler Carruthedf59962016-02-18 09:45:17 +0000387#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
388 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000389 AA.registerFunctionAnalysis< \
390 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +0000391 return true; \
392 }
393#include "PassRegistry.def"
394
395 return false;
396}
397
Justin Bognereecc3c82016-02-25 07:23:08 +0000398bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
399 StringRef &PipelineText,
400 bool VerifyEachPass,
401 bool DebugLogging) {
402 for (;;) {
403 // Parse nested pass managers by recursing.
404 if (PipelineText.startswith("loop(")) {
405 LoopPassManager NestedLPM(DebugLogging);
406
407 // Parse the inner pipeline inte the nested manager.
408 PipelineText = PipelineText.substr(strlen("loop("));
409 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
410 DebugLogging) ||
411 PipelineText.empty())
412 return false;
413 assert(PipelineText[0] == ')');
414 PipelineText = PipelineText.substr(1);
415
416 // Add the nested pass manager with the appropriate adaptor.
417 LPM.addPass(std::move(NestedLPM));
418 } else {
419 // Otherwise try to parse a pass name.
420 size_t End = PipelineText.find_first_of(",)");
421 if (!parseLoopPassName(LPM, PipelineText.substr(0, End)))
422 return false;
423 // TODO: Ideally, we would run a LoopVerifierPass() here in the
424 // VerifyEachPass case, but we don't have such a verifier yet.
425
426 PipelineText = PipelineText.substr(End);
427 }
428
429 if (PipelineText.empty() || PipelineText[0] == ')')
430 return true;
431
432 assert(PipelineText[0] == ',');
433 PipelineText = PipelineText.substr(1);
434 }
435}
436
Chandler Carruth1ff77242015-03-07 09:02:36 +0000437bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
438 StringRef &PipelineText,
439 bool VerifyEachPass,
440 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000441 for (;;) {
442 // Parse nested pass managers by recursing.
443 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000444 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000445
446 // Parse the inner pipeline inte the nested manager.
447 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000448 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
449 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000450 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000451 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000452 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000453 PipelineText = PipelineText.substr(1);
454
455 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000456 FPM.addPass(std::move(NestedFPM));
Justin Bognereecc3c82016-02-25 07:23:08 +0000457 } else if (PipelineText.startswith("loop(")) {
458 LoopPassManager NestedLPM(DebugLogging);
459
460 // Parse the inner pipeline inte the nested manager.
461 PipelineText = PipelineText.substr(strlen("loop("));
462 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
463 DebugLogging) ||
464 PipelineText.empty())
465 return false;
466 assert(PipelineText[0] == ')');
467 PipelineText = PipelineText.substr(1);
468
469 // Add the nested pass manager with the appropriate adaptor.
470 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(NestedLPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000471 } else {
472 // Otherwise try to parse a pass name.
473 size_t End = PipelineText.find_first_of(",)");
474 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
475 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000476 if (VerifyEachPass)
477 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000478
479 PipelineText = PipelineText.substr(End);
480 }
481
482 if (PipelineText.empty() || PipelineText[0] == ')')
483 return true;
484
485 assert(PipelineText[0] == ',');
486 PipelineText = PipelineText.substr(1);
487 }
488}
489
Chandler Carruth1ff77242015-03-07 09:02:36 +0000490bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
491 StringRef &PipelineText,
492 bool VerifyEachPass,
493 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000494 for (;;) {
495 // Parse nested pass managers by recursing.
496 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000497 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000498
499 // Parse the inner pipeline into the nested manager.
500 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000501 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
502 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000503 PipelineText.empty())
504 return false;
505 assert(PipelineText[0] == ')');
506 PipelineText = PipelineText.substr(1);
507
508 // Add the nested pass manager with the appropriate adaptor.
509 CGPM.addPass(std::move(NestedCGPM));
510 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000511 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000512
513 // Parse the inner pipeline inte the nested manager.
514 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000515 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
516 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000517 PipelineText.empty())
518 return false;
519 assert(PipelineText[0] == ')');
520 PipelineText = PipelineText.substr(1);
521
522 // Add the nested pass manager with the appropriate adaptor.
523 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
524 } else {
525 // Otherwise try to parse a pass name.
526 size_t End = PipelineText.find_first_of(",)");
527 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
528 return false;
529 // FIXME: No verifier support for CGSCC passes!
530
531 PipelineText = PipelineText.substr(End);
532 }
533
534 if (PipelineText.empty() || PipelineText[0] == ')')
535 return true;
536
537 assert(PipelineText[0] == ',');
538 PipelineText = PipelineText.substr(1);
539 }
540}
541
NAKAMURA Takumia42f5282016-05-16 10:13:37 +0000542void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
543 FunctionAnalysisManager &FAM,
544 CGSCCAnalysisManager &CGAM,
545 ModuleAnalysisManager &MAM) {
546 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
547 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
548 CGAM.registerPass([&] { return FunctionAnalysisManagerCGSCCProxy(FAM); });
549 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
550 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
551 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
552 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
553 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
554}
555
Chandler Carruth1ff77242015-03-07 09:02:36 +0000556bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
557 StringRef &PipelineText,
558 bool VerifyEachPass,
559 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000560 for (;;) {
561 // Parse nested pass managers by recursing.
562 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000563 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000564
565 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000566 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000567 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
568 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000569 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000570 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000571 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000572 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000573
574 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000575 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000576 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000577 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000578
579 // Parse the inner pipeline inte the nested manager.
580 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000581 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
582 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000583 PipelineText.empty())
584 return false;
585 assert(PipelineText[0] == ')');
586 PipelineText = PipelineText.substr(1);
587
588 // Add the nested pass manager with the appropriate adaptor.
589 MPM.addPass(
590 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000591 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000592 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000593
594 // Parse the inner pipeline inte the nested manager.
595 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000596 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
597 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000598 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000599 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000600 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000601 PipelineText = PipelineText.substr(1);
602
603 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000604 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000605 } else {
606 // Otherwise try to parse a pass name.
607 size_t End = PipelineText.find_first_of(",)");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000608 if (!parseModulePassName(MPM, PipelineText.substr(0, End), DebugLogging))
Chandler Carruth66445382014-01-11 08:16:35 +0000609 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000610 if (VerifyEachPass)
611 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000612
613 PipelineText = PipelineText.substr(End);
614 }
615
616 if (PipelineText.empty() || PipelineText[0] == ')')
617 return true;
618
619 assert(PipelineText[0] == ',');
620 PipelineText = PipelineText.substr(1);
621 }
622}
623
624// Primary pass pipeline description parsing routine.
625// FIXME: Should this routine accept a TargetMachine or require the caller to
626// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000627bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
628 StringRef PipelineText, bool VerifyEachPass,
629 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000630 // By default, try to parse the pipeline as-if it were within an implicit
631 // 'module(...)' pass pipeline. If this will parse at all, it needs to
632 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000633 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000634 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000635
Chandler Carruthea368f12015-01-06 08:37:58 +0000636 // This isn't parsable as a module pipeline, look for the end of a pass name
637 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000638 StringRef FirstName =
639 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000640 assert(!isModulePassName(FirstName) &&
641 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000642
Chandler Carruthea368f12015-01-06 08:37:58 +0000643 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
644 // pipeline.
Justin Bognereecc3c82016-02-25 07:23:08 +0000645 if (PipelineText.startswith("cgscc(") || isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000646 CGSCCPassManager CGPM(DebugLogging);
647 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
648 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000649 !PipelineText.empty())
650 return false;
651 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
652 return true;
653 }
654
Chandler Carruthea368f12015-01-06 08:37:58 +0000655 // Similarly, if this looks like a Function pass, parse the whole thing as
656 // a Function pipelien.
Justin Bognereecc3c82016-02-25 07:23:08 +0000657 if (PipelineText.startswith("function(") || isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000658 FunctionPassManager FPM(DebugLogging);
659 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
660 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000661 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000662 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000663 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000664 return true;
665 }
Chandler Carruth66445382014-01-11 08:16:35 +0000666
Justin Bognereecc3c82016-02-25 07:23:08 +0000667 // If this looks like a Loop pass, parse the whole thing as a Loop pipeline.
668 if (PipelineText.startswith("loop(") || isLoopPassName(FirstName)) {
669 LoopPassManager LPM(DebugLogging);
670 if (!parseLoopPassPipeline(LPM, PipelineText, VerifyEachPass,
671 DebugLogging) ||
672 !PipelineText.empty())
673 return false;
674 FunctionPassManager FPM(DebugLogging);
675 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
676 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
677 return true;
678 }
679
680
Chandler Carruth66445382014-01-11 08:16:35 +0000681 return false;
682}
Chandler Carruthedf59962016-02-18 09:45:17 +0000683
684bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
685 while (!PipelineText.empty()) {
686 StringRef Name;
687 std::tie(Name, PipelineText) = PipelineText.split(',');
688 if (!parseAAPassName(AA, Name))
689 return false;
690 }
691
692 return true;
693}