[PM] Simplify how we use the registry by including it only once. Still
[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 #define MODULE_ANALYSIS(NAME, CREATE_PASS)                                     \
108   if (Name == "require<" NAME ">")                                             \
109     return true;
110 #include "PassRegistry.def"
111
112   return false;
113 }
114
115 static bool isCGSCCPassName(StringRef Name) {
116 #define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
117 #define CGSCC_ANALYSIS(NAME, CREATE_PASS)                                      \
118   if (Name == "require<" NAME ">")                                             \
119     return true;
120 #include "PassRegistry.def"
121
122   return false;
123 }
124
125 static bool isFunctionPassName(StringRef Name) {
126 #define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
127 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)                                   \
128   if (Name == "require<" NAME ">")                                             \
129     return true;
130 #include "PassRegistry.def"
131
132   return false;
133 }
134
135 static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
136 #define MODULE_PASS(NAME, CREATE_PASS)                                         \
137   if (Name == NAME) {                                                          \
138     MPM.addPass(CREATE_PASS);                                                  \
139     return true;                                                               \
140   }
141 #define MODULE_ANALYSIS(NAME, CREATE_PASS)                                     \
142   if (Name == "require<" NAME ">") {                                           \
143     MPM.addPass(NoopAnalysisRequirementPass<decltype(CREATE_PASS)>());         \
144     return true;                                                               \
145   }
146 #include "PassRegistry.def"
147
148   return false;
149 }
150
151 static bool parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
152 #define CGSCC_PASS(NAME, CREATE_PASS)                                          \
153   if (Name == NAME) {                                                          \
154     CGPM.addPass(CREATE_PASS);                                                 \
155     return true;                                                               \
156   }
157 #define CGSCC_ANALYSIS(NAME, CREATE_PASS)                                      \
158   if (Name == "require<" NAME ">") {                                           \
159     CGPM.addPass(NoopAnalysisRequirementPass<decltype(CREATE_PASS)>());        \
160     return true;                                                               \
161   }
162 #include "PassRegistry.def"
163
164   return false;
165 }
166
167 static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
168 #define FUNCTION_PASS(NAME, CREATE_PASS)                                       \
169   if (Name == NAME) {                                                          \
170     FPM.addPass(CREATE_PASS);                                                  \
171     return true;                                                               \
172   }
173 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)                                   \
174   if (Name == "require<" NAME ">") {                                           \
175     FPM.addPass(NoopAnalysisRequirementPass<decltype(CREATE_PASS)>());         \
176     return true;                                                               \
177   }
178 #include "PassRegistry.def"
179
180   return false;
181 }
182
183 static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
184                                       StringRef &PipelineText,
185                                       bool VerifyEachPass) {
186   for (;;) {
187     // Parse nested pass managers by recursing.
188     if (PipelineText.startswith("function(")) {
189       FunctionPassManager NestedFPM;
190
191       // Parse the inner pipeline inte the nested manager.
192       PipelineText = PipelineText.substr(strlen("function("));
193       if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
194           PipelineText.empty())
195         return false;
196       assert(PipelineText[0] == ')');
197       PipelineText = PipelineText.substr(1);
198
199       // Add the nested pass manager with the appropriate adaptor.
200       FPM.addPass(std::move(NestedFPM));
201     } else {
202       // Otherwise try to parse a pass name.
203       size_t End = PipelineText.find_first_of(",)");
204       if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
205         return false;
206       if (VerifyEachPass)
207         FPM.addPass(VerifierPass());
208
209       PipelineText = PipelineText.substr(End);
210     }
211
212     if (PipelineText.empty() || PipelineText[0] == ')')
213       return true;
214
215     assert(PipelineText[0] == ',');
216     PipelineText = PipelineText.substr(1);
217   }
218 }
219
220 static bool parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
221                                       StringRef &PipelineText,
222                                       bool VerifyEachPass) {
223   for (;;) {
224     // Parse nested pass managers by recursing.
225     if (PipelineText.startswith("cgscc(")) {
226       CGSCCPassManager NestedCGPM;
227
228       // Parse the inner pipeline into the nested manager.
229       PipelineText = PipelineText.substr(strlen("cgscc("));
230       if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass) ||
231           PipelineText.empty())
232         return false;
233       assert(PipelineText[0] == ')');
234       PipelineText = PipelineText.substr(1);
235
236       // Add the nested pass manager with the appropriate adaptor.
237       CGPM.addPass(std::move(NestedCGPM));
238     } else if (PipelineText.startswith("function(")) {
239       FunctionPassManager NestedFPM;
240
241       // Parse the inner pipeline inte the nested manager.
242       PipelineText = PipelineText.substr(strlen("function("));
243       if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
244           PipelineText.empty())
245         return false;
246       assert(PipelineText[0] == ')');
247       PipelineText = PipelineText.substr(1);
248
249       // Add the nested pass manager with the appropriate adaptor.
250       CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
251     } else {
252       // Otherwise try to parse a pass name.
253       size_t End = PipelineText.find_first_of(",)");
254       if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
255         return false;
256       // FIXME: No verifier support for CGSCC passes!
257
258       PipelineText = PipelineText.substr(End);
259     }
260
261     if (PipelineText.empty() || PipelineText[0] == ')')
262       return true;
263
264     assert(PipelineText[0] == ',');
265     PipelineText = PipelineText.substr(1);
266   }
267 }
268
269 static bool parseModulePassPipeline(ModulePassManager &MPM,
270                                     StringRef &PipelineText,
271                                     bool VerifyEachPass) {
272   for (;;) {
273     // Parse nested pass managers by recursing.
274     if (PipelineText.startswith("module(")) {
275       ModulePassManager NestedMPM;
276
277       // Parse the inner pipeline into the nested manager.
278       PipelineText = PipelineText.substr(strlen("module("));
279       if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass) ||
280           PipelineText.empty())
281         return false;
282       assert(PipelineText[0] == ')');
283       PipelineText = PipelineText.substr(1);
284
285       // Now add the nested manager as a module pass.
286       MPM.addPass(std::move(NestedMPM));
287     } else if (PipelineText.startswith("cgscc(")) {
288       CGSCCPassManager NestedCGPM;
289
290       // Parse the inner pipeline inte the nested manager.
291       PipelineText = PipelineText.substr(strlen("cgscc("));
292       if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass) ||
293           PipelineText.empty())
294         return false;
295       assert(PipelineText[0] == ')');
296       PipelineText = PipelineText.substr(1);
297
298       // Add the nested pass manager with the appropriate adaptor.
299       MPM.addPass(
300           createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
301     } else if (PipelineText.startswith("function(")) {
302       FunctionPassManager NestedFPM;
303
304       // Parse the inner pipeline inte the nested manager.
305       PipelineText = PipelineText.substr(strlen("function("));
306       if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
307           PipelineText.empty())
308         return false;
309       assert(PipelineText[0] == ')');
310       PipelineText = PipelineText.substr(1);
311
312       // Add the nested pass manager with the appropriate adaptor.
313       MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
314     } else {
315       // Otherwise try to parse a pass name.
316       size_t End = PipelineText.find_first_of(",)");
317       if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
318         return false;
319       if (VerifyEachPass)
320         MPM.addPass(VerifierPass());
321
322       PipelineText = PipelineText.substr(End);
323     }
324
325     if (PipelineText.empty() || PipelineText[0] == ')')
326       return true;
327
328     assert(PipelineText[0] == ',');
329     PipelineText = PipelineText.substr(1);
330   }
331 }
332
333 // Primary pass pipeline description parsing routine.
334 // FIXME: Should this routine accept a TargetMachine or require the caller to
335 // pre-populate the analysis managers with target-specific stuff?
336 bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText,
337                              bool VerifyEachPass) {
338   // Look at the first entry to figure out which layer to start parsing at.
339   if (PipelineText.startswith("module("))
340     return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
341            PipelineText.empty();
342   if (PipelineText.startswith("cgscc(")) {
343     CGSCCPassManager CGPM;
344     if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass) ||
345         !PipelineText.empty())
346       return false;
347     MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
348     return true;
349   }
350   if (PipelineText.startswith("function(")) {
351     FunctionPassManager FPM;
352     if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
353         !PipelineText.empty())
354       return false;
355     MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
356     return true;
357   }
358
359   // This isn't a direct pass manager name, look for the end of a pass name.
360   StringRef FirstName =
361       PipelineText.substr(0, PipelineText.find_first_of(",)"));
362   if (isModulePassName(FirstName))
363     return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
364            PipelineText.empty();
365
366   if (isCGSCCPassName(FirstName)) {
367     CGSCCPassManager CGPM;
368     if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass) ||
369         !PipelineText.empty())
370       return false;
371     MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
372     return true;
373   }
374
375   if (isFunctionPassName(FirstName)) {
376     FunctionPassManager FPM;
377     if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
378         !PipelineText.empty())
379       return false;
380     MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
381     return true;
382   }
383
384   return false;
385 }