diff --git a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp --- a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp +++ b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp @@ -1770,6 +1770,26 @@ bool storeIsNoop(MemoryDef *Def, const MemoryLocation &DefLoc, const Value *DefUO) { StoreInst *Store = dyn_cast(Def->getMemoryInst()); + MemSetInst *MemSet = dyn_cast(Def->getMemoryInst()); + Constant *StoredConstant = nullptr; + if (Store) + StoredConstant = dyn_cast(Store->getOperand(0)); + if (MemSet) + StoredConstant = dyn_cast(MemSet->getValue()); + + if (StoredConstant && StoredConstant->isNullValue()) { + auto *DefUOInst = dyn_cast(DefUO); + if (DefUOInst && isCallocLikeFn(DefUOInst, &TLI)) { + + auto *UnderlyingDef = cast(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 +1837,6 @@ } } - Constant *StoredConstant = dyn_cast(Store->getOperand(0)); - if (StoredConstant && StoredConstant->isNullValue()) { - auto *DefUOInst = dyn_cast(DefUO); - if (DefUOInst && isCallocLikeFn(DefUOInst, &TLI)) { - auto *UnderlyingDef = cast(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 --- a/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll +++ b/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll @@ -325,7 +325,6 @@ ; CHECK-LABEL: @zero_memset_after_calloc( ; CHECK-NEXT: [[CALL:%.*]] = tail call i8* @calloc(i64 10000, i64 4) ; CHECK-NEXT: [[L0:%.*]] = bitcast i8* [[CALL]] to i32* -; 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) @@ -338,7 +337,6 @@ ; CHECK-LABEL: @zero_memset_and_store_after_calloc( ; CHECK-NEXT: [[CALL:%.*]] = tail call i8* @calloc(i64 10000, i64 4) ; CHECK-NEXT: [[L0:%.*]] = bitcast i8* [[CALL]] to i32* -; 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) @@ -379,7 +377,6 @@ ; CHECK-LABEL: @zero_memset_after_calloc_smaller_size( ; CHECK-NEXT: [[CALL:%.*]] = tail call i8* @calloc(i64 10000, i64 4) ; CHECK-NEXT: [[L0:%.*]] = bitcast i8* [[CALL]] to i32* -; 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) @@ -395,7 +392,6 @@ ; CHECK-LABEL: @memset_pattern16_after_calloc( ; CHECK-NEXT: [[CALL:%.*]] = tail call i8* @calloc(i64 10000, i64 4) ; CHECK-NEXT: [[L0:%.*]] = bitcast i8* [[CALL]] to i32* -; 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]] ;