blob: 66457633bbaa2b6f5bbc0e5532893d17af509466 [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 Carruthdf8b2232015-01-22 21:53:09 +000020#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000021#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000022#include "llvm/Analysis/LazyCallGraph.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000023#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000024#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000025#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000026#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000027#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000028#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000029#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000030#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000031#include "llvm/Support/Debug.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000032#include "llvm/Target/TargetMachine.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000033#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000034#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Justin Bogner21e15372015-10-30 23:28:12 +000035#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000036#include "llvm/Transforms/InstCombine/InstCombine.h"
Justin Bogner19b67992015-10-30 23:13:18 +000037#include "llvm/Transforms/Scalar/ADCE.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000038#include "llvm/Transforms/Scalar/EarlyCSE.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000039#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000040#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000041#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Chandler Carruth66445382014-01-11 08:16:35 +000042
43using namespace llvm;
44
45namespace {
46
Chandler Carruthd8330982014-01-12 09:34:22 +000047/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000048struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000049 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000050 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000051};
52
Chandler Carruth0b576b32015-01-06 02:50:06 +000053/// \brief No-op module analysis.
54struct NoOpModuleAnalysis {
55 struct Result {};
56 Result run(Module &) { return Result(); }
57 static StringRef name() { return "NoOpModuleAnalysis"; }
58 static void *ID() { return (void *)&PassID; }
59private:
60 static char PassID;
61};
62
63char NoOpModuleAnalysis::PassID;
64
Chandler Carruth572e3402014-04-21 11:12:00 +000065/// \brief No-op CGSCC pass which does nothing.
66struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000067 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +000068 return PreservedAnalyses::all();
69 }
70 static StringRef name() { return "NoOpCGSCCPass"; }
71};
72
Chandler Carruth0b576b32015-01-06 02:50:06 +000073/// \brief No-op CGSCC analysis.
74struct NoOpCGSCCAnalysis {
75 struct Result {};
76 Result run(LazyCallGraph::SCC &) { return Result(); }
77 static StringRef name() { return "NoOpCGSCCAnalysis"; }
78 static void *ID() { return (void *)&PassID; }
79private:
80 static char PassID;
81};
82
83char NoOpCGSCCAnalysis::PassID;
84
Chandler Carruthd8330982014-01-12 09:34:22 +000085/// \brief No-op function pass which does nothing.
86struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000087 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +000088 static StringRef name() { return "NoOpFunctionPass"; }
89};
90
Chandler Carruth0b576b32015-01-06 02:50:06 +000091/// \brief No-op function analysis.
92struct NoOpFunctionAnalysis {
93 struct Result {};
94 Result run(Function &) { return Result(); }
95 static StringRef name() { return "NoOpFunctionAnalysis"; }
96 static void *ID() { return (void *)&PassID; }
97private:
98 static char PassID;
99};
100
101char NoOpFunctionAnalysis::PassID;
102
Chandler Carruth66445382014-01-11 08:16:35 +0000103} // End anonymous namespace.
104
Chandler Carruth1ff77242015-03-07 09:02:36 +0000105void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000106#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
107 MAM.registerPass(CREATE_PASS);
108#include "PassRegistry.def"
109}
110
Chandler Carruth1ff77242015-03-07 09:02:36 +0000111void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000112#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
113 CGAM.registerPass(CREATE_PASS);
114#include "PassRegistry.def"
115}
116
Chandler Carruth1ff77242015-03-07 09:02:36 +0000117void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000118#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
119 FAM.registerPass(CREATE_PASS);
120#include "PassRegistry.def"
121}
122
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000123#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000124static bool isModulePassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000125#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000126#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000127 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000128 return true;
129#include "PassRegistry.def"
130
Chandler Carruth66445382014-01-11 08:16:35 +0000131 return false;
132}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000133#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000134
Chandler Carruth572e3402014-04-21 11:12:00 +0000135static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000136#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000137#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000138 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000139 return true;
140#include "PassRegistry.def"
141
Chandler Carruth572e3402014-04-21 11:12:00 +0000142 return false;
143}
144
Chandler Carruthd8330982014-01-12 09:34:22 +0000145static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000146#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000147#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000148 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000149 return true;
150#include "PassRegistry.def"
151
Chandler Carruthd8330982014-01-12 09:34:22 +0000152 return false;
153}
154
Chandler Carruth1ff77242015-03-07 09:02:36 +0000155bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000156#define MODULE_PASS(NAME, CREATE_PASS) \
157 if (Name == NAME) { \
158 MPM.addPass(CREATE_PASS); \
159 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000160 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000161#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
162 if (Name == "require<" NAME ">") { \
Chandler Carruthe5b0a9c2015-01-07 11:14:51 +0000163 MPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000164 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000165 } \
166 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthe5b0a9c2015-01-07 11:14:51 +0000167 MPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000168 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000169 }
170#include "PassRegistry.def"
171
Chandler Carruth66445382014-01-11 08:16:35 +0000172 return false;
173}
174
Chandler Carruth1ff77242015-03-07 09:02:36 +0000175bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000176#define CGSCC_PASS(NAME, CREATE_PASS) \
177 if (Name == NAME) { \
178 CGPM.addPass(CREATE_PASS); \
179 return true; \
180 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000181#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
182 if (Name == "require<" NAME ">") { \
Chandler Carruthe5b0a9c2015-01-07 11:14:51 +0000183 CGPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000184 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000185 } \
186 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthe5b0a9c2015-01-07 11:14:51 +0000187 CGPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000188 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000189 }
190#include "PassRegistry.def"
191
Chandler Carruth572e3402014-04-21 11:12:00 +0000192 return false;
193}
194
Chandler Carruth1ff77242015-03-07 09:02:36 +0000195bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
196 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000197#define FUNCTION_PASS(NAME, CREATE_PASS) \
198 if (Name == NAME) { \
199 FPM.addPass(CREATE_PASS); \
200 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000201 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000202#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
203 if (Name == "require<" NAME ">") { \
Chandler Carruthe5b0a9c2015-01-07 11:14:51 +0000204 FPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000205 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000206 } \
207 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthe5b0a9c2015-01-07 11:14:51 +0000208 FPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000209 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000210 }
211#include "PassRegistry.def"
212
Chandler Carruthd8330982014-01-12 09:34:22 +0000213 return false;
214}
215
Chandler Carruth1ff77242015-03-07 09:02:36 +0000216bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
217 StringRef &PipelineText,
218 bool VerifyEachPass,
219 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000220 for (;;) {
221 // Parse nested pass managers by recursing.
222 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000223 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000224
225 // Parse the inner pipeline inte the nested manager.
226 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000227 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
228 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000229 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000230 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000231 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000232 PipelineText = PipelineText.substr(1);
233
234 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000235 FPM.addPass(std::move(NestedFPM));
Chandler Carruthd8330982014-01-12 09:34:22 +0000236 } else {
237 // Otherwise try to parse a pass name.
238 size_t End = PipelineText.find_first_of(",)");
239 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
240 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000241 if (VerifyEachPass)
242 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000243
244 PipelineText = PipelineText.substr(End);
245 }
246
247 if (PipelineText.empty() || PipelineText[0] == ')')
248 return true;
249
250 assert(PipelineText[0] == ',');
251 PipelineText = PipelineText.substr(1);
252 }
253}
254
Chandler Carruth1ff77242015-03-07 09:02:36 +0000255bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
256 StringRef &PipelineText,
257 bool VerifyEachPass,
258 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000259 for (;;) {
260 // Parse nested pass managers by recursing.
261 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000262 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000263
264 // Parse the inner pipeline into the nested manager.
265 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000266 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
267 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000268 PipelineText.empty())
269 return false;
270 assert(PipelineText[0] == ')');
271 PipelineText = PipelineText.substr(1);
272
273 // Add the nested pass manager with the appropriate adaptor.
274 CGPM.addPass(std::move(NestedCGPM));
275 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000276 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000277
278 // Parse the inner pipeline inte the nested manager.
279 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000280 if (!parseFunctionPassPipeline(NestedFPM, 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(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
289 } else {
290 // Otherwise try to parse a pass name.
291 size_t End = PipelineText.find_first_of(",)");
292 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
293 return false;
294 // FIXME: No verifier support for CGSCC passes!
295
296 PipelineText = PipelineText.substr(End);
297 }
298
299 if (PipelineText.empty() || PipelineText[0] == ')')
300 return true;
301
302 assert(PipelineText[0] == ',');
303 PipelineText = PipelineText.substr(1);
304 }
305}
306
Chandler Carruth1ff77242015-03-07 09:02:36 +0000307bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
308 StringRef &PipelineText,
309 bool VerifyEachPass,
310 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000311 for (;;) {
312 // Parse nested pass managers by recursing.
313 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000314 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000315
316 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000317 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000318 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
319 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000320 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000321 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000322 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000323 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000324
325 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000326 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000327 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000328 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000329
330 // Parse the inner pipeline inte the nested manager.
331 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000332 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
333 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000334 PipelineText.empty())
335 return false;
336 assert(PipelineText[0] == ')');
337 PipelineText = PipelineText.substr(1);
338
339 // Add the nested pass manager with the appropriate adaptor.
340 MPM.addPass(
341 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000342 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000343 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000344
345 // Parse the inner pipeline inte the nested manager.
346 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000347 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
348 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000349 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000350 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000351 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000352 PipelineText = PipelineText.substr(1);
353
354 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000355 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000356 } else {
357 // Otherwise try to parse a pass name.
358 size_t End = PipelineText.find_first_of(",)");
359 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
360 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000361 if (VerifyEachPass)
362 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000363
364 PipelineText = PipelineText.substr(End);
365 }
366
367 if (PipelineText.empty() || PipelineText[0] == ')')
368 return true;
369
370 assert(PipelineText[0] == ',');
371 PipelineText = PipelineText.substr(1);
372 }
373}
374
375// Primary pass pipeline description parsing routine.
376// FIXME: Should this routine accept a TargetMachine or require the caller to
377// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000378bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
379 StringRef PipelineText, bool VerifyEachPass,
380 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000381 // By default, try to parse the pipeline as-if it were within an implicit
382 // 'module(...)' pass pipeline. If this will parse at all, it needs to
383 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000384 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000385 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000386
Chandler Carruthea368f12015-01-06 08:37:58 +0000387 // This isn't parsable as a module pipeline, look for the end of a pass name
388 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000389 StringRef FirstName =
390 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000391 assert(!isModulePassName(FirstName) &&
392 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000393
Chandler Carruthea368f12015-01-06 08:37:58 +0000394 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
395 // pipeline.
Chandler Carruth572e3402014-04-21 11:12:00 +0000396 if (isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000397 CGSCCPassManager CGPM(DebugLogging);
398 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
399 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000400 !PipelineText.empty())
401 return false;
402 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
403 return true;
404 }
405
Chandler Carruthea368f12015-01-06 08:37:58 +0000406 // Similarly, if this looks like a Function pass, parse the whole thing as
407 // a Function pipelien.
Chandler Carruthd8330982014-01-12 09:34:22 +0000408 if (isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000409 FunctionPassManager FPM(DebugLogging);
410 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
411 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000412 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000413 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000414 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000415 return true;
416 }
Chandler Carruth66445382014-01-11 08:16:35 +0000417
418 return false;
419}