From 2902bdeea1a58f9d14c92da966b027cee516dfd8 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sat, 13 Mar 2021 11:37:31 +0100 Subject: [PATCH] [MemCpyOpt] Use AA to check for MustAlias between memset and memcpy Rather than checking for simple equality, check for MustAlias, as we do in other transforms. This catches equivalent GEPs. --- llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp | 2 +- llvm/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp index 6b1c168..f2f11c2 100644 --- a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp +++ b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp @@ -1134,7 +1134,7 @@ bool MemCpyOptPass::processMemCpyMemCpyDependence(MemCpyInst *M, bool MemCpyOptPass::processMemSetMemCpyDependence(MemCpyInst *MemCpy, MemSetInst *MemSet) { // We can only transform memset/memcpy with the same destination. - if (MemSet->getDest() != MemCpy->getDest()) + if (!AA->isMustAlias(MemSet->getDest(), MemCpy->getDest())) return false; // Check that src and dst of the memcpy aren't the same. While memcpy diff --git a/llvm/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll b/llvm/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll index ce313ff..10ec8b4 100644 --- a/llvm/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll +++ b/llvm/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll @@ -261,7 +261,6 @@ define void @test_same_dynamic_size(i8* noalias %src, i8* noalias %dst, i64 %siz define void @test_must_alias_same_size(i8* noalias %src, i8* noalias %dst, i8 %c) { ; CHECK-LABEL: @test_must_alias_same_size( ; CHECK-NEXT: [[GEP1:%.*]] = getelementptr i8, i8* [[DST:%.*]], i64 16 -; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* [[GEP1]], i8 [[C:%.*]], i64 16, i1 false) ; CHECK-NEXT: [[GEP2:%.*]] = getelementptr i8, i8* [[DST]], i64 16 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[GEP2]], i8* [[SRC:%.*]], i64 16, i1 false) ; CHECK-NEXT: ret void @@ -276,9 +275,13 @@ define void @test_must_alias_same_size(i8* noalias %src, i8* noalias %dst, i8 %c define void @test_must_alias_different_size(i8* noalias %src, i64 %src_size, i8* noalias %dst, i64 %dst_size, i8 %c) { ; CHECK-LABEL: @test_must_alias_different_size( ; CHECK-NEXT: [[GEP1:%.*]] = getelementptr i8, i8* [[DST:%.*]], i64 16 -; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* [[GEP1]], i8 [[C:%.*]], i64 [[DST_SIZE:%.*]], i1 false) ; CHECK-NEXT: [[GEP2:%.*]] = getelementptr i8, i8* [[DST]], i64 16 -; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[GEP2]], i8* [[SRC:%.*]], i64 [[SRC_SIZE:%.*]], i1 false) +; CHECK-NEXT: [[TMP1:%.*]] = icmp ule i64 [[DST_SIZE:%.*]], [[SRC_SIZE:%.*]] +; CHECK-NEXT: [[TMP2:%.*]] = sub i64 [[DST_SIZE]], [[SRC_SIZE]] +; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP1]], i64 0, i64 [[TMP2]] +; CHECK-NEXT: [[TMP4:%.*]] = getelementptr i8, i8* [[GEP2]], i64 [[SRC_SIZE]] +; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* align 1 [[TMP4]], i8 [[C:%.*]], i64 [[TMP3]], i1 false) +; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[GEP2]], i8* [[SRC:%.*]], i64 [[SRC_SIZE]], i1 false) ; CHECK-NEXT: ret void ; %gep1 = getelementptr i8, i8* %dst, i64 16 -- 2.7.4