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/LazyCallGraph.h"
19 #include "llvm/IR/IRPrintingPasses.h"
20 #include "llvm/IR/PassManager.h"
21 #include "llvm/IR/Verifier.h"
22 #include "llvm/Support/Debug.h"
28 /// \brief No-op module pass which does nothing.
29 struct NoOpModulePass {
30 PreservedAnalyses run(Module *M) { return PreservedAnalyses::all(); }
31 static StringRef name() { return "NoOpModulePass"; }
34 /// \brief No-op function pass which does nothing.
35 struct NoOpFunctionPass {
36 PreservedAnalyses run(Function *F) { return PreservedAnalyses::all(); }
37 static StringRef name() { return "NoOpFunctionPass"; }
40 } // End anonymous namespace.
42 static bool isModulePassName(StringRef Name) {
43 if (Name == "no-op-module") return true;
45 #define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
46 #include "PassRegistry.def"
51 static bool isFunctionPassName(StringRef Name) {
52 if (Name == "no-op-function") return true;
54 #define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
55 #include "PassRegistry.def"
60 static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
61 if (Name == "no-op-module") {
62 MPM.addPass(NoOpModulePass());
66 #define MODULE_PASS(NAME, CREATE_PASS) \
68 MPM.addPass(CREATE_PASS); \
71 #include "PassRegistry.def"
76 static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
77 if (Name == "no-op-function") {
78 FPM.addPass(NoOpFunctionPass());
82 #define FUNCTION_PASS(NAME, CREATE_PASS) \
84 FPM.addPass(CREATE_PASS); \
87 #include "PassRegistry.def"
92 static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
93 StringRef &PipelineText,
94 bool VerifyEachPass) {
96 // Parse nested pass managers by recursing.
97 if (PipelineText.startswith("function(")) {
98 FunctionPassManager NestedFPM;
100 // Parse the inner pipeline inte the nested manager.
101 PipelineText = PipelineText.substr(strlen("function("));
102 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
103 PipelineText.empty())
105 assert(PipelineText[0] == ')');
106 PipelineText = PipelineText.substr(1);
108 // Add the nested pass manager with the appropriate adaptor.
109 FPM.addPass(std::move(NestedFPM));
111 // Otherwise try to parse a pass name.
112 size_t End = PipelineText.find_first_of(",)");
113 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
116 FPM.addPass(VerifierPass());
118 PipelineText = PipelineText.substr(End);
121 if (PipelineText.empty() || PipelineText[0] == ')')
124 assert(PipelineText[0] == ',');
125 PipelineText = PipelineText.substr(1);
129 static bool parseModulePassPipeline(ModulePassManager &MPM,
130 StringRef &PipelineText,
131 bool VerifyEachPass) {
133 // Parse nested pass managers by recursing.
134 if (PipelineText.startswith("module(")) {
135 ModulePassManager NestedMPM;
137 // Parse the inner pipeline into the nested manager.
138 PipelineText = PipelineText.substr(strlen("module("));
139 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass) ||
140 PipelineText.empty())
142 assert(PipelineText[0] == ')');
143 PipelineText = PipelineText.substr(1);
145 // Now add the nested manager as a module pass.
146 MPM.addPass(std::move(NestedMPM));
147 } else if (PipelineText.startswith("function(")) {
148 FunctionPassManager NestedFPM;
150 // Parse the inner pipeline inte the nested manager.
151 PipelineText = PipelineText.substr(strlen("function("));
152 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
153 PipelineText.empty())
155 assert(PipelineText[0] == ')');
156 PipelineText = PipelineText.substr(1);
158 // Add the nested pass manager with the appropriate adaptor.
159 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
161 // Otherwise try to parse a pass name.
162 size_t End = PipelineText.find_first_of(",)");
163 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
166 MPM.addPass(VerifierPass());
168 PipelineText = PipelineText.substr(End);
171 if (PipelineText.empty() || PipelineText[0] == ')')
174 assert(PipelineText[0] == ',');
175 PipelineText = PipelineText.substr(1);
179 // Primary pass pipeline description parsing routine.
180 // FIXME: Should this routine accept a TargetMachine or require the caller to
181 // pre-populate the analysis managers with target-specific stuff?
182 bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText,
183 bool VerifyEachPass) {
184 // Look at the first entry to figure out which layer to start parsing at.
185 if (PipelineText.startswith("module("))
186 return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
187 PipelineText.empty();
188 if (PipelineText.startswith("function(")) {
189 FunctionPassManager FPM;
190 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
191 !PipelineText.empty())
193 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
197 // This isn't a direct pass manager name, look for the end of a pass name.
198 StringRef FirstName =
199 PipelineText.substr(0, PipelineText.find_first_of(",)"));
200 if (isModulePassName(FirstName))
201 return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
202 PipelineText.empty();
204 if (isFunctionPassName(FirstName)) {
205 FunctionPassManager FPM;
206 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
207 !PipelineText.empty())
209 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));