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 assert(isModulePassName(Name));
54 if (Name == "no-op-module") {
55 MPM.addPass(NoOpModulePass());
61 static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
62 assert(isFunctionPassName(Name));
63 if (Name == "no-op-function") {
64 FPM.addPass(NoOpFunctionPass());
70 static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
71 StringRef &PipelineText) {
73 // Parse nested pass managers by recursing.
74 if (PipelineText.startswith("function(")) {
75 FunctionPassManager NestedFPM;
77 // Parse the inner pipeline inte the nested manager.
78 PipelineText = PipelineText.substr(strlen("function("));
79 if (!parseFunctionPassPipeline(NestedFPM, PipelineText))
81 assert(!PipelineText.empty() && PipelineText[0] == ')');
82 PipelineText = PipelineText.substr(1);
84 // Add the nested pass manager with the appropriate adaptor.
85 FPM.addPass(NestedFPM);
87 // Otherwise try to parse a pass name.
88 size_t End = PipelineText.find_first_of(",)");
89 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
92 PipelineText = PipelineText.substr(End);
95 if (PipelineText.empty() || PipelineText[0] == ')')
98 assert(PipelineText[0] == ',');
99 PipelineText = PipelineText.substr(1);
103 static bool parseModulePassPipeline(ModulePassManager &MPM,
104 StringRef &PipelineText) {
106 // Parse nested pass managers by recursing.
107 if (PipelineText.startswith("module(")) {
108 ModulePassManager NestedMPM;
110 // Parse the inner pipeline into the nested manager.
111 PipelineText = PipelineText.substr(strlen("module("));
112 if (!parseModulePassPipeline(NestedMPM, PipelineText))
114 assert(!PipelineText.empty() && 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))
126 assert(!PipelineText.empty() && PipelineText[0] == ')');
127 PipelineText = PipelineText.substr(1);
129 // Add the nested pass manager with the appropriate adaptor.
130 MPM.addPass(createModuleToFunctionPassAdaptor(NestedFPM));
132 // Otherwise try to parse a pass name.
133 size_t End = PipelineText.find_first_of(",)");
134 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
137 PipelineText = PipelineText.substr(End);
140 if (PipelineText.empty() || PipelineText[0] == ')')
143 assert(PipelineText[0] == ',');
144 PipelineText = PipelineText.substr(1);
148 // Primary pass pipeline description parsing routine.
149 // FIXME: Should this routine accept a TargetMachine or require the caller to
150 // pre-populate the analysis managers with target-specific stuff?
151 bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText) {
152 // Look at the first entry to figure out which layer to start parsing at.
153 if (PipelineText.startswith("module("))
154 return parseModulePassPipeline(MPM, PipelineText);
155 if (PipelineText.startswith("function(")) {
156 FunctionPassManager FPM;
157 if (!parseFunctionPassPipeline(FPM, PipelineText))
159 MPM.addPass(createModuleToFunctionPassAdaptor(FPM));
163 // This isn't a direct pass manager name, look for the end of a pass name.
164 StringRef FirstName = PipelineText.substr(0, PipelineText.find_first_of(","));
165 if (isModulePassName(FirstName))
166 return parseModulePassPipeline(MPM, PipelineText);
168 if (isFunctionPassName(FirstName)) {
169 FunctionPassManager FPM;
170 if (!parseFunctionPassPipeline(FPM, PipelineText))
172 MPM.addPass(createModuleToFunctionPassAdaptor(FPM));