blob: eab4edadacc392a4aa17a02572719f4ce1c6c171 [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"
Hongbin Zheng751337f2016-02-25 17:54:15 +000026#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000027#include "llvm/Analysis/LazyCallGraph.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000028#include "llvm/Analysis/LoopInfo.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000029#include "llvm/Analysis/PostDominators.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000030#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000031#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000032#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000033#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000034#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000035#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000036#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000037#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000038#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000039#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000040#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000041#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000042#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000043#include "llvm/Target/TargetMachine.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000044#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000045#include "llvm/Transforms/IPO/FunctionAttrs.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000046#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Justin Bogner21e15372015-10-30 23:28:12 +000047#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000048#include "llvm/Transforms/InstCombine/InstCombine.h"
Justin Bogner19b67992015-10-30 23:13:18 +000049#include "llvm/Transforms/Scalar/ADCE.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000050#include "llvm/Transforms/Scalar/EarlyCSE.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000051#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000052#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000053#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Chandler Carruth58dde8c2016-02-26 12:17:54 +000054#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +000055
56using namespace llvm;
57
Chandler Carruth8b5a74192016-02-28 22:16:03 +000058static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
59
Chandler Carruth66445382014-01-11 08:16:35 +000060namespace {
61
Chandler Carruthd8330982014-01-12 09:34:22 +000062/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000063struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000064 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000065 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000066};
67
Chandler Carruth0b576b32015-01-06 02:50:06 +000068/// \brief No-op module analysis.
Chandler Carruth3a634352016-02-26 11:44:45 +000069struct NoOpModuleAnalysis : AnalysisBase<NoOpModuleAnalysis> {
Chandler Carruth0b576b32015-01-06 02:50:06 +000070 struct Result {};
71 Result run(Module &) { return Result(); }
72 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +000073};
74
Chandler Carruth572e3402014-04-21 11:12:00 +000075/// \brief No-op CGSCC pass which does nothing.
76struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000077 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +000078 return PreservedAnalyses::all();
79 }
80 static StringRef name() { return "NoOpCGSCCPass"; }
81};
82
Chandler Carruth0b576b32015-01-06 02:50:06 +000083/// \brief No-op CGSCC analysis.
Chandler Carruth3a634352016-02-26 11:44:45 +000084struct NoOpCGSCCAnalysis : AnalysisBase<NoOpCGSCCAnalysis> {
Chandler Carruth0b576b32015-01-06 02:50:06 +000085 struct Result {};
86 Result run(LazyCallGraph::SCC &) { return Result(); }
87 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +000088};
89
Chandler Carruthd8330982014-01-12 09:34:22 +000090/// \brief No-op function pass which does nothing.
91struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000092 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +000093 static StringRef name() { return "NoOpFunctionPass"; }
94};
95
Chandler Carruth0b576b32015-01-06 02:50:06 +000096/// \brief No-op function analysis.
Chandler Carruth3a634352016-02-26 11:44:45 +000097struct NoOpFunctionAnalysis : AnalysisBase<NoOpFunctionAnalysis> {
Chandler Carruth0b576b32015-01-06 02:50:06 +000098 struct Result {};
99 Result run(Function &) { return Result(); }
100 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000101};
102
Justin Bognereecc3c82016-02-25 07:23:08 +0000103/// \brief No-op loop pass which does nothing.
104struct NoOpLoopPass {
105 PreservedAnalyses run(Loop &L) { return PreservedAnalyses::all(); }
106 static StringRef name() { return "NoOpLoopPass"; }
107};
108
109/// \brief No-op loop analysis.
Chandler Carruth3a634352016-02-26 11:44:45 +0000110struct NoOpLoopAnalysis : AnalysisBase<NoOpLoopAnalysis> {
Justin Bognereecc3c82016-02-25 07:23:08 +0000111 struct Result {};
112 Result run(Loop &) { return Result(); }
113 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000114};
115
Chandler Carruth66445382014-01-11 08:16:35 +0000116} // End anonymous namespace.
117
Chandler Carruth1ff77242015-03-07 09:02:36 +0000118void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000119#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000120 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000121#include "PassRegistry.def"
122}
123
Chandler Carruth1ff77242015-03-07 09:02:36 +0000124void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000125#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000126 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000127#include "PassRegistry.def"
128}
129
Chandler Carruth1ff77242015-03-07 09:02:36 +0000130void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000131#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000132 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000133#include "PassRegistry.def"
134}
135
Justin Bognereecc3c82016-02-25 07:23:08 +0000136void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
137#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
138 LAM.registerPass([&] { return CREATE_PASS; });
139#include "PassRegistry.def"
140}
141
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000142void PassBuilder::addPerModuleDefaultPipeline(ModulePassManager &MPM,
143 OptimizationLevel Level,
144 bool DebugLogging) {
145 // FIXME: Finish fleshing this out to match the legacy pipelines.
146 FunctionPassManager EarlyFPM(DebugLogging);
147 EarlyFPM.addPass(SimplifyCFGPass());
148 EarlyFPM.addPass(SROA());
149 EarlyFPM.addPass(EarlyCSEPass());
150 EarlyFPM.addPass(LowerExpectIntrinsicPass());
151
152 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
153}
154
155void PassBuilder::addLTOPreLinkDefaultPipeline(ModulePassManager &MPM,
156 OptimizationLevel Level,
157 bool DebugLogging) {
158 // FIXME: We should use a customized pre-link pipeline!
159 addPerModuleDefaultPipeline(MPM, Level, DebugLogging);
160}
161
162void PassBuilder::addLTODefaultPipeline(ModulePassManager &MPM,
163 OptimizationLevel Level,
164 bool DebugLogging) {
165 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
166 FunctionPassManager LateFPM(DebugLogging);
167 LateFPM.addPass(InstCombinePass());
168 LateFPM.addPass(SimplifyCFGPass());
169
170 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
171}
172
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000173#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000174static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000175 // Manually handle aliases for pre-configured pipeline fragments.
176 if (Name.startswith("default") || Name.startswith("lto"))
177 return DefaultAliasRegex.match(Name);
178
Chandler Carruth58944182014-04-21 08:08:50 +0000179#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000180#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000181 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000182 return true;
183#include "PassRegistry.def"
184
Chandler Carruth66445382014-01-11 08:16:35 +0000185 return false;
186}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000187#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000188
Chandler Carruth572e3402014-04-21 11:12:00 +0000189static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000190#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000191#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000192 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000193 return true;
194#include "PassRegistry.def"
195
Chandler Carruth572e3402014-04-21 11:12:00 +0000196 return false;
197}
198
Chandler Carruthd8330982014-01-12 09:34:22 +0000199static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000200#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000201#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000202 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000203 return true;
204#include "PassRegistry.def"
205
Chandler Carruthd8330982014-01-12 09:34:22 +0000206 return false;
207}
208
Justin Bognereecc3c82016-02-25 07:23:08 +0000209static bool isLoopPassName(StringRef Name) {
210#define LOOP_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
211#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
212 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
213 return true;
214#include "PassRegistry.def"
215
216 return false;
217}
218
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000219bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name,
220 bool DebugLogging) {
221 // Manually handle aliases for pre-configured pipeline fragments.
222 if (Name.startswith("default") || Name.startswith("lto")) {
223 SmallVector<StringRef, 3> Matches;
224 if (!DefaultAliasRegex.match(Name, &Matches))
225 return false;
226 assert(Matches.size() == 3 && "Must capture two matched strings!");
227
228 auto L = StringSwitch<OptimizationLevel>(Matches[2])
229 .Case("O0", O0)
230 .Case("O1", O1)
231 .Case("O2", O2)
232 .Case("O3", O3)
233 .Case("Os", Os)
234 .Case("Oz", Oz);
235
236 if (Matches[1] == "default") {
237 addPerModuleDefaultPipeline(MPM, L, DebugLogging);
238 } else if (Matches[1] == "lto-pre-link") {
239 addLTOPreLinkDefaultPipeline(MPM, L, DebugLogging);
240 } else {
241 assert(Matches[1] == "lto" && "Not one of the matched options!");
242 addLTODefaultPipeline(MPM, L, DebugLogging);
243 }
244 return true;
245 }
246
Chandler Carruth58944182014-04-21 08:08:50 +0000247#define MODULE_PASS(NAME, CREATE_PASS) \
248 if (Name == NAME) { \
249 MPM.addPass(CREATE_PASS); \
250 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000251 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000252#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
253 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000254 MPM.addPass(RequireAnalysisPass< \
255 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000256 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000257 } \
258 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000259 MPM.addPass(InvalidateAnalysisPass< \
260 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000261 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000262 }
263#include "PassRegistry.def"
264
Chandler Carruth66445382014-01-11 08:16:35 +0000265 return false;
266}
267
Chandler Carruth1ff77242015-03-07 09:02:36 +0000268bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000269#define CGSCC_PASS(NAME, CREATE_PASS) \
270 if (Name == NAME) { \
271 CGPM.addPass(CREATE_PASS); \
272 return true; \
273 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000274#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
275 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000276 CGPM.addPass(RequireAnalysisPass< \
277 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000278 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000279 } \
280 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000281 CGPM.addPass(InvalidateAnalysisPass< \
282 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000283 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000284 }
285#include "PassRegistry.def"
286
Chandler Carruth572e3402014-04-21 11:12:00 +0000287 return false;
288}
289
Chandler Carruth1ff77242015-03-07 09:02:36 +0000290bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
291 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000292#define FUNCTION_PASS(NAME, CREATE_PASS) \
293 if (Name == NAME) { \
294 FPM.addPass(CREATE_PASS); \
295 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000296 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000297#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
298 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000299 FPM.addPass(RequireAnalysisPass< \
300 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000301 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000302 } \
303 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000304 FPM.addPass(InvalidateAnalysisPass< \
305 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000306 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000307 }
308#include "PassRegistry.def"
309
Chandler Carruthd8330982014-01-12 09:34:22 +0000310 return false;
311}
312
Justin Bognereecc3c82016-02-25 07:23:08 +0000313bool PassBuilder::parseLoopPassName(LoopPassManager &FPM,
314 StringRef Name) {
315#define LOOP_PASS(NAME, CREATE_PASS) \
316 if (Name == NAME) { \
317 FPM.addPass(CREATE_PASS); \
318 return true; \
319 }
320#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
321 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000322 FPM.addPass(RequireAnalysisPass< \
323 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000324 return true; \
325 } \
326 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000327 FPM.addPass(InvalidateAnalysisPass< \
328 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000329 return true; \
330 }
331#include "PassRegistry.def"
332
333 return false;
334}
335
Chandler Carruthedf59962016-02-18 09:45:17 +0000336bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
337#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
338 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000339 AA.registerFunctionAnalysis< \
340 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +0000341 return true; \
342 }
343#include "PassRegistry.def"
344
345 return false;
346}
347
Justin Bognereecc3c82016-02-25 07:23:08 +0000348bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
349 StringRef &PipelineText,
350 bool VerifyEachPass,
351 bool DebugLogging) {
352 for (;;) {
353 // Parse nested pass managers by recursing.
354 if (PipelineText.startswith("loop(")) {
355 LoopPassManager NestedLPM(DebugLogging);
356
357 // Parse the inner pipeline inte the nested manager.
358 PipelineText = PipelineText.substr(strlen("loop("));
359 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
360 DebugLogging) ||
361 PipelineText.empty())
362 return false;
363 assert(PipelineText[0] == ')');
364 PipelineText = PipelineText.substr(1);
365
366 // Add the nested pass manager with the appropriate adaptor.
367 LPM.addPass(std::move(NestedLPM));
368 } else {
369 // Otherwise try to parse a pass name.
370 size_t End = PipelineText.find_first_of(",)");
371 if (!parseLoopPassName(LPM, PipelineText.substr(0, End)))
372 return false;
373 // TODO: Ideally, we would run a LoopVerifierPass() here in the
374 // VerifyEachPass case, but we don't have such a verifier yet.
375
376 PipelineText = PipelineText.substr(End);
377 }
378
379 if (PipelineText.empty() || PipelineText[0] == ')')
380 return true;
381
382 assert(PipelineText[0] == ',');
383 PipelineText = PipelineText.substr(1);
384 }
385}
386
Chandler Carruth1ff77242015-03-07 09:02:36 +0000387bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
388 StringRef &PipelineText,
389 bool VerifyEachPass,
390 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000391 for (;;) {
392 // Parse nested pass managers by recursing.
393 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000394 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000395
396 // Parse the inner pipeline inte the nested manager.
397 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000398 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
399 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000400 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000401 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000402 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000403 PipelineText = PipelineText.substr(1);
404
405 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000406 FPM.addPass(std::move(NestedFPM));
Justin Bognereecc3c82016-02-25 07:23:08 +0000407 } else if (PipelineText.startswith("loop(")) {
408 LoopPassManager NestedLPM(DebugLogging);
409
410 // Parse the inner pipeline inte the nested manager.
411 PipelineText = PipelineText.substr(strlen("loop("));
412 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
413 DebugLogging) ||
414 PipelineText.empty())
415 return false;
416 assert(PipelineText[0] == ')');
417 PipelineText = PipelineText.substr(1);
418
419 // Add the nested pass manager with the appropriate adaptor.
420 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(NestedLPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000421 } else {
422 // Otherwise try to parse a pass name.
423 size_t End = PipelineText.find_first_of(",)");
424 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
425 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000426 if (VerifyEachPass)
427 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000428
429 PipelineText = PipelineText.substr(End);
430 }
431
432 if (PipelineText.empty() || PipelineText[0] == ')')
433 return true;
434
435 assert(PipelineText[0] == ',');
436 PipelineText = PipelineText.substr(1);
437 }
438}
439
Chandler Carruth1ff77242015-03-07 09:02:36 +0000440bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
441 StringRef &PipelineText,
442 bool VerifyEachPass,
443 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000444 for (;;) {
445 // Parse nested pass managers by recursing.
446 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000447 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000448
449 // Parse the inner pipeline into the nested manager.
450 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000451 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
452 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000453 PipelineText.empty())
454 return false;
455 assert(PipelineText[0] == ')');
456 PipelineText = PipelineText.substr(1);
457
458 // Add the nested pass manager with the appropriate adaptor.
459 CGPM.addPass(std::move(NestedCGPM));
460 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000461 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000462
463 // Parse the inner pipeline inte the nested manager.
464 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000465 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
466 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000467 PipelineText.empty())
468 return false;
469 assert(PipelineText[0] == ')');
470 PipelineText = PipelineText.substr(1);
471
472 // Add the nested pass manager with the appropriate adaptor.
473 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
474 } else {
475 // Otherwise try to parse a pass name.
476 size_t End = PipelineText.find_first_of(",)");
477 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
478 return false;
479 // FIXME: No verifier support for CGSCC passes!
480
481 PipelineText = PipelineText.substr(End);
482 }
483
484 if (PipelineText.empty() || PipelineText[0] == ')')
485 return true;
486
487 assert(PipelineText[0] == ',');
488 PipelineText = PipelineText.substr(1);
489 }
490}
491
Chandler Carruth1ff77242015-03-07 09:02:36 +0000492bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
493 StringRef &PipelineText,
494 bool VerifyEachPass,
495 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000496 for (;;) {
497 // Parse nested pass managers by recursing.
498 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000499 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000500
501 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000502 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000503 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
504 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000505 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000506 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000507 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000508 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000509
510 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000511 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000512 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000513 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000514
515 // Parse the inner pipeline inte the nested manager.
516 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000517 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
518 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000519 PipelineText.empty())
520 return false;
521 assert(PipelineText[0] == ')');
522 PipelineText = PipelineText.substr(1);
523
524 // Add the nested pass manager with the appropriate adaptor.
525 MPM.addPass(
526 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000527 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000528 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000529
530 // Parse the inner pipeline inte the nested manager.
531 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000532 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
533 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000534 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000535 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000536 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000537 PipelineText = PipelineText.substr(1);
538
539 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000540 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000541 } else {
542 // Otherwise try to parse a pass name.
543 size_t End = PipelineText.find_first_of(",)");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000544 if (!parseModulePassName(MPM, PipelineText.substr(0, End), DebugLogging))
Chandler Carruth66445382014-01-11 08:16:35 +0000545 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000546 if (VerifyEachPass)
547 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000548
549 PipelineText = PipelineText.substr(End);
550 }
551
552 if (PipelineText.empty() || PipelineText[0] == ')')
553 return true;
554
555 assert(PipelineText[0] == ',');
556 PipelineText = PipelineText.substr(1);
557 }
558}
559
560// Primary pass pipeline description parsing routine.
561// FIXME: Should this routine accept a TargetMachine or require the caller to
562// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000563bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
564 StringRef PipelineText, bool VerifyEachPass,
565 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000566 // By default, try to parse the pipeline as-if it were within an implicit
567 // 'module(...)' pass pipeline. If this will parse at all, it needs to
568 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000569 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000570 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000571
Chandler Carruthea368f12015-01-06 08:37:58 +0000572 // This isn't parsable as a module pipeline, look for the end of a pass name
573 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000574 StringRef FirstName =
575 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000576 assert(!isModulePassName(FirstName) &&
577 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000578
Chandler Carruthea368f12015-01-06 08:37:58 +0000579 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
580 // pipeline.
Justin Bognereecc3c82016-02-25 07:23:08 +0000581 if (PipelineText.startswith("cgscc(") || isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000582 CGSCCPassManager CGPM(DebugLogging);
583 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
584 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000585 !PipelineText.empty())
586 return false;
587 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
588 return true;
589 }
590
Chandler Carruthea368f12015-01-06 08:37:58 +0000591 // Similarly, if this looks like a Function pass, parse the whole thing as
592 // a Function pipelien.
Justin Bognereecc3c82016-02-25 07:23:08 +0000593 if (PipelineText.startswith("function(") || isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000594 FunctionPassManager FPM(DebugLogging);
595 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
596 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000597 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000598 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000599 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000600 return true;
601 }
Chandler Carruth66445382014-01-11 08:16:35 +0000602
Justin Bognereecc3c82016-02-25 07:23:08 +0000603 // If this looks like a Loop pass, parse the whole thing as a Loop pipeline.
604 if (PipelineText.startswith("loop(") || isLoopPassName(FirstName)) {
605 LoopPassManager LPM(DebugLogging);
606 if (!parseLoopPassPipeline(LPM, PipelineText, VerifyEachPass,
607 DebugLogging) ||
608 !PipelineText.empty())
609 return false;
610 FunctionPassManager FPM(DebugLogging);
611 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
612 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
613 return true;
614 }
615
616
Chandler Carruth66445382014-01-11 08:16:35 +0000617 return false;
618}
Chandler Carruthedf59962016-02-18 09:45:17 +0000619
620bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
621 while (!PipelineText.empty()) {
622 StringRef Name;
623 std::tie(Name, PipelineText) = PipelineText.split(',');
624 if (!parseAAPassName(AA, Name))
625 return false;
626 }
627
628 return true;
629}