blob: f17eb681f1c2f9e2ca65f43afe2b12694888000d [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"
Davide Italiano164b9bc2016-05-05 00:51:09 +000048#include "llvm/Transforms/IPO/ConstantMerge.h"
Davide Italiano344e8382016-05-05 02:37:32 +000049#include "llvm/Transforms/IPO/ElimAvailExtern.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000050#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000051#include "llvm/Transforms/IPO/FunctionAttrs.h"
Davide Italiano66228c42016-05-03 19:39:15 +000052#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000053#include "llvm/Transforms/IPO/GlobalOpt.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000054#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Justin Bogner4563a062016-04-26 20:15:52 +000055#include "llvm/Transforms/IPO/Internalize.h"
Justin Bogner21e15372015-10-30 23:28:12 +000056#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000057#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000058#include "llvm/Transforms/InstrProfiling.h"
Justin Bogner19b67992015-10-30 23:13:18 +000059#include "llvm/Transforms/Scalar/ADCE.h"
Justin Bogner395c2122016-04-22 19:40:41 +000060#include "llvm/Transforms/Scalar/DCE.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000061#include "llvm/Transforms/Scalar/EarlyCSE.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000062#include "llvm/Transforms/Scalar/GVN.h"
Justin Bognerd0d23412016-05-03 22:02:31 +000063#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +000064#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000065#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +000066#include "llvm/Transforms/Scalar/Reassociate.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000067#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000068#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +000069#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruth58dde8c2016-02-26 12:17:54 +000070#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +000071
72using namespace llvm;
73
Chandler Carruth8b5a74192016-02-28 22:16:03 +000074static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
75
Chandler Carruth66445382014-01-11 08:16:35 +000076namespace {
77
Chandler Carruthd8330982014-01-12 09:34:22 +000078/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000079struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000080 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000081 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000082};
83
Chandler Carruth0b576b32015-01-06 02:50:06 +000084/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +000085class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
86 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +000087 static char PassID;
88
89public:
Chandler Carruth0b576b32015-01-06 02:50:06 +000090 struct Result {};
91 Result run(Module &) { return Result(); }
92 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +000093};
94
Chandler Carruth572e3402014-04-21 11:12:00 +000095/// \brief No-op CGSCC pass which does nothing.
96struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000097 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +000098 return PreservedAnalyses::all();
99 }
100 static StringRef name() { return "NoOpCGSCCPass"; }
101};
102
Chandler Carruth0b576b32015-01-06 02:50:06 +0000103/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000104class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
105 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000106 static char PassID;
107
108public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000109 struct Result {};
110 Result run(LazyCallGraph::SCC &) { return Result(); }
111 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000112};
113
Chandler Carruthd8330982014-01-12 09:34:22 +0000114/// \brief No-op function pass which does nothing.
115struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000116 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +0000117 static StringRef name() { return "NoOpFunctionPass"; }
118};
119
Chandler Carruth0b576b32015-01-06 02:50:06 +0000120/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000121class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
122 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000123 static char PassID;
124
125public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000126 struct Result {};
127 Result run(Function &) { return Result(); }
128 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000129};
130
Justin Bognereecc3c82016-02-25 07:23:08 +0000131/// \brief No-op loop pass which does nothing.
132struct NoOpLoopPass {
133 PreservedAnalyses run(Loop &L) { return PreservedAnalyses::all(); }
134 static StringRef name() { return "NoOpLoopPass"; }
135};
136
137/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000138class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
139 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000140 static char PassID;
141
142public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000143 struct Result {};
144 Result run(Loop &) { return Result(); }
145 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000146};
147
Chandler Carruthb4faf132016-03-11 10:22:49 +0000148char NoOpModuleAnalysis::PassID;
149char NoOpCGSCCAnalysis::PassID;
150char NoOpFunctionAnalysis::PassID;
151char NoOpLoopAnalysis::PassID;
152
Chandler Carruth66445382014-01-11 08:16:35 +0000153} // End anonymous namespace.
154
Chandler Carruth1ff77242015-03-07 09:02:36 +0000155void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000156#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000157 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000158#include "PassRegistry.def"
159}
160
Chandler Carruth1ff77242015-03-07 09:02:36 +0000161void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000162#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000163 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000164#include "PassRegistry.def"
165}
166
Chandler Carruth1ff77242015-03-07 09:02:36 +0000167void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000168#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000169 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000170#include "PassRegistry.def"
171}
172
Justin Bognereecc3c82016-02-25 07:23:08 +0000173void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
174#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
175 LAM.registerPass([&] { return CREATE_PASS; });
176#include "PassRegistry.def"
177}
178
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000179void PassBuilder::addPerModuleDefaultPipeline(ModulePassManager &MPM,
180 OptimizationLevel Level,
181 bool DebugLogging) {
182 // FIXME: Finish fleshing this out to match the legacy pipelines.
183 FunctionPassManager EarlyFPM(DebugLogging);
184 EarlyFPM.addPass(SimplifyCFGPass());
185 EarlyFPM.addPass(SROA());
186 EarlyFPM.addPass(EarlyCSEPass());
187 EarlyFPM.addPass(LowerExpectIntrinsicPass());
188
189 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
190}
191
192void PassBuilder::addLTOPreLinkDefaultPipeline(ModulePassManager &MPM,
193 OptimizationLevel Level,
194 bool DebugLogging) {
195 // FIXME: We should use a customized pre-link pipeline!
196 addPerModuleDefaultPipeline(MPM, Level, DebugLogging);
197}
198
199void PassBuilder::addLTODefaultPipeline(ModulePassManager &MPM,
200 OptimizationLevel Level,
201 bool DebugLogging) {
202 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
203 FunctionPassManager LateFPM(DebugLogging);
204 LateFPM.addPass(InstCombinePass());
205 LateFPM.addPass(SimplifyCFGPass());
206
207 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
208}
209
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000210#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000211static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000212 // Manually handle aliases for pre-configured pipeline fragments.
213 if (Name.startswith("default") || Name.startswith("lto"))
214 return DefaultAliasRegex.match(Name);
215
Chandler Carruth58944182014-04-21 08:08:50 +0000216#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000217#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000218 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000219 return true;
220#include "PassRegistry.def"
221
Chandler Carruth66445382014-01-11 08:16:35 +0000222 return false;
223}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000224#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000225
Chandler Carruth572e3402014-04-21 11:12:00 +0000226static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000227#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000228#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000229 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000230 return true;
231#include "PassRegistry.def"
232
Chandler Carruth572e3402014-04-21 11:12:00 +0000233 return false;
234}
235
Chandler Carruthd8330982014-01-12 09:34:22 +0000236static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000237#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000238#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000239 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000240 return true;
241#include "PassRegistry.def"
242
Chandler Carruthd8330982014-01-12 09:34:22 +0000243 return false;
244}
245
Justin Bognereecc3c82016-02-25 07:23:08 +0000246static bool isLoopPassName(StringRef Name) {
247#define LOOP_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
248#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
249 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
250 return true;
251#include "PassRegistry.def"
252
253 return false;
254}
255
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000256bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name,
257 bool DebugLogging) {
258 // Manually handle aliases for pre-configured pipeline fragments.
259 if (Name.startswith("default") || Name.startswith("lto")) {
260 SmallVector<StringRef, 3> Matches;
261 if (!DefaultAliasRegex.match(Name, &Matches))
262 return false;
263 assert(Matches.size() == 3 && "Must capture two matched strings!");
264
265 auto L = StringSwitch<OptimizationLevel>(Matches[2])
266 .Case("O0", O0)
267 .Case("O1", O1)
268 .Case("O2", O2)
269 .Case("O3", O3)
270 .Case("Os", Os)
271 .Case("Oz", Oz);
272
273 if (Matches[1] == "default") {
274 addPerModuleDefaultPipeline(MPM, L, DebugLogging);
275 } else if (Matches[1] == "lto-pre-link") {
276 addLTOPreLinkDefaultPipeline(MPM, L, DebugLogging);
277 } else {
278 assert(Matches[1] == "lto" && "Not one of the matched options!");
279 addLTODefaultPipeline(MPM, L, DebugLogging);
280 }
281 return true;
282 }
283
Chandler Carruth58944182014-04-21 08:08:50 +0000284#define MODULE_PASS(NAME, CREATE_PASS) \
285 if (Name == NAME) { \
286 MPM.addPass(CREATE_PASS); \
287 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000288 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000289#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
290 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000291 MPM.addPass(RequireAnalysisPass< \
292 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000293 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000294 } \
295 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000296 MPM.addPass(InvalidateAnalysisPass< \
297 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000298 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000299 }
300#include "PassRegistry.def"
301
Chandler Carruth66445382014-01-11 08:16:35 +0000302 return false;
303}
304
Chandler Carruth1ff77242015-03-07 09:02:36 +0000305bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000306#define CGSCC_PASS(NAME, CREATE_PASS) \
307 if (Name == NAME) { \
308 CGPM.addPass(CREATE_PASS); \
309 return true; \
310 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000311#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
312 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000313 CGPM.addPass(RequireAnalysisPass< \
314 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000315 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000316 } \
317 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000318 CGPM.addPass(InvalidateAnalysisPass< \
319 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000320 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000321 }
322#include "PassRegistry.def"
323
Chandler Carruth572e3402014-04-21 11:12:00 +0000324 return false;
325}
326
Chandler Carruth1ff77242015-03-07 09:02:36 +0000327bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
328 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000329#define FUNCTION_PASS(NAME, CREATE_PASS) \
330 if (Name == NAME) { \
331 FPM.addPass(CREATE_PASS); \
332 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000333 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000334#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
335 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000336 FPM.addPass(RequireAnalysisPass< \
337 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000338 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000339 } \
340 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000341 FPM.addPass(InvalidateAnalysisPass< \
342 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000343 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000344 }
345#include "PassRegistry.def"
346
Chandler Carruthd8330982014-01-12 09:34:22 +0000347 return false;
348}
349
Justin Bognereecc3c82016-02-25 07:23:08 +0000350bool PassBuilder::parseLoopPassName(LoopPassManager &FPM,
351 StringRef Name) {
352#define LOOP_PASS(NAME, CREATE_PASS) \
353 if (Name == NAME) { \
354 FPM.addPass(CREATE_PASS); \
355 return true; \
356 }
357#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
358 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000359 FPM.addPass(RequireAnalysisPass< \
360 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000361 return true; \
362 } \
363 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000364 FPM.addPass(InvalidateAnalysisPass< \
365 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000366 return true; \
367 }
368#include "PassRegistry.def"
369
370 return false;
371}
372
Chandler Carruthedf59962016-02-18 09:45:17 +0000373bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +0000374#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
375 if (Name == NAME) { \
376 AA.registerModuleAnalysis< \
377 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
378 return true; \
379 }
Chandler Carruthedf59962016-02-18 09:45:17 +0000380#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
381 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000382 AA.registerFunctionAnalysis< \
383 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +0000384 return true; \
385 }
386#include "PassRegistry.def"
387
388 return false;
389}
390
Justin Bognereecc3c82016-02-25 07:23:08 +0000391bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
392 StringRef &PipelineText,
393 bool VerifyEachPass,
394 bool DebugLogging) {
395 for (;;) {
396 // Parse nested pass managers by recursing.
397 if (PipelineText.startswith("loop(")) {
398 LoopPassManager NestedLPM(DebugLogging);
399
400 // Parse the inner pipeline inte the nested manager.
401 PipelineText = PipelineText.substr(strlen("loop("));
402 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
403 DebugLogging) ||
404 PipelineText.empty())
405 return false;
406 assert(PipelineText[0] == ')');
407 PipelineText = PipelineText.substr(1);
408
409 // Add the nested pass manager with the appropriate adaptor.
410 LPM.addPass(std::move(NestedLPM));
411 } else {
412 // Otherwise try to parse a pass name.
413 size_t End = PipelineText.find_first_of(",)");
414 if (!parseLoopPassName(LPM, PipelineText.substr(0, End)))
415 return false;
416 // TODO: Ideally, we would run a LoopVerifierPass() here in the
417 // VerifyEachPass case, but we don't have such a verifier yet.
418
419 PipelineText = PipelineText.substr(End);
420 }
421
422 if (PipelineText.empty() || PipelineText[0] == ')')
423 return true;
424
425 assert(PipelineText[0] == ',');
426 PipelineText = PipelineText.substr(1);
427 }
428}
429
Chandler Carruth1ff77242015-03-07 09:02:36 +0000430bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
431 StringRef &PipelineText,
432 bool VerifyEachPass,
433 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000434 for (;;) {
435 // Parse nested pass managers by recursing.
436 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000437 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000438
439 // Parse the inner pipeline inte the nested manager.
440 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000441 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
442 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000443 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000444 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000445 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000446 PipelineText = PipelineText.substr(1);
447
448 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000449 FPM.addPass(std::move(NestedFPM));
Justin Bognereecc3c82016-02-25 07:23:08 +0000450 } else if (PipelineText.startswith("loop(")) {
451 LoopPassManager NestedLPM(DebugLogging);
452
453 // Parse the inner pipeline inte the nested manager.
454 PipelineText = PipelineText.substr(strlen("loop("));
455 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
456 DebugLogging) ||
457 PipelineText.empty())
458 return false;
459 assert(PipelineText[0] == ')');
460 PipelineText = PipelineText.substr(1);
461
462 // Add the nested pass manager with the appropriate adaptor.
463 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(NestedLPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000464 } else {
465 // Otherwise try to parse a pass name.
466 size_t End = PipelineText.find_first_of(",)");
467 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
468 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000469 if (VerifyEachPass)
470 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000471
472 PipelineText = PipelineText.substr(End);
473 }
474
475 if (PipelineText.empty() || PipelineText[0] == ')')
476 return true;
477
478 assert(PipelineText[0] == ',');
479 PipelineText = PipelineText.substr(1);
480 }
481}
482
Chandler Carruth1ff77242015-03-07 09:02:36 +0000483bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
484 StringRef &PipelineText,
485 bool VerifyEachPass,
486 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000487 for (;;) {
488 // Parse nested pass managers by recursing.
489 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000490 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000491
492 // Parse the inner pipeline into the nested manager.
493 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000494 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
495 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000496 PipelineText.empty())
497 return false;
498 assert(PipelineText[0] == ')');
499 PipelineText = PipelineText.substr(1);
500
501 // Add the nested pass manager with the appropriate adaptor.
502 CGPM.addPass(std::move(NestedCGPM));
503 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000504 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000505
506 // Parse the inner pipeline inte the nested manager.
507 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000508 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
509 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000510 PipelineText.empty())
511 return false;
512 assert(PipelineText[0] == ')');
513 PipelineText = PipelineText.substr(1);
514
515 // Add the nested pass manager with the appropriate adaptor.
516 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
517 } else {
518 // Otherwise try to parse a pass name.
519 size_t End = PipelineText.find_first_of(",)");
520 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
521 return false;
522 // FIXME: No verifier support for CGSCC passes!
523
524 PipelineText = PipelineText.substr(End);
525 }
526
527 if (PipelineText.empty() || PipelineText[0] == ')')
528 return true;
529
530 assert(PipelineText[0] == ',');
531 PipelineText = PipelineText.substr(1);
532 }
533}
534
Chandler Carruth1ff77242015-03-07 09:02:36 +0000535bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
536 StringRef &PipelineText,
537 bool VerifyEachPass,
538 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000539 for (;;) {
540 // Parse nested pass managers by recursing.
541 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000542 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000543
544 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000545 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000546 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
547 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000548 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000549 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000550 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000551 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000552
553 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000554 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000555 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000556 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000557
558 // Parse the inner pipeline inte the nested manager.
559 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000560 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
561 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000562 PipelineText.empty())
563 return false;
564 assert(PipelineText[0] == ')');
565 PipelineText = PipelineText.substr(1);
566
567 // Add the nested pass manager with the appropriate adaptor.
568 MPM.addPass(
569 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000570 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000571 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000572
573 // Parse the inner pipeline inte the nested manager.
574 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000575 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
576 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000577 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000578 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000579 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000580 PipelineText = PipelineText.substr(1);
581
582 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000583 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000584 } else {
585 // Otherwise try to parse a pass name.
586 size_t End = PipelineText.find_first_of(",)");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000587 if (!parseModulePassName(MPM, PipelineText.substr(0, End), DebugLogging))
Chandler Carruth66445382014-01-11 08:16:35 +0000588 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000589 if (VerifyEachPass)
590 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000591
592 PipelineText = PipelineText.substr(End);
593 }
594
595 if (PipelineText.empty() || PipelineText[0] == ')')
596 return true;
597
598 assert(PipelineText[0] == ',');
599 PipelineText = PipelineText.substr(1);
600 }
601}
602
603// Primary pass pipeline description parsing routine.
604// FIXME: Should this routine accept a TargetMachine or require the caller to
605// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000606bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
607 StringRef PipelineText, bool VerifyEachPass,
608 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000609 // By default, try to parse the pipeline as-if it were within an implicit
610 // 'module(...)' pass pipeline. If this will parse at all, it needs to
611 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000612 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000613 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000614
Chandler Carruthea368f12015-01-06 08:37:58 +0000615 // This isn't parsable as a module pipeline, look for the end of a pass name
616 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000617 StringRef FirstName =
618 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000619 assert(!isModulePassName(FirstName) &&
620 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000621
Chandler Carruthea368f12015-01-06 08:37:58 +0000622 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
623 // pipeline.
Justin Bognereecc3c82016-02-25 07:23:08 +0000624 if (PipelineText.startswith("cgscc(") || isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000625 CGSCCPassManager CGPM(DebugLogging);
626 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
627 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000628 !PipelineText.empty())
629 return false;
630 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
631 return true;
632 }
633
Chandler Carruthea368f12015-01-06 08:37:58 +0000634 // Similarly, if this looks like a Function pass, parse the whole thing as
635 // a Function pipelien.
Justin Bognereecc3c82016-02-25 07:23:08 +0000636 if (PipelineText.startswith("function(") || isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000637 FunctionPassManager FPM(DebugLogging);
638 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
639 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000640 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000641 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000642 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000643 return true;
644 }
Chandler Carruth66445382014-01-11 08:16:35 +0000645
Justin Bognereecc3c82016-02-25 07:23:08 +0000646 // If this looks like a Loop pass, parse the whole thing as a Loop pipeline.
647 if (PipelineText.startswith("loop(") || isLoopPassName(FirstName)) {
648 LoopPassManager LPM(DebugLogging);
649 if (!parseLoopPassPipeline(LPM, PipelineText, VerifyEachPass,
650 DebugLogging) ||
651 !PipelineText.empty())
652 return false;
653 FunctionPassManager FPM(DebugLogging);
654 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
655 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
656 return true;
657 }
658
659
Chandler Carruth66445382014-01-11 08:16:35 +0000660 return false;
661}
Chandler Carruthedf59962016-02-18 09:45:17 +0000662
663bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
664 while (!PipelineText.empty()) {
665 StringRef Name;
666 std::tie(Name, PipelineText) = PipelineText.split(',');
667 if (!parseAAPassName(AA, Name))
668 return false;
669 }
670
671 return true;
672}