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 | 2ad1858 | 2013-11-23 01:25:02 +0000 | [diff] [blame] | 22 | class 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 | |
| 29 | /// \brief Returns an opaque, unique ID for this pass type. |
| 30 | static void *ID() { return (void *)&PassID; } |
| 31 | |
Chandler Carruth | 2ad1858 | 2013-11-23 01:25:02 +0000 | [diff] [blame] | 32 | TestFunctionAnalysis(int &Runs) : Runs(Runs) {} |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 33 | |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 34 | /// \brief Run the analysis pass over the function and return a result. |
Chandler Carruth | f2edc07 | 2013-11-22 12:11:02 +0000 | [diff] [blame] | 35 | Result run(Function *F, FunctionAnalysisManager *AM) { |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 36 | ++Runs; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 37 | int Count = 0; |
| 38 | for (Function::iterator BBI = F->begin(), BBE = F->end(); BBI != BBE; ++BBI) |
| 39 | for (BasicBlock::iterator II = BBI->begin(), IE = BBI->end(); II != IE; |
| 40 | ++II) |
| 41 | ++Count; |
| 42 | return Result(Count); |
| 43 | } |
| 44 | |
| 45 | private: |
| 46 | /// \brief Private static data to provide unique ID. |
| 47 | static char PassID; |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 48 | |
| 49 | int &Runs; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
Chandler Carruth | 2ad1858 | 2013-11-23 01:25:02 +0000 | [diff] [blame] | 52 | char TestFunctionAnalysis::PassID; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 53 | |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 54 | class TestModuleAnalysis { |
| 55 | public: |
| 56 | struct Result { |
| 57 | Result(int Count) : FunctionCount(Count) {} |
| 58 | int FunctionCount; |
| 59 | }; |
| 60 | |
Chandler Carruth | 5ae74a6 | 2014-03-10 02:12:14 +0000 | [diff] [blame] | 61 | static void *ID() { return (void *)&PassID; } |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 62 | |
| 63 | TestModuleAnalysis(int &Runs) : Runs(Runs) {} |
| 64 | |
| 65 | Result run(Module *M, ModuleAnalysisManager *AM) { |
| 66 | ++Runs; |
| 67 | int Count = 0; |
| 68 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 69 | ++Count; |
| 70 | return Result(Count); |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | static char PassID; |
| 75 | |
| 76 | int &Runs; |
| 77 | }; |
| 78 | |
| 79 | char TestModuleAnalysis::PassID; |
| 80 | |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 81 | struct TestModulePass { |
| 82 | TestModulePass(int &RunCount) : RunCount(RunCount) {} |
| 83 | |
Chandler Carruth | c0bfa8c | 2013-11-20 11:31:50 +0000 | [diff] [blame] | 84 | PreservedAnalyses run(Module *M) { |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 85 | ++RunCount; |
Chandler Carruth | c0bfa8c | 2013-11-20 11:31:50 +0000 | [diff] [blame] | 86 | return PreservedAnalyses::none(); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Chandler Carruth | a13f27c | 2014-01-11 11:52:05 +0000 | [diff] [blame] | 89 | static StringRef name() { return "TestModulePass"; } |
| 90 | |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 91 | int &RunCount; |
| 92 | }; |
| 93 | |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 94 | struct TestPreservingModulePass { |
Chandler Carruth | 5ae74a6 | 2014-03-10 02:12:14 +0000 | [diff] [blame] | 95 | PreservedAnalyses run(Module *M) { return PreservedAnalyses::all(); } |
Chandler Carruth | a13f27c | 2014-01-11 11:52:05 +0000 | [diff] [blame] | 96 | |
| 97 | static StringRef name() { return "TestPreservingModulePass"; } |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | struct TestMinPreservingModulePass { |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 101 | PreservedAnalyses run(Module *M, ModuleAnalysisManager *AM) { |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 102 | PreservedAnalyses PA; |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 103 | |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 104 | // Force running an analysis. |
| 105 | (void)AM->getResult<TestModuleAnalysis>(M); |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 106 | |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 107 | PA.preserve<FunctionAnalysisManagerModuleProxy>(); |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 108 | return PA; |
| 109 | } |
Chandler Carruth | a13f27c | 2014-01-11 11:52:05 +0000 | [diff] [blame] | 110 | |
| 111 | static StringRef name() { return "TestMinPreservingModulePass"; } |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 114 | struct TestFunctionPass { |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 115 | TestFunctionPass(int &RunCount, int &AnalyzedInstrCount, |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 116 | int &AnalyzedFunctionCount, |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 117 | bool OnlyUseCachedResults = false) |
| 118 | : RunCount(RunCount), AnalyzedInstrCount(AnalyzedInstrCount), |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 119 | AnalyzedFunctionCount(AnalyzedFunctionCount), |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 120 | OnlyUseCachedResults(OnlyUseCachedResults) {} |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 121 | |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 122 | PreservedAnalyses run(Function *F, FunctionAnalysisManager *AM) { |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 123 | ++RunCount; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 124 | |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 125 | const ModuleAnalysisManager &MAM = |
| 126 | AM->getResult<ModuleAnalysisManagerFunctionProxy>(F).getManager(); |
Chandler Carruth | eedf9fc | 2014-02-05 21:41:42 +0000 | [diff] [blame] | 127 | if (TestModuleAnalysis::Result *TMA = |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 128 | MAM.getCachedResult<TestModuleAnalysis>(F->getParent())) |
| 129 | AnalyzedFunctionCount += TMA->FunctionCount; |
| 130 | |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 131 | if (OnlyUseCachedResults) { |
| 132 | // Hack to force the use of the cached interface. |
Chandler Carruth | eedf9fc | 2014-02-05 21:41:42 +0000 | [diff] [blame] | 133 | if (TestFunctionAnalysis::Result *AR = |
Chandler Carruth | 2ad1858 | 2013-11-23 01:25:02 +0000 | [diff] [blame] | 134 | AM->getCachedResult<TestFunctionAnalysis>(F)) |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 135 | AnalyzedInstrCount += AR->InstructionCount; |
| 136 | } else { |
| 137 | // Typical path just runs the analysis as needed. |
Chandler Carruth | eedf9fc | 2014-02-05 21:41:42 +0000 | [diff] [blame] | 138 | TestFunctionAnalysis::Result &AR = AM->getResult<TestFunctionAnalysis>(F); |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 139 | AnalyzedInstrCount += AR.InstructionCount; |
| 140 | } |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 141 | |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 142 | return PreservedAnalyses::all(); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Chandler Carruth | a13f27c | 2014-01-11 11:52:05 +0000 | [diff] [blame] | 145 | static StringRef name() { return "TestFunctionPass"; } |
| 146 | |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 147 | int &RunCount; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 148 | int &AnalyzedInstrCount; |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 149 | int &AnalyzedFunctionCount; |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 150 | bool OnlyUseCachedResults; |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
Chandler Carruth | bceeb22 | 2013-11-22 23:38:07 +0000 | [diff] [blame] | 153 | // A test function pass that invalidates all function analyses for a function |
| 154 | // with a specific name. |
| 155 | struct TestInvalidationFunctionPass { |
| 156 | TestInvalidationFunctionPass(StringRef FunctionName) : Name(FunctionName) {} |
| 157 | |
| 158 | PreservedAnalyses run(Function *F) { |
| 159 | return F->getName() == Name ? PreservedAnalyses::none() |
| 160 | : PreservedAnalyses::all(); |
| 161 | } |
| 162 | |
Chandler Carruth | a13f27c | 2014-01-11 11:52:05 +0000 | [diff] [blame] | 163 | static StringRef name() { return "TestInvalidationFunctionPass"; } |
| 164 | |
Chandler Carruth | bceeb22 | 2013-11-22 23:38:07 +0000 | [diff] [blame] | 165 | StringRef Name; |
| 166 | }; |
| 167 | |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 168 | Module *parseIR(const char *IR) { |
| 169 | LLVMContext &C = getGlobalContext(); |
| 170 | SMDiagnostic Err; |
Rafael Espindola | 11c07d7 | 2014-08-19 16:58:54 +0000 | [diff] [blame] | 171 | return parseAssemblyString(IR, Err, C).release(); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | class PassManagerTest : public ::testing::Test { |
| 175 | protected: |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 176 | std::unique_ptr<Module> M; |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 177 | |
| 178 | public: |
| 179 | PassManagerTest() |
| 180 | : M(parseIR("define void @f() {\n" |
| 181 | "entry:\n" |
| 182 | " call void @g()\n" |
| 183 | " call void @h()\n" |
| 184 | " ret void\n" |
| 185 | "}\n" |
| 186 | "define void @g() {\n" |
| 187 | " ret void\n" |
| 188 | "}\n" |
| 189 | "define void @h() {\n" |
| 190 | " ret void\n" |
| 191 | "}\n")) {} |
| 192 | }; |
| 193 | |
Chandler Carruth | 999b92d | 2014-03-13 10:42:18 +0000 | [diff] [blame] | 194 | TEST_F(PassManagerTest, BasicPreservedAnalyses) { |
| 195 | PreservedAnalyses PA1 = PreservedAnalyses(); |
| 196 | EXPECT_FALSE(PA1.preserved<TestFunctionAnalysis>()); |
| 197 | EXPECT_FALSE(PA1.preserved<TestModuleAnalysis>()); |
| 198 | PreservedAnalyses PA2 = PreservedAnalyses::none(); |
| 199 | EXPECT_FALSE(PA2.preserved<TestFunctionAnalysis>()); |
| 200 | EXPECT_FALSE(PA2.preserved<TestModuleAnalysis>()); |
| 201 | PreservedAnalyses PA3 = PreservedAnalyses::all(); |
| 202 | EXPECT_TRUE(PA3.preserved<TestFunctionAnalysis>()); |
| 203 | EXPECT_TRUE(PA3.preserved<TestModuleAnalysis>()); |
| 204 | PreservedAnalyses PA4 = PA1; |
| 205 | EXPECT_FALSE(PA4.preserved<TestFunctionAnalysis>()); |
| 206 | EXPECT_FALSE(PA4.preserved<TestModuleAnalysis>()); |
| 207 | PA4 = PA3; |
| 208 | EXPECT_TRUE(PA4.preserved<TestFunctionAnalysis>()); |
| 209 | EXPECT_TRUE(PA4.preserved<TestModuleAnalysis>()); |
| 210 | PA4 = std::move(PA2); |
| 211 | EXPECT_FALSE(PA4.preserved<TestFunctionAnalysis>()); |
| 212 | EXPECT_FALSE(PA4.preserved<TestModuleAnalysis>()); |
| 213 | PA4.preserve<TestFunctionAnalysis>(); |
| 214 | EXPECT_TRUE(PA4.preserved<TestFunctionAnalysis>()); |
| 215 | EXPECT_FALSE(PA4.preserved<TestModuleAnalysis>()); |
| 216 | PA1.preserve<TestModuleAnalysis>(); |
| 217 | EXPECT_FALSE(PA1.preserved<TestFunctionAnalysis>()); |
| 218 | EXPECT_TRUE(PA1.preserved<TestModuleAnalysis>()); |
| 219 | PA1.preserve<TestFunctionAnalysis>(); |
| 220 | EXPECT_TRUE(PA1.preserved<TestFunctionAnalysis>()); |
| 221 | EXPECT_TRUE(PA1.preserved<TestModuleAnalysis>()); |
| 222 | PA1.intersect(PA4); |
| 223 | EXPECT_TRUE(PA1.preserved<TestFunctionAnalysis>()); |
| 224 | EXPECT_FALSE(PA1.preserved<TestModuleAnalysis>()); |
| 225 | } |
| 226 | |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 227 | TEST_F(PassManagerTest, Basic) { |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 228 | FunctionAnalysisManager FAM; |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 229 | int FunctionAnalysisRuns = 0; |
| 230 | FAM.registerPass(TestFunctionAnalysis(FunctionAnalysisRuns)); |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 231 | |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 232 | ModuleAnalysisManager MAM; |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 233 | int ModuleAnalysisRuns = 0; |
| 234 | MAM.registerPass(TestModuleAnalysis(ModuleAnalysisRuns)); |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 235 | MAM.registerPass(FunctionAnalysisManagerModuleProxy(FAM)); |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 236 | FAM.registerPass(ModuleAnalysisManagerFunctionProxy(MAM)); |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 237 | |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 238 | ModulePassManager MPM; |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 239 | |
| 240 | // Count the runs over a Function. |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 241 | int FunctionPassRunCount1 = 0; |
| 242 | int AnalyzedInstrCount1 = 0; |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 243 | int AnalyzedFunctionCount1 = 0; |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 244 | { |
Chandler Carruth | 999b92d | 2014-03-13 10:42:18 +0000 | [diff] [blame] | 245 | // Pointless scoped copy to test move assignment. |
| 246 | ModulePassManager NestedMPM; |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 247 | FunctionPassManager FPM; |
Chandler Carruth | 999b92d | 2014-03-13 10:42:18 +0000 | [diff] [blame] | 248 | { |
| 249 | // Pointless scope to test move assignment. |
| 250 | FunctionPassManager NestedFPM; |
| 251 | NestedFPM.addPass(TestFunctionPass(FunctionPassRunCount1, AnalyzedInstrCount1, |
| 252 | AnalyzedFunctionCount1)); |
| 253 | FPM = std::move(NestedFPM); |
| 254 | } |
| 255 | NestedMPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 256 | MPM = std::move(NestedMPM); |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 257 | } |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 258 | |
| 259 | // Count the runs over a module. |
| 260 | int ModulePassRunCount = 0; |
| 261 | MPM.addPass(TestModulePass(ModulePassRunCount)); |
| 262 | |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 263 | // Count the runs over a Function in a separate manager. |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 264 | int FunctionPassRunCount2 = 0; |
| 265 | int AnalyzedInstrCount2 = 0; |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 266 | int AnalyzedFunctionCount2 = 0; |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 267 | { |
| 268 | FunctionPassManager FPM; |
| 269 | FPM.addPass(TestFunctionPass(FunctionPassRunCount2, AnalyzedInstrCount2, |
| 270 | AnalyzedFunctionCount2)); |
| 271 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 272 | } |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 273 | |
Chandler Carruth | bceeb22 | 2013-11-22 23:38:07 +0000 | [diff] [blame] | 274 | // A third function pass manager but with only preserving intervening passes |
| 275 | // and with a function pass that invalidates exactly one analysis. |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 276 | MPM.addPass(TestPreservingModulePass()); |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 277 | int FunctionPassRunCount3 = 0; |
| 278 | int AnalyzedInstrCount3 = 0; |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 279 | int AnalyzedFunctionCount3 = 0; |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 280 | { |
| 281 | FunctionPassManager FPM; |
| 282 | FPM.addPass(TestFunctionPass(FunctionPassRunCount3, AnalyzedInstrCount3, |
| 283 | AnalyzedFunctionCount3)); |
| 284 | FPM.addPass(TestInvalidationFunctionPass("f")); |
| 285 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 286 | } |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 287 | |
| 288 | // A fourth function pass manager but with a minimal intervening passes. |
| 289 | MPM.addPass(TestMinPreservingModulePass()); |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 290 | int FunctionPassRunCount4 = 0; |
| 291 | int AnalyzedInstrCount4 = 0; |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 292 | int AnalyzedFunctionCount4 = 0; |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 293 | { |
| 294 | FunctionPassManager FPM; |
| 295 | FPM.addPass(TestFunctionPass(FunctionPassRunCount4, AnalyzedInstrCount4, |
| 296 | AnalyzedFunctionCount4)); |
| 297 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 298 | } |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 299 | |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 300 | // A fifth function pass manager but which uses only cached results. |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 301 | int FunctionPassRunCount5 = 0; |
| 302 | int AnalyzedInstrCount5 = 0; |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 303 | int AnalyzedFunctionCount5 = 0; |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame] | 304 | { |
| 305 | FunctionPassManager FPM; |
| 306 | FPM.addPass(TestInvalidationFunctionPass("f")); |
| 307 | FPM.addPass(TestFunctionPass(FunctionPassRunCount5, AnalyzedInstrCount5, |
| 308 | AnalyzedFunctionCount5, |
| 309 | /*OnlyUseCachedResults=*/true)); |
| 310 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 311 | } |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 312 | |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 313 | MPM.run(M.get(), &MAM); |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 314 | |
| 315 | // Validate module pass counters. |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 316 | EXPECT_EQ(1, ModulePassRunCount); |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 317 | |
Chandler Carruth | bceeb22 | 2013-11-22 23:38:07 +0000 | [diff] [blame] | 318 | // Validate all function pass counter sets are the same. |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 319 | EXPECT_EQ(3, FunctionPassRunCount1); |
| 320 | EXPECT_EQ(5, AnalyzedInstrCount1); |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 321 | EXPECT_EQ(0, AnalyzedFunctionCount1); |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 322 | EXPECT_EQ(3, FunctionPassRunCount2); |
| 323 | EXPECT_EQ(5, AnalyzedInstrCount2); |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 324 | EXPECT_EQ(0, AnalyzedFunctionCount2); |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 325 | EXPECT_EQ(3, FunctionPassRunCount3); |
| 326 | EXPECT_EQ(5, AnalyzedInstrCount3); |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 327 | EXPECT_EQ(0, AnalyzedFunctionCount3); |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 328 | EXPECT_EQ(3, FunctionPassRunCount4); |
| 329 | EXPECT_EQ(5, AnalyzedInstrCount4); |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 330 | EXPECT_EQ(0, AnalyzedFunctionCount4); |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 331 | EXPECT_EQ(3, FunctionPassRunCount5); |
| 332 | EXPECT_EQ(2, AnalyzedInstrCount5); // Only 'g' and 'h' were cached. |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 333 | EXPECT_EQ(0, AnalyzedFunctionCount5); |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 334 | |
Chandler Carruth | bceeb22 | 2013-11-22 23:38:07 +0000 | [diff] [blame] | 335 | // Validate the analysis counters: |
| 336 | // first run over 3 functions, then module pass invalidates |
| 337 | // second run over 3 functions, nothing invalidates |
| 338 | // third run over 0 functions, but 1 function invalidated |
| 339 | // fourth run over 1 function |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame] | 340 | EXPECT_EQ(7, FunctionAnalysisRuns); |
| 341 | |
| 342 | EXPECT_EQ(1, ModuleAnalysisRuns); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 343 | } |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 344 | } |