8eec0eca7bc2f5901dbb5ed6b3a54dd03970a125
[oota-llvm.git] / unittests / IR / PassManagerTest.cpp
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
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
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 {
64   TestFunctionPass(AnalysisManager &AM, int &RunCount, int &AnalyzedInstrCount)
65       : AM(AM), RunCount(RunCount), AnalyzedInstrCount(AnalyzedInstrCount) {
66     AM.requireAnalysisPass<TestAnalysisPass>();
67   }
68
69   bool run(Function *F) {
70     ++RunCount;
71
72     const TestAnalysisPass::Result &AR = AM.getResult<TestAnalysisPass>(F);
73     AnalyzedInstrCount += AR.InstructionCount;
74
75     return true;
76   }
77
78   AnalysisManager &AM;
79   int &RunCount;
80   int &AnalyzedInstrCount;
81 };
82
83 Module *parseIR(const char *IR) {
84   LLVMContext &C = getGlobalContext();
85   SMDiagnostic Err;
86   return ParseAssemblyString(IR, 0, Err, C);
87 }
88
89 class PassManagerTest : public ::testing::Test {
90 protected:
91   OwningPtr<Module> M;
92
93 public:
94   PassManagerTest()
95       : M(parseIR("define void @f() {\n"
96                   "entry:\n"
97                   "  call void @g()\n"
98                   "  call void @h()\n"
99                   "  ret void\n"
100                   "}\n"
101                   "define void @g() {\n"
102                   "  ret void\n"
103                   "}\n"
104                   "define void @h() {\n"
105                   "  ret void\n"
106                   "}\n")) {}
107 };
108
109 TEST_F(PassManagerTest, Basic) {
110   AnalysisManager AM(M.get());
111   AM.registerAnalysisPass(TestAnalysisPass());
112
113   ModulePassManager MPM(M.get(), &AM);
114   FunctionPassManager FPM(&AM);
115
116   // Count the runs over a module.
117   int ModulePassRunCount = 0;
118   MPM.addPass(TestModulePass(ModulePassRunCount));
119
120   // Count the runs over a Function.
121   int FunctionPassRunCount = 0;
122   int AnalyzedInstrCount = 0;
123   FPM.addPass(TestFunctionPass(AM, FunctionPassRunCount, AnalyzedInstrCount));
124   MPM.addPass(FPM);
125
126   MPM.run();
127   EXPECT_EQ(1, ModulePassRunCount);
128   EXPECT_EQ(3, FunctionPassRunCount);
129   EXPECT_EQ(5, AnalyzedInstrCount);
130 }
131
132 }