blob: 6d6993b305db1b15436f3642a871a863d9b8dcc2 [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"
Chandler Carruth3a040e62015-12-27 08:41:34 +000050#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Justin Bogner21e15372015-10-30 23:28:12 +000051#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000052#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000053#include "llvm/Transforms/InstrProfiling.h"
Justin Bogner19b67992015-10-30 23:13:18 +000054#include "llvm/Transforms/Scalar/ADCE.h"
Justin Bogner395c2122016-04-22 19:40:41 +000055#include "llvm/Transforms/Scalar/DCE.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000056#include "llvm/Transforms/Scalar/EarlyCSE.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000057#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Chandler Carruth89c45a12016-03-11 08:50:55 +000058#include "llvm/Transforms/Scalar/GVN.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000059#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000060#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +000061#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruth58dde8c2016-02-26 12:17:54 +000062#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +000063
64using namespace llvm;
65
Chandler Carruth8b5a74192016-02-28 22:16:03 +000066static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
67
Chandler Carruth66445382014-01-11 08:16:35 +000068namespace {
69
Chandler Carruthd8330982014-01-12 09:34:22 +000070/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000071struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000072 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000073 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000074};
75
Chandler Carruth0b576b32015-01-06 02:50:06 +000076/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +000077class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
78 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +000079 static char PassID;
80
81public:
Chandler Carruth0b576b32015-01-06 02:50:06 +000082 struct Result {};
83 Result run(Module &) { return Result(); }
84 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +000085};
86
Chandler Carruth572e3402014-04-21 11:12:00 +000087/// \brief No-op CGSCC pass which does nothing.
88struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000089 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +000090 return PreservedAnalyses::all();
91 }
92 static StringRef name() { return "NoOpCGSCCPass"; }
93};
94
Chandler Carruth0b576b32015-01-06 02:50:06 +000095/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +000096class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
97 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +000098 static char PassID;
99
100public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000101 struct Result {};
102 Result run(LazyCallGraph::SCC &) { return Result(); }
103 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000104};
105
Chandler Carruthd8330982014-01-12 09:34:22 +0000106/// \brief No-op function pass which does nothing.
107struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000108 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +0000109 static StringRef name() { return "NoOpFunctionPass"; }
110};
111
Chandler Carruth0b576b32015-01-06 02:50:06 +0000112/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000113class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
114 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000115 static char PassID;
116
117public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000118 struct Result {};
119 Result run(Function &) { return Result(); }
120 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000121};
122
Justin Bognereecc3c82016-02-25 07:23:08 +0000123/// \brief No-op loop pass which does nothing.
124struct NoOpLoopPass {
125 PreservedAnalyses run(Loop &L) { return PreservedAnalyses::all(); }
126 static StringRef name() { return "NoOpLoopPass"; }
127};
128
129/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000130class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
131 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000132 static char PassID;
133
134public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000135 struct Result {};
136 Result run(Loop &) { return Result(); }
137 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000138};
139
Chandler Carruthb4faf132016-03-11 10:22:49 +0000140char NoOpModuleAnalysis::PassID;
141char NoOpCGSCCAnalysis::PassID;
142char NoOpFunctionAnalysis::PassID;
143char NoOpLoopAnalysis::PassID;
144
Chandler Carruth66445382014-01-11 08:16:35 +0000145} // End anonymous namespace.
146
Chandler Carruth1ff77242015-03-07 09:02:36 +0000147void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000148#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000149 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000150#include "PassRegistry.def"
151}
152
Chandler Carruth1ff77242015-03-07 09:02:36 +0000153void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000154#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000155 CGAM.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::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000160#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000161 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000162#include "PassRegistry.def"
163}
164
Justin Bognereecc3c82016-02-25 07:23:08 +0000165void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
166#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
167 LAM.registerPass([&] { return CREATE_PASS; });
168#include "PassRegistry.def"
169}
170
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000171void PassBuilder::addPerModuleDefaultPipeline(ModulePassManager &MPM,
172 OptimizationLevel Level,
173 bool DebugLogging) {
174 // FIXME: Finish fleshing this out to match the legacy pipelines.
175 FunctionPassManager EarlyFPM(DebugLogging);
176 EarlyFPM.addPass(SimplifyCFGPass());
177 EarlyFPM.addPass(SROA());
178 EarlyFPM.addPass(EarlyCSEPass());
179 EarlyFPM.addPass(LowerExpectIntrinsicPass());
180
181 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
182}
183
184void PassBuilder::addLTOPreLinkDefaultPipeline(ModulePassManager &MPM,
185 OptimizationLevel Level,
186 bool DebugLogging) {
187 // FIXME: We should use a customized pre-link pipeline!
188 addPerModuleDefaultPipeline(MPM, Level, DebugLogging);
189}
190
191void PassBuilder::addLTODefaultPipeline(ModulePassManager &MPM,
192 OptimizationLevel Level,
193 bool DebugLogging) {
194 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
195 FunctionPassManager LateFPM(DebugLogging);
196 LateFPM.addPass(InstCombinePass());
197 LateFPM.addPass(SimplifyCFGPass());
198
199 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
200}
201
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000202#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000203static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000204 // Manually handle aliases for pre-configured pipeline fragments.
205 if (Name.startswith("default") || Name.startswith("lto"))
206 return DefaultAliasRegex.match(Name);
207
Chandler Carruth58944182014-04-21 08:08:50 +0000208#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000209#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000210 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000211 return true;
212#include "PassRegistry.def"
213
Chandler Carruth66445382014-01-11 08:16:35 +0000214 return false;
215}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000216#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000217
Chandler Carruth572e3402014-04-21 11:12:00 +0000218static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000219#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000220#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000221 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000222 return true;
223#include "PassRegistry.def"
224
Chandler Carruth572e3402014-04-21 11:12:00 +0000225 return false;
226}
227
Chandler Carruthd8330982014-01-12 09:34:22 +0000228static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000229#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000230#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000231 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000232 return true;
233#include "PassRegistry.def"
234
Chandler Carruthd8330982014-01-12 09:34:22 +0000235 return false;
236}
237
Justin Bognereecc3c82016-02-25 07:23:08 +0000238static bool isLoopPassName(StringRef Name) {
239#define LOOP_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
240#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
241 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
242 return true;
243#include "PassRegistry.def"
244
245 return false;
246}
247
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000248bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name,
249 bool DebugLogging) {
250 // Manually handle aliases for pre-configured pipeline fragments.
251 if (Name.startswith("default") || Name.startswith("lto")) {
252 SmallVector<StringRef, 3> Matches;
253 if (!DefaultAliasRegex.match(Name, &Matches))
254 return false;
255 assert(Matches.size() == 3 && "Must capture two matched strings!");
256
257 auto L = StringSwitch<OptimizationLevel>(Matches[2])
258 .Case("O0", O0)
259 .Case("O1", O1)
260 .Case("O2", O2)
261 .Case("O3", O3)
262 .Case("Os", Os)
263 .Case("Oz", Oz);
264
265 if (Matches[1] == "default") {
266 addPerModuleDefaultPipeline(MPM, L, DebugLogging);
267 } else if (Matches[1] == "lto-pre-link") {
268 addLTOPreLinkDefaultPipeline(MPM, L, DebugLogging);
269 } else {
270 assert(Matches[1] == "lto" && "Not one of the matched options!");
271 addLTODefaultPipeline(MPM, L, DebugLogging);
272 }
273 return true;
274 }
275
Chandler Carruth58944182014-04-21 08:08:50 +0000276#define MODULE_PASS(NAME, CREATE_PASS) \
277 if (Name == NAME) { \
278 MPM.addPass(CREATE_PASS); \
279 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000280 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000281#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
282 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000283 MPM.addPass(RequireAnalysisPass< \
284 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000285 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000286 } \
287 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000288 MPM.addPass(InvalidateAnalysisPass< \
289 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000290 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000291 }
292#include "PassRegistry.def"
293
Chandler Carruth66445382014-01-11 08:16:35 +0000294 return false;
295}
296
Chandler Carruth1ff77242015-03-07 09:02:36 +0000297bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000298#define CGSCC_PASS(NAME, CREATE_PASS) \
299 if (Name == NAME) { \
300 CGPM.addPass(CREATE_PASS); \
301 return true; \
302 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000303#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
304 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000305 CGPM.addPass(RequireAnalysisPass< \
306 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000307 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000308 } \
309 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000310 CGPM.addPass(InvalidateAnalysisPass< \
311 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000312 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000313 }
314#include "PassRegistry.def"
315
Chandler Carruth572e3402014-04-21 11:12:00 +0000316 return false;
317}
318
Chandler Carruth1ff77242015-03-07 09:02:36 +0000319bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
320 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000321#define FUNCTION_PASS(NAME, CREATE_PASS) \
322 if (Name == NAME) { \
323 FPM.addPass(CREATE_PASS); \
324 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000325 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000326#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
327 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000328 FPM.addPass(RequireAnalysisPass< \
329 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000330 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000331 } \
332 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000333 FPM.addPass(InvalidateAnalysisPass< \
334 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000335 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000336 }
337#include "PassRegistry.def"
338
Chandler Carruthd8330982014-01-12 09:34:22 +0000339 return false;
340}
341
Justin Bognereecc3c82016-02-25 07:23:08 +0000342bool PassBuilder::parseLoopPassName(LoopPassManager &FPM,
343 StringRef Name) {
344#define LOOP_PASS(NAME, CREATE_PASS) \
345 if (Name == NAME) { \
346 FPM.addPass(CREATE_PASS); \
347 return true; \
348 }
349#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
350 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000351 FPM.addPass(RequireAnalysisPass< \
352 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000353 return true; \
354 } \
355 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000356 FPM.addPass(InvalidateAnalysisPass< \
357 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000358 return true; \
359 }
360#include "PassRegistry.def"
361
362 return false;
363}
364
Chandler Carruthedf59962016-02-18 09:45:17 +0000365bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +0000366#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
367 if (Name == NAME) { \
368 AA.registerModuleAnalysis< \
369 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
370 return true; \
371 }
Chandler Carruthedf59962016-02-18 09:45:17 +0000372#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
373 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000374 AA.registerFunctionAnalysis< \
375 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +0000376 return true; \
377 }
378#include "PassRegistry.def"
379
380 return false;
381}
382
Justin Bognereecc3c82016-02-25 07:23:08 +0000383bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
384 StringRef &PipelineText,
385 bool VerifyEachPass,
386 bool DebugLogging) {
387 for (;;) {
388 // Parse nested pass managers by recursing.
389 if (PipelineText.startswith("loop(")) {
390 LoopPassManager NestedLPM(DebugLogging);
391
392 // Parse the inner pipeline inte the nested manager.
393 PipelineText = PipelineText.substr(strlen("loop("));
394 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
395 DebugLogging) ||
396 PipelineText.empty())
397 return false;
398 assert(PipelineText[0] == ')');
399 PipelineText = PipelineText.substr(1);
400
401 // Add the nested pass manager with the appropriate adaptor.
402 LPM.addPass(std::move(NestedLPM));
403 } else {
404 // Otherwise try to parse a pass name.
405 size_t End = PipelineText.find_first_of(",)");
406 if (!parseLoopPassName(LPM, PipelineText.substr(0, End)))
407 return false;
408 // TODO: Ideally, we would run a LoopVerifierPass() here in the
409 // VerifyEachPass case, but we don't have such a verifier yet.
410
411 PipelineText = PipelineText.substr(End);
412 }
413
414 if (PipelineText.empty() || PipelineText[0] == ')')
415 return true;
416
417 assert(PipelineText[0] == ',');
418 PipelineText = PipelineText.substr(1);
419 }
420}
421
Chandler Carruth1ff77242015-03-07 09:02:36 +0000422bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
423 StringRef &PipelineText,
424 bool VerifyEachPass,
425 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000426 for (;;) {
427 // Parse nested pass managers by recursing.
428 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000429 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000430
431 // Parse the inner pipeline inte the nested manager.
432 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000433 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
434 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000435 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000436 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000437 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000438 PipelineText = PipelineText.substr(1);
439
440 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000441 FPM.addPass(std::move(NestedFPM));
Justin Bognereecc3c82016-02-25 07:23:08 +0000442 } else if (PipelineText.startswith("loop(")) {
443 LoopPassManager NestedLPM(DebugLogging);
444
445 // Parse the inner pipeline inte the nested manager.
446 PipelineText = PipelineText.substr(strlen("loop("));
447 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
448 DebugLogging) ||
449 PipelineText.empty())
450 return false;
451 assert(PipelineText[0] == ')');
452 PipelineText = PipelineText.substr(1);
453
454 // Add the nested pass manager with the appropriate adaptor.
455 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(NestedLPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000456 } else {
457 // Otherwise try to parse a pass name.
458 size_t End = PipelineText.find_first_of(",)");
459 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
460 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000461 if (VerifyEachPass)
462 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000463
464 PipelineText = PipelineText.substr(End);
465 }
466
467 if (PipelineText.empty() || PipelineText[0] == ')')
468 return true;
469
470 assert(PipelineText[0] == ',');
471 PipelineText = PipelineText.substr(1);
472 }
473}
474
Chandler Carruth1ff77242015-03-07 09:02:36 +0000475bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
476 StringRef &PipelineText,
477 bool VerifyEachPass,
478 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000479 for (;;) {
480 // Parse nested pass managers by recursing.
481 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000482 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000483
484 // Parse the inner pipeline into the nested manager.
485 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000486 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
487 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000488 PipelineText.empty())
489 return false;
490 assert(PipelineText[0] == ')');
491 PipelineText = PipelineText.substr(1);
492
493 // Add the nested pass manager with the appropriate adaptor.
494 CGPM.addPass(std::move(NestedCGPM));
495 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000496 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000497
498 // Parse the inner pipeline inte the nested manager.
499 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000500 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
501 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000502 PipelineText.empty())
503 return false;
504 assert(PipelineText[0] == ')');
505 PipelineText = PipelineText.substr(1);
506
507 // Add the nested pass manager with the appropriate adaptor.
508 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
509 } else {
510 // Otherwise try to parse a pass name.
511 size_t End = PipelineText.find_first_of(",)");
512 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
513 return false;
514 // FIXME: No verifier support for CGSCC passes!
515
516 PipelineText = PipelineText.substr(End);
517 }
518
519 if (PipelineText.empty() || PipelineText[0] == ')')
520 return true;
521
522 assert(PipelineText[0] == ',');
523 PipelineText = PipelineText.substr(1);
524 }
525}
526
Chandler Carruth1ff77242015-03-07 09:02:36 +0000527bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
528 StringRef &PipelineText,
529 bool VerifyEachPass,
530 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000531 for (;;) {
532 // Parse nested pass managers by recursing.
533 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000534 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000535
536 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000537 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000538 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
539 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000540 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000541 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000542 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000543 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000544
545 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000546 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000547 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000548 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000549
550 // Parse the inner pipeline inte the nested manager.
551 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000552 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
553 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000554 PipelineText.empty())
555 return false;
556 assert(PipelineText[0] == ')');
557 PipelineText = PipelineText.substr(1);
558
559 // Add the nested pass manager with the appropriate adaptor.
560 MPM.addPass(
561 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000562 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000563 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000564
565 // Parse the inner pipeline inte the nested manager.
566 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000567 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
568 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000569 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000570 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000571 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000572 PipelineText = PipelineText.substr(1);
573
574 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000575 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000576 } else {
577 // Otherwise try to parse a pass name.
578 size_t End = PipelineText.find_first_of(",)");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000579 if (!parseModulePassName(MPM, PipelineText.substr(0, End), DebugLogging))
Chandler Carruth66445382014-01-11 08:16:35 +0000580 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000581 if (VerifyEachPass)
582 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000583
584 PipelineText = PipelineText.substr(End);
585 }
586
587 if (PipelineText.empty() || PipelineText[0] == ')')
588 return true;
589
590 assert(PipelineText[0] == ',');
591 PipelineText = PipelineText.substr(1);
592 }
593}
594
595// Primary pass pipeline description parsing routine.
596// FIXME: Should this routine accept a TargetMachine or require the caller to
597// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000598bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
599 StringRef PipelineText, bool VerifyEachPass,
600 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000601 // By default, try to parse the pipeline as-if it were within an implicit
602 // 'module(...)' pass pipeline. If this will parse at all, it needs to
603 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000604 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000605 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000606
Chandler Carruthea368f12015-01-06 08:37:58 +0000607 // This isn't parsable as a module pipeline, look for the end of a pass name
608 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000609 StringRef FirstName =
610 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000611 assert(!isModulePassName(FirstName) &&
612 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000613
Chandler Carruthea368f12015-01-06 08:37:58 +0000614 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
615 // pipeline.
Justin Bognereecc3c82016-02-25 07:23:08 +0000616 if (PipelineText.startswith("cgscc(") || isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000617 CGSCCPassManager CGPM(DebugLogging);
618 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
619 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000620 !PipelineText.empty())
621 return false;
622 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
623 return true;
624 }
625
Chandler Carruthea368f12015-01-06 08:37:58 +0000626 // Similarly, if this looks like a Function pass, parse the whole thing as
627 // a Function pipelien.
Justin Bognereecc3c82016-02-25 07:23:08 +0000628 if (PipelineText.startswith("function(") || isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000629 FunctionPassManager FPM(DebugLogging);
630 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
631 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000632 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000633 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000634 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000635 return true;
636 }
Chandler Carruth66445382014-01-11 08:16:35 +0000637
Justin Bognereecc3c82016-02-25 07:23:08 +0000638 // If this looks like a Loop pass, parse the whole thing as a Loop pipeline.
639 if (PipelineText.startswith("loop(") || isLoopPassName(FirstName)) {
640 LoopPassManager LPM(DebugLogging);
641 if (!parseLoopPassPipeline(LPM, PipelineText, VerifyEachPass,
642 DebugLogging) ||
643 !PipelineText.empty())
644 return false;
645 FunctionPassManager FPM(DebugLogging);
646 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
647 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
648 return true;
649 }
650
651
Chandler Carruth66445382014-01-11 08:16:35 +0000652 return false;
653}
Chandler Carruthedf59962016-02-18 09:45:17 +0000654
655bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
656 while (!PipelineText.empty()) {
657 StringRef Name;
658 std::tie(Name, PipelineText) = PipelineText.split(',');
659 if (!parseAAPassName(AA, Name))
660 return false;
661 }
662
663 return true;
664}