]>
git.proxmox.com Git - rustc.git/blob - src/llvm/tools/opt/Passes.cpp
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/Dominators.h"
21 #include "llvm/IR/IRPrintingPasses.h"
22 #include "llvm/IR/PassManager.h"
23 #include "llvm/IR/Verifier.h"
24 #include "llvm/Support/Debug.h"
30 /// \brief No-op module pass which does nothing.
31 struct NoOpModulePass
{
32 PreservedAnalyses
run(Module
&M
) { return PreservedAnalyses::all(); }
33 static StringRef
name() { return "NoOpModulePass"; }
36 /// \brief No-op module analysis.
37 struct NoOpModuleAnalysis
{
39 Result
run(Module
&) { return Result(); }
40 static StringRef
name() { return "NoOpModuleAnalysis"; }
41 static void *ID() { return (void *)&PassID
; }
46 char NoOpModuleAnalysis::PassID
;
48 /// \brief No-op CGSCC pass which does nothing.
49 struct NoOpCGSCCPass
{
50 PreservedAnalyses
run(LazyCallGraph::SCC
&C
) {
51 return PreservedAnalyses::all();
53 static StringRef
name() { return "NoOpCGSCCPass"; }
56 /// \brief No-op CGSCC analysis.
57 struct NoOpCGSCCAnalysis
{
59 Result
run(LazyCallGraph::SCC
&) { return Result(); }
60 static StringRef
name() { return "NoOpCGSCCAnalysis"; }
61 static void *ID() { return (void *)&PassID
; }
66 char NoOpCGSCCAnalysis::PassID
;
68 /// \brief No-op function pass which does nothing.
69 struct NoOpFunctionPass
{
70 PreservedAnalyses
run(Function
&F
) { return PreservedAnalyses::all(); }
71 static StringRef
name() { return "NoOpFunctionPass"; }
74 /// \brief No-op function analysis.
75 struct NoOpFunctionAnalysis
{
77 Result
run(Function
&) { return Result(); }
78 static StringRef
name() { return "NoOpFunctionAnalysis"; }
79 static void *ID() { return (void *)&PassID
; }
84 char NoOpFunctionAnalysis::PassID
;
86 } // End anonymous namespace.
88 void llvm::registerModuleAnalyses(ModuleAnalysisManager
&MAM
) {
89 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
90 MAM.registerPass(CREATE_PASS);
91 #include "PassRegistry.def"
94 void llvm::registerCGSCCAnalyses(CGSCCAnalysisManager
&CGAM
) {
95 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
96 CGAM.registerPass(CREATE_PASS);
97 #include "PassRegistry.def"
100 void llvm::registerFunctionAnalyses(FunctionAnalysisManager
&FAM
) {
101 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
102 FAM.registerPass(CREATE_PASS);
103 #include "PassRegistry.def"
107 static bool isModulePassName(StringRef Name
) {
108 #define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
109 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
110 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
112 #include "PassRegistry.def"
118 static bool isCGSCCPassName(StringRef Name
) {
119 #define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
120 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
121 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
123 #include "PassRegistry.def"
128 static bool isFunctionPassName(StringRef Name
) {
129 #define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
130 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
131 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
133 #include "PassRegistry.def"
138 static bool parseModulePassName(ModulePassManager
&MPM
, StringRef Name
) {
139 #define MODULE_PASS(NAME, CREATE_PASS) \
140 if (Name == NAME) { \
141 MPM.addPass(CREATE_PASS); \
144 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
145 if (Name == "require<" NAME ">") { \
146 MPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
149 if (Name == "invalidate<" NAME ">") { \
150 MPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
153 #include "PassRegistry.def"
158 static bool parseCGSCCPassName(CGSCCPassManager
&CGPM
, StringRef Name
) {
159 #define CGSCC_PASS(NAME, CREATE_PASS) \
160 if (Name == NAME) { \
161 CGPM.addPass(CREATE_PASS); \
164 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
165 if (Name == "require<" NAME ">") { \
166 CGPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
169 if (Name == "invalidate<" NAME ">") { \
170 CGPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
173 #include "PassRegistry.def"
178 static bool parseFunctionPassName(FunctionPassManager
&FPM
, StringRef Name
) {
179 #define FUNCTION_PASS(NAME, CREATE_PASS) \
180 if (Name == NAME) { \
181 FPM.addPass(CREATE_PASS); \
184 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
185 if (Name == "require<" NAME ">") { \
186 FPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
189 if (Name == "invalidate<" NAME ">") { \
190 FPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
193 #include "PassRegistry.def"
198 static bool parseFunctionPassPipeline(FunctionPassManager
&FPM
,
199 StringRef
&PipelineText
,
200 bool VerifyEachPass
, bool DebugLogging
) {
202 // Parse nested pass managers by recursing.
203 if (PipelineText
.startswith("function(")) {
204 FunctionPassManager
NestedFPM(DebugLogging
);
206 // Parse the inner pipeline inte the nested manager.
207 PipelineText
= PipelineText
.substr(strlen("function("));
208 if (!parseFunctionPassPipeline(NestedFPM
, PipelineText
, VerifyEachPass
,
210 PipelineText
.empty())
212 assert(PipelineText
[0] == ')');
213 PipelineText
= PipelineText
.substr(1);
215 // Add the nested pass manager with the appropriate adaptor.
216 FPM
.addPass(std::move(NestedFPM
));
218 // Otherwise try to parse a pass name.
219 size_t End
= PipelineText
.find_first_of(",)");
220 if (!parseFunctionPassName(FPM
, PipelineText
.substr(0, End
)))
223 FPM
.addPass(VerifierPass());
225 PipelineText
= PipelineText
.substr(End
);
228 if (PipelineText
.empty() || PipelineText
[0] == ')')
231 assert(PipelineText
[0] == ',');
232 PipelineText
= PipelineText
.substr(1);
236 static bool parseCGSCCPassPipeline(CGSCCPassManager
&CGPM
,
237 StringRef
&PipelineText
, bool VerifyEachPass
,
240 // Parse nested pass managers by recursing.
241 if (PipelineText
.startswith("cgscc(")) {
242 CGSCCPassManager
NestedCGPM(DebugLogging
);
244 // Parse the inner pipeline into the nested manager.
245 PipelineText
= PipelineText
.substr(strlen("cgscc("));
246 if (!parseCGSCCPassPipeline(NestedCGPM
, PipelineText
, VerifyEachPass
,
248 PipelineText
.empty())
250 assert(PipelineText
[0] == ')');
251 PipelineText
= PipelineText
.substr(1);
253 // Add the nested pass manager with the appropriate adaptor.
254 CGPM
.addPass(std::move(NestedCGPM
));
255 } else if (PipelineText
.startswith("function(")) {
256 FunctionPassManager
NestedFPM(DebugLogging
);
258 // Parse the inner pipeline inte the nested manager.
259 PipelineText
= PipelineText
.substr(strlen("function("));
260 if (!parseFunctionPassPipeline(NestedFPM
, PipelineText
, VerifyEachPass
,
262 PipelineText
.empty())
264 assert(PipelineText
[0] == ')');
265 PipelineText
= PipelineText
.substr(1);
267 // Add the nested pass manager with the appropriate adaptor.
268 CGPM
.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM
)));
270 // Otherwise try to parse a pass name.
271 size_t End
= PipelineText
.find_first_of(",)");
272 if (!parseCGSCCPassName(CGPM
, PipelineText
.substr(0, End
)))
274 // FIXME: No verifier support for CGSCC passes!
276 PipelineText
= PipelineText
.substr(End
);
279 if (PipelineText
.empty() || PipelineText
[0] == ')')
282 assert(PipelineText
[0] == ',');
283 PipelineText
= PipelineText
.substr(1);
287 static bool parseModulePassPipeline(ModulePassManager
&MPM
,
288 StringRef
&PipelineText
,
289 bool VerifyEachPass
, bool DebugLogging
) {
291 // Parse nested pass managers by recursing.
292 if (PipelineText
.startswith("module(")) {
293 ModulePassManager
NestedMPM(DebugLogging
);
295 // Parse the inner pipeline into the nested manager.
296 PipelineText
= PipelineText
.substr(strlen("module("));
297 if (!parseModulePassPipeline(NestedMPM
, PipelineText
, VerifyEachPass
,
299 PipelineText
.empty())
301 assert(PipelineText
[0] == ')');
302 PipelineText
= PipelineText
.substr(1);
304 // Now add the nested manager as a module pass.
305 MPM
.addPass(std::move(NestedMPM
));
306 } else if (PipelineText
.startswith("cgscc(")) {
307 CGSCCPassManager
NestedCGPM(DebugLogging
);
309 // Parse the inner pipeline inte the nested manager.
310 PipelineText
= PipelineText
.substr(strlen("cgscc("));
311 if (!parseCGSCCPassPipeline(NestedCGPM
, PipelineText
, VerifyEachPass
,
313 PipelineText
.empty())
315 assert(PipelineText
[0] == ')');
316 PipelineText
= PipelineText
.substr(1);
318 // Add the nested pass manager with the appropriate adaptor.
320 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM
)));
321 } else if (PipelineText
.startswith("function(")) {
322 FunctionPassManager
NestedFPM(DebugLogging
);
324 // Parse the inner pipeline inte the nested manager.
325 PipelineText
= PipelineText
.substr(strlen("function("));
326 if (!parseFunctionPassPipeline(NestedFPM
, PipelineText
, VerifyEachPass
,
328 PipelineText
.empty())
330 assert(PipelineText
[0] == ')');
331 PipelineText
= PipelineText
.substr(1);
333 // Add the nested pass manager with the appropriate adaptor.
334 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM
)));
336 // Otherwise try to parse a pass name.
337 size_t End
= PipelineText
.find_first_of(",)");
338 if (!parseModulePassName(MPM
, PipelineText
.substr(0, End
)))
341 MPM
.addPass(VerifierPass());
343 PipelineText
= PipelineText
.substr(End
);
346 if (PipelineText
.empty() || PipelineText
[0] == ')')
349 assert(PipelineText
[0] == ',');
350 PipelineText
= PipelineText
.substr(1);
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?
357 bool llvm::parsePassPipeline(ModulePassManager
&MPM
, StringRef PipelineText
,
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();
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.
367 StringRef FirstName
=
368 PipelineText
.substr(0, PipelineText
.find_first_of(",)"));
369 assert(!isModulePassName(FirstName
) &&
370 "Already handled all module pipeline options.");
372 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
374 if (isCGSCCPassName(FirstName
)) {
375 CGSCCPassManager
CGPM(DebugLogging
);
376 if (!parseCGSCCPassPipeline(CGPM
, PipelineText
, VerifyEachPass
,
378 !PipelineText
.empty())
380 MPM
.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM
)));
384 // Similarly, if this looks like a Function pass, parse the whole thing as
385 // a Function pipelien.
386 if (isFunctionPassName(FirstName
)) {
387 FunctionPassManager
FPM(DebugLogging
);
388 if (!parseFunctionPassPipeline(FPM
, PipelineText
, VerifyEachPass
,
390 !PipelineText
.empty())
392 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(FPM
)));