[llvm] e818193 - [DSE] Eliminate zero memset after calloc
Dávid Bolvanský via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 27 18:31:50 PDT 2021
Author: Dávid Bolvanský
Date: 2021-04-28T03:30:52+02:00
New Revision: e81819377ececef53d3e90743eeb44e6a089b65a
URL: https://github.com/llvm/llvm-project/commit/e81819377ececef53d3e90743eeb44e6a089b65a
DIFF: https://github.com/llvm/llvm-project/commit/e81819377ececef53d3e90743eeb44e6a089b65a.diff
LOG: [DSE] Eliminate zero memset after calloc
Solves PR11896
As noted, this can be improved futher (calloc -> malloc) in some cases. But for know, this is the first step.
Reviewed By: nikic
Differential Revision: https://reviews.llvm.org/D101391
Added:
Modified:
llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
llvm/test/Transforms/DeadStoreElimination/noop-stores.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
index d3052e0a4cc3..6922d84db4e9 100644
--- a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
@@ -1770,6 +1770,25 @@ struct DSEState {
bool storeIsNoop(MemoryDef *Def, const MemoryLocation &DefLoc,
const Value *DefUO) {
StoreInst *Store = dyn_cast<StoreInst>(Def->getMemoryInst());
+ MemSetInst *MemSet = dyn_cast<MemSetInst>(Def->getMemoryInst());
+ Constant *StoredConstant = nullptr;
+ if (Store)
+ StoredConstant = dyn_cast<Constant>(Store->getOperand(0));
+ if (MemSet)
+ StoredConstant = dyn_cast<Constant>(MemSet->getValue());
+
+ if (StoredConstant && StoredConstant->isNullValue()) {
+ auto *DefUOInst = dyn_cast<Instruction>(DefUO);
+ if (DefUOInst && isCallocLikeFn(DefUOInst, &TLI)) {
+ auto *UnderlyingDef = cast<MemoryDef>(MSSA.getMemoryAccess(DefUOInst));
+ // 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 (!Store)
return false;
@@ -1817,18 +1836,6 @@ struct DSEState {
}
}
- Constant *StoredConstant = dyn_cast<Constant>(Store->getOperand(0));
- if (StoredConstant && StoredConstant->isNullValue()) {
- auto *DefUOInst = dyn_cast<Instruction>(DefUO);
- if (DefUOInst && isCallocLikeFn(DefUOInst, &TLI)) {
- auto *UnderlyingDef = cast<MemoryDef>(MSSA.getMemoryAccess(DefUOInst));
- // 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;
- }
- }
return false;
}
};
diff --git a/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll b/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll
index c07c41d05877..184653982a6a 100644
--- a/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll
+++ b/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll
@@ -324,7 +324,6 @@ define i8* @store_zero_after_calloc_inaccessiblememonly() {
define i8* @zero_memset_after_calloc() {
; CHECK-LABEL: @zero_memset_after_calloc(
; CHECK-NEXT: [[CALL:%.*]] = tail call i8* @calloc(i64 10000, i64 4)
-; 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)
@@ -332,10 +331,20 @@ define i8* @zero_memset_after_calloc() {
ret i8* %call
}
+define i8* @volatile_zero_memset_after_calloc() {
+; CHECK-LABEL: @volatile_zero_memset_after_calloc(
+; CHECK-NEXT: [[CALL:%.*]] = tail call i8* @calloc(i64 10000, i64 4)
+; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* [[CALL]], i8 0, i64 40000, i1 true)
+; CHECK-NEXT: ret i8* [[CALL]]
+;
+ %call = tail call i8* @calloc(i64 10000, i64 4)
+ call void @llvm.memset.p0i8.i64(i8* %call, i8 0, i64 40000, i1 true)
+ ret i8* %call
+}
+
define i8* @zero_memset_and_store_after_calloc(i8 %v) {
; CHECK-LABEL: @zero_memset_and_store_after_calloc(
; CHECK-NEXT: [[CALL:%.*]] = tail call i8* @calloc(i64 10000, i64 4)
-; 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)
@@ -347,7 +356,6 @@ define i8* @zero_memset_and_store_after_calloc(i8 %v) {
define i8* @partial_zero_memset_after_calloc() {
; CHECK-LABEL: @partial_zero_memset_after_calloc(
; CHECK-NEXT: [[CALL:%.*]] = tail call i8* @calloc(i64 10000, i64 4)
-; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* [[CALL]], i8 0, i64 20, i1 false)
; CHECK-NEXT: ret i8* [[CALL]]
;
%call = tail call i8* @calloc(i64 10000, i64 4)
@@ -360,7 +368,6 @@ define i8* @partial_zero_memset_and_store_after_calloc(i8 %v) {
; CHECK-NEXT: [[CALL:%.*]] = tail call i8* @calloc(i64 10000, i64 4)
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, i8* [[CALL]], i64 30
; CHECK-NEXT: store i8 [[V:%.*]], i8* [[GEP]], align 1
-; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* [[CALL]], i8 0, i64 20, i1 false)
; CHECK-NEXT: ret i8* [[CALL]]
;
%call = tail call i8* @calloc(i64 10000, i64 4)
@@ -373,7 +380,6 @@ define i8* @partial_zero_memset_and_store_after_calloc(i8 %v) {
define i8* @zero_memset_and_store_with_dyn_index_after_calloc(i8 %v, i64 %idx) {
; CHECK-LABEL: @zero_memset_and_store_with_dyn_index_after_calloc(
; CHECK-NEXT: [[CALL:%.*]] = tail call i8* @calloc(i64 10000, i64 4)
-; 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)
@@ -409,6 +415,16 @@ define i8* @zero_memset_after_calloc_inaccessiblememonly() {
ret i8* %call
}
+define i8* @cst_nonzero_memset_after_calloc() {
+; CHECK-LABEL: @cst_nonzero_memset_after_calloc(
+; CHECK-NEXT: [[CALL:%.*]] = tail call i8* @calloc(i64 10000, i64 4)
+; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* [[CALL]], i8 1, i64 40000, i1 false)
+; CHECK-NEXT: ret i8* [[CALL]]
+;
+ %call = tail call i8* @calloc(i64 10000, i64 4)
+ call void @llvm.memset.p0i8.i64(i8* %call, i8 1, i64 40000, i1 false)
+ ret i8* %call
+}
define i8* @nonzero_memset_after_calloc(i8 %v) {
; CHECK-LABEL: @nonzero_memset_after_calloc(
@@ -427,7 +443,6 @@ define i8* @nonzero_memset_after_calloc(i8 %v) {
define i8* @memset_pattern16_after_calloc(i8* %pat) {
; CHECK-LABEL: @memset_pattern16_after_calloc(
; CHECK-NEXT: [[CALL:%.*]] = tail call i8* @calloc(i64 10000, i64 4)
-; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* align 4 [[CALL]], i8 0, i64 40000, i1 false)
; CHECK-NEXT: call void @memset_pattern16(i8* [[CALL]], i8* [[PAT:%.*]], i64 40000)
; CHECK-NEXT: ret i8* [[CALL]]
;
More information about the llvm-commits
mailing list