blob: 29be9dee94e042d1dbc48bdf727d110598cecc45 [file] [log] [blame]
Chandler Carruth66445382014-01-11 08:16:35 +00001//===- Passes.cpp - Parsing, selection, and running of passes -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10///
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.
14///
15//===----------------------------------------------------------------------===//
16
17#include "Passes.h"
18#include "llvm/IR/PassManager.h"
19
20using namespace llvm;
21
22namespace {
23
Chandler Carruthd8330982014-01-12 09:34:22 +000024/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000025struct NoOpModulePass {
26 PreservedAnalyses run(Module *M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000027 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000028};
29
Chandler Carruthd8330982014-01-12 09:34:22 +000030/// \brief No-op function pass which does nothing.
31struct NoOpFunctionPass {
32 PreservedAnalyses run(Function *F) { return PreservedAnalyses::all(); }
33 static StringRef name() { return "NoOpFunctionPass"; }
34};
35
Chandler Carruth66445382014-01-11 08:16:35 +000036} // End anonymous namespace.
37
38// FIXME: Factor all of the parsing logic into a .def file that we include
39// under different macros.
40static bool isModulePassName(StringRef Name) {
41 if (Name == "no-op-module") return true;
42
43 return false;
44}
45
Chandler Carruthd8330982014-01-12 09:34:22 +000046static bool isFunctionPassName(StringRef Name) {
47 if (Name == "no-op-function") return true;
48
49 return false;
50}
51
Chandler Carruth66445382014-01-11 08:16:35 +000052static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
Chandler Carruth66445382014-01-11 08:16:35 +000053 if (Name == "no-op-module") {
54 MPM.addPass(NoOpModulePass());
55 return true;
56 }
57 return false;
58}
59
Chandler Carruthd8330982014-01-12 09:34:22 +000060static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
Chandler Carruthd8330982014-01-12 09:34:22 +000061 if (Name == "no-op-function") {
62 FPM.addPass(NoOpFunctionPass());
63 return true;
64 }
65 return false;
66}
67
68static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
69 StringRef &PipelineText) {
70 for (;;) {
71 // Parse nested pass managers by recursing.
72 if (PipelineText.startswith("function(")) {
73 FunctionPassManager NestedFPM;
74
75 // Parse the inner pipeline inte the nested manager.
76 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth6546cb62014-01-12 10:02:02 +000077 if (!parseFunctionPassPipeline(NestedFPM, PipelineText) ||
78 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +000079 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +000080 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +000081 PipelineText = PipelineText.substr(1);
82
83 // Add the nested pass manager with the appropriate adaptor.
84 FPM.addPass(NestedFPM);
85 } else {
86 // Otherwise try to parse a pass name.
87 size_t End = PipelineText.find_first_of(",)");
88 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
89 return false;
90
91 PipelineText = PipelineText.substr(End);
92 }
93
94 if (PipelineText.empty() || PipelineText[0] == ')')
95 return true;
96
97 assert(PipelineText[0] == ',');
98 PipelineText = PipelineText.substr(1);
99 }
100}
101
Chandler Carruth66445382014-01-11 08:16:35 +0000102static bool parseModulePassPipeline(ModulePassManager &MPM,
103 StringRef &PipelineText) {
104 for (;;) {
105 // Parse nested pass managers by recursing.
106 if (PipelineText.startswith("module(")) {
Chandler Carruth258dbb32014-01-11 12:06:47 +0000107 ModulePassManager NestedMPM;
108
109 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000110 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth6546cb62014-01-12 10:02:02 +0000111 if (!parseModulePassPipeline(NestedMPM, PipelineText) ||
112 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000113 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000114 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000115 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000116
117 // Now add the nested manager as a module pass.
118 MPM.addPass(NestedMPM);
Chandler Carruthd8330982014-01-12 09:34:22 +0000119 } else if (PipelineText.startswith("function(")) {
120 FunctionPassManager NestedFPM;
121
122 // Parse the inner pipeline inte the nested manager.
123 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth6546cb62014-01-12 10:02:02 +0000124 if (!parseFunctionPassPipeline(NestedFPM, PipelineText) ||
125 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000126 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000127 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000128 PipelineText = PipelineText.substr(1);
129
130 // Add the nested pass manager with the appropriate adaptor.
131 MPM.addPass(createModuleToFunctionPassAdaptor(NestedFPM));
Chandler Carruth66445382014-01-11 08:16:35 +0000132 } else {
133 // Otherwise try to parse a pass name.
134 size_t End = PipelineText.find_first_of(",)");
135 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
136 return false;
137
138 PipelineText = PipelineText.substr(End);
139 }
140
141 if (PipelineText.empty() || PipelineText[0] == ')')
142 return true;
143
144 assert(PipelineText[0] == ',');
145 PipelineText = PipelineText.substr(1);
146 }
147}
148
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?
152bool 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("))
Chandler Carruth6546cb62014-01-12 10:02:02 +0000155 return parseModulePassPipeline(MPM, PipelineText) && PipelineText.empty();
Chandler Carruthd8330982014-01-12 09:34:22 +0000156 if (PipelineText.startswith("function(")) {
157 FunctionPassManager FPM;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000158 if (!parseFunctionPassPipeline(FPM, PipelineText) || !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000159 return false;
160 MPM.addPass(createModuleToFunctionPassAdaptor(FPM));
161 return true;
162 }
Chandler Carruth66445382014-01-11 08:16:35 +0000163
164 // This isn't a direct pass manager name, look for the end of a pass name.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000165 StringRef FirstName =
166 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruth66445382014-01-11 08:16:35 +0000167 if (isModulePassName(FirstName))
Chandler Carruth6546cb62014-01-12 10:02:02 +0000168 return parseModulePassPipeline(MPM, PipelineText) && PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000169
Chandler Carruthd8330982014-01-12 09:34:22 +0000170 if (isFunctionPassName(FirstName)) {
171 FunctionPassManager FPM;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000172 if (!parseFunctionPassPipeline(FPM, PipelineText) || !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000173 return false;
174 MPM.addPass(createModuleToFunctionPassAdaptor(FPM));
175 return true;
176 }
Chandler Carruth66445382014-01-11 08:16:35 +0000177
178 return false;
179}