Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/IR/PassManager.cpp - PassManager tests ---------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Chandler Carruth | 9a67b07 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 9 | #include "llvm/IR/PassManager.h" |
Chandler Carruth | 9aca918 | 2014-01-07 12:34:26 +0000 | [diff] [blame] | 10 | #include "llvm/AsmParser/Parser.h" |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 11 | #include "llvm/IR/Function.h" |
| 12 | #include "llvm/IR/LLVMContext.h" |
| 13 | #include "llvm/IR/Module.h" |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 14 | #include "llvm/Support/SourceMgr.h" |
| 15 | #include "gtest/gtest.h" |
| 16 | |
| 17 | using namespace llvm; |
| 18 | |
| 19 | namespace { |
| 20 | |
Chandler Carruth | 30a0730 | 2016-03-11 10:33:22 +0000 | [diff] [blame] | 21 | class TestFunctionAnalysis : public AnalysisInfoMixin<TestFunctionAnalysis> { |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 22 | public: |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 23 | struct Result { |
| 24 | Result(int Count) : InstructionCount(Count) {} |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 25 | int InstructionCount; |
| 26 | }; |
| 27 | |
Chandler Carruth | 2ad1858 | 2013-11-23 01:25:02 +0000 | [diff] [blame] | 28 | TestFunctionAnalysis(int &Runs) : Runs(Runs) {} |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 29 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 30 | /// Run the analysis pass over the function and return a result. |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 31 | Result run(Function &F, FunctionAnalysisManager &AM) { |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 32 | ++Runs; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 33 | int Count = 0; |
Chandler Carruth | d174ce4 | 2015-01-05 02:47:05 +0000 | [diff] [blame] | 34 | for (Function::iterator BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI) |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 35 | for (BasicBlock::iterator II = BBI->begin(), IE = BBI->end(); II != IE; |
| 36 | ++II) |
| 37 | ++Count; |
| 38 | return Result(Count); |
| 39 | } |
| 40 | |
| 41 | private: |
Chandler Carruth | 30a0730 | 2016-03-11 10:33:22 +0000 | [diff] [blame] | 42 | friend AnalysisInfoMixin<TestFunctionAnalysis>; |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 43 | static AnalysisKey Key; |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 44 | |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 45 | int &Runs; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 48 | AnalysisKey TestFunctionAnalysis::Key; |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 49 | |
Chandler Carruth | 30a0730 | 2016-03-11 10:33:22 +0000 | [diff] [blame] | 50 | class TestModuleAnalysis : public AnalysisInfoMixin<TestModuleAnalysis> { |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 51 | public: |
| 52 | struct Result { |
| 53 | Result(int Count) : FunctionCount(Count) {} |
| 54 | int FunctionCount; |
| 55 | }; |
| 56 | |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 57 | TestModuleAnalysis(int &Runs) : Runs(Runs) {} |
| 58 | |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 59 | Result run(Module &M, ModuleAnalysisManager &AM) { |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 60 | ++Runs; |
| 61 | int Count = 0; |
Chandler Carruth | d174ce4 | 2015-01-05 02:47:05 +0000 | [diff] [blame] | 62 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 63 | ++Count; |
| 64 | return Result(Count); |
| 65 | } |
| 66 | |
| 67 | private: |
Chandler Carruth | 30a0730 | 2016-03-11 10:33:22 +0000 | [diff] [blame] | 68 | friend AnalysisInfoMixin<TestModuleAnalysis>; |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 69 | static AnalysisKey Key; |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 70 | |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 71 | int &Runs; |
| 72 | }; |
| 73 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 74 | AnalysisKey TestModuleAnalysis::Key; |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 75 | |
Chandler Carruth | 30a0730 | 2016-03-11 10:33:22 +0000 | [diff] [blame] | 76 | struct TestModulePass : PassInfoMixin<TestModulePass> { |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 77 | TestModulePass(int &RunCount) : RunCount(RunCount) {} |
| 78 | |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 79 | PreservedAnalyses run(Module &M, ModuleAnalysisManager &) { |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 80 | ++RunCount; |
Chandler Carruth | c0bfa8c | 2013-11-20 11:31:50 +0000 | [diff] [blame] | 81 | return PreservedAnalyses::none(); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | int &RunCount; |
| 85 | }; |
| 86 | |
Chandler Carruth | 30a0730 | 2016-03-11 10:33:22 +0000 | [diff] [blame] | 87 | struct TestPreservingModulePass : PassInfoMixin<TestPreservingModulePass> { |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 88 | PreservedAnalyses run(Module &M, ModuleAnalysisManager &) { |
| 89 | return PreservedAnalyses::all(); |
| 90 | } |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
Chandler Carruth | 30a0730 | 2016-03-11 10:33:22 +0000 | [diff] [blame] | 93 | struct TestFunctionPass : PassInfoMixin<TestFunctionPass> { |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 94 | TestFunctionPass(int &RunCount, int &AnalyzedInstrCount, |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 95 | int &AnalyzedFunctionCount, |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 96 | bool OnlyUseCachedResults = false) |
| 97 | : RunCount(RunCount), AnalyzedInstrCount(AnalyzedInstrCount), |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 98 | AnalyzedFunctionCount(AnalyzedFunctionCount), |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 99 | OnlyUseCachedResults(OnlyUseCachedResults) {} |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 100 | |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 101 | PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) { |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 102 | ++RunCount; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 103 | |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 104 | const ModuleAnalysisManager &MAM = |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 105 | AM.getResult<ModuleAnalysisManagerFunctionProxy>(F).getManager(); |
Chandler Carruth | eedf9fc | 2014-02-05 21:41:42 +0000 | [diff] [blame] | 106 | if (TestModuleAnalysis::Result *TMA = |
Chandler Carruth | d174ce4 | 2015-01-05 02:47:05 +0000 | [diff] [blame] | 107 | MAM.getCachedResult<TestModuleAnalysis>(*F.getParent())) |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 108 | AnalyzedFunctionCount += TMA->FunctionCount; |
| 109 | |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 110 | if (OnlyUseCachedResults) { |
| 111 | // Hack to force the use of the cached interface. |
Chandler Carruth | eedf9fc | 2014-02-05 21:41:42 +0000 | [diff] [blame] | 112 | if (TestFunctionAnalysis::Result *AR = |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 113 | AM.getCachedResult<TestFunctionAnalysis>(F)) |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 114 | AnalyzedInstrCount += AR->InstructionCount; |
| 115 | } else { |
| 116 | // Typical path just runs the analysis as needed. |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 117 | TestFunctionAnalysis::Result &AR = AM.getResult<TestFunctionAnalysis>(F); |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 118 | AnalyzedInstrCount += AR.InstructionCount; |
| 119 | } |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 120 | |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 121 | return PreservedAnalyses::all(); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | int &RunCount; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 125 | int &AnalyzedInstrCount; |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 126 | int &AnalyzedFunctionCount; |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 127 | bool OnlyUseCachedResults; |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
Chandler Carruth | bceeb22 | 2013-11-22 23:38:07 +0000 | [diff] [blame] | 130 | // A test function pass that invalidates all function analyses for a function |
| 131 | // with a specific name. |
Chandler Carruth | 30a0730 | 2016-03-11 10:33:22 +0000 | [diff] [blame] | 132 | struct TestInvalidationFunctionPass |
| 133 | : PassInfoMixin<TestInvalidationFunctionPass> { |
Chandler Carruth | bceeb22 | 2013-11-22 23:38:07 +0000 | [diff] [blame] | 134 | TestInvalidationFunctionPass(StringRef FunctionName) : Name(FunctionName) {} |
| 135 | |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 136 | PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) { |
Chandler Carruth | d174ce4 | 2015-01-05 02:47:05 +0000 | [diff] [blame] | 137 | return F.getName() == Name ? PreservedAnalyses::none() |
| 138 | : PreservedAnalyses::all(); |
Chandler Carruth | bceeb22 | 2013-11-22 23:38:07 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | StringRef Name; |
| 142 | }; |
| 143 | |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 144 | std::unique_ptr<Module> parseIR(LLVMContext &Context, const char *IR) { |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 145 | SMDiagnostic Err; |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 146 | return parseAssemblyString(IR, Err, Context); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | class PassManagerTest : public ::testing::Test { |
| 150 | protected: |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 151 | LLVMContext Context; |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 152 | std::unique_ptr<Module> M; |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 153 | |
| 154 | public: |
| 155 | PassManagerTest() |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 156 | : M(parseIR(Context, "define void @f() {\n" |
| 157 | "entry:\n" |
| 158 | " call void @g()\n" |
| 159 | " call void @h()\n" |
| 160 | " ret void\n" |
| 161 | "}\n" |
| 162 | "define void @g() {\n" |
| 163 | " ret void\n" |
| 164 | "}\n" |
| 165 | "define void @h() {\n" |
| 166 | " ret void\n" |
| 167 | "}\n")) {} |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 170 | TEST(PreservedAnalysesTest, Basic) { |
Chandler Carruth | 999b92d | 2014-03-13 10:42:18 +0000 | [diff] [blame] | 171 | PreservedAnalyses PA1 = PreservedAnalyses(); |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 172 | { |
| 173 | auto PAC = PA1.getChecker<TestFunctionAnalysis>(); |
| 174 | EXPECT_FALSE(PAC.preserved()); |
| 175 | EXPECT_FALSE(PAC.preservedSet<AllAnalysesOn<Function>>()); |
| 176 | } |
| 177 | { |
| 178 | auto PAC = PA1.getChecker<TestModuleAnalysis>(); |
| 179 | EXPECT_FALSE(PAC.preserved()); |
| 180 | EXPECT_FALSE(PAC.preservedSet<AllAnalysesOn<Module>>()); |
| 181 | } |
| 182 | auto PA2 = PreservedAnalyses::none(); |
| 183 | { |
| 184 | auto PAC = PA2.getChecker<TestFunctionAnalysis>(); |
| 185 | EXPECT_FALSE(PAC.preserved()); |
| 186 | EXPECT_FALSE(PAC.preservedSet<AllAnalysesOn<Function>>()); |
| 187 | } |
| 188 | auto PA3 = PreservedAnalyses::all(); |
| 189 | { |
| 190 | auto PAC = PA3.getChecker<TestFunctionAnalysis>(); |
| 191 | EXPECT_TRUE(PAC.preserved()); |
| 192 | EXPECT_TRUE(PAC.preservedSet<AllAnalysesOn<Function>>()); |
| 193 | } |
Chandler Carruth | 999b92d | 2014-03-13 10:42:18 +0000 | [diff] [blame] | 194 | PreservedAnalyses PA4 = PA1; |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 195 | { |
| 196 | auto PAC = PA4.getChecker<TestFunctionAnalysis>(); |
| 197 | EXPECT_FALSE(PAC.preserved()); |
| 198 | EXPECT_FALSE(PAC.preservedSet<AllAnalysesOn<Function>>()); |
| 199 | } |
Chandler Carruth | 999b92d | 2014-03-13 10:42:18 +0000 | [diff] [blame] | 200 | PA4 = PA3; |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 201 | { |
| 202 | auto PAC = PA4.getChecker<TestFunctionAnalysis>(); |
| 203 | EXPECT_TRUE(PAC.preserved()); |
| 204 | EXPECT_TRUE(PAC.preservedSet<AllAnalysesOn<Function>>()); |
| 205 | } |
Chandler Carruth | 999b92d | 2014-03-13 10:42:18 +0000 | [diff] [blame] | 206 | PA4 = std::move(PA2); |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 207 | { |
| 208 | auto PAC = PA4.getChecker<TestFunctionAnalysis>(); |
| 209 | EXPECT_FALSE(PAC.preserved()); |
| 210 | EXPECT_FALSE(PAC.preservedSet<AllAnalysesOn<Function>>()); |
| 211 | } |
Chandler Carruth | 9c35065 | 2017-07-09 07:23:27 +0000 | [diff] [blame] | 212 | auto PA5 = PreservedAnalyses::allInSet<AllAnalysesOn<Function>>(); |
| 213 | { |
| 214 | auto PAC = PA5.getChecker<TestFunctionAnalysis>(); |
| 215 | EXPECT_FALSE(PAC.preserved()); |
| 216 | EXPECT_TRUE(PAC.preservedSet<AllAnalysesOn<Function>>()); |
| 217 | EXPECT_FALSE(PAC.preservedSet<AllAnalysesOn<Module>>()); |
| 218 | } |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | TEST(PreservedAnalysesTest, Preserve) { |
| 222 | auto PA = PreservedAnalyses::none(); |
| 223 | PA.preserve<TestFunctionAnalysis>(); |
| 224 | EXPECT_TRUE(PA.getChecker<TestFunctionAnalysis>().preserved()); |
| 225 | EXPECT_FALSE(PA.getChecker<TestModuleAnalysis>().preserved()); |
| 226 | PA.preserve<TestModuleAnalysis>(); |
| 227 | EXPECT_TRUE(PA.getChecker<TestFunctionAnalysis>().preserved()); |
| 228 | EXPECT_TRUE(PA.getChecker<TestModuleAnalysis>().preserved()); |
| 229 | |
| 230 | // Redundant calls are fine. |
| 231 | PA.preserve<TestFunctionAnalysis>(); |
| 232 | EXPECT_TRUE(PA.getChecker<TestFunctionAnalysis>().preserved()); |
| 233 | EXPECT_TRUE(PA.getChecker<TestModuleAnalysis>().preserved()); |
| 234 | } |
| 235 | |
| 236 | TEST(PreservedAnalysesTest, PreserveSets) { |
| 237 | auto PA = PreservedAnalyses::none(); |
| 238 | PA.preserveSet<AllAnalysesOn<Function>>(); |
| 239 | EXPECT_TRUE(PA.getChecker<TestFunctionAnalysis>() |
| 240 | .preservedSet<AllAnalysesOn<Function>>()); |
| 241 | EXPECT_FALSE(PA.getChecker<TestModuleAnalysis>() |
| 242 | .preservedSet<AllAnalysesOn<Module>>()); |
| 243 | PA.preserveSet<AllAnalysesOn<Module>>(); |
| 244 | EXPECT_TRUE(PA.getChecker<TestFunctionAnalysis>() |
| 245 | .preservedSet<AllAnalysesOn<Function>>()); |
| 246 | EXPECT_TRUE(PA.getChecker<TestModuleAnalysis>() |
| 247 | .preservedSet<AllAnalysesOn<Module>>()); |
| 248 | |
| 249 | // Mixing is fine. |
| 250 | PA.preserve<TestFunctionAnalysis>(); |
| 251 | EXPECT_TRUE(PA.getChecker<TestFunctionAnalysis>() |
| 252 | .preservedSet<AllAnalysesOn<Function>>()); |
| 253 | EXPECT_TRUE(PA.getChecker<TestModuleAnalysis>() |
| 254 | .preservedSet<AllAnalysesOn<Module>>()); |
| 255 | |
| 256 | // Redundant calls are fine. |
| 257 | PA.preserveSet<AllAnalysesOn<Module>>(); |
| 258 | EXPECT_TRUE(PA.getChecker<TestFunctionAnalysis>() |
| 259 | .preservedSet<AllAnalysesOn<Function>>()); |
| 260 | EXPECT_TRUE(PA.getChecker<TestModuleAnalysis>() |
| 261 | .preservedSet<AllAnalysesOn<Module>>()); |
| 262 | } |
| 263 | |
| 264 | TEST(PreservedAnalysisTest, Intersect) { |
| 265 | // Setup the initial sets. |
| 266 | auto PA1 = PreservedAnalyses::none(); |
Chandler Carruth | 999b92d | 2014-03-13 10:42:18 +0000 | [diff] [blame] | 267 | PA1.preserve<TestFunctionAnalysis>(); |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 268 | PA1.preserveSet<AllAnalysesOn<Module>>(); |
| 269 | auto PA2 = PreservedAnalyses::none(); |
| 270 | PA2.preserve<TestFunctionAnalysis>(); |
| 271 | PA2.preserveSet<AllAnalysesOn<Function>>(); |
| 272 | PA2.preserve<TestModuleAnalysis>(); |
| 273 | PA2.preserveSet<AllAnalysesOn<Module>>(); |
| 274 | auto PA3 = PreservedAnalyses::none(); |
| 275 | PA3.preserve<TestModuleAnalysis>(); |
| 276 | PA3.preserveSet<AllAnalysesOn<Function>>(); |
| 277 | |
| 278 | // Self intersection is a no-op. |
| 279 | auto Intersected = PA1; |
| 280 | Intersected.intersect(PA1); |
| 281 | EXPECT_TRUE(Intersected.getChecker<TestFunctionAnalysis>().preserved()); |
| 282 | EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>() |
| 283 | .preservedSet<AllAnalysesOn<Function>>()); |
| 284 | EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved()); |
| 285 | EXPECT_TRUE(Intersected.getChecker<TestModuleAnalysis>() |
| 286 | .preservedSet<AllAnalysesOn<Module>>()); |
| 287 | |
| 288 | // Intersecting with all is a no-op. |
| 289 | Intersected.intersect(PreservedAnalyses::all()); |
| 290 | EXPECT_TRUE(Intersected.getChecker<TestFunctionAnalysis>().preserved()); |
| 291 | EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>() |
| 292 | .preservedSet<AllAnalysesOn<Function>>()); |
| 293 | EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved()); |
| 294 | EXPECT_TRUE(Intersected.getChecker<TestModuleAnalysis>() |
| 295 | .preservedSet<AllAnalysesOn<Module>>()); |
| 296 | |
| 297 | // Intersecting a narrow set with a more broad set is the narrow set. |
| 298 | Intersected.intersect(PA2); |
| 299 | EXPECT_TRUE(Intersected.getChecker<TestFunctionAnalysis>().preserved()); |
| 300 | EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>() |
| 301 | .preservedSet<AllAnalysesOn<Function>>()); |
| 302 | EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved()); |
| 303 | EXPECT_TRUE(Intersected.getChecker<TestModuleAnalysis>() |
| 304 | .preservedSet<AllAnalysesOn<Module>>()); |
| 305 | |
| 306 | // Intersecting a broad set with a more narrow set is the narrow set. |
| 307 | Intersected = PA2; |
| 308 | Intersected.intersect(PA1); |
| 309 | EXPECT_TRUE(Intersected.getChecker<TestFunctionAnalysis>().preserved()); |
| 310 | EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>() |
| 311 | .preservedSet<AllAnalysesOn<Function>>()); |
| 312 | EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved()); |
| 313 | EXPECT_TRUE(Intersected.getChecker<TestModuleAnalysis>() |
| 314 | .preservedSet<AllAnalysesOn<Module>>()); |
| 315 | |
| 316 | // Intersecting with empty clears. |
| 317 | Intersected.intersect(PreservedAnalyses::none()); |
| 318 | EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>().preserved()); |
| 319 | EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>() |
| 320 | .preservedSet<AllAnalysesOn<Function>>()); |
| 321 | EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved()); |
| 322 | EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>() |
| 323 | .preservedSet<AllAnalysesOn<Module>>()); |
| 324 | |
| 325 | // Intersecting non-overlapping clears. |
| 326 | Intersected = PA1; |
| 327 | Intersected.intersect(PA3); |
| 328 | EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>().preserved()); |
| 329 | EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>() |
| 330 | .preservedSet<AllAnalysesOn<Function>>()); |
| 331 | EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved()); |
| 332 | EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>() |
| 333 | .preservedSet<AllAnalysesOn<Module>>()); |
| 334 | |
| 335 | // Intersecting with moves works in when there is storage on both sides. |
| 336 | Intersected = PA1; |
| 337 | auto Tmp = PA2; |
| 338 | Intersected.intersect(std::move(Tmp)); |
| 339 | EXPECT_TRUE(Intersected.getChecker<TestFunctionAnalysis>().preserved()); |
| 340 | EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>() |
| 341 | .preservedSet<AllAnalysesOn<Function>>()); |
| 342 | EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved()); |
| 343 | EXPECT_TRUE(Intersected.getChecker<TestModuleAnalysis>() |
| 344 | .preservedSet<AllAnalysesOn<Module>>()); |
| 345 | |
| 346 | // Intersecting with move works for incoming all and existing all. |
| 347 | auto Tmp2 = PreservedAnalyses::all(); |
| 348 | Intersected.intersect(std::move(Tmp2)); |
| 349 | EXPECT_TRUE(Intersected.getChecker<TestFunctionAnalysis>().preserved()); |
| 350 | EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>() |
| 351 | .preservedSet<AllAnalysesOn<Function>>()); |
| 352 | EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved()); |
| 353 | EXPECT_TRUE(Intersected.getChecker<TestModuleAnalysis>() |
| 354 | .preservedSet<AllAnalysesOn<Module>>()); |
| 355 | Intersected = PreservedAnalyses::all(); |
| 356 | auto Tmp3 = PA1; |
| 357 | Intersected.intersect(std::move(Tmp3)); |
| 358 | EXPECT_TRUE(Intersected.getChecker<TestFunctionAnalysis>().preserved()); |
| 359 | EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>() |
| 360 | .preservedSet<AllAnalysesOn<Function>>()); |
| 361 | EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved()); |
| 362 | EXPECT_TRUE(Intersected.getChecker<TestModuleAnalysis>() |
| 363 | .preservedSet<AllAnalysesOn<Module>>()); |
| 364 | } |
| 365 | |
| 366 | TEST(PreservedAnalysisTest, Abandon) { |
| 367 | auto PA = PreservedAnalyses::none(); |
| 368 | |
| 369 | // We can abandon things after they are preserved. |
| 370 | PA.preserve<TestFunctionAnalysis>(); |
| 371 | PA.abandon<TestFunctionAnalysis>(); |
| 372 | EXPECT_FALSE(PA.getChecker<TestFunctionAnalysis>().preserved()); |
| 373 | |
| 374 | // Repeated is fine, and abandoning if they were never preserved is fine. |
| 375 | PA.abandon<TestFunctionAnalysis>(); |
| 376 | EXPECT_FALSE(PA.getChecker<TestFunctionAnalysis>().preserved()); |
| 377 | PA.abandon<TestModuleAnalysis>(); |
| 378 | EXPECT_FALSE(PA.getChecker<TestModuleAnalysis>().preserved()); |
| 379 | |
| 380 | // Even if the sets are preserved, the abandoned analyses' checker won't |
| 381 | // return true for those sets. |
| 382 | PA.preserveSet<AllAnalysesOn<Function>>(); |
| 383 | PA.preserveSet<AllAnalysesOn<Module>>(); |
| 384 | EXPECT_FALSE(PA.getChecker<TestFunctionAnalysis>() |
| 385 | .preservedSet<AllAnalysesOn<Function>>()); |
| 386 | EXPECT_FALSE(PA.getChecker<TestModuleAnalysis>() |
| 387 | .preservedSet<AllAnalysesOn<Module>>()); |
| 388 | |
| 389 | // But an arbitrary (opaque) analysis will still observe the sets as |
| 390 | // preserved. This also checks that we can use an explicit ID rather than |
| 391 | // a type. |
| 392 | AnalysisKey FakeKey, *FakeID = &FakeKey; |
| 393 | EXPECT_TRUE(PA.getChecker(FakeID).preservedSet<AllAnalysesOn<Function>>()); |
| 394 | EXPECT_TRUE(PA.getChecker(FakeID).preservedSet<AllAnalysesOn<Module>>()); |
Chandler Carruth | 999b92d | 2014-03-13 10:42:18 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 397 | TEST_F(PassManagerTest, Basic) { |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 398 | FunctionAnalysisManager FAM(/*DebugLogging*/ true); |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 399 | int FunctionAnalysisRuns = 0; |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 400 | FAM.registerPass([&] { return TestFunctionAnalysis(FunctionAnalysisRuns); }); |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 401 | |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 402 | ModuleAnalysisManager MAM(/*DebugLogging*/ true); |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 403 | int ModuleAnalysisRuns = 0; |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 404 | MAM.registerPass([&] { return TestModuleAnalysis(ModuleAnalysisRuns); }); |
| 405 | MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); }); |
| 406 | FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); }); |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 407 | |
Fedor Sergeev | ee8d31c | 2018-09-20 17:08:45 +0000 | [diff] [blame] | 408 | MAM.registerPass([&] { return PassInstrumentationAnalysis(); }); |
| 409 | FAM.registerPass([&] { return PassInstrumentationAnalysis(); }); |
| 410 | |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 411 | ModulePassManager MPM; |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 412 | |
| 413 | // Count the runs over a Function. |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 414 | int FunctionPassRunCount1 = 0; |
| 415 | int AnalyzedInstrCount1 = 0; |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 416 | int AnalyzedFunctionCount1 = 0; |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 417 | { |
Chandler Carruth | 999b92d | 2014-03-13 10:42:18 +0000 | [diff] [blame] | 418 | // Pointless scoped copy to test move assignment. |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 419 | ModulePassManager NestedMPM(/*DebugLogging*/ true); |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 420 | FunctionPassManager FPM; |
Chandler Carruth | 999b92d | 2014-03-13 10:42:18 +0000 | [diff] [blame] | 421 | { |
| 422 | // Pointless scope to test move assignment. |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 423 | FunctionPassManager NestedFPM(/*DebugLogging*/ true); |
Chandler Carruth | 74a8a22 | 2016-06-17 07:15:29 +0000 | [diff] [blame] | 424 | NestedFPM.addPass(TestFunctionPass( |
| 425 | FunctionPassRunCount1, AnalyzedInstrCount1, AnalyzedFunctionCount1)); |
Chandler Carruth | 999b92d | 2014-03-13 10:42:18 +0000 | [diff] [blame] | 426 | FPM = std::move(NestedFPM); |
| 427 | } |
| 428 | NestedMPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 429 | MPM = std::move(NestedMPM); |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 430 | } |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 431 | |
| 432 | // Count the runs over a module. |
| 433 | int ModulePassRunCount = 0; |
| 434 | MPM.addPass(TestModulePass(ModulePassRunCount)); |
| 435 | |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 436 | // Count the runs over a Function in a separate manager. |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 437 | int FunctionPassRunCount2 = 0; |
| 438 | int AnalyzedInstrCount2 = 0; |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 439 | int AnalyzedFunctionCount2 = 0; |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 440 | { |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 441 | FunctionPassManager FPM(/*DebugLogging*/ true); |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 442 | FPM.addPass(TestFunctionPass(FunctionPassRunCount2, AnalyzedInstrCount2, |
| 443 | AnalyzedFunctionCount2)); |
| 444 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 445 | } |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 446 | |
Chandler Carruth | bceeb22 | 2013-11-22 23:38:07 +0000 | [diff] [blame] | 447 | // A third function pass manager but with only preserving intervening passes |
| 448 | // and with a function pass that invalidates exactly one analysis. |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 449 | MPM.addPass(TestPreservingModulePass()); |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 450 | int FunctionPassRunCount3 = 0; |
| 451 | int AnalyzedInstrCount3 = 0; |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 452 | int AnalyzedFunctionCount3 = 0; |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 453 | { |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 454 | FunctionPassManager FPM(/*DebugLogging*/ true); |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 455 | FPM.addPass(TestFunctionPass(FunctionPassRunCount3, AnalyzedInstrCount3, |
| 456 | AnalyzedFunctionCount3)); |
| 457 | FPM.addPass(TestInvalidationFunctionPass("f")); |
| 458 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 459 | } |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 460 | |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 461 | // A fourth function pass manager but with only preserving intervening |
| 462 | // passes but triggering the module analysis. |
| 463 | MPM.addPass(RequireAnalysisPass<TestModuleAnalysis, Module>()); |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 464 | int FunctionPassRunCount4 = 0; |
| 465 | int AnalyzedInstrCount4 = 0; |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 466 | int AnalyzedFunctionCount4 = 0; |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 467 | { |
| 468 | FunctionPassManager FPM; |
| 469 | FPM.addPass(TestFunctionPass(FunctionPassRunCount4, AnalyzedInstrCount4, |
| 470 | AnalyzedFunctionCount4)); |
| 471 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 472 | } |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 473 | |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 474 | // A fifth function pass manager which invalidates one function first but |
| 475 | // uses only cached results. |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 476 | int FunctionPassRunCount5 = 0; |
| 477 | int AnalyzedInstrCount5 = 0; |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 478 | int AnalyzedFunctionCount5 = 0; |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 479 | { |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 480 | FunctionPassManager FPM(/*DebugLogging*/ true); |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 481 | FPM.addPass(TestInvalidationFunctionPass("f")); |
| 482 | FPM.addPass(TestFunctionPass(FunctionPassRunCount5, AnalyzedInstrCount5, |
| 483 | AnalyzedFunctionCount5, |
| 484 | /*OnlyUseCachedResults=*/true)); |
| 485 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 486 | } |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 487 | |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 488 | MPM.run(*M, MAM); |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 489 | |
| 490 | // Validate module pass counters. |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 491 | EXPECT_EQ(1, ModulePassRunCount); |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 492 | |
Chandler Carruth | bceeb22 | 2013-11-22 23:38:07 +0000 | [diff] [blame] | 493 | // Validate all function pass counter sets are the same. |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 494 | EXPECT_EQ(3, FunctionPassRunCount1); |
| 495 | EXPECT_EQ(5, AnalyzedInstrCount1); |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 496 | EXPECT_EQ(0, AnalyzedFunctionCount1); |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 497 | EXPECT_EQ(3, FunctionPassRunCount2); |
| 498 | EXPECT_EQ(5, AnalyzedInstrCount2); |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 499 | EXPECT_EQ(0, AnalyzedFunctionCount2); |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 500 | EXPECT_EQ(3, FunctionPassRunCount3); |
| 501 | EXPECT_EQ(5, AnalyzedInstrCount3); |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 502 | EXPECT_EQ(0, AnalyzedFunctionCount3); |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 503 | EXPECT_EQ(3, FunctionPassRunCount4); |
| 504 | EXPECT_EQ(5, AnalyzedInstrCount4); |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 505 | EXPECT_EQ(9, AnalyzedFunctionCount4); |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 506 | EXPECT_EQ(3, FunctionPassRunCount5); |
| 507 | EXPECT_EQ(2, AnalyzedInstrCount5); // Only 'g' and 'h' were cached. |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 508 | EXPECT_EQ(9, AnalyzedFunctionCount5); |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 509 | |
Chandler Carruth | bceeb22 | 2013-11-22 23:38:07 +0000 | [diff] [blame] | 510 | // Validate the analysis counters: |
| 511 | // first run over 3 functions, then module pass invalidates |
| 512 | // second run over 3 functions, nothing invalidates |
| 513 | // third run over 0 functions, but 1 function invalidated |
| 514 | // fourth run over 1 function |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 515 | // fifth run invalidates 1 function first, but runs over 0 functions |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 516 | EXPECT_EQ(7, FunctionAnalysisRuns); |
| 517 | |
| 518 | EXPECT_EQ(1, ModuleAnalysisRuns); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 519 | } |
Chandler Carruth | 9b35e6d | 2016-08-19 18:36:06 +0000 | [diff] [blame] | 520 | |
| 521 | // A customized pass manager that passes extra arguments through the |
| 522 | // infrastructure. |
| 523 | typedef AnalysisManager<Function, int> CustomizedAnalysisManager; |
| 524 | typedef PassManager<Function, CustomizedAnalysisManager, int, int &> |
| 525 | CustomizedPassManager; |
| 526 | |
| 527 | class CustomizedAnalysis : public AnalysisInfoMixin<CustomizedAnalysis> { |
| 528 | public: |
| 529 | struct Result { |
| 530 | Result(int I) : I(I) {} |
| 531 | int I; |
| 532 | }; |
| 533 | |
| 534 | Result run(Function &F, CustomizedAnalysisManager &AM, int I) { |
| 535 | return Result(I); |
| 536 | } |
| 537 | |
| 538 | private: |
| 539 | friend AnalysisInfoMixin<CustomizedAnalysis>; |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 540 | static AnalysisKey Key; |
Chandler Carruth | 9b35e6d | 2016-08-19 18:36:06 +0000 | [diff] [blame] | 541 | }; |
| 542 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 543 | AnalysisKey CustomizedAnalysis::Key; |
Chandler Carruth | 9b35e6d | 2016-08-19 18:36:06 +0000 | [diff] [blame] | 544 | |
| 545 | struct CustomizedPass : PassInfoMixin<CustomizedPass> { |
| 546 | std::function<void(CustomizedAnalysis::Result &, int &)> Callback; |
| 547 | |
| 548 | template <typename CallbackT> |
| 549 | CustomizedPass(CallbackT Callback) : Callback(Callback) {} |
| 550 | |
| 551 | PreservedAnalyses run(Function &F, CustomizedAnalysisManager &AM, int I, |
| 552 | int &O) { |
| 553 | Callback(AM.getResult<CustomizedAnalysis>(F, I), O); |
| 554 | return PreservedAnalyses::none(); |
| 555 | } |
| 556 | }; |
| 557 | |
| 558 | TEST_F(PassManagerTest, CustomizedPassManagerArgs) { |
| 559 | CustomizedAnalysisManager AM; |
| 560 | AM.registerPass([&] { return CustomizedAnalysis(); }); |
Fedor Sergeev | ee8d31c | 2018-09-20 17:08:45 +0000 | [diff] [blame] | 561 | PassInstrumentationCallbacks PIC; |
| 562 | AM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); }); |
Chandler Carruth | 9b35e6d | 2016-08-19 18:36:06 +0000 | [diff] [blame] | 563 | |
| 564 | CustomizedPassManager PM; |
| 565 | |
| 566 | // Add an instance of the customized pass that just accumulates the input |
| 567 | // after it is round-tripped through the analysis. |
| 568 | int Result = 0; |
| 569 | PM.addPass( |
| 570 | CustomizedPass([](CustomizedAnalysis::Result &R, int &O) { O += R.I; })); |
| 571 | |
| 572 | // Run this over every function with the input of 42. |
| 573 | for (Function &F : *M) |
| 574 | PM.run(F, AM, 42, Result); |
| 575 | |
| 576 | // And ensure that we accumulated the correct result. |
| 577 | EXPECT_EQ(42 * (int)M->size(), Result); |
| 578 | } |
Chandler Carruth | 3ab2a5a | 2016-11-28 22:04:31 +0000 | [diff] [blame] | 579 | |
| 580 | /// A test analysis pass which caches in its result another analysis pass and |
| 581 | /// uses it to serve queries. This requires the result to invalidate itself |
| 582 | /// when its dependency is invalidated. |
| 583 | struct TestIndirectFunctionAnalysis |
| 584 | : public AnalysisInfoMixin<TestIndirectFunctionAnalysis> { |
| 585 | struct Result { |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 586 | Result(TestFunctionAnalysis::Result &FDep, TestModuleAnalysis::Result &MDep) |
| 587 | : FDep(FDep), MDep(MDep) {} |
| 588 | TestFunctionAnalysis::Result &FDep; |
| 589 | TestModuleAnalysis::Result &MDep; |
Chandler Carruth | 3ab2a5a | 2016-11-28 22:04:31 +0000 | [diff] [blame] | 590 | |
| 591 | bool invalidate(Function &F, const PreservedAnalyses &PA, |
| 592 | FunctionAnalysisManager::Invalidator &Inv) { |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 593 | auto PAC = PA.getChecker<TestIndirectFunctionAnalysis>(); |
| 594 | return !(PAC.preserved() || |
| 595 | PAC.preservedSet<AllAnalysesOn<Function>>()) || |
Chandler Carruth | 3ab2a5a | 2016-11-28 22:04:31 +0000 | [diff] [blame] | 596 | Inv.invalidate<TestFunctionAnalysis>(F, PA); |
| 597 | } |
| 598 | }; |
| 599 | |
| 600 | TestIndirectFunctionAnalysis(int &Runs) : Runs(Runs) {} |
| 601 | |
| 602 | /// Run the analysis pass over the function and return a result. |
| 603 | Result run(Function &F, FunctionAnalysisManager &AM) { |
| 604 | ++Runs; |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 605 | auto &FDep = AM.getResult<TestFunctionAnalysis>(F); |
| 606 | auto &Proxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F); |
| 607 | const ModuleAnalysisManager &MAM = Proxy.getManager(); |
| 608 | // For the test, we insist that the module analysis starts off in the |
| 609 | // cache. |
| 610 | auto &MDep = *MAM.getCachedResult<TestModuleAnalysis>(*F.getParent()); |
| 611 | // And register the dependency as module analysis dependencies have to be |
| 612 | // pre-registered on the proxy. |
| 613 | Proxy.registerOuterAnalysisInvalidation<TestModuleAnalysis, |
| 614 | TestIndirectFunctionAnalysis>(); |
| 615 | return Result(FDep, MDep); |
Chandler Carruth | 3ab2a5a | 2016-11-28 22:04:31 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | private: |
| 619 | friend AnalysisInfoMixin<TestIndirectFunctionAnalysis>; |
| 620 | static AnalysisKey Key; |
| 621 | |
| 622 | int &Runs; |
| 623 | }; |
| 624 | |
| 625 | AnalysisKey TestIndirectFunctionAnalysis::Key; |
| 626 | |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 627 | /// A test analysis pass which chaches in its result the result from the above |
| 628 | /// indirect analysis pass. |
| 629 | /// |
| 630 | /// This allows us to ensure that whenever an analysis pass is invalidated due |
| 631 | /// to dependencies (especially dependencies across IR units that trigger |
| 632 | /// asynchronous invalidation) we correctly detect that this may in turn cause |
| 633 | /// other analysis to be invalidated. |
| 634 | struct TestDoublyIndirectFunctionAnalysis |
| 635 | : public AnalysisInfoMixin<TestDoublyIndirectFunctionAnalysis> { |
| 636 | struct Result { |
| 637 | Result(TestIndirectFunctionAnalysis::Result &IDep) : IDep(IDep) {} |
| 638 | TestIndirectFunctionAnalysis::Result &IDep; |
| 639 | |
| 640 | bool invalidate(Function &F, const PreservedAnalyses &PA, |
| 641 | FunctionAnalysisManager::Invalidator &Inv) { |
| 642 | auto PAC = PA.getChecker<TestDoublyIndirectFunctionAnalysis>(); |
| 643 | return !(PAC.preserved() || |
| 644 | PAC.preservedSet<AllAnalysesOn<Function>>()) || |
| 645 | Inv.invalidate<TestIndirectFunctionAnalysis>(F, PA); |
| 646 | } |
| 647 | }; |
| 648 | |
| 649 | TestDoublyIndirectFunctionAnalysis(int &Runs) : Runs(Runs) {} |
| 650 | |
| 651 | /// Run the analysis pass over the function and return a result. |
| 652 | Result run(Function &F, FunctionAnalysisManager &AM) { |
| 653 | ++Runs; |
| 654 | auto &IDep = AM.getResult<TestIndirectFunctionAnalysis>(F); |
| 655 | return Result(IDep); |
| 656 | } |
| 657 | |
| 658 | private: |
| 659 | friend AnalysisInfoMixin<TestDoublyIndirectFunctionAnalysis>; |
| 660 | static AnalysisKey Key; |
| 661 | |
| 662 | int &Runs; |
| 663 | }; |
| 664 | |
| 665 | AnalysisKey TestDoublyIndirectFunctionAnalysis::Key; |
| 666 | |
Chandler Carruth | 3ab2a5a | 2016-11-28 22:04:31 +0000 | [diff] [blame] | 667 | struct LambdaPass : public PassInfoMixin<LambdaPass> { |
| 668 | using FuncT = std::function<PreservedAnalyses(Function &, FunctionAnalysisManager &)>; |
| 669 | |
| 670 | LambdaPass(FuncT Func) : Func(std::move(Func)) {} |
| 671 | |
| 672 | PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) { |
| 673 | return Func(F, AM); |
| 674 | } |
| 675 | |
| 676 | FuncT Func; |
| 677 | }; |
| 678 | |
| 679 | TEST_F(PassManagerTest, IndirectAnalysisInvalidation) { |
| 680 | FunctionAnalysisManager FAM(/*DebugLogging*/ true); |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 681 | int FunctionAnalysisRuns = 0, ModuleAnalysisRuns = 0, |
| 682 | IndirectAnalysisRuns = 0, DoublyIndirectAnalysisRuns = 0; |
| 683 | FAM.registerPass([&] { return TestFunctionAnalysis(FunctionAnalysisRuns); }); |
Chandler Carruth | 3ab2a5a | 2016-11-28 22:04:31 +0000 | [diff] [blame] | 684 | FAM.registerPass( |
| 685 | [&] { return TestIndirectFunctionAnalysis(IndirectAnalysisRuns); }); |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 686 | FAM.registerPass([&] { |
| 687 | return TestDoublyIndirectFunctionAnalysis(DoublyIndirectAnalysisRuns); |
| 688 | }); |
Chandler Carruth | 3ab2a5a | 2016-11-28 22:04:31 +0000 | [diff] [blame] | 689 | |
| 690 | ModuleAnalysisManager MAM(/*DebugLogging*/ true); |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 691 | MAM.registerPass([&] { return TestModuleAnalysis(ModuleAnalysisRuns); }); |
Chandler Carruth | 3ab2a5a | 2016-11-28 22:04:31 +0000 | [diff] [blame] | 692 | MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); }); |
| 693 | FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); }); |
| 694 | |
Fedor Sergeev | ee8d31c | 2018-09-20 17:08:45 +0000 | [diff] [blame] | 695 | PassInstrumentationCallbacks PIC; |
| 696 | MAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); }); |
| 697 | FAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); }); |
| 698 | |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 699 | int InstrCount = 0, FunctionCount = 0; |
Chandler Carruth | 3ab2a5a | 2016-11-28 22:04:31 +0000 | [diff] [blame] | 700 | ModulePassManager MPM(/*DebugLogging*/ true); |
| 701 | FunctionPassManager FPM(/*DebugLogging*/ true); |
| 702 | // First just use the analysis to get the instruction count, and preserve |
| 703 | // everything. |
| 704 | FPM.addPass(LambdaPass([&](Function &F, FunctionAnalysisManager &AM) { |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 705 | auto &DoublyIndirectResult = |
| 706 | AM.getResult<TestDoublyIndirectFunctionAnalysis>(F); |
| 707 | auto &IndirectResult = DoublyIndirectResult.IDep; |
| 708 | InstrCount += IndirectResult.FDep.InstructionCount; |
| 709 | FunctionCount += IndirectResult.MDep.FunctionCount; |
Chandler Carruth | 3ab2a5a | 2016-11-28 22:04:31 +0000 | [diff] [blame] | 710 | return PreservedAnalyses::all(); |
| 711 | })); |
| 712 | // Next, invalidate |
| 713 | // - both analyses for "f", |
| 714 | // - just the underlying (indirect) analysis for "g", and |
| 715 | // - just the direct analysis for "h". |
| 716 | FPM.addPass(LambdaPass([&](Function &F, FunctionAnalysisManager &AM) { |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 717 | auto &DoublyIndirectResult = |
| 718 | AM.getResult<TestDoublyIndirectFunctionAnalysis>(F); |
| 719 | auto &IndirectResult = DoublyIndirectResult.IDep; |
| 720 | InstrCount += IndirectResult.FDep.InstructionCount; |
| 721 | FunctionCount += IndirectResult.MDep.FunctionCount; |
Chandler Carruth | 3ab2a5a | 2016-11-28 22:04:31 +0000 | [diff] [blame] | 722 | auto PA = PreservedAnalyses::none(); |
| 723 | if (F.getName() == "g") |
| 724 | PA.preserve<TestFunctionAnalysis>(); |
| 725 | else if (F.getName() == "h") |
| 726 | PA.preserve<TestIndirectFunctionAnalysis>(); |
| 727 | return PA; |
| 728 | })); |
| 729 | // Finally, use the analysis again on each function, forcing re-computation |
| 730 | // for all of them. |
| 731 | FPM.addPass(LambdaPass([&](Function &F, FunctionAnalysisManager &AM) { |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 732 | auto &DoublyIndirectResult = |
| 733 | AM.getResult<TestDoublyIndirectFunctionAnalysis>(F); |
| 734 | auto &IndirectResult = DoublyIndirectResult.IDep; |
| 735 | InstrCount += IndirectResult.FDep.InstructionCount; |
| 736 | FunctionCount += IndirectResult.MDep.FunctionCount; |
Chandler Carruth | 3ab2a5a | 2016-11-28 22:04:31 +0000 | [diff] [blame] | 737 | return PreservedAnalyses::all(); |
| 738 | })); |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 739 | |
| 740 | // Create a second function pass manager. This will cause the module-level |
| 741 | // invalidation to occur, which will force yet another invalidation of the |
| 742 | // indirect function-level analysis as the module analysis it depends on gets |
| 743 | // invalidated. |
| 744 | FunctionPassManager FPM2(/*DebugLogging*/ true); |
| 745 | FPM2.addPass(LambdaPass([&](Function &F, FunctionAnalysisManager &AM) { |
| 746 | auto &DoublyIndirectResult = |
| 747 | AM.getResult<TestDoublyIndirectFunctionAnalysis>(F); |
| 748 | auto &IndirectResult = DoublyIndirectResult.IDep; |
| 749 | InstrCount += IndirectResult.FDep.InstructionCount; |
| 750 | FunctionCount += IndirectResult.MDep.FunctionCount; |
| 751 | return PreservedAnalyses::all(); |
| 752 | })); |
| 753 | |
| 754 | // Add a requires pass to populate the module analysis and then our function |
| 755 | // pass pipeline. |
| 756 | MPM.addPass(RequireAnalysisPass<TestModuleAnalysis, Module>()); |
Chandler Carruth | 3ab2a5a | 2016-11-28 22:04:31 +0000 | [diff] [blame] | 757 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 758 | // Now require the module analysis again (it will have been invalidated once) |
| 759 | // and then use it again from a function pass manager. |
| 760 | MPM.addPass(RequireAnalysisPass<TestModuleAnalysis, Module>()); |
| 761 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM2))); |
Chandler Carruth | 3ab2a5a | 2016-11-28 22:04:31 +0000 | [diff] [blame] | 762 | MPM.run(*M, MAM); |
| 763 | |
| 764 | // There are generally two possible runs for each of the three functions. But |
| 765 | // for one function, we only invalidate the indirect analysis so the base one |
| 766 | // only gets run five times. |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 767 | EXPECT_EQ(5, FunctionAnalysisRuns); |
| 768 | // The module analysis pass should be run twice here. |
| 769 | EXPECT_EQ(2, ModuleAnalysisRuns); |
Chandler Carruth | 3ab2a5a | 2016-11-28 22:04:31 +0000 | [diff] [blame] | 770 | // The indirect analysis is invalidated for each function (either directly or |
| 771 | // indirectly) and run twice for each. |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 772 | EXPECT_EQ(9, IndirectAnalysisRuns); |
| 773 | EXPECT_EQ(9, DoublyIndirectAnalysisRuns); |
Chandler Carruth | 3ab2a5a | 2016-11-28 22:04:31 +0000 | [diff] [blame] | 774 | |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 775 | // There are five instructions in the module and we add the count four |
Chandler Carruth | 3ab2a5a | 2016-11-28 22:04:31 +0000 | [diff] [blame] | 776 | // times. |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 777 | EXPECT_EQ(5 * 4, InstrCount); |
| 778 | |
| 779 | // There are three functions and we count them four times for each of the |
| 780 | // three functions. |
| 781 | EXPECT_EQ(3 * 4 * 3, FunctionCount); |
Chandler Carruth | 3ab2a5a | 2016-11-28 22:04:31 +0000 | [diff] [blame] | 782 | } |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 783 | } |