1 //===- Passes.cpp - Parsing, selection, and running of passes -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file provides the infrastructure to parse and build a custom pass
12 /// manager based on a commandline flag. It also provides helpers to aid in
13 /// analyzing, debugging, and testing pass structures.
15 //===----------------------------------------------------------------------===//
18 #include "llvm/Analysis/CGSCCPassManager.h"
19 #include "llvm/Analysis/LazyCallGraph.h"
20 #include "llvm/IR/IRPrintingPasses.h"
21 #include "llvm/IR/PassManager.h"
22 #include "llvm/IR/Verifier.h"
23 #include "llvm/Support/Debug.h"
29 /// \brief No-op module pass which does nothing.
30 struct NoOpModulePass {
31 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
32 static StringRef name() { return "NoOpModulePass"; }
35 /// \brief No-op module analysis.
36 struct NoOpModuleAnalysis {
38 Result run(Module &) { return Result(); }
39 static StringRef name() { return "NoOpModuleAnalysis"; }
40 static void *ID() { return (void *)&PassID; }
45 char NoOpModuleAnalysis::PassID;
47 /// \brief No-op CGSCC pass which does nothing.
48 struct NoOpCGSCCPass {
49 PreservedAnalyses run(LazyCallGraph::SCC &C) {
50 return PreservedAnalyses::all();
52 static StringRef name() { return "NoOpCGSCCPass"; }
55 /// \brief No-op CGSCC analysis.
56 struct NoOpCGSCCAnalysis {
58 Result run(LazyCallGraph::SCC &) { return Result(); }
59 static StringRef name() { return "NoOpCGSCCAnalysis"; }
60 static void *ID() { return (void *)&PassID; }
65 char NoOpCGSCCAnalysis::PassID;
67 /// \brief No-op function pass which does nothing.
68 struct NoOpFunctionPass {
69 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
70 static StringRef name() { return "NoOpFunctionPass"; }
73 /// \brief No-op function analysis.
74 struct NoOpFunctionAnalysis {
76 Result run(Function &) { return Result(); }
77 static StringRef name() { return "NoOpFunctionAnalysis"; }
78 static void *ID() { return (void *)&PassID; }
83 char NoOpFunctionAnalysis::PassID;
85 } // End anonymous namespace.
87 void llvm::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
88 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
89 MAM.registerPass(CREATE_PASS);
90 #include "PassRegistry.def"
93 void llvm::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
94 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
95 CGAM.registerPass(CREATE_PASS);
96 #include "PassRegistry.def"
99 void llvm::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
100 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
101 FAM.registerPass(CREATE_PASS);
102 #include "PassRegistry.def"
106 static bool isModulePassName(StringRef Name) {
107 #define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
108 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
109 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
111 #include "PassRegistry.def"
117 static bool isCGSCCPassName(StringRef Name) {
118 #define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
119 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
120 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
122 #include "PassRegistry.def"
127 static bool isFunctionPassName(StringRef Name) {
128 #define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
129 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
130 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
132 #include "PassRegistry.def"
137 static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
138 #define MODULE_PASS(NAME, CREATE_PASS) \
139 if (Name == NAME) { \
140 MPM.addPass(CREATE_PASS); \
143 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
144 if (Name == "require<" NAME ">") { \
145 MPM.addPass(NoopAnalysisRequirementPass<decltype(CREATE_PASS)>()); \
148 if (Name == "invalidate<" NAME ">") { \
149 MPM.addPass(NoopAnalysisInvalidationPass<decltype(CREATE_PASS)>()); \
152 #include "PassRegistry.def"
157 static bool parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
158 #define CGSCC_PASS(NAME, CREATE_PASS) \
159 if (Name == NAME) { \
160 CGPM.addPass(CREATE_PASS); \
163 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
164 if (Name == "require<" NAME ">") { \
165 CGPM.addPass(NoopAnalysisRequirementPass<decltype(CREATE_PASS)>()); \
168 if (Name == "invalidate<" NAME ">") { \
169 CGPM.addPass(NoopAnalysisInvalidationPass<decltype(CREATE_PASS)>()); \
172 #include "PassRegistry.def"
177 static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
178 #define FUNCTION_PASS(NAME, CREATE_PASS) \
179 if (Name == NAME) { \
180 FPM.addPass(CREATE_PASS); \
183 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
184 if (Name == "require<" NAME ">") { \
185 FPM.addPass(NoopAnalysisRequirementPass<decltype(CREATE_PASS)>()); \
188 if (Name == "invalidate<" NAME ">") { \
189 FPM.addPass(NoopAnalysisInvalidationPass<decltype(CREATE_PASS)>()); \
192 #include "PassRegistry.def"
197 static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
198 StringRef &PipelineText,
199 bool VerifyEachPass) {
201 // Parse nested pass managers by recursing.
202 if (PipelineText.startswith("function(")) {
203 FunctionPassManager NestedFPM;
205 // Parse the inner pipeline inte the nested manager.
206 PipelineText = PipelineText.substr(strlen("function("));
207 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
208 PipelineText.empty())
210 assert(PipelineText[0] == ')');
211 PipelineText = PipelineText.substr(1);
213 // Add the nested pass manager with the appropriate adaptor.
214 FPM.addPass(std::move(NestedFPM));
216 // Otherwise try to parse a pass name.
217 size_t End = PipelineText.find_first_of(",)");
218 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
221 FPM.addPass(VerifierPass());
223 PipelineText = PipelineText.substr(End);
226 if (PipelineText.empty() || PipelineText[0] == ')')
229 assert(PipelineText[0] == ',');
230 PipelineText = PipelineText.substr(1);
234 static bool parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
235 StringRef &PipelineText,
236 bool VerifyEachPass) {
238 // Parse nested pass managers by recursing.
239 if (PipelineText.startswith("cgscc(")) {
240 CGSCCPassManager NestedCGPM;
242 // Parse the inner pipeline into the nested manager.
243 PipelineText = PipelineText.substr(strlen("cgscc("));
244 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass) ||
245 PipelineText.empty())
247 assert(PipelineText[0] == ')');
248 PipelineText = PipelineText.substr(1);
250 // Add the nested pass manager with the appropriate adaptor.
251 CGPM.addPass(std::move(NestedCGPM));
252 } else if (PipelineText.startswith("function(")) {
253 FunctionPassManager NestedFPM;
255 // Parse the inner pipeline inte the nested manager.
256 PipelineText = PipelineText.substr(strlen("function("));
257 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
258 PipelineText.empty())
260 assert(PipelineText[0] == ')');
261 PipelineText = PipelineText.substr(1);
263 // Add the nested pass manager with the appropriate adaptor.
264 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
266 // Otherwise try to parse a pass name.
267 size_t End = PipelineText.find_first_of(",)");
268 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
270 // FIXME: No verifier support for CGSCC passes!
272 PipelineText = PipelineText.substr(End);
275 if (PipelineText.empty() || PipelineText[0] == ')')
278 assert(PipelineText[0] == ',');
279 PipelineText = PipelineText.substr(1);
283 static bool parseModulePassPipeline(ModulePassManager &MPM,
284 StringRef &PipelineText,
285 bool VerifyEachPass) {
287 // Parse nested pass managers by recursing.
288 if (PipelineText.startswith("module(")) {
289 ModulePassManager NestedMPM;
291 // Parse the inner pipeline into the nested manager.
292 PipelineText = PipelineText.substr(strlen("module("));
293 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass) ||
294 PipelineText.empty())
296 assert(PipelineText[0] == ')');
297 PipelineText = PipelineText.substr(1);
299 // Now add the nested manager as a module pass.
300 MPM.addPass(std::move(NestedMPM));
301 } else if (PipelineText.startswith("cgscc(")) {
302 CGSCCPassManager NestedCGPM;
304 // Parse the inner pipeline inte the nested manager.
305 PipelineText = PipelineText.substr(strlen("cgscc("));
306 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass) ||
307 PipelineText.empty())
309 assert(PipelineText[0] == ')');
310 PipelineText = PipelineText.substr(1);
312 // Add the nested pass manager with the appropriate adaptor.
314 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
315 } else if (PipelineText.startswith("function(")) {
316 FunctionPassManager NestedFPM;
318 // Parse the inner pipeline inte the nested manager.
319 PipelineText = PipelineText.substr(strlen("function("));
320 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
321 PipelineText.empty())
323 assert(PipelineText[0] == ')');
324 PipelineText = PipelineText.substr(1);
326 // Add the nested pass manager with the appropriate adaptor.
327 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
329 // Otherwise try to parse a pass name.
330 size_t End = PipelineText.find_first_of(",)");
331 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
334 MPM.addPass(VerifierPass());
336 PipelineText = PipelineText.substr(End);
339 if (PipelineText.empty() || PipelineText[0] == ')')
342 assert(PipelineText[0] == ',');
343 PipelineText = PipelineText.substr(1);
347 // Primary pass pipeline description parsing routine.
348 // FIXME: Should this routine accept a TargetMachine or require the caller to
349 // pre-populate the analysis managers with target-specific stuff?
350 bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText,
351 bool VerifyEachPass) {
352 // By default, try to parse the pipeline as-if it were within an implicit
353 // 'module(...)' pass pipeline. If this will parse at all, it needs to
354 // consume the entire string.
355 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass))
356 return PipelineText.empty();
358 // This isn't parsable as a module pipeline, look for the end of a pass name
359 // and directly drop down to that layer.
360 StringRef FirstName =
361 PipelineText.substr(0, PipelineText.find_first_of(",)"));
362 assert(!isModulePassName(FirstName) &&
363 "Already handled all module pipeline options.");
365 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
367 if (isCGSCCPassName(FirstName)) {
368 CGSCCPassManager CGPM;
369 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass) ||
370 !PipelineText.empty())
372 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
376 // Similarly, if this looks like a Function pass, parse the whole thing as
377 // a Function pipelien.
378 if (isFunctionPassName(FirstName)) {
379 FunctionPassManager FPM;
380 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
381 !PipelineText.empty())
383 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));