blob: ffdf9bfec1ffa3dc14b81f806d55e49ceb2d9377 [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 Carruthbf71a342014-02-06 04:37:03 +000018#include "llvm/Analysis/LazyCallGraph.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000019#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000020#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000021#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000022#include "llvm/Support/Debug.h"
Chandler Carruth66445382014-01-11 08:16:35 +000023
24using namespace llvm;
25
26namespace {
27
Chandler Carruthd8330982014-01-12 09:34:22 +000028/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000029struct NoOpModulePass {
30 PreservedAnalyses run(Module *M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000031 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000032};
33
Chandler Carruthd8330982014-01-12 09:34:22 +000034/// \brief No-op function pass which does nothing.
35struct NoOpFunctionPass {
36 PreservedAnalyses run(Function *F) { return PreservedAnalyses::all(); }
37 static StringRef name() { return "NoOpFunctionPass"; }
38};
39
Chandler Carruth66445382014-01-11 08:16:35 +000040} // End anonymous namespace.
41
42// FIXME: Factor all of the parsing logic into a .def file that we include
43// under different macros.
44static bool isModulePassName(StringRef Name) {
45 if (Name == "no-op-module") return true;
Chandler Carruth52eef882014-01-12 12:15:39 +000046 if (Name == "print") return true;
Chandler Carruthbf71a342014-02-06 04:37:03 +000047 if (Name == "print-cg") return true;
Chandler Carruth66445382014-01-11 08:16:35 +000048
49 return false;
50}
51
Chandler Carruthd8330982014-01-12 09:34:22 +000052static bool isFunctionPassName(StringRef Name) {
53 if (Name == "no-op-function") return true;
Chandler Carruth52eef882014-01-12 12:15:39 +000054 if (Name == "print") return true;
Chandler Carruthd8330982014-01-12 09:34:22 +000055
56 return false;
57}
58
Chandler Carruth66445382014-01-11 08:16:35 +000059static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
Chandler Carruth66445382014-01-11 08:16:35 +000060 if (Name == "no-op-module") {
61 MPM.addPass(NoOpModulePass());
62 return true;
63 }
Chandler Carruth52eef882014-01-12 12:15:39 +000064 if (Name == "print") {
65 MPM.addPass(PrintModulePass(dbgs()));
66 return true;
67 }
Chandler Carruthbf71a342014-02-06 04:37:03 +000068 if (Name == "print-cg") {
69 MPM.addPass(LazyCallGraphPrinterPass(dbgs()));
70 return true;
71 }
Chandler Carruth66445382014-01-11 08:16:35 +000072 return false;
73}
74
Chandler Carruthd8330982014-01-12 09:34:22 +000075static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
Chandler Carruthd8330982014-01-12 09:34:22 +000076 if (Name == "no-op-function") {
77 FPM.addPass(NoOpFunctionPass());
78 return true;
79 }
Chandler Carruth52eef882014-01-12 12:15:39 +000080 if (Name == "print") {
81 FPM.addPass(PrintFunctionPass(dbgs()));
82 return true;
83 }
Chandler Carruthd8330982014-01-12 09:34:22 +000084 return false;
85}
86
87static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth4d356312014-01-20 11:34:08 +000088 StringRef &PipelineText,
89 bool VerifyEachPass) {
Chandler Carruthd8330982014-01-12 09:34:22 +000090 for (;;) {
91 // Parse nested pass managers by recursing.
92 if (PipelineText.startswith("function(")) {
93 FunctionPassManager NestedFPM;
94
95 // Parse the inner pipeline inte the nested manager.
96 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth4d356312014-01-20 11:34:08 +000097 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +000098 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +000099 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000100 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000101 PipelineText = PipelineText.substr(1);
102
103 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000104 FPM.addPass(std::move(NestedFPM));
Chandler Carruthd8330982014-01-12 09:34:22 +0000105 } else {
106 // Otherwise try to parse a pass name.
107 size_t End = PipelineText.find_first_of(",)");
108 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
109 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000110 if (VerifyEachPass)
111 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000112
113 PipelineText = PipelineText.substr(End);
114 }
115
116 if (PipelineText.empty() || PipelineText[0] == ')')
117 return true;
118
119 assert(PipelineText[0] == ',');
120 PipelineText = PipelineText.substr(1);
121 }
122}
123
Chandler Carruth66445382014-01-11 08:16:35 +0000124static bool parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth4d356312014-01-20 11:34:08 +0000125 StringRef &PipelineText,
126 bool VerifyEachPass) {
Chandler Carruth66445382014-01-11 08:16:35 +0000127 for (;;) {
128 // Parse nested pass managers by recursing.
129 if (PipelineText.startswith("module(")) {
Chandler Carruth258dbb32014-01-11 12:06:47 +0000130 ModulePassManager NestedMPM;
131
132 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000133 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth4d356312014-01-20 11:34:08 +0000134 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000135 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000136 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000137 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000138 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000139
140 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000141 MPM.addPass(std::move(NestedMPM));
Chandler Carruthd8330982014-01-12 09:34:22 +0000142 } else if (PipelineText.startswith("function(")) {
143 FunctionPassManager NestedFPM;
144
145 // Parse the inner pipeline inte the nested manager.
146 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth4d356312014-01-20 11:34:08 +0000147 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000148 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000149 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000150 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000151 PipelineText = PipelineText.substr(1);
152
153 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000154 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000155 } else {
156 // Otherwise try to parse a pass name.
157 size_t End = PipelineText.find_first_of(",)");
158 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
159 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000160 if (VerifyEachPass)
161 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000162
163 PipelineText = PipelineText.substr(End);
164 }
165
166 if (PipelineText.empty() || PipelineText[0] == ')')
167 return true;
168
169 assert(PipelineText[0] == ',');
170 PipelineText = PipelineText.substr(1);
171 }
172}
173
174// Primary pass pipeline description parsing routine.
175// FIXME: Should this routine accept a TargetMachine or require the caller to
176// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth4d356312014-01-20 11:34:08 +0000177bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText,
178 bool VerifyEachPass) {
Chandler Carruth66445382014-01-11 08:16:35 +0000179 // Look at the first entry to figure out which layer to start parsing at.
180 if (PipelineText.startswith("module("))
Chandler Carruth4d356312014-01-20 11:34:08 +0000181 return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
182 PipelineText.empty();
Chandler Carruthd8330982014-01-12 09:34:22 +0000183 if (PipelineText.startswith("function(")) {
184 FunctionPassManager FPM;
Chandler Carruth4d356312014-01-20 11:34:08 +0000185 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
186 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000187 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000188 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000189 return true;
190 }
Chandler Carruth66445382014-01-11 08:16:35 +0000191
192 // This isn't a direct pass manager name, look for the end of a pass name.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000193 StringRef FirstName =
194 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruth66445382014-01-11 08:16:35 +0000195 if (isModulePassName(FirstName))
Chandler Carruth4d356312014-01-20 11:34:08 +0000196 return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
197 PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000198
Chandler Carruthd8330982014-01-12 09:34:22 +0000199 if (isFunctionPassName(FirstName)) {
200 FunctionPassManager FPM;
Chandler Carruth4d356312014-01-20 11:34:08 +0000201 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
202 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000203 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000204 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000205 return true;
206 }
Chandler Carruth66445382014-01-11 08:16:35 +0000207
208 return false;
209}