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