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 #include "PassRegistry.def"
109 // We also support building a require pass around any analysis.
110 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
111 if (Name == "require<" NAME ">") \
113 #include "PassRegistry.def"
118 static bool isCGSCCPassName(StringRef Name) {
119 #define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
120 #include "PassRegistry.def"
122 // We also support building a require pass around any analysis.
123 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
124 if (Name == "require<" NAME ">") \
126 #include "PassRegistry.def"
131 static bool isFunctionPassName(StringRef Name) {
132 #define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
133 #include "PassRegistry.def"
135 // We also support building a require pass around any analysis.
136 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
137 if (Name == "require<" NAME ">") \
139 #include "PassRegistry.def"
144 static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
145 #define MODULE_PASS(NAME, CREATE_PASS) \
146 if (Name == NAME) { \
147 MPM.addPass(CREATE_PASS); \
150 #include "PassRegistry.def"
152 // We also support building a require pass around any analysis.
153 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
154 if (Name == "require<" NAME ">") { \
155 MPM.addPass(NoopAnalysisRequirementPass<decltype(CREATE_PASS)>()); \
158 #include "PassRegistry.def"
163 static bool parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
164 #define CGSCC_PASS(NAME, CREATE_PASS) \
165 if (Name == NAME) { \
166 CGPM.addPass(CREATE_PASS); \
169 #include "PassRegistry.def"
171 // We also support building a require pass around any analysis.
172 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
173 if (Name == "require<" NAME ">") { \
174 CGPM.addPass(NoopAnalysisRequirementPass<decltype(CREATE_PASS)>()); \
177 #include "PassRegistry.def"
182 static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
183 #define FUNCTION_PASS(NAME, CREATE_PASS) \
184 if (Name == NAME) { \
185 FPM.addPass(CREATE_PASS); \
188 #include "PassRegistry.def"
190 // We also support building a require pass around any analysis.
191 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
192 if (Name == "require<" NAME ">") { \
193 FPM.addPass(NoopAnalysisRequirementPass<decltype(CREATE_PASS)>()); \
196 #include "PassRegistry.def"
201 static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
202 StringRef &PipelineText,
203 bool VerifyEachPass) {
205 // Parse nested pass managers by recursing.
206 if (PipelineText.startswith("function(")) {
207 FunctionPassManager NestedFPM;
209 // Parse the inner pipeline inte the nested manager.
210 PipelineText = PipelineText.substr(strlen("function("));
211 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
212 PipelineText.empty())
214 assert(PipelineText[0] == ')');
215 PipelineText = PipelineText.substr(1);
217 // Add the nested pass manager with the appropriate adaptor.
218 FPM.addPass(std::move(NestedFPM));
220 // Otherwise try to parse a pass name.
221 size_t End = PipelineText.find_first_of(",)");
222 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
225 FPM.addPass(VerifierPass());
227 PipelineText = PipelineText.substr(End);
230 if (PipelineText.empty() || PipelineText[0] == ')')
233 assert(PipelineText[0] == ',');
234 PipelineText = PipelineText.substr(1);
238 static bool parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
239 StringRef &PipelineText,
240 bool VerifyEachPass) {
242 // Parse nested pass managers by recursing.
243 if (PipelineText.startswith("cgscc(")) {
244 CGSCCPassManager NestedCGPM;
246 // Parse the inner pipeline into the nested manager.
247 PipelineText = PipelineText.substr(strlen("cgscc("));
248 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass) ||
249 PipelineText.empty())
251 assert(PipelineText[0] == ')');
252 PipelineText = PipelineText.substr(1);
254 // Add the nested pass manager with the appropriate adaptor.
255 CGPM.addPass(std::move(NestedCGPM));
256 } else if (PipelineText.startswith("function(")) {
257 FunctionPassManager NestedFPM;
259 // Parse the inner pipeline inte the nested manager.
260 PipelineText = PipelineText.substr(strlen("function("));
261 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
262 PipelineText.empty())
264 assert(PipelineText[0] == ')');
265 PipelineText = PipelineText.substr(1);
267 // Add the nested pass manager with the appropriate adaptor.
268 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
270 // Otherwise try to parse a pass name.
271 size_t End = PipelineText.find_first_of(",)");
272 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
274 // FIXME: No verifier support for CGSCC passes!
276 PipelineText = PipelineText.substr(End);
279 if (PipelineText.empty() || PipelineText[0] == ')')
282 assert(PipelineText[0] == ',');
283 PipelineText = PipelineText.substr(1);
287 static bool parseModulePassPipeline(ModulePassManager &MPM,
288 StringRef &PipelineText,
289 bool VerifyEachPass) {
291 // Parse nested pass managers by recursing.
292 if (PipelineText.startswith("module(")) {
293 ModulePassManager NestedMPM;
295 // Parse the inner pipeline into the nested manager.
296 PipelineText = PipelineText.substr(strlen("module("));
297 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;
308 // Parse the inner pipeline inte the nested manager.
309 PipelineText = PipelineText.substr(strlen("cgscc("));
310 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass) ||
311 PipelineText.empty())
313 assert(PipelineText[0] == ')');
314 PipelineText = PipelineText.substr(1);
316 // Add the nested pass manager with the appropriate adaptor.
318 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
319 } else if (PipelineText.startswith("function(")) {
320 FunctionPassManager NestedFPM;
322 // Parse the inner pipeline inte the nested manager.
323 PipelineText = PipelineText.substr(strlen("function("));
324 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
325 PipelineText.empty())
327 assert(PipelineText[0] == ')');
328 PipelineText = PipelineText.substr(1);
330 // Add the nested pass manager with the appropriate adaptor.
331 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
333 // Otherwise try to parse a pass name.
334 size_t End = PipelineText.find_first_of(",)");
335 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
338 MPM.addPass(VerifierPass());
340 PipelineText = PipelineText.substr(End);
343 if (PipelineText.empty() || PipelineText[0] == ')')
346 assert(PipelineText[0] == ',');
347 PipelineText = PipelineText.substr(1);
351 // Primary pass pipeline description parsing routine.
352 // FIXME: Should this routine accept a TargetMachine or require the caller to
353 // pre-populate the analysis managers with target-specific stuff?
354 bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText,
355 bool VerifyEachPass) {
356 // Look at the first entry to figure out which layer to start parsing at.
357 if (PipelineText.startswith("module("))
358 return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
359 PipelineText.empty();
360 if (PipelineText.startswith("cgscc(")) {
361 CGSCCPassManager CGPM;
362 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass) ||
363 !PipelineText.empty())
365 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
368 if (PipelineText.startswith("function(")) {
369 FunctionPassManager FPM;
370 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
371 !PipelineText.empty())
373 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
377 // This isn't a direct pass manager name, look for the end of a pass name.
378 StringRef FirstName =
379 PipelineText.substr(0, PipelineText.find_first_of(",)"));
380 if (isModulePassName(FirstName))
381 return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
382 PipelineText.empty();
384 if (isCGSCCPassName(FirstName)) {
385 CGSCCPassManager CGPM;
386 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass) ||
387 !PipelineText.empty())
389 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
393 if (isFunctionPassName(FirstName)) {
394 FunctionPassManager FPM;
395 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
396 !PipelineText.empty())
398 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));