From: Alina Sbirlea Date: Fri, 20 Aug 2021 17:33:33 +0000 (-0700) Subject: [DSE] Check post-dominance for malloc+memset->calloc transform. X-Git-Tag: upstream/15.0.7~33234 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e8723abf43c37aff4d6d9b6ba131e345b74c7798;p=platform%2Fupstream%2Fllvm.git [DSE] Check post-dominance for malloc+memset->calloc transform. Aiming to address the regression discussed in https://reviews.llvm.org/D103009. Differential Revision: https://reviews.llvm.org/D108485 --- diff --git a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp index 1bc4a38..a6b0fce 100644 --- a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp +++ b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp @@ -1846,7 +1846,7 @@ struct DSEState { Func != LibFunc_malloc) return false; if (Malloc->getOperand(0) == MemSet->getLength()) { - if (DT.dominates(Malloc, MemSet) && + if (DT.dominates(Malloc, MemSet) && PDT.dominates(MemSet, Malloc) && memoryIsNotModifiedBetween(Malloc, MemSet, BatchAA, DL, &DT)) { IRBuilder<> IRB(Malloc); const auto &DL = Malloc->getModule()->getDataLayout(); diff --git a/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll b/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll index 12534b6..38072c1 100644 --- a/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll +++ b/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll @@ -405,14 +405,17 @@ if.end: ; preds = %if.then, %entry ret i8* %call } +; FIXME: malloc+memset are not currently transformed into calloc unless the +; memset post-dominates the malloc. define float* @pr25892(i64 %size) { ; CHECK-LABEL: @pr25892( ; CHECK: entry: -; CHECK-NEXT: [[CALL:%.*]] = call i8* @calloc(i64 1, i64 [[SIZE:%.*]]) +; CHECK-NEXT: [[CALL:%.*]] = call i8* @malloc(i64 [[SIZE:%.*]]) ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8* [[CALL]], null ; CHECK-NEXT: br i1 [[CMP]], label [[CLEANUP:%.*]], label [[IF_END:%.*]] ; CHECK: if.end: ; CHECK-NEXT: [[BC:%.*]] = bitcast i8* [[CALL]] to float* +; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* %call, i8 0, i64 %size, i1 false) ; CHECK-NEXT: br label [[CLEANUP]] ; CHECK: cleanup: ; CHECK-NEXT: [[RETVAL_0:%.*]] = phi float* [ [[BC]], [[IF_END]] ], [ null, [[ENTRY:%.*]] ] @@ -434,11 +437,13 @@ cleanup: define float* @pr25892_with_extra_store(i64 %size) { ; CHECK-LABEL: @pr25892_with_extra_store( ; CHECK: entry: -; CHECK-NEXT: [[CALL:%.*]] = call i8* @calloc(i64 1, i64 [[SIZE:%.*]]) +; CHECK-NEXT: [[CALL:%.*]] = call i8* @malloc(i64 [[SIZE:%.*]]) ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8* [[CALL]], null ; CHECK-NEXT: br i1 [[CMP]], label [[CLEANUP:%.*]], label [[IF_END:%.*]] ; CHECK: if.end: ; CHECK-NEXT: [[BC:%.*]] = bitcast i8* [[CALL]] to float* +; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* %call, i8 0, i64 %size, i1 false) +; CHECK-NEXT: store i8 0, i8* %call, align 1 ; CHECK-NEXT: br label [[CLEANUP]] ; CHECK: cleanup: ; CHECK-NEXT: [[RETVAL_0:%.*]] = phi float* [ [[BC]], [[IF_END]] ], [ null, [[ENTRY:%.*]] ]