]> git.proxmox.com Git - rustc.git/blame - src/llvm/tools/opt/Passes.cpp
Imported Upstream version 1.0.0+dfsg1
[rustc.git] / src / llvm / tools / opt / Passes.cpp
CommitLineData
1a4d82fc
JJ
1//===- 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"
18#include "llvm/Analysis/CGSCCPassManager.h"
19#include "llvm/Analysis/LazyCallGraph.h"
85aaf69f 20#include "llvm/IR/Dominators.h"
1a4d82fc
JJ
21#include "llvm/IR/IRPrintingPasses.h"
22#include "llvm/IR/PassManager.h"
23#include "llvm/IR/Verifier.h"
24#include "llvm/Support/Debug.h"
25
26using namespace llvm;
27
28namespace {
29
30/// \brief No-op module pass which does nothing.
31struct NoOpModulePass {
85aaf69f 32 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
1a4d82fc
JJ
33 static StringRef name() { return "NoOpModulePass"; }
34};
35
85aaf69f
SL
36/// \brief No-op module analysis.
37struct NoOpModuleAnalysis {
38 struct Result {};
39 Result run(Module &) { return Result(); }
40 static StringRef name() { return "NoOpModuleAnalysis"; }
41 static void *ID() { return (void *)&PassID; }
42private:
43 static char PassID;
44};
45
46char NoOpModuleAnalysis::PassID;
47
1a4d82fc
JJ
48/// \brief No-op CGSCC pass which does nothing.
49struct NoOpCGSCCPass {
85aaf69f 50 PreservedAnalyses run(LazyCallGraph::SCC &C) {
1a4d82fc
JJ
51 return PreservedAnalyses::all();
52 }
53 static StringRef name() { return "NoOpCGSCCPass"; }
54};
55
85aaf69f
SL
56/// \brief No-op CGSCC analysis.
57struct NoOpCGSCCAnalysis {
58 struct Result {};
59 Result run(LazyCallGraph::SCC &) { return Result(); }
60 static StringRef name() { return "NoOpCGSCCAnalysis"; }
61 static void *ID() { return (void *)&PassID; }
62private:
63 static char PassID;
64};
65
66char NoOpCGSCCAnalysis::PassID;
67
1a4d82fc
JJ
68/// \brief No-op function pass which does nothing.
69struct NoOpFunctionPass {
85aaf69f 70 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
1a4d82fc
JJ
71 static StringRef name() { return "NoOpFunctionPass"; }
72};
73
85aaf69f
SL
74/// \brief No-op function analysis.
75struct NoOpFunctionAnalysis {
76 struct Result {};
77 Result run(Function &) { return Result(); }
78 static StringRef name() { return "NoOpFunctionAnalysis"; }
79 static void *ID() { return (void *)&PassID; }
80private:
81 static char PassID;
82};
83
84char NoOpFunctionAnalysis::PassID;
85
1a4d82fc
JJ
86} // End anonymous namespace.
87
85aaf69f
SL
88void llvm::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
89#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
90 MAM.registerPass(CREATE_PASS);
91#include "PassRegistry.def"
92}
1a4d82fc 93
85aaf69f
SL
94void llvm::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
95#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
96 CGAM.registerPass(CREATE_PASS);
97#include "PassRegistry.def"
98}
99
100void llvm::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
101#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
102 FAM.registerPass(CREATE_PASS);
103#include "PassRegistry.def"
104}
105
106#ifndef NDEBUG
107static bool isModulePassName(StringRef Name) {
1a4d82fc 108#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
85aaf69f
SL
109#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
110 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
111 return true;
1a4d82fc
JJ
112#include "PassRegistry.def"
113
114 return false;
115}
85aaf69f 116#endif
1a4d82fc
JJ
117
118static bool isCGSCCPassName(StringRef Name) {
1a4d82fc 119#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
85aaf69f
SL
120#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
121 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
122 return true;
1a4d82fc
JJ
123#include "PassRegistry.def"
124
125 return false;
126}
127
128static bool isFunctionPassName(StringRef Name) {
1a4d82fc 129#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
85aaf69f
SL
130#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
131 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
132 return true;
1a4d82fc
JJ
133#include "PassRegistry.def"
134
135 return false;
136}
137
138static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
1a4d82fc
JJ
139#define MODULE_PASS(NAME, CREATE_PASS) \
140 if (Name == NAME) { \
141 MPM.addPass(CREATE_PASS); \
142 return true; \
143 }
85aaf69f
SL
144#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
145 if (Name == "require<" NAME ">") { \
146 MPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
147 return true; \
148 } \
149 if (Name == "invalidate<" NAME ">") { \
150 MPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
151 return true; \
152 }
1a4d82fc
JJ
153#include "PassRegistry.def"
154
155 return false;
156}
157
158static bool parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
1a4d82fc
JJ
159#define CGSCC_PASS(NAME, CREATE_PASS) \
160 if (Name == NAME) { \
161 CGPM.addPass(CREATE_PASS); \
162 return true; \
163 }
85aaf69f
SL
164#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
165 if (Name == "require<" NAME ">") { \
166 CGPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
167 return true; \
168 } \
169 if (Name == "invalidate<" NAME ">") { \
170 CGPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
171 return true; \
172 }
1a4d82fc
JJ
173#include "PassRegistry.def"
174
175 return false;
176}
177
178static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
1a4d82fc
JJ
179#define FUNCTION_PASS(NAME, CREATE_PASS) \
180 if (Name == NAME) { \
181 FPM.addPass(CREATE_PASS); \
182 return true; \
183 }
85aaf69f
SL
184#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
185 if (Name == "require<" NAME ">") { \
186 FPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
187 return true; \
188 } \
189 if (Name == "invalidate<" NAME ">") { \
190 FPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
191 return true; \
192 }
1a4d82fc
JJ
193#include "PassRegistry.def"
194
195 return false;
196}
197
198static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
199 StringRef &PipelineText,
85aaf69f 200 bool VerifyEachPass, bool DebugLogging) {
1a4d82fc
JJ
201 for (;;) {
202 // Parse nested pass managers by recursing.
203 if (PipelineText.startswith("function(")) {
85aaf69f 204 FunctionPassManager NestedFPM(DebugLogging);
1a4d82fc
JJ
205
206 // Parse the inner pipeline inte the nested manager.
207 PipelineText = PipelineText.substr(strlen("function("));
85aaf69f
SL
208 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
209 DebugLogging) ||
1a4d82fc
JJ
210 PipelineText.empty())
211 return false;
212 assert(PipelineText[0] == ')');
213 PipelineText = PipelineText.substr(1);
214
215 // Add the nested pass manager with the appropriate adaptor.
216 FPM.addPass(std::move(NestedFPM));
217 } else {
218 // Otherwise try to parse a pass name.
219 size_t End = PipelineText.find_first_of(",)");
220 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
221 return false;
222 if (VerifyEachPass)
223 FPM.addPass(VerifierPass());
224
225 PipelineText = PipelineText.substr(End);
226 }
227
228 if (PipelineText.empty() || PipelineText[0] == ')')
229 return true;
230
231 assert(PipelineText[0] == ',');
232 PipelineText = PipelineText.substr(1);
233 }
234}
235
236static bool parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
85aaf69f
SL
237 StringRef &PipelineText, bool VerifyEachPass,
238 bool DebugLogging) {
1a4d82fc
JJ
239 for (;;) {
240 // Parse nested pass managers by recursing.
241 if (PipelineText.startswith("cgscc(")) {
85aaf69f 242 CGSCCPassManager NestedCGPM(DebugLogging);
1a4d82fc
JJ
243
244 // Parse the inner pipeline into the nested manager.
245 PipelineText = PipelineText.substr(strlen("cgscc("));
85aaf69f
SL
246 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
247 DebugLogging) ||
1a4d82fc
JJ
248 PipelineText.empty())
249 return false;
250 assert(PipelineText[0] == ')');
251 PipelineText = PipelineText.substr(1);
252
253 // Add the nested pass manager with the appropriate adaptor.
254 CGPM.addPass(std::move(NestedCGPM));
255 } else if (PipelineText.startswith("function(")) {
85aaf69f 256 FunctionPassManager NestedFPM(DebugLogging);
1a4d82fc
JJ
257
258 // Parse the inner pipeline inte the nested manager.
259 PipelineText = PipelineText.substr(strlen("function("));
85aaf69f
SL
260 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
261 DebugLogging) ||
1a4d82fc
JJ
262 PipelineText.empty())
263 return false;
264 assert(PipelineText[0] == ')');
265 PipelineText = PipelineText.substr(1);
266
267 // Add the nested pass manager with the appropriate adaptor.
268 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
269 } else {
270 // Otherwise try to parse a pass name.
271 size_t End = PipelineText.find_first_of(",)");
272 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
273 return false;
274 // FIXME: No verifier support for CGSCC passes!
275
276 PipelineText = PipelineText.substr(End);
277 }
278
279 if (PipelineText.empty() || PipelineText[0] == ')')
280 return true;
281
282 assert(PipelineText[0] == ',');
283 PipelineText = PipelineText.substr(1);
284 }
285}
286
287static bool parseModulePassPipeline(ModulePassManager &MPM,
288 StringRef &PipelineText,
85aaf69f 289 bool VerifyEachPass, bool DebugLogging) {
1a4d82fc
JJ
290 for (;;) {
291 // Parse nested pass managers by recursing.
292 if (PipelineText.startswith("module(")) {
85aaf69f 293 ModulePassManager NestedMPM(DebugLogging);
1a4d82fc
JJ
294
295 // Parse the inner pipeline into the nested manager.
296 PipelineText = PipelineText.substr(strlen("module("));
85aaf69f
SL
297 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
298 DebugLogging) ||
1a4d82fc
JJ
299 PipelineText.empty())
300 return false;
301 assert(PipelineText[0] == ')');
302 PipelineText = PipelineText.substr(1);
303
304 // Now add the nested manager as a module pass.
305 MPM.addPass(std::move(NestedMPM));
306 } else if (PipelineText.startswith("cgscc(")) {
85aaf69f 307 CGSCCPassManager NestedCGPM(DebugLogging);
1a4d82fc
JJ
308
309 // Parse the inner pipeline inte the nested manager.
310 PipelineText = PipelineText.substr(strlen("cgscc("));
85aaf69f
SL
311 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
312 DebugLogging) ||
1a4d82fc
JJ
313 PipelineText.empty())
314 return false;
315 assert(PipelineText[0] == ')');
316 PipelineText = PipelineText.substr(1);
317
318 // Add the nested pass manager with the appropriate adaptor.
319 MPM.addPass(
320 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
321 } else if (PipelineText.startswith("function(")) {
85aaf69f 322 FunctionPassManager NestedFPM(DebugLogging);
1a4d82fc
JJ
323
324 // Parse the inner pipeline inte the nested manager.
325 PipelineText = PipelineText.substr(strlen("function("));
85aaf69f
SL
326 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
327 DebugLogging) ||
1a4d82fc
JJ
328 PipelineText.empty())
329 return false;
330 assert(PipelineText[0] == ')');
331 PipelineText = PipelineText.substr(1);
332
333 // Add the nested pass manager with the appropriate adaptor.
334 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
335 } else {
336 // Otherwise try to parse a pass name.
337 size_t End = PipelineText.find_first_of(",)");
338 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
339 return false;
340 if (VerifyEachPass)
341 MPM.addPass(VerifierPass());
342
343 PipelineText = PipelineText.substr(End);
344 }
345
346 if (PipelineText.empty() || PipelineText[0] == ')')
347 return true;
348
349 assert(PipelineText[0] == ',');
350 PipelineText = PipelineText.substr(1);
351 }
352}
353
354// Primary pass pipeline description parsing routine.
355// FIXME: Should this routine accept a TargetMachine or require the caller to
356// pre-populate the analysis managers with target-specific stuff?
357bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText,
85aaf69f
SL
358 bool VerifyEachPass, bool DebugLogging) {
359 // By default, try to parse the pipeline as-if it were within an implicit
360 // 'module(...)' pass pipeline. If this will parse at all, it needs to
361 // consume the entire string.
362 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
363 return PipelineText.empty();
364
365 // This isn't parsable as a module pipeline, look for the end of a pass name
366 // and directly drop down to that layer.
1a4d82fc
JJ
367 StringRef FirstName =
368 PipelineText.substr(0, PipelineText.find_first_of(",)"));
85aaf69f
SL
369 assert(!isModulePassName(FirstName) &&
370 "Already handled all module pipeline options.");
1a4d82fc 371
85aaf69f
SL
372 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
373 // pipeline.
1a4d82fc 374 if (isCGSCCPassName(FirstName)) {
85aaf69f
SL
375 CGSCCPassManager CGPM(DebugLogging);
376 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
377 DebugLogging) ||
1a4d82fc
JJ
378 !PipelineText.empty())
379 return false;
380 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
381 return true;
382 }
383
85aaf69f
SL
384 // Similarly, if this looks like a Function pass, parse the whole thing as
385 // a Function pipelien.
1a4d82fc 386 if (isFunctionPassName(FirstName)) {
85aaf69f
SL
387 FunctionPassManager FPM(DebugLogging);
388 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
389 DebugLogging) ||
1a4d82fc
JJ
390 !PipelineText.empty())
391 return false;
392 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
393 return true;
394 }
395
396 return false;
397}