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