From aab71d44431eabfb65fbbc8a2930cb6d34b7e2be Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Sat, 31 Oct 2020 19:30:19 +0000 Subject: [PATCH] [DSE] Use same logic as legacy impl to check if free kills a location. This patch updates DSE + MemorySSA to use the same check as the legacy implementation to determine if a location is killed by a free call. This changes the existing behavior so that a free does not kill locations before the start of the freed pointer. This should fix PR48036. --- llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp | 8 ++++++-- llvm/test/Transforms/DeadStoreElimination/MSSA/free.ll | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp index e578d15..5ec732c 100644 --- a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp +++ b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp @@ -1857,9 +1857,13 @@ struct DSEState { getUnderlyingObject(MaybeTermLoc->first.Ptr)) return false; + auto TermLoc = MaybeTermLoc->first; + if (MaybeTermLoc->second) { + const Value *LocUO = getUnderlyingObject(Loc.Ptr); + return BatchAA.isMustAlias(TermLoc.Ptr, LocUO); + } int64_t InstWriteOffset, DepWriteOffset; - return MaybeTermLoc->second || - isOverwrite(MaybeTerm, AccessI, MaybeTermLoc->first, Loc, DL, TLI, + return isOverwrite(MaybeTerm, AccessI, TermLoc, Loc, DL, TLI, DepWriteOffset, InstWriteOffset, BatchAA, &F) == OW_Complete; } diff --git a/llvm/test/Transforms/DeadStoreElimination/MSSA/free.ll b/llvm/test/Transforms/DeadStoreElimination/MSSA/free.ll index a37f76d..4000115 100644 --- a/llvm/test/Transforms/DeadStoreElimination/MSSA/free.ll +++ b/llvm/test/Transforms/DeadStoreElimination/MSSA/free.ll @@ -90,9 +90,12 @@ bb: ; Test case inspired by PR48036. define void @delete_field_after(%struct* %ptr) { +; ; CHECK-LABEL: @delete_field_after( ; CHECK-NEXT: [[PTR_F0:%.*]] = getelementptr [[STRUCT:%.*]], %struct* [[PTR:%.*]], i32 1 ; CHECK-NEXT: [[BC:%.*]] = bitcast %struct* [[PTR_F0]] to i8* +; CHECK-NEXT: [[PTR_F1:%.*]] = getelementptr [[STRUCT]], %struct* [[PTR]], i32 0, i32 1 +; CHECK-NEXT: store i32 0, i32* [[PTR_F1]], align 4 ; CHECK-NEXT: call void @free(i8* [[BC]]) ; CHECK-NEXT: ret void ; -- 2.7.4