blob: e79ac422eb24b241ab659479605cc5f84d26ec58 [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"
Chandler Carruth52eef882014-01-12 12:15:39 +000018#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000019#include "llvm/IR/PassManager.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000020#include "llvm/Support/Debug.h"
Chandler Carruth66445382014-01-11 08:16:35 +000021
22using namespace llvm;
23
24namespace {
25
Chandler Carruthd8330982014-01-12 09:34:22 +000026/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000027struct NoOpModulePass {
28 PreservedAnalyses run(Module *M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000029 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000030};
31
Chandler Carruthd8330982014-01-12 09:34:22 +000032/// \brief No-op function pass which does nothing.
33struct NoOpFunctionPass {
34 PreservedAnalyses run(Function *F) { return PreservedAnalyses::all(); }
35 static StringRef name() { return "NoOpFunctionPass"; }
36};
37
Chandler Carruth66445382014-01-11 08:16:35 +000038} // End anonymous namespace.
39
40// FIXME: Factor all of the parsing logic into a .def file that we include
41// under different macros.
42static bool isModulePassName(StringRef Name) {
43 if (Name == "no-op-module") return true;
Chandler Carruth52eef882014-01-12 12:15:39 +000044 if (Name == "print") return true;
Chandler Carruth66445382014-01-11 08:16:35 +000045
46 return false;
47}
48
Chandler Carruthd8330982014-01-12 09:34:22 +000049static bool isFunctionPassName(StringRef Name) {
50 if (Name == "no-op-function") return true;
Chandler Carruth52eef882014-01-12 12:15:39 +000051 if (Name == "print") return true;
Chandler Carruthd8330982014-01-12 09:34:22 +000052
53 return false;
54}
55
Chandler Carruth66445382014-01-11 08:16:35 +000056static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
Chandler Carruth66445382014-01-11 08:16:35 +000057 if (Name == "no-op-module") {
58 MPM.addPass(NoOpModulePass());
59 return true;
60 }
Chandler Carruth52eef882014-01-12 12:15:39 +000061 if (Name == "print") {
62 MPM.addPass(PrintModulePass(dbgs()));
63 return true;
64 }
Chandler Carruth66445382014-01-11 08:16:35 +000065 return false;
66}
67
Chandler Carruthd8330982014-01-12 09:34:22 +000068static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
Chandler Carruthd8330982014-01-12 09:34:22 +000069 if (Name == "no-op-function") {
70 FPM.addPass(NoOpFunctionPass());
71 return true;
72 }
Chandler Carruth52eef882014-01-12 12:15:39 +000073 if (Name == "print") {
74 FPM.addPass(PrintFunctionPass(dbgs()));
75 return true;
76 }
Chandler Carruthd8330982014-01-12 09:34:22 +000077 return false;
78}
79
80static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
81 StringRef &PipelineText) {
82 for (;;) {
83 // Parse nested pass managers by recursing.
84 if (PipelineText.startswith("function(")) {
85 FunctionPassManager NestedFPM;
86
87 // Parse the inner pipeline inte the nested manager.
88 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth6546cb62014-01-12 10:02:02 +000089 if (!parseFunctionPassPipeline(NestedFPM, PipelineText) ||
90 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +000091 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +000092 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +000093 PipelineText = PipelineText.substr(1);
94
95 // Add the nested pass manager with the appropriate adaptor.
96 FPM.addPass(NestedFPM);
97 } else {
98 // Otherwise try to parse a pass name.
99 size_t End = PipelineText.find_first_of(",)");
100 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
101 return false;
102
103 PipelineText = PipelineText.substr(End);
104 }
105
106 if (PipelineText.empty() || PipelineText[0] == ')')
107 return true;
108
109 assert(PipelineText[0] == ',');
110 PipelineText = PipelineText.substr(1);
111 }
112}
113
Chandler Carruth66445382014-01-11 08:16:35 +0000114static bool parseModulePassPipeline(ModulePassManager &MPM,
115 StringRef &PipelineText) {
116 for (;;) {
117 // Parse nested pass managers by recursing.
118 if (PipelineText.startswith("module(")) {
Chandler Carruth258dbb32014-01-11 12:06:47 +0000119 ModulePassManager NestedMPM;
120
121 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000122 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth6546cb62014-01-12 10:02:02 +0000123 if (!parseModulePassPipeline(NestedMPM, PipelineText) ||
124 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000125 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000126 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000127 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000128
129 // Now add the nested manager as a module pass.
130 MPM.addPass(NestedMPM);
Chandler Carruthd8330982014-01-12 09:34:22 +0000131 } else if (PipelineText.startswith("function(")) {
132 FunctionPassManager NestedFPM;
133
134 // Parse the inner pipeline inte the nested manager.
135 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth6546cb62014-01-12 10:02:02 +0000136 if (!parseFunctionPassPipeline(NestedFPM, PipelineText) ||
137 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000138 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000139 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000140 PipelineText = PipelineText.substr(1);
141
142 // Add the nested pass manager with the appropriate adaptor.
143 MPM.addPass(createModuleToFunctionPassAdaptor(NestedFPM));
Chandler Carruth66445382014-01-11 08:16:35 +0000144 } else {
145 // Otherwise try to parse a pass name.
146 size_t End = PipelineText.find_first_of(",)");
147 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
148 return false;
149
150 PipelineText = PipelineText.substr(End);
151 }
152
153 if (PipelineText.empty() || PipelineText[0] == ')')
154 return true;
155
156 assert(PipelineText[0] == ',');
157 PipelineText = PipelineText.substr(1);
158 }
159}
160
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?
164bool 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("))
Chandler Carruth6546cb62014-01-12 10:02:02 +0000167 return parseModulePassPipeline(MPM, PipelineText) && PipelineText.empty();
Chandler Carruthd8330982014-01-12 09:34:22 +0000168 if (PipelineText.startswith("function(")) {
169 FunctionPassManager FPM;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000170 if (!parseFunctionPassPipeline(FPM, PipelineText) || !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000171 return false;
172 MPM.addPass(createModuleToFunctionPassAdaptor(FPM));
173 return true;
174 }
Chandler Carruth66445382014-01-11 08:16:35 +0000175
176 // This isn't a direct pass manager name, look for the end of a pass name.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000177 StringRef FirstName =
178 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruth66445382014-01-11 08:16:35 +0000179 if (isModulePassName(FirstName))
Chandler Carruth6546cb62014-01-12 10:02:02 +0000180 return parseModulePassPipeline(MPM, PipelineText) && PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000181
Chandler Carruthd8330982014-01-12 09:34:22 +0000182 if (isFunctionPassName(FirstName)) {
183 FunctionPassManager FPM;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000184 if (!parseFunctionPassPipeline(FPM, PipelineText) || !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000185 return false;
186 MPM.addPass(createModuleToFunctionPassAdaptor(FPM));
187 return true;
188 }
Chandler Carruth66445382014-01-11 08:16:35 +0000189
190 return false;
191}