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