[DSE] Use same logic as legacy impl to check if free kills a location.
authorFlorian Hahn <flo@fhahn.com>
Sat, 31 Oct 2020 19:30:19 +0000 (19:30 +0000)
committerFlorian Hahn <flo@fhahn.com>
Sat, 31 Oct 2020 20:09:25 +0000 (20:09 +0000)
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
llvm/test/Transforms/DeadStoreElimination/MSSA/free.ll

index e578d15..5ec732c 100644 (file)
@@ -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;
   }
index a37f76d..4000115 100644 (file)
@@ -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
 ;