blob: df34ba20de28972cf55f4987df2a5990333b99bf [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"
Xinliang David Li28a93272016-05-05 21:13:27 +000024#include "llvm/Analysis/BlockFrequencyInfo.h"
25#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
Xinliang David Li6e5dd412016-05-05 02:59:57 +000026#include "llvm/Analysis/BranchProbabilityInfo.h"
Chandler Carruth342c6712016-02-20 03:52:02 +000027#include "llvm/Analysis/CFLAliasAnalysis.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000028#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruth4c660f72016-03-10 11:24:11 +000029#include "llvm/Analysis/CallGraph.h"
Michael Kupersteinde16b442016-04-18 23:55:01 +000030#include "llvm/Analysis/DemandedBits.h"
Hongbin Zheng751337f2016-02-25 17:54:15 +000031#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruth45a9c202016-03-11 09:15:11 +000032#include "llvm/Analysis/GlobalsModRef.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000033#include "llvm/Analysis/LazyCallGraph.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000034#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth61440d22016-03-10 00:55:30 +000035#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000036#include "llvm/Analysis/PostDominators.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000037#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000038#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000039#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000040#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000041#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000042#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000043#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000044#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000045#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000046#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000047#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000048#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000049#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000050#include "llvm/Target/TargetMachine.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000051#include "llvm/Transforms/IPO/ConstantMerge.h"
Davide Italiano344e8382016-05-05 02:37:32 +000052#include "llvm/Transforms/IPO/ElimAvailExtern.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000053#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000054#include "llvm/Transforms/IPO/FunctionAttrs.h"
Davide Italiano66228c42016-05-03 19:39:15 +000055#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000056#include "llvm/Transforms/IPO/GlobalOpt.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000057#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Justin Bogner4563a062016-04-26 20:15:52 +000058#include "llvm/Transforms/IPO/Internalize.h"
Davide Italianof54f2f02016-05-05 21:05:36 +000059#include "llvm/Transforms/IPO/SCCP.h"
Justin Bogner21e15372015-10-30 23:28:12 +000060#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000061#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000062#include "llvm/Transforms/InstrProfiling.h"
Xinliang David Li8aebf442016-05-06 05:49:19 +000063#include "llvm/Transforms/PGOInstrumentation.h"
Justin Bogner19b67992015-10-30 23:13:18 +000064#include "llvm/Transforms/Scalar/ADCE.h"
Justin Bogner395c2122016-04-22 19:40:41 +000065#include "llvm/Transforms/Scalar/DCE.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000066#include "llvm/Transforms/Scalar/EarlyCSE.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000067#include "llvm/Transforms/Scalar/GVN.h"
Justin Bognerd0d23412016-05-03 22:02:31 +000068#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +000069#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000070#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +000071#include "llvm/Transforms/Scalar/Reassociate.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000072#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000073#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +000074#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruth58dde8c2016-02-26 12:17:54 +000075#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +000076
77using namespace llvm;
78
Chandler Carruth8b5a74192016-02-28 22:16:03 +000079static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
80
Chandler Carruth66445382014-01-11 08:16:35 +000081namespace {
82
Chandler Carruthd8330982014-01-12 09:34:22 +000083/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000084struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000085 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000086 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000087};
88
Chandler Carruth0b576b32015-01-06 02:50:06 +000089/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +000090class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
91 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +000092 static char PassID;
93
94public:
Chandler Carruth0b576b32015-01-06 02:50:06 +000095 struct Result {};
96 Result run(Module &) { return Result(); }
97 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +000098};
99
Chandler Carruth572e3402014-04-21 11:12:00 +0000100/// \brief No-op CGSCC pass which does nothing.
101struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000102 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000103 return PreservedAnalyses::all();
104 }
105 static StringRef name() { return "NoOpCGSCCPass"; }
106};
107
Chandler Carruth0b576b32015-01-06 02:50:06 +0000108/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000109class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
110 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
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(LazyCallGraph::SCC &) { return Result(); }
116 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000117};
118
Chandler Carruthd8330982014-01-12 09:34:22 +0000119/// \brief No-op function pass which does nothing.
120struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000121 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +0000122 static StringRef name() { return "NoOpFunctionPass"; }
123};
124
Chandler Carruth0b576b32015-01-06 02:50:06 +0000125/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000126class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
127 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000128 static char PassID;
129
130public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000131 struct Result {};
132 Result run(Function &) { return Result(); }
133 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000134};
135
Justin Bognereecc3c82016-02-25 07:23:08 +0000136/// \brief No-op loop pass which does nothing.
137struct NoOpLoopPass {
138 PreservedAnalyses run(Loop &L) { return PreservedAnalyses::all(); }
139 static StringRef name() { return "NoOpLoopPass"; }
140};
141
142/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000143class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
144 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000145 static char PassID;
146
147public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000148 struct Result {};
149 Result run(Loop &) { return Result(); }
150 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000151};
152
Chandler Carruthb4faf132016-03-11 10:22:49 +0000153char NoOpModuleAnalysis::PassID;
154char NoOpCGSCCAnalysis::PassID;
155char NoOpFunctionAnalysis::PassID;
156char NoOpLoopAnalysis::PassID;
157
Chandler Carruth66445382014-01-11 08:16:35 +0000158} // End anonymous namespace.
159
Chandler Carruth1ff77242015-03-07 09:02:36 +0000160void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000161#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000162 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000163#include "PassRegistry.def"
164}
165
Chandler Carruth1ff77242015-03-07 09:02:36 +0000166void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000167#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000168 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000169#include "PassRegistry.def"
170}
171
Chandler Carruth1ff77242015-03-07 09:02:36 +0000172void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000173#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000174 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000175#include "PassRegistry.def"
176}
177
Justin Bognereecc3c82016-02-25 07:23:08 +0000178void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
179#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
180 LAM.registerPass([&] { return CREATE_PASS; });
181#include "PassRegistry.def"
182}
183
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000184void PassBuilder::addPerModuleDefaultPipeline(ModulePassManager &MPM,
185 OptimizationLevel Level,
186 bool DebugLogging) {
187 // FIXME: Finish fleshing this out to match the legacy pipelines.
188 FunctionPassManager EarlyFPM(DebugLogging);
189 EarlyFPM.addPass(SimplifyCFGPass());
190 EarlyFPM.addPass(SROA());
191 EarlyFPM.addPass(EarlyCSEPass());
192 EarlyFPM.addPass(LowerExpectIntrinsicPass());
193
194 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
195}
196
197void PassBuilder::addLTOPreLinkDefaultPipeline(ModulePassManager &MPM,
198 OptimizationLevel Level,
199 bool DebugLogging) {
200 // FIXME: We should use a customized pre-link pipeline!
201 addPerModuleDefaultPipeline(MPM, Level, DebugLogging);
202}
203
204void PassBuilder::addLTODefaultPipeline(ModulePassManager &MPM,
205 OptimizationLevel Level,
206 bool DebugLogging) {
207 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
208 FunctionPassManager LateFPM(DebugLogging);
209 LateFPM.addPass(InstCombinePass());
210 LateFPM.addPass(SimplifyCFGPass());
211
212 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
213}
214
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000215#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000216static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000217 // Manually handle aliases for pre-configured pipeline fragments.
218 if (Name.startswith("default") || Name.startswith("lto"))
219 return DefaultAliasRegex.match(Name);
220
Chandler Carruth58944182014-04-21 08:08:50 +0000221#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000222#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000223 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000224 return true;
225#include "PassRegistry.def"
226
Chandler Carruth66445382014-01-11 08:16:35 +0000227 return false;
228}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000229#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000230
Chandler Carruth572e3402014-04-21 11:12:00 +0000231static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000232#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000233#define CGSCC_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 Carruth572e3402014-04-21 11:12:00 +0000238 return false;
239}
240
Chandler Carruthd8330982014-01-12 09:34:22 +0000241static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000242#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000243#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000244 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000245 return true;
246#include "PassRegistry.def"
247
Chandler Carruthd8330982014-01-12 09:34:22 +0000248 return false;
249}
250
Justin Bognereecc3c82016-02-25 07:23:08 +0000251static bool isLoopPassName(StringRef Name) {
252#define LOOP_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
253#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
254 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
255 return true;
256#include "PassRegistry.def"
257
258 return false;
259}
260
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000261bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name,
262 bool DebugLogging) {
263 // Manually handle aliases for pre-configured pipeline fragments.
264 if (Name.startswith("default") || Name.startswith("lto")) {
265 SmallVector<StringRef, 3> Matches;
266 if (!DefaultAliasRegex.match(Name, &Matches))
267 return false;
268 assert(Matches.size() == 3 && "Must capture two matched strings!");
269
270 auto L = StringSwitch<OptimizationLevel>(Matches[2])
271 .Case("O0", O0)
272 .Case("O1", O1)
273 .Case("O2", O2)
274 .Case("O3", O3)
275 .Case("Os", Os)
276 .Case("Oz", Oz);
277
278 if (Matches[1] == "default") {
279 addPerModuleDefaultPipeline(MPM, L, DebugLogging);
280 } else if (Matches[1] == "lto-pre-link") {
281 addLTOPreLinkDefaultPipeline(MPM, L, DebugLogging);
282 } else {
283 assert(Matches[1] == "lto" && "Not one of the matched options!");
284 addLTODefaultPipeline(MPM, L, DebugLogging);
285 }
286 return true;
287 }
288
Chandler Carruth58944182014-04-21 08:08:50 +0000289#define MODULE_PASS(NAME, CREATE_PASS) \
290 if (Name == NAME) { \
291 MPM.addPass(CREATE_PASS); \
292 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000293 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000294#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
295 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000296 MPM.addPass(RequireAnalysisPass< \
297 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000298 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000299 } \
300 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000301 MPM.addPass(InvalidateAnalysisPass< \
302 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000303 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000304 }
305#include "PassRegistry.def"
306
Chandler Carruth66445382014-01-11 08:16:35 +0000307 return false;
308}
309
Chandler Carruth1ff77242015-03-07 09:02:36 +0000310bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000311#define CGSCC_PASS(NAME, CREATE_PASS) \
312 if (Name == NAME) { \
313 CGPM.addPass(CREATE_PASS); \
314 return true; \
315 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000316#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
317 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000318 CGPM.addPass(RequireAnalysisPass< \
319 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000320 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000321 } \
322 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000323 CGPM.addPass(InvalidateAnalysisPass< \
324 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000325 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000326 }
327#include "PassRegistry.def"
328
Chandler Carruth572e3402014-04-21 11:12:00 +0000329 return false;
330}
331
Chandler Carruth1ff77242015-03-07 09:02:36 +0000332bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
333 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000334#define FUNCTION_PASS(NAME, CREATE_PASS) \
335 if (Name == NAME) { \
336 FPM.addPass(CREATE_PASS); \
337 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000338 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000339#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
340 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000341 FPM.addPass(RequireAnalysisPass< \
342 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000343 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000344 } \
345 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000346 FPM.addPass(InvalidateAnalysisPass< \
347 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000348 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000349 }
350#include "PassRegistry.def"
351
Chandler Carruthd8330982014-01-12 09:34:22 +0000352 return false;
353}
354
Justin Bognereecc3c82016-02-25 07:23:08 +0000355bool PassBuilder::parseLoopPassName(LoopPassManager &FPM,
356 StringRef Name) {
357#define LOOP_PASS(NAME, CREATE_PASS) \
358 if (Name == NAME) { \
359 FPM.addPass(CREATE_PASS); \
360 return true; \
361 }
362#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
363 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000364 FPM.addPass(RequireAnalysisPass< \
365 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000366 return true; \
367 } \
368 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000369 FPM.addPass(InvalidateAnalysisPass< \
370 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000371 return true; \
372 }
373#include "PassRegistry.def"
374
375 return false;
376}
377
Chandler Carruthedf59962016-02-18 09:45:17 +0000378bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +0000379#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
380 if (Name == NAME) { \
381 AA.registerModuleAnalysis< \
382 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
383 return true; \
384 }
Chandler Carruthedf59962016-02-18 09:45:17 +0000385#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
386 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000387 AA.registerFunctionAnalysis< \
388 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +0000389 return true; \
390 }
391#include "PassRegistry.def"
392
393 return false;
394}
395
Justin Bognereecc3c82016-02-25 07:23:08 +0000396bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
397 StringRef &PipelineText,
398 bool VerifyEachPass,
399 bool DebugLogging) {
400 for (;;) {
401 // Parse nested pass managers by recursing.
402 if (PipelineText.startswith("loop(")) {
403 LoopPassManager NestedLPM(DebugLogging);
404
405 // Parse the inner pipeline inte the nested manager.
406 PipelineText = PipelineText.substr(strlen("loop("));
407 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
408 DebugLogging) ||
409 PipelineText.empty())
410 return false;
411 assert(PipelineText[0] == ')');
412 PipelineText = PipelineText.substr(1);
413
414 // Add the nested pass manager with the appropriate adaptor.
415 LPM.addPass(std::move(NestedLPM));
416 } else {
417 // Otherwise try to parse a pass name.
418 size_t End = PipelineText.find_first_of(",)");
419 if (!parseLoopPassName(LPM, PipelineText.substr(0, End)))
420 return false;
421 // TODO: Ideally, we would run a LoopVerifierPass() here in the
422 // VerifyEachPass case, but we don't have such a verifier yet.
423
424 PipelineText = PipelineText.substr(End);
425 }
426
427 if (PipelineText.empty() || PipelineText[0] == ')')
428 return true;
429
430 assert(PipelineText[0] == ',');
431 PipelineText = PipelineText.substr(1);
432 }
433}
434
Chandler Carruth1ff77242015-03-07 09:02:36 +0000435bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
436 StringRef &PipelineText,
437 bool VerifyEachPass,
438 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000439 for (;;) {
440 // Parse nested pass managers by recursing.
441 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000442 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000443
444 // Parse the inner pipeline inte the nested manager.
445 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000446 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
447 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000448 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000449 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000450 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000451 PipelineText = PipelineText.substr(1);
452
453 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000454 FPM.addPass(std::move(NestedFPM));
Justin Bognereecc3c82016-02-25 07:23:08 +0000455 } else if (PipelineText.startswith("loop(")) {
456 LoopPassManager NestedLPM(DebugLogging);
457
458 // Parse the inner pipeline inte the nested manager.
459 PipelineText = PipelineText.substr(strlen("loop("));
460 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
461 DebugLogging) ||
462 PipelineText.empty())
463 return false;
464 assert(PipelineText[0] == ')');
465 PipelineText = PipelineText.substr(1);
466
467 // Add the nested pass manager with the appropriate adaptor.
468 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(NestedLPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000469 } else {
470 // Otherwise try to parse a pass name.
471 size_t End = PipelineText.find_first_of(",)");
472 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
473 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000474 if (VerifyEachPass)
475 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000476
477 PipelineText = PipelineText.substr(End);
478 }
479
480 if (PipelineText.empty() || PipelineText[0] == ')')
481 return true;
482
483 assert(PipelineText[0] == ',');
484 PipelineText = PipelineText.substr(1);
485 }
486}
487
Chandler Carruth1ff77242015-03-07 09:02:36 +0000488bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
489 StringRef &PipelineText,
490 bool VerifyEachPass,
491 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000492 for (;;) {
493 // Parse nested pass managers by recursing.
494 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000495 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000496
497 // Parse the inner pipeline into the nested manager.
498 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000499 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
500 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000501 PipelineText.empty())
502 return false;
503 assert(PipelineText[0] == ')');
504 PipelineText = PipelineText.substr(1);
505
506 // Add the nested pass manager with the appropriate adaptor.
507 CGPM.addPass(std::move(NestedCGPM));
508 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000509 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000510
511 // Parse the inner pipeline inte the nested manager.
512 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000513 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
514 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000515 PipelineText.empty())
516 return false;
517 assert(PipelineText[0] == ')');
518 PipelineText = PipelineText.substr(1);
519
520 // Add the nested pass manager with the appropriate adaptor.
521 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
522 } else {
523 // Otherwise try to parse a pass name.
524 size_t End = PipelineText.find_first_of(",)");
525 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
526 return false;
527 // FIXME: No verifier support for CGSCC passes!
528
529 PipelineText = PipelineText.substr(End);
530 }
531
532 if (PipelineText.empty() || PipelineText[0] == ')')
533 return true;
534
535 assert(PipelineText[0] == ',');
536 PipelineText = PipelineText.substr(1);
537 }
538}
539
Chandler Carruth1ff77242015-03-07 09:02:36 +0000540bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
541 StringRef &PipelineText,
542 bool VerifyEachPass,
543 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000544 for (;;) {
545 // Parse nested pass managers by recursing.
546 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000547 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000548
549 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000550 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000551 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
552 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000553 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000554 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000555 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000556 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000557
558 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000559 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000560 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000561 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000562
563 // Parse the inner pipeline inte the nested manager.
564 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000565 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
566 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000567 PipelineText.empty())
568 return false;
569 assert(PipelineText[0] == ')');
570 PipelineText = PipelineText.substr(1);
571
572 // Add the nested pass manager with the appropriate adaptor.
573 MPM.addPass(
574 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000575 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000576 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000577
578 // Parse the inner pipeline inte the nested manager.
579 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000580 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
581 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000582 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000583 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000584 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000585 PipelineText = PipelineText.substr(1);
586
587 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000588 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000589 } else {
590 // Otherwise try to parse a pass name.
591 size_t End = PipelineText.find_first_of(",)");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000592 if (!parseModulePassName(MPM, PipelineText.substr(0, End), DebugLogging))
Chandler Carruth66445382014-01-11 08:16:35 +0000593 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000594 if (VerifyEachPass)
595 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000596
597 PipelineText = PipelineText.substr(End);
598 }
599
600 if (PipelineText.empty() || PipelineText[0] == ')')
601 return true;
602
603 assert(PipelineText[0] == ',');
604 PipelineText = PipelineText.substr(1);
605 }
606}
607
608// Primary pass pipeline description parsing routine.
609// FIXME: Should this routine accept a TargetMachine or require the caller to
610// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000611bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
612 StringRef PipelineText, bool VerifyEachPass,
613 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000614 // By default, try to parse the pipeline as-if it were within an implicit
615 // 'module(...)' pass pipeline. If this will parse at all, it needs to
616 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000617 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000618 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000619
Chandler Carruthea368f12015-01-06 08:37:58 +0000620 // This isn't parsable as a module pipeline, look for the end of a pass name
621 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000622 StringRef FirstName =
623 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000624 assert(!isModulePassName(FirstName) &&
625 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000626
Chandler Carruthea368f12015-01-06 08:37:58 +0000627 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
628 // pipeline.
Justin Bognereecc3c82016-02-25 07:23:08 +0000629 if (PipelineText.startswith("cgscc(") || isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000630 CGSCCPassManager CGPM(DebugLogging);
631 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
632 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000633 !PipelineText.empty())
634 return false;
635 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
636 return true;
637 }
638
Chandler Carruthea368f12015-01-06 08:37:58 +0000639 // Similarly, if this looks like a Function pass, parse the whole thing as
640 // a Function pipelien.
Justin Bognereecc3c82016-02-25 07:23:08 +0000641 if (PipelineText.startswith("function(") || isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000642 FunctionPassManager FPM(DebugLogging);
643 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
644 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000645 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000646 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000647 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000648 return true;
649 }
Chandler Carruth66445382014-01-11 08:16:35 +0000650
Justin Bognereecc3c82016-02-25 07:23:08 +0000651 // If this looks like a Loop pass, parse the whole thing as a Loop pipeline.
652 if (PipelineText.startswith("loop(") || isLoopPassName(FirstName)) {
653 LoopPassManager LPM(DebugLogging);
654 if (!parseLoopPassPipeline(LPM, PipelineText, VerifyEachPass,
655 DebugLogging) ||
656 !PipelineText.empty())
657 return false;
658 FunctionPassManager FPM(DebugLogging);
659 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
660 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
661 return true;
662 }
663
664
Chandler Carruth66445382014-01-11 08:16:35 +0000665 return false;
666}
Chandler Carruthedf59962016-02-18 09:45:17 +0000667
668bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
669 while (!PipelineText.empty()) {
670 StringRef Name;
671 std::tie(Name, PipelineText) = PipelineText.split(',');
672 if (!parseAAPassName(AA, Name))
673 return false;
674 }
675
676 return true;
677}