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/IR/IRPrintingPasses.h"
19 #include "llvm/IR/PassManager.h"
20 #include "llvm/IR/Verifier.h"
21 #include "llvm/Support/Debug.h"
27 /// \brief No-op module pass which does nothing.
28 struct NoOpModulePass {
29 PreservedAnalyses run(Module *M) { return PreservedAnalyses::all(); }
30 static StringRef name() { return "NoOpModulePass"; }
33 /// \brief No-op function pass which does nothing.
34 struct NoOpFunctionPass {
35 PreservedAnalyses run(Function *F) { return PreservedAnalyses::all(); }
36 static StringRef name() { return "NoOpFunctionPass"; }
39 } // End anonymous namespace.
41 // FIXME: Factor all of the parsing logic into a .def file that we include
42 // under different macros.
43 static bool isModulePassName(StringRef Name) {
44 if (Name == "no-op-module") return true;
45 if (Name == "print") return true;
50 static bool isFunctionPassName(StringRef Name) {
51 if (Name == "no-op-function") return true;
52 if (Name == "print") return true;
57 static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
58 if (Name == "no-op-module") {
59 MPM.addPass(NoOpModulePass());
62 if (Name == "print") {
63 MPM.addPass(PrintModulePass(dbgs()));
69 static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
70 if (Name == "no-op-function") {
71 FPM.addPass(NoOpFunctionPass());
74 if (Name == "print") {
75 FPM.addPass(PrintFunctionPass(dbgs()));
81 static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
82 StringRef &PipelineText,
83 bool VerifyEachPass) {
85 // Parse nested pass managers by recursing.
86 if (PipelineText.startswith("function(")) {
87 FunctionPassManager NestedFPM;
89 // Parse the inner pipeline inte the nested manager.
90 PipelineText = PipelineText.substr(strlen("function("));
91 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
94 assert(PipelineText[0] == ')');
95 PipelineText = PipelineText.substr(1);
97 // Add the nested pass manager with the appropriate adaptor.
98 FPM.addPass(NestedFPM);
100 // Otherwise try to parse a pass name.
101 size_t End = PipelineText.find_first_of(",)");
102 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
105 FPM.addPass(VerifierPass());
107 PipelineText = PipelineText.substr(End);
110 if (PipelineText.empty() || PipelineText[0] == ')')
113 assert(PipelineText[0] == ',');
114 PipelineText = PipelineText.substr(1);
118 static bool parseModulePassPipeline(ModulePassManager &MPM,
119 StringRef &PipelineText,
120 bool VerifyEachPass) {
122 // Parse nested pass managers by recursing.
123 if (PipelineText.startswith("module(")) {
124 ModulePassManager NestedMPM;
126 // Parse the inner pipeline into the nested manager.
127 PipelineText = PipelineText.substr(strlen("module("));
128 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass) ||
129 PipelineText.empty())
131 assert(PipelineText[0] == ')');
132 PipelineText = PipelineText.substr(1);
134 // Now add the nested manager as a module pass.
135 MPM.addPass(NestedMPM);
136 } else if (PipelineText.startswith("function(")) {
137 FunctionPassManager NestedFPM;
139 // Parse the inner pipeline inte the nested manager.
140 PipelineText = PipelineText.substr(strlen("function("));
141 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
142 PipelineText.empty())
144 assert(PipelineText[0] == ')');
145 PipelineText = PipelineText.substr(1);
147 // Add the nested pass manager with the appropriate adaptor.
148 MPM.addPass(createModuleToFunctionPassAdaptor(NestedFPM));
150 // Otherwise try to parse a pass name.
151 size_t End = PipelineText.find_first_of(",)");
152 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
155 MPM.addPass(VerifierPass());
157 PipelineText = PipelineText.substr(End);
160 if (PipelineText.empty() || PipelineText[0] == ')')
163 assert(PipelineText[0] == ',');
164 PipelineText = PipelineText.substr(1);
168 // Primary pass pipeline description parsing routine.
169 // FIXME: Should this routine accept a TargetMachine or require the caller to
170 // pre-populate the analysis managers with target-specific stuff?
171 bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText,
172 bool VerifyEachPass) {
173 // Look at the first entry to figure out which layer to start parsing at.
174 if (PipelineText.startswith("module("))
175 return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
176 PipelineText.empty();
177 if (PipelineText.startswith("function(")) {
178 FunctionPassManager FPM;
179 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
180 !PipelineText.empty())
182 MPM.addPass(createModuleToFunctionPassAdaptor(FPM));
186 // This isn't a direct pass manager name, look for the end of a pass name.
187 StringRef FirstName =
188 PipelineText.substr(0, PipelineText.find_first_of(",)"));
189 if (isModulePassName(FirstName))
190 return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
191 PipelineText.empty();
193 if (isFunctionPassName(FirstName)) {
194 FunctionPassManager FPM;
195 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
196 !PipelineText.empty())
198 MPM.addPass(createModuleToFunctionPassAdaptor(FPM));