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/Support/Debug.h"
26 /// \brief No-op module pass which does nothing.
27 struct NoOpModulePass {
28 PreservedAnalyses run(Module *M) { return PreservedAnalyses::all(); }
29 static StringRef name() { return "NoOpModulePass"; }
32 /// \brief No-op function pass which does nothing.
33 struct NoOpFunctionPass {
34 PreservedAnalyses run(Function *F) { return PreservedAnalyses::all(); }
35 static StringRef name() { return "NoOpFunctionPass"; }
38 } // End anonymous namespace.
40 // FIXME: Factor all of the parsing logic into a .def file that we include
41 // under different macros.
42 static bool isModulePassName(StringRef Name) {
43 if (Name == "no-op-module") return true;
44 if (Name == "print") return true;
49 static bool isFunctionPassName(StringRef Name) {
50 if (Name == "no-op-function") return true;
51 if (Name == "print") return true;
56 static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
57 if (Name == "no-op-module") {
58 MPM.addPass(NoOpModulePass());
61 if (Name == "print") {
62 MPM.addPass(PrintModulePass(dbgs()));
68 static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
69 if (Name == "no-op-function") {
70 FPM.addPass(NoOpFunctionPass());
73 if (Name == "print") {
74 FPM.addPass(PrintFunctionPass(dbgs()));
80 static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
81 StringRef &PipelineText) {
83 // Parse nested pass managers by recursing.
84 if (PipelineText.startswith("function(")) {
85 FunctionPassManager NestedFPM;
87 // Parse the inner pipeline inte the nested manager.
88 PipelineText = PipelineText.substr(strlen("function("));
89 if (!parseFunctionPassPipeline(NestedFPM, PipelineText) ||
92 assert(PipelineText[0] == ')');
93 PipelineText = PipelineText.substr(1);
95 // Add the nested pass manager with the appropriate adaptor.
96 FPM.addPass(NestedFPM);
98 // Otherwise try to parse a pass name.
99 size_t End = PipelineText.find_first_of(",)");
100 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
103 PipelineText = PipelineText.substr(End);
106 if (PipelineText.empty() || PipelineText[0] == ')')
109 assert(PipelineText[0] == ',');
110 PipelineText = PipelineText.substr(1);
114 static bool parseModulePassPipeline(ModulePassManager &MPM,
115 StringRef &PipelineText) {
117 // Parse nested pass managers by recursing.
118 if (PipelineText.startswith("module(")) {
119 ModulePassManager NestedMPM;
121 // Parse the inner pipeline into the nested manager.
122 PipelineText = PipelineText.substr(strlen("module("));
123 if (!parseModulePassPipeline(NestedMPM, PipelineText) ||
124 PipelineText.empty())
126 assert(PipelineText[0] == ')');
127 PipelineText = PipelineText.substr(1);
129 // Now add the nested manager as a module pass.
130 MPM.addPass(NestedMPM);
131 } else if (PipelineText.startswith("function(")) {
132 FunctionPassManager NestedFPM;
134 // Parse the inner pipeline inte the nested manager.
135 PipelineText = PipelineText.substr(strlen("function("));
136 if (!parseFunctionPassPipeline(NestedFPM, PipelineText) ||
137 PipelineText.empty())
139 assert(PipelineText[0] == ')');
140 PipelineText = PipelineText.substr(1);
142 // Add the nested pass manager with the appropriate adaptor.
143 MPM.addPass(createModuleToFunctionPassAdaptor(NestedFPM));
145 // Otherwise try to parse a pass name.
146 size_t End = PipelineText.find_first_of(",)");
147 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
150 PipelineText = PipelineText.substr(End);
153 if (PipelineText.empty() || PipelineText[0] == ')')
156 assert(PipelineText[0] == ',');
157 PipelineText = PipelineText.substr(1);
161 // Primary pass pipeline description parsing routine.
162 // FIXME: Should this routine accept a TargetMachine or require the caller to
163 // pre-populate the analysis managers with target-specific stuff?
164 bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText) {
165 // Look at the first entry to figure out which layer to start parsing at.
166 if (PipelineText.startswith("module("))
167 return parseModulePassPipeline(MPM, PipelineText) && PipelineText.empty();
168 if (PipelineText.startswith("function(")) {
169 FunctionPassManager FPM;
170 if (!parseFunctionPassPipeline(FPM, PipelineText) || !PipelineText.empty())
172 MPM.addPass(createModuleToFunctionPassAdaptor(FPM));
176 // This isn't a direct pass manager name, look for the end of a pass name.
177 StringRef FirstName =
178 PipelineText.substr(0, PipelineText.find_first_of(",)"));
179 if (isModulePassName(FirstName))
180 return parseModulePassPipeline(MPM, PipelineText) && PipelineText.empty();
182 if (isFunctionPassName(FirstName)) {
183 FunctionPassManager FPM;
184 if (!parseFunctionPassPipeline(FPM, PipelineText) || !PipelineText.empty())
186 MPM.addPass(createModuleToFunctionPassAdaptor(FPM));