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