#include "llvm/IR/PassManager.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManagerImpl.h"
#include "llvm/Passes/StandardInstrumentations.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Transforms/Scalar/SimplifyCFG.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
class TestFunctionAnalysis : public AnalysisInfoMixin<TestFunctionAnalysis> {
public:
struct Result {
Result(int Count) : InstructionCount(Count) {}
int InstructionCount;
bool invalidate(Function &, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &) {
auto PAC = PA.getChecker<TestFunctionAnalysis>();
return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>());
}
};
TestFunctionAnalysis(int &Runs) : Runs(Runs) {}
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);
}
private:
friend AnalysisInfoMixin<TestFunctionAnalysis>;
static AnalysisKey Key;
int &Runs;
};
AnalysisKey TestFunctionAnalysis::Key;
class TestModuleAnalysis : public AnalysisInfoMixin<TestModuleAnalysis> {
public:
struct Result {
Result(int Count) : FunctionCount(Count) {}
int FunctionCount;
bool invalidate(Module &, const PreservedAnalyses &PA,
ModuleAnalysisManager::Invalidator &) {
auto PAC = PA.getChecker<TestModuleAnalysis>();
return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Module>>());
}
};
TestModuleAnalysis(int &Runs) : Runs(Runs) {}
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:
friend AnalysisInfoMixin<TestModuleAnalysis>;
static AnalysisKey Key;
int &Runs;
};
AnalysisKey TestModuleAnalysis::Key;
struct TestModulePass : PassInfoMixin<TestModulePass> {
TestModulePass(int &RunCount) : RunCount(RunCount) {}
PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
++RunCount;
return PreservedAnalyses::none();
}
int &RunCount;
};
struct TestPreservingModulePass : PassInfoMixin<TestPreservingModulePass> {
PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
return PreservedAnalyses::all();
}
};
struct TestFunctionPass : PassInfoMixin<TestFunctionPass> {
TestFunctionPass(int &RunCount, int &AnalyzedInstrCount,
int &AnalyzedFunctionCount, ModuleAnalysisManager &MAM,
bool OnlyUseCachedResults = false)
: RunCount(RunCount), AnalyzedInstrCount(AnalyzedInstrCount),
AnalyzedFunctionCount(AnalyzedFunctionCount), MAM(MAM),
OnlyUseCachedResults(OnlyUseCachedResults) {}
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) {
++RunCount;
if (TestModuleAnalysis::Result *TMA =
MAM.getCachedResult<TestModuleAnalysis>(*F.getParent())) {
AnalyzedFunctionCount += TMA->FunctionCount;
}
if (OnlyUseCachedResults) {
if (TestFunctionAnalysis::Result *AR =
AM.getCachedResult<TestFunctionAnalysis>(F))
AnalyzedInstrCount += AR->InstructionCount;
} else {
TestFunctionAnalysis::Result &AR = AM.getResult<TestFunctionAnalysis>(F);
AnalyzedInstrCount += AR.InstructionCount;
}
return PreservedAnalyses::all();
}
int &RunCount;
int &AnalyzedInstrCount;
int &AnalyzedFunctionCount;
ModuleAnalysisManager &MAM;
bool OnlyUseCachedResults;
};
struct TestInvalidationFunctionPass
: PassInfoMixin<TestInvalidationFunctionPass> {
TestInvalidationFunctionPass(StringRef FunctionName) : Name(FunctionName) {}
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) {
return F.getName() == Name ? PreservedAnalyses::none()
: PreservedAnalyses::all();
}
StringRef Name;
};
std::unique_ptr<Module> parseIR(LLVMContext &Context, const char *IR) {
SMDiagnostic Err;
return parseAssemblyString(IR, Err, Context);
}
class PassManagerTest : public ::testing::Test {
protected:
LLVMContext Context;
std::unique_ptr<Module> M;
public:
PassManagerTest()
: M(parseIR(Context, "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(PreservedAnalysesTest, Basic) {
PreservedAnalyses PA1 = PreservedAnalyses();
{
auto PAC = PA1.getChecker<TestFunctionAnalysis>();
EXPECT_FALSE(PAC.preserved());
EXPECT_FALSE(PAC.preservedSet<AllAnalysesOn<Function>>());
}
{
auto PAC = PA1.getChecker<TestModuleAnalysis>();
EXPECT_FALSE(PAC.preserved());
EXPECT_FALSE(PAC.preservedSet<AllAnalysesOn<Module>>());
}
auto PA2 = PreservedAnalyses::none();
{
auto PAC = PA2.getChecker<TestFunctionAnalysis>();
EXPECT_FALSE(PAC.preserved());
EXPECT_FALSE(PAC.preservedSet<AllAnalysesOn<Function>>());
}
auto PA3 = PreservedAnalyses::all();
{
auto PAC = PA3.getChecker<TestFunctionAnalysis>();
EXPECT_TRUE(PAC.preserved());
EXPECT_TRUE(PAC.preservedSet<AllAnalysesOn<Function>>());
}
PreservedAnalyses PA4 = PA1;
{
auto PAC = PA4.getChecker<TestFunctionAnalysis>();
EXPECT_FALSE(PAC.preserved());
EXPECT_FALSE(PAC.preservedSet<AllAnalysesOn<Function>>());
}
PA4 = PA3;
{
auto PAC = PA4.getChecker<TestFunctionAnalysis>();
EXPECT_TRUE(PAC.preserved());
EXPECT_TRUE(PAC.preservedSet<AllAnalysesOn<Function>>());
}
PA4 = std::move(PA2);
{
auto PAC = PA4.getChecker<TestFunctionAnalysis>();
EXPECT_FALSE(PAC.preserved());
EXPECT_FALSE(PAC.preservedSet<AllAnalysesOn<Function>>());
}
auto PA5 = PreservedAnalyses::allInSet<AllAnalysesOn<Function>>();
{
auto PAC = PA5.getChecker<TestFunctionAnalysis>();
EXPECT_FALSE(PAC.preserved());
EXPECT_TRUE(PAC.preservedSet<AllAnalysesOn<Function>>());
EXPECT_FALSE(PAC.preservedSet<AllAnalysesOn<Module>>());
}
}
TEST(PreservedAnalysesTest, Preserve) {
auto PA = PreservedAnalyses::none();
PA.preserve<TestFunctionAnalysis>();
EXPECT_TRUE(PA.getChecker<TestFunctionAnalysis>().preserved());
EXPECT_FALSE(PA.getChecker<TestModuleAnalysis>().preserved());
PA.preserve<TestModuleAnalysis>();
EXPECT_TRUE(PA.getChecker<TestFunctionAnalysis>().preserved());
EXPECT_TRUE(PA.getChecker<TestModuleAnalysis>().preserved());
PA.preserve<TestFunctionAnalysis>();
EXPECT_TRUE(PA.getChecker<TestFunctionAnalysis>().preserved());
EXPECT_TRUE(PA.getChecker<TestModuleAnalysis>().preserved());
}
TEST(PreservedAnalysesTest, PreserveSets) {
auto PA = PreservedAnalyses::none();
PA.preserveSet<AllAnalysesOn<Function>>();
EXPECT_TRUE(PA.getChecker<TestFunctionAnalysis>()
.preservedSet<AllAnalysesOn<Function>>());
EXPECT_FALSE(PA.getChecker<TestModuleAnalysis>()
.preservedSet<AllAnalysesOn<Module>>());
PA.preserveSet<AllAnalysesOn<Module>>();
EXPECT_TRUE(PA.getChecker<TestFunctionAnalysis>()
.preservedSet<AllAnalysesOn<Function>>());
EXPECT_TRUE(PA.getChecker<TestModuleAnalysis>()
.preservedSet<AllAnalysesOn<Module>>());
PA.preserve<TestFunctionAnalysis>();
EXPECT_TRUE(PA.getChecker<TestFunctionAnalysis>()
.preservedSet<AllAnalysesOn<Function>>());
EXPECT_TRUE(PA.getChecker<TestModuleAnalysis>()
.preservedSet<AllAnalysesOn<Module>>());
PA.preserveSet<AllAnalysesOn<Module>>();
EXPECT_TRUE(PA.getChecker<TestFunctionAnalysis>()
.preservedSet<AllAnalysesOn<Function>>());
EXPECT_TRUE(PA.getChecker<TestModuleAnalysis>()
.preservedSet<AllAnalysesOn<Module>>());
}
TEST(PreservedAnalysisTest, Intersect) {
auto PA1 = PreservedAnalyses::none();
PA1.preserve<TestFunctionAnalysis>();
PA1.preserveSet<AllAnalysesOn<Module>>();
auto PA2 = PreservedAnalyses::none();
PA2.preserve<TestFunctionAnalysis>();
PA2.preserveSet<AllAnalysesOn<Function>>();
PA2.preserve<TestModuleAnalysis>();
PA2.preserveSet<AllAnalysesOn<Module>>();
auto PA3 = PreservedAnalyses::none();
PA3.preserve<TestModuleAnalysis>();
PA3.preserveSet<AllAnalysesOn<Function>>();
auto Intersected = PA1;
Intersected.intersect(PA1);
EXPECT_TRUE(Intersected.getChecker<TestFunctionAnalysis>().preserved());
EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>()
.preservedSet<AllAnalysesOn<Function>>());
EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved());
EXPECT_TRUE(Intersected.getChecker<TestModuleAnalysis>()
.preservedSet<AllAnalysesOn<Module>>());
Intersected.intersect(PreservedAnalyses::all());
EXPECT_TRUE(Intersected.getChecker<TestFunctionAnalysis>().preserved());
EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>()
.preservedSet<AllAnalysesOn<Function>>());
EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved());
EXPECT_TRUE(Intersected.getChecker<TestModuleAnalysis>()
.preservedSet<AllAnalysesOn<Module>>());
Intersected.intersect(PA2);
EXPECT_TRUE(Intersected.getChecker<TestFunctionAnalysis>().preserved());
EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>()
.preservedSet<AllAnalysesOn<Function>>());
EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved());
EXPECT_TRUE(Intersected.getChecker<TestModuleAnalysis>()
.preservedSet<AllAnalysesOn<Module>>());
Intersected = PA2;
Intersected.intersect(PA1);
EXPECT_TRUE(Intersected.getChecker<TestFunctionAnalysis>().preserved());
EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>()
.preservedSet<AllAnalysesOn<Function>>());
EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved());
EXPECT_TRUE(Intersected.getChecker<TestModuleAnalysis>()
.preservedSet<AllAnalysesOn<Module>>());
Intersected.intersect(PreservedAnalyses::none());
EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>().preserved());
EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>()
.preservedSet<AllAnalysesOn<Function>>());
EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved());
EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>()
.preservedSet<AllAnalysesOn<Module>>());
Intersected = PA1;
Intersected.intersect(PA3);
EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>().preserved());
EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>()
.preservedSet<AllAnalysesOn<Function>>());
EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved());
EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>()
.preservedSet<AllAnalysesOn<Module>>());
Intersected = PA1;
auto Tmp = PA2;
Intersected.intersect(std::move(Tmp));
EXPECT_TRUE(Intersected.getChecker<TestFunctionAnalysis>().preserved());
EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>()
.preservedSet<AllAnalysesOn<Function>>());
EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved());
EXPECT_TRUE(Intersected.getChecker<TestModuleAnalysis>()
.preservedSet<AllAnalysesOn<Module>>());
auto Tmp2 = PreservedAnalyses::all();
Intersected.intersect(std::move(Tmp2));
EXPECT_TRUE(Intersected.getChecker<TestFunctionAnalysis>().preserved());
EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>()
.preservedSet<AllAnalysesOn<Function>>());
EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved());
EXPECT_TRUE(Intersected.getChecker<TestModuleAnalysis>()
.preservedSet<AllAnalysesOn<Module>>());
Intersected = PreservedAnalyses::all();
auto Tmp3 = PA1;
Intersected.intersect(std::move(Tmp3));
EXPECT_TRUE(Intersected.getChecker<TestFunctionAnalysis>().preserved());
EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>()
.preservedSet<AllAnalysesOn<Function>>());
EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved());
EXPECT_TRUE(Intersected.getChecker<TestModuleAnalysis>()
.preservedSet<AllAnalysesOn<Module>>());
}
TEST(PreservedAnalysisTest, Abandon) {
auto PA = PreservedAnalyses::none();
PA.preserve<TestFunctionAnalysis>();
PA.abandon<TestFunctionAnalysis>();
EXPECT_FALSE(PA.getChecker<TestFunctionAnalysis>().preserved());
PA.abandon<TestFunctionAnalysis>();
EXPECT_FALSE(PA.getChecker<TestFunctionAnalysis>().preserved());
PA.abandon<TestModuleAnalysis>();
EXPECT_FALSE(PA.getChecker<TestModuleAnalysis>().preserved());
PA.preserveSet<AllAnalysesOn<Function>>();
PA.preserveSet<AllAnalysesOn<Module>>();
EXPECT_FALSE(PA.getChecker<TestFunctionAnalysis>()
.preservedSet<AllAnalysesOn<Function>>());
EXPECT_FALSE(PA.getChecker<TestModuleAnalysis>()
.preservedSet<AllAnalysesOn<Module>>());
AnalysisKey FakeKey, *FakeID = &FakeKey;
EXPECT_TRUE(PA.getChecker(FakeID).preservedSet<AllAnalysesOn<Function>>());
EXPECT_TRUE(PA.getChecker(FakeID).preservedSet<AllAnalysesOn<Module>>());
}
TEST_F(PassManagerTest, Basic) {
FunctionAnalysisManager FAM;
int FunctionAnalysisRuns = 0;
FAM.registerPass([&] { return TestFunctionAnalysis(FunctionAnalysisRuns); });
ModuleAnalysisManager MAM;
int ModuleAnalysisRuns = 0;
MAM.registerPass([&] { return TestModuleAnalysis(ModuleAnalysisRuns); });
MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
MAM.registerPass([&] { return PassInstrumentationAnalysis(); });
FAM.registerPass([&] { return PassInstrumentationAnalysis(); });
ModulePassManager MPM;
int FunctionPassRunCount1 = 0;
int AnalyzedInstrCount1 = 0;
int AnalyzedFunctionCount1 = 0;
{
ModulePassManager NestedMPM;
FunctionPassManager FPM;
{
FunctionPassManager NestedFPM;
NestedFPM.addPass(TestFunctionPass(FunctionPassRunCount1,
AnalyzedInstrCount1,
AnalyzedFunctionCount1, MAM));
FPM = std::move(NestedFPM);
}
NestedMPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
MPM = std::move(NestedMPM);
}
int ModulePassRunCount = 0;
MPM.addPass(TestModulePass(ModulePassRunCount));
int FunctionPassRunCount2 = 0;
int AnalyzedInstrCount2 = 0;
int AnalyzedFunctionCount2 = 0;
{
FunctionPassManager FPM;
FPM.addPass(TestFunctionPass(FunctionPassRunCount2, AnalyzedInstrCount2,
AnalyzedFunctionCount2, MAM));
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
}
MPM.addPass(TestPreservingModulePass());
int FunctionPassRunCount3 = 0;
int AnalyzedInstrCount3 = 0;
int AnalyzedFunctionCount3 = 0;
{
FunctionPassManager FPM;
FPM.addPass(TestFunctionPass(FunctionPassRunCount3, AnalyzedInstrCount3,
AnalyzedFunctionCount3, MAM));
FPM.addPass(TestInvalidationFunctionPass("f"));
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
}
MPM.addPass(RequireAnalysisPass<TestModuleAnalysis, Module>());
int FunctionPassRunCount4 = 0;
int AnalyzedInstrCount4 = 0;
int AnalyzedFunctionCount4 = 0;
{
FunctionPassManager FPM;
FPM.addPass(TestFunctionPass(FunctionPassRunCount4, AnalyzedInstrCount4,
AnalyzedFunctionCount4, MAM));
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
}
int FunctionPassRunCount5 = 0;
int AnalyzedInstrCount5 = 0;
int AnalyzedFunctionCount5 = 0;
{
FunctionPassManager FPM;
FPM.addPass(TestInvalidationFunctionPass("f"));
FPM.addPass(TestFunctionPass(FunctionPassRunCount5, AnalyzedInstrCount5,
AnalyzedFunctionCount5, MAM,
true));
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
}
MPM.run(*M, MAM);
EXPECT_EQ(1, ModulePassRunCount);
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(9, AnalyzedFunctionCount4);
EXPECT_EQ(3, FunctionPassRunCount5);
EXPECT_EQ(2, AnalyzedInstrCount5); EXPECT_EQ(9, AnalyzedFunctionCount5);
EXPECT_EQ(7, FunctionAnalysisRuns);
EXPECT_EQ(1, ModuleAnalysisRuns);
}
typedef AnalysisManager<Function, int> CustomizedAnalysisManager;
typedef PassManager<Function, CustomizedAnalysisManager, int, int &>
CustomizedPassManager;
class CustomizedAnalysis : public AnalysisInfoMixin<CustomizedAnalysis> {
public:
struct Result {
Result(int I) : I(I) {}
int I;
};
Result run(Function &F, CustomizedAnalysisManager &AM, int I) {
return Result(I);
}
private:
friend AnalysisInfoMixin<CustomizedAnalysis>;
static AnalysisKey Key;
};
AnalysisKey CustomizedAnalysis::Key;
struct CustomizedPass : PassInfoMixin<CustomizedPass> {
std::function<void(CustomizedAnalysis::Result &, int &)> Callback;
template <typename CallbackT>
CustomizedPass(CallbackT Callback) : Callback(Callback) {}
PreservedAnalyses run(Function &F, CustomizedAnalysisManager &AM, int I,
int &O) {
Callback(AM.getResult<CustomizedAnalysis>(F, I), O);
return PreservedAnalyses::none();
}
};
TEST_F(PassManagerTest, CustomizedPassManagerArgs) {
CustomizedAnalysisManager AM;
AM.registerPass([&] { return CustomizedAnalysis(); });
PassInstrumentationCallbacks PIC;
AM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
CustomizedPassManager PM;
int Result = 0;
PM.addPass(
CustomizedPass([](CustomizedAnalysis::Result &R, int &O) { O += R.I; }));
for (Function &F : *M)
PM.run(F, AM, 42, Result);
EXPECT_EQ(42 * (int)M->size(), Result);
}
struct TestIndirectFunctionAnalysis
: public AnalysisInfoMixin<TestIndirectFunctionAnalysis> {
struct Result {
Result(TestFunctionAnalysis::Result &FDep, TestModuleAnalysis::Result &MDep)
: FDep(FDep), MDep(MDep) {}
TestFunctionAnalysis::Result &FDep;
TestModuleAnalysis::Result &MDep;
bool invalidate(Function &F, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &Inv) {
auto PAC = PA.getChecker<TestIndirectFunctionAnalysis>();
return !(PAC.preserved() ||
PAC.preservedSet<AllAnalysesOn<Function>>()) ||
Inv.invalidate<TestFunctionAnalysis>(F, PA);
}
};
TestIndirectFunctionAnalysis(int &Runs, ModuleAnalysisManager &MAM)
: Runs(Runs), MAM(MAM) {}
Result run(Function &F, FunctionAnalysisManager &AM) {
++Runs;
auto &FDep = AM.getResult<TestFunctionAnalysis>(F);
auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
auto &MDep = *MAM.getCachedResult<TestModuleAnalysis>(*F.getParent());
MAMProxy.registerOuterAnalysisInvalidation<TestModuleAnalysis,
TestIndirectFunctionAnalysis>();
return Result(FDep, MDep);
}
private:
friend AnalysisInfoMixin<TestIndirectFunctionAnalysis>;
static AnalysisKey Key;
int &Runs;
ModuleAnalysisManager &MAM;
};
AnalysisKey TestIndirectFunctionAnalysis::Key;
struct TestDoublyIndirectFunctionAnalysis
: public AnalysisInfoMixin<TestDoublyIndirectFunctionAnalysis> {
struct Result {
Result(TestIndirectFunctionAnalysis::Result &IDep) : IDep(IDep) {}
TestIndirectFunctionAnalysis::Result &IDep;
bool invalidate(Function &F, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &Inv) {
auto PAC = PA.getChecker<TestDoublyIndirectFunctionAnalysis>();
return !(PAC.preserved() ||
PAC.preservedSet<AllAnalysesOn<Function>>()) ||
Inv.invalidate<TestIndirectFunctionAnalysis>(F, PA);
}
};
TestDoublyIndirectFunctionAnalysis(int &Runs) : Runs(Runs) {}
Result run(Function &F, FunctionAnalysisManager &AM) {
++Runs;
auto &IDep = AM.getResult<TestIndirectFunctionAnalysis>(F);
return Result(IDep);
}
private:
friend AnalysisInfoMixin<TestDoublyIndirectFunctionAnalysis>;
static AnalysisKey Key;
int &Runs;
};
AnalysisKey TestDoublyIndirectFunctionAnalysis::Key;
struct LambdaPass : public PassInfoMixin<LambdaPass> {
using FuncT = std::function<PreservedAnalyses(Function &, FunctionAnalysisManager &)>;
LambdaPass(FuncT Func) : Func(std::move(Func)) {}
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) {
return Func(F, AM);
}
FuncT Func;
};
TEST_F(PassManagerTest, IndirectAnalysisInvalidation) {
FunctionAnalysisManager FAM;
ModuleAnalysisManager MAM;
int FunctionAnalysisRuns = 0, ModuleAnalysisRuns = 0,
IndirectAnalysisRuns = 0, DoublyIndirectAnalysisRuns = 0;
FAM.registerPass([&] { return TestFunctionAnalysis(FunctionAnalysisRuns); });
FAM.registerPass(
[&] { return TestIndirectFunctionAnalysis(IndirectAnalysisRuns, MAM); });
FAM.registerPass([&] {
return TestDoublyIndirectFunctionAnalysis(DoublyIndirectAnalysisRuns);
});
MAM.registerPass([&] { return TestModuleAnalysis(ModuleAnalysisRuns); });
MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
PassInstrumentationCallbacks PIC;
MAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
FAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
int InstrCount = 0, FunctionCount = 0;
ModulePassManager MPM;
FunctionPassManager FPM;
FPM.addPass(LambdaPass([&](Function &F, FunctionAnalysisManager &AM) {
auto &DoublyIndirectResult =
AM.getResult<TestDoublyIndirectFunctionAnalysis>(F);
auto &IndirectResult = DoublyIndirectResult.IDep;
InstrCount += IndirectResult.FDep.InstructionCount;
FunctionCount += IndirectResult.MDep.FunctionCount;
return PreservedAnalyses::all();
}));
FPM.addPass(LambdaPass([&](Function &F, FunctionAnalysisManager &AM) {
auto &DoublyIndirectResult =
AM.getResult<TestDoublyIndirectFunctionAnalysis>(F);
auto &IndirectResult = DoublyIndirectResult.IDep;
InstrCount += IndirectResult.FDep.InstructionCount;
FunctionCount += IndirectResult.MDep.FunctionCount;
auto PA = PreservedAnalyses::none();
if (F.getName() == "g")
PA.preserve<TestFunctionAnalysis>();
else if (F.getName() == "h")
PA.preserve<TestIndirectFunctionAnalysis>();
return PA;
}));
FPM.addPass(LambdaPass([&](Function &F, FunctionAnalysisManager &AM) {
auto &DoublyIndirectResult =
AM.getResult<TestDoublyIndirectFunctionAnalysis>(F);
auto &IndirectResult = DoublyIndirectResult.IDep;
InstrCount += IndirectResult.FDep.InstructionCount;
FunctionCount += IndirectResult.MDep.FunctionCount;
return PreservedAnalyses::all();
}));
FunctionPassManager FPM2;
FPM2.addPass(LambdaPass([&](Function &F, FunctionAnalysisManager &AM) {
auto &DoublyIndirectResult =
AM.getResult<TestDoublyIndirectFunctionAnalysis>(F);
auto &IndirectResult = DoublyIndirectResult.IDep;
InstrCount += IndirectResult.FDep.InstructionCount;
FunctionCount += IndirectResult.MDep.FunctionCount;
return PreservedAnalyses::all();
}));
MPM.addPass(RequireAnalysisPass<TestModuleAnalysis, Module>());
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
MPM.addPass(RequireAnalysisPass<TestModuleAnalysis, Module>());
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM2)));
MPM.run(*M, MAM);
EXPECT_EQ(5, FunctionAnalysisRuns);
EXPECT_EQ(2, ModuleAnalysisRuns);
EXPECT_EQ(9, IndirectAnalysisRuns);
EXPECT_EQ(9, DoublyIndirectAnalysisRuns);
EXPECT_EQ(5 * 4, InstrCount);
EXPECT_EQ(3 * 4 * 3, FunctionCount);
}
TEST_F(PassManagerTest, FunctionPassCFGChecker) {
LLVMContext Context;
auto M = parseIR(Context, "define void @foo() {\n"
" br label %next\n"
"next:\n"
" br label %exit\n"
"exit:\n"
" ret void\n"
"}\n");
auto *F = M->getFunction("foo");
FunctionAnalysisManager FAM;
FunctionPassManager FPM;
PassInstrumentationCallbacks PIC;
StandardInstrumentations SI( true);
SI.registerCallbacks(PIC, &FAM);
FAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
FAM.registerPass([&] { return DominatorTreeAnalysis(); });
FAM.registerPass([&] { return AssumptionAnalysis(); });
FAM.registerPass([&] { return TargetIRAnalysis(); });
FPM.addPass(SimplifyCFGPass());
FPM.run(*F, FAM);
}
struct TestSimplifyCFGInvalidatingAnalysisPass
: PassInfoMixin<TestSimplifyCFGInvalidatingAnalysisPass> {
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM) {
PreservedAnalyses PA = CFGSimplifier.run(F, FAM);
FAM.invalidate(F, PA);
return PreservedAnalyses::all();
}
SimplifyCFGPass CFGSimplifier;
};
TEST_F(PassManagerTest, FunctionPassCFGCheckerInvalidateAnalysis) {
LLVMContext Context;
auto M = parseIR(Context, "define void @foo() {\n"
" br label %next\n"
"next:\n"
" br label %exit\n"
"exit:\n"
" ret void\n"
"}\n");
auto *F = M->getFunction("foo");
FunctionAnalysisManager FAM;
FunctionPassManager FPM;
PassInstrumentationCallbacks PIC;
StandardInstrumentations SI( true);
SI.registerCallbacks(PIC, &FAM);
FAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
FAM.registerPass([&] { return DominatorTreeAnalysis(); });
FAM.registerPass([&] { return AssumptionAnalysis(); });
FAM.registerPass([&] { return TargetIRAnalysis(); });
FPM.addPass(TestSimplifyCFGInvalidatingAnalysisPass());
FPM.run(*F, FAM);
}
struct TestSimplifyCFGWrapperPass : PassInfoMixin<TestSimplifyCFGWrapperPass> {
TestSimplifyCFGWrapperPass(FunctionPassManager &InnerPM) : InnerPM(InnerPM) {}
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM) {
PreservedAnalyses PA = PreservedAnalyses::all();
PassInstrumentation PI = FAM.getResult<PassInstrumentationAnalysis>(F);
if (!PI.runBeforePass<Function>(InnerPM, F))
return PreservedAnalyses::all();
PreservedAnalyses PassPA = InnerPM.run(F, FAM);
PI.runAfterPass(InnerPM, F, PassPA);
FAM.invalidate(F, PassPA);
PA.intersect(PassPA);
PA.preserveSet<AllAnalysesOn<Function>>();
return PA;
}
FunctionPassManager &InnerPM;
};
TEST_F(PassManagerTest, FunctionPassCFGCheckerWrapped) {
LLVMContext Context;
auto M = parseIR(Context, "define void @foo() {\n"
" br label %next\n"
"next:\n"
" br label %exit\n"
"exit:\n"
" ret void\n"
"}\n");
auto *F = M->getFunction("foo");
FunctionAnalysisManager FAM;
FunctionPassManager FPM;
PassInstrumentationCallbacks PIC;
StandardInstrumentations SI( true);
SI.registerCallbacks(PIC, &FAM);
FAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
FAM.registerPass([&] { return DominatorTreeAnalysis(); });
FAM.registerPass([&] { return AssumptionAnalysis(); });
FAM.registerPass([&] { return TargetIRAnalysis(); });
FunctionPassManager InnerFPM;
InnerFPM.addPass(SimplifyCFGPass());
FPM.addPass(TestSimplifyCFGWrapperPass(InnerFPM));
FPM.run(*F, FAM);
}
}