Reapply [MemCpyOpt] Look through pointer casts when checking capture

This is a recommit of the patch without changes. The reason for
the revert has been addressed in D117679.

-----

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.
This commit is contained in:
Nikita Popov 2022-01-05 09:48:17 +01:00
parent 655a7024db
commit 0d20407d1a
3 changed files with 10 additions and 10 deletions

View File

@ -953,7 +953,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

View 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
}

View 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
;