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(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
148 if (Name == "invalidate<" NAME ">") { \
149 MPM.addPass(InvalidateAnalysisPass<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(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
168 if (Name == "invalidate<" NAME ">") { \
169 CGPM.addPass(InvalidateAnalysisPass<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(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
188 if (Name == "invalidate<" NAME ">") { \
189 FPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
192 #include "PassRegistry.def"
197 static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
198 StringRef &PipelineText,
199 bool VerifyEachPass, bool DebugLogging) {
201 // Parse nested pass managers by recursing.
202 if (PipelineText.startswith("function(")) {
203 FunctionPassManager NestedFPM(DebugLogging);
205 // Parse the inner pipeline inte the nested manager.
206 PipelineText = PipelineText.substr(strlen("function("));
207 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
209 PipelineText.empty())
211 assert(PipelineText[0] == ')');
212 PipelineText = PipelineText.substr(1);
214 // Add the nested pass manager with the appropriate adaptor.
215 FPM.addPass(std::move(NestedFPM));
217 // Otherwise try to parse a pass name.
218 size_t End = PipelineText.find_first_of(",)");
219 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
222 FPM.addPass(VerifierPass());
224 PipelineText = PipelineText.substr(End);
227 if (PipelineText.empty() || PipelineText[0] == ')')
230 assert(PipelineText[0] == ',');
231 PipelineText = PipelineText.substr(1);
235 static bool parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
236 StringRef &PipelineText, bool VerifyEachPass,
239 // Parse nested pass managers by recursing.
240 if (PipelineText.startswith("cgscc(")) {
241 CGSCCPassManager NestedCGPM(DebugLogging);
243 // Parse the inner pipeline into the nested manager.
244 PipelineText = PipelineText.substr(strlen("cgscc("));
245 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
247 PipelineText.empty())
249 assert(PipelineText[0] == ')');
250 PipelineText = PipelineText.substr(1);
252 // Add the nested pass manager with the appropriate adaptor.
253 CGPM.addPass(std::move(NestedCGPM));
254 } else if (PipelineText.startswith("function(")) {
255 FunctionPassManager NestedFPM(DebugLogging);
257 // Parse the inner pipeline inte the nested manager.
258 PipelineText = PipelineText.substr(strlen("function("));
259 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
261 PipelineText.empty())
263 assert(PipelineText[0] == ')');
264 PipelineText = PipelineText.substr(1);
266 // Add the nested pass manager with the appropriate adaptor.
267 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
269 // Otherwise try to parse a pass name.
270 size_t End = PipelineText.find_first_of(",)");
271 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
273 // FIXME: No verifier support for CGSCC passes!
275 PipelineText = PipelineText.substr(End);
278 if (PipelineText.empty() || PipelineText[0] == ')')
281 assert(PipelineText[0] == ',');
282 PipelineText = PipelineText.substr(1);
286 static bool parseModulePassPipeline(ModulePassManager &MPM,
287 StringRef &PipelineText,
288 bool VerifyEachPass, bool DebugLogging) {
290 // Parse nested pass managers by recursing.
291 if (PipelineText.startswith("module(")) {
292 ModulePassManager NestedMPM(DebugLogging);
294 // Parse the inner pipeline into the nested manager.
295 PipelineText = PipelineText.substr(strlen("module("));
296 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
298 PipelineText.empty())
300 assert(PipelineText[0] == ')');
301 PipelineText = PipelineText.substr(1);
303 // Now add the nested manager as a module pass.
304 MPM.addPass(std::move(NestedMPM));
305 } else if (PipelineText.startswith("cgscc(")) {
306 CGSCCPassManager NestedCGPM(DebugLogging);
308 // Parse the inner pipeline inte the nested manager.
309 PipelineText = PipelineText.substr(strlen("cgscc("));
310 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
312 PipelineText.empty())
314 assert(PipelineText[0] == ')');
315 PipelineText = PipelineText.substr(1);
317 // Add the nested pass manager with the appropriate adaptor.
319 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
320 } else if (PipelineText.startswith("function(")) {
321 FunctionPassManager NestedFPM(DebugLogging);
323 // Parse the inner pipeline inte the nested manager.
324 PipelineText = PipelineText.substr(strlen("function("));
325 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
327 PipelineText.empty())
329 assert(PipelineText[0] == ')');
330 PipelineText = PipelineText.substr(1);
332 // Add the nested pass manager with the appropriate adaptor.
333 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
335 // Otherwise try to parse a pass name.
336 size_t End = PipelineText.find_first_of(",)");
337 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
340 MPM.addPass(VerifierPass());
342 PipelineText = PipelineText.substr(End);
345 if (PipelineText.empty() || PipelineText[0] == ')')
348 assert(PipelineText[0] == ',');
349 PipelineText = PipelineText.substr(1);
353 // Primary pass pipeline description parsing routine.
354 // FIXME: Should this routine accept a TargetMachine or require the caller to
355 // pre-populate the analysis managers with target-specific stuff?
356 bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText,
357 bool VerifyEachPass, bool DebugLogging) {
358 // By default, try to parse the pipeline as-if it were within an implicit
359 // 'module(...)' pass pipeline. If this will parse at all, it needs to
360 // consume the entire string.
361 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
362 return PipelineText.empty();
364 // This isn't parsable as a module pipeline, look for the end of a pass name
365 // and directly drop down to that layer.
366 StringRef FirstName =
367 PipelineText.substr(0, PipelineText.find_first_of(",)"));
368 assert(!isModulePassName(FirstName) &&
369 "Already handled all module pipeline options.");
371 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
373 if (isCGSCCPassName(FirstName)) {
374 CGSCCPassManager CGPM(DebugLogging);
375 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
377 !PipelineText.empty())
379 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
383 // Similarly, if this looks like a Function pass, parse the whole thing as
384 // a Function pipelien.
385 if (isFunctionPassName(FirstName)) {
386 FunctionPassManager FPM(DebugLogging);
387 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
389 !PipelineText.empty())
391 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));