[llvm] 2902bde - [MemCpyOpt] Use AA to check for MustAlias between memset and memcpy
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Sat Mar 13 02:43:40 PST 2021
Author: Nikita Popov
Date: 2021-03-13T11:41:15+01:00
New Revision: 2902bdeea1a58f9d14c92da966b027cee516dfd8
URL: https://github.com/llvm/llvm-project/commit/2902bdeea1a58f9d14c92da966b027cee516dfd8
DIFF: https://github.com/llvm/llvm-project/commit/2902bdeea1a58f9d14c92da966b027cee516dfd8.diff
LOG: [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.
Added:
Modified:
llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
llvm/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
index 6b1c168967d2..f2f11c2d142c 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 ce313ffe93a0..10ec8b46dbd2 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_
diff erent_size(i8* noalias %src, i64 %src_size, i8* noalias %dst, i64 %dst_size, i8 %c) {
; CHECK-LABEL: @test_must_alias_
diff erent_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
More information about the llvm-commits
mailing list