From e14524ca30735e5e9aeee8ac3173f37ec5ed68f8 Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Tue, 27 Dec 2016 19:33:04 +0000 Subject: [PATCH] [PM] Teach MemDep to invalidate its result object when its cached analysis handles become invalid. Add a test case for its invalidation logic. llvm-svn: 290620 --- .../llvm/Analysis/MemoryDependenceAnalysis.h | 4 + .../lib/Analysis/MemoryDependenceAnalysis.cpp | 18 +++++ .../MemoryDependenceAnalysis/invalidation.ll | 76 +++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 llvm/test/Analysis/MemoryDependenceAnalysis/invalidation.ll diff --git a/llvm/include/llvm/Analysis/MemoryDependenceAnalysis.h b/llvm/include/llvm/Analysis/MemoryDependenceAnalysis.h index 750266d79a5a..33dbd22f7a20 100644 --- a/llvm/include/llvm/Analysis/MemoryDependenceAnalysis.h +++ b/llvm/include/llvm/Analysis/MemoryDependenceAnalysis.h @@ -350,6 +350,10 @@ public: DominatorTree &DT) : AA(AA), AC(AC), TLI(TLI), DT(DT) {} + /// Handle invalidation in the new PM. + bool invalidate(Function &F, const PreservedAnalyses &PA, + FunctionAnalysisManager::Invalidator &Inv); + /// Some methods limit the number of instructions they will examine. /// The return value of this method is the default limit that will be /// used if no limit is explicitly passed in. diff --git a/llvm/lib/Analysis/MemoryDependenceAnalysis.cpp b/llvm/lib/Analysis/MemoryDependenceAnalysis.cpp index 647b1dec7c8c..74621f51ba97 100644 --- a/llvm/lib/Analysis/MemoryDependenceAnalysis.cpp +++ b/llvm/lib/Analysis/MemoryDependenceAnalysis.cpp @@ -1683,6 +1683,24 @@ void MemoryDependenceWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { AU.addRequiredTransitive(); } +bool MemoryDependenceResults::invalidate(Function &F, const PreservedAnalyses &PA, + FunctionAnalysisManager::Invalidator &Inv) { + // Check whether our analysis is preserved. + auto PAC = PA.getChecker(); + if (!PAC.preserved() && !PAC.preservedSet>()) + // If not, give up now. + return true; + + // Check whether the analyses we depend on became invalid for any reason. + if (Inv.invalidate(F, PA) || + Inv.invalidate(F, PA) || + Inv.invalidate(F, PA)) + return true; + + // Otherwise this analysis result remains valid. + return false; +} + unsigned MemoryDependenceResults::getDefaultBlockScanLimit() const { return BlockScanLimit; } diff --git a/llvm/test/Analysis/MemoryDependenceAnalysis/invalidation.ll b/llvm/test/Analysis/MemoryDependenceAnalysis/invalidation.ll new file mode 100644 index 000000000000..6e5d4a4bf846 --- /dev/null +++ b/llvm/test/Analysis/MemoryDependenceAnalysis/invalidation.ll @@ -0,0 +1,76 @@ +; Test that memdep gets invalidated when the analyses it depends on are +; invalidated. +; +; Check AA specifically. +; RUN: opt -disable-output -debug-pass-manager -aa-pipeline='basic-aa' %s 2>&1 \ +; RUN: -passes='require,invalidate,gvn' \ +; RUN: | FileCheck %s --check-prefix=CHECK-AA-INVALIDATE +; CHECK-AA-INVALIDATE: Running pass: RequireAnalysisPass +; CHECK-AA-INVALIDATE: Running analysis: MemoryDependenceAnalysis +; CHECK-AA-INVALIDATE: Running pass: InvalidateAnalysisPass +; CHECK-AA-INVALIDATE: Invalidating analysis: AAManager +; CHECK-AA-INVALIDATE: Invalidating analysis: MemoryDependenceAnalysis +; CHECK-AA-INVALIDATE: Running pass: GVN +; CHECK-AA-INVALIDATE: Running analysis: MemoryDependenceAnalysis +; +; Check the assumptions analysis specifically. +; FIXME: We don't have any test cases that actually fail if the assumption +; cache becomes stale. This just tests what we believe to be correct. +; RUN: opt -disable-output -debug-pass-manager %s 2>&1 \ +; RUN: -passes='require,invalidate,gvn' \ +; RUN: | FileCheck %s --check-prefix=CHECK-ASSUMPTIONS-INVALIDATE +; CHECK-ASSUMPTIONS-INVALIDATE: Running pass: RequireAnalysisPass +; CHECK-ASSUMPTIONS-INVALIDATE: Running analysis: MemoryDependenceAnalysis +; CHECK-ASSUMPTIONS-INVALIDATE: Running pass: InvalidateAnalysisPass +; CHECK-ASSUMPTIONS-INVALIDATE: Invalidating analysis: AssumptionAnalysis +; CHECK-ASSUMPTIONS-INVALIDATE: Invalidating analysis: MemoryDependenceAnalysis +; CHECK-ASSUMPTIONS-INVALIDATE: Running pass: GVN +; CHECK-ASSUMPTIONS-INVALIDATE: Running analysis: MemoryDependenceAnalysis +; +; Check domtree specifically. +; RUN: opt -disable-output -debug-pass-manager %s 2>&1 \ +; RUN: -passes='require,invalidate,gvn' \ +; RUN: | FileCheck %s --check-prefix=CHECK-DT-INVALIDATE +; CHECK-DT-INVALIDATE: Running pass: RequireAnalysisPass +; CHECK-DT-INVALIDATE: Running analysis: MemoryDependenceAnalysis +; CHECK-DT-INVALIDATE: Running pass: InvalidateAnalysisPass +; CHECK-DT-INVALIDATE: Invalidating analysis: DominatorTreeAnalysis +; CHECK-DT-INVALIDATE: Invalidating analysis: MemoryDependenceAnalysis +; CHECK-DT-INVALIDATE: Running pass: GVN +; CHECK-DT-INVALIDATE: Running analysis: MemoryDependenceAnalysis +; + +define void @test_use_domtree(i32* nocapture %bufUInt, i32* nocapture %pattern) nounwind { +entry: + br label %for.body + +for.exit: ; preds = %for.body + ret void + +for.body: ; preds = %for.body, %entry + %i.01 = phi i32 [ 0, %entry ], [ %tmp8.7, %for.body ] + %arrayidx = getelementptr i32, i32* %bufUInt, i32 %i.01 + %arrayidx5 = getelementptr i32, i32* %pattern, i32 %i.01 + %tmp6 = load i32, i32* %arrayidx5, align 4 + store i32 %tmp6, i32* %arrayidx, align 4 + %tmp8.7 = add i32 %i.01, 8 + %cmp.7 = icmp ult i32 %tmp8.7, 1024 + br i1 %cmp.7, label %for.body, label %for.exit +} + +%t = type { i32 } +declare void @foo(i8*) + +define void @test_use_aa(%t* noalias %stuff ) { +entry: + %p = getelementptr inbounds %t, %t* %stuff, i32 0, i32 0 + %before = load i32, i32* %p + + call void @foo(i8* null) + + %after = load i32, i32* %p + %sum = add i32 %before, %after + + store i32 %sum, i32* %p + ret void +}