blob: f18fcb185754273e17d5e1d4aa2fe6f8929dfe82 [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 Carruthdf8b2232015-01-22 21:53:09 +000019#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000020#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000021#include "llvm/Analysis/LazyCallGraph.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000022#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000023#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000024#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000025#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000026#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000027#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000028#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000029#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000030#include "llvm/Support/Debug.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000031#include "llvm/Target/TargetMachine.h"
Chandler Carruth83ba2692015-01-24 04:19:17 +000032#include "llvm/Transforms/InstCombine/InstCombine.h"
Justin Bogner19b67992015-10-30 23:13:18 +000033#include "llvm/Transforms/Scalar/ADCE.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000034#include "llvm/Transforms/Scalar/EarlyCSE.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000035#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Chandler Carruthfdffd872015-02-01 11:34:21 +000036#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000037#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruth66445382014-01-11 08:16:35 +000038
39using namespace llvm;
40
41namespace {
42
Chandler Carruthd8330982014-01-12 09:34:22 +000043/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000044struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000045 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000046 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000047};
48
Chandler Carruth0b576b32015-01-06 02:50:06 +000049/// \brief No-op module analysis.
50struct NoOpModuleAnalysis {
51 struct Result {};
52 Result run(Module &) { return Result(); }
53 static StringRef name() { return "NoOpModuleAnalysis"; }
54 static void *ID() { return (void *)&PassID; }
55private:
56 static char PassID;
57};
58
59char NoOpModuleAnalysis::PassID;
60
Chandler Carruth572e3402014-04-21 11:12:00 +000061/// \brief No-op CGSCC pass which does nothing.
62struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000063 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +000064 return PreservedAnalyses::all();
65 }
66 static StringRef name() { return "NoOpCGSCCPass"; }
67};
68
Chandler Carruth0b576b32015-01-06 02:50:06 +000069/// \brief No-op CGSCC analysis.
70struct NoOpCGSCCAnalysis {
71 struct Result {};
72 Result run(LazyCallGraph::SCC &) { return Result(); }
73 static StringRef name() { return "NoOpCGSCCAnalysis"; }
74 static void *ID() { return (void *)&PassID; }
75private:
76 static char PassID;
77};
78
79char NoOpCGSCCAnalysis::PassID;
80
Chandler Carruthd8330982014-01-12 09:34:22 +000081/// \brief No-op function pass which does nothing.
82struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000083 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +000084 static StringRef name() { return "NoOpFunctionPass"; }
85};
86
Chandler Carruth0b576b32015-01-06 02:50:06 +000087/// \brief No-op function analysis.
88struct NoOpFunctionAnalysis {
89 struct Result {};
90 Result run(Function &) { return Result(); }
91 static StringRef name() { return "NoOpFunctionAnalysis"; }
92 static void *ID() { return (void *)&PassID; }
93private:
94 static char PassID;
95};
96
97char NoOpFunctionAnalysis::PassID;
98
Chandler Carruth66445382014-01-11 08:16:35 +000099} // End anonymous namespace.
100
Chandler Carruth1ff77242015-03-07 09:02:36 +0000101void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000102#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
103 MAM.registerPass(CREATE_PASS);
104#include "PassRegistry.def"
105}
106
Chandler Carruth1ff77242015-03-07 09:02:36 +0000107void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000108#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
109 CGAM.registerPass(CREATE_PASS);
110#include "PassRegistry.def"
111}
112
Chandler Carruth1ff77242015-03-07 09:02:36 +0000113void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000114#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
115 FAM.registerPass(CREATE_PASS);
116#include "PassRegistry.def"
117}
118
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000119#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000120static bool isModulePassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000121#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000122#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000123 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000124 return true;
125#include "PassRegistry.def"
126
Chandler Carruth66445382014-01-11 08:16:35 +0000127 return false;
128}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000129#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000130
Chandler Carruth572e3402014-04-21 11:12:00 +0000131static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000132#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000133#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000134 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000135 return true;
136#include "PassRegistry.def"
137
Chandler Carruth572e3402014-04-21 11:12:00 +0000138 return false;
139}
140
Chandler Carruthd8330982014-01-12 09:34:22 +0000141static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000142#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000143#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000144 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000145 return true;
146#include "PassRegistry.def"
147
Chandler Carruthd8330982014-01-12 09:34:22 +0000148 return false;
149}
150
Chandler Carruth1ff77242015-03-07 09:02:36 +0000151bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000152#define MODULE_PASS(NAME, CREATE_PASS) \
153 if (Name == NAME) { \
154 MPM.addPass(CREATE_PASS); \
155 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000156 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000157#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
158 if (Name == "require<" NAME ">") { \
Chandler Carruthe5b0a9c2015-01-07 11:14:51 +0000159 MPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000160 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000161 } \
162 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthe5b0a9c2015-01-07 11:14:51 +0000163 MPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000164 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000165 }
166#include "PassRegistry.def"
167
Chandler Carruth66445382014-01-11 08:16:35 +0000168 return false;
169}
170
Chandler Carruth1ff77242015-03-07 09:02:36 +0000171bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000172#define CGSCC_PASS(NAME, CREATE_PASS) \
173 if (Name == NAME) { \
174 CGPM.addPass(CREATE_PASS); \
175 return true; \
176 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000177#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
178 if (Name == "require<" NAME ">") { \
Chandler Carruthe5b0a9c2015-01-07 11:14:51 +0000179 CGPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000180 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000181 } \
182 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthe5b0a9c2015-01-07 11:14:51 +0000183 CGPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000184 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000185 }
186#include "PassRegistry.def"
187
Chandler Carruth572e3402014-04-21 11:12:00 +0000188 return false;
189}
190
Chandler Carruth1ff77242015-03-07 09:02:36 +0000191bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
192 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000193#define FUNCTION_PASS(NAME, CREATE_PASS) \
194 if (Name == NAME) { \
195 FPM.addPass(CREATE_PASS); \
196 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000197 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000198#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
199 if (Name == "require<" NAME ">") { \
Chandler Carruthe5b0a9c2015-01-07 11:14:51 +0000200 FPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000201 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000202 } \
203 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthe5b0a9c2015-01-07 11:14:51 +0000204 FPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000205 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000206 }
207#include "PassRegistry.def"
208
Chandler Carruthd8330982014-01-12 09:34:22 +0000209 return false;
210}
211
Chandler Carruth1ff77242015-03-07 09:02:36 +0000212bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
213 StringRef &PipelineText,
214 bool VerifyEachPass,
215 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000216 for (;;) {
217 // Parse nested pass managers by recursing.
218 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000219 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000220
221 // Parse the inner pipeline inte the nested manager.
222 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000223 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
224 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000225 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000226 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000227 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000228 PipelineText = PipelineText.substr(1);
229
230 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000231 FPM.addPass(std::move(NestedFPM));
Chandler Carruthd8330982014-01-12 09:34:22 +0000232 } else {
233 // Otherwise try to parse a pass name.
234 size_t End = PipelineText.find_first_of(",)");
235 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
236 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000237 if (VerifyEachPass)
238 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000239
240 PipelineText = PipelineText.substr(End);
241 }
242
243 if (PipelineText.empty() || PipelineText[0] == ')')
244 return true;
245
246 assert(PipelineText[0] == ',');
247 PipelineText = PipelineText.substr(1);
248 }
249}
250
Chandler Carruth1ff77242015-03-07 09:02:36 +0000251bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
252 StringRef &PipelineText,
253 bool VerifyEachPass,
254 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000255 for (;;) {
256 // Parse nested pass managers by recursing.
257 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000258 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000259
260 // Parse the inner pipeline into the nested manager.
261 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000262 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
263 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000264 PipelineText.empty())
265 return false;
266 assert(PipelineText[0] == ')');
267 PipelineText = PipelineText.substr(1);
268
269 // Add the nested pass manager with the appropriate adaptor.
270 CGPM.addPass(std::move(NestedCGPM));
271 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000272 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000273
274 // Parse the inner pipeline inte the nested manager.
275 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000276 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
277 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000278 PipelineText.empty())
279 return false;
280 assert(PipelineText[0] == ')');
281 PipelineText = PipelineText.substr(1);
282
283 // Add the nested pass manager with the appropriate adaptor.
284 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
285 } else {
286 // Otherwise try to parse a pass name.
287 size_t End = PipelineText.find_first_of(",)");
288 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
289 return false;
290 // FIXME: No verifier support for CGSCC passes!
291
292 PipelineText = PipelineText.substr(End);
293 }
294
295 if (PipelineText.empty() || PipelineText[0] == ')')
296 return true;
297
298 assert(PipelineText[0] == ',');
299 PipelineText = PipelineText.substr(1);
300 }
301}
302
Chandler Carruth1ff77242015-03-07 09:02:36 +0000303bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
304 StringRef &PipelineText,
305 bool VerifyEachPass,
306 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000307 for (;;) {
308 // Parse nested pass managers by recursing.
309 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000310 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000311
312 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000313 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000314 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
315 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000316 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000317 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000318 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000319 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000320
321 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000322 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000323 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000324 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000325
326 // Parse the inner pipeline inte the nested manager.
327 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000328 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
329 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000330 PipelineText.empty())
331 return false;
332 assert(PipelineText[0] == ')');
333 PipelineText = PipelineText.substr(1);
334
335 // Add the nested pass manager with the appropriate adaptor.
336 MPM.addPass(
337 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000338 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000339 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000340
341 // Parse the inner pipeline inte the nested manager.
342 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000343 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
344 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000345 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000346 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000347 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000348 PipelineText = PipelineText.substr(1);
349
350 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000351 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000352 } else {
353 // Otherwise try to parse a pass name.
354 size_t End = PipelineText.find_first_of(",)");
355 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
356 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000357 if (VerifyEachPass)
358 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000359
360 PipelineText = PipelineText.substr(End);
361 }
362
363 if (PipelineText.empty() || PipelineText[0] == ')')
364 return true;
365
366 assert(PipelineText[0] == ',');
367 PipelineText = PipelineText.substr(1);
368 }
369}
370
371// Primary pass pipeline description parsing routine.
372// FIXME: Should this routine accept a TargetMachine or require the caller to
373// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000374bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
375 StringRef PipelineText, bool VerifyEachPass,
376 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000377 // By default, try to parse the pipeline as-if it were within an implicit
378 // 'module(...)' pass pipeline. If this will parse at all, it needs to
379 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000380 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000381 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000382
Chandler Carruthea368f12015-01-06 08:37:58 +0000383 // This isn't parsable as a module pipeline, look for the end of a pass name
384 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000385 StringRef FirstName =
386 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000387 assert(!isModulePassName(FirstName) &&
388 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000389
Chandler Carruthea368f12015-01-06 08:37:58 +0000390 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
391 // pipeline.
Chandler Carruth572e3402014-04-21 11:12:00 +0000392 if (isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000393 CGSCCPassManager CGPM(DebugLogging);
394 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
395 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000396 !PipelineText.empty())
397 return false;
398 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
399 return true;
400 }
401
Chandler Carruthea368f12015-01-06 08:37:58 +0000402 // Similarly, if this looks like a Function pass, parse the whole thing as
403 // a Function pipelien.
Chandler Carruthd8330982014-01-12 09:34:22 +0000404 if (isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000405 FunctionPassManager FPM(DebugLogging);
406 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
407 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000408 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000409 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000410 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000411 return true;
412 }
Chandler Carruth66445382014-01-11 08:16:35 +0000413
414 return false;
415}