X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=unittests%2FIR%2FPassManagerTest.cpp;h=41af0b0bd25c4165b98b34b4e4a3679619a8a765;hb=3a491e5d338d233763337f316a8bafba6282a627;hp=48514fc568677e7ee512eb2d434ea885be51f844;hpb=c143c7573bfd0d55cf283cc2676dbd852f939c87;p=oota-llvm.git diff --git a/unittests/IR/PassManagerTest.cpp b/unittests/IR/PassManagerTest.cpp index 48514fc5686..41af0b0bd25 100644 --- a/unittests/IR/PassManagerTest.cpp +++ b/unittests/IR/PassManagerTest.cpp @@ -1,4 +1,4 @@ -//===- llvm/unittest/IR/PassManager.cpp - PassManager unit tests ----------===// +//===- llvm/unittest/IR/PassManager.cpp - PassManager tests ---------------===// // // The LLVM Compiler Infrastructure // @@ -7,546 +7,343 @@ // //===----------------------------------------------------------------------===// -#include "llvm/PassManager.h" -#include "llvm/ADT/SmallVector.h" -#include "llvm/Analysis/CallGraphSCCPass.h" -#include "llvm/Analysis/LoopInfo.h" -#include "llvm/Analysis/LoopPass.h" -#include "llvm/Analysis/Verifier.h" -#include "llvm/Assembly/PrintModulePass.h" -#include "llvm/IR/BasicBlock.h" -#include "llvm/IR/CallingConv.h" -#include "llvm/IR/Constants.h" -#include "llvm/IR/DataLayout.h" -#include "llvm/IR/DerivedTypes.h" +#include "llvm/AsmParser/Parser.h" #include "llvm/IR/Function.h" -#include "llvm/IR/GlobalVariable.h" -#include "llvm/IR/InlineAsm.h" -#include "llvm/IR/Instructions.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" -#include "llvm/Pass.h" -#include "llvm/Support/MathExtras.h" -#include "llvm/Support/raw_ostream.h" +#include "llvm/IR/PassManager.h" +#include "llvm/Support/SourceMgr.h" #include "gtest/gtest.h" using namespace llvm; -namespace llvm { - void initializeModuleNDMPass(PassRegistry&); - void initializeFPassPass(PassRegistry&); - void initializeCGPassPass(PassRegistry&); - void initializeLPassPass(PassRegistry&); - void initializeBPassPass(PassRegistry&); - - namespace { - // ND = no deps - // NM = no modifications - struct ModuleNDNM: public ModulePass { - public: - static char run; - static char ID; - ModuleNDNM() : ModulePass(ID) { } - virtual bool runOnModule(Module &M) { - run++; - return false; - } - virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.setPreservesAll(); - } - }; - char ModuleNDNM::ID=0; - char ModuleNDNM::run=0; - - struct ModuleNDM : public ModulePass { - public: - static char run; - static char ID; - ModuleNDM() : ModulePass(ID) {} - virtual bool runOnModule(Module &M) { - run++; - return true; - } - }; - char ModuleNDM::ID=0; - char ModuleNDM::run=0; - - struct ModuleNDM2 : public ModulePass { - public: - static char run; - static char ID; - ModuleNDM2() : ModulePass(ID) {} - virtual bool runOnModule(Module &M) { - run++; - return true; - } - }; - char ModuleNDM2::ID=0; - char ModuleNDM2::run=0; - - struct ModuleDNM : public ModulePass { - public: - static char run; - static char ID; - ModuleDNM() : ModulePass(ID) { - initializeModuleNDMPass(*PassRegistry::getPassRegistry()); - } - virtual bool runOnModule(Module &M) { - EXPECT_TRUE(getAnalysisIfAvailable()); - run++; - return false; - } - virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequired(); - AU.setPreservesAll(); - } - }; - char ModuleDNM::ID=0; - char ModuleDNM::run=0; - - template - struct PassTestBase : public P { - protected: - static int runc; - static bool initialized; - static bool finalized; - int allocated; - void run() { - EXPECT_TRUE(initialized); - EXPECT_FALSE(finalized); - EXPECT_EQ(0, allocated); - allocated++; - runc++; - } - public: - static char ID; - static void finishedOK(int run) { - EXPECT_GT(runc, 0); - EXPECT_TRUE(initialized); - EXPECT_TRUE(finalized); - EXPECT_EQ(run, runc); - } - PassTestBase() : P(ID), allocated(0) { - initialized = false; - finalized = false; - runc = 0; - } - - virtual void releaseMemory() { - EXPECT_GT(runc, 0); - EXPECT_GT(allocated, 0); - allocated--; - } - }; - template char PassTestBase

::ID; - template int PassTestBase

::runc; - template bool PassTestBase

::initialized; - template bool PassTestBase

::finalized; - - template - struct PassTest : public PassTestBase

{ - public: -#ifndef _MSC_VER // MSVC complains that Pass is not base class. - using llvm::Pass::doInitialization; - using llvm::Pass::doFinalization; -#endif - virtual bool doInitialization(T &t) { - EXPECT_FALSE(PassTestBase

::initialized); - PassTestBase

::initialized = true; - return false; - } - virtual bool doFinalization(T &t) { - EXPECT_FALSE(PassTestBase

::finalized); - PassTestBase

::finalized = true; - EXPECT_EQ(0, PassTestBase

::allocated); - return false; - } - }; - - struct CGPass : public PassTest { - public: - CGPass() { - initializeCGPassPass(*PassRegistry::getPassRegistry()); - } - virtual bool runOnSCC(CallGraphSCC &SCMM) { - EXPECT_TRUE(getAnalysisIfAvailable()); - run(); - return false; - } - }; - - struct FPass : public PassTest { - public: - virtual bool runOnFunction(Function &F) { - // FIXME: PR4112 - // EXPECT_TRUE(getAnalysisIfAvailable()); - run(); - return false; - } - }; - - struct LPass : public PassTestBase { - private: - static int initcount; - static int fincount; - public: - LPass() { - initializeLPassPass(*PassRegistry::getPassRegistry()); - initcount = 0; fincount=0; - EXPECT_FALSE(initialized); - } - static void finishedOK(int run, int finalized) { - PassTestBase::finishedOK(run); - EXPECT_EQ(run, initcount); - EXPECT_EQ(finalized, fincount); - } - using llvm::Pass::doInitialization; - using llvm::Pass::doFinalization; - virtual bool doInitialization(Loop* L, LPPassManager &LPM) { - initialized = true; - initcount++; - return false; - } - virtual bool runOnLoop(Loop *L, LPPassManager &LPM) { - EXPECT_TRUE(getAnalysisIfAvailable()); - run(); - return false; - } - virtual bool doFinalization() { - fincount++; - finalized = true; - return false; - } - }; - int LPass::initcount=0; - int LPass::fincount=0; - - struct BPass : public PassTestBase { - private: - static int inited; - static int fin; - public: - static void finishedOK(int run, int N) { - PassTestBase::finishedOK(run); - EXPECT_EQ(inited, N); - EXPECT_EQ(fin, N); - } - BPass() { - inited = 0; - fin = 0; - } - virtual bool doInitialization(Module &M) { - EXPECT_FALSE(initialized); - initialized = true; - return false; - } - virtual bool doInitialization(Function &F) { - inited++; - return false; - } - virtual bool runOnBasicBlock(BasicBlock &BB) { - EXPECT_TRUE(getAnalysisIfAvailable()); - run(); - return false; - } - virtual bool doFinalization(Function &F) { - fin++; - return false; - } - virtual bool doFinalization(Module &M) { - EXPECT_FALSE(finalized); - finalized = true; - EXPECT_EQ(0, allocated); - return false; - } - }; - int BPass::inited=0; - int BPass::fin=0; - - struct OnTheFlyTest: public ModulePass { - public: - static char ID; - OnTheFlyTest() : ModulePass(ID) { - initializeFPassPass(*PassRegistry::getPassRegistry()); - } - virtual bool runOnModule(Module &M) { - EXPECT_TRUE(getAnalysisIfAvailable()); - for (Module::iterator I=M.begin(),E=M.end(); I != E; ++I) { - Function &F = *I; - { - SCOPED_TRACE("Running on the fly function pass"); - getAnalysis(F); - } - } - return false; - } - virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequired(); - } - }; - char OnTheFlyTest::ID=0; - - TEST(PassManager, RunOnce) { - Module M("test-once", getGlobalContext()); - struct ModuleNDNM *mNDNM = new ModuleNDNM(); - struct ModuleDNM *mDNM = new ModuleDNM(); - struct ModuleNDM *mNDM = new ModuleNDM(); - struct ModuleNDM2 *mNDM2 = new ModuleNDM2(); - - mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0; - - PassManager Passes; - Passes.add(new DataLayout(&M)); - Passes.add(mNDM2); - Passes.add(mNDM); - Passes.add(mNDNM); - Passes.add(mDNM); - - Passes.run(M); - // each pass must be run exactly once, since nothing invalidates them - EXPECT_EQ(1, mNDM->run); - EXPECT_EQ(1, mNDNM->run); - EXPECT_EQ(1, mDNM->run); - EXPECT_EQ(1, mNDM2->run); - } - - TEST(PassManager, ReRun) { - Module M("test-rerun", getGlobalContext()); - struct ModuleNDNM *mNDNM = new ModuleNDNM(); - struct ModuleDNM *mDNM = new ModuleDNM(); - struct ModuleNDM *mNDM = new ModuleNDM(); - struct ModuleNDM2 *mNDM2 = new ModuleNDM2(); - - mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0; - - PassManager Passes; - Passes.add(new DataLayout(&M)); - Passes.add(mNDM); - Passes.add(mNDNM); - Passes.add(mNDM2);// invalidates mNDM needed by mDNM - Passes.add(mDNM); - - Passes.run(M); - // Some passes must be rerun because a pass that modified the - // module/function was run in between - EXPECT_EQ(2, mNDM->run); - EXPECT_EQ(1, mNDNM->run); - EXPECT_EQ(1, mNDM2->run); - EXPECT_EQ(1, mDNM->run); - } - - Module* makeLLVMModule(); - - template - void MemoryTestHelper(int run) { - OwningPtr M(makeLLVMModule()); - T *P = new T(); - PassManager Passes; - Passes.add(new DataLayout(M.get())); - Passes.add(P); - Passes.run(*M); - T::finishedOK(run); - } - - template - void MemoryTestHelper(int run, int N) { - Module *M = makeLLVMModule(); - T *P = new T(); - PassManager Passes; - Passes.add(new DataLayout(M)); - Passes.add(P); - Passes.run(*M); - T::finishedOK(run, N); - delete M; - } +namespace { - TEST(PassManager, Memory) { - // SCC#1: test1->test2->test3->test1 - // SCC#2: test4 - // SCC#3: indirect call node - { - SCOPED_TRACE("Callgraph pass"); - MemoryTestHelper(3); - } - - { - SCOPED_TRACE("Function pass"); - MemoryTestHelper(4);// 4 functions - } - - { - SCOPED_TRACE("Loop pass"); - MemoryTestHelper(2, 1); //2 loops, 1 function - } - { - SCOPED_TRACE("Basic block pass"); - MemoryTestHelper(7, 4); //9 basic blocks - } +class TestFunctionAnalysis { +public: + struct Result { + Result(int Count) : InstructionCount(Count) {} + int InstructionCount; + }; - } + /// \brief Returns an opaque, unique ID for this pass type. + static void *ID() { return (void *)&PassID; } - TEST(PassManager, MemoryOnTheFly) { - Module *M = makeLLVMModule(); - { - SCOPED_TRACE("Running OnTheFlyTest"); - struct OnTheFlyTest *O = new OnTheFlyTest(); - PassManager Passes; - Passes.add(new DataLayout(M)); - Passes.add(O); - Passes.run(*M); - - FPass::finishedOK(4); - } - delete M; - } + /// \brief Returns the name of the analysis. + static StringRef name() { return "TestFunctionAnalysis"; } - Module* makeLLVMModule() { - // Module Construction - Module* mod = new Module("test-mem", getGlobalContext()); - mod->setDataLayout("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-" - "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-" - "a0:0:64-s0:64:64-f80:128:128"); - mod->setTargetTriple("x86_64-unknown-linux-gnu"); + TestFunctionAnalysis(int &Runs) : Runs(Runs) {} - // Type Definitions - std::vectorFuncTy_0_args; - FunctionType* FuncTy_0 = FunctionType::get( - /*Result=*/IntegerType::get(getGlobalContext(), 32), - /*Params=*/FuncTy_0_args, - /*isVarArg=*/false); + /// \brief Run the analysis pass over the function and return a result. + Result run(Function &F, FunctionAnalysisManager *AM) { + ++Runs; + int Count = 0; + for (Function::iterator BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI) + for (BasicBlock::iterator II = BBI->begin(), IE = BBI->end(); II != IE; + ++II) + ++Count; + return Result(Count); + } - std::vectorFuncTy_2_args; - FuncTy_2_args.push_back(IntegerType::get(getGlobalContext(), 1)); - FunctionType* FuncTy_2 = FunctionType::get( - /*Result=*/Type::getVoidTy(getGlobalContext()), - /*Params=*/FuncTy_2_args, - /*isVarArg=*/false); +private: + /// \brief Private static data to provide unique ID. + static char PassID; + int &Runs; +}; - // Function Declarations +char TestFunctionAnalysis::PassID; - Function* func_test1 = Function::Create( - /*Type=*/FuncTy_0, - /*Linkage=*/GlobalValue::ExternalLinkage, - /*Name=*/"test1", mod); - func_test1->setCallingConv(CallingConv::C); - AttributeSet func_test1_PAL; - func_test1->setAttributes(func_test1_PAL); +class TestModuleAnalysis { +public: + struct Result { + Result(int Count) : FunctionCount(Count) {} + int FunctionCount; + }; - Function* func_test2 = Function::Create( - /*Type=*/FuncTy_0, - /*Linkage=*/GlobalValue::ExternalLinkage, - /*Name=*/"test2", mod); - func_test2->setCallingConv(CallingConv::C); - AttributeSet func_test2_PAL; - func_test2->setAttributes(func_test2_PAL); + static void *ID() { return (void *)&PassID; } - Function* func_test3 = Function::Create( - /*Type=*/FuncTy_0, - /*Linkage=*/GlobalValue::ExternalLinkage, - /*Name=*/"test3", mod); - func_test3->setCallingConv(CallingConv::C); - AttributeSet func_test3_PAL; - func_test3->setAttributes(func_test3_PAL); + static StringRef name() { return "TestModuleAnalysis"; } - Function* func_test4 = Function::Create( - /*Type=*/FuncTy_2, - /*Linkage=*/GlobalValue::ExternalLinkage, - /*Name=*/"test4", mod); - func_test4->setCallingConv(CallingConv::C); - AttributeSet func_test4_PAL; - func_test4->setAttributes(func_test4_PAL); + TestModuleAnalysis(int &Runs) : Runs(Runs) {} - // Global Variable Declarations + Result run(Module &M, ModuleAnalysisManager *AM) { + ++Runs; + int Count = 0; + for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) + ++Count; + return Result(Count); + } +private: + static char PassID; - // Constant Definitions + int &Runs; +}; - // Global Variable Definitions +char TestModuleAnalysis::PassID; - // Function Definitions +struct TestModulePass { + TestModulePass(int &RunCount) : RunCount(RunCount) {} - // Function: test1 (func_test1) - { + PreservedAnalyses run(Module &M) { + ++RunCount; + return PreservedAnalyses::none(); + } - BasicBlock* label_entry = BasicBlock::Create(getGlobalContext(), "entry",func_test1,0); + static StringRef name() { return "TestModulePass"; } - // Block entry (label_entry) - CallInst* int32_3 = CallInst::Create(func_test2, "", label_entry); - int32_3->setCallingConv(CallingConv::C); - int32_3->setTailCall(false);AttributeSet int32_3_PAL; - int32_3->setAttributes(int32_3_PAL); + int &RunCount; +}; - ReturnInst::Create(getGlobalContext(), int32_3, label_entry); +struct TestPreservingModulePass { + PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); } - } + static StringRef name() { return "TestPreservingModulePass"; } +}; - // Function: test2 (func_test2) - { +struct TestMinPreservingModulePass { + PreservedAnalyses run(Module &M, ModuleAnalysisManager *AM) { + PreservedAnalyses PA; - BasicBlock* label_entry_5 = BasicBlock::Create(getGlobalContext(), "entry",func_test2,0); + // Force running an analysis. + (void)AM->getResult(M); - // Block entry (label_entry_5) - CallInst* int32_6 = CallInst::Create(func_test3, "", label_entry_5); - int32_6->setCallingConv(CallingConv::C); - int32_6->setTailCall(false);AttributeSet int32_6_PAL; - int32_6->setAttributes(int32_6_PAL); + PA.preserve(); + return PA; + } - ReturnInst::Create(getGlobalContext(), int32_6, label_entry_5); + static StringRef name() { return "TestMinPreservingModulePass"; } +}; + +struct TestFunctionPass { + TestFunctionPass(int &RunCount, int &AnalyzedInstrCount, + int &AnalyzedFunctionCount, + bool OnlyUseCachedResults = false) + : RunCount(RunCount), AnalyzedInstrCount(AnalyzedInstrCount), + AnalyzedFunctionCount(AnalyzedFunctionCount), + OnlyUseCachedResults(OnlyUseCachedResults) {} + + PreservedAnalyses run(Function &F, FunctionAnalysisManager *AM) { + ++RunCount; + + const ModuleAnalysisManager &MAM = + AM->getResult(F).getManager(); + if (TestModuleAnalysis::Result *TMA = + MAM.getCachedResult(*F.getParent())) + AnalyzedFunctionCount += TMA->FunctionCount; + + if (OnlyUseCachedResults) { + // Hack to force the use of the cached interface. + if (TestFunctionAnalysis::Result *AR = + AM->getCachedResult(F)) + AnalyzedInstrCount += AR->InstructionCount; + } else { + // Typical path just runs the analysis as needed. + TestFunctionAnalysis::Result &AR = AM->getResult(F); + AnalyzedInstrCount += AR.InstructionCount; + } - } + return PreservedAnalyses::all(); + } - // Function: test3 (func_test3) - { + static StringRef name() { return "TestFunctionPass"; } - BasicBlock* label_entry_8 = BasicBlock::Create(getGlobalContext(), "entry",func_test3,0); + int &RunCount; + int &AnalyzedInstrCount; + int &AnalyzedFunctionCount; + bool OnlyUseCachedResults; +}; - // Block entry (label_entry_8) - CallInst* int32_9 = CallInst::Create(func_test1, "", label_entry_8); - int32_9->setCallingConv(CallingConv::C); - int32_9->setTailCall(false);AttributeSet int32_9_PAL; - int32_9->setAttributes(int32_9_PAL); +// A test function pass that invalidates all function analyses for a function +// with a specific name. +struct TestInvalidationFunctionPass { + TestInvalidationFunctionPass(StringRef FunctionName) : Name(FunctionName) {} - ReturnInst::Create(getGlobalContext(), int32_9, label_entry_8); + PreservedAnalyses run(Function &F) { + return F.getName() == Name ? PreservedAnalyses::none() + : PreservedAnalyses::all(); + } - } + static StringRef name() { return "TestInvalidationFunctionPass"; } - // Function: test4 (func_test4) - { - Function::arg_iterator args = func_test4->arg_begin(); - Value* int1_f = args++; - int1_f->setName("f"); + StringRef Name; +}; - BasicBlock* label_entry_11 = BasicBlock::Create(getGlobalContext(), "entry",func_test4,0); - BasicBlock* label_bb = BasicBlock::Create(getGlobalContext(), "bb",func_test4,0); - BasicBlock* label_bb1 = BasicBlock::Create(getGlobalContext(), "bb1",func_test4,0); - BasicBlock* label_return = BasicBlock::Create(getGlobalContext(), "return",func_test4,0); +std::unique_ptr parseIR(const char *IR) { + LLVMContext &C = getGlobalContext(); + SMDiagnostic Err; + return parseAssemblyString(IR, Err, C); +} - // Block entry (label_entry_11) - BranchInst::Create(label_bb, label_entry_11); +class PassManagerTest : public ::testing::Test { +protected: + std::unique_ptr M; + +public: + PassManagerTest() + : M(parseIR("define void @f() {\n" + "entry:\n" + " call void @g()\n" + " call void @h()\n" + " ret void\n" + "}\n" + "define void @g() {\n" + " ret void\n" + "}\n" + "define void @h() {\n" + " ret void\n" + "}\n")) {} +}; + +TEST_F(PassManagerTest, BasicPreservedAnalyses) { + PreservedAnalyses PA1 = PreservedAnalyses(); + EXPECT_FALSE(PA1.preserved()); + EXPECT_FALSE(PA1.preserved()); + PreservedAnalyses PA2 = PreservedAnalyses::none(); + EXPECT_FALSE(PA2.preserved()); + EXPECT_FALSE(PA2.preserved()); + PreservedAnalyses PA3 = PreservedAnalyses::all(); + EXPECT_TRUE(PA3.preserved()); + EXPECT_TRUE(PA3.preserved()); + PreservedAnalyses PA4 = PA1; + EXPECT_FALSE(PA4.preserved()); + EXPECT_FALSE(PA4.preserved()); + PA4 = PA3; + EXPECT_TRUE(PA4.preserved()); + EXPECT_TRUE(PA4.preserved()); + PA4 = std::move(PA2); + EXPECT_FALSE(PA4.preserved()); + EXPECT_FALSE(PA4.preserved()); + PA4.preserve(); + EXPECT_TRUE(PA4.preserved()); + EXPECT_FALSE(PA4.preserved()); + PA1.preserve(); + EXPECT_FALSE(PA1.preserved()); + EXPECT_TRUE(PA1.preserved()); + PA1.preserve(); + EXPECT_TRUE(PA1.preserved()); + EXPECT_TRUE(PA1.preserved()); + PA1.intersect(PA4); + EXPECT_TRUE(PA1.preserved()); + EXPECT_FALSE(PA1.preserved()); +} - // Block bb (label_bb) - BranchInst::Create(label_bb, label_bb1, int1_f, label_bb); +TEST_F(PassManagerTest, Basic) { + FunctionAnalysisManager FAM; + int FunctionAnalysisRuns = 0; + FAM.registerPass(TestFunctionAnalysis(FunctionAnalysisRuns)); + + ModuleAnalysisManager MAM; + int ModuleAnalysisRuns = 0; + MAM.registerPass(TestModuleAnalysis(ModuleAnalysisRuns)); + MAM.registerPass(FunctionAnalysisManagerModuleProxy(FAM)); + FAM.registerPass(ModuleAnalysisManagerFunctionProxy(MAM)); + + ModulePassManager MPM; + + // Count the runs over a Function. + int FunctionPassRunCount1 = 0; + int AnalyzedInstrCount1 = 0; + int AnalyzedFunctionCount1 = 0; + { + // Pointless scoped copy to test move assignment. + ModulePassManager NestedMPM; + FunctionPassManager FPM; + { + // Pointless scope to test move assignment. + FunctionPassManager NestedFPM; + NestedFPM.addPass(TestFunctionPass(FunctionPassRunCount1, AnalyzedInstrCount1, + AnalyzedFunctionCount1)); + FPM = std::move(NestedFPM); + } + NestedMPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); + MPM = std::move(NestedMPM); + } - // Block bb1 (label_bb1) - BranchInst::Create(label_bb1, label_return, int1_f, label_bb1); + // Count the runs over a module. + int ModulePassRunCount = 0; + MPM.addPass(TestModulePass(ModulePassRunCount)); + + // Count the runs over a Function in a separate manager. + int FunctionPassRunCount2 = 0; + int AnalyzedInstrCount2 = 0; + int AnalyzedFunctionCount2 = 0; + { + FunctionPassManager FPM; + FPM.addPass(TestFunctionPass(FunctionPassRunCount2, AnalyzedInstrCount2, + AnalyzedFunctionCount2)); + MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); + } - // Block return (label_return) - ReturnInst::Create(getGlobalContext(), label_return); + // A third function pass manager but with only preserving intervening passes + // and with a function pass that invalidates exactly one analysis. + MPM.addPass(TestPreservingModulePass()); + int FunctionPassRunCount3 = 0; + int AnalyzedInstrCount3 = 0; + int AnalyzedFunctionCount3 = 0; + { + FunctionPassManager FPM; + FPM.addPass(TestFunctionPass(FunctionPassRunCount3, AnalyzedInstrCount3, + AnalyzedFunctionCount3)); + FPM.addPass(TestInvalidationFunctionPass("f")); + MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); + } - } - return mod; - } + // A fourth function pass manager but with a minimal intervening passes. + MPM.addPass(TestMinPreservingModulePass()); + int FunctionPassRunCount4 = 0; + int AnalyzedInstrCount4 = 0; + int AnalyzedFunctionCount4 = 0; + { + FunctionPassManager FPM; + FPM.addPass(TestFunctionPass(FunctionPassRunCount4, AnalyzedInstrCount4, + AnalyzedFunctionCount4)); + MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); + } + // A fifth function pass manager but which uses only cached results. + int FunctionPassRunCount5 = 0; + int AnalyzedInstrCount5 = 0; + int AnalyzedFunctionCount5 = 0; + { + FunctionPassManager FPM; + FPM.addPass(TestInvalidationFunctionPass("f")); + FPM.addPass(TestFunctionPass(FunctionPassRunCount5, AnalyzedInstrCount5, + AnalyzedFunctionCount5, + /*OnlyUseCachedResults=*/true)); + MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); } -} -INITIALIZE_PASS(ModuleNDM, "mndm", "mndm", false, false) -INITIALIZE_PASS_BEGIN(CGPass, "cgp","cgp", false, false) -INITIALIZE_PASS_DEPENDENCY(CallGraph) -INITIALIZE_PASS_END(CGPass, "cgp","cgp", false, false) -INITIALIZE_PASS(FPass, "fp","fp", false, false) -INITIALIZE_PASS_BEGIN(LPass, "lp","lp", false, false) -INITIALIZE_PASS_DEPENDENCY(LoopInfo) -INITIALIZE_PASS_END(LPass, "lp","lp", false, false) -INITIALIZE_PASS(BPass, "bp","bp", false, false) + MPM.run(*M, &MAM); + + // Validate module pass counters. + EXPECT_EQ(1, ModulePassRunCount); + + // Validate all function pass counter sets are the same. + EXPECT_EQ(3, FunctionPassRunCount1); + EXPECT_EQ(5, AnalyzedInstrCount1); + EXPECT_EQ(0, AnalyzedFunctionCount1); + EXPECT_EQ(3, FunctionPassRunCount2); + EXPECT_EQ(5, AnalyzedInstrCount2); + EXPECT_EQ(0, AnalyzedFunctionCount2); + EXPECT_EQ(3, FunctionPassRunCount3); + EXPECT_EQ(5, AnalyzedInstrCount3); + EXPECT_EQ(0, AnalyzedFunctionCount3); + EXPECT_EQ(3, FunctionPassRunCount4); + EXPECT_EQ(5, AnalyzedInstrCount4); + EXPECT_EQ(0, AnalyzedFunctionCount4); + EXPECT_EQ(3, FunctionPassRunCount5); + EXPECT_EQ(2, AnalyzedInstrCount5); // Only 'g' and 'h' were cached. + EXPECT_EQ(0, AnalyzedFunctionCount5); + + // Validate the analysis counters: + // first run over 3 functions, then module pass invalidates + // second run over 3 functions, nothing invalidates + // third run over 0 functions, but 1 function invalidated + // fourth run over 1 function + EXPECT_EQ(7, FunctionAnalysisRuns); + + EXPECT_EQ(1, ModuleAnalysisRuns); +} +}