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 // FIXME: Factor all of the parsing logic into a .def file that we include
43 // under different macros.
44 static bool isModulePassName(StringRef Name) {
45 if (Name == "no-op-module") return true;
46 if (Name == "print") return true;
47 if (Name == "print-cg") return true;
52 static bool isFunctionPassName(StringRef Name) {
53 if (Name == "no-op-function") return true;
54 if (Name == "print") return true;
59 static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
60 if (Name == "no-op-module") {
61 MPM.addPass(NoOpModulePass());
64 if (Name == "print") {
65 MPM.addPass(PrintModulePass(dbgs()));
68 if (Name == "print-cg") {
69 MPM.addPass(LazyCallGraphPrinterPass(dbgs()));
75 static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
76 if (Name == "no-op-function") {
77 FPM.addPass(NoOpFunctionPass());
80 if (Name == "print") {
81 FPM.addPass(PrintFunctionPass(dbgs()));
87 static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
88 StringRef &PipelineText,
89 bool VerifyEachPass) {
91 // Parse nested pass managers by recursing.
92 if (PipelineText.startswith("function(")) {
93 FunctionPassManager NestedFPM;
95 // Parse the inner pipeline inte the nested manager.
96 PipelineText = PipelineText.substr(strlen("function("));
97 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
100 assert(PipelineText[0] == ')');
101 PipelineText = PipelineText.substr(1);
103 // Add the nested pass manager with the appropriate adaptor.
104 FPM.addPass(NestedFPM);
106 // Otherwise try to parse a pass name.
107 size_t End = PipelineText.find_first_of(",)");
108 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
111 FPM.addPass(VerifierPass());
113 PipelineText = PipelineText.substr(End);
116 if (PipelineText.empty() || PipelineText[0] == ')')
119 assert(PipelineText[0] == ',');
120 PipelineText = PipelineText.substr(1);
124 static bool parseModulePassPipeline(ModulePassManager &MPM,
125 StringRef &PipelineText,
126 bool VerifyEachPass) {
128 // Parse nested pass managers by recursing.
129 if (PipelineText.startswith("module(")) {
130 ModulePassManager NestedMPM;
132 // Parse the inner pipeline into the nested manager.
133 PipelineText = PipelineText.substr(strlen("module("));
134 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass) ||
135 PipelineText.empty())
137 assert(PipelineText[0] == ')');
138 PipelineText = PipelineText.substr(1);
140 // Now add the nested manager as a module pass.
141 MPM.addPass(NestedMPM);
142 } else if (PipelineText.startswith("function(")) {
143 FunctionPassManager NestedFPM;
145 // Parse the inner pipeline inte the nested manager.
146 PipelineText = PipelineText.substr(strlen("function("));
147 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
148 PipelineText.empty())
150 assert(PipelineText[0] == ')');
151 PipelineText = PipelineText.substr(1);
153 // Add the nested pass manager with the appropriate adaptor.
154 MPM.addPass(createModuleToFunctionPassAdaptor(NestedFPM));
156 // Otherwise try to parse a pass name.
157 size_t End = PipelineText.find_first_of(",)");
158 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
161 MPM.addPass(VerifierPass());
163 PipelineText = PipelineText.substr(End);
166 if (PipelineText.empty() || PipelineText[0] == ')')
169 assert(PipelineText[0] == ',');
170 PipelineText = PipelineText.substr(1);
174 // Primary pass pipeline description parsing routine.
175 // FIXME: Should this routine accept a TargetMachine or require the caller to
176 // pre-populate the analysis managers with target-specific stuff?
177 bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText,
178 bool VerifyEachPass) {
179 // Look at the first entry to figure out which layer to start parsing at.
180 if (PipelineText.startswith("module("))
181 return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
182 PipelineText.empty();
183 if (PipelineText.startswith("function(")) {
184 FunctionPassManager FPM;
185 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
186 !PipelineText.empty())
188 MPM.addPass(createModuleToFunctionPassAdaptor(FPM));
192 // This isn't a direct pass manager name, look for the end of a pass name.
193 StringRef FirstName =
194 PipelineText.substr(0, PipelineText.find_first_of(",)"));
195 if (isModulePassName(FirstName))
196 return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
197 PipelineText.empty();
199 if (isFunctionPassName(FirstName)) {
200 FunctionPassManager FPM;
201 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
202 !PipelineText.empty())
204 MPM.addPass(createModuleToFunctionPassAdaptor(FPM));