blob: ad6ce725c1283752dfe1f887564d76c20354a9c2 [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"
Hongbin Zheng751337f2016-02-25 17:54:15 +000027#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruth45a9c202016-03-11 09:15:11 +000028#include "llvm/Analysis/GlobalsModRef.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000029#include "llvm/Analysis/LazyCallGraph.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000030#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth61440d22016-03-10 00:55:30 +000031#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000032#include "llvm/Analysis/PostDominators.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000033#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000034#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000035#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000036#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000037#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000038#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000039#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000040#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000041#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000042#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000043#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000044#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000045#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000046#include "llvm/Target/TargetMachine.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000047#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000048#include "llvm/Transforms/IPO/FunctionAttrs.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000049#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Justin Bogner21e15372015-10-30 23:28:12 +000050#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000051#include "llvm/Transforms/InstCombine/InstCombine.h"
Justin Bogner19b67992015-10-30 23:13:18 +000052#include "llvm/Transforms/Scalar/ADCE.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000053#include "llvm/Transforms/Scalar/EarlyCSE.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000054#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Chandler Carruth89c45a12016-03-11 08:50:55 +000055#include "llvm/Transforms/Scalar/GVN.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000056#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000057#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Chandler Carruth58dde8c2016-02-26 12:17:54 +000058#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +000059
60using namespace llvm;
61
Chandler Carruth8b5a74192016-02-28 22:16:03 +000062static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
63
Chandler Carruth66445382014-01-11 08:16:35 +000064namespace {
65
Chandler Carruthd8330982014-01-12 09:34:22 +000066/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000067struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000068 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000069 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000070};
71
Chandler Carruth0b576b32015-01-06 02:50:06 +000072/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +000073class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
74 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +000075 static char PassID;
76
77public:
Chandler Carruth0b576b32015-01-06 02:50:06 +000078 struct Result {};
79 Result run(Module &) { return Result(); }
80 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +000081};
82
Chandler Carruth572e3402014-04-21 11:12:00 +000083/// \brief No-op CGSCC pass which does nothing.
84struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000085 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +000086 return PreservedAnalyses::all();
87 }
88 static StringRef name() { return "NoOpCGSCCPass"; }
89};
90
Chandler Carruth0b576b32015-01-06 02:50:06 +000091/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +000092class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
93 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
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(LazyCallGraph::SCC &) { return Result(); }
99 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000100};
101
Chandler Carruthd8330982014-01-12 09:34:22 +0000102/// \brief No-op function pass which does nothing.
103struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000104 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +0000105 static StringRef name() { return "NoOpFunctionPass"; }
106};
107
Chandler Carruth0b576b32015-01-06 02:50:06 +0000108/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000109class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
110 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000111 static char PassID;
112
113public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000114 struct Result {};
115 Result run(Function &) { return Result(); }
116 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000117};
118
Justin Bognereecc3c82016-02-25 07:23:08 +0000119/// \brief No-op loop pass which does nothing.
120struct NoOpLoopPass {
121 PreservedAnalyses run(Loop &L) { return PreservedAnalyses::all(); }
122 static StringRef name() { return "NoOpLoopPass"; }
123};
124
125/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000126class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
127 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000128 static char PassID;
129
130public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000131 struct Result {};
132 Result run(Loop &) { return Result(); }
133 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000134};
135
Chandler Carruthb4faf132016-03-11 10:22:49 +0000136char NoOpModuleAnalysis::PassID;
137char NoOpCGSCCAnalysis::PassID;
138char NoOpFunctionAnalysis::PassID;
139char NoOpLoopAnalysis::PassID;
140
Chandler Carruth66445382014-01-11 08:16:35 +0000141} // End anonymous namespace.
142
Chandler Carruth1ff77242015-03-07 09:02:36 +0000143void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000144#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000145 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000146#include "PassRegistry.def"
147}
148
Chandler Carruth1ff77242015-03-07 09:02:36 +0000149void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000150#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000151 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000152#include "PassRegistry.def"
153}
154
Chandler Carruth1ff77242015-03-07 09:02:36 +0000155void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000156#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000157 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000158#include "PassRegistry.def"
159}
160
Justin Bognereecc3c82016-02-25 07:23:08 +0000161void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
162#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
163 LAM.registerPass([&] { return CREATE_PASS; });
164#include "PassRegistry.def"
165}
166
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000167void PassBuilder::addPerModuleDefaultPipeline(ModulePassManager &MPM,
168 OptimizationLevel Level,
169 bool DebugLogging) {
170 // FIXME: Finish fleshing this out to match the legacy pipelines.
171 FunctionPassManager EarlyFPM(DebugLogging);
172 EarlyFPM.addPass(SimplifyCFGPass());
173 EarlyFPM.addPass(SROA());
174 EarlyFPM.addPass(EarlyCSEPass());
175 EarlyFPM.addPass(LowerExpectIntrinsicPass());
176
177 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
178}
179
180void PassBuilder::addLTOPreLinkDefaultPipeline(ModulePassManager &MPM,
181 OptimizationLevel Level,
182 bool DebugLogging) {
183 // FIXME: We should use a customized pre-link pipeline!
184 addPerModuleDefaultPipeline(MPM, Level, DebugLogging);
185}
186
187void PassBuilder::addLTODefaultPipeline(ModulePassManager &MPM,
188 OptimizationLevel Level,
189 bool DebugLogging) {
190 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
191 FunctionPassManager LateFPM(DebugLogging);
192 LateFPM.addPass(InstCombinePass());
193 LateFPM.addPass(SimplifyCFGPass());
194
195 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
196}
197
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000198#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000199static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000200 // Manually handle aliases for pre-configured pipeline fragments.
201 if (Name.startswith("default") || Name.startswith("lto"))
202 return DefaultAliasRegex.match(Name);
203
Chandler Carruth58944182014-04-21 08:08:50 +0000204#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000205#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000206 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000207 return true;
208#include "PassRegistry.def"
209
Chandler Carruth66445382014-01-11 08:16:35 +0000210 return false;
211}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000212#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000213
Chandler Carruth572e3402014-04-21 11:12:00 +0000214static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000215#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000216#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000217 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000218 return true;
219#include "PassRegistry.def"
220
Chandler Carruth572e3402014-04-21 11:12:00 +0000221 return false;
222}
223
Chandler Carruthd8330982014-01-12 09:34:22 +0000224static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000225#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000226#define FUNCTION_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 Carruthd8330982014-01-12 09:34:22 +0000231 return false;
232}
233
Justin Bognereecc3c82016-02-25 07:23:08 +0000234static bool isLoopPassName(StringRef Name) {
235#define LOOP_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
236#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
237 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
238 return true;
239#include "PassRegistry.def"
240
241 return false;
242}
243
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000244bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name,
245 bool DebugLogging) {
246 // Manually handle aliases for pre-configured pipeline fragments.
247 if (Name.startswith("default") || Name.startswith("lto")) {
248 SmallVector<StringRef, 3> Matches;
249 if (!DefaultAliasRegex.match(Name, &Matches))
250 return false;
251 assert(Matches.size() == 3 && "Must capture two matched strings!");
252
253 auto L = StringSwitch<OptimizationLevel>(Matches[2])
254 .Case("O0", O0)
255 .Case("O1", O1)
256 .Case("O2", O2)
257 .Case("O3", O3)
258 .Case("Os", Os)
259 .Case("Oz", Oz);
260
261 if (Matches[1] == "default") {
262 addPerModuleDefaultPipeline(MPM, L, DebugLogging);
263 } else if (Matches[1] == "lto-pre-link") {
264 addLTOPreLinkDefaultPipeline(MPM, L, DebugLogging);
265 } else {
266 assert(Matches[1] == "lto" && "Not one of the matched options!");
267 addLTODefaultPipeline(MPM, L, DebugLogging);
268 }
269 return true;
270 }
271
Chandler Carruth58944182014-04-21 08:08:50 +0000272#define MODULE_PASS(NAME, CREATE_PASS) \
273 if (Name == NAME) { \
274 MPM.addPass(CREATE_PASS); \
275 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000276 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000277#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
278 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000279 MPM.addPass(RequireAnalysisPass< \
280 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000281 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000282 } \
283 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000284 MPM.addPass(InvalidateAnalysisPass< \
285 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000286 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000287 }
288#include "PassRegistry.def"
289
Chandler Carruth66445382014-01-11 08:16:35 +0000290 return false;
291}
292
Chandler Carruth1ff77242015-03-07 09:02:36 +0000293bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000294#define CGSCC_PASS(NAME, CREATE_PASS) \
295 if (Name == NAME) { \
296 CGPM.addPass(CREATE_PASS); \
297 return true; \
298 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000299#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
300 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000301 CGPM.addPass(RequireAnalysisPass< \
302 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000303 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000304 } \
305 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000306 CGPM.addPass(InvalidateAnalysisPass< \
307 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000308 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000309 }
310#include "PassRegistry.def"
311
Chandler Carruth572e3402014-04-21 11:12:00 +0000312 return false;
313}
314
Chandler Carruth1ff77242015-03-07 09:02:36 +0000315bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
316 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000317#define FUNCTION_PASS(NAME, CREATE_PASS) \
318 if (Name == NAME) { \
319 FPM.addPass(CREATE_PASS); \
320 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000321 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000322#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
323 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000324 FPM.addPass(RequireAnalysisPass< \
325 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000326 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000327 } \
328 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000329 FPM.addPass(InvalidateAnalysisPass< \
330 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000331 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000332 }
333#include "PassRegistry.def"
334
Chandler Carruthd8330982014-01-12 09:34:22 +0000335 return false;
336}
337
Justin Bognereecc3c82016-02-25 07:23:08 +0000338bool PassBuilder::parseLoopPassName(LoopPassManager &FPM,
339 StringRef Name) {
340#define LOOP_PASS(NAME, CREATE_PASS) \
341 if (Name == NAME) { \
342 FPM.addPass(CREATE_PASS); \
343 return true; \
344 }
345#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
346 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000347 FPM.addPass(RequireAnalysisPass< \
348 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000349 return true; \
350 } \
351 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000352 FPM.addPass(InvalidateAnalysisPass< \
353 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000354 return true; \
355 }
356#include "PassRegistry.def"
357
358 return false;
359}
360
Chandler Carruthedf59962016-02-18 09:45:17 +0000361bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +0000362#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
363 if (Name == NAME) { \
364 AA.registerModuleAnalysis< \
365 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
366 return true; \
367 }
Chandler Carruthedf59962016-02-18 09:45:17 +0000368#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
369 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000370 AA.registerFunctionAnalysis< \
371 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +0000372 return true; \
373 }
374#include "PassRegistry.def"
375
376 return false;
377}
378
Justin Bognereecc3c82016-02-25 07:23:08 +0000379bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
380 StringRef &PipelineText,
381 bool VerifyEachPass,
382 bool DebugLogging) {
383 for (;;) {
384 // Parse nested pass managers by recursing.
385 if (PipelineText.startswith("loop(")) {
386 LoopPassManager NestedLPM(DebugLogging);
387
388 // Parse the inner pipeline inte the nested manager.
389 PipelineText = PipelineText.substr(strlen("loop("));
390 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
391 DebugLogging) ||
392 PipelineText.empty())
393 return false;
394 assert(PipelineText[0] == ')');
395 PipelineText = PipelineText.substr(1);
396
397 // Add the nested pass manager with the appropriate adaptor.
398 LPM.addPass(std::move(NestedLPM));
399 } else {
400 // Otherwise try to parse a pass name.
401 size_t End = PipelineText.find_first_of(",)");
402 if (!parseLoopPassName(LPM, PipelineText.substr(0, End)))
403 return false;
404 // TODO: Ideally, we would run a LoopVerifierPass() here in the
405 // VerifyEachPass case, but we don't have such a verifier yet.
406
407 PipelineText = PipelineText.substr(End);
408 }
409
410 if (PipelineText.empty() || PipelineText[0] == ')')
411 return true;
412
413 assert(PipelineText[0] == ',');
414 PipelineText = PipelineText.substr(1);
415 }
416}
417
Chandler Carruth1ff77242015-03-07 09:02:36 +0000418bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
419 StringRef &PipelineText,
420 bool VerifyEachPass,
421 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000422 for (;;) {
423 // Parse nested pass managers by recursing.
424 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000425 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000426
427 // Parse the inner pipeline inte the nested manager.
428 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000429 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
430 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000431 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000432 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000433 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000434 PipelineText = PipelineText.substr(1);
435
436 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000437 FPM.addPass(std::move(NestedFPM));
Justin Bognereecc3c82016-02-25 07:23:08 +0000438 } else if (PipelineText.startswith("loop(")) {
439 LoopPassManager NestedLPM(DebugLogging);
440
441 // Parse the inner pipeline inte the nested manager.
442 PipelineText = PipelineText.substr(strlen("loop("));
443 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
444 DebugLogging) ||
445 PipelineText.empty())
446 return false;
447 assert(PipelineText[0] == ')');
448 PipelineText = PipelineText.substr(1);
449
450 // Add the nested pass manager with the appropriate adaptor.
451 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(NestedLPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000452 } else {
453 // Otherwise try to parse a pass name.
454 size_t End = PipelineText.find_first_of(",)");
455 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
456 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000457 if (VerifyEachPass)
458 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000459
460 PipelineText = PipelineText.substr(End);
461 }
462
463 if (PipelineText.empty() || PipelineText[0] == ')')
464 return true;
465
466 assert(PipelineText[0] == ',');
467 PipelineText = PipelineText.substr(1);
468 }
469}
470
Chandler Carruth1ff77242015-03-07 09:02:36 +0000471bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
472 StringRef &PipelineText,
473 bool VerifyEachPass,
474 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000475 for (;;) {
476 // Parse nested pass managers by recursing.
477 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000478 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000479
480 // Parse the inner pipeline into the nested manager.
481 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000482 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
483 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000484 PipelineText.empty())
485 return false;
486 assert(PipelineText[0] == ')');
487 PipelineText = PipelineText.substr(1);
488
489 // Add the nested pass manager with the appropriate adaptor.
490 CGPM.addPass(std::move(NestedCGPM));
491 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000492 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000493
494 // Parse the inner pipeline inte the nested manager.
495 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000496 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
497 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000498 PipelineText.empty())
499 return false;
500 assert(PipelineText[0] == ')');
501 PipelineText = PipelineText.substr(1);
502
503 // Add the nested pass manager with the appropriate adaptor.
504 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
505 } else {
506 // Otherwise try to parse a pass name.
507 size_t End = PipelineText.find_first_of(",)");
508 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
509 return false;
510 // FIXME: No verifier support for CGSCC passes!
511
512 PipelineText = PipelineText.substr(End);
513 }
514
515 if (PipelineText.empty() || PipelineText[0] == ')')
516 return true;
517
518 assert(PipelineText[0] == ',');
519 PipelineText = PipelineText.substr(1);
520 }
521}
522
Chandler Carruth1ff77242015-03-07 09:02:36 +0000523bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
524 StringRef &PipelineText,
525 bool VerifyEachPass,
526 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000527 for (;;) {
528 // Parse nested pass managers by recursing.
529 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000530 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000531
532 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000533 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000534 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
535 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000536 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000537 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000538 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000539 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000540
541 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000542 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000543 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000544 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000545
546 // Parse the inner pipeline inte the nested manager.
547 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000548 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
549 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000550 PipelineText.empty())
551 return false;
552 assert(PipelineText[0] == ')');
553 PipelineText = PipelineText.substr(1);
554
555 // Add the nested pass manager with the appropriate adaptor.
556 MPM.addPass(
557 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000558 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000559 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000560
561 // Parse the inner pipeline inte the nested manager.
562 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000563 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
564 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000565 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000566 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000567 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000568 PipelineText = PipelineText.substr(1);
569
570 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000571 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000572 } else {
573 // Otherwise try to parse a pass name.
574 size_t End = PipelineText.find_first_of(",)");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000575 if (!parseModulePassName(MPM, PipelineText.substr(0, End), DebugLogging))
Chandler Carruth66445382014-01-11 08:16:35 +0000576 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000577 if (VerifyEachPass)
578 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000579
580 PipelineText = PipelineText.substr(End);
581 }
582
583 if (PipelineText.empty() || PipelineText[0] == ')')
584 return true;
585
586 assert(PipelineText[0] == ',');
587 PipelineText = PipelineText.substr(1);
588 }
589}
590
591// Primary pass pipeline description parsing routine.
592// FIXME: Should this routine accept a TargetMachine or require the caller to
593// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000594bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
595 StringRef PipelineText, bool VerifyEachPass,
596 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000597 // By default, try to parse the pipeline as-if it were within an implicit
598 // 'module(...)' pass pipeline. If this will parse at all, it needs to
599 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000600 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000601 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000602
Chandler Carruthea368f12015-01-06 08:37:58 +0000603 // This isn't parsable as a module pipeline, look for the end of a pass name
604 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000605 StringRef FirstName =
606 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000607 assert(!isModulePassName(FirstName) &&
608 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000609
Chandler Carruthea368f12015-01-06 08:37:58 +0000610 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
611 // pipeline.
Justin Bognereecc3c82016-02-25 07:23:08 +0000612 if (PipelineText.startswith("cgscc(") || isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000613 CGSCCPassManager CGPM(DebugLogging);
614 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
615 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000616 !PipelineText.empty())
617 return false;
618 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
619 return true;
620 }
621
Chandler Carruthea368f12015-01-06 08:37:58 +0000622 // Similarly, if this looks like a Function pass, parse the whole thing as
623 // a Function pipelien.
Justin Bognereecc3c82016-02-25 07:23:08 +0000624 if (PipelineText.startswith("function(") || isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000625 FunctionPassManager FPM(DebugLogging);
626 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
627 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000628 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000629 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000630 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000631 return true;
632 }
Chandler Carruth66445382014-01-11 08:16:35 +0000633
Justin Bognereecc3c82016-02-25 07:23:08 +0000634 // If this looks like a Loop pass, parse the whole thing as a Loop pipeline.
635 if (PipelineText.startswith("loop(") || isLoopPassName(FirstName)) {
636 LoopPassManager LPM(DebugLogging);
637 if (!parseLoopPassPipeline(LPM, PipelineText, VerifyEachPass,
638 DebugLogging) ||
639 !PipelineText.empty())
640 return false;
641 FunctionPassManager FPM(DebugLogging);
642 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
643 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
644 return true;
645 }
646
647
Chandler Carruth66445382014-01-11 08:16:35 +0000648 return false;
649}
Chandler Carruthedf59962016-02-18 09:45:17 +0000650
651bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
652 while (!PipelineText.empty()) {
653 StringRef Name;
654 std::tie(Name, PipelineText) = PipelineText.split(',');
655 if (!parseAAPassName(AA, Name))
656 return false;
657 }
658
659 return true;
660}