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