blob: 80c6e5e8df6da35ed8738b78aa9150812491f18f [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 Carruth6f5770b102016-02-13 23:32:00 +000019#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth4f846a52016-02-20 03:46:03 +000020#include "llvm/Analysis/AliasAnalysisEvaluator.h"
Chandler Carruthdf8b2232015-01-22 21:53:09 +000021#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthbece8d52016-02-13 23:46:24 +000022#include "llvm/Analysis/BasicAliasAnalysis.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000023#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000024#include "llvm/Analysis/LazyCallGraph.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000025#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000026#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000027#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000028#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000029#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000030#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000031#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000032#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000033#include "llvm/Support/Debug.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000034#include "llvm/Target/TargetMachine.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000035#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000036#include "llvm/Transforms/IPO/FunctionAttrs.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000037#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Justin Bogner21e15372015-10-30 23:28:12 +000038#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000039#include "llvm/Transforms/InstCombine/InstCombine.h"
Justin Bogner19b67992015-10-30 23:13:18 +000040#include "llvm/Transforms/Scalar/ADCE.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000041#include "llvm/Transforms/Scalar/EarlyCSE.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000042#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000043#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000044#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Chandler Carruth66445382014-01-11 08:16:35 +000045
46using namespace llvm;
47
48namespace {
49
Chandler Carruthd8330982014-01-12 09:34:22 +000050/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000051struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000052 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000053 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000054};
55
Chandler Carruth0b576b32015-01-06 02:50:06 +000056/// \brief No-op module analysis.
57struct NoOpModuleAnalysis {
58 struct Result {};
59 Result run(Module &) { return Result(); }
60 static StringRef name() { return "NoOpModuleAnalysis"; }
61 static void *ID() { return (void *)&PassID; }
62private:
63 static char PassID;
64};
65
66char NoOpModuleAnalysis::PassID;
67
Chandler Carruth572e3402014-04-21 11:12:00 +000068/// \brief No-op CGSCC pass which does nothing.
69struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000070 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +000071 return PreservedAnalyses::all();
72 }
73 static StringRef name() { return "NoOpCGSCCPass"; }
74};
75
Chandler Carruth0b576b32015-01-06 02:50:06 +000076/// \brief No-op CGSCC analysis.
77struct NoOpCGSCCAnalysis {
78 struct Result {};
79 Result run(LazyCallGraph::SCC &) { return Result(); }
80 static StringRef name() { return "NoOpCGSCCAnalysis"; }
81 static void *ID() { return (void *)&PassID; }
82private:
83 static char PassID;
84};
85
86char NoOpCGSCCAnalysis::PassID;
87
Chandler Carruthd8330982014-01-12 09:34:22 +000088/// \brief No-op function pass which does nothing.
89struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000090 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +000091 static StringRef name() { return "NoOpFunctionPass"; }
92};
93
Chandler Carruth0b576b32015-01-06 02:50:06 +000094/// \brief No-op function analysis.
95struct NoOpFunctionAnalysis {
96 struct Result {};
97 Result run(Function &) { return Result(); }
98 static StringRef name() { return "NoOpFunctionAnalysis"; }
99 static void *ID() { return (void *)&PassID; }
100private:
101 static char PassID;
102};
103
104char NoOpFunctionAnalysis::PassID;
105
Chandler Carruth66445382014-01-11 08:16:35 +0000106} // End anonymous namespace.
107
Chandler Carruth1ff77242015-03-07 09:02:36 +0000108void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000109#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000110 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000111#include "PassRegistry.def"
112}
113
Chandler Carruth1ff77242015-03-07 09:02:36 +0000114void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000115#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000116 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000117#include "PassRegistry.def"
118}
119
Chandler Carruth1ff77242015-03-07 09:02:36 +0000120void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000121#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000122 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000123#include "PassRegistry.def"
124}
125
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000126#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000127static bool isModulePassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000128#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000129#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000130 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000131 return true;
132#include "PassRegistry.def"
133
Chandler Carruth66445382014-01-11 08:16:35 +0000134 return false;
135}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000136#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000137
Chandler Carruth572e3402014-04-21 11:12:00 +0000138static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000139#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000140#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000141 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000142 return true;
143#include "PassRegistry.def"
144
Chandler Carruth572e3402014-04-21 11:12:00 +0000145 return false;
146}
147
Chandler Carruthd8330982014-01-12 09:34:22 +0000148static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000149#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000150#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000151 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000152 return true;
153#include "PassRegistry.def"
154
Chandler Carruthd8330982014-01-12 09:34:22 +0000155 return false;
156}
157
Chandler Carruth1ff77242015-03-07 09:02:36 +0000158bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000159#define MODULE_PASS(NAME, CREATE_PASS) \
160 if (Name == NAME) { \
161 MPM.addPass(CREATE_PASS); \
162 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000163 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000164#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
165 if (Name == "require<" NAME ">") { \
Chandler Carruthe5b0a9c2015-01-07 11:14:51 +0000166 MPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000167 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000168 } \
169 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthe5b0a9c2015-01-07 11:14:51 +0000170 MPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000171 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000172 }
173#include "PassRegistry.def"
174
Chandler Carruth66445382014-01-11 08:16:35 +0000175 return false;
176}
177
Chandler Carruth1ff77242015-03-07 09:02:36 +0000178bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000179#define CGSCC_PASS(NAME, CREATE_PASS) \
180 if (Name == NAME) { \
181 CGPM.addPass(CREATE_PASS); \
182 return true; \
183 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000184#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
185 if (Name == "require<" NAME ">") { \
Chandler Carruthe5b0a9c2015-01-07 11:14:51 +0000186 CGPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000187 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000188 } \
189 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthe5b0a9c2015-01-07 11:14:51 +0000190 CGPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000191 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000192 }
193#include "PassRegistry.def"
194
Chandler Carruth572e3402014-04-21 11:12:00 +0000195 return false;
196}
197
Chandler Carruth1ff77242015-03-07 09:02:36 +0000198bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
199 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000200#define FUNCTION_PASS(NAME, CREATE_PASS) \
201 if (Name == NAME) { \
202 FPM.addPass(CREATE_PASS); \
203 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000204 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000205#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
206 if (Name == "require<" NAME ">") { \
Chandler Carruthe5b0a9c2015-01-07 11:14:51 +0000207 FPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000208 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000209 } \
210 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthe5b0a9c2015-01-07 11:14:51 +0000211 FPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000212 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000213 }
214#include "PassRegistry.def"
215
Chandler Carruthd8330982014-01-12 09:34:22 +0000216 return false;
217}
218
Chandler Carruthedf59962016-02-18 09:45:17 +0000219bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
220#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
221 if (Name == NAME) { \
222 AA.registerFunctionAnalysis<decltype(CREATE_PASS)>(); \
223 return true; \
224 }
225#include "PassRegistry.def"
226
227 return false;
228}
229
Chandler Carruth1ff77242015-03-07 09:02:36 +0000230bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
231 StringRef &PipelineText,
232 bool VerifyEachPass,
233 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000234 for (;;) {
235 // Parse nested pass managers by recursing.
236 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000237 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000238
239 // Parse the inner pipeline inte the nested manager.
240 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000241 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
242 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000243 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000244 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000245 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000246 PipelineText = PipelineText.substr(1);
247
248 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000249 FPM.addPass(std::move(NestedFPM));
Chandler Carruthd8330982014-01-12 09:34:22 +0000250 } else {
251 // Otherwise try to parse a pass name.
252 size_t End = PipelineText.find_first_of(",)");
253 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
254 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000255 if (VerifyEachPass)
256 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000257
258 PipelineText = PipelineText.substr(End);
259 }
260
261 if (PipelineText.empty() || PipelineText[0] == ')')
262 return true;
263
264 assert(PipelineText[0] == ',');
265 PipelineText = PipelineText.substr(1);
266 }
267}
268
Chandler Carruth1ff77242015-03-07 09:02:36 +0000269bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
270 StringRef &PipelineText,
271 bool VerifyEachPass,
272 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000273 for (;;) {
274 // Parse nested pass managers by recursing.
275 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000276 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000277
278 // Parse the inner pipeline into the nested manager.
279 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000280 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
281 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000282 PipelineText.empty())
283 return false;
284 assert(PipelineText[0] == ')');
285 PipelineText = PipelineText.substr(1);
286
287 // Add the nested pass manager with the appropriate adaptor.
288 CGPM.addPass(std::move(NestedCGPM));
289 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000290 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000291
292 // Parse the inner pipeline inte the nested manager.
293 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000294 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
295 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000296 PipelineText.empty())
297 return false;
298 assert(PipelineText[0] == ')');
299 PipelineText = PipelineText.substr(1);
300
301 // Add the nested pass manager with the appropriate adaptor.
302 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
303 } else {
304 // Otherwise try to parse a pass name.
305 size_t End = PipelineText.find_first_of(",)");
306 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
307 return false;
308 // FIXME: No verifier support for CGSCC passes!
309
310 PipelineText = PipelineText.substr(End);
311 }
312
313 if (PipelineText.empty() || PipelineText[0] == ')')
314 return true;
315
316 assert(PipelineText[0] == ',');
317 PipelineText = PipelineText.substr(1);
318 }
319}
320
Chandler Carruth1ff77242015-03-07 09:02:36 +0000321bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
322 StringRef &PipelineText,
323 bool VerifyEachPass,
324 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000325 for (;;) {
326 // Parse nested pass managers by recursing.
327 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000328 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000329
330 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000331 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000332 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
333 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000334 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000335 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000336 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000337 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000338
339 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000340 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000341 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000342 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000343
344 // Parse the inner pipeline inte the nested manager.
345 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000346 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
347 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000348 PipelineText.empty())
349 return false;
350 assert(PipelineText[0] == ')');
351 PipelineText = PipelineText.substr(1);
352
353 // Add the nested pass manager with the appropriate adaptor.
354 MPM.addPass(
355 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000356 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000357 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000358
359 // Parse the inner pipeline inte the nested manager.
360 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000361 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
362 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000363 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000364 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000365 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000366 PipelineText = PipelineText.substr(1);
367
368 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000369 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000370 } else {
371 // Otherwise try to parse a pass name.
372 size_t End = PipelineText.find_first_of(",)");
373 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
374 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000375 if (VerifyEachPass)
376 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000377
378 PipelineText = PipelineText.substr(End);
379 }
380
381 if (PipelineText.empty() || PipelineText[0] == ')')
382 return true;
383
384 assert(PipelineText[0] == ',');
385 PipelineText = PipelineText.substr(1);
386 }
387}
388
389// Primary pass pipeline description parsing routine.
390// FIXME: Should this routine accept a TargetMachine or require the caller to
391// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000392bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
393 StringRef PipelineText, bool VerifyEachPass,
394 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000395 // By default, try to parse the pipeline as-if it were within an implicit
396 // 'module(...)' pass pipeline. If this will parse at all, it needs to
397 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000398 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000399 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000400
Chandler Carruthea368f12015-01-06 08:37:58 +0000401 // This isn't parsable as a module pipeline, look for the end of a pass name
402 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000403 StringRef FirstName =
404 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000405 assert(!isModulePassName(FirstName) &&
406 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000407
Chandler Carruthea368f12015-01-06 08:37:58 +0000408 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
409 // pipeline.
Chandler Carruth572e3402014-04-21 11:12:00 +0000410 if (isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000411 CGSCCPassManager CGPM(DebugLogging);
412 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
413 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000414 !PipelineText.empty())
415 return false;
416 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
417 return true;
418 }
419
Chandler Carruthea368f12015-01-06 08:37:58 +0000420 // Similarly, if this looks like a Function pass, parse the whole thing as
421 // a Function pipelien.
Chandler Carruthd8330982014-01-12 09:34:22 +0000422 if (isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000423 FunctionPassManager FPM(DebugLogging);
424 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
425 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000426 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000427 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000428 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000429 return true;
430 }
Chandler Carruth66445382014-01-11 08:16:35 +0000431
432 return false;
433}
Chandler Carruthedf59962016-02-18 09:45:17 +0000434
435bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
436 while (!PipelineText.empty()) {
437 StringRef Name;
438 std::tie(Name, PipelineText) = PipelineText.split(',');
439 if (!parseAAPassName(AA, Name))
440 return false;
441 }
442
443 return true;
444}