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: |
| 24 | typedef Function IRUnitT; |
| 25 | |
| 26 | struct Result { |
| 27 | Result(int Count) : InstructionCount(Count) {} |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 28 | int InstructionCount; |
| 29 | }; |
| 30 | |
| 31 | /// \brief Returns an opaque, unique ID for this pass type. |
| 32 | static void *ID() { return (void *)&PassID; } |
| 33 | |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 34 | TestAnalysisPass(int &Runs) : Runs(Runs) {} |
| 35 | |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 36 | /// \brief Run the analysis pass over the function and return a result. |
| 37 | Result run(Function *F) { |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 38 | ++Runs; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 39 | int Count = 0; |
| 40 | for (Function::iterator BBI = F->begin(), BBE = F->end(); BBI != BBE; ++BBI) |
| 41 | for (BasicBlock::iterator II = BBI->begin(), IE = BBI->end(); II != IE; |
| 42 | ++II) |
| 43 | ++Count; |
| 44 | return Result(Count); |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | /// \brief Private static data to provide unique ID. |
| 49 | static char PassID; |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 50 | |
| 51 | int &Runs; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | char TestAnalysisPass::PassID; |
| 55 | |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 56 | struct TestModulePass { |
| 57 | TestModulePass(int &RunCount) : RunCount(RunCount) {} |
| 58 | |
Chandler Carruth | c0bfa8c | 2013-11-20 11:31:50 +0000 | [diff] [blame] | 59 | PreservedAnalyses run(Module *M) { |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 60 | ++RunCount; |
Chandler Carruth | c0bfa8c | 2013-11-20 11:31:50 +0000 | [diff] [blame] | 61 | return PreservedAnalyses::none(); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | int &RunCount; |
| 65 | }; |
| 66 | |
| 67 | struct TestFunctionPass { |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame] | 68 | TestFunctionPass(FunctionAnalysisManager &AM, int &RunCount, |
| 69 | int &AnalyzedInstrCount) |
| 70 | : AM(AM), RunCount(RunCount), AnalyzedInstrCount(AnalyzedInstrCount) {} |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 71 | |
Chandler Carruth | c0bfa8c | 2013-11-20 11:31:50 +0000 | [diff] [blame] | 72 | PreservedAnalyses run(Function *F) { |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 73 | ++RunCount; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 74 | |
| 75 | const TestAnalysisPass::Result &AR = AM.getResult<TestAnalysisPass>(F); |
| 76 | AnalyzedInstrCount += AR.InstructionCount; |
| 77 | |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 78 | return PreservedAnalyses::all(); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame] | 81 | FunctionAnalysisManager &AM; |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 82 | int &RunCount; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 83 | int &AnalyzedInstrCount; |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | Module *parseIR(const char *IR) { |
| 87 | LLVMContext &C = getGlobalContext(); |
| 88 | SMDiagnostic Err; |
| 89 | return ParseAssemblyString(IR, 0, Err, C); |
| 90 | } |
| 91 | |
| 92 | class PassManagerTest : public ::testing::Test { |
| 93 | protected: |
| 94 | OwningPtr<Module> M; |
| 95 | |
| 96 | public: |
| 97 | PassManagerTest() |
| 98 | : M(parseIR("define void @f() {\n" |
| 99 | "entry:\n" |
| 100 | " call void @g()\n" |
| 101 | " call void @h()\n" |
| 102 | " ret void\n" |
| 103 | "}\n" |
| 104 | "define void @g() {\n" |
| 105 | " ret void\n" |
| 106 | "}\n" |
| 107 | "define void @h() {\n" |
| 108 | " ret void\n" |
| 109 | "}\n")) {} |
| 110 | }; |
| 111 | |
| 112 | TEST_F(PassManagerTest, Basic) { |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 113 | FunctionAnalysisManager FAM; |
| 114 | int AnalysisRuns = 0; |
| 115 | FAM.registerPass(TestAnalysisPass(AnalysisRuns)); |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 116 | |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 117 | ModuleAnalysisManager MAM; |
| 118 | MAM.registerPass(FunctionAnalysisModuleProxy(FAM)); |
| 119 | |
| 120 | ModulePassManager MPM(&MAM); |
| 121 | |
| 122 | // Count the runs over a Function. |
| 123 | FunctionPassManager FPM1(&FAM); |
| 124 | int FunctionPassRunCount1 = 0; |
| 125 | int AnalyzedInstrCount1 = 0; |
| 126 | FPM1.addPass(TestFunctionPass(FAM, FunctionPassRunCount1, AnalyzedInstrCount1)); |
| 127 | MPM.addPass(createModuleToFunctionPassAdaptor(FPM1, &MAM)); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 128 | |
| 129 | // Count the runs over a module. |
| 130 | int ModulePassRunCount = 0; |
| 131 | MPM.addPass(TestModulePass(ModulePassRunCount)); |
| 132 | |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 133 | // Count the runs over a Function in a separate manager. |
| 134 | FunctionPassManager FPM2(&FAM); |
| 135 | int FunctionPassRunCount2 = 0; |
| 136 | int AnalyzedInstrCount2 = 0; |
| 137 | FPM2.addPass(TestFunctionPass(FAM, FunctionPassRunCount2, AnalyzedInstrCount2)); |
| 138 | MPM.addPass(createModuleToFunctionPassAdaptor(FPM2, &MAM)); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 139 | |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame] | 140 | MPM.run(M.get()); |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 141 | |
| 142 | // Validate module pass counters. |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 143 | EXPECT_EQ(1, ModulePassRunCount); |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 144 | |
| 145 | // Validate both function pass counter sets. |
| 146 | EXPECT_EQ(3, FunctionPassRunCount1); |
| 147 | EXPECT_EQ(5, AnalyzedInstrCount1); |
| 148 | EXPECT_EQ(3, FunctionPassRunCount2); |
| 149 | EXPECT_EQ(5, AnalyzedInstrCount2); |
| 150 | |
| 151 | // Validate the analysis counters. |
| 152 | EXPECT_EQ(6, AnalysisRuns); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 153 | } |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 154 | } |