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/Analysis/LoopInfo.h"
21 #include "llvm/Analysis/TargetLibraryInfo.h"
22 #include "llvm/IR/Dominators.h"
23 #include "llvm/IR/IRPrintingPasses.h"
24 #include "llvm/IR/PassManager.h"
25 #include "llvm/IR/Verifier.h"
26 #include "llvm/Support/Debug.h"
32 /// \brief No-op module pass which does nothing.
33 struct NoOpModulePass {
34 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
35 static StringRef name() { return "NoOpModulePass"; }
38 /// \brief No-op module analysis.
39 struct NoOpModuleAnalysis {
41 Result run(Module &) { return Result(); }
42 static StringRef name() { return "NoOpModuleAnalysis"; }
43 static void *ID() { return (void *)&PassID; }
48 char NoOpModuleAnalysis::PassID;
50 /// \brief No-op CGSCC pass which does nothing.
51 struct NoOpCGSCCPass {
52 PreservedAnalyses run(LazyCallGraph::SCC &C) {
53 return PreservedAnalyses::all();
55 static StringRef name() { return "NoOpCGSCCPass"; }
58 /// \brief No-op CGSCC analysis.
59 struct NoOpCGSCCAnalysis {
61 Result run(LazyCallGraph::SCC &) { return Result(); }
62 static StringRef name() { return "NoOpCGSCCAnalysis"; }
63 static void *ID() { return (void *)&PassID; }
68 char NoOpCGSCCAnalysis::PassID;
70 /// \brief No-op function pass which does nothing.
71 struct NoOpFunctionPass {
72 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
73 static StringRef name() { return "NoOpFunctionPass"; }
76 /// \brief No-op function analysis.
77 struct NoOpFunctionAnalysis {
79 Result run(Function &) { return Result(); }
80 static StringRef name() { return "NoOpFunctionAnalysis"; }
81 static void *ID() { return (void *)&PassID; }
86 char NoOpFunctionAnalysis::PassID;
88 } // End anonymous namespace.
90 void llvm::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
91 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
92 MAM.registerPass(CREATE_PASS);
93 #include "PassRegistry.def"
96 void llvm::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
97 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
98 CGAM.registerPass(CREATE_PASS);
99 #include "PassRegistry.def"
102 void llvm::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
103 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
104 FAM.registerPass(CREATE_PASS);
105 #include "PassRegistry.def"
109 static bool isModulePassName(StringRef Name) {
110 #define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
111 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
112 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
114 #include "PassRegistry.def"
120 static bool isCGSCCPassName(StringRef Name) {
121 #define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
122 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
123 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
125 #include "PassRegistry.def"
130 static bool isFunctionPassName(StringRef Name) {
131 #define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
132 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
133 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
135 #include "PassRegistry.def"
140 static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
141 #define MODULE_PASS(NAME, CREATE_PASS) \
142 if (Name == NAME) { \
143 MPM.addPass(CREATE_PASS); \
146 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
147 if (Name == "require<" NAME ">") { \
148 MPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
151 if (Name == "invalidate<" NAME ">") { \
152 MPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
155 #include "PassRegistry.def"
160 static bool parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
161 #define CGSCC_PASS(NAME, CREATE_PASS) \
162 if (Name == NAME) { \
163 CGPM.addPass(CREATE_PASS); \
166 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
167 if (Name == "require<" NAME ">") { \
168 CGPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
171 if (Name == "invalidate<" NAME ">") { \
172 CGPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
175 #include "PassRegistry.def"
180 static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
181 #define FUNCTION_PASS(NAME, CREATE_PASS) \
182 if (Name == NAME) { \
183 FPM.addPass(CREATE_PASS); \
186 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
187 if (Name == "require<" NAME ">") { \
188 FPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
191 if (Name == "invalidate<" NAME ">") { \
192 FPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
195 #include "PassRegistry.def"
200 static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
201 StringRef &PipelineText,
202 bool VerifyEachPass, bool DebugLogging) {
204 // Parse nested pass managers by recursing.
205 if (PipelineText.startswith("function(")) {
206 FunctionPassManager NestedFPM(DebugLogging);
208 // Parse the inner pipeline inte the nested manager.
209 PipelineText = PipelineText.substr(strlen("function("));
210 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, bool VerifyEachPass,
242 // Parse nested pass managers by recursing.
243 if (PipelineText.startswith("cgscc(")) {
244 CGSCCPassManager NestedCGPM(DebugLogging);
246 // Parse the inner pipeline into the nested manager.
247 PipelineText = PipelineText.substr(strlen("cgscc("));
248 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
250 PipelineText.empty())
252 assert(PipelineText[0] == ')');
253 PipelineText = PipelineText.substr(1);
255 // Add the nested pass manager with the appropriate adaptor.
256 CGPM.addPass(std::move(NestedCGPM));
257 } else if (PipelineText.startswith("function(")) {
258 FunctionPassManager NestedFPM(DebugLogging);
260 // Parse the inner pipeline inte the nested manager.
261 PipelineText = PipelineText.substr(strlen("function("));
262 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
264 PipelineText.empty())
266 assert(PipelineText[0] == ')');
267 PipelineText = PipelineText.substr(1);
269 // Add the nested pass manager with the appropriate adaptor.
270 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
272 // Otherwise try to parse a pass name.
273 size_t End = PipelineText.find_first_of(",)");
274 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
276 // FIXME: No verifier support for CGSCC passes!
278 PipelineText = PipelineText.substr(End);
281 if (PipelineText.empty() || PipelineText[0] == ')')
284 assert(PipelineText[0] == ',');
285 PipelineText = PipelineText.substr(1);
289 static bool parseModulePassPipeline(ModulePassManager &MPM,
290 StringRef &PipelineText,
291 bool VerifyEachPass, bool DebugLogging) {
293 // Parse nested pass managers by recursing.
294 if (PipelineText.startswith("module(")) {
295 ModulePassManager NestedMPM(DebugLogging);
297 // Parse the inner pipeline into the nested manager.
298 PipelineText = PipelineText.substr(strlen("module("));
299 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
301 PipelineText.empty())
303 assert(PipelineText[0] == ')');
304 PipelineText = PipelineText.substr(1);
306 // Now add the nested manager as a module pass.
307 MPM.addPass(std::move(NestedMPM));
308 } else if (PipelineText.startswith("cgscc(")) {
309 CGSCCPassManager NestedCGPM(DebugLogging);
311 // Parse the inner pipeline inte the nested manager.
312 PipelineText = PipelineText.substr(strlen("cgscc("));
313 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
315 PipelineText.empty())
317 assert(PipelineText[0] == ')');
318 PipelineText = PipelineText.substr(1);
320 // Add the nested pass manager with the appropriate adaptor.
322 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
323 } else if (PipelineText.startswith("function(")) {
324 FunctionPassManager NestedFPM(DebugLogging);
326 // Parse the inner pipeline inte the nested manager.
327 PipelineText = PipelineText.substr(strlen("function("));
328 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
330 PipelineText.empty())
332 assert(PipelineText[0] == ')');
333 PipelineText = PipelineText.substr(1);
335 // Add the nested pass manager with the appropriate adaptor.
336 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
338 // Otherwise try to parse a pass name.
339 size_t End = PipelineText.find_first_of(",)");
340 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
343 MPM.addPass(VerifierPass());
345 PipelineText = PipelineText.substr(End);
348 if (PipelineText.empty() || PipelineText[0] == ')')
351 assert(PipelineText[0] == ',');
352 PipelineText = PipelineText.substr(1);
356 // Primary pass pipeline description parsing routine.
357 // FIXME: Should this routine accept a TargetMachine or require the caller to
358 // pre-populate the analysis managers with target-specific stuff?
359 bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText,
360 bool VerifyEachPass, bool DebugLogging) {
361 // By default, try to parse the pipeline as-if it were within an implicit
362 // 'module(...)' pass pipeline. If this will parse at all, it needs to
363 // consume the entire string.
364 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
365 return PipelineText.empty();
367 // This isn't parsable as a module pipeline, look for the end of a pass name
368 // and directly drop down to that layer.
369 StringRef FirstName =
370 PipelineText.substr(0, PipelineText.find_first_of(",)"));
371 assert(!isModulePassName(FirstName) &&
372 "Already handled all module pipeline options.");
374 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
376 if (isCGSCCPassName(FirstName)) {
377 CGSCCPassManager CGPM(DebugLogging);
378 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
380 !PipelineText.empty())
382 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
386 // Similarly, if this looks like a Function pass, parse the whole thing as
387 // a Function pipelien.
388 if (isFunctionPassName(FirstName)) {
389 FunctionPassManager FPM(DebugLogging);
390 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
392 !PipelineText.empty())
394 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));