[MemCpyOpt] Look through pointer casts when checking capture
authorNikita Popov <npopov@redhat.com>
Wed, 5 Jan 2022 08:48:17 +0000 (09:48 +0100)
committerNikita Popov <npopov@redhat.com>
Wed, 5 Jan 2022 08:50:33 +0000 (09:50 +0100)
The user scanning loop above looks through pointer casts, so we
also need to strip pointer casts in the capture check. Previously
the source was incorrectly considered not captured if a bitcast
was passed to the call.

llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
llvm/test/Transforms/MemCpyOpt/callslot.ll
llvm/test/Transforms/MemCpyOpt/capturing-func.ll

index d6723ff..1384740 100644 (file)
@@ -949,7 +949,8 @@ bool MemCpyOptPass::performCallSlotOptzn(Instruction *cpyLoad,
   // Check whether src is captured by the called function, in which case there
   // may be further indirect uses of src.
   bool SrcIsCaptured = any_of(C->args(), [&](Use &U) {
-    return U == cpySrc && !C->doesNotCapture(C->getArgOperandNo(&U));
+    return U->stripPointerCasts() == cpySrc &&
+           !C->doesNotCapture(C->getArgOperandNo(&U));
   });
 
   // If src is captured, then check whether there are any potential uses of
index c118646..5cb5702 100644 (file)
@@ -172,7 +172,7 @@ define void @capture_before_call_argmemonly() {
 ; CHECK-NEXT:    [[SRC_I8:%.*]] = bitcast [16 x i8]* [[SRC]] to i8*
 ; CHECK-NEXT:    call void @accept_ptr(i8* [[DEST_I8]])
 ; CHECK-NEXT:    [[DEST1:%.*]] = bitcast [16 x i8]* [[DEST]] to i8*
-; CHECK-NEXT:    call void @accept_ptr(i8* [[DEST1]]) #[[ATTR4:[0-9]+]]
+; CHECK-NEXT:    call void @accept_ptr(i8* nocapture [[DEST1]]) #[[ATTR4:[0-9]+]]
 ; CHECK-NEXT:    ret void
 ;
   %dest = alloca [16 x i8]
@@ -180,7 +180,7 @@ define void @capture_before_call_argmemonly() {
   %dest.i8 = bitcast [16 x i8]* %dest to i8*
   %src.i8 = bitcast [16 x i8]* %src to i8*
   call void @accept_ptr(i8* %dest.i8) ; capture
-  call void @accept_ptr(i8* %src.i8) argmemonly
+  call void @accept_ptr(i8* nocapture %src.i8) argmemonly
   call void @llvm.memcpy.p0i8.p0i8.i64(i8* %dest.i8, i8* %src.i8, i64 16, i1 false)
   ret void
 }
@@ -193,7 +193,7 @@ define void @capture_before_call_argmemonly_nounwind() {
 ; CHECK-NEXT:    [[SRC_I8:%.*]] = bitcast [16 x i8]* [[SRC]] to i8*
 ; CHECK-NEXT:    call void @accept_ptr(i8* [[DEST_I8]])
 ; CHECK-NEXT:    [[DEST1:%.*]] = bitcast [16 x i8]* [[DEST]] to i8*
-; CHECK-NEXT:    call void @accept_ptr(i8* [[DEST1]]) #[[ATTR5:[0-9]+]]
+; CHECK-NEXT:    call void @accept_ptr(i8* nocapture [[DEST1]]) #[[ATTR5:[0-9]+]]
 ; CHECK-NEXT:    ret void
 ;
   %dest = alloca [16 x i8]
@@ -202,7 +202,7 @@ define void @capture_before_call_argmemonly_nounwind() {
   %src.i8 = bitcast [16 x i8]* %src to i8*
   call void @accept_ptr(i8* %dest.i8) ; capture
   ; NB: argmemonly currently implies willreturn.
-  call void @accept_ptr(i8* %src.i8) argmemonly nounwind
+  call void @accept_ptr(i8* nocapture %src.i8) argmemonly nounwind
   call void @llvm.memcpy.p0i8.p0i8.i64(i8* %dest.i8, i8* %src.i8, i64 16, i1 false)
   ret void
 }
@@ -215,7 +215,7 @@ define void @capture_before_call_argmemonly_nounwind_willreturn() {
 ; CHECK-NEXT:    [[SRC_I8:%.*]] = bitcast [16 x i8]* [[SRC]] to i8*
 ; CHECK-NEXT:    call void @accept_ptr(i8* [[DEST_I8]])
 ; CHECK-NEXT:    [[DEST1:%.*]] = bitcast [16 x i8]* [[DEST]] to i8*
-; CHECK-NEXT:    call void @accept_ptr(i8* [[DEST1]]) #[[ATTR6:[0-9]+]]
+; CHECK-NEXT:    call void @accept_ptr(i8* nocapture [[DEST1]]) #[[ATTR6:[0-9]+]]
 ; CHECK-NEXT:    ret void
 ;
   %dest = alloca [16 x i8]
@@ -223,7 +223,7 @@ define void @capture_before_call_argmemonly_nounwind_willreturn() {
   %dest.i8 = bitcast [16 x i8]* %dest to i8*
   %src.i8 = bitcast [16 x i8]* %src to i8*
   call void @accept_ptr(i8* %dest.i8) ; capture
-  call void @accept_ptr(i8* %src.i8) argmemonly nounwind willreturn
+  call void @accept_ptr(i8* nocapture %src.i8) argmemonly nounwind willreturn
   call void @llvm.memcpy.p0i8.p0i8.i64(i8* %dest.i8, i8* %src.i8, i64 16, i1 false)
   ret void
 }
index 4099e3c..84c1446 100644 (file)
@@ -28,15 +28,14 @@ define void @test() {
 }
 
 ; Same as previous test, but with a bitcasted argument.
-; TODO: Call slot optimization should not be applied here.
 define void @test_bitcast() {
 ; CHECK-LABEL: define {{[^@]+}}@test_bitcast() {
 ; CHECK-NEXT:    [[PTR1:%.*]] = alloca [2 x i8], align 1
 ; CHECK-NEXT:    [[PTR2:%.*]] = alloca [2 x i8], align 1
 ; CHECK-NEXT:    [[PTR1_CAST:%.*]] = bitcast [2 x i8]* [[PTR1]] to i8*
 ; CHECK-NEXT:    [[PTR2_CAST:%.*]] = bitcast [2 x i8]* [[PTR2]] to i8*
-; CHECK-NEXT:    [[PTR11:%.*]] = bitcast [2 x i8]* [[PTR1]] to i8*
-; CHECK-NEXT:    call void @foo(i8* [[PTR11]])
+; CHECK-NEXT:    call void @foo(i8* [[PTR2_CAST]])
+; CHECK-NEXT:    call void @llvm.memcpy.p0i8.p0i8.i32(i8* [[PTR1_CAST]], i8* [[PTR2_CAST]], i32 2, i1 false)
 ; CHECK-NEXT:    call void @foo(i8* [[PTR1_CAST]])
 ; CHECK-NEXT:    ret void
 ;