blob: 4a6341d5123757fbdc2d1de65ac95aeb64cb83c2 [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) {
53 assert(isModulePassName(Name));
54 if (Name == "no-op-module") {
55 MPM.addPass(NoOpModulePass());
56 return true;
57 }
58 return false;
59}
60
Chandler Carruthd8330982014-01-12 09:34:22 +000061static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
62 assert(isFunctionPassName(Name));
63 if (Name == "no-op-function") {
64 FPM.addPass(NoOpFunctionPass());
65 return true;
66 }
67 return false;
68}
69
70static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
71 StringRef &PipelineText) {
72 for (;;) {
73 // Parse nested pass managers by recursing.
74 if (PipelineText.startswith("function(")) {
75 FunctionPassManager NestedFPM;
76
77 // Parse the inner pipeline inte the nested manager.
78 PipelineText = PipelineText.substr(strlen("function("));
79 if (!parseFunctionPassPipeline(NestedFPM, PipelineText))
80 return false;
81 assert(!PipelineText.empty() && PipelineText[0] == ')');
82 PipelineText = PipelineText.substr(1);
83
84 // Add the nested pass manager with the appropriate adaptor.
85 FPM.addPass(NestedFPM);
86 } else {
87 // Otherwise try to parse a pass name.
88 size_t End = PipelineText.find_first_of(",)");
89 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
90 return false;
91
92 PipelineText = PipelineText.substr(End);
93 }
94
95 if (PipelineText.empty() || PipelineText[0] == ')')
96 return true;
97
98 assert(PipelineText[0] == ',');
99 PipelineText = PipelineText.substr(1);
100 }
101}
102
Chandler Carruth66445382014-01-11 08:16:35 +0000103static bool parseModulePassPipeline(ModulePassManager &MPM,
104 StringRef &PipelineText) {
105 for (;;) {
106 // Parse nested pass managers by recursing.
107 if (PipelineText.startswith("module(")) {
Chandler Carruth258dbb32014-01-11 12:06:47 +0000108 ModulePassManager NestedMPM;
109
110 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000111 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth258dbb32014-01-11 12:06:47 +0000112 if (!parseModulePassPipeline(NestedMPM, PipelineText))
Chandler Carruth66445382014-01-11 08:16:35 +0000113 return false;
114 assert(!PipelineText.empty() && PipelineText[0] == ')');
115 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("));
124 if (!parseFunctionPassPipeline(NestedFPM, PipelineText))
125 return false;
126 assert(!PipelineText.empty() && PipelineText[0] == ')');
127 PipelineText = PipelineText.substr(1);
128
129 // Add the nested pass manager with the appropriate adaptor.
130 MPM.addPass(createModuleToFunctionPassAdaptor(NestedFPM));
Chandler Carruth66445382014-01-11 08:16:35 +0000131 } else {
132 // Otherwise try to parse a pass name.
133 size_t End = PipelineText.find_first_of(",)");
134 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
135 return false;
136
137 PipelineText = PipelineText.substr(End);
138 }
139
140 if (PipelineText.empty() || PipelineText[0] == ')')
141 return true;
142
143 assert(PipelineText[0] == ',');
144 PipelineText = PipelineText.substr(1);
145 }
146}
147
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?
151bool 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);
Chandler Carruthd8330982014-01-12 09:34:22 +0000155 if (PipelineText.startswith("function(")) {
156 FunctionPassManager FPM;
157 if (!parseFunctionPassPipeline(FPM, PipelineText))
158 return false;
159 MPM.addPass(createModuleToFunctionPassAdaptor(FPM));
160 return true;
161 }
Chandler Carruth66445382014-01-11 08:16:35 +0000162
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);
167
Chandler Carruthd8330982014-01-12 09:34:22 +0000168 if (isFunctionPassName(FirstName)) {
169 FunctionPassManager FPM;
170 if (!parseFunctionPassPipeline(FPM, PipelineText))
171 return false;
172 MPM.addPass(createModuleToFunctionPassAdaptor(FPM));
173 return true;
174 }
Chandler Carruth66445382014-01-11 08:16:35 +0000175
176 return false;
177}