[PM] Add a collection of no-op analysis passes and switch the new pass
[oota-llvm.git] / tools / opt / Passes.cpp
1 //===- 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/Analysis/CGSCCPassManager.h"
19 #include "llvm/Analysis/LazyCallGraph.h"
20 #include "llvm/IR/IRPrintingPasses.h"
21 #include "llvm/IR/PassManager.h"
22 #include "llvm/IR/Verifier.h"
23 #include "llvm/Support/Debug.h"
24
25 using namespace llvm;
26
27 namespace {
28
29 /// \brief No-op module pass which does nothing.
30 struct NoOpModulePass {
31   PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
32   static StringRef name() { return "NoOpModulePass"; }
33 };
34
35 /// \brief No-op module analysis.
36 struct NoOpModuleAnalysis {
37   struct Result {};
38   Result run(Module &) { return Result(); }
39   static StringRef name() { return "NoOpModuleAnalysis"; }
40   static void *ID() { return (void *)&PassID; }
41 private:
42   static char PassID;
43 };
44
45 char NoOpModuleAnalysis::PassID;
46
47 /// \brief No-op CGSCC pass which does nothing.
48 struct NoOpCGSCCPass {
49   PreservedAnalyses run(LazyCallGraph::SCC &C) {
50     return PreservedAnalyses::all();
51   }
52   static StringRef name() { return "NoOpCGSCCPass"; }
53 };
54
55 /// \brief No-op CGSCC analysis.
56 struct NoOpCGSCCAnalysis {
57   struct Result {};
58   Result run(LazyCallGraph::SCC &) { return Result(); }
59   static StringRef name() { return "NoOpCGSCCAnalysis"; }
60   static void *ID() { return (void *)&PassID; }
61 private:
62   static char PassID;
63 };
64
65 char NoOpCGSCCAnalysis::PassID;
66
67 /// \brief No-op function pass which does nothing.
68 struct NoOpFunctionPass {
69   PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
70   static StringRef name() { return "NoOpFunctionPass"; }
71 };
72
73 /// \brief No-op function analysis.
74 struct NoOpFunctionAnalysis {
75   struct Result {};
76   Result run(Function &) { return Result(); }
77   static StringRef name() { return "NoOpFunctionAnalysis"; }
78   static void *ID() { return (void *)&PassID; }
79 private:
80   static char PassID;
81 };
82
83 char NoOpFunctionAnalysis::PassID;
84
85 } // End anonymous namespace.
86
87 void llvm::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
88 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
89   MAM.registerPass(CREATE_PASS);
90 #include "PassRegistry.def"
91 }
92
93 void llvm::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
94 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
95   CGAM.registerPass(CREATE_PASS);
96 #include "PassRegistry.def"
97 }
98
99 void llvm::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
100 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
101   FAM.registerPass(CREATE_PASS);
102 #include "PassRegistry.def"
103 }
104
105 static bool isModulePassName(StringRef Name) {
106 #define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
107 #include "PassRegistry.def"
108
109   // We also support building a require pass around any analysis.
110 #define MODULE_ANALYSIS(NAME, CREATE_PASS)                                     \
111   if (Name == "require<" NAME ">")                                             \
112     return true;
113 #include "PassRegistry.def"
114
115   return false;
116 }
117
118 static bool isCGSCCPassName(StringRef Name) {
119 #define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
120 #include "PassRegistry.def"
121
122   // We also support building a require pass around any analysis.
123 #define CGSCC_ANALYSIS(NAME, CREATE_PASS)                                      \
124   if (Name == "require<" NAME ">")                                             \
125     return true;
126 #include "PassRegistry.def"
127
128   return false;
129 }
130
131 static bool isFunctionPassName(StringRef Name) {
132 #define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
133 #include "PassRegistry.def"
134
135   // We also support building a require pass around any analysis.
136 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)                                   \
137   if (Name == "require<" NAME ">")                                             \
138     return true;
139 #include "PassRegistry.def"
140
141   return false;
142 }
143
144 static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
145 #define MODULE_PASS(NAME, CREATE_PASS)                                         \
146   if (Name == NAME) {                                                          \
147     MPM.addPass(CREATE_PASS);                                                  \
148     return true;                                                               \
149   }
150 #include "PassRegistry.def"
151
152   // We also support building a require pass around any analysis.
153 #define MODULE_ANALYSIS(NAME, CREATE_PASS)                                     \
154   if (Name == "require<" NAME ">") {                                           \
155     MPM.addPass(NoopAnalysisRequirementPass<decltype(CREATE_PASS)>());         \
156     return true;                                                               \
157   }
158 #include "PassRegistry.def"
159
160   return false;
161 }
162
163 static bool parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
164 #define CGSCC_PASS(NAME, CREATE_PASS)                                          \
165   if (Name == NAME) {                                                          \
166     CGPM.addPass(CREATE_PASS);                                                 \
167     return true;                                                               \
168   }
169 #include "PassRegistry.def"
170
171   // We also support building a require pass around any analysis.
172 #define CGSCC_ANALYSIS(NAME, CREATE_PASS)                                      \
173   if (Name == "require<" NAME ">") {                                           \
174     CGPM.addPass(NoopAnalysisRequirementPass<decltype(CREATE_PASS)>());        \
175     return true;                                                               \
176   }
177 #include "PassRegistry.def"
178
179   return false;
180 }
181
182 static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
183 #define FUNCTION_PASS(NAME, CREATE_PASS)                                       \
184   if (Name == NAME) {                                                          \
185     FPM.addPass(CREATE_PASS);                                                  \
186     return true;                                                               \
187   }
188 #include "PassRegistry.def"
189
190   // We also support building a require pass around any analysis.
191 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)                                   \
192   if (Name == "require<" NAME ">") {                                           \
193     FPM.addPass(NoopAnalysisRequirementPass<decltype(CREATE_PASS)>());         \
194     return true;                                                               \
195   }
196 #include "PassRegistry.def"
197
198   return false;
199 }
200
201 static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
202                                       StringRef &PipelineText,
203                                       bool VerifyEachPass) {
204   for (;;) {
205     // Parse nested pass managers by recursing.
206     if (PipelineText.startswith("function(")) {
207       FunctionPassManager NestedFPM;
208
209       // Parse the inner pipeline inte the nested manager.
210       PipelineText = PipelineText.substr(strlen("function("));
211       if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
212           PipelineText.empty())
213         return false;
214       assert(PipelineText[0] == ')');
215       PipelineText = PipelineText.substr(1);
216
217       // Add the nested pass manager with the appropriate adaptor.
218       FPM.addPass(std::move(NestedFPM));
219     } else {
220       // Otherwise try to parse a pass name.
221       size_t End = PipelineText.find_first_of(",)");
222       if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
223         return false;
224       if (VerifyEachPass)
225         FPM.addPass(VerifierPass());
226
227       PipelineText = PipelineText.substr(End);
228     }
229
230     if (PipelineText.empty() || PipelineText[0] == ')')
231       return true;
232
233     assert(PipelineText[0] == ',');
234     PipelineText = PipelineText.substr(1);
235   }
236 }
237
238 static bool parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
239                                       StringRef &PipelineText,
240                                       bool VerifyEachPass) {
241   for (;;) {
242     // Parse nested pass managers by recursing.
243     if (PipelineText.startswith("cgscc(")) {
244       CGSCCPassManager NestedCGPM;
245
246       // Parse the inner pipeline into the nested manager.
247       PipelineText = PipelineText.substr(strlen("cgscc("));
248       if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass) ||
249           PipelineText.empty())
250         return false;
251       assert(PipelineText[0] == ')');
252       PipelineText = PipelineText.substr(1);
253
254       // Add the nested pass manager with the appropriate adaptor.
255       CGPM.addPass(std::move(NestedCGPM));
256     } else if (PipelineText.startswith("function(")) {
257       FunctionPassManager NestedFPM;
258
259       // Parse the inner pipeline inte the nested manager.
260       PipelineText = PipelineText.substr(strlen("function("));
261       if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
262           PipelineText.empty())
263         return false;
264       assert(PipelineText[0] == ')');
265       PipelineText = PipelineText.substr(1);
266
267       // Add the nested pass manager with the appropriate adaptor.
268       CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
269     } else {
270       // Otherwise try to parse a pass name.
271       size_t End = PipelineText.find_first_of(",)");
272       if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
273         return false;
274       // FIXME: No verifier support for CGSCC passes!
275
276       PipelineText = PipelineText.substr(End);
277     }
278
279     if (PipelineText.empty() || PipelineText[0] == ')')
280       return true;
281
282     assert(PipelineText[0] == ',');
283     PipelineText = PipelineText.substr(1);
284   }
285 }
286
287 static bool parseModulePassPipeline(ModulePassManager &MPM,
288                                     StringRef &PipelineText,
289                                     bool VerifyEachPass) {
290   for (;;) {
291     // Parse nested pass managers by recursing.
292     if (PipelineText.startswith("module(")) {
293       ModulePassManager NestedMPM;
294
295       // Parse the inner pipeline into the nested manager.
296       PipelineText = PipelineText.substr(strlen("module("));
297       if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass) ||
298           PipelineText.empty())
299         return false;
300       assert(PipelineText[0] == ')');
301       PipelineText = PipelineText.substr(1);
302
303       // Now add the nested manager as a module pass.
304       MPM.addPass(std::move(NestedMPM));
305     } else if (PipelineText.startswith("cgscc(")) {
306       CGSCCPassManager NestedCGPM;
307
308       // Parse the inner pipeline inte the nested manager.
309       PipelineText = PipelineText.substr(strlen("cgscc("));
310       if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass) ||
311           PipelineText.empty())
312         return false;
313       assert(PipelineText[0] == ')');
314       PipelineText = PipelineText.substr(1);
315
316       // Add the nested pass manager with the appropriate adaptor.
317       MPM.addPass(
318           createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
319     } else if (PipelineText.startswith("function(")) {
320       FunctionPassManager NestedFPM;
321
322       // Parse the inner pipeline inte the nested manager.
323       PipelineText = PipelineText.substr(strlen("function("));
324       if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
325           PipelineText.empty())
326         return false;
327       assert(PipelineText[0] == ')');
328       PipelineText = PipelineText.substr(1);
329
330       // Add the nested pass manager with the appropriate adaptor.
331       MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
332     } else {
333       // Otherwise try to parse a pass name.
334       size_t End = PipelineText.find_first_of(",)");
335       if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
336         return false;
337       if (VerifyEachPass)
338         MPM.addPass(VerifierPass());
339
340       PipelineText = PipelineText.substr(End);
341     }
342
343     if (PipelineText.empty() || PipelineText[0] == ')')
344       return true;
345
346     assert(PipelineText[0] == ',');
347     PipelineText = PipelineText.substr(1);
348   }
349 }
350
351 // Primary pass pipeline description parsing routine.
352 // FIXME: Should this routine accept a TargetMachine or require the caller to
353 // pre-populate the analysis managers with target-specific stuff?
354 bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText,
355                              bool VerifyEachPass) {
356   // Look at the first entry to figure out which layer to start parsing at.
357   if (PipelineText.startswith("module("))
358     return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
359            PipelineText.empty();
360   if (PipelineText.startswith("cgscc(")) {
361     CGSCCPassManager CGPM;
362     if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass) ||
363         !PipelineText.empty())
364       return false;
365     MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
366     return true;
367   }
368   if (PipelineText.startswith("function(")) {
369     FunctionPassManager FPM;
370     if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
371         !PipelineText.empty())
372       return false;
373     MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
374     return true;
375   }
376
377   // This isn't a direct pass manager name, look for the end of a pass name.
378   StringRef FirstName =
379       PipelineText.substr(0, PipelineText.find_first_of(",)"));
380   if (isModulePassName(FirstName))
381     return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
382            PipelineText.empty();
383
384   if (isCGSCCPassName(FirstName)) {
385     CGSCCPassManager CGPM;
386     if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass) ||
387         !PipelineText.empty())
388       return false;
389     MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
390     return true;
391   }
392
393   if (isFunctionPassName(FirstName)) {
394     FunctionPassManager FPM;
395     if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
396         !PipelineText.empty())
397       return false;
398     MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
399     return true;
400   }
401
402   return false;
403 }