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 CGSCC pass which does nothing.
36 struct NoOpCGSCCPass {
37 PreservedAnalyses run(LazyCallGraph::SCC *C) {
38 return PreservedAnalyses::all();
40 static StringRef name() { return "NoOpCGSCCPass"; }
43 /// \brief No-op function pass which does nothing.
44 struct NoOpFunctionPass {
45 PreservedAnalyses run(Function *F) { return PreservedAnalyses::all(); }
46 static StringRef name() { return "NoOpFunctionPass"; }
49 } // End anonymous namespace.
51 static bool isModulePassName(StringRef Name) {
52 if (Name == "no-op-module") return true;
54 #define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
55 #include "PassRegistry.def"
60 static bool isCGSCCPassName(StringRef Name) {
61 if (Name == "no-op-cgscc") return true;
63 #define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
64 #include "PassRegistry.def"
69 static bool isFunctionPassName(StringRef Name) {
70 if (Name == "no-op-function") return true;
72 #define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
73 #include "PassRegistry.def"
78 static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
79 if (Name == "no-op-module") {
80 MPM.addPass(NoOpModulePass());
84 #define MODULE_PASS(NAME, CREATE_PASS) \
86 MPM.addPass(CREATE_PASS); \
89 #include "PassRegistry.def"
94 static bool parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
95 if (Name == "no-op-cgscc") {
96 CGPM.addPass(NoOpCGSCCPass());
100 #define CGSCC_PASS(NAME, CREATE_PASS) \
101 if (Name == NAME) { \
102 CGPM.addPass(CREATE_PASS); \
105 #include "PassRegistry.def"
110 static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
111 if (Name == "no-op-function") {
112 FPM.addPass(NoOpFunctionPass());
116 #define FUNCTION_PASS(NAME, CREATE_PASS) \
117 if (Name == NAME) { \
118 FPM.addPass(CREATE_PASS); \
121 #include "PassRegistry.def"
126 static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
127 StringRef &PipelineText,
128 bool VerifyEachPass) {
130 // Parse nested pass managers by recursing.
131 if (PipelineText.startswith("function(")) {
132 FunctionPassManager NestedFPM;
134 // Parse the inner pipeline inte the nested manager.
135 PipelineText = PipelineText.substr(strlen("function("));
136 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
137 PipelineText.empty())
139 assert(PipelineText[0] == ')');
140 PipelineText = PipelineText.substr(1);
142 // Add the nested pass manager with the appropriate adaptor.
143 FPM.addPass(std::move(NestedFPM));
145 // Otherwise try to parse a pass name.
146 size_t End = PipelineText.find_first_of(",)");
147 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
150 FPM.addPass(VerifierPass());
152 PipelineText = PipelineText.substr(End);
155 if (PipelineText.empty() || PipelineText[0] == ')')
158 assert(PipelineText[0] == ',');
159 PipelineText = PipelineText.substr(1);
163 static bool parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
164 StringRef &PipelineText,
165 bool VerifyEachPass) {
167 // Parse nested pass managers by recursing.
168 if (PipelineText.startswith("cgscc(")) {
169 CGSCCPassManager NestedCGPM;
171 // Parse the inner pipeline into the nested manager.
172 PipelineText = PipelineText.substr(strlen("cgscc("));
173 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass) ||
174 PipelineText.empty())
176 assert(PipelineText[0] == ')');
177 PipelineText = PipelineText.substr(1);
179 // Add the nested pass manager with the appropriate adaptor.
180 CGPM.addPass(std::move(NestedCGPM));
181 } else if (PipelineText.startswith("function(")) {
182 FunctionPassManager NestedFPM;
184 // Parse the inner pipeline inte the nested manager.
185 PipelineText = PipelineText.substr(strlen("function("));
186 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
187 PipelineText.empty())
189 assert(PipelineText[0] == ')');
190 PipelineText = PipelineText.substr(1);
192 // Add the nested pass manager with the appropriate adaptor.
193 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
195 // Otherwise try to parse a pass name.
196 size_t End = PipelineText.find_first_of(",)");
197 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
199 // FIXME: No verifier support for CGSCC passes!
201 PipelineText = PipelineText.substr(End);
204 if (PipelineText.empty() || PipelineText[0] == ')')
207 assert(PipelineText[0] == ',');
208 PipelineText = PipelineText.substr(1);
212 static bool parseModulePassPipeline(ModulePassManager &MPM,
213 StringRef &PipelineText,
214 bool VerifyEachPass) {
216 // Parse nested pass managers by recursing.
217 if (PipelineText.startswith("module(")) {
218 ModulePassManager NestedMPM;
220 // Parse the inner pipeline into the nested manager.
221 PipelineText = PipelineText.substr(strlen("module("));
222 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass) ||
223 PipelineText.empty())
225 assert(PipelineText[0] == ')');
226 PipelineText = PipelineText.substr(1);
228 // Now add the nested manager as a module pass.
229 MPM.addPass(std::move(NestedMPM));
230 } else if (PipelineText.startswith("cgscc(")) {
231 CGSCCPassManager NestedCGPM;
233 // Parse the inner pipeline inte the nested manager.
234 PipelineText = PipelineText.substr(strlen("cgscc("));
235 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass) ||
236 PipelineText.empty())
238 assert(PipelineText[0] == ')');
239 PipelineText = PipelineText.substr(1);
241 // Add the nested pass manager with the appropriate adaptor.
243 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
244 } else if (PipelineText.startswith("function(")) {
245 FunctionPassManager NestedFPM;
247 // Parse the inner pipeline inte the nested manager.
248 PipelineText = PipelineText.substr(strlen("function("));
249 if (!parseFunctionPassPipeline(NestedFPM, 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 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
258 // Otherwise try to parse a pass name.
259 size_t End = PipelineText.find_first_of(",)");
260 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
263 MPM.addPass(VerifierPass());
265 PipelineText = PipelineText.substr(End);
268 if (PipelineText.empty() || PipelineText[0] == ')')
271 assert(PipelineText[0] == ',');
272 PipelineText = PipelineText.substr(1);
276 // Primary pass pipeline description parsing routine.
277 // FIXME: Should this routine accept a TargetMachine or require the caller to
278 // pre-populate the analysis managers with target-specific stuff?
279 bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText,
280 bool VerifyEachPass) {
281 // Look at the first entry to figure out which layer to start parsing at.
282 if (PipelineText.startswith("module("))
283 return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
284 PipelineText.empty();
285 if (PipelineText.startswith("cgscc(")) {
286 CGSCCPassManager CGPM;
287 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass) ||
288 !PipelineText.empty())
290 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
293 if (PipelineText.startswith("function(")) {
294 FunctionPassManager FPM;
295 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
296 !PipelineText.empty())
298 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
302 // This isn't a direct pass manager name, look for the end of a pass name.
303 StringRef FirstName =
304 PipelineText.substr(0, PipelineText.find_first_of(",)"));
305 if (isModulePassName(FirstName))
306 return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
307 PipelineText.empty();
309 if (isCGSCCPassName(FirstName)) {
310 CGSCCPassManager CGPM;
311 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass) ||
312 !PipelineText.empty())
314 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
318 if (isFunctionPassName(FirstName)) {
319 FunctionPassManager FPM;
320 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
321 !PipelineText.empty())
323 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));