[llvm] 26f8198 - [DSE] Handle inaccessiblememonly calloc

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 19 03:55:17 PST 2022


Author: Nikita Popov
Date: 2022-01-19T12:55:09+01:00
New Revision: 26f81984e7972c17c4e7f97b1f5a3fc2f4d76ca7

URL: https://github.com/llvm/llvm-project/commit/26f81984e7972c17c4e7f97b1f5a3fc2f4d76ca7
DIFF: https://github.com/llvm/llvm-project/commit/26f81984e7972c17c4e7f97b1f5a3fc2f4d76ca7.diff

LOG: [DSE] Handle inaccessiblememonly calloc

Change the DSE calloc handling to assume that it is
inaccessiblememonly, i.e. the defining access is liveOnEntry.

Differential Revision: https://reviews.llvm.org/D117543

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
    llvm/test/Transforms/DeadStoreElimination/calloc-store.ll
    llvm/test/Transforms/DeadStoreElimination/noop-stores.ll
    llvm/test/Transforms/DeadStoreElimination/operand-bundles.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
index 2003918d94547..9ed6082903d94 100644
--- a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
@@ -1786,14 +1786,11 @@ struct DSEState {
       auto *CB = cast<CallBase>(DefUO);
       auto *InitC = getInitialValueOfAllocation(CB, &TLI,
                                                 StoredConstant->getType());
-      if (InitC && InitC == StoredConstant) {
-        auto *UnderlyingDef = cast<MemoryDef>(MSSA.getMemoryAccess(CB));
-        // If UnderlyingDef is the clobbering access of Def, no instructions
-        // between them can modify the memory location.
-        auto *ClobberDef =
-          MSSA.getSkipSelfWalker()->getClobberingMemoryAccess(Def);
-        return UnderlyingDef == ClobberDef;
-      }
+      // If the clobbering access is LiveOnEntry, no instructions between them
+      // can modify the memory location.
+      if (InitC && InitC == StoredConstant)
+        return MSSA.isLiveOnEntryDef(
+            MSSA.getSkipSelfWalker()->getClobberingMemoryAccess(Def));
     }
 
     if (!Store)

diff  --git a/llvm/test/Transforms/DeadStoreElimination/calloc-store.ll b/llvm/test/Transforms/DeadStoreElimination/calloc-store.ll
index 830d20f77fef7..26fe2b9074326 100644
--- a/llvm/test/Transforms/DeadStoreElimination/calloc-store.ll
+++ b/llvm/test/Transforms/DeadStoreElimination/calloc-store.ll
@@ -1,6 +1,6 @@
 ; RUN: opt < %s -basic-aa -dse -S | FileCheck %s
 
-declare noalias i8* @calloc(i64, i64)
+declare noalias i8* @calloc(i64, i64) inaccessiblememonly
 
 define i32* @test1() {
 ; CHECK-LABEL: test1

diff  --git a/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll b/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll
index 7a8d09b049d26..77b398f3b51ad 100644
--- a/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll
+++ b/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll
@@ -378,7 +378,7 @@ define i8* @notmalloc_memset(i64 %size, i8*(i64)* %notmalloc) {
 }
 
 ; This should not create recursive call to calloc.
-define i8* @calloc(i64 %nmemb, i64 %size) {
+define i8* @calloc(i64 %nmemb, i64 %size) inaccessiblememonly {
 ; CHECK-LABEL: @calloc(
 ; CHECK:       entry:
 ; CHECK-NEXT:    [[MUL:%.*]] = mul i64 [[SIZE:%.*]], [[NMEMB:%.*]]
@@ -488,7 +488,6 @@ cleanup:
 define i8* @store_zero_after_calloc_inaccessiblememonly() {
 ; CHECK-LABEL: @store_zero_after_calloc_inaccessiblememonly(
 ; CHECK-NEXT:    [[CALL:%.*]] = tail call i8* @calloc(i64 1, i64 10) #[[ATTR6:[0-9]+]]
-; CHECK-NEXT:    store i8 0, i8* [[CALL]], align 1
 ; CHECK-NEXT:    ret i8* [[CALL]]
 ;
   %call = tail call i8* @calloc(i64 1, i64 10)  inaccessiblememonly
@@ -582,7 +581,6 @@ define i8* @partial_zero_memset_and_store_with_dyn_index_after_calloc(i8 %v, i64
 define i8* @zero_memset_after_calloc_inaccessiblememonly()  {
 ; CHECK-LABEL: @zero_memset_after_calloc_inaccessiblememonly(
 ; CHECK-NEXT:    [[CALL:%.*]] = tail call i8* @calloc(i64 10000, i64 4) #[[ATTR6]]
-; CHECK-NEXT:    call void @llvm.memset.p0i8.i64(i8* [[CALL]], i8 0, i64 40000, i1 false)
 ; CHECK-NEXT:    ret i8* [[CALL]]
 ;
   %call = tail call i8* @calloc(i64 10000, i64 4) inaccessiblememonly

diff  --git a/llvm/test/Transforms/DeadStoreElimination/operand-bundles.ll b/llvm/test/Transforms/DeadStoreElimination/operand-bundles.ll
index f3df74be031b7..85a64c887dd6b 100644
--- a/llvm/test/Transforms/DeadStoreElimination/operand-bundles.ll
+++ b/llvm/test/Transforms/DeadStoreElimination/operand-bundles.ll
@@ -42,7 +42,7 @@ define void @test3() {
   ret void
 }
 
-declare noalias i8* @calloc(i64, i64)
+declare noalias i8* @calloc(i64, i64) inaccessiblememonly
 
 define void @test4() {
 ; CHECK-LABEL: @test4


        


More information about the llvm-commits mailing list