blob: 2dcd3342f384d577d90733e65c67713814a3b54f [file] [log] [blame]
Chandler Carruth1ff77242015-03-07 09:02:36 +00001//===- Parsing, selection, and construction of pass pipelines -------------===//
Chandler Carruth66445382014-01-11 08:16:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10///
Chandler Carruth1ff77242015-03-07 09:02:36 +000011/// This file provides the implementation of the PassBuilder based on our
12/// static pass registry as well as related functionality. It also provides
13/// helpers to aid in analyzing, debugging, and testing passes and pass
14/// pipelines.
Chandler Carruth66445382014-01-11 08:16:35 +000015///
16//===----------------------------------------------------------------------===//
17
Chandler Carruth1ff77242015-03-07 09:02:36 +000018#include "llvm/Passes/PassBuilder.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000019#include "llvm/ADT/StringSwitch.h"
Chandler Carruth6f5770b102016-02-13 23:32:00 +000020#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth4f846a52016-02-20 03:46:03 +000021#include "llvm/Analysis/AliasAnalysisEvaluator.h"
Chandler Carruthdf8b2232015-01-22 21:53:09 +000022#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthbece8d52016-02-13 23:46:24 +000023#include "llvm/Analysis/BasicAliasAnalysis.h"
Chandler Carruth342c6712016-02-20 03:52:02 +000024#include "llvm/Analysis/CFLAliasAnalysis.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000025#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruth4c660f72016-03-10 11:24:11 +000026#include "llvm/Analysis/CallGraph.h"
Michael Kupersteinde16b442016-04-18 23:55:01 +000027#include "llvm/Analysis/DemandedBits.h"
Hongbin Zheng751337f2016-02-25 17:54:15 +000028#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruth45a9c202016-03-11 09:15:11 +000029#include "llvm/Analysis/GlobalsModRef.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000030#include "llvm/Analysis/LazyCallGraph.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000031#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth61440d22016-03-10 00:55:30 +000032#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000033#include "llvm/Analysis/PostDominators.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000034#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000035#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000036#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000037#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000038#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000039#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000040#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000041#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000042#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000043#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000044#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000045#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000046#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000047#include "llvm/Target/TargetMachine.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000048#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000049#include "llvm/Transforms/IPO/FunctionAttrs.h"
Davide Italiano66228c42016-05-03 19:39:15 +000050#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000051#include "llvm/Transforms/IPO/GlobalOpt.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000052#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Justin Bogner4563a062016-04-26 20:15:52 +000053#include "llvm/Transforms/IPO/Internalize.h"
Justin Bogner21e15372015-10-30 23:28:12 +000054#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000055#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000056#include "llvm/Transforms/InstrProfiling.h"
Justin Bogner19b67992015-10-30 23:13:18 +000057#include "llvm/Transforms/Scalar/ADCE.h"
Justin Bogner395c2122016-04-22 19:40:41 +000058#include "llvm/Transforms/Scalar/DCE.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000059#include "llvm/Transforms/Scalar/EarlyCSE.h"
Justin Bognerd0d23412016-05-03 22:02:31 +000060#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +000061#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000062#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Chandler Carruth89c45a12016-03-11 08:50:55 +000063#include "llvm/Transforms/Scalar/GVN.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +000064#include "llvm/Transforms/Scalar/Reassociate.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000065#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000066#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +000067#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruth58dde8c2016-02-26 12:17:54 +000068#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +000069
70using namespace llvm;
71
Chandler Carruth8b5a74192016-02-28 22:16:03 +000072static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
73
Chandler Carruth66445382014-01-11 08:16:35 +000074namespace {
75
Chandler Carruthd8330982014-01-12 09:34:22 +000076/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000077struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000078 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000079 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000080};
81
Chandler Carruth0b576b32015-01-06 02:50:06 +000082/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +000083class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
84 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +000085 static char PassID;
86
87public:
Chandler Carruth0b576b32015-01-06 02:50:06 +000088 struct Result {};
89 Result run(Module &) { return Result(); }
90 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +000091};
92
Chandler Carruth572e3402014-04-21 11:12:00 +000093/// \brief No-op CGSCC pass which does nothing.
94struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000095 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +000096 return PreservedAnalyses::all();
97 }
98 static StringRef name() { return "NoOpCGSCCPass"; }
99};
100
Chandler Carruth0b576b32015-01-06 02:50:06 +0000101/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000102class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
103 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000104 static char PassID;
105
106public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000107 struct Result {};
108 Result run(LazyCallGraph::SCC &) { return Result(); }
109 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000110};
111
Chandler Carruthd8330982014-01-12 09:34:22 +0000112/// \brief No-op function pass which does nothing.
113struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000114 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +0000115 static StringRef name() { return "NoOpFunctionPass"; }
116};
117
Chandler Carruth0b576b32015-01-06 02:50:06 +0000118/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000119class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
120 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000121 static char PassID;
122
123public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000124 struct Result {};
125 Result run(Function &) { return Result(); }
126 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000127};
128
Justin Bognereecc3c82016-02-25 07:23:08 +0000129/// \brief No-op loop pass which does nothing.
130struct NoOpLoopPass {
131 PreservedAnalyses run(Loop &L) { return PreservedAnalyses::all(); }
132 static StringRef name() { return "NoOpLoopPass"; }
133};
134
135/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000136class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
137 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000138 static char PassID;
139
140public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000141 struct Result {};
142 Result run(Loop &) { return Result(); }
143 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000144};
145
Chandler Carruthb4faf132016-03-11 10:22:49 +0000146char NoOpModuleAnalysis::PassID;
147char NoOpCGSCCAnalysis::PassID;
148char NoOpFunctionAnalysis::PassID;
149char NoOpLoopAnalysis::PassID;
150
Chandler Carruth66445382014-01-11 08:16:35 +0000151} // End anonymous namespace.
152
Chandler Carruth1ff77242015-03-07 09:02:36 +0000153void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000154#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000155 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000156#include "PassRegistry.def"
157}
158
Chandler Carruth1ff77242015-03-07 09:02:36 +0000159void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000160#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000161 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000162#include "PassRegistry.def"
163}
164
Chandler Carruth1ff77242015-03-07 09:02:36 +0000165void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000166#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000167 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000168#include "PassRegistry.def"
169}
170
Justin Bognereecc3c82016-02-25 07:23:08 +0000171void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
172#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
173 LAM.registerPass([&] { return CREATE_PASS; });
174#include "PassRegistry.def"
175}
176
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000177void PassBuilder::addPerModuleDefaultPipeline(ModulePassManager &MPM,
178 OptimizationLevel Level,
179 bool DebugLogging) {
180 // FIXME: Finish fleshing this out to match the legacy pipelines.
181 FunctionPassManager EarlyFPM(DebugLogging);
182 EarlyFPM.addPass(SimplifyCFGPass());
183 EarlyFPM.addPass(SROA());
184 EarlyFPM.addPass(EarlyCSEPass());
185 EarlyFPM.addPass(LowerExpectIntrinsicPass());
186
187 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
188}
189
190void PassBuilder::addLTOPreLinkDefaultPipeline(ModulePassManager &MPM,
191 OptimizationLevel Level,
192 bool DebugLogging) {
193 // FIXME: We should use a customized pre-link pipeline!
194 addPerModuleDefaultPipeline(MPM, Level, DebugLogging);
195}
196
197void PassBuilder::addLTODefaultPipeline(ModulePassManager &MPM,
198 OptimizationLevel Level,
199 bool DebugLogging) {
200 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
201 FunctionPassManager LateFPM(DebugLogging);
202 LateFPM.addPass(InstCombinePass());
203 LateFPM.addPass(SimplifyCFGPass());
204
205 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
206}
207
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000208#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000209static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000210 // Manually handle aliases for pre-configured pipeline fragments.
211 if (Name.startswith("default") || Name.startswith("lto"))
212 return DefaultAliasRegex.match(Name);
213
Chandler Carruth58944182014-04-21 08:08:50 +0000214#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000215#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000216 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000217 return true;
218#include "PassRegistry.def"
219
Chandler Carruth66445382014-01-11 08:16:35 +0000220 return false;
221}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000222#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000223
Chandler Carruth572e3402014-04-21 11:12:00 +0000224static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000225#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000226#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000227 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000228 return true;
229#include "PassRegistry.def"
230
Chandler Carruth572e3402014-04-21 11:12:00 +0000231 return false;
232}
233
Chandler Carruthd8330982014-01-12 09:34:22 +0000234static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000235#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000236#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000237 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000238 return true;
239#include "PassRegistry.def"
240
Chandler Carruthd8330982014-01-12 09:34:22 +0000241 return false;
242}
243
Justin Bognereecc3c82016-02-25 07:23:08 +0000244static bool isLoopPassName(StringRef Name) {
245#define LOOP_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
246#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
247 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
248 return true;
249#include "PassRegistry.def"
250
251 return false;
252}
253
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000254bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name,
255 bool DebugLogging) {
256 // Manually handle aliases for pre-configured pipeline fragments.
257 if (Name.startswith("default") || Name.startswith("lto")) {
258 SmallVector<StringRef, 3> Matches;
259 if (!DefaultAliasRegex.match(Name, &Matches))
260 return false;
261 assert(Matches.size() == 3 && "Must capture two matched strings!");
262
263 auto L = StringSwitch<OptimizationLevel>(Matches[2])
264 .Case("O0", O0)
265 .Case("O1", O1)
266 .Case("O2", O2)
267 .Case("O3", O3)
268 .Case("Os", Os)
269 .Case("Oz", Oz);
270
271 if (Matches[1] == "default") {
272 addPerModuleDefaultPipeline(MPM, L, DebugLogging);
273 } else if (Matches[1] == "lto-pre-link") {
274 addLTOPreLinkDefaultPipeline(MPM, L, DebugLogging);
275 } else {
276 assert(Matches[1] == "lto" && "Not one of the matched options!");
277 addLTODefaultPipeline(MPM, L, DebugLogging);
278 }
279 return true;
280 }
281
Chandler Carruth58944182014-04-21 08:08:50 +0000282#define MODULE_PASS(NAME, CREATE_PASS) \
283 if (Name == NAME) { \
284 MPM.addPass(CREATE_PASS); \
285 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000286 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000287#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
288 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000289 MPM.addPass(RequireAnalysisPass< \
290 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000291 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000292 } \
293 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000294 MPM.addPass(InvalidateAnalysisPass< \
295 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000296 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000297 }
298#include "PassRegistry.def"
299
Chandler Carruth66445382014-01-11 08:16:35 +0000300 return false;
301}
302
Chandler Carruth1ff77242015-03-07 09:02:36 +0000303bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000304#define CGSCC_PASS(NAME, CREATE_PASS) \
305 if (Name == NAME) { \
306 CGPM.addPass(CREATE_PASS); \
307 return true; \
308 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000309#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
310 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000311 CGPM.addPass(RequireAnalysisPass< \
312 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000313 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000314 } \
315 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000316 CGPM.addPass(InvalidateAnalysisPass< \
317 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000318 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000319 }
320#include "PassRegistry.def"
321
Chandler Carruth572e3402014-04-21 11:12:00 +0000322 return false;
323}
324
Chandler Carruth1ff77242015-03-07 09:02:36 +0000325bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
326 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000327#define FUNCTION_PASS(NAME, CREATE_PASS) \
328 if (Name == NAME) { \
329 FPM.addPass(CREATE_PASS); \
330 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000331 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000332#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
333 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000334 FPM.addPass(RequireAnalysisPass< \
335 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000336 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000337 } \
338 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000339 FPM.addPass(InvalidateAnalysisPass< \
340 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000341 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000342 }
343#include "PassRegistry.def"
344
Chandler Carruthd8330982014-01-12 09:34:22 +0000345 return false;
346}
347
Justin Bognereecc3c82016-02-25 07:23:08 +0000348bool PassBuilder::parseLoopPassName(LoopPassManager &FPM,
349 StringRef Name) {
350#define LOOP_PASS(NAME, CREATE_PASS) \
351 if (Name == NAME) { \
352 FPM.addPass(CREATE_PASS); \
353 return true; \
354 }
355#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
356 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000357 FPM.addPass(RequireAnalysisPass< \
358 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000359 return true; \
360 } \
361 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000362 FPM.addPass(InvalidateAnalysisPass< \
363 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000364 return true; \
365 }
366#include "PassRegistry.def"
367
368 return false;
369}
370
Chandler Carruthedf59962016-02-18 09:45:17 +0000371bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +0000372#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
373 if (Name == NAME) { \
374 AA.registerModuleAnalysis< \
375 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
376 return true; \
377 }
Chandler Carruthedf59962016-02-18 09:45:17 +0000378#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
379 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000380 AA.registerFunctionAnalysis< \
381 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +0000382 return true; \
383 }
384#include "PassRegistry.def"
385
386 return false;
387}
388
Justin Bognereecc3c82016-02-25 07:23:08 +0000389bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
390 StringRef &PipelineText,
391 bool VerifyEachPass,
392 bool DebugLogging) {
393 for (;;) {
394 // Parse nested pass managers by recursing.
395 if (PipelineText.startswith("loop(")) {
396 LoopPassManager NestedLPM(DebugLogging);
397
398 // Parse the inner pipeline inte the nested manager.
399 PipelineText = PipelineText.substr(strlen("loop("));
400 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
401 DebugLogging) ||
402 PipelineText.empty())
403 return false;
404 assert(PipelineText[0] == ')');
405 PipelineText = PipelineText.substr(1);
406
407 // Add the nested pass manager with the appropriate adaptor.
408 LPM.addPass(std::move(NestedLPM));
409 } else {
410 // Otherwise try to parse a pass name.
411 size_t End = PipelineText.find_first_of(",)");
412 if (!parseLoopPassName(LPM, PipelineText.substr(0, End)))
413 return false;
414 // TODO: Ideally, we would run a LoopVerifierPass() here in the
415 // VerifyEachPass case, but we don't have such a verifier yet.
416
417 PipelineText = PipelineText.substr(End);
418 }
419
420 if (PipelineText.empty() || PipelineText[0] == ')')
421 return true;
422
423 assert(PipelineText[0] == ',');
424 PipelineText = PipelineText.substr(1);
425 }
426}
427
Chandler Carruth1ff77242015-03-07 09:02:36 +0000428bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
429 StringRef &PipelineText,
430 bool VerifyEachPass,
431 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000432 for (;;) {
433 // Parse nested pass managers by recursing.
434 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000435 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000436
437 // Parse the inner pipeline inte the nested manager.
438 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000439 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
440 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000441 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000442 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000443 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000444 PipelineText = PipelineText.substr(1);
445
446 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000447 FPM.addPass(std::move(NestedFPM));
Justin Bognereecc3c82016-02-25 07:23:08 +0000448 } else if (PipelineText.startswith("loop(")) {
449 LoopPassManager NestedLPM(DebugLogging);
450
451 // Parse the inner pipeline inte the nested manager.
452 PipelineText = PipelineText.substr(strlen("loop("));
453 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
454 DebugLogging) ||
455 PipelineText.empty())
456 return false;
457 assert(PipelineText[0] == ')');
458 PipelineText = PipelineText.substr(1);
459
460 // Add the nested pass manager with the appropriate adaptor.
461 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(NestedLPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000462 } else {
463 // Otherwise try to parse a pass name.
464 size_t End = PipelineText.find_first_of(",)");
465 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
466 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000467 if (VerifyEachPass)
468 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000469
470 PipelineText = PipelineText.substr(End);
471 }
472
473 if (PipelineText.empty() || PipelineText[0] == ')')
474 return true;
475
476 assert(PipelineText[0] == ',');
477 PipelineText = PipelineText.substr(1);
478 }
479}
480
Chandler Carruth1ff77242015-03-07 09:02:36 +0000481bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
482 StringRef &PipelineText,
483 bool VerifyEachPass,
484 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000485 for (;;) {
486 // Parse nested pass managers by recursing.
487 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000488 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000489
490 // Parse the inner pipeline into the nested manager.
491 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000492 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
493 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000494 PipelineText.empty())
495 return false;
496 assert(PipelineText[0] == ')');
497 PipelineText = PipelineText.substr(1);
498
499 // Add the nested pass manager with the appropriate adaptor.
500 CGPM.addPass(std::move(NestedCGPM));
501 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000502 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000503
504 // Parse the inner pipeline inte the nested manager.
505 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000506 if (!parseFunctionPassPipeline(NestedFPM, 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(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
515 } else {
516 // Otherwise try to parse a pass name.
517 size_t End = PipelineText.find_first_of(",)");
518 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
519 return false;
520 // FIXME: No verifier support for CGSCC passes!
521
522 PipelineText = PipelineText.substr(End);
523 }
524
525 if (PipelineText.empty() || PipelineText[0] == ')')
526 return true;
527
528 assert(PipelineText[0] == ',');
529 PipelineText = PipelineText.substr(1);
530 }
531}
532
Chandler Carruth1ff77242015-03-07 09:02:36 +0000533bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
534 StringRef &PipelineText,
535 bool VerifyEachPass,
536 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000537 for (;;) {
538 // Parse nested pass managers by recursing.
539 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000540 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000541
542 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000543 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000544 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
545 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000546 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000547 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000548 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000549 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000550
551 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000552 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000553 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000554 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000555
556 // Parse the inner pipeline inte the nested manager.
557 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000558 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
559 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000560 PipelineText.empty())
561 return false;
562 assert(PipelineText[0] == ')');
563 PipelineText = PipelineText.substr(1);
564
565 // Add the nested pass manager with the appropriate adaptor.
566 MPM.addPass(
567 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000568 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000569 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000570
571 // Parse the inner pipeline inte the nested manager.
572 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000573 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
574 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000575 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000576 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000577 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000578 PipelineText = PipelineText.substr(1);
579
580 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000581 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000582 } else {
583 // Otherwise try to parse a pass name.
584 size_t End = PipelineText.find_first_of(",)");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000585 if (!parseModulePassName(MPM, PipelineText.substr(0, End), DebugLogging))
Chandler Carruth66445382014-01-11 08:16:35 +0000586 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000587 if (VerifyEachPass)
588 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000589
590 PipelineText = PipelineText.substr(End);
591 }
592
593 if (PipelineText.empty() || PipelineText[0] == ')')
594 return true;
595
596 assert(PipelineText[0] == ',');
597 PipelineText = PipelineText.substr(1);
598 }
599}
600
601// Primary pass pipeline description parsing routine.
602// FIXME: Should this routine accept a TargetMachine or require the caller to
603// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000604bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
605 StringRef PipelineText, bool VerifyEachPass,
606 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000607 // By default, try to parse the pipeline as-if it were within an implicit
608 // 'module(...)' pass pipeline. If this will parse at all, it needs to
609 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000610 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000611 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000612
Chandler Carruthea368f12015-01-06 08:37:58 +0000613 // This isn't parsable as a module pipeline, look for the end of a pass name
614 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000615 StringRef FirstName =
616 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000617 assert(!isModulePassName(FirstName) &&
618 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000619
Chandler Carruthea368f12015-01-06 08:37:58 +0000620 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
621 // pipeline.
Justin Bognereecc3c82016-02-25 07:23:08 +0000622 if (PipelineText.startswith("cgscc(") || isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000623 CGSCCPassManager CGPM(DebugLogging);
624 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
625 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000626 !PipelineText.empty())
627 return false;
628 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
629 return true;
630 }
631
Chandler Carruthea368f12015-01-06 08:37:58 +0000632 // Similarly, if this looks like a Function pass, parse the whole thing as
633 // a Function pipelien.
Justin Bognereecc3c82016-02-25 07:23:08 +0000634 if (PipelineText.startswith("function(") || isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000635 FunctionPassManager FPM(DebugLogging);
636 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
637 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000638 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000639 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000640 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000641 return true;
642 }
Chandler Carruth66445382014-01-11 08:16:35 +0000643
Justin Bognereecc3c82016-02-25 07:23:08 +0000644 // If this looks like a Loop pass, parse the whole thing as a Loop pipeline.
645 if (PipelineText.startswith("loop(") || isLoopPassName(FirstName)) {
646 LoopPassManager LPM(DebugLogging);
647 if (!parseLoopPassPipeline(LPM, PipelineText, VerifyEachPass,
648 DebugLogging) ||
649 !PipelineText.empty())
650 return false;
651 FunctionPassManager FPM(DebugLogging);
652 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
653 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
654 return true;
655 }
656
657
Chandler Carruth66445382014-01-11 08:16:35 +0000658 return false;
659}
Chandler Carruthedf59962016-02-18 09:45:17 +0000660
661bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
662 while (!PipelineText.empty()) {
663 StringRef Name;
664 std::tie(Name, PipelineText) = PipelineText.split(',');
665 if (!parseAAPassName(AA, Name))
666 return false;
667 }
668
669 return true;
670}