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/AssumptionCache.h"
19 #include "llvm/Analysis/CGSCCPassManager.h"
20 #include "llvm/Analysis/LazyCallGraph.h"
21 #include "llvm/Analysis/LoopInfo.h"
22 #include "llvm/Analysis/TargetLibraryInfo.h"
23 #include "llvm/IR/Dominators.h"
24 #include "llvm/IR/IRPrintingPasses.h"
25 #include "llvm/IR/PassManager.h"
26 #include "llvm/IR/Verifier.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Transforms/InstCombine/InstCombine.h"
29 #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
35 /// \brief No-op module pass which does nothing.
36 struct NoOpModulePass {
37 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
38 static StringRef name() { return "NoOpModulePass"; }
41 /// \brief No-op module analysis.
42 struct NoOpModuleAnalysis {
44 Result run(Module &) { return Result(); }
45 static StringRef name() { return "NoOpModuleAnalysis"; }
46 static void *ID() { return (void *)&PassID; }
51 char NoOpModuleAnalysis::PassID;
53 /// \brief No-op CGSCC pass which does nothing.
54 struct NoOpCGSCCPass {
55 PreservedAnalyses run(LazyCallGraph::SCC &C) {
56 return PreservedAnalyses::all();
58 static StringRef name() { return "NoOpCGSCCPass"; }
61 /// \brief No-op CGSCC analysis.
62 struct NoOpCGSCCAnalysis {
64 Result run(LazyCallGraph::SCC &) { return Result(); }
65 static StringRef name() { return "NoOpCGSCCAnalysis"; }
66 static void *ID() { return (void *)&PassID; }
71 char NoOpCGSCCAnalysis::PassID;
73 /// \brief No-op function pass which does nothing.
74 struct NoOpFunctionPass {
75 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
76 static StringRef name() { return "NoOpFunctionPass"; }
79 /// \brief No-op function analysis.
80 struct NoOpFunctionAnalysis {
82 Result run(Function &) { return Result(); }
83 static StringRef name() { return "NoOpFunctionAnalysis"; }
84 static void *ID() { return (void *)&PassID; }
89 char NoOpFunctionAnalysis::PassID;
91 } // End anonymous namespace.
93 void Passes::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
94 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
95 MAM.registerPass(CREATE_PASS);
96 #include "PassRegistry.def"
99 void Passes::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
100 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
101 CGAM.registerPass(CREATE_PASS);
102 #include "PassRegistry.def"
105 void Passes::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
106 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
107 FAM.registerPass(CREATE_PASS);
108 #include "PassRegistry.def"
112 static bool isModulePassName(StringRef Name) {
113 #define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
114 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
115 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
117 #include "PassRegistry.def"
123 static bool isCGSCCPassName(StringRef Name) {
124 #define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
125 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
126 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
128 #include "PassRegistry.def"
133 static bool isFunctionPassName(StringRef Name) {
134 #define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
135 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
136 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
138 #include "PassRegistry.def"
143 bool Passes::parseModulePassName(ModulePassManager &MPM, StringRef Name) {
144 #define MODULE_PASS(NAME, CREATE_PASS) \
145 if (Name == NAME) { \
146 MPM.addPass(CREATE_PASS); \
149 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
150 if (Name == "require<" NAME ">") { \
151 MPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
154 if (Name == "invalidate<" NAME ">") { \
155 MPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
158 #include "PassRegistry.def"
163 bool Passes::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
164 #define CGSCC_PASS(NAME, CREATE_PASS) \
165 if (Name == NAME) { \
166 CGPM.addPass(CREATE_PASS); \
169 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
170 if (Name == "require<" NAME ">") { \
171 CGPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
174 if (Name == "invalidate<" NAME ">") { \
175 CGPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
178 #include "PassRegistry.def"
183 bool Passes::parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
184 #define FUNCTION_PASS(NAME, CREATE_PASS) \
185 if (Name == NAME) { \
186 FPM.addPass(CREATE_PASS); \
189 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
190 if (Name == "require<" NAME ">") { \
191 FPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
194 if (Name == "invalidate<" NAME ">") { \
195 FPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
198 #include "PassRegistry.def"
203 bool Passes::parseFunctionPassPipeline(FunctionPassManager &FPM,
204 StringRef &PipelineText,
205 bool VerifyEachPass, bool DebugLogging) {
207 // Parse nested pass managers by recursing.
208 if (PipelineText.startswith("function(")) {
209 FunctionPassManager NestedFPM(DebugLogging);
211 // Parse the inner pipeline inte the nested manager.
212 PipelineText = PipelineText.substr(strlen("function("));
213 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
215 PipelineText.empty())
217 assert(PipelineText[0] == ')');
218 PipelineText = PipelineText.substr(1);
220 // Add the nested pass manager with the appropriate adaptor.
221 FPM.addPass(std::move(NestedFPM));
223 // Otherwise try to parse a pass name.
224 size_t End = PipelineText.find_first_of(",)");
225 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
228 FPM.addPass(VerifierPass());
230 PipelineText = PipelineText.substr(End);
233 if (PipelineText.empty() || PipelineText[0] == ')')
236 assert(PipelineText[0] == ',');
237 PipelineText = PipelineText.substr(1);
241 bool Passes::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
242 StringRef &PipelineText,
243 bool VerifyEachPass, bool DebugLogging) {
245 // Parse nested pass managers by recursing.
246 if (PipelineText.startswith("cgscc(")) {
247 CGSCCPassManager NestedCGPM(DebugLogging);
249 // Parse the inner pipeline into the nested manager.
250 PipelineText = PipelineText.substr(strlen("cgscc("));
251 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
253 PipelineText.empty())
255 assert(PipelineText[0] == ')');
256 PipelineText = PipelineText.substr(1);
258 // Add the nested pass manager with the appropriate adaptor.
259 CGPM.addPass(std::move(NestedCGPM));
260 } else if (PipelineText.startswith("function(")) {
261 FunctionPassManager NestedFPM(DebugLogging);
263 // Parse the inner pipeline inte the nested manager.
264 PipelineText = PipelineText.substr(strlen("function("));
265 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
267 PipelineText.empty())
269 assert(PipelineText[0] == ')');
270 PipelineText = PipelineText.substr(1);
272 // Add the nested pass manager with the appropriate adaptor.
273 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
275 // Otherwise try to parse a pass name.
276 size_t End = PipelineText.find_first_of(",)");
277 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
279 // FIXME: No verifier support for CGSCC passes!
281 PipelineText = PipelineText.substr(End);
284 if (PipelineText.empty() || PipelineText[0] == ')')
287 assert(PipelineText[0] == ',');
288 PipelineText = PipelineText.substr(1);
292 bool Passes::parseModulePassPipeline(ModulePassManager &MPM,
293 StringRef &PipelineText,
294 bool VerifyEachPass, bool DebugLogging) {
296 // Parse nested pass managers by recursing.
297 if (PipelineText.startswith("module(")) {
298 ModulePassManager NestedMPM(DebugLogging);
300 // Parse the inner pipeline into the nested manager.
301 PipelineText = PipelineText.substr(strlen("module("));
302 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
304 PipelineText.empty())
306 assert(PipelineText[0] == ')');
307 PipelineText = PipelineText.substr(1);
309 // Now add the nested manager as a module pass.
310 MPM.addPass(std::move(NestedMPM));
311 } else if (PipelineText.startswith("cgscc(")) {
312 CGSCCPassManager NestedCGPM(DebugLogging);
314 // Parse the inner pipeline inte the nested manager.
315 PipelineText = PipelineText.substr(strlen("cgscc("));
316 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
318 PipelineText.empty())
320 assert(PipelineText[0] == ')');
321 PipelineText = PipelineText.substr(1);
323 // Add the nested pass manager with the appropriate adaptor.
325 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
326 } else if (PipelineText.startswith("function(")) {
327 FunctionPassManager NestedFPM(DebugLogging);
329 // Parse the inner pipeline inte the nested manager.
330 PipelineText = PipelineText.substr(strlen("function("));
331 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
333 PipelineText.empty())
335 assert(PipelineText[0] == ')');
336 PipelineText = PipelineText.substr(1);
338 // Add the nested pass manager with the appropriate adaptor.
339 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
341 // Otherwise try to parse a pass name.
342 size_t End = PipelineText.find_first_of(",)");
343 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
346 MPM.addPass(VerifierPass());
348 PipelineText = PipelineText.substr(End);
351 if (PipelineText.empty() || PipelineText[0] == ')')
354 assert(PipelineText[0] == ',');
355 PipelineText = PipelineText.substr(1);
359 // Primary pass pipeline description parsing routine.
360 // FIXME: Should this routine accept a TargetMachine or require the caller to
361 // pre-populate the analysis managers with target-specific stuff?
362 bool Passes::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText,
363 bool VerifyEachPass, bool DebugLogging) {
364 // By default, try to parse the pipeline as-if it were within an implicit
365 // 'module(...)' pass pipeline. If this will parse at all, it needs to
366 // consume the entire string.
367 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
368 return PipelineText.empty();
370 // This isn't parsable as a module pipeline, look for the end of a pass name
371 // and directly drop down to that layer.
372 StringRef FirstName =
373 PipelineText.substr(0, PipelineText.find_first_of(",)"));
374 assert(!isModulePassName(FirstName) &&
375 "Already handled all module pipeline options.");
377 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
379 if (isCGSCCPassName(FirstName)) {
380 CGSCCPassManager CGPM(DebugLogging);
381 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
383 !PipelineText.empty())
385 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
389 // Similarly, if this looks like a Function pass, parse the whole thing as
390 // a Function pipelien.
391 if (isFunctionPassName(FirstName)) {
392 FunctionPassManager FPM(DebugLogging);
393 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
395 !PipelineText.empty())
397 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));