1 //===- Parsing, selection, and construction of pass pipelines -------------===//
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 implementation of the PassBuilder based on our
12 /// static pass registry as well as related functionality. It also provides
13 /// helpers to aid in analyzing, debugging, and testing passes and pass
16 //===----------------------------------------------------------------------===//
18 #include "llvm/Passes/PassBuilder.h"
19 #include "llvm/Analysis/AssumptionCache.h"
20 #include "llvm/Analysis/CGSCCPassManager.h"
21 #include "llvm/Analysis/LazyCallGraph.h"
22 #include "llvm/Analysis/LoopInfo.h"
23 #include "llvm/Analysis/ScalarEvolution.h"
24 #include "llvm/Analysis/TargetLibraryInfo.h"
25 #include "llvm/Analysis/TargetTransformInfo.h"
26 #include "llvm/IR/Dominators.h"
27 #include "llvm/IR/IRPrintingPasses.h"
28 #include "llvm/IR/PassManager.h"
29 #include "llvm/IR/Verifier.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Target/TargetMachine.h"
32 #include "llvm/Transforms/InstCombine/InstCombine.h"
33 #include "llvm/Transforms/IPO/StripDeadPrototypes.h"
34 #include "llvm/Transforms/Scalar/ADCE.h"
35 #include "llvm/Transforms/Scalar/EarlyCSE.h"
36 #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
37 #include "llvm/Transforms/Scalar/SimplifyCFG.h"
38 #include "llvm/Transforms/Scalar/SROA.h"
44 /// \brief No-op module pass which does nothing.
45 struct NoOpModulePass {
46 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
47 static StringRef name() { return "NoOpModulePass"; }
50 /// \brief No-op module analysis.
51 struct NoOpModuleAnalysis {
53 Result run(Module &) { return Result(); }
54 static StringRef name() { return "NoOpModuleAnalysis"; }
55 static void *ID() { return (void *)&PassID; }
60 char NoOpModuleAnalysis::PassID;
62 /// \brief No-op CGSCC pass which does nothing.
63 struct NoOpCGSCCPass {
64 PreservedAnalyses run(LazyCallGraph::SCC &C) {
65 return PreservedAnalyses::all();
67 static StringRef name() { return "NoOpCGSCCPass"; }
70 /// \brief No-op CGSCC analysis.
71 struct NoOpCGSCCAnalysis {
73 Result run(LazyCallGraph::SCC &) { return Result(); }
74 static StringRef name() { return "NoOpCGSCCAnalysis"; }
75 static void *ID() { return (void *)&PassID; }
80 char NoOpCGSCCAnalysis::PassID;
82 /// \brief No-op function pass which does nothing.
83 struct NoOpFunctionPass {
84 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
85 static StringRef name() { return "NoOpFunctionPass"; }
88 /// \brief No-op function analysis.
89 struct NoOpFunctionAnalysis {
91 Result run(Function &) { return Result(); }
92 static StringRef name() { return "NoOpFunctionAnalysis"; }
93 static void *ID() { return (void *)&PassID; }
98 char NoOpFunctionAnalysis::PassID;
100 } // End anonymous namespace.
102 void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
103 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
104 MAM.registerPass(CREATE_PASS);
105 #include "PassRegistry.def"
108 void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
109 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
110 CGAM.registerPass(CREATE_PASS);
111 #include "PassRegistry.def"
114 void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
115 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
116 FAM.registerPass(CREATE_PASS);
117 #include "PassRegistry.def"
121 static bool isModulePassName(StringRef Name) {
122 #define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
123 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
124 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
126 #include "PassRegistry.def"
132 static bool isCGSCCPassName(StringRef Name) {
133 #define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
134 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
135 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
137 #include "PassRegistry.def"
142 static bool isFunctionPassName(StringRef Name) {
143 #define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
144 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
145 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
147 #include "PassRegistry.def"
152 bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name) {
153 #define MODULE_PASS(NAME, CREATE_PASS) \
154 if (Name == NAME) { \
155 MPM.addPass(CREATE_PASS); \
158 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
159 if (Name == "require<" NAME ">") { \
160 MPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
163 if (Name == "invalidate<" NAME ">") { \
164 MPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
167 #include "PassRegistry.def"
172 bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
173 #define CGSCC_PASS(NAME, CREATE_PASS) \
174 if (Name == NAME) { \
175 CGPM.addPass(CREATE_PASS); \
178 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
179 if (Name == "require<" NAME ">") { \
180 CGPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
183 if (Name == "invalidate<" NAME ">") { \
184 CGPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
187 #include "PassRegistry.def"
192 bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
194 #define FUNCTION_PASS(NAME, CREATE_PASS) \
195 if (Name == NAME) { \
196 FPM.addPass(CREATE_PASS); \
199 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
200 if (Name == "require<" NAME ">") { \
201 FPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
204 if (Name == "invalidate<" NAME ">") { \
205 FPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
208 #include "PassRegistry.def"
213 bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
214 StringRef &PipelineText,
218 // Parse nested pass managers by recursing.
219 if (PipelineText.startswith("function(")) {
220 FunctionPassManager NestedFPM(DebugLogging);
222 // Parse the inner pipeline inte the nested manager.
223 PipelineText = PipelineText.substr(strlen("function("));
224 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
226 PipelineText.empty())
228 assert(PipelineText[0] == ')');
229 PipelineText = PipelineText.substr(1);
231 // Add the nested pass manager with the appropriate adaptor.
232 FPM.addPass(std::move(NestedFPM));
234 // Otherwise try to parse a pass name.
235 size_t End = PipelineText.find_first_of(",)");
236 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
239 FPM.addPass(VerifierPass());
241 PipelineText = PipelineText.substr(End);
244 if (PipelineText.empty() || PipelineText[0] == ')')
247 assert(PipelineText[0] == ',');
248 PipelineText = PipelineText.substr(1);
252 bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
253 StringRef &PipelineText,
257 // Parse nested pass managers by recursing.
258 if (PipelineText.startswith("cgscc(")) {
259 CGSCCPassManager NestedCGPM(DebugLogging);
261 // Parse the inner pipeline into the nested manager.
262 PipelineText = PipelineText.substr(strlen("cgscc("));
263 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
265 PipelineText.empty())
267 assert(PipelineText[0] == ')');
268 PipelineText = PipelineText.substr(1);
270 // Add the nested pass manager with the appropriate adaptor.
271 CGPM.addPass(std::move(NestedCGPM));
272 } else if (PipelineText.startswith("function(")) {
273 FunctionPassManager NestedFPM(DebugLogging);
275 // Parse the inner pipeline inte the nested manager.
276 PipelineText = PipelineText.substr(strlen("function("));
277 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
279 PipelineText.empty())
281 assert(PipelineText[0] == ')');
282 PipelineText = PipelineText.substr(1);
284 // Add the nested pass manager with the appropriate adaptor.
285 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
287 // Otherwise try to parse a pass name.
288 size_t End = PipelineText.find_first_of(",)");
289 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
291 // FIXME: No verifier support for CGSCC passes!
293 PipelineText = PipelineText.substr(End);
296 if (PipelineText.empty() || PipelineText[0] == ')')
299 assert(PipelineText[0] == ',');
300 PipelineText = PipelineText.substr(1);
304 bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
305 StringRef &PipelineText,
309 // Parse nested pass managers by recursing.
310 if (PipelineText.startswith("module(")) {
311 ModulePassManager NestedMPM(DebugLogging);
313 // Parse the inner pipeline into the nested manager.
314 PipelineText = PipelineText.substr(strlen("module("));
315 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
317 PipelineText.empty())
319 assert(PipelineText[0] == ')');
320 PipelineText = PipelineText.substr(1);
322 // Now add the nested manager as a module pass.
323 MPM.addPass(std::move(NestedMPM));
324 } else if (PipelineText.startswith("cgscc(")) {
325 CGSCCPassManager NestedCGPM(DebugLogging);
327 // Parse the inner pipeline inte the nested manager.
328 PipelineText = PipelineText.substr(strlen("cgscc("));
329 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
331 PipelineText.empty())
333 assert(PipelineText[0] == ')');
334 PipelineText = PipelineText.substr(1);
336 // Add the nested pass manager with the appropriate adaptor.
338 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
339 } else if (PipelineText.startswith("function(")) {
340 FunctionPassManager NestedFPM(DebugLogging);
342 // Parse the inner pipeline inte the nested manager.
343 PipelineText = PipelineText.substr(strlen("function("));
344 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
346 PipelineText.empty())
348 assert(PipelineText[0] == ')');
349 PipelineText = PipelineText.substr(1);
351 // Add the nested pass manager with the appropriate adaptor.
352 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
354 // Otherwise try to parse a pass name.
355 size_t End = PipelineText.find_first_of(",)");
356 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
359 MPM.addPass(VerifierPass());
361 PipelineText = PipelineText.substr(End);
364 if (PipelineText.empty() || PipelineText[0] == ')')
367 assert(PipelineText[0] == ',');
368 PipelineText = PipelineText.substr(1);
372 // Primary pass pipeline description parsing routine.
373 // FIXME: Should this routine accept a TargetMachine or require the caller to
374 // pre-populate the analysis managers with target-specific stuff?
375 bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
376 StringRef PipelineText, bool VerifyEachPass,
378 // By default, try to parse the pipeline as-if it were within an implicit
379 // 'module(...)' pass pipeline. If this will parse at all, it needs to
380 // consume the entire string.
381 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
382 return PipelineText.empty();
384 // This isn't parsable as a module pipeline, look for the end of a pass name
385 // and directly drop down to that layer.
386 StringRef FirstName =
387 PipelineText.substr(0, PipelineText.find_first_of(",)"));
388 assert(!isModulePassName(FirstName) &&
389 "Already handled all module pipeline options.");
391 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
393 if (isCGSCCPassName(FirstName)) {
394 CGSCCPassManager CGPM(DebugLogging);
395 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
397 !PipelineText.empty())
399 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
403 // Similarly, if this looks like a Function pass, parse the whole thing as
404 // a Function pipelien.
405 if (isFunctionPassName(FirstName)) {
406 FunctionPassManager FPM(DebugLogging);
407 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
409 !PipelineText.empty())
411 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));