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 ">" || Name == "invalidate<" 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 ">" || Name == "invalidate<" 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 ">" || Name == "invalidate<" 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 if (Name == "invalidate<" NAME ">") { \
147 MPM.addPass(NoopAnalysisInvalidationPass<decltype(CREATE_PASS)>()); \
150 #include "PassRegistry.def"
155 static bool parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
156 #define CGSCC_PASS(NAME, CREATE_PASS) \
157 if (Name == NAME) { \
158 CGPM.addPass(CREATE_PASS); \
161 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
162 if (Name == "require<" NAME ">") { \
163 CGPM.addPass(NoopAnalysisRequirementPass<decltype(CREATE_PASS)>()); \
166 if (Name == "invalidate<" NAME ">") { \
167 CGPM.addPass(NoopAnalysisInvalidationPass<decltype(CREATE_PASS)>()); \
170 #include "PassRegistry.def"
175 static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
176 #define FUNCTION_PASS(NAME, CREATE_PASS) \
177 if (Name == NAME) { \
178 FPM.addPass(CREATE_PASS); \
181 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
182 if (Name == "require<" NAME ">") { \
183 FPM.addPass(NoopAnalysisRequirementPass<decltype(CREATE_PASS)>()); \
186 if (Name == "invalidate<" NAME ">") { \
187 FPM.addPass(NoopAnalysisInvalidationPass<decltype(CREATE_PASS)>()); \
190 #include "PassRegistry.def"
195 static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
196 StringRef &PipelineText,
197 bool VerifyEachPass) {
199 // Parse nested pass managers by recursing.
200 if (PipelineText.startswith("function(")) {
201 FunctionPassManager NestedFPM;
203 // Parse the inner pipeline inte the nested manager.
204 PipelineText = PipelineText.substr(strlen("function("));
205 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
206 PipelineText.empty())
208 assert(PipelineText[0] == ')');
209 PipelineText = PipelineText.substr(1);
211 // Add the nested pass manager with the appropriate adaptor.
212 FPM.addPass(std::move(NestedFPM));
214 // Otherwise try to parse a pass name.
215 size_t End = PipelineText.find_first_of(",)");
216 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
219 FPM.addPass(VerifierPass());
221 PipelineText = PipelineText.substr(End);
224 if (PipelineText.empty() || PipelineText[0] == ')')
227 assert(PipelineText[0] == ',');
228 PipelineText = PipelineText.substr(1);
232 static bool parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
233 StringRef &PipelineText,
234 bool VerifyEachPass) {
236 // Parse nested pass managers by recursing.
237 if (PipelineText.startswith("cgscc(")) {
238 CGSCCPassManager NestedCGPM;
240 // Parse the inner pipeline into the nested manager.
241 PipelineText = PipelineText.substr(strlen("cgscc("));
242 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass) ||
243 PipelineText.empty())
245 assert(PipelineText[0] == ')');
246 PipelineText = PipelineText.substr(1);
248 // Add the nested pass manager with the appropriate adaptor.
249 CGPM.addPass(std::move(NestedCGPM));
250 } else if (PipelineText.startswith("function(")) {
251 FunctionPassManager NestedFPM;
253 // Parse the inner pipeline inte the nested manager.
254 PipelineText = PipelineText.substr(strlen("function("));
255 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
256 PipelineText.empty())
258 assert(PipelineText[0] == ')');
259 PipelineText = PipelineText.substr(1);
261 // Add the nested pass manager with the appropriate adaptor.
262 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
264 // Otherwise try to parse a pass name.
265 size_t End = PipelineText.find_first_of(",)");
266 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
268 // FIXME: No verifier support for CGSCC passes!
270 PipelineText = PipelineText.substr(End);
273 if (PipelineText.empty() || PipelineText[0] == ')')
276 assert(PipelineText[0] == ',');
277 PipelineText = PipelineText.substr(1);
281 static bool parseModulePassPipeline(ModulePassManager &MPM,
282 StringRef &PipelineText,
283 bool VerifyEachPass) {
285 // Parse nested pass managers by recursing.
286 if (PipelineText.startswith("module(")) {
287 ModulePassManager NestedMPM;
289 // Parse the inner pipeline into the nested manager.
290 PipelineText = PipelineText.substr(strlen("module("));
291 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass) ||
292 PipelineText.empty())
294 assert(PipelineText[0] == ')');
295 PipelineText = PipelineText.substr(1);
297 // Now add the nested manager as a module pass.
298 MPM.addPass(std::move(NestedMPM));
299 } else if (PipelineText.startswith("cgscc(")) {
300 CGSCCPassManager NestedCGPM;
302 // Parse the inner pipeline inte the nested manager.
303 PipelineText = PipelineText.substr(strlen("cgscc("));
304 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass) ||
305 PipelineText.empty())
307 assert(PipelineText[0] == ')');
308 PipelineText = PipelineText.substr(1);
310 // Add the nested pass manager with the appropriate adaptor.
312 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
313 } else if (PipelineText.startswith("function(")) {
314 FunctionPassManager NestedFPM;
316 // Parse the inner pipeline inte the nested manager.
317 PipelineText = PipelineText.substr(strlen("function("));
318 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
319 PipelineText.empty())
321 assert(PipelineText[0] == ')');
322 PipelineText = PipelineText.substr(1);
324 // Add the nested pass manager with the appropriate adaptor.
325 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
327 // Otherwise try to parse a pass name.
328 size_t End = PipelineText.find_first_of(",)");
329 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
332 MPM.addPass(VerifierPass());
334 PipelineText = PipelineText.substr(End);
337 if (PipelineText.empty() || PipelineText[0] == ')')
340 assert(PipelineText[0] == ',');
341 PipelineText = PipelineText.substr(1);
345 // Primary pass pipeline description parsing routine.
346 // FIXME: Should this routine accept a TargetMachine or require the caller to
347 // pre-populate the analysis managers with target-specific stuff?
348 bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText,
349 bool VerifyEachPass) {
350 // Look at the first entry to figure out which layer to start parsing at.
351 if (PipelineText.startswith("module("))
352 return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
353 PipelineText.empty();
354 if (PipelineText.startswith("cgscc(")) {
355 CGSCCPassManager CGPM;
356 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass) ||
357 !PipelineText.empty())
359 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
362 if (PipelineText.startswith("function(")) {
363 FunctionPassManager FPM;
364 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
365 !PipelineText.empty())
367 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
371 // This isn't a direct pass manager name, look for the end of a pass name.
372 StringRef FirstName =
373 PipelineText.substr(0, PipelineText.find_first_of(",)"));
374 if (isModulePassName(FirstName))
375 return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
376 PipelineText.empty();
378 if (isCGSCCPassName(FirstName)) {
379 CGSCCPassManager CGPM;
380 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass) ||
381 !PipelineText.empty())
383 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
387 if (isFunctionPassName(FirstName)) {
388 FunctionPassManager FPM;
389 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
390 !PipelineText.empty())
392 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));