1 ; RUN: opt < %s -basicaa -memcpyopt -dse -S | FileCheck -enable-var-scope %s
3 target 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"
4 target triple = "i686-apple-darwin9"
6 %0 = type { x86_fp80, x86_fp80 }
9 define void @test1(%0* sret %agg.result, x86_fp80 %z.0, x86_fp80 %z.1) nounwind {
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* align 16 %tmp219, i8* align 16 %memtmp20, i32 32, i1 false)
18 %agg.result21 = bitcast %0* %agg.result to i8*
19 call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 16 %agg.result21, i8* align 16 %tmp219, i32 32, i1 false)
22 ; Check that one of the memcpy's are removed.
23 ;; FIXME: PR 8643 We should be able to eliminate the last memcpy here.
25 ; CHECK-LABEL: @test1(
26 ; CHECK: call void @ccoshl
27 ; CHECK: call void @llvm.memcpy
28 ; CHECK-NOT: llvm.memcpy
32 declare void @ccoshl(%0* nocapture sret, x86_fp80, x86_fp80) nounwind
35 ; The intermediate alloca and one of the memcpy's should be eliminated, the
36 ; other should be related with a memmove.
37 define 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* align 16 %R, i8* align 16 %P, i32 32, i1 false)
41 call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 16 %Q, i8* align 16 %R, i32 32, i1 false)
44 ; CHECK-LABEL: @test2(
45 ; CHECK-NEXT: call void @llvm.memmove{{.*}}(i8* align 16 %Q, i8* align 16 %P
46 ; CHECK-NEXT: ret void
49 ; The intermediate alloca and one of the memcpy's should be eliminated, the
50 ; other should be related with a memcpy.
51 define void @test2_memcpy(i8* noalias %P, i8* noalias %Q) nounwind {
52 %memtmp = alloca %0, align 16
53 %R = bitcast %0* %memtmp to i8*
54 call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 16 %R, i8* align 16 %P, i32 32, i1 false)
55 call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 16 %Q, i8* align 16 %R, i32 32, i1 false)
58 ; CHECK-LABEL: @test2_memcpy(
59 ; CHECK-NEXT: call void @llvm.memcpy{{.*}}(i8* align 16 %Q, i8* align 16 %P
60 ; CHECK-NEXT: ret void
66 @x = external global %0
68 define void @test3(%0* noalias sret %agg.result) nounwind {
70 %x.01 = bitcast %0* %x.0 to i8*
71 call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 16 %x.01, i8* align 16 bitcast (%0* @x to i8*), i32 32, i1 false)
72 %agg.result2 = bitcast %0* %agg.result to i8*
73 call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 16 %agg.result2, i8* align 16 %x.01, i32 32, i1 false)
75 ; CHECK-LABEL: @test3(
76 ; CHECK-NEXT: %agg.result1 = bitcast
77 ; CHECK-NEXT: call void @llvm.memcpy
78 ; CHECK-NEXT: ret void
83 define void @test4(i8 *%P) {
85 %a = bitcast %1* %A to i8*
86 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %a, i8* align 4 %P, i64 8, i1 false)
87 call void @test4a(i8* align 1 byval %a)
89 ; CHECK-LABEL: @test4(
90 ; CHECK-NEXT: call void @test4a(
93 ; Make sure we don't remove the memcpy if the source address space doesn't match the byval argument
94 define void @test4_addrspace(i8 addrspace(1)* %P) {
96 %a = bitcast %1* %A to i8*
97 call void @llvm.memcpy.p0i8.p1i8.i64(i8* align 4 %a, i8 addrspace(1)* align 4 %P, i64 8, i1 false)
98 call void @test4a(i8* align 1 byval %a)
100 ; CHECK-LABEL: @test4_addrspace(
101 ; CHECK: call void @llvm.memcpy.p0i8.p1i8.i64(
102 ; CHECK-NEXT: call void @test4a(
105 declare void @test4a(i8* align 1 byval)
106 declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i1) nounwind
107 declare void @llvm.memcpy.p0i8.p1i8.i64(i8* nocapture, i8 addrspace(1)* nocapture, i64, i1) nounwind
108 declare void @llvm.memcpy.p1i8.p1i8.i64(i8 addrspace(1)* nocapture, i8 addrspace(1)* nocapture, i64, i1) nounwind
110 %struct.S = type { i128, [4 x i8]}
112 @sS = external global %struct.S, align 16
114 declare void @test5a(%struct.S* align 16 byval) nounwind ssp
117 ; rdar://8713376 - This memcpy can't be eliminated.
118 define i32 @test5(i32 %x) nounwind ssp {
120 %y = alloca %struct.S, align 16
121 %tmp = bitcast %struct.S* %y to i8*
122 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 16 %tmp, i8* align 16 bitcast (%struct.S* @sS to i8*), i64 32, i1 false)
123 %a = getelementptr %struct.S, %struct.S* %y, i64 0, i32 1, i64 0
125 call void @test5a(%struct.S* align 16 byval %y)
127 ; CHECK-LABEL: @test5(
129 ; CHECK: call void @test5a(%struct.S* byval align 16 %y)
132 ;; Noop memcpy should be zapped.
133 define void @test6(i8 *%P) {
134 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %P, i8* align 4 %P, i64 8, i1 false)
136 ; CHECK-LABEL: @test6(
137 ; CHECK-NEXT: ret void
141 ; PR9794 - Should forward memcpy into byval argument even though the memcpy
142 ; isn't itself 8 byte aligned.
143 %struct.p = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }
145 define i32 @test7(%struct.p* nocapture align 8 byval %q) nounwind ssp {
147 %agg.tmp = alloca %struct.p, align 4
148 %tmp = bitcast %struct.p* %agg.tmp to i8*
149 %tmp1 = bitcast %struct.p* %q to i8*
150 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %tmp, i8* align 4 %tmp1, i64 48, i1 false)
151 %call = call i32 @g(%struct.p* align 8 byval %agg.tmp) nounwind
153 ; CHECK-LABEL: @test7(
154 ; CHECK: call i32 @g(%struct.p* byval align 8 %q) [[$NUW:#[0-9]+]]
157 declare i32 @g(%struct.p* align 8 byval)
159 declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i1) nounwind
161 ; PR11142 - When looking for a memcpy-memcpy dependency, don't get stuck on
162 ; instructions between the memcpy's that only affect the destination pointer.
163 @test8.str = internal constant [7 x i8] c"ABCDEF\00"
165 define void @test8() {
168 %A = tail call i8* @malloc(i32 10)
169 %B = getelementptr inbounds i8, i8* %A, i64 2
170 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, i1 false)
171 %C = tail call i8* @malloc(i32 10)
172 %D = getelementptr inbounds i8, i8* %C, i64 2
173 tail call void @llvm.memcpy.p0i8.p0i8.i32(i8* %D, i8* %B, i32 7, i1 false)
178 declare noalias i8* @malloc(i32)
181 %struct.big = type { [50 x i32] }
183 define void @test9_addrspacecast() nounwind ssp uwtable {
185 ; CHECK-LABEL: @test9_addrspacecast(
189 %b = alloca %struct.big, align 4
190 %tmp = alloca %struct.big, align 4
191 call void @f1(%struct.big* sret %tmp)
192 %0 = addrspacecast %struct.big* %b to i8 addrspace(1)*
193 %1 = addrspacecast %struct.big* %tmp to i8 addrspace(1)*
194 call void @llvm.memcpy.p1i8.p1i8.i64(i8 addrspace(1)* align 4 %0, i8 addrspace(1)* align 4 %1, i64 200, i1 false)
195 call void @f2(%struct.big* %b)
199 define void @test9() nounwind ssp uwtable {
205 %b = alloca %struct.big, align 4
206 %tmp = alloca %struct.big, align 4
207 call void @f1(%struct.big* sret %tmp)
208 %0 = bitcast %struct.big* %b to i8*
209 %1 = bitcast %struct.big* %tmp to i8*
210 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %0, i8* align 4 %1, i64 200, i1 false)
211 call void @f2(%struct.big* %b)
216 ; Test10 triggered assertion when the compiler try to get the size of the
217 ; opaque type of *x, where the x is the formal argument with attribute 'sret'.
219 %opaque = type opaque
220 declare void @foo(i32* noalias nocapture)
222 define void @test10(%opaque* noalias nocapture sret %x, i32 %y) {
223 %a = alloca i32, align 4
224 store i32 %y, i32* %a
225 call void @foo(i32* noalias nocapture %a)
226 %c = load i32, i32* %a
227 %d = bitcast %opaque* %x to i32*
228 store i32 %c, i32* %d
232 ; don't create new addressspacecasts when we don't know they're safe for the target
233 define void @test11([20 x i32] addrspace(1)* nocapture dereferenceable(80) %P) {
234 %A = alloca [20 x i32], align 4
235 %a = bitcast [20 x i32]* %A to i8*
236 %b = bitcast [20 x i32] addrspace(1)* %P to i8 addrspace(1)*
237 call void @llvm.memset.p0i8.i64(i8* align 4 %a, i8 0, i64 80, i1 false)
238 call void @llvm.memcpy.p1i8.p0i8.i64(i8 addrspace(1)* align 4 %b, i8* align 4 %a, i64 80, i1 false)
240 ; CHECK-LABEL: @test11(
241 ; CHECK-NOT: addrspacecast
244 declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i1) nounwind
245 declare void @llvm.memcpy.p1i8.p0i8.i64(i8 addrspace(1)* nocapture, i8* nocapture, i64, i1) nounwind
247 declare void @f1(%struct.big* nocapture sret)
248 declare void @f2(%struct.big*)
250 ; CHECK: attributes [[$NUW]] = { nounwind }
251 ; CHECK: attributes #1 = { argmemonly nounwind willreturn }
252 ; CHECK: attributes #2 = { nounwind ssp }
253 ; CHECK: attributes #3 = { nounwind ssp uwtable }