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"
105 static bool isModulePassName(StringRef Name) {
106 #define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
107 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
108 if (Name == "require<" NAME ">") \
110 #include "PassRegistry.def"
115 static bool isCGSCCPassName(StringRef Name) {
116 #define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
117 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
118 if (Name == "require<" NAME ">") \
120 #include "PassRegistry.def"
125 static bool isFunctionPassName(StringRef Name) {
126 #define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
127 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
128 if (Name == "require<" NAME ">") \
130 #include "PassRegistry.def"
135 static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
136 #define MODULE_PASS(NAME, CREATE_PASS) \
137 if (Name == NAME) { \
138 MPM.addPass(CREATE_PASS); \
141 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
142 if (Name == "require<" NAME ">") { \
143 MPM.addPass(NoopAnalysisRequirementPass<decltype(CREATE_PASS)>()); \
146 #include "PassRegistry.def"
151 static bool parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
152 #define CGSCC_PASS(NAME, CREATE_PASS) \
153 if (Name == NAME) { \
154 CGPM.addPass(CREATE_PASS); \
157 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
158 if (Name == "require<" NAME ">") { \
159 CGPM.addPass(NoopAnalysisRequirementPass<decltype(CREATE_PASS)>()); \
162 #include "PassRegistry.def"
167 static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
168 #define FUNCTION_PASS(NAME, CREATE_PASS) \
169 if (Name == NAME) { \
170 FPM.addPass(CREATE_PASS); \
173 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
174 if (Name == "require<" NAME ">") { \
175 FPM.addPass(NoopAnalysisRequirementPass<decltype(CREATE_PASS)>()); \
178 #include "PassRegistry.def"
183 static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
184 StringRef &PipelineText,
185 bool VerifyEachPass) {
187 // Parse nested pass managers by recursing.
188 if (PipelineText.startswith("function(")) {
189 FunctionPassManager NestedFPM;
191 // Parse the inner pipeline inte the nested manager.
192 PipelineText = PipelineText.substr(strlen("function("));
193 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
194 PipelineText.empty())
196 assert(PipelineText[0] == ')');
197 PipelineText = PipelineText.substr(1);
199 // Add the nested pass manager with the appropriate adaptor.
200 FPM.addPass(std::move(NestedFPM));
202 // Otherwise try to parse a pass name.
203 size_t End = PipelineText.find_first_of(",)");
204 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
207 FPM.addPass(VerifierPass());
209 PipelineText = PipelineText.substr(End);
212 if (PipelineText.empty() || PipelineText[0] == ')')
215 assert(PipelineText[0] == ',');
216 PipelineText = PipelineText.substr(1);
220 static bool parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
221 StringRef &PipelineText,
222 bool VerifyEachPass) {
224 // Parse nested pass managers by recursing.
225 if (PipelineText.startswith("cgscc(")) {
226 CGSCCPassManager NestedCGPM;
228 // Parse the inner pipeline into the nested manager.
229 PipelineText = PipelineText.substr(strlen("cgscc("));
230 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass) ||
231 PipelineText.empty())
233 assert(PipelineText[0] == ')');
234 PipelineText = PipelineText.substr(1);
236 // Add the nested pass manager with the appropriate adaptor.
237 CGPM.addPass(std::move(NestedCGPM));
238 } else if (PipelineText.startswith("function(")) {
239 FunctionPassManager NestedFPM;
241 // Parse the inner pipeline inte the nested manager.
242 PipelineText = PipelineText.substr(strlen("function("));
243 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
244 PipelineText.empty())
246 assert(PipelineText[0] == ')');
247 PipelineText = PipelineText.substr(1);
249 // Add the nested pass manager with the appropriate adaptor.
250 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
252 // Otherwise try to parse a pass name.
253 size_t End = PipelineText.find_first_of(",)");
254 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
256 // FIXME: No verifier support for CGSCC passes!
258 PipelineText = PipelineText.substr(End);
261 if (PipelineText.empty() || PipelineText[0] == ')')
264 assert(PipelineText[0] == ',');
265 PipelineText = PipelineText.substr(1);
269 static bool parseModulePassPipeline(ModulePassManager &MPM,
270 StringRef &PipelineText,
271 bool VerifyEachPass) {
273 // Parse nested pass managers by recursing.
274 if (PipelineText.startswith("module(")) {
275 ModulePassManager NestedMPM;
277 // Parse the inner pipeline into the nested manager.
278 PipelineText = PipelineText.substr(strlen("module("));
279 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass) ||
280 PipelineText.empty())
282 assert(PipelineText[0] == ')');
283 PipelineText = PipelineText.substr(1);
285 // Now add the nested manager as a module pass.
286 MPM.addPass(std::move(NestedMPM));
287 } else if (PipelineText.startswith("cgscc(")) {
288 CGSCCPassManager NestedCGPM;
290 // Parse the inner pipeline inte the nested manager.
291 PipelineText = PipelineText.substr(strlen("cgscc("));
292 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass) ||
293 PipelineText.empty())
295 assert(PipelineText[0] == ')');
296 PipelineText = PipelineText.substr(1);
298 // Add the nested pass manager with the appropriate adaptor.
300 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
301 } else if (PipelineText.startswith("function(")) {
302 FunctionPassManager NestedFPM;
304 // Parse the inner pipeline inte the nested manager.
305 PipelineText = PipelineText.substr(strlen("function("));
306 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
307 PipelineText.empty())
309 assert(PipelineText[0] == ')');
310 PipelineText = PipelineText.substr(1);
312 // Add the nested pass manager with the appropriate adaptor.
313 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
315 // Otherwise try to parse a pass name.
316 size_t End = PipelineText.find_first_of(",)");
317 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
320 MPM.addPass(VerifierPass());
322 PipelineText = PipelineText.substr(End);
325 if (PipelineText.empty() || PipelineText[0] == ')')
328 assert(PipelineText[0] == ',');
329 PipelineText = PipelineText.substr(1);
333 // Primary pass pipeline description parsing routine.
334 // FIXME: Should this routine accept a TargetMachine or require the caller to
335 // pre-populate the analysis managers with target-specific stuff?
336 bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText,
337 bool VerifyEachPass) {
338 // Look at the first entry to figure out which layer to start parsing at.
339 if (PipelineText.startswith("module("))
340 return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
341 PipelineText.empty();
342 if (PipelineText.startswith("cgscc(")) {
343 CGSCCPassManager CGPM;
344 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass) ||
345 !PipelineText.empty())
347 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
350 if (PipelineText.startswith("function(")) {
351 FunctionPassManager FPM;
352 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
353 !PipelineText.empty())
355 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
359 // This isn't a direct pass manager name, look for the end of a pass name.
360 StringRef FirstName =
361 PipelineText.substr(0, PipelineText.find_first_of(",)"));
362 if (isModulePassName(FirstName))
363 return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
364 PipelineText.empty();
366 if (isCGSCCPassName(FirstName)) {
367 CGSCCPassManager CGPM;
368 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass) ||
369 !PipelineText.empty())
371 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
375 if (isFunctionPassName(FirstName)) {
376 FunctionPassManager FPM;
377 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
378 !PipelineText.empty())
380 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));