blob: a171f42691a873ca1ff9cb2a89eca2b40caef116 [file] [log] [blame]
Chandler Carruth66445382014-01-11 08:16:35 +00001//===- Passes.cpp - Parsing, selection, and running of passes -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10///
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.
14///
15//===----------------------------------------------------------------------===//
16
17#include "Passes.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000018#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000019#include "llvm/Analysis/LazyCallGraph.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000020#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000021#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000022#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000023#include "llvm/Support/Debug.h"
Chandler Carruth66445382014-01-11 08:16:35 +000024
25using namespace llvm;
26
27namespace {
28
Chandler Carruthd8330982014-01-12 09:34:22 +000029/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000030struct NoOpModulePass {
31 PreservedAnalyses run(Module *M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000032 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000033};
34
Chandler Carruth572e3402014-04-21 11:12:00 +000035/// \brief No-op CGSCC pass which does nothing.
36struct NoOpCGSCCPass {
37 PreservedAnalyses run(LazyCallGraph::SCC *C) {
38 return PreservedAnalyses::all();
39 }
40 static StringRef name() { return "NoOpCGSCCPass"; }
41};
42
Chandler Carruthd8330982014-01-12 09:34:22 +000043/// \brief No-op function pass which does nothing.
44struct NoOpFunctionPass {
45 PreservedAnalyses run(Function *F) { return PreservedAnalyses::all(); }
46 static StringRef name() { return "NoOpFunctionPass"; }
47};
48
Chandler Carruth66445382014-01-11 08:16:35 +000049} // End anonymous namespace.
50
Chandler Carruth66445382014-01-11 08:16:35 +000051static bool isModulePassName(StringRef Name) {
52 if (Name == "no-op-module") return true;
Chandler Carruth58944182014-04-21 08:08:50 +000053
54#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
55#include "PassRegistry.def"
Chandler Carruth66445382014-01-11 08:16:35 +000056
57 return false;
58}
59
Chandler Carruth572e3402014-04-21 11:12:00 +000060static bool isCGSCCPassName(StringRef Name) {
61 if (Name == "no-op-cgscc") return true;
62
63#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
64#include "PassRegistry.def"
65
66 return false;
67}
68
Chandler Carruthd8330982014-01-12 09:34:22 +000069static bool isFunctionPassName(StringRef Name) {
70 if (Name == "no-op-function") return true;
Chandler Carruth58944182014-04-21 08:08:50 +000071
72#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
73#include "PassRegistry.def"
Chandler Carruthd8330982014-01-12 09:34:22 +000074
75 return false;
76}
77
Chandler Carruth66445382014-01-11 08:16:35 +000078static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
Chandler Carruth66445382014-01-11 08:16:35 +000079 if (Name == "no-op-module") {
80 MPM.addPass(NoOpModulePass());
81 return true;
82 }
Chandler Carruth58944182014-04-21 08:08:50 +000083
84#define MODULE_PASS(NAME, CREATE_PASS) \
85 if (Name == NAME) { \
86 MPM.addPass(CREATE_PASS); \
87 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +000088 }
Chandler Carruth58944182014-04-21 08:08:50 +000089#include "PassRegistry.def"
90
Chandler Carruth66445382014-01-11 08:16:35 +000091 return false;
92}
93
Chandler Carruth572e3402014-04-21 11:12:00 +000094static bool parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
95 if (Name == "no-op-cgscc") {
96 CGPM.addPass(NoOpCGSCCPass());
97 return true;
98 }
99
100#define CGSCC_PASS(NAME, CREATE_PASS) \
101 if (Name == NAME) { \
102 CGPM.addPass(CREATE_PASS); \
103 return true; \
104 }
105#include "PassRegistry.def"
106
107 return false;
108}
109
Chandler Carruthd8330982014-01-12 09:34:22 +0000110static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000111 if (Name == "no-op-function") {
112 FPM.addPass(NoOpFunctionPass());
113 return true;
114 }
Chandler Carruth58944182014-04-21 08:08:50 +0000115
116#define FUNCTION_PASS(NAME, CREATE_PASS) \
117 if (Name == NAME) { \
118 FPM.addPass(CREATE_PASS); \
119 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000120 }
Chandler Carruth58944182014-04-21 08:08:50 +0000121#include "PassRegistry.def"
122
Chandler Carruthd8330982014-01-12 09:34:22 +0000123 return false;
124}
125
126static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth4d356312014-01-20 11:34:08 +0000127 StringRef &PipelineText,
128 bool VerifyEachPass) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000129 for (;;) {
130 // Parse nested pass managers by recursing.
131 if (PipelineText.startswith("function(")) {
132 FunctionPassManager NestedFPM;
133
134 // Parse the inner pipeline inte the nested manager.
135 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth4d356312014-01-20 11:34:08 +0000136 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000137 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000138 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000139 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000140 PipelineText = PipelineText.substr(1);
141
142 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000143 FPM.addPass(std::move(NestedFPM));
Chandler Carruthd8330982014-01-12 09:34:22 +0000144 } else {
145 // Otherwise try to parse a pass name.
146 size_t End = PipelineText.find_first_of(",)");
147 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
148 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000149 if (VerifyEachPass)
150 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000151
152 PipelineText = PipelineText.substr(End);
153 }
154
155 if (PipelineText.empty() || PipelineText[0] == ')')
156 return true;
157
158 assert(PipelineText[0] == ',');
159 PipelineText = PipelineText.substr(1);
160 }
161}
162
Chandler Carruth572e3402014-04-21 11:12:00 +0000163static bool parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
164 StringRef &PipelineText,
165 bool VerifyEachPass) {
166 for (;;) {
167 // Parse nested pass managers by recursing.
168 if (PipelineText.startswith("cgscc(")) {
169 CGSCCPassManager NestedCGPM;
170
171 // Parse the inner pipeline into the nested manager.
172 PipelineText = PipelineText.substr(strlen("cgscc("));
173 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass) ||
174 PipelineText.empty())
175 return false;
176 assert(PipelineText[0] == ')');
177 PipelineText = PipelineText.substr(1);
178
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;
183
184 // Parse the inner pipeline inte the nested manager.
185 PipelineText = PipelineText.substr(strlen("function("));
186 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
187 PipelineText.empty())
188 return false;
189 assert(PipelineText[0] == ')');
190 PipelineText = PipelineText.substr(1);
191
192 // Add the nested pass manager with the appropriate adaptor.
193 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
194 } else {
195 // Otherwise try to parse a pass name.
196 size_t End = PipelineText.find_first_of(",)");
197 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
198 return false;
199 // FIXME: No verifier support for CGSCC passes!
200
201 PipelineText = PipelineText.substr(End);
202 }
203
204 if (PipelineText.empty() || PipelineText[0] == ')')
205 return true;
206
207 assert(PipelineText[0] == ',');
208 PipelineText = PipelineText.substr(1);
209 }
210}
211
Chandler Carruth66445382014-01-11 08:16:35 +0000212static bool parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth4d356312014-01-20 11:34:08 +0000213 StringRef &PipelineText,
214 bool VerifyEachPass) {
Chandler Carruth66445382014-01-11 08:16:35 +0000215 for (;;) {
216 // Parse nested pass managers by recursing.
217 if (PipelineText.startswith("module(")) {
Chandler Carruth258dbb32014-01-11 12:06:47 +0000218 ModulePassManager NestedMPM;
219
220 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000221 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth4d356312014-01-20 11:34:08 +0000222 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000223 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000224 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000225 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000226 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000227
228 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000229 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000230 } else if (PipelineText.startswith("cgscc(")) {
231 CGSCCPassManager NestedCGPM;
232
233 // Parse the inner pipeline inte the nested manager.
234 PipelineText = PipelineText.substr(strlen("cgscc("));
235 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass) ||
236 PipelineText.empty())
237 return false;
238 assert(PipelineText[0] == ')');
239 PipelineText = PipelineText.substr(1);
240
241 // Add the nested pass manager with the appropriate adaptor.
242 MPM.addPass(
243 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000244 } else if (PipelineText.startswith("function(")) {
245 FunctionPassManager NestedFPM;
246
247 // Parse the inner pipeline inte the nested manager.
248 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth4d356312014-01-20 11:34:08 +0000249 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000250 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000251 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000252 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000253 PipelineText = PipelineText.substr(1);
254
255 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000256 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000257 } else {
258 // Otherwise try to parse a pass name.
259 size_t End = PipelineText.find_first_of(",)");
260 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
261 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000262 if (VerifyEachPass)
263 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000264
265 PipelineText = PipelineText.substr(End);
266 }
267
268 if (PipelineText.empty() || PipelineText[0] == ')')
269 return true;
270
271 assert(PipelineText[0] == ',');
272 PipelineText = PipelineText.substr(1);
273 }
274}
275
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?
Chandler Carruth4d356312014-01-20 11:34:08 +0000279bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText,
280 bool VerifyEachPass) {
Chandler Carruth66445382014-01-11 08:16:35 +0000281 // Look at the first entry to figure out which layer to start parsing at.
282 if (PipelineText.startswith("module("))
Chandler Carruth4d356312014-01-20 11:34:08 +0000283 return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
284 PipelineText.empty();
Chandler Carruth572e3402014-04-21 11:12:00 +0000285 if (PipelineText.startswith("cgscc(")) {
286 CGSCCPassManager CGPM;
287 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass) ||
288 !PipelineText.empty())
289 return false;
290 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
291 return true;
292 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000293 if (PipelineText.startswith("function(")) {
294 FunctionPassManager FPM;
Chandler Carruth4d356312014-01-20 11:34:08 +0000295 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
296 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000297 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000298 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000299 return true;
300 }
Chandler Carruth66445382014-01-11 08:16:35 +0000301
302 // This isn't a direct pass manager name, look for the end of a pass name.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000303 StringRef FirstName =
304 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruth66445382014-01-11 08:16:35 +0000305 if (isModulePassName(FirstName))
Chandler Carruth4d356312014-01-20 11:34:08 +0000306 return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
307 PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000308
Chandler Carruth572e3402014-04-21 11:12:00 +0000309 if (isCGSCCPassName(FirstName)) {
310 CGSCCPassManager CGPM;
311 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass) ||
312 !PipelineText.empty())
313 return false;
314 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
315 return true;
316 }
317
Chandler Carruthd8330982014-01-12 09:34:22 +0000318 if (isFunctionPassName(FirstName)) {
319 FunctionPassManager FPM;
Chandler Carruth4d356312014-01-20 11:34:08 +0000320 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
321 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000322 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000323 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000324 return true;
325 }
Chandler Carruth66445382014-01-11 08:16:35 +0000326
327 return false;
328}