[PATCH] D31569: [MemCpyOpt] Only replace memcpy with bitcast if address spaces match

James Price via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 1 08:01:24 PDT 2017


jprice created this revision.
Herald added a subscriber: wdng.

Without this check, invalid bitcast instructions that change the address space are generated for the added test.

Similar to https://reviews.llvm.org/D30902 by @escha, just in a different MemCpyOpt optimisation.


https://reviews.llvm.org/D31569

Files:
  lib/Transforms/Scalar/MemCpyOptimizer.cpp
  test/Transforms/MemCpyOpt/memcpy.ll


Index: test/Transforms/MemCpyOpt/memcpy.ll
===================================================================
--- test/Transforms/MemCpyOpt/memcpy.ll
+++ test/Transforms/MemCpyOpt/memcpy.ll
@@ -76,8 +76,21 @@
 ; CHECK-NEXT: call void @test4a(
 }
 
+; Make sure we don't remove the memcpy if the source address space doesn't match the byval argument
+define void @test4_addrspace(i8 addrspace(1)* %P) {
+  %A = alloca %1
+  %a = bitcast %1* %A to i8*
+  call void @llvm.memcpy.p0i8.p1i8.i64(i8* %a, i8 addrspace(1)* %P, i64 8, i32 4, i1 false)
+  call void @test4a(i8* align 1 byval %a)
+  ret void
+; CHECK-LABEL: @test4_addrspace(
+; CHECK: call void @llvm.memcpy.p0i8.p1i8.i64(
+; CHECK-NEXT: call void @test4a(
+}
+
 declare void @test4a(i8* align 1 byval)
 declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind
+declare void @llvm.memcpy.p0i8.p1i8.i64(i8* nocapture, i8 addrspace(1)* nocapture, i64, i32, i1) nounwind
 declare void @llvm.memcpy.p1i8.p1i8.i64(i8 addrspace(1)* nocapture, i8 addrspace(1)* nocapture, i64, i32, i1) nounwind
 
 %struct.S = type { i128, [4 x i8]}
Index: lib/Transforms/Scalar/MemCpyOptimizer.cpp
===================================================================
--- lib/Transforms/Scalar/MemCpyOptimizer.cpp
+++ lib/Transforms/Scalar/MemCpyOptimizer.cpp
@@ -1335,6 +1335,11 @@
                                  CS.getInstruction(), &AC, &DT) < ByValAlign)
     return false;
 
+  // The address space of the memcpy source must match the byval argument
+  if (MDep->getSource()->getType()->getPointerAddressSpace() !=
+      ByValArg->getType()->getPointerAddressSpace())
+    return false;
+
   // Verify that the copied-from memory doesn't change in between the memcpy and
   // the byval call.
   //    memcpy(a <- b)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D31569.93746.patch
Type: text/x-patch
Size: 1795 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170401/d6f407c5/attachment.bin>


More information about the llvm-commits mailing list