1; RUN: opt < %s -basicaa -memcpyopt -dse -S | FileCheck %s
2
3target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
4target triple = "i686-apple-darwin9"
5
6%0 = type { x86_fp80, x86_fp80 }
7%1 = type { i32, i32 }
8
9define void @test1(%0* sret  %agg.result, x86_fp80 %z.0, x86_fp80 %z.1) nounwind  {
10entry:
11  %tmp2 = alloca %0
12  %memtmp = alloca %0, align 16
13  %tmp5 = fsub x86_fp80 0xK80000000000000000000, %z.1
14  call void @ccoshl(%0* sret %memtmp, x86_fp80 %tmp5, x86_fp80 %z.0) nounwind
15  %tmp219 = bitcast %0* %tmp2 to i8*
16  %memtmp20 = bitcast %0* %memtmp to i8*
17  call void @llvm.memcpy.p0i8.p0i8.i32(i8* %tmp219, i8* %memtmp20, i32 32, i32 16, i1 false)
18  %agg.result21 = bitcast %0* %agg.result to i8*
19  call void @llvm.memcpy.p0i8.p0i8.i32(i8* %agg.result21, i8* %tmp219, i32 32, i32 16, i1 false)
20  ret void
21
22; Check that one of the memcpy's are removed.
23;; FIXME: PR 8643 We should be able to eliminate the last memcpy here.
24
25; CHECK-LABEL: @test1(
26; CHECK: call void @ccoshl
27; CHECK: call void @llvm.memcpy
28; CHECK-NOT: llvm.memcpy
29; CHECK: ret void
30}
31
32declare void @ccoshl(%0* nocapture sret, x86_fp80, x86_fp80) nounwind
33
34
35; The intermediate alloca and one of the memcpy's should be eliminated, the
36; other should be related with a memmove.
37define void @test2(i8* %P, i8* %Q) nounwind  {
38  %memtmp = alloca %0, align 16
39  %R = bitcast %0* %memtmp to i8*
40  call void @llvm.memcpy.p0i8.p0i8.i32(i8* %R, i8* %P, i32 32, i32 16, i1 false)
41  call void @llvm.memcpy.p0i8.p0i8.i32(i8* %Q, i8* %R, i32 32, i32 16, i1 false)
42  ret void
43
44; CHECK-LABEL: @test2(
45; CHECK-NEXT: call void @llvm.memmove{{.*}}(i8* %Q, i8* %P
46; CHECK-NEXT: ret void
47}
48
49
50
51
52@x = external global %0
53
54define void @test3(%0* noalias sret %agg.result) nounwind  {
55  %x.0 = alloca %0
56  %x.01 = bitcast %0* %x.0 to i8*
57  call void @llvm.memcpy.p0i8.p0i8.i32(i8* %x.01, i8* bitcast (%0* @x to i8*), i32 32, i32 16, i1 false)
58  %agg.result2 = bitcast %0* %agg.result to i8*
59  call void @llvm.memcpy.p0i8.p0i8.i32(i8* %agg.result2, i8* %x.01, i32 32, i32 16, i1 false)
60  ret void
61; CHECK-LABEL: @test3(
62; CHECK-NEXT: %agg.result1 = bitcast
63; CHECK-NEXT: call void @llvm.memcpy
64; CHECK-NEXT: ret void
65}
66
67
68; PR8644
69define void @test4(i8 *%P) {
70  %A = alloca %1
71  %a = bitcast %1* %A to i8*
72  call void @llvm.memcpy.p0i8.p0i8.i64(i8* %a, i8* %P, i64 8, i32 4, i1 false)
73  call void @test4a(i8* align 1 byval %a)
74  ret void
75; CHECK-LABEL: @test4(
76; CHECK-NEXT: call void @test4a(
77}
78
79declare void @test4a(i8* align 1 byval)
80declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind
81declare void @llvm.memcpy.p1i8.p1i8.i64(i8 addrspace(1)* nocapture, i8 addrspace(1)* nocapture, i64, i32, i1) nounwind
82
83%struct.S = type { i128, [4 x i8]}
84
85@sS = external global %struct.S, align 16
86
87declare void @test5a(%struct.S* align 16 byval) nounwind ssp
88
89
90; rdar://8713376 - This memcpy can't be eliminated.
91define i32 @test5(i32 %x) nounwind ssp {
92entry:
93  %y = alloca %struct.S, align 16
94  %tmp = bitcast %struct.S* %y to i8*
95  call void @llvm.memcpy.p0i8.p0i8.i64(i8* %tmp, i8* bitcast (%struct.S* @sS to i8*), i64 32, i32 16, i1 false)
96  %a = getelementptr %struct.S, %struct.S* %y, i64 0, i32 1, i64 0
97  store i8 4, i8* %a
98  call void @test5a(%struct.S* align 16 byval %y)
99  ret i32 0
100  ; CHECK-LABEL: @test5(
101  ; CHECK: store i8 4
102  ; CHECK: call void @test5a(%struct.S* byval align 16 %y)
103}
104
105;; Noop memcpy should be zapped.
106define void @test6(i8 *%P) {
107  call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %P, i64 8, i32 4, i1 false)
108  ret void
109; CHECK-LABEL: @test6(
110; CHECK-NEXT: ret void
111}
112
113
114; PR9794 - Should forward memcpy into byval argument even though the memcpy
115; isn't itself 8 byte aligned.
116%struct.p = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }
117
118define i32 @test7(%struct.p* nocapture align 8 byval %q) nounwind ssp {
119entry:
120  %agg.tmp = alloca %struct.p, align 4
121  %tmp = bitcast %struct.p* %agg.tmp to i8*
122  %tmp1 = bitcast %struct.p* %q to i8*
123  call void @llvm.memcpy.p0i8.p0i8.i64(i8* %tmp, i8* %tmp1, i64 48, i32 4, i1 false)
124  %call = call i32 @g(%struct.p* align 8 byval %agg.tmp) nounwind
125  ret i32 %call
126; CHECK-LABEL: @test7(
127; CHECK: call i32 @g(%struct.p* byval align 8 %q) [[NUW:#[0-9]+]]
128}
129
130declare i32 @g(%struct.p* align 8 byval)
131
132declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i32, i1) nounwind
133
134; PR11142 - When looking for a memcpy-memcpy dependency, don't get stuck on
135; instructions between the memcpy's that only affect the destination pointer.
136@test8.str = internal constant [7 x i8] c"ABCDEF\00"
137
138define void @test8() {
139; CHECK: test8
140; CHECK-NOT: memcpy
141  %A = tail call i8* @malloc(i32 10)
142  %B = getelementptr inbounds i8, i8* %A, i64 2
143  tail call void @llvm.memcpy.p0i8.p0i8.i32(i8* %B, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @test8.str, i64 0, i64 0), i32 7, i32 1, i1 false)
144  %C = tail call i8* @malloc(i32 10)
145  %D = getelementptr inbounds i8, i8* %C, i64 2
146  tail call void @llvm.memcpy.p0i8.p0i8.i32(i8* %D, i8* %B, i32 7, i32 1, i1 false)
147  ret void
148; CHECK: ret void
149}
150
151declare noalias i8* @malloc(i32)
152
153; rdar://11341081
154%struct.big = type { [50 x i32] }
155
156define void @test9_addrspacecast() nounwind ssp uwtable {
157entry:
158; CHECK-LABEL: @test9_addrspacecast(
159; CHECK: f1
160; CHECK-NOT: memcpy
161; CHECK: f2
162  %b = alloca %struct.big, align 4
163  %tmp = alloca %struct.big, align 4
164  call void @f1(%struct.big* sret %tmp)
165  %0 = addrspacecast %struct.big* %b to i8 addrspace(1)*
166  %1 = addrspacecast %struct.big* %tmp to i8 addrspace(1)*
167  call void @llvm.memcpy.p1i8.p1i8.i64(i8 addrspace(1)* %0, i8 addrspace(1)* %1, i64 200, i32 4, i1 false)
168  call void @f2(%struct.big* %b)
169  ret void
170}
171
172define void @test9() nounwind ssp uwtable {
173entry:
174; CHECK: test9
175; CHECK: f1
176; CHECK-NOT: memcpy
177; CHECK: f2
178  %b = alloca %struct.big, align 4
179  %tmp = alloca %struct.big, align 4
180  call void @f1(%struct.big* sret %tmp)
181  %0 = bitcast %struct.big* %b to i8*
182  %1 = bitcast %struct.big* %tmp to i8*
183  call void @llvm.memcpy.p0i8.p0i8.i64(i8* %0, i8* %1, i64 200, i32 4, i1 false)
184  call void @f2(%struct.big* %b)
185  ret void
186}
187
188; rdar://14073661.
189; Test10 triggered assertion when the compiler try to get the size of the
190; opaque type of *x, where the x is the formal argument with attribute 'sret'.
191
192%opaque = type opaque
193declare void @foo(i32* noalias nocapture)
194
195define void @test10(%opaque* noalias nocapture sret %x, i32 %y) {
196  %a = alloca i32, align 4
197  store i32 %y, i32* %a
198  call void @foo(i32* noalias nocapture %a)
199  %c = load i32, i32* %a
200  %d = bitcast %opaque* %x to i32*
201  store i32 %c, i32* %d
202  ret void
203}
204
205declare void @f1(%struct.big* nocapture sret)
206declare void @f2(%struct.big*)
207
208; CHECK: attributes [[NUW]] = { nounwind }
209; CHECK: attributes #1 = { argmemonly nounwind }
210; CHECK: attributes #2 = { nounwind ssp }
211; CHECK: attributes #3 = { nounwind ssp uwtable }
212