[PM] Start sketching out the new module and function pass manager.
[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 struct TestModulePass {
23   TestModulePass(int &RunCount) : RunCount(RunCount) {}
24
25   bool run(Module *M) {
26     ++RunCount;
27     return true;
28   }
29
30   int &RunCount;
31 };
32
33 struct TestFunctionPass {
34   TestFunctionPass(int &RunCount) : RunCount(RunCount) {}
35
36   bool run(Function *F) {
37     ++RunCount;
38     return true;
39   }
40
41   int &RunCount;
42 };
43
44 Module *parseIR(const char *IR) {
45   LLVMContext &C = getGlobalContext();
46   SMDiagnostic Err;
47   return ParseAssemblyString(IR, 0, Err, C);
48 }
49
50 class PassManagerTest : public ::testing::Test {
51 protected:
52   OwningPtr<Module> M;
53
54 public:
55   PassManagerTest()
56       : M(parseIR("define void @f() {\n"
57                   "entry:\n"
58                   "  call void @g()\n"
59                   "  call void @h()\n"
60                   "  ret void\n"
61                   "}\n"
62                   "define void @g() {\n"
63                   "  ret void\n"
64                   "}\n"
65                   "define void @h() {\n"
66                   "  ret void\n"
67                   "}\n")) {}
68 };
69
70 TEST_F(PassManagerTest, Basic) {
71   ModulePassManager MPM(M.get());
72   FunctionPassManager FPM;
73
74   // Count the runs over a module.
75   int ModulePassRunCount = 0;
76   MPM.addPass(TestModulePass(ModulePassRunCount));
77
78   // Count the runs over a Function.
79   int FunctionPassRunCount = 0;
80   FPM.addPass(TestFunctionPass(FunctionPassRunCount));
81   MPM.addPass(FPM);
82
83   MPM.run();
84   EXPECT_EQ(1, ModulePassRunCount);
85   EXPECT_EQ(3, FunctionPassRunCount);
86 }
87
88 }