1 //===- llvm/unittest/IR/PassManager.cpp - PassManager tests ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
22 class TestAnalysisPass {
24 typedef Function IRUnitT;
27 Result(int Count) : InstructionCount(Count) {}
28 bool invalidate(Function *) { return true; }
32 /// \brief Returns an opaque, unique ID for this pass type.
33 static void *ID() { return (void *)&PassID; }
35 /// \brief Run the analysis pass over the function and return a result.
36 Result run(Function *F) {
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;
46 /// \brief Private static data to provide unique ID.
50 char TestAnalysisPass::PassID;
52 struct TestModulePass {
53 TestModulePass(int &RunCount) : RunCount(RunCount) {}
63 struct TestFunctionPass {
64 TestFunctionPass(AnalysisManager &AM, int &RunCount, int &AnalyzedInstrCount)
65 : AM(AM), RunCount(RunCount), AnalyzedInstrCount(AnalyzedInstrCount) {
66 AM.requireAnalysisPass<TestAnalysisPass>();
69 bool run(Function *F) {
72 const TestAnalysisPass::Result &AR = AM.getResult<TestAnalysisPass>(F);
73 AnalyzedInstrCount += AR.InstructionCount;
80 int &AnalyzedInstrCount;
83 Module *parseIR(const char *IR) {
84 LLVMContext &C = getGlobalContext();
86 return ParseAssemblyString(IR, 0, Err, C);
89 class PassManagerTest : public ::testing::Test {
95 : M(parseIR("define void @f() {\n"
101 "define void @g() {\n"
104 "define void @h() {\n"
109 TEST_F(PassManagerTest, Basic) {
110 AnalysisManager AM(M.get());
111 AM.registerAnalysisPass(TestAnalysisPass());
113 ModulePassManager MPM(M.get(), &AM);
114 FunctionPassManager FPM(&AM);
116 // Count the runs over a module.
117 int ModulePassRunCount = 0;
118 MPM.addPass(TestModulePass(ModulePassRunCount));
120 // Count the runs over a Function.
121 int FunctionPassRunCount = 0;
122 int AnalyzedInstrCount = 0;
123 FPM.addPass(TestFunctionPass(AM, FunctionPassRunCount, AnalyzedInstrCount));
127 EXPECT_EQ(1, ModulePassRunCount);
128 EXPECT_EQ(3, FunctionPassRunCount);
129 EXPECT_EQ(5, AnalyzedInstrCount);