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