Skip to content

Commit

Permalink
[test][InstCombine] Add tests for removing memcpy to an alloca that i…
Browse files Browse the repository at this point in the history
…s passed to a readonly nocapture function parameter, in preparation for D136822.

This commit adds tests to Transforms/InstCombine/memcpy-from-global.ll that
test various situations involving memcpy from a constant to an alloca that is
then passed to function parameters with various attributes. The forthcoming
D136822 allows InstCombine to remove these memcpys if they're passed to a
single readonly nocapture parameter.

Differential Revision: https://reviews.llvm.org/D137033
  • Loading branch information
pcwalton committed Oct 30, 2022
1 parent cb2cb2d commit 86c95dc
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions llvm/test/Transforms/InstCombine/memcpy-from-global.ll
Expand Up @@ -380,4 +380,52 @@ define void @volatile_memcpy() {
ret void
}

; Test that we don't yet elide a memcpy when copying a constant value onto the
; stack and then forwarding it by readonly nocapture reference.
define void @memcpy_to_nocapture_readonly() {
; CHECK-LABEL: @memcpy_to_nocapture_readonly(
; CHECK-NEXT: [[A:%.*]] = alloca [[U:%.*]], align 16
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(20) [[A]], ptr noundef nonnull align 16 dereferenceable(20) @H, i64 20, i1 false)
; CHECK-NEXT: call void @bar(ptr nocapture nonnull readonly [[A]])
; CHECK-NEXT: ret void
;
%A = alloca %U, align 16
call void @llvm.memcpy.p0.p0.i64(ptr align 4 %A, ptr align 4 @H, i64 20, i1 false)
call void @bar(ptr nocapture readonly %A)
ret void
}

; Test that we don't elide the memcpy when copying a constant value onto the
; stack and then forwarding it by readonly, but capturing, reference.
define void @memcpy_to_capturing_readonly() {
; CHECK-LABEL: @memcpy_to_capturing_readonly(
; CHECK-NEXT: [[A:%.*]] = alloca [[U:%.*]], align 16
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(20) [[A]], ptr noundef nonnull align 16 dereferenceable(20) @H, i64 20, i1 false)
; CHECK-NEXT: call void @bar(ptr nonnull readonly [[A]])
; CHECK-NEXT: ret void
;
%A = alloca %U, align 16
call void @llvm.memcpy.p0.p0.i64(ptr align 4 %A, ptr align 4 @H, i64 20, i1 false)
call void @bar(ptr readonly %A)
ret void
}

; Test that we don't elide the memcpy when copying a constant value onto the
; stack and then forwarding it by read-write, nocapture reference, even if it's
; also forwarded by readonly nocapture reference to the same function.
define void @memcpy_to_aliased_nocapture_readonly() {
; CHECK-LABEL: @memcpy_to_aliased_nocapture_readonly(
; CHECK-NEXT: [[A:%.*]] = alloca [[U:%.*]], align 16
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(20) [[A]], ptr noundef nonnull align 16 dereferenceable(20) @H, i64 20, i1 false)
; CHECK-NEXT: call void @two_params(ptr nocapture nonnull readonly [[A]], ptr nocapture nonnull [[A]])
; CHECK-NEXT: ret void
;
%A = alloca %U, align 16
call void @llvm.memcpy.p0.p0.i64(ptr align 4 %A, ptr align 4 @H, i64 20, i1 false)
call void @two_params(ptr nocapture readonly %A, ptr nocapture %A)
ret void
}

declare void @two_params(ptr nocapture readonly, ptr nocapture)

attributes #0 = { null_pointer_is_valid }

0 comments on commit 86c95dc

Please sign in to comment.