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 | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 44 | static char PassID; |
| 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 | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 49 | char TestFunctionAnalysis::PassID; |
| 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 | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 70 | static char PassID; |
| 71 | |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 72 | int &Runs; |
| 73 | }; |
| 74 | |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 75 | char TestModuleAnalysis::PassID; |
| 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 TestMinPreservingModulePass |
| 95 | : PassInfoMixin<TestMinPreservingModulePass> { |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 96 | PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM) { |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 97 | PreservedAnalyses PA; |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 98 | |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 99 | // Force running an analysis. |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 100 | (void)AM.getResult<TestModuleAnalysis>(M); |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 101 | |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 102 | PA.preserve<FunctionAnalysisManagerModuleProxy>(); |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 103 | return PA; |
| 104 | } |
| 105 | }; |
| 106 | |
Chandler Carruth | 30a0730 | 2016-03-11 10:33:22 +0000 | [diff] [blame] | 107 | struct TestFunctionPass : PassInfoMixin<TestFunctionPass> { |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 108 | TestFunctionPass(int &RunCount, int &AnalyzedInstrCount, |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 109 | int &AnalyzedFunctionCount, |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 110 | bool OnlyUseCachedResults = false) |
| 111 | : RunCount(RunCount), AnalyzedInstrCount(AnalyzedInstrCount), |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 112 | AnalyzedFunctionCount(AnalyzedFunctionCount), |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 113 | OnlyUseCachedResults(OnlyUseCachedResults) {} |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 114 | |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 115 | PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) { |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 116 | ++RunCount; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 117 | |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 118 | const ModuleAnalysisManager &MAM = |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 119 | AM.getResult<ModuleAnalysisManagerFunctionProxy>(F).getManager(); |
Chandler Carruth | eedf9fc | 2014-02-05 21:41:42 +0000 | [diff] [blame] | 120 | if (TestModuleAnalysis::Result *TMA = |
Chandler Carruth | d174ce4 | 2015-01-05 02:47:05 +0000 | [diff] [blame] | 121 | MAM.getCachedResult<TestModuleAnalysis>(*F.getParent())) |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 122 | AnalyzedFunctionCount += TMA->FunctionCount; |
| 123 | |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 124 | if (OnlyUseCachedResults) { |
| 125 | // Hack to force the use of the cached interface. |
Chandler Carruth | eedf9fc | 2014-02-05 21:41:42 +0000 | [diff] [blame] | 126 | if (TestFunctionAnalysis::Result *AR = |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 127 | AM.getCachedResult<TestFunctionAnalysis>(F)) |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 128 | AnalyzedInstrCount += AR->InstructionCount; |
| 129 | } else { |
| 130 | // Typical path just runs the analysis as needed. |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 131 | TestFunctionAnalysis::Result &AR = AM.getResult<TestFunctionAnalysis>(F); |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 132 | AnalyzedInstrCount += AR.InstructionCount; |
| 133 | } |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 134 | |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 135 | return PreservedAnalyses::all(); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | int &RunCount; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 139 | int &AnalyzedInstrCount; |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 140 | int &AnalyzedFunctionCount; |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 141 | bool OnlyUseCachedResults; |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
Chandler Carruth | bceeb22 | 2013-11-22 23:38:07 +0000 | [diff] [blame] | 144 | // A test function pass that invalidates all function analyses for a function |
| 145 | // with a specific name. |
Chandler Carruth | 30a0730 | 2016-03-11 10:33:22 +0000 | [diff] [blame] | 146 | struct TestInvalidationFunctionPass |
| 147 | : PassInfoMixin<TestInvalidationFunctionPass> { |
Chandler Carruth | bceeb22 | 2013-11-22 23:38:07 +0000 | [diff] [blame] | 148 | TestInvalidationFunctionPass(StringRef FunctionName) : Name(FunctionName) {} |
| 149 | |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 150 | PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) { |
Chandler Carruth | d174ce4 | 2015-01-05 02:47:05 +0000 | [diff] [blame] | 151 | return F.getName() == Name ? PreservedAnalyses::none() |
| 152 | : PreservedAnalyses::all(); |
Chandler Carruth | bceeb22 | 2013-11-22 23:38:07 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | StringRef Name; |
| 156 | }; |
| 157 | |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 158 | std::unique_ptr<Module> parseIR(LLVMContext &Context, const char *IR) { |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 159 | SMDiagnostic Err; |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 160 | return parseAssemblyString(IR, Err, Context); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | class PassManagerTest : public ::testing::Test { |
| 164 | protected: |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 165 | LLVMContext Context; |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 166 | std::unique_ptr<Module> M; |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 167 | |
| 168 | public: |
| 169 | PassManagerTest() |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 170 | : M(parseIR(Context, "define void @f() {\n" |
| 171 | "entry:\n" |
| 172 | " call void @g()\n" |
| 173 | " call void @h()\n" |
| 174 | " ret void\n" |
| 175 | "}\n" |
| 176 | "define void @g() {\n" |
| 177 | " ret void\n" |
| 178 | "}\n" |
| 179 | "define void @h() {\n" |
| 180 | " ret void\n" |
| 181 | "}\n")) {} |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 182 | }; |
| 183 | |
Chandler Carruth | 999b92d | 2014-03-13 10:42:18 +0000 | [diff] [blame] | 184 | TEST_F(PassManagerTest, BasicPreservedAnalyses) { |
| 185 | PreservedAnalyses PA1 = PreservedAnalyses(); |
| 186 | EXPECT_FALSE(PA1.preserved<TestFunctionAnalysis>()); |
| 187 | EXPECT_FALSE(PA1.preserved<TestModuleAnalysis>()); |
| 188 | PreservedAnalyses PA2 = PreservedAnalyses::none(); |
| 189 | EXPECT_FALSE(PA2.preserved<TestFunctionAnalysis>()); |
| 190 | EXPECT_FALSE(PA2.preserved<TestModuleAnalysis>()); |
| 191 | PreservedAnalyses PA3 = PreservedAnalyses::all(); |
| 192 | EXPECT_TRUE(PA3.preserved<TestFunctionAnalysis>()); |
| 193 | EXPECT_TRUE(PA3.preserved<TestModuleAnalysis>()); |
| 194 | PreservedAnalyses PA4 = PA1; |
| 195 | EXPECT_FALSE(PA4.preserved<TestFunctionAnalysis>()); |
| 196 | EXPECT_FALSE(PA4.preserved<TestModuleAnalysis>()); |
| 197 | PA4 = PA3; |
| 198 | EXPECT_TRUE(PA4.preserved<TestFunctionAnalysis>()); |
| 199 | EXPECT_TRUE(PA4.preserved<TestModuleAnalysis>()); |
| 200 | PA4 = std::move(PA2); |
| 201 | EXPECT_FALSE(PA4.preserved<TestFunctionAnalysis>()); |
| 202 | EXPECT_FALSE(PA4.preserved<TestModuleAnalysis>()); |
| 203 | PA4.preserve<TestFunctionAnalysis>(); |
| 204 | EXPECT_TRUE(PA4.preserved<TestFunctionAnalysis>()); |
| 205 | EXPECT_FALSE(PA4.preserved<TestModuleAnalysis>()); |
| 206 | PA1.preserve<TestModuleAnalysis>(); |
| 207 | EXPECT_FALSE(PA1.preserved<TestFunctionAnalysis>()); |
| 208 | EXPECT_TRUE(PA1.preserved<TestModuleAnalysis>()); |
| 209 | PA1.preserve<TestFunctionAnalysis>(); |
| 210 | EXPECT_TRUE(PA1.preserved<TestFunctionAnalysis>()); |
| 211 | EXPECT_TRUE(PA1.preserved<TestModuleAnalysis>()); |
| 212 | PA1.intersect(PA4); |
| 213 | EXPECT_TRUE(PA1.preserved<TestFunctionAnalysis>()); |
| 214 | EXPECT_FALSE(PA1.preserved<TestModuleAnalysis>()); |
| 215 | } |
| 216 | |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 217 | TEST_F(PassManagerTest, Basic) { |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 218 | FunctionAnalysisManager FAM; |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 219 | int FunctionAnalysisRuns = 0; |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 220 | FAM.registerPass([&] { return TestFunctionAnalysis(FunctionAnalysisRuns); }); |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 221 | |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 222 | ModuleAnalysisManager MAM; |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 223 | int ModuleAnalysisRuns = 0; |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 224 | MAM.registerPass([&] { return TestModuleAnalysis(ModuleAnalysisRuns); }); |
| 225 | MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); }); |
| 226 | FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); }); |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 227 | |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 228 | ModulePassManager MPM; |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 229 | |
| 230 | // Count the runs over a Function. |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 231 | int FunctionPassRunCount1 = 0; |
| 232 | int AnalyzedInstrCount1 = 0; |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 233 | int AnalyzedFunctionCount1 = 0; |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 234 | { |
Chandler Carruth | 999b92d | 2014-03-13 10:42:18 +0000 | [diff] [blame] | 235 | // Pointless scoped copy to test move assignment. |
| 236 | ModulePassManager NestedMPM; |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 237 | FunctionPassManager FPM; |
Chandler Carruth | 999b92d | 2014-03-13 10:42:18 +0000 | [diff] [blame] | 238 | { |
| 239 | // Pointless scope to test move assignment. |
| 240 | FunctionPassManager NestedFPM; |
Chandler Carruth | 74a8a22 | 2016-06-17 07:15:29 +0000 | [diff] [blame] | 241 | NestedFPM.addPass(TestFunctionPass( |
| 242 | FunctionPassRunCount1, AnalyzedInstrCount1, AnalyzedFunctionCount1)); |
Chandler Carruth | 999b92d | 2014-03-13 10:42:18 +0000 | [diff] [blame] | 243 | FPM = std::move(NestedFPM); |
| 244 | } |
| 245 | NestedMPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 246 | MPM = std::move(NestedMPM); |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 247 | } |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 248 | |
| 249 | // Count the runs over a module. |
| 250 | int ModulePassRunCount = 0; |
| 251 | MPM.addPass(TestModulePass(ModulePassRunCount)); |
| 252 | |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 253 | // Count the runs over a Function in a separate manager. |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 254 | int FunctionPassRunCount2 = 0; |
| 255 | int AnalyzedInstrCount2 = 0; |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 256 | int AnalyzedFunctionCount2 = 0; |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 257 | { |
| 258 | FunctionPassManager FPM; |
| 259 | FPM.addPass(TestFunctionPass(FunctionPassRunCount2, AnalyzedInstrCount2, |
| 260 | AnalyzedFunctionCount2)); |
| 261 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 262 | } |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 263 | |
Chandler Carruth | bceeb22 | 2013-11-22 23:38:07 +0000 | [diff] [blame] | 264 | // A third function pass manager but with only preserving intervening passes |
| 265 | // and with a function pass that invalidates exactly one analysis. |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 266 | MPM.addPass(TestPreservingModulePass()); |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 267 | int FunctionPassRunCount3 = 0; |
| 268 | int AnalyzedInstrCount3 = 0; |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 269 | int AnalyzedFunctionCount3 = 0; |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 270 | { |
| 271 | FunctionPassManager FPM; |
| 272 | FPM.addPass(TestFunctionPass(FunctionPassRunCount3, AnalyzedInstrCount3, |
| 273 | AnalyzedFunctionCount3)); |
| 274 | FPM.addPass(TestInvalidationFunctionPass("f")); |
| 275 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 276 | } |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 277 | |
| 278 | // A fourth function pass manager but with a minimal intervening passes. |
| 279 | MPM.addPass(TestMinPreservingModulePass()); |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 280 | int FunctionPassRunCount4 = 0; |
| 281 | int AnalyzedInstrCount4 = 0; |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 282 | int AnalyzedFunctionCount4 = 0; |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 283 | { |
| 284 | FunctionPassManager FPM; |
| 285 | FPM.addPass(TestFunctionPass(FunctionPassRunCount4, AnalyzedInstrCount4, |
| 286 | AnalyzedFunctionCount4)); |
| 287 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 288 | } |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 289 | |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 290 | // A fifth function pass manager but which uses only cached results. |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 291 | int FunctionPassRunCount5 = 0; |
| 292 | int AnalyzedInstrCount5 = 0; |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 293 | int AnalyzedFunctionCount5 = 0; |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 294 | { |
| 295 | FunctionPassManager FPM; |
| 296 | FPM.addPass(TestInvalidationFunctionPass("f")); |
| 297 | FPM.addPass(TestFunctionPass(FunctionPassRunCount5, AnalyzedInstrCount5, |
| 298 | AnalyzedFunctionCount5, |
| 299 | /*OnlyUseCachedResults=*/true)); |
| 300 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 301 | } |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 302 | |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 303 | MPM.run(*M, MAM); |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 304 | |
| 305 | // Validate module pass counters. |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 306 | EXPECT_EQ(1, ModulePassRunCount); |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 307 | |
Chandler Carruth | bceeb22 | 2013-11-22 23:38:07 +0000 | [diff] [blame] | 308 | // Validate all function pass counter sets are the same. |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 309 | EXPECT_EQ(3, FunctionPassRunCount1); |
| 310 | EXPECT_EQ(5, AnalyzedInstrCount1); |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 311 | EXPECT_EQ(0, AnalyzedFunctionCount1); |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 312 | EXPECT_EQ(3, FunctionPassRunCount2); |
| 313 | EXPECT_EQ(5, AnalyzedInstrCount2); |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 314 | EXPECT_EQ(0, AnalyzedFunctionCount2); |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 315 | EXPECT_EQ(3, FunctionPassRunCount3); |
| 316 | EXPECT_EQ(5, AnalyzedInstrCount3); |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 317 | EXPECT_EQ(0, AnalyzedFunctionCount3); |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 318 | EXPECT_EQ(3, FunctionPassRunCount4); |
| 319 | EXPECT_EQ(5, AnalyzedInstrCount4); |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 320 | EXPECT_EQ(0, AnalyzedFunctionCount4); |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 321 | EXPECT_EQ(3, FunctionPassRunCount5); |
| 322 | EXPECT_EQ(2, AnalyzedInstrCount5); // Only 'g' and 'h' were cached. |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 323 | EXPECT_EQ(0, AnalyzedFunctionCount5); |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 324 | |
Chandler Carruth | bceeb22 | 2013-11-22 23:38:07 +0000 | [diff] [blame] | 325 | // Validate the analysis counters: |
| 326 | // first run over 3 functions, then module pass invalidates |
| 327 | // second run over 3 functions, nothing invalidates |
| 328 | // third run over 0 functions, but 1 function invalidated |
| 329 | // fourth run over 1 function |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 330 | EXPECT_EQ(7, FunctionAnalysisRuns); |
| 331 | |
| 332 | EXPECT_EQ(1, ModuleAnalysisRuns); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 333 | } |
Chandler Carruth | 9b35e6d | 2016-08-19 18:36:06 +0000 | [diff] [blame] | 334 | |
| 335 | // A customized pass manager that passes extra arguments through the |
| 336 | // infrastructure. |
| 337 | typedef AnalysisManager<Function, int> CustomizedAnalysisManager; |
| 338 | typedef PassManager<Function, CustomizedAnalysisManager, int, int &> |
| 339 | CustomizedPassManager; |
| 340 | |
| 341 | class CustomizedAnalysis : public AnalysisInfoMixin<CustomizedAnalysis> { |
| 342 | public: |
| 343 | struct Result { |
| 344 | Result(int I) : I(I) {} |
| 345 | int I; |
| 346 | }; |
| 347 | |
| 348 | Result run(Function &F, CustomizedAnalysisManager &AM, int I) { |
| 349 | return Result(I); |
| 350 | } |
| 351 | |
| 352 | private: |
| 353 | friend AnalysisInfoMixin<CustomizedAnalysis>; |
| 354 | static char PassID; |
| 355 | }; |
| 356 | |
| 357 | char CustomizedAnalysis::PassID; |
| 358 | |
| 359 | struct CustomizedPass : PassInfoMixin<CustomizedPass> { |
| 360 | std::function<void(CustomizedAnalysis::Result &, int &)> Callback; |
| 361 | |
| 362 | template <typename CallbackT> |
| 363 | CustomizedPass(CallbackT Callback) : Callback(Callback) {} |
| 364 | |
| 365 | PreservedAnalyses run(Function &F, CustomizedAnalysisManager &AM, int I, |
| 366 | int &O) { |
| 367 | Callback(AM.getResult<CustomizedAnalysis>(F, I), O); |
| 368 | return PreservedAnalyses::none(); |
| 369 | } |
| 370 | }; |
| 371 | |
| 372 | TEST_F(PassManagerTest, CustomizedPassManagerArgs) { |
| 373 | CustomizedAnalysisManager AM; |
| 374 | AM.registerPass([&] { return CustomizedAnalysis(); }); |
| 375 | |
| 376 | CustomizedPassManager PM; |
| 377 | |
| 378 | // Add an instance of the customized pass that just accumulates the input |
| 379 | // after it is round-tripped through the analysis. |
| 380 | int Result = 0; |
| 381 | PM.addPass( |
| 382 | CustomizedPass([](CustomizedAnalysis::Result &R, int &O) { O += R.I; })); |
| 383 | |
| 384 | // Run this over every function with the input of 42. |
| 385 | for (Function &F : *M) |
| 386 | PM.run(F, AM, 42, Result); |
| 387 | |
| 388 | // And ensure that we accumulated the correct result. |
| 389 | EXPECT_EQ(42 * (int)M->size(), Result); |
| 390 | } |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 391 | } |