blob: 1e813852b04c276ad4fade2e92b18684d64a4f68 [file] [log] [blame]
Chris Lattner6e222212010-11-21 07:32:40 +00001; RUN: opt < %s -basicaa -dse -S | FileCheck %s
Kenneth Uildriks90fedc62009-11-03 15:29:06 +00002target 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"
Chris Lattnere82780c22004-07-22 07:59:20 +00003
Chris Lattnerb63ba732010-11-30 19:12:10 +00004declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1) nounwind
5declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind
Chris Lattnerb63ba732010-11-30 19:12:10 +00006declare i8* @llvm.init.trampoline(i8*, i8*, i8*)
7
Chris Lattner6e222212010-11-21 07:32:40 +00008define void @test1(i32* %Q, i32* %P) {
9 %DEAD = load i32* %Q
Tanya Lattner5640bd12008-03-01 09:15:35 +000010 store i32 %DEAD, i32* %P
11 store i32 0, i32* %P
12 ret void
Stephen Linc1c7a132013-07-14 01:42:54 +000013; CHECK-LABEL: @test1(
Chris Lattner6e222212010-11-21 07:32:40 +000014; CHECK-NEXT: store i32 0, i32* %P
15; CHECK-NEXT: ret void
Chris Lattnere82780c22004-07-22 07:59:20 +000016}
Tanya Lattner5640bd12008-03-01 09:15:35 +000017
Chris Lattnere48c31c2010-11-21 07:34:32 +000018; PR8576 - Should delete store of 10 even though p/q are may aliases.
19define 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
23 ret void
Stephen Linc1c7a132013-07-14 01:42:54 +000024; CHECK-LABEL: @test2(
Chris Lattnere48c31c2010-11-21 07:34:32 +000025; CHECK-NEXT: store i32 20
26}
Chris Lattner2e8793482010-11-29 21:59:31 +000027
28
29; PR8677
30@g = global i32 1
31
32define i32 @test3(i32* %g_addr) nounwind {
Stephen Linc1c7a132013-07-14 01:42:54 +000033; CHECK-LABEL: @test3(
Benjamin Kramere6840ef2010-11-29 22:34:55 +000034; CHECK: load i32* %g_addr
Chris Lattner2e8793482010-11-29 21:59:31 +000035 %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
39 ret i32 %tmp3
40}
Chris Lattnerc3c754f2010-11-30 00:12:39 +000041
42
43define void @test4(i32* %Q) {
44 %a = load i32* %Q
Eli Friedman02e737b2011-08-12 22:50:01 +000045 store volatile i32 %a, i32* %Q
Chris Lattnerc3c754f2010-11-30 00:12:39 +000046 ret void
Stephen Linc1c7a132013-07-14 01:42:54 +000047; CHECK-LABEL: @test4(
Chris Lattnerc3c754f2010-11-30 00:12:39 +000048; CHECK-NEXT: load i32
Eli Friedman02e737b2011-08-12 22:50:01 +000049; CHECK-NEXT: store volatile
Chris Lattnerc3c754f2010-11-30 00:12:39 +000050; CHECK-NEXT: ret void
51}
52
53define void @test5(i32* %Q) {
Eli Friedman02e737b2011-08-12 22:50:01 +000054 %a = load volatile i32* %Q
Chris Lattnerc3c754f2010-11-30 00:12:39 +000055 store i32 %a, i32* %Q
56 ret void
Stephen Linc1c7a132013-07-14 01:42:54 +000057; CHECK-LABEL: @test5(
Eli Friedman02e737b2011-08-12 22:50:01 +000058; CHECK-NEXT: load volatile
Chris Lattnerc3c754f2010-11-30 00:12:39 +000059; CHECK-NEXT: ret void
60}
Chris Lattner9a146372010-11-30 00:28:45 +000061
Chris Lattner9a146372010-11-30 00:28:45 +000062; Should delete store of 10 even though memset is a may-store to P (P and Q may
63; alias).
64define void @test6(i32 *%p, i8 *%q) {
65 store i32 10, i32* %p, align 4 ;; dead.
Chris Lattner80ed9dc2011-06-18 06:05:24 +000066 call void @llvm.memset.p0i8.i64(i8* %q, i8 42, i64 900, i32 1, i1 false)
Chris Lattner9a146372010-11-30 00:28:45 +000067 store i32 30, i32* %p, align 4
68 ret void
Stephen Linc1c7a132013-07-14 01:42:54 +000069; CHECK-LABEL: @test6(
Chris Lattner9a146372010-11-30 00:28:45 +000070; CHECK-NEXT: call void @llvm.memset
71}
72
Chris Lattner90c49472010-11-30 00:43:16 +000073; Should delete store of 10 even though memcpy is a may-store to P (P and Q may
74; alias).
75define void @test7(i32 *%p, i8 *%q, i8* noalias %r) {
76 store i32 10, i32* %p, align 4 ;; dead.
Chris Lattner80ed9dc2011-06-18 06:05:24 +000077 call void @llvm.memcpy.p0i8.p0i8.i64(i8* %q, i8* %r, i64 900, i32 1, i1 false)
Chris Lattner90c49472010-11-30 00:43:16 +000078 store i32 30, i32* %p, align 4
79 ret void
Stephen Linc1c7a132013-07-14 01:42:54 +000080; CHECK-LABEL: @test7(
Chris Lattner90c49472010-11-30 00:43:16 +000081; CHECK-NEXT: call void @llvm.memcpy
82}
Chris Lattner43e3a982010-11-30 01:00:34 +000083
84; Do not delete stores that are only partially killed.
85define i32 @test8() {
86 %V = alloca i32
87 store i32 1234567, i32* %V
88 %V2 = bitcast i32* %V to i8*
89 store i8 0, i8* %V2
90 %X = load i32* %V
91 ret i32 %X
92
Stephen Linc1c7a132013-07-14 01:42:54 +000093; CHECK-LABEL: @test8(
Chris Lattner43e3a982010-11-30 01:00:34 +000094; CHECK: store i32 1234567
95}
96
97
98; Test for byval handling.
99%struct.x = type { i32, i32, i32, i32 }
100define 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
103 ret void
Stephen Linc1c7a132013-07-14 01:42:54 +0000104; CHECK-LABEL: @test9(
Chris Lattner43e3a982010-11-30 01:00:34 +0000105; CHECK-NEXT: ret void
106}
107
Reid Kleckner26af2ca2014-01-28 02:38:36 +0000108; Test for inalloca handling.
109define void @test9_2(%struct.x* inalloca %a) nounwind {
110 %tmp2 = getelementptr %struct.x* %a, i32 0, i32 0
111 store i32 1, i32* %tmp2, align 4
112 ret void
113; CHECK-LABEL: @test9_2(
114; CHECK-NEXT: ret void
115}
116
Chris Lattner43e3a982010-11-30 01:00:34 +0000117; va_arg has fuzzy dependence, the store shouldn't be zapped.
118define double @test10(i8* %X) {
119 %X_addr = alloca i8*
120 store i8* %X, i8** %X_addr
121 %tmp.0 = va_arg i8** %X_addr, double
122 ret double %tmp.0
Stephen Linc1c7a132013-07-14 01:42:54 +0000123; CHECK-LABEL: @test10(
Chris Lattner43e3a982010-11-30 01:00:34 +0000124; CHECK: store
125}
126
127
128; DSE should delete the dead trampoline.
Chris Lattner43e3a982010-11-30 01:00:34 +0000129declare void @test11f()
130define void @test11() {
Stephen Linc1c7a132013-07-14 01:42:54 +0000131; CHECK-LABEL: @test11(
Chris Lattner43e3a982010-11-30 01:00:34 +0000132 %storage = alloca [10 x i8], align 16 ; <[10 x i8]*> [#uses=1]
133; CHECK-NOT: alloca
134 %cast = getelementptr [10 x i8]* %storage, i32 0, i32 0 ; <i8*> [#uses=1]
135 %tramp = call i8* @llvm.init.trampoline( i8* %cast, i8* bitcast (void ()* @test11f to i8*), i8* null ) ; <i8*> [#uses=1]
136; CHECK-NOT: trampoline
137 ret void
138; CHECK: ret void
139}
140
141
142; PR2599 - load -> store to same address.
143define void @test12({ i32, i32 }* %x) nounwind {
144 %tmp4 = getelementptr { i32, i32 }* %x, i32 0, i32 0
145 %tmp5 = load i32* %tmp4, align 4
146 %tmp7 = getelementptr { i32, i32 }* %x, i32 0, i32 1
147 %tmp8 = load i32* %tmp7, align 4
148 %tmp17 = sub i32 0, %tmp8
149 store i32 %tmp5, i32* %tmp4, align 4
150 store i32 %tmp17, i32* %tmp7, align 4
151 ret void
Stephen Linc1c7a132013-07-14 01:42:54 +0000152; CHECK-LABEL: @test12(
Chris Lattner43e3a982010-11-30 01:00:34 +0000153; CHECK-NOT: tmp5
154; CHECK: ret void
155}
156
Chris Lattner7578d0d2010-11-30 01:05:07 +0000157
158; %P doesn't escape, the DEAD instructions should be removed.
159declare void @test13f()
160define i32* @test13() {
161 %p = tail call i8* @malloc(i32 4)
162 %P = bitcast i8* %p to i32*
163 %DEAD = load i32* %P
164 %DEAD2 = add i32 %DEAD, 1
165 store i32 %DEAD2, i32* %P
166 call void @test13f( )
167 store i32 0, i32* %P
168 ret i32* %P
169; CHECK: @test13()
170; CHECK-NEXT: malloc
171; CHECK-NEXT: bitcast
172; CHECK-NEXT: call void
173}
174
Matt Arsenault70f4db882014-07-15 00:56:40 +0000175define i32 addrspace(1)* @test13_addrspacecast() {
176 %p = tail call i8* @malloc(i32 4)
177 %p.bc = bitcast i8* %p to i32*
178 %P = addrspacecast i32* %p.bc to i32 addrspace(1)*
179 %DEAD = load i32 addrspace(1)* %P
180 %DEAD2 = add i32 %DEAD, 1
181 store i32 %DEAD2, i32 addrspace(1)* %P
182 call void @test13f( )
183 store i32 0, i32 addrspace(1)* %P
184 ret i32 addrspace(1)* %P
185; CHECK: @test13_addrspacecast()
186; CHECK-NEXT: malloc
187; CHECK-NEXT: bitcast
188; CHECK-NEXT: addrspacecast
189; CHECK-NEXT: call void
190}
191
Chris Lattner7578d0d2010-11-30 01:05:07 +0000192declare noalias i8* @malloc(i32)
Nuno Lopes300d6292012-05-10 17:14:00 +0000193declare noalias i8* @calloc(i32, i32)
Chris Lattnerc2e34452010-11-30 01:06:43 +0000194
195
196define void @test14(i32* %Q) {
197 %P = alloca i32
198 %DEAD = load i32* %Q
199 store i32 %DEAD, i32* %P
200 ret void
201
Stephen Linc1c7a132013-07-14 01:42:54 +0000202; CHECK-LABEL: @test14(
Chris Lattnerc2e34452010-11-30 01:06:43 +0000203; CHECK-NEXT: ret void
204}
205
Chris Lattner58b779e2010-11-30 07:23:21 +0000206
207; PR8701
208
209;; Fully dead overwrite of memcpy.
210define void @test15(i8* %P, i8* %Q) nounwind ssp {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000211 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
212 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
Chris Lattner58b779e2010-11-30 07:23:21 +0000213 ret void
Stephen Linc1c7a132013-07-14 01:42:54 +0000214; CHECK-LABEL: @test15(
Chris Lattner58b779e2010-11-30 07:23:21 +0000215; CHECK-NEXT: call void @llvm.memcpy
216; CHECK-NEXT: ret
217}
218
219;; Full overwrite of smaller memcpy.
220define void @test16(i8* %P, i8* %Q) nounwind ssp {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000221 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 8, i32 1, i1 false)
222 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
Chris Lattner58b779e2010-11-30 07:23:21 +0000223 ret void
Stephen Linc1c7a132013-07-14 01:42:54 +0000224; CHECK-LABEL: @test16(
Chris Lattner58b779e2010-11-30 07:23:21 +0000225; CHECK-NEXT: call void @llvm.memcpy
226; CHECK-NEXT: ret
227}
228
229;; Overwrite of memset by memcpy.
Chris Lattner94fbdf32010-12-06 01:48:06 +0000230define void @test17(i8* %P, i8* noalias %Q) nounwind ssp {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000231 tail call void @llvm.memset.p0i8.i64(i8* %P, i8 42, i64 8, i32 1, i1 false)
232 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
Chris Lattner58b779e2010-11-30 07:23:21 +0000233 ret void
Stephen Linc1c7a132013-07-14 01:42:54 +0000234; CHECK-LABEL: @test17(
Chris Lattner58b779e2010-11-30 07:23:21 +0000235; CHECK-NEXT: call void @llvm.memcpy
236; CHECK-NEXT: ret
237}
238
Chris Lattnerb63ba732010-11-30 19:12:10 +0000239; Should not delete the volatile memset.
240define void @test17v(i8* %P, i8* %Q) nounwind ssp {
241 tail call void @llvm.memset.p0i8.i64(i8* %P, i8 42, i64 8, i32 1, i1 true)
242 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
243 ret void
Stephen Linc1c7a132013-07-14 01:42:54 +0000244; CHECK-LABEL: @test17v(
Chris Lattnerb63ba732010-11-30 19:12:10 +0000245; CHECK-NEXT: call void @llvm.memset
246; CHECK-NEXT: call void @llvm.memcpy
247; CHECK-NEXT: ret
248}
Chris Lattner94fbdf32010-12-06 01:48:06 +0000249
250; PR8728
251; Do not delete instruction where possible situation is:
252; A = B
253; A = A
254define void @test18(i8* %P, i8* %Q, i8* %R) nounwind ssp {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000255 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
256 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %R, i64 12, i32 1, i1 false)
Chris Lattner94fbdf32010-12-06 01:48:06 +0000257 ret void
Stephen Linc1c7a132013-07-14 01:42:54 +0000258; CHECK-LABEL: @test18(
Chris Lattner94fbdf32010-12-06 01:48:06 +0000259; CHECK-NEXT: call void @llvm.memcpy
260; CHECK-NEXT: call void @llvm.memcpy
261; CHECK-NEXT: ret
262}
Chris Lattner026f5e62011-05-23 05:15:43 +0000263
264
265; The store here is not dead because the byval call reads it.
266declare void @test19f({i32}* byval align 4 %P)
267
268define void @test19({i32} * nocapture byval align 4 %arg5) nounwind ssp {
269bb:
270 %tmp7 = getelementptr inbounds {i32}* %arg5, i32 0, i32 0
271 store i32 912, i32* %tmp7
272 call void @test19f({i32}* byval align 4 %arg5)
273 ret void
274
Stephen Linc1c7a132013-07-14 01:42:54 +0000275; CHECK-LABEL: @test19(
Chris Lattner026f5e62011-05-23 05:15:43 +0000276; CHECK: store i32 912
277; CHECK: call void @test19f
278}
279
Nick Lewycky32f80512011-10-22 21:59:35 +0000280define void @test20() {
281 %m = call i8* @malloc(i32 24)
282 store i8 0, i8* %m
283 ret void
284}
Stephen Linc1c7a132013-07-14 01:42:54 +0000285; CHECK-LABEL: @test20(
Nick Lewyckydd1d3df2011-10-24 04:35:36 +0000286; CHECK-NEXT: ret void
Nuno Lopes300d6292012-05-10 17:14:00 +0000287
Stephen Linc1c7a132013-07-14 01:42:54 +0000288; CHECK-LABEL: @test21(
Nuno Lopes300d6292012-05-10 17:14:00 +0000289define void @test21() {
290 %m = call i8* @calloc(i32 9, i32 7)
291 store i8 0, i8* %m
292; CHECK-NEXT: ret void
293 ret void
294}
Dan Gohmaned7c24e22012-05-10 18:57:38 +0000295
Stephen Linc1c7a132013-07-14 01:42:54 +0000296; CHECK-LABEL: @test22(
Dan Gohmaned7c24e22012-05-10 18:57:38 +0000297define void @test22(i1 %i, i32 %k, i32 %m) nounwind {
298 %k.addr = alloca i32
299 %m.addr = alloca i32
300 %k.addr.m.addr = select i1 %i, i32* %k.addr, i32* %m.addr
301 store i32 0, i32* %k.addr.m.addr, align 4
302; CHECK-NEXT: ret void
303 ret void
304}
Eli Friedman08ec0a82012-08-08 02:17:32 +0000305
306; PR13547
Stephen Linc1c7a132013-07-14 01:42:54 +0000307; CHECK-LABEL: @test23(
Eli Friedman08ec0a82012-08-08 02:17:32 +0000308; CHECK: store i8 97
309; CHECK: store i8 0
310declare noalias i8* @strdup(i8* nocapture) nounwind
311define noalias i8* @test23() nounwind uwtable ssp {
312 %x = alloca [2 x i8], align 1
313 %arrayidx = getelementptr inbounds [2 x i8]* %x, i64 0, i64 0
314 store i8 97, i8* %arrayidx, align 1
315 %arrayidx1 = getelementptr inbounds [2 x i8]* %x, i64 0, i64 1
316 store i8 0, i8* %arrayidx1, align 1
317 %call = call i8* @strdup(i8* %arrayidx) nounwind
318 ret i8* %call
319}
Craig Topper2a404182012-08-14 07:32:05 +0000320
321; Make sure same sized store to later element is deleted
Stephen Linc1c7a132013-07-14 01:42:54 +0000322; CHECK-LABEL: @test24(
Craig Topper2a404182012-08-14 07:32:05 +0000323; CHECK-NOT: store i32 0
324; CHECK-NOT: store i32 0
325; CHECK: store i32 %b
326; CHECK: store i32 %c
327; CHECK: ret void
328define void @test24([2 x i32]* %a, i32 %b, i32 %c) nounwind {
329 %1 = getelementptr inbounds [2 x i32]* %a, i64 0, i64 0
330 store i32 0, i32* %1, align 4
331 %2 = getelementptr inbounds [2 x i32]* %a, i64 0, i64 1
332 store i32 0, i32* %2, align 4
333 %3 = getelementptr inbounds [2 x i32]* %a, i64 0, i64 0
334 store i32 %b, i32* %3, align 4
335 %4 = getelementptr inbounds [2 x i32]* %a, i64 0, i64 1
336 store i32 %c, i32* %4, align 4
337 ret void
338}
Bob Wilsondcc54de2012-09-03 05:15:15 +0000339
340; Check another case like PR13547 where strdup is not like malloc.
Stephen Linc1c7a132013-07-14 01:42:54 +0000341; CHECK-LABEL: @test25(
Bob Wilsondcc54de2012-09-03 05:15:15 +0000342; CHECK: load i8
343; CHECK: store i8 0
344; CHECK: store i8 %tmp
345define i8* @test25(i8* %p) nounwind {
346 %p.4 = getelementptr i8* %p, i64 4
347 %tmp = load i8* %p.4, align 1
348 store i8 0, i8* %p.4, align 1
349 %q = call i8* @strdup(i8* %p) nounwind optsize
350 store i8 %tmp, i8* %p.4, align 1
351 ret i8* %q
352}