[PM] Hide a function we only use in an assert behind NDEBUG.
[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(NoopAnalysisRequirementPass<decltype(CREATE_PASS)>());         \
146     return true;                                                               \
147   }                                                                            \
148   if (Name == "invalidate<" NAME ">") {                                        \
149     MPM.addPass(NoopAnalysisInvalidationPass<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(NoopAnalysisRequirementPass<decltype(CREATE_PASS)>());        \
166     return true;                                                               \
167   }                                                                            \
168   if (Name == "invalidate<" NAME ">") {                                        \
169     CGPM.addPass(NoopAnalysisInvalidationPass<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(NoopAnalysisRequirementPass<decltype(CREATE_PASS)>());         \
186     return true;                                                               \
187   }                                                                            \
188   if (Name == "invalidate<" NAME ">") {                                        \
189     FPM.addPass(NoopAnalysisInvalidationPass<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) {
200   for (;;) {
201     // Parse nested pass managers by recursing.
202     if (PipelineText.startswith("function(")) {
203       FunctionPassManager NestedFPM;
204
205       // Parse the inner pipeline inte the nested manager.
206       PipelineText = PipelineText.substr(strlen("function("));
207       if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
208           PipelineText.empty())
209         return false;
210       assert(PipelineText[0] == ')');
211       PipelineText = PipelineText.substr(1);
212
213       // Add the nested pass manager with the appropriate adaptor.
214       FPM.addPass(std::move(NestedFPM));
215     } else {
216       // Otherwise try to parse a pass name.
217       size_t End = PipelineText.find_first_of(",)");
218       if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
219         return false;
220       if (VerifyEachPass)
221         FPM.addPass(VerifierPass());
222
223       PipelineText = PipelineText.substr(End);
224     }
225
226     if (PipelineText.empty() || PipelineText[0] == ')')
227       return true;
228
229     assert(PipelineText[0] == ',');
230     PipelineText = PipelineText.substr(1);
231   }
232 }
233
234 static bool parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
235                                       StringRef &PipelineText,
236                                       bool VerifyEachPass) {
237   for (;;) {
238     // Parse nested pass managers by recursing.
239     if (PipelineText.startswith("cgscc(")) {
240       CGSCCPassManager NestedCGPM;
241
242       // Parse the inner pipeline into the nested manager.
243       PipelineText = PipelineText.substr(strlen("cgscc("));
244       if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass) ||
245           PipelineText.empty())
246         return false;
247       assert(PipelineText[0] == ')');
248       PipelineText = PipelineText.substr(1);
249
250       // Add the nested pass manager with the appropriate adaptor.
251       CGPM.addPass(std::move(NestedCGPM));
252     } else if (PipelineText.startswith("function(")) {
253       FunctionPassManager NestedFPM;
254
255       // Parse the inner pipeline inte the nested manager.
256       PipelineText = PipelineText.substr(strlen("function("));
257       if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
258           PipelineText.empty())
259         return false;
260       assert(PipelineText[0] == ')');
261       PipelineText = PipelineText.substr(1);
262
263       // Add the nested pass manager with the appropriate adaptor.
264       CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
265     } else {
266       // Otherwise try to parse a pass name.
267       size_t End = PipelineText.find_first_of(",)");
268       if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
269         return false;
270       // FIXME: No verifier support for CGSCC passes!
271
272       PipelineText = PipelineText.substr(End);
273     }
274
275     if (PipelineText.empty() || PipelineText[0] == ')')
276       return true;
277
278     assert(PipelineText[0] == ',');
279     PipelineText = PipelineText.substr(1);
280   }
281 }
282
283 static bool parseModulePassPipeline(ModulePassManager &MPM,
284                                     StringRef &PipelineText,
285                                     bool VerifyEachPass) {
286   for (;;) {
287     // Parse nested pass managers by recursing.
288     if (PipelineText.startswith("module(")) {
289       ModulePassManager NestedMPM;
290
291       // Parse the inner pipeline into the nested manager.
292       PipelineText = PipelineText.substr(strlen("module("));
293       if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass) ||
294           PipelineText.empty())
295         return false;
296       assert(PipelineText[0] == ')');
297       PipelineText = PipelineText.substr(1);
298
299       // Now add the nested manager as a module pass.
300       MPM.addPass(std::move(NestedMPM));
301     } else if (PipelineText.startswith("cgscc(")) {
302       CGSCCPassManager NestedCGPM;
303
304       // Parse the inner pipeline inte the nested manager.
305       PipelineText = PipelineText.substr(strlen("cgscc("));
306       if (!parseCGSCCPassPipeline(NestedCGPM, 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(
314           createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
315     } else if (PipelineText.startswith("function(")) {
316       FunctionPassManager NestedFPM;
317
318       // Parse the inner pipeline inte the nested manager.
319       PipelineText = PipelineText.substr(strlen("function("));
320       if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
321           PipelineText.empty())
322         return false;
323       assert(PipelineText[0] == ')');
324       PipelineText = PipelineText.substr(1);
325
326       // Add the nested pass manager with the appropriate adaptor.
327       MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
328     } else {
329       // Otherwise try to parse a pass name.
330       size_t End = PipelineText.find_first_of(",)");
331       if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
332         return false;
333       if (VerifyEachPass)
334         MPM.addPass(VerifierPass());
335
336       PipelineText = PipelineText.substr(End);
337     }
338
339     if (PipelineText.empty() || PipelineText[0] == ')')
340       return true;
341
342     assert(PipelineText[0] == ',');
343     PipelineText = PipelineText.substr(1);
344   }
345 }
346
347 // Primary pass pipeline description parsing routine.
348 // FIXME: Should this routine accept a TargetMachine or require the caller to
349 // pre-populate the analysis managers with target-specific stuff?
350 bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText,
351                              bool VerifyEachPass) {
352   // By default, try to parse the pipeline as-if it were within an implicit
353   // 'module(...)' pass pipeline. If this will parse at all, it needs to
354   // consume the entire string.
355   if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass))
356     return PipelineText.empty();
357
358   // This isn't parsable as a module pipeline, look for the end of a pass name
359   // and directly drop down to that layer.
360   StringRef FirstName =
361       PipelineText.substr(0, PipelineText.find_first_of(",)"));
362   assert(!isModulePassName(FirstName) &&
363          "Already handled all module pipeline options.");
364
365   // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
366   // pipeline.
367   if (isCGSCCPassName(FirstName)) {
368     CGSCCPassManager CGPM;
369     if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass) ||
370         !PipelineText.empty())
371       return false;
372     MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
373     return true;
374   }
375
376   // Similarly, if this looks like a Function pass, parse the whole thing as
377   // a Function pipelien.
378   if (isFunctionPassName(FirstName)) {
379     FunctionPassManager FPM;
380     if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
381         !PipelineText.empty())
382       return false;
383     MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
384     return true;
385   }
386
387   return false;
388 }