1 ; RUN: opt < %s -basicaa -dse -S | FileCheck %s
2 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
4 declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1) nounwind
5 declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind
6 declare i8* @llvm.init.trampoline(i8*, i8*, i8*)
8 define void @test1(i32* %Q, i32* %P) {
10 store i32 %DEAD, i32* %P
14 ; CHECK-NEXT: store i32 0, i32* %P
15 ; CHECK-NEXT: ret void
18 ; PR8576 - Should delete store of 10 even though p/q are may aliases.
19 define void @test2(i32 *%p, i32 *%q) {
20 store i32 10, i32* %p, align 4
21 store i32 20, i32* %q, align 4
22 store i32 30, i32* %p, align 4
25 ; CHECK-NEXT: store i32 20
32 define i32 @test3(i32* %g_addr) nounwind {
34 ; CHECK: load i32* %g_addr
35 %g_value = load i32* %g_addr, align 4
36 store i32 -1, i32* @g, align 4
37 store i32 %g_value, i32* %g_addr, align 4
38 %tmp3 = load i32* @g, align 4
43 define void @test4(i32* %Q) {
45 volatile store i32 %a, i32* %Q
48 ; CHECK-NEXT: load i32
49 ; CHECK-NEXT: volatile store
50 ; CHECK-NEXT: ret void
53 define void @test5(i32* %Q) {
54 %a = volatile load i32* %Q
58 ; CHECK-NEXT: volatile load
59 ; CHECK-NEXT: ret void
62 ; Should delete store of 10 even though memset is a may-store to P (P and Q may
64 define void @test6(i32 *%p, i8 *%q) {
65 store i32 10, i32* %p, align 4 ;; dead.
66 call void @llvm.memset.p0i8.i64(i8* %q, i8 42, i64 900, i32 1, i1 false)
67 store i32 30, i32* %p, align 4
70 ; CHECK-NEXT: call void @llvm.memset
73 ; Should delete store of 10 even though memcpy is a may-store to P (P and Q may
75 define void @test7(i32 *%p, i8 *%q, i8* noalias %r) {
76 store i32 10, i32* %p, align 4 ;; dead.
77 call void @llvm.memcpy.p0i8.p0i8.i64(i8* %q, i8* %r, i64 900, i32 1, i1 false)
78 store i32 30, i32* %p, align 4
81 ; CHECK-NEXT: call void @llvm.memcpy
84 ; Do not delete stores that are only partially killed.
87 store i32 1234567, i32* %V
88 %V2 = bitcast i32* %V to i8*
94 ; CHECK: store i32 1234567
98 ; Test for byval handling.
99 %struct.x = type { i32, i32, i32, i32 }
100 define void @test9(%struct.x* byval %a) nounwind {
101 %tmp2 = getelementptr %struct.x* %a, i32 0, i32 0
102 store i32 1, i32* %tmp2, align 4
105 ; CHECK-NEXT: ret void
108 ; va_arg has fuzzy dependence, the store shouldn't be zapped.
109 define double @test10(i8* %X) {
111 store i8* %X, i8** %X_addr
112 %tmp.0 = va_arg i8** %X_addr, double
119 ; DSE should delete the dead trampoline.
120 declare void @test11f()
121 define void @test11() {
123 %storage = alloca [10 x i8], align 16 ; <[10 x i8]*> [#uses=1]
125 %cast = getelementptr [10 x i8]* %storage, i32 0, i32 0 ; <i8*> [#uses=1]
126 %tramp = call i8* @llvm.init.trampoline( i8* %cast, i8* bitcast (void ()* @test11f to i8*), i8* null ) ; <i8*> [#uses=1]
127 ; CHECK-NOT: trampoline
133 ; PR2599 - load -> store to same address.
134 define void @test12({ i32, i32 }* %x) nounwind {
135 %tmp4 = getelementptr { i32, i32 }* %x, i32 0, i32 0
136 %tmp5 = load i32* %tmp4, align 4
137 %tmp7 = getelementptr { i32, i32 }* %x, i32 0, i32 1
138 %tmp8 = load i32* %tmp7, align 4
139 %tmp17 = sub i32 0, %tmp8
140 store i32 %tmp5, i32* %tmp4, align 4
141 store i32 %tmp17, i32* %tmp7, align 4
149 ; %P doesn't escape, the DEAD instructions should be removed.
150 declare void @test13f()
151 define i32* @test13() {
152 %p = tail call i8* @malloc(i32 4)
153 %P = bitcast i8* %p to i32*
155 %DEAD2 = add i32 %DEAD, 1
156 store i32 %DEAD2, i32* %P
157 call void @test13f( )
162 ; CHECK-NEXT: bitcast
163 ; CHECK-NEXT: call void
166 declare noalias i8* @malloc(i32)
170 define void @test14(i32* %Q) {
173 store i32 %DEAD, i32* %P
177 ; CHECK-NEXT: ret void
183 ;; Fully dead overwrite of memcpy.
184 define void @test15(i8* %P, i8* %Q) nounwind ssp {
185 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
186 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
189 ; CHECK-NEXT: call void @llvm.memcpy
193 ;; Full overwrite of smaller memcpy.
194 define void @test16(i8* %P, i8* %Q) nounwind ssp {
195 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 8, i32 1, i1 false)
196 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
199 ; CHECK-NEXT: call void @llvm.memcpy
203 ;; Overwrite of memset by memcpy.
204 define void @test17(i8* %P, i8* noalias %Q) nounwind ssp {
205 tail call void @llvm.memset.p0i8.i64(i8* %P, i8 42, i64 8, i32 1, i1 false)
206 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
209 ; CHECK-NEXT: call void @llvm.memcpy
213 ; Should not delete the volatile memset.
214 define void @test17v(i8* %P, i8* %Q) nounwind ssp {
215 tail call void @llvm.memset.p0i8.i64(i8* %P, i8 42, i64 8, i32 1, i1 true)
216 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
219 ; CHECK-NEXT: call void @llvm.memset
220 ; CHECK-NEXT: call void @llvm.memcpy
225 ; Do not delete instruction where possible situation is:
228 define void @test18(i8* %P, i8* %Q, i8* %R) nounwind ssp {
229 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
230 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %R, i64 12, i32 1, i1 false)
233 ; CHECK-NEXT: call void @llvm.memcpy
234 ; CHECK-NEXT: call void @llvm.memcpy
239 ; The store here is not dead because the byval call reads it.
240 declare void @test19f({i32}* byval align 4 %P)
242 define void @test19({i32} * nocapture byval align 4 %arg5) nounwind ssp {
244 %tmp7 = getelementptr inbounds {i32}* %arg5, i32 0, i32 0
245 store i32 912, i32* %tmp7
246 call void @test19f({i32}* byval align 4 %arg5)
250 ; CHECK: store i32 912
251 ; CHECK: call void @test19f