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/PassManager.h"
24 /// \brief No-op module pass which does nothing.
25 struct NoOpModulePass {
26 PreservedAnalyses run(Module *M) { return PreservedAnalyses::all(); }
27 static StringRef name() { return "NoOpModulePass"; }
30 /// \brief No-op function pass which does nothing.
31 struct NoOpFunctionPass {
32 PreservedAnalyses run(Function *F) { return PreservedAnalyses::all(); }
33 static StringRef name() { return "NoOpFunctionPass"; }
36 } // End anonymous namespace.
38 // FIXME: Factor all of the parsing logic into a .def file that we include
39 // under different macros.
40 static bool isModulePassName(StringRef Name) {
41 if (Name == "no-op-module") return true;
46 static bool isFunctionPassName(StringRef Name) {
47 if (Name == "no-op-function") return true;
52 static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
53 if (Name == "no-op-module") {
54 MPM.addPass(NoOpModulePass());
60 static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
61 if (Name == "no-op-function") {
62 FPM.addPass(NoOpFunctionPass());
68 static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
69 StringRef &PipelineText) {
71 // Parse nested pass managers by recursing.
72 if (PipelineText.startswith("function(")) {
73 FunctionPassManager NestedFPM;
75 // Parse the inner pipeline inte the nested manager.
76 PipelineText = PipelineText.substr(strlen("function("));
77 if (!parseFunctionPassPipeline(NestedFPM, PipelineText) ||
80 assert(PipelineText[0] == ')');
81 PipelineText = PipelineText.substr(1);
83 // Add the nested pass manager with the appropriate adaptor.
84 FPM.addPass(NestedFPM);
86 // Otherwise try to parse a pass name.
87 size_t End = PipelineText.find_first_of(",)");
88 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
91 PipelineText = PipelineText.substr(End);
94 if (PipelineText.empty() || PipelineText[0] == ')')
97 assert(PipelineText[0] == ',');
98 PipelineText = PipelineText.substr(1);
102 static bool parseModulePassPipeline(ModulePassManager &MPM,
103 StringRef &PipelineText) {
105 // Parse nested pass managers by recursing.
106 if (PipelineText.startswith("module(")) {
107 ModulePassManager NestedMPM;
109 // Parse the inner pipeline into the nested manager.
110 PipelineText = PipelineText.substr(strlen("module("));
111 if (!parseModulePassPipeline(NestedMPM, PipelineText) ||
112 PipelineText.empty())
114 assert(PipelineText[0] == ')');
115 PipelineText = PipelineText.substr(1);
117 // Now add the nested manager as a module pass.
118 MPM.addPass(NestedMPM);
119 } else if (PipelineText.startswith("function(")) {
120 FunctionPassManager NestedFPM;
122 // Parse the inner pipeline inte the nested manager.
123 PipelineText = PipelineText.substr(strlen("function("));
124 if (!parseFunctionPassPipeline(NestedFPM, PipelineText) ||
125 PipelineText.empty())
127 assert(PipelineText[0] == ')');
128 PipelineText = PipelineText.substr(1);
130 // Add the nested pass manager with the appropriate adaptor.
131 MPM.addPass(createModuleToFunctionPassAdaptor(NestedFPM));
133 // Otherwise try to parse a pass name.
134 size_t End = PipelineText.find_first_of(",)");
135 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
138 PipelineText = PipelineText.substr(End);
141 if (PipelineText.empty() || PipelineText[0] == ')')
144 assert(PipelineText[0] == ',');
145 PipelineText = PipelineText.substr(1);
149 // Primary pass pipeline description parsing routine.
150 // FIXME: Should this routine accept a TargetMachine or require the caller to
151 // pre-populate the analysis managers with target-specific stuff?
152 bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText) {
153 // Look at the first entry to figure out which layer to start parsing at.
154 if (PipelineText.startswith("module("))
155 return parseModulePassPipeline(MPM, PipelineText) && PipelineText.empty();
156 if (PipelineText.startswith("function(")) {
157 FunctionPassManager FPM;
158 if (!parseFunctionPassPipeline(FPM, PipelineText) || !PipelineText.empty())
160 MPM.addPass(createModuleToFunctionPassAdaptor(FPM));
164 // This isn't a direct pass manager name, look for the end of a pass name.
165 StringRef FirstName =
166 PipelineText.substr(0, PipelineText.find_first_of(",)"));
167 if (isModulePassName(FirstName))
168 return parseModulePassPipeline(MPM, PipelineText) && PipelineText.empty();
170 if (isFunctionPassName(FirstName)) {
171 FunctionPassManager FPM;
172 if (!parseFunctionPassPipeline(FPM, PipelineText) || !PipelineText.empty())
174 MPM.addPass(createModuleToFunctionPassAdaptor(FPM));