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 | |
| 10 | #include "llvm/Assembly/Parser.h" |
| 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 | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 22 | class TestAnalysisPass { |
| 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 | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 32 | TestAnalysisPass(int &Runs) : Runs(Runs) {} |
| 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. |
| 35 | Result run(Function *F) { |
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 | |
| 52 | char TestAnalysisPass::PassID; |
| 53 | |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 54 | struct TestModulePass { |
| 55 | TestModulePass(int &RunCount) : RunCount(RunCount) {} |
| 56 | |
Chandler Carruth | c0bfa8c | 2013-11-20 11:31:50 +0000 | [diff] [blame] | 57 | PreservedAnalyses run(Module *M) { |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 58 | ++RunCount; |
Chandler Carruth | c0bfa8c | 2013-11-20 11:31:50 +0000 | [diff] [blame] | 59 | return PreservedAnalyses::none(); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | int &RunCount; |
| 63 | }; |
| 64 | |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 65 | struct TestPreservingModulePass { |
| 66 | PreservedAnalyses run(Module *M) { |
| 67 | return PreservedAnalyses::all(); |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | struct TestMinPreservingModulePass { |
| 72 | PreservedAnalyses run(Module *M) { |
| 73 | PreservedAnalyses PA; |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 74 | PA.preserve<FunctionAnalysisManagerModuleProxy>(); |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 75 | return PA; |
| 76 | } |
| 77 | }; |
| 78 | |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 79 | struct TestFunctionPass { |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 80 | TestFunctionPass(int &RunCount, int &AnalyzedInstrCount) |
| 81 | : RunCount(RunCount), AnalyzedInstrCount(AnalyzedInstrCount) {} |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 82 | |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 83 | PreservedAnalyses run(Function *F, FunctionAnalysisManager *AM) { |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 84 | ++RunCount; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 85 | |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 86 | const TestAnalysisPass::Result &AR = AM->getResult<TestAnalysisPass>(F); |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 87 | AnalyzedInstrCount += AR.InstructionCount; |
| 88 | |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 89 | return PreservedAnalyses::all(); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | int &RunCount; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 93 | int &AnalyzedInstrCount; |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | Module *parseIR(const char *IR) { |
| 97 | LLVMContext &C = getGlobalContext(); |
| 98 | SMDiagnostic Err; |
| 99 | return ParseAssemblyString(IR, 0, Err, C); |
| 100 | } |
| 101 | |
| 102 | class PassManagerTest : public ::testing::Test { |
| 103 | protected: |
| 104 | OwningPtr<Module> M; |
| 105 | |
| 106 | public: |
| 107 | PassManagerTest() |
| 108 | : M(parseIR("define void @f() {\n" |
| 109 | "entry:\n" |
| 110 | " call void @g()\n" |
| 111 | " call void @h()\n" |
| 112 | " ret void\n" |
| 113 | "}\n" |
| 114 | "define void @g() {\n" |
| 115 | " ret void\n" |
| 116 | "}\n" |
| 117 | "define void @h() {\n" |
| 118 | " ret void\n" |
| 119 | "}\n")) {} |
| 120 | }; |
| 121 | |
| 122 | TEST_F(PassManagerTest, Basic) { |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 123 | FunctionAnalysisManager FAM; |
| 124 | int AnalysisRuns = 0; |
| 125 | FAM.registerPass(TestAnalysisPass(AnalysisRuns)); |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 126 | |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 127 | ModuleAnalysisManager MAM; |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 128 | MAM.registerPass(FunctionAnalysisManagerModuleProxy(FAM)); |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 129 | |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 130 | ModulePassManager MPM; |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 131 | |
| 132 | // Count the runs over a Function. |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 133 | FunctionPassManager FPM1; |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 134 | int FunctionPassRunCount1 = 0; |
| 135 | int AnalyzedInstrCount1 = 0; |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 136 | FPM1.addPass(TestFunctionPass(FunctionPassRunCount1, AnalyzedInstrCount1)); |
| 137 | MPM.addPass(createModuleToFunctionPassAdaptor(FPM1)); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 138 | |
| 139 | // Count the runs over a module. |
| 140 | int ModulePassRunCount = 0; |
| 141 | MPM.addPass(TestModulePass(ModulePassRunCount)); |
| 142 | |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 143 | // Count the runs over a Function in a separate manager. |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 144 | FunctionPassManager FPM2; |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 145 | int FunctionPassRunCount2 = 0; |
| 146 | int AnalyzedInstrCount2 = 0; |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 147 | FPM2.addPass(TestFunctionPass(FunctionPassRunCount2, AnalyzedInstrCount2)); |
| 148 | MPM.addPass(createModuleToFunctionPassAdaptor(FPM2)); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 149 | |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 150 | // A third function pass manager but with only preserving intervening passes. |
| 151 | MPM.addPass(TestPreservingModulePass()); |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 152 | FunctionPassManager FPM3; |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 153 | int FunctionPassRunCount3 = 0; |
| 154 | int AnalyzedInstrCount3 = 0; |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 155 | FPM3.addPass(TestFunctionPass(FunctionPassRunCount3, AnalyzedInstrCount3)); |
| 156 | MPM.addPass(createModuleToFunctionPassAdaptor(FPM3)); |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 157 | |
| 158 | // A fourth function pass manager but with a minimal intervening passes. |
| 159 | MPM.addPass(TestMinPreservingModulePass()); |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 160 | FunctionPassManager FPM4; |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 161 | int FunctionPassRunCount4 = 0; |
| 162 | int AnalyzedInstrCount4 = 0; |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 163 | FPM4.addPass(TestFunctionPass(FunctionPassRunCount4, AnalyzedInstrCount4)); |
| 164 | MPM.addPass(createModuleToFunctionPassAdaptor(FPM4)); |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 165 | |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 166 | MPM.run(M.get(), &MAM); |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 167 | |
| 168 | // Validate module pass counters. |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 169 | EXPECT_EQ(1, ModulePassRunCount); |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 170 | |
| 171 | // Validate both function pass counter sets. |
| 172 | EXPECT_EQ(3, FunctionPassRunCount1); |
| 173 | EXPECT_EQ(5, AnalyzedInstrCount1); |
| 174 | EXPECT_EQ(3, FunctionPassRunCount2); |
| 175 | EXPECT_EQ(5, AnalyzedInstrCount2); |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 176 | EXPECT_EQ(3, FunctionPassRunCount3); |
| 177 | EXPECT_EQ(5, AnalyzedInstrCount3); |
| 178 | EXPECT_EQ(3, FunctionPassRunCount4); |
| 179 | EXPECT_EQ(5, AnalyzedInstrCount4); |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 180 | |
| 181 | // Validate the analysis counters. |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 182 | EXPECT_EQ(9, AnalysisRuns); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 183 | } |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 184 | } |