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