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) {} |
| 28 | bool invalidate(Function *) { return true; } |
| 29 | int InstructionCount; |
| 30 | }; |
| 31 | |
| 32 | /// \brief Returns an opaque, unique ID for this pass type. |
| 33 | static void *ID() { return (void *)&PassID; } |
| 34 | |
| 35 | /// \brief Run the analysis pass over the function and return a result. |
| 36 | Result run(Function *F) { |
| 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; |
| 48 | }; |
| 49 | |
| 50 | char TestAnalysisPass::PassID; |
| 51 | |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 52 | struct TestModulePass { |
| 53 | TestModulePass(int &RunCount) : RunCount(RunCount) {} |
| 54 | |
| 55 | bool run(Module *M) { |
| 56 | ++RunCount; |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | int &RunCount; |
| 61 | }; |
| 62 | |
| 63 | struct TestFunctionPass { |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame] | 64 | TestFunctionPass(FunctionAnalysisManager &AM, int &RunCount, |
| 65 | int &AnalyzedInstrCount) |
| 66 | : AM(AM), RunCount(RunCount), AnalyzedInstrCount(AnalyzedInstrCount) {} |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 67 | |
| 68 | bool run(Function *F) { |
| 69 | ++RunCount; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 70 | |
| 71 | const TestAnalysisPass::Result &AR = AM.getResult<TestAnalysisPass>(F); |
| 72 | AnalyzedInstrCount += AR.InstructionCount; |
| 73 | |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 74 | return true; |
| 75 | } |
| 76 | |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame] | 77 | FunctionAnalysisManager &AM; |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 78 | int &RunCount; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 79 | int &AnalyzedInstrCount; |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | Module *parseIR(const char *IR) { |
| 83 | LLVMContext &C = getGlobalContext(); |
| 84 | SMDiagnostic Err; |
| 85 | return ParseAssemblyString(IR, 0, Err, C); |
| 86 | } |
| 87 | |
| 88 | class PassManagerTest : public ::testing::Test { |
| 89 | protected: |
| 90 | OwningPtr<Module> M; |
| 91 | |
| 92 | public: |
| 93 | PassManagerTest() |
| 94 | : M(parseIR("define void @f() {\n" |
| 95 | "entry:\n" |
| 96 | " call void @g()\n" |
| 97 | " call void @h()\n" |
| 98 | " ret void\n" |
| 99 | "}\n" |
| 100 | "define void @g() {\n" |
| 101 | " ret void\n" |
| 102 | "}\n" |
| 103 | "define void @h() {\n" |
| 104 | " ret void\n" |
| 105 | "}\n")) {} |
| 106 | }; |
| 107 | |
| 108 | TEST_F(PassManagerTest, Basic) { |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame] | 109 | FunctionAnalysisManager AM; |
| 110 | AM.registerPass(TestAnalysisPass()); |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 111 | |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame] | 112 | ModulePassManager MPM; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 113 | FunctionPassManager FPM(&AM); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 114 | |
| 115 | // Count the runs over a module. |
| 116 | int ModulePassRunCount = 0; |
| 117 | MPM.addPass(TestModulePass(ModulePassRunCount)); |
| 118 | |
| 119 | // Count the runs over a Function. |
| 120 | int FunctionPassRunCount = 0; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 121 | int AnalyzedInstrCount = 0; |
| 122 | FPM.addPass(TestFunctionPass(AM, FunctionPassRunCount, AnalyzedInstrCount)); |
Chandler Carruth | d895e29 | 2013-11-20 04:39:16 +0000 | [diff] [blame] | 123 | MPM.addPass(createModuleToFunctionPassAdaptor(FPM)); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 124 | |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame] | 125 | MPM.run(M.get()); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 126 | EXPECT_EQ(1, ModulePassRunCount); |
| 127 | EXPECT_EQ(3, FunctionPassRunCount); |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 128 | EXPECT_EQ(5, AnalyzedInstrCount); |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 129 | } |
Chandler Carruth | 90a835d | 2013-11-09 13:09:08 +0000 | [diff] [blame] | 130 | } |