[PATCH] D89190: [MemCpyOpt] Don't shorten memset if destination observable through unwinding

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 10 08:55:00 PDT 2020


nikic created this revision.
nikic added reviewers: efriedma, jdoerfert.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.
nikic requested review of this revision.

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.

As a side-note, at least for the constant size case this transform should really be performed by DSE, and it does have some support for this (isShortenableAtTheBeginning), but apparently doesn't catch the memset+memcpy combination.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D89190

Files:
  llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
  llvm/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll


Index: llvm/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll
===================================================================
--- llvm/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll
+++ llvm/test/Transforms/MemCpyOpt/memset-memcpy-redundant-memset.ll
@@ -193,13 +193,9 @@
 
 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)
Index: llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
+++ llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
@@ -1050,6 +1050,16 @@
   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 (!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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D89190.297415.patch
Type: text/x-patch
Size: 2206 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201010/56cb5532/attachment.bin>


More information about the llvm-commits mailing list