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