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/IPO/ForceFunctionAttrs.h"
33 #include "llvm/Transforms/IPO/InferFunctionAttrs.h"
34 #include "llvm/Transforms/IPO/StripDeadPrototypes.h"
35 #include "llvm/Transforms/InstCombine/InstCombine.h"
36 #include "llvm/Transforms/Scalar/ADCE.h"
37 #include "llvm/Transforms/Scalar/EarlyCSE.h"
38 #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
39 #include "llvm/Transforms/Scalar/SROA.h"
40 #include "llvm/Transforms/Scalar/SimplifyCFG.h"
46 /// \brief No-op module pass which does nothing.
47 struct NoOpModulePass {
48 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
49 static StringRef name() { return "NoOpModulePass"; }
52 /// \brief No-op module analysis.
53 struct NoOpModuleAnalysis {
55 Result run(Module &) { return Result(); }
56 static StringRef name() { return "NoOpModuleAnalysis"; }
57 static void *ID() { return (void *)&PassID; }
62 char NoOpModuleAnalysis::PassID;
64 /// \brief No-op CGSCC pass which does nothing.
65 struct NoOpCGSCCPass {
66 PreservedAnalyses run(LazyCallGraph::SCC &C) {
67 return PreservedAnalyses::all();
69 static StringRef name() { return "NoOpCGSCCPass"; }
72 /// \brief No-op CGSCC analysis.
73 struct NoOpCGSCCAnalysis {
75 Result run(LazyCallGraph::SCC &) { return Result(); }
76 static StringRef name() { return "NoOpCGSCCAnalysis"; }
77 static void *ID() { return (void *)&PassID; }
82 char NoOpCGSCCAnalysis::PassID;
84 /// \brief No-op function pass which does nothing.
85 struct NoOpFunctionPass {
86 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
87 static StringRef name() { return "NoOpFunctionPass"; }
90 /// \brief No-op function analysis.
91 struct NoOpFunctionAnalysis {
93 Result run(Function &) { return Result(); }
94 static StringRef name() { return "NoOpFunctionAnalysis"; }
95 static void *ID() { return (void *)&PassID; }
100 char NoOpFunctionAnalysis::PassID;
102 } // End anonymous namespace.
104 void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
105 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
106 MAM.registerPass(CREATE_PASS);
107 #include "PassRegistry.def"
110 void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
111 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
112 CGAM.registerPass(CREATE_PASS);
113 #include "PassRegistry.def"
116 void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
117 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
118 FAM.registerPass(CREATE_PASS);
119 #include "PassRegistry.def"
123 static bool isModulePassName(StringRef Name) {
124 #define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
125 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
126 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
128 #include "PassRegistry.def"
134 static bool isCGSCCPassName(StringRef Name) {
135 #define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
136 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
137 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
139 #include "PassRegistry.def"
144 static bool isFunctionPassName(StringRef Name) {
145 #define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
146 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
147 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
149 #include "PassRegistry.def"
154 bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name) {
155 #define MODULE_PASS(NAME, CREATE_PASS) \
156 if (Name == NAME) { \
157 MPM.addPass(CREATE_PASS); \
160 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
161 if (Name == "require<" NAME ">") { \
162 MPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
165 if (Name == "invalidate<" NAME ">") { \
166 MPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
169 #include "PassRegistry.def"
174 bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
175 #define CGSCC_PASS(NAME, CREATE_PASS) \
176 if (Name == NAME) { \
177 CGPM.addPass(CREATE_PASS); \
180 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
181 if (Name == "require<" NAME ">") { \
182 CGPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
185 if (Name == "invalidate<" NAME ">") { \
186 CGPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
189 #include "PassRegistry.def"
194 bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
196 #define FUNCTION_PASS(NAME, CREATE_PASS) \
197 if (Name == NAME) { \
198 FPM.addPass(CREATE_PASS); \
201 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
202 if (Name == "require<" NAME ">") { \
203 FPM.addPass(RequireAnalysisPass<decltype(CREATE_PASS)>()); \
206 if (Name == "invalidate<" NAME ">") { \
207 FPM.addPass(InvalidateAnalysisPass<decltype(CREATE_PASS)>()); \
210 #include "PassRegistry.def"
215 bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
216 StringRef &PipelineText,
220 // Parse nested pass managers by recursing.
221 if (PipelineText.startswith("function(")) {
222 FunctionPassManager NestedFPM(DebugLogging);
224 // Parse the inner pipeline inte the nested manager.
225 PipelineText = PipelineText.substr(strlen("function("));
226 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
228 PipelineText.empty())
230 assert(PipelineText[0] == ')');
231 PipelineText = PipelineText.substr(1);
233 // Add the nested pass manager with the appropriate adaptor.
234 FPM.addPass(std::move(NestedFPM));
236 // Otherwise try to parse a pass name.
237 size_t End = PipelineText.find_first_of(",)");
238 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
241 FPM.addPass(VerifierPass());
243 PipelineText = PipelineText.substr(End);
246 if (PipelineText.empty() || PipelineText[0] == ')')
249 assert(PipelineText[0] == ',');
250 PipelineText = PipelineText.substr(1);
254 bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
255 StringRef &PipelineText,
259 // Parse nested pass managers by recursing.
260 if (PipelineText.startswith("cgscc(")) {
261 CGSCCPassManager NestedCGPM(DebugLogging);
263 // Parse the inner pipeline into the nested manager.
264 PipelineText = PipelineText.substr(strlen("cgscc("));
265 if (!parseCGSCCPassPipeline(NestedCGPM, 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(std::move(NestedCGPM));
274 } else if (PipelineText.startswith("function(")) {
275 FunctionPassManager NestedFPM(DebugLogging);
277 // Parse the inner pipeline inte the nested manager.
278 PipelineText = PipelineText.substr(strlen("function("));
279 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
281 PipelineText.empty())
283 assert(PipelineText[0] == ')');
284 PipelineText = PipelineText.substr(1);
286 // Add the nested pass manager with the appropriate adaptor.
287 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
289 // Otherwise try to parse a pass name.
290 size_t End = PipelineText.find_first_of(",)");
291 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
293 // FIXME: No verifier support for CGSCC passes!
295 PipelineText = PipelineText.substr(End);
298 if (PipelineText.empty() || PipelineText[0] == ')')
301 assert(PipelineText[0] == ',');
302 PipelineText = PipelineText.substr(1);
306 bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
307 StringRef &PipelineText,
311 // Parse nested pass managers by recursing.
312 if (PipelineText.startswith("module(")) {
313 ModulePassManager NestedMPM(DebugLogging);
315 // Parse the inner pipeline into the nested manager.
316 PipelineText = PipelineText.substr(strlen("module("));
317 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
319 PipelineText.empty())
321 assert(PipelineText[0] == ')');
322 PipelineText = PipelineText.substr(1);
324 // Now add the nested manager as a module pass.
325 MPM.addPass(std::move(NestedMPM));
326 } else if (PipelineText.startswith("cgscc(")) {
327 CGSCCPassManager NestedCGPM(DebugLogging);
329 // Parse the inner pipeline inte the nested manager.
330 PipelineText = PipelineText.substr(strlen("cgscc("));
331 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
333 PipelineText.empty())
335 assert(PipelineText[0] == ')');
336 PipelineText = PipelineText.substr(1);
338 // Add the nested pass manager with the appropriate adaptor.
340 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
341 } else if (PipelineText.startswith("function(")) {
342 FunctionPassManager NestedFPM(DebugLogging);
344 // Parse the inner pipeline inte the nested manager.
345 PipelineText = PipelineText.substr(strlen("function("));
346 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
348 PipelineText.empty())
350 assert(PipelineText[0] == ')');
351 PipelineText = PipelineText.substr(1);
353 // Add the nested pass manager with the appropriate adaptor.
354 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
356 // Otherwise try to parse a pass name.
357 size_t End = PipelineText.find_first_of(",)");
358 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
361 MPM.addPass(VerifierPass());
363 PipelineText = PipelineText.substr(End);
366 if (PipelineText.empty() || PipelineText[0] == ')')
369 assert(PipelineText[0] == ',');
370 PipelineText = PipelineText.substr(1);
374 // Primary pass pipeline description parsing routine.
375 // FIXME: Should this routine accept a TargetMachine or require the caller to
376 // pre-populate the analysis managers with target-specific stuff?
377 bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
378 StringRef PipelineText, bool VerifyEachPass,
380 // By default, try to parse the pipeline as-if it were within an implicit
381 // 'module(...)' pass pipeline. If this will parse at all, it needs to
382 // consume the entire string.
383 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
384 return PipelineText.empty();
386 // This isn't parsable as a module pipeline, look for the end of a pass name
387 // and directly drop down to that layer.
388 StringRef FirstName =
389 PipelineText.substr(0, PipelineText.find_first_of(",)"));
390 assert(!isModulePassName(FirstName) &&
391 "Already handled all module pipeline options.");
393 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
395 if (isCGSCCPassName(FirstName)) {
396 CGSCCPassManager CGPM(DebugLogging);
397 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
399 !PipelineText.empty())
401 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
405 // Similarly, if this looks like a Function pass, parse the whole thing as
406 // a Function pipelien.
407 if (isFunctionPassName(FirstName)) {
408 FunctionPassManager FPM(DebugLogging);
409 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
411 !PipelineText.empty())
413 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));