[llvm] 39c39e8 - [MemCpyOpt] Don't shorten memset if destination observable through unwinding
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 13 12:12:33 PDT 2020
Author: Nikita Popov
Date: 2020-10-13T21:12:19+02:00
New Revision: 39c39e8a7f09ff8f0385ef9962d1075d9559d139
URL: https://github.com/llvm/llvm-project/commit/39c39e8a7f09ff8f0385ef9962d1075d9559d139
DIFF: https://github.com/llvm/llvm-project/commit/39c39e8a7f09ff8f0385ef9962d1075d9559d139.diff
LOG: [MemCpyOpt] Don't shorten memset if destination observable through unwinding
MemCpyOpt can shorten a memset if it is later partially overwritten
by a memcpy. It checks that the destination is not read in between,
but we also need to make sure that the destination cannot be observed
via unwinding.
Differential Revision: https://reviews.llvm.org/D89190
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 c7bdf860f14b..9f9e9dfa1afd 100644
--- a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
+++ b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
@@ -1050,6 +1050,17 @@ bool MemCpyOptPass::processMemSetMemCpyDependence(MemCpyInst *MemCpy,
Value *DestSize = MemSet->getLength();
Value *SrcSize = MemCpy->getLength();
+ // If the destination might be accessible by the caller, make sure we cannot
+ // unwind between the memset and the memcpy.
+ if (!MemCpy->getFunction()->doesNotThrow() &&
+ !isa<AllocaInst>(getUnderlyingObject(Dest))) {
+ for (const Instruction &I :
+ make_range(MemSet->getIterator(), MemCpy->getIterator())) {
+ if (I.mayThrow())
+ return false;
+ }
+ }
+
// By default, create an unaligned memset.
unsigned Align = 1;
// If Dest is aligned, and SrcSize is constant, use the minimum alignment
diff --git a/llvm/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll b/llvm/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll
index 4f9c8be32437..687f312fa15b 100644
--- a/llvm/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll
+++ b/llvm/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll
@@ -193,13 +193,9 @@ define void @test_intermediate_write(i8* %b) #0 {
define void @test_throwing_call(i8* %src, i64 %src_size, i8* %dst, i64 %dst_size, i8 %c) {
; CHECK-LABEL: @test_throwing_call(
+; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* [[DST:%.*]], i8 [[C:%.*]], i64 [[DST_SIZE:%.*]], i1 false)
; CHECK-NEXT: call void @call() [[ATTR2:#.*]]
-; 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* [[DST:%.*]], 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* [[DST]], i8* [[SRC:%.*]], i64 [[SRC_SIZE]], i1 false)
+; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[DST]], i8* [[SRC:%.*]], i64 [[SRC_SIZE:%.*]], i1 false)
; CHECK-NEXT: ret void
;
call void @llvm.memset.p0i8.i64(i8* %dst, i8 %c, i64 %dst_size, i1 false)
More information about the llvm-commits
mailing list