blob: 1729b2218d129c7002e65c4d0e882f910098f53f [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"
Justin Bogner1a075012016-04-26 00:28:01 +000050#include "llvm/Transforms/IPO/GlobalOpt.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000051#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Justin Bogner4563a062016-04-26 20:15:52 +000052#include "llvm/Transforms/IPO/Internalize.h"
Justin Bogner21e15372015-10-30 23:28:12 +000053#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000054#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000055#include "llvm/Transforms/InstrProfiling.h"
Justin Bogner19b67992015-10-30 23:13:18 +000056#include "llvm/Transforms/Scalar/ADCE.h"
Justin Bogner395c2122016-04-22 19:40:41 +000057#include "llvm/Transforms/Scalar/DCE.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000058#include "llvm/Transforms/Scalar/EarlyCSE.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000059#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Chandler Carruth89c45a12016-03-11 08:50:55 +000060#include "llvm/Transforms/Scalar/GVN.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +000061#include "llvm/Transforms/Scalar/Reassociate.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000062#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000063#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +000064#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruth58dde8c2016-02-26 12:17:54 +000065#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +000066
67using namespace llvm;
68
Chandler Carruth8b5a74192016-02-28 22:16:03 +000069static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
70
Chandler Carruth66445382014-01-11 08:16:35 +000071namespace {
72
Chandler Carruthd8330982014-01-12 09:34:22 +000073/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000074struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000075 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000076 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000077};
78
Chandler Carruth0b576b32015-01-06 02:50:06 +000079/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +000080class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
81 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +000082 static char PassID;
83
84public:
Chandler Carruth0b576b32015-01-06 02:50:06 +000085 struct Result {};
86 Result run(Module &) { return Result(); }
87 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +000088};
89
Chandler Carruth572e3402014-04-21 11:12:00 +000090/// \brief No-op CGSCC pass which does nothing.
91struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000092 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +000093 return PreservedAnalyses::all();
94 }
95 static StringRef name() { return "NoOpCGSCCPass"; }
96};
97
Chandler Carruth0b576b32015-01-06 02:50:06 +000098/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +000099class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
100 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000101 static char PassID;
102
103public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000104 struct Result {};
105 Result run(LazyCallGraph::SCC &) { return Result(); }
106 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000107};
108
Chandler Carruthd8330982014-01-12 09:34:22 +0000109/// \brief No-op function pass which does nothing.
110struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000111 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +0000112 static StringRef name() { return "NoOpFunctionPass"; }
113};
114
Chandler Carruth0b576b32015-01-06 02:50:06 +0000115/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000116class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
117 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000118 static char PassID;
119
120public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000121 struct Result {};
122 Result run(Function &) { return Result(); }
123 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000124};
125
Justin Bognereecc3c82016-02-25 07:23:08 +0000126/// \brief No-op loop pass which does nothing.
127struct NoOpLoopPass {
128 PreservedAnalyses run(Loop &L) { return PreservedAnalyses::all(); }
129 static StringRef name() { return "NoOpLoopPass"; }
130};
131
132/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000133class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
134 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000135 static char PassID;
136
137public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000138 struct Result {};
139 Result run(Loop &) { return Result(); }
140 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000141};
142
Chandler Carruthb4faf132016-03-11 10:22:49 +0000143char NoOpModuleAnalysis::PassID;
144char NoOpCGSCCAnalysis::PassID;
145char NoOpFunctionAnalysis::PassID;
146char NoOpLoopAnalysis::PassID;
147
Chandler Carruth66445382014-01-11 08:16:35 +0000148} // End anonymous namespace.
149
Chandler Carruth1ff77242015-03-07 09:02:36 +0000150void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000151#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000152 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000153#include "PassRegistry.def"
154}
155
Chandler Carruth1ff77242015-03-07 09:02:36 +0000156void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000157#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000158 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000159#include "PassRegistry.def"
160}
161
Chandler Carruth1ff77242015-03-07 09:02:36 +0000162void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000163#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000164 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000165#include "PassRegistry.def"
166}
167
Justin Bognereecc3c82016-02-25 07:23:08 +0000168void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
169#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
170 LAM.registerPass([&] { return CREATE_PASS; });
171#include "PassRegistry.def"
172}
173
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000174void PassBuilder::addPerModuleDefaultPipeline(ModulePassManager &MPM,
175 OptimizationLevel Level,
176 bool DebugLogging) {
177 // FIXME: Finish fleshing this out to match the legacy pipelines.
178 FunctionPassManager EarlyFPM(DebugLogging);
179 EarlyFPM.addPass(SimplifyCFGPass());
180 EarlyFPM.addPass(SROA());
181 EarlyFPM.addPass(EarlyCSEPass());
182 EarlyFPM.addPass(LowerExpectIntrinsicPass());
183
184 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
185}
186
187void PassBuilder::addLTOPreLinkDefaultPipeline(ModulePassManager &MPM,
188 OptimizationLevel Level,
189 bool DebugLogging) {
190 // FIXME: We should use a customized pre-link pipeline!
191 addPerModuleDefaultPipeline(MPM, Level, DebugLogging);
192}
193
194void PassBuilder::addLTODefaultPipeline(ModulePassManager &MPM,
195 OptimizationLevel Level,
196 bool DebugLogging) {
197 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
198 FunctionPassManager LateFPM(DebugLogging);
199 LateFPM.addPass(InstCombinePass());
200 LateFPM.addPass(SimplifyCFGPass());
201
202 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
203}
204
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000205#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000206static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000207 // Manually handle aliases for pre-configured pipeline fragments.
208 if (Name.startswith("default") || Name.startswith("lto"))
209 return DefaultAliasRegex.match(Name);
210
Chandler Carruth58944182014-04-21 08:08:50 +0000211#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000212#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000213 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000214 return true;
215#include "PassRegistry.def"
216
Chandler Carruth66445382014-01-11 08:16:35 +0000217 return false;
218}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000219#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000220
Chandler Carruth572e3402014-04-21 11:12:00 +0000221static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000222#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000223#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000224 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000225 return true;
226#include "PassRegistry.def"
227
Chandler Carruth572e3402014-04-21 11:12:00 +0000228 return false;
229}
230
Chandler Carruthd8330982014-01-12 09:34:22 +0000231static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000232#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000233#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000234 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000235 return true;
236#include "PassRegistry.def"
237
Chandler Carruthd8330982014-01-12 09:34:22 +0000238 return false;
239}
240
Justin Bognereecc3c82016-02-25 07:23:08 +0000241static bool isLoopPassName(StringRef Name) {
242#define LOOP_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
243#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
244 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
245 return true;
246#include "PassRegistry.def"
247
248 return false;
249}
250
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000251bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name,
252 bool DebugLogging) {
253 // Manually handle aliases for pre-configured pipeline fragments.
254 if (Name.startswith("default") || Name.startswith("lto")) {
255 SmallVector<StringRef, 3> Matches;
256 if (!DefaultAliasRegex.match(Name, &Matches))
257 return false;
258 assert(Matches.size() == 3 && "Must capture two matched strings!");
259
260 auto L = StringSwitch<OptimizationLevel>(Matches[2])
261 .Case("O0", O0)
262 .Case("O1", O1)
263 .Case("O2", O2)
264 .Case("O3", O3)
265 .Case("Os", Os)
266 .Case("Oz", Oz);
267
268 if (Matches[1] == "default") {
269 addPerModuleDefaultPipeline(MPM, L, DebugLogging);
270 } else if (Matches[1] == "lto-pre-link") {
271 addLTOPreLinkDefaultPipeline(MPM, L, DebugLogging);
272 } else {
273 assert(Matches[1] == "lto" && "Not one of the matched options!");
274 addLTODefaultPipeline(MPM, L, DebugLogging);
275 }
276 return true;
277 }
278
Chandler Carruth58944182014-04-21 08:08:50 +0000279#define MODULE_PASS(NAME, CREATE_PASS) \
280 if (Name == NAME) { \
281 MPM.addPass(CREATE_PASS); \
282 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000283 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000284#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
285 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000286 MPM.addPass(RequireAnalysisPass< \
287 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000288 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000289 } \
290 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000291 MPM.addPass(InvalidateAnalysisPass< \
292 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000293 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000294 }
295#include "PassRegistry.def"
296
Chandler Carruth66445382014-01-11 08:16:35 +0000297 return false;
298}
299
Chandler Carruth1ff77242015-03-07 09:02:36 +0000300bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000301#define CGSCC_PASS(NAME, CREATE_PASS) \
302 if (Name == NAME) { \
303 CGPM.addPass(CREATE_PASS); \
304 return true; \
305 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000306#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
307 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000308 CGPM.addPass(RequireAnalysisPass< \
309 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000310 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000311 } \
312 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000313 CGPM.addPass(InvalidateAnalysisPass< \
314 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000315 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000316 }
317#include "PassRegistry.def"
318
Chandler Carruth572e3402014-04-21 11:12:00 +0000319 return false;
320}
321
Chandler Carruth1ff77242015-03-07 09:02:36 +0000322bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
323 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000324#define FUNCTION_PASS(NAME, CREATE_PASS) \
325 if (Name == NAME) { \
326 FPM.addPass(CREATE_PASS); \
327 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000328 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000329#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
330 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000331 FPM.addPass(RequireAnalysisPass< \
332 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000333 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000334 } \
335 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000336 FPM.addPass(InvalidateAnalysisPass< \
337 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000338 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000339 }
340#include "PassRegistry.def"
341
Chandler Carruthd8330982014-01-12 09:34:22 +0000342 return false;
343}
344
Justin Bognereecc3c82016-02-25 07:23:08 +0000345bool PassBuilder::parseLoopPassName(LoopPassManager &FPM,
346 StringRef Name) {
347#define LOOP_PASS(NAME, CREATE_PASS) \
348 if (Name == NAME) { \
349 FPM.addPass(CREATE_PASS); \
350 return true; \
351 }
352#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
353 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000354 FPM.addPass(RequireAnalysisPass< \
355 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000356 return true; \
357 } \
358 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000359 FPM.addPass(InvalidateAnalysisPass< \
360 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000361 return true; \
362 }
363#include "PassRegistry.def"
364
365 return false;
366}
367
Chandler Carruthedf59962016-02-18 09:45:17 +0000368bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +0000369#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
370 if (Name == NAME) { \
371 AA.registerModuleAnalysis< \
372 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
373 return true; \
374 }
Chandler Carruthedf59962016-02-18 09:45:17 +0000375#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
376 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000377 AA.registerFunctionAnalysis< \
378 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +0000379 return true; \
380 }
381#include "PassRegistry.def"
382
383 return false;
384}
385
Justin Bognereecc3c82016-02-25 07:23:08 +0000386bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
387 StringRef &PipelineText,
388 bool VerifyEachPass,
389 bool DebugLogging) {
390 for (;;) {
391 // Parse nested pass managers by recursing.
392 if (PipelineText.startswith("loop(")) {
393 LoopPassManager NestedLPM(DebugLogging);
394
395 // Parse the inner pipeline inte the nested manager.
396 PipelineText = PipelineText.substr(strlen("loop("));
397 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
398 DebugLogging) ||
399 PipelineText.empty())
400 return false;
401 assert(PipelineText[0] == ')');
402 PipelineText = PipelineText.substr(1);
403
404 // Add the nested pass manager with the appropriate adaptor.
405 LPM.addPass(std::move(NestedLPM));
406 } else {
407 // Otherwise try to parse a pass name.
408 size_t End = PipelineText.find_first_of(",)");
409 if (!parseLoopPassName(LPM, PipelineText.substr(0, End)))
410 return false;
411 // TODO: Ideally, we would run a LoopVerifierPass() here in the
412 // VerifyEachPass case, but we don't have such a verifier yet.
413
414 PipelineText = PipelineText.substr(End);
415 }
416
417 if (PipelineText.empty() || PipelineText[0] == ')')
418 return true;
419
420 assert(PipelineText[0] == ',');
421 PipelineText = PipelineText.substr(1);
422 }
423}
424
Chandler Carruth1ff77242015-03-07 09:02:36 +0000425bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
426 StringRef &PipelineText,
427 bool VerifyEachPass,
428 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000429 for (;;) {
430 // Parse nested pass managers by recursing.
431 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000432 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000433
434 // Parse the inner pipeline inte the nested manager.
435 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000436 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
437 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000438 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000439 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000440 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000441 PipelineText = PipelineText.substr(1);
442
443 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000444 FPM.addPass(std::move(NestedFPM));
Justin Bognereecc3c82016-02-25 07:23:08 +0000445 } else if (PipelineText.startswith("loop(")) {
446 LoopPassManager NestedLPM(DebugLogging);
447
448 // Parse the inner pipeline inte the nested manager.
449 PipelineText = PipelineText.substr(strlen("loop("));
450 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
451 DebugLogging) ||
452 PipelineText.empty())
453 return false;
454 assert(PipelineText[0] == ')');
455 PipelineText = PipelineText.substr(1);
456
457 // Add the nested pass manager with the appropriate adaptor.
458 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(NestedLPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000459 } else {
460 // Otherwise try to parse a pass name.
461 size_t End = PipelineText.find_first_of(",)");
462 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
463 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000464 if (VerifyEachPass)
465 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000466
467 PipelineText = PipelineText.substr(End);
468 }
469
470 if (PipelineText.empty() || PipelineText[0] == ')')
471 return true;
472
473 assert(PipelineText[0] == ',');
474 PipelineText = PipelineText.substr(1);
475 }
476}
477
Chandler Carruth1ff77242015-03-07 09:02:36 +0000478bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
479 StringRef &PipelineText,
480 bool VerifyEachPass,
481 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000482 for (;;) {
483 // Parse nested pass managers by recursing.
484 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000485 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000486
487 // Parse the inner pipeline into the nested manager.
488 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000489 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
490 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000491 PipelineText.empty())
492 return false;
493 assert(PipelineText[0] == ')');
494 PipelineText = PipelineText.substr(1);
495
496 // Add the nested pass manager with the appropriate adaptor.
497 CGPM.addPass(std::move(NestedCGPM));
498 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000499 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000500
501 // Parse the inner pipeline inte the nested manager.
502 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000503 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
504 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000505 PipelineText.empty())
506 return false;
507 assert(PipelineText[0] == ')');
508 PipelineText = PipelineText.substr(1);
509
510 // Add the nested pass manager with the appropriate adaptor.
511 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
512 } else {
513 // Otherwise try to parse a pass name.
514 size_t End = PipelineText.find_first_of(",)");
515 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
516 return false;
517 // FIXME: No verifier support for CGSCC passes!
518
519 PipelineText = PipelineText.substr(End);
520 }
521
522 if (PipelineText.empty() || PipelineText[0] == ')')
523 return true;
524
525 assert(PipelineText[0] == ',');
526 PipelineText = PipelineText.substr(1);
527 }
528}
529
Chandler Carruth1ff77242015-03-07 09:02:36 +0000530bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
531 StringRef &PipelineText,
532 bool VerifyEachPass,
533 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000534 for (;;) {
535 // Parse nested pass managers by recursing.
536 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000537 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000538
539 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000540 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000541 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
542 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000543 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000544 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000545 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000546 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000547
548 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000549 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000550 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000551 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000552
553 // Parse the inner pipeline inte the nested manager.
554 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000555 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
556 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000557 PipelineText.empty())
558 return false;
559 assert(PipelineText[0] == ')');
560 PipelineText = PipelineText.substr(1);
561
562 // Add the nested pass manager with the appropriate adaptor.
563 MPM.addPass(
564 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000565 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000566 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000567
568 // Parse the inner pipeline inte the nested manager.
569 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000570 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
571 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000572 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000573 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000574 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000575 PipelineText = PipelineText.substr(1);
576
577 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000578 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000579 } else {
580 // Otherwise try to parse a pass name.
581 size_t End = PipelineText.find_first_of(",)");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000582 if (!parseModulePassName(MPM, PipelineText.substr(0, End), DebugLogging))
Chandler Carruth66445382014-01-11 08:16:35 +0000583 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000584 if (VerifyEachPass)
585 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000586
587 PipelineText = PipelineText.substr(End);
588 }
589
590 if (PipelineText.empty() || PipelineText[0] == ')')
591 return true;
592
593 assert(PipelineText[0] == ',');
594 PipelineText = PipelineText.substr(1);
595 }
596}
597
598// Primary pass pipeline description parsing routine.
599// FIXME: Should this routine accept a TargetMachine or require the caller to
600// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000601bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
602 StringRef PipelineText, bool VerifyEachPass,
603 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000604 // By default, try to parse the pipeline as-if it were within an implicit
605 // 'module(...)' pass pipeline. If this will parse at all, it needs to
606 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000607 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000608 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000609
Chandler Carruthea368f12015-01-06 08:37:58 +0000610 // This isn't parsable as a module pipeline, look for the end of a pass name
611 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000612 StringRef FirstName =
613 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000614 assert(!isModulePassName(FirstName) &&
615 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000616
Chandler Carruthea368f12015-01-06 08:37:58 +0000617 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
618 // pipeline.
Justin Bognereecc3c82016-02-25 07:23:08 +0000619 if (PipelineText.startswith("cgscc(") || isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000620 CGSCCPassManager CGPM(DebugLogging);
621 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
622 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000623 !PipelineText.empty())
624 return false;
625 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
626 return true;
627 }
628
Chandler Carruthea368f12015-01-06 08:37:58 +0000629 // Similarly, if this looks like a Function pass, parse the whole thing as
630 // a Function pipelien.
Justin Bognereecc3c82016-02-25 07:23:08 +0000631 if (PipelineText.startswith("function(") || isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000632 FunctionPassManager FPM(DebugLogging);
633 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
634 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000635 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000636 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000637 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000638 return true;
639 }
Chandler Carruth66445382014-01-11 08:16:35 +0000640
Justin Bognereecc3c82016-02-25 07:23:08 +0000641 // If this looks like a Loop pass, parse the whole thing as a Loop pipeline.
642 if (PipelineText.startswith("loop(") || isLoopPassName(FirstName)) {
643 LoopPassManager LPM(DebugLogging);
644 if (!parseLoopPassPipeline(LPM, PipelineText, VerifyEachPass,
645 DebugLogging) ||
646 !PipelineText.empty())
647 return false;
648 FunctionPassManager FPM(DebugLogging);
649 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
650 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
651 return true;
652 }
653
654
Chandler Carruth66445382014-01-11 08:16:35 +0000655 return false;
656}
Chandler Carruthedf59962016-02-18 09:45:17 +0000657
658bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
659 while (!PipelineText.empty()) {
660 StringRef Name;
661 std::tie(Name, PipelineText) = PipelineText.split(',');
662 if (!parseAAPassName(AA, Name))
663 return false;
664 }
665
666 return true;
667}