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