22; RUN: opt < %s -S -passes=memcpyopt -verify-memoryssa | FileCheck %s
33target datalayout = "e-i64:64-f80:128-n8:16:32:64-S128"
44
5- declare void @llvm.memcpy.p0.p0.i64 (ptr nocapture , ptr nocapture readonly , i64 , i1 ) unnamed_addr nounwind
5+ declare void @llvm.memcpy.p0.p0.i64 (ptr nocapture writeonly , ptr nocapture readonly , i64 , i1 ) unnamed_addr nounwind
66declare void @llvm.memset.p0.i64 (ptr nocapture , i8 , i64 , i1 ) nounwind
77
88; all bytes of %dst that are touch by the memset are dereferenceable
@@ -23,7 +23,6 @@ define void @must_remove_memcpy(ptr noalias nocapture writable dereferenceable(4
2323define void @must_not_remove_memcpy (ptr noalias nocapture writable dereferenceable (1024 ) %dst ) nofree nosync {
2424; CHECK-LABEL: @must_not_remove_memcpy(
2525; CHECK-NEXT: [[SRC:%.*]] = alloca [4096 x i8], align 1
26- ; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr [[SRC]], i8 0, i64 4096, i1 false)
2726; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr [[DST:%.*]], i8 0, i64 4096, i1 false)
2827; CHECK-NEXT: ret void
2928;
@@ -32,3 +31,71 @@ define void @must_not_remove_memcpy(ptr noalias nocapture writable dereferenceab
3231 call void @llvm.memcpy.p0.p0.i64 (ptr %dst , ptr %src , i64 4096 , i1 false ) #2
3332 ret void
3433}
34+
35+ ; enable copy elision when call has both willreturn and nounwind.
36+ define void @test_positive_willreturn_nounwind (ptr %val ) {
37+ ; CHECK-LABEL: @test_positive_willreturn_nounwind(
38+ ; CHECK-NEXT: entry:
39+ ; CHECK-NEXT: [[TMP:%.*]] = alloca [1024 x i32], align 4
40+ ; CHECK-NEXT: call void @bar(ptr sret([1024 x i32]) align 4 [[VAL:%.*]]) #[[ATTR3:[0-9]+]]
41+ ; CHECK-NEXT: ret void
42+ ;
43+ entry:
44+ %tmp = alloca [1024 x i32 ], align 4
45+ call void @bar (ptr sret ([1024 x i32 ]) align 4 %tmp ) willreturn nounwind memory(argmem: write)
46+ call void @llvm.memcpy.p0.p0.i64 (ptr align 4 %val , ptr align 4 %tmp , i64 4096 , i1 false )
47+ ret void
48+ }
49+
50+ ; missing willreturn attribute; optimization should not occur.
51+ define void @test_negative_missing_willreturn (ptr %val ) {
52+ ; CHECK-LABEL: @test_negative_missing_willreturn(
53+ ; CHECK-NEXT: entry:
54+ ; CHECK-NEXT: [[TMP:%.*]] = alloca [1024 x i32], align 4
55+ ; CHECK-NEXT: call void @bar(ptr sret([1024 x i32]) align 4 [[TMP]]) #[[ATTR4:[0-9]+]]
56+ ; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[VAL:%.*]], ptr align 4 [[TMP]], i64 4096, i1 false)
57+ ; CHECK-NEXT: ret void
58+ ;
59+ entry:
60+ %tmp = alloca [1024 x i32 ], align 4
61+ call void @bar (ptr sret ([1024 x i32 ]) align 4 %tmp ) nounwind memory(argmem: write)
62+ call void @llvm.memcpy.p0.p0.i64 (ptr align 4 %val , ptr align 4 %tmp , i64 4096 , i1 false )
63+ ret void
64+ }
65+
66+ ; missing nounwind attribute; optimization should not occur.
67+ define void @test_negative_missing_nounwind (ptr %val ) {
68+ ; CHECK-LABEL: @test_negative_missing_nounwind(
69+ ; CHECK-NEXT: entry:
70+ ; CHECK-NEXT: [[TMP:%.*]] = alloca [1024 x i32], align 4
71+ ; CHECK-NEXT: call void @bar(ptr sret([1024 x i32]) align 4 [[TMP]]) #[[ATTR5:[0-9]+]]
72+ ; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[VAL:%.*]], ptr align 4 [[TMP]], i64 4096, i1 false)
73+ ; CHECK-NEXT: ret void
74+ ;
75+ entry:
76+ %tmp = alloca [1024 x i32 ], align 4
77+ call void @bar (ptr sret ([1024 x i32 ]) align 4 %tmp ) willreturn memory(argmem: write)
78+ call void @llvm.memcpy.p0.p0.i64 (ptr align 4 %val , ptr align 4 %tmp , i64 4096 , i1 false )
79+ ret void
80+ }
81+
82+ ; willreturn nounwind attributes, but instruction between call and memcpy that may trap.
83+ define void @test_negative_may_trap_between (ptr %val , ptr %unknown ) {
84+ ; CHECK-LABEL: @test_negative_may_trap_between(
85+ ; CHECK-NEXT: entry:
86+ ; CHECK-NEXT: [[TMP:%.*]] = alloca [1024 x i32], align 4
87+ ; CHECK-NEXT: call void @bar(ptr sret([1024 x i32]) align 4 [[TMP]]) #[[ATTR3]]
88+ ; CHECK-NEXT: [[VAL_LOADED:%.*]] = load volatile i32, ptr [[UNKNOWN:%.*]], align 4
89+ ; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[VAL:%.*]], ptr align 4 [[TMP]], i64 4096, i1 false)
90+ ; CHECK-NEXT: ret void
91+ ;
92+ entry:
93+ %tmp = alloca [1024 x i32 ], align 4
94+ call void @bar (ptr sret ([1024 x i32 ]) align 4 %tmp ) willreturn nounwind memory(argmem: write)
95+ ; A volatile load can trap and has side effects.
96+ %val_loaded = load volatile i32 , ptr %unknown
97+ call void @llvm.memcpy.p0.p0.i64 (ptr align 4 %val , ptr align 4 %tmp , i64 4096 , i1 false )
98+ ret void
99+ }
100+
101+ declare void @bar (ptr nocapture sret ([1024 x i32 ]) align 4 )
0 commit comments