1 ; RUN: opt < %s -basicaa -dse -S | FileCheck %s
2 ; RUN: opt < %s -aa-pipeline=basic-aa -passes=dse -S | FileCheck %s
3 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"
5 declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1) nounwind
6 declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind
7 declare void @llvm.init.trampoline(i8*, i8*, i8*)
9 define void @test1(i32* %Q, i32* %P) {
10 %DEAD = load i32, i32* %Q
11 store i32 %DEAD, i32* %P
14 ; CHECK-LABEL: @test1(
15 ; CHECK-NEXT: store i32 0, i32* %P
16 ; CHECK-NEXT: ret void
19 ; PR8576 - Should delete store of 10 even though p/q are may aliases.
20 define void @test2(i32 *%p, i32 *%q) {
21 store i32 10, i32* %p, align 4
22 store i32 20, i32* %q, align 4
23 store i32 30, i32* %p, align 4
25 ; CHECK-LABEL: @test2(
26 ; CHECK-NEXT: store i32 20
33 define i32 @test3(i32* %g_addr) nounwind {
34 ; CHECK-LABEL: @test3(
35 ; CHECK: load i32, i32* %g_addr
36 %g_value = load i32, i32* %g_addr, align 4
37 store i32 -1, i32* @g, align 4
38 store i32 %g_value, i32* %g_addr, align 4
39 %tmp3 = load i32, i32* @g, align 4
44 define void @test4(i32* %Q) {
45 %a = load i32, i32* %Q
46 store volatile i32 %a, i32* %Q
48 ; CHECK-LABEL: @test4(
49 ; CHECK-NEXT: load i32
50 ; CHECK-NEXT: store volatile
51 ; CHECK-NEXT: ret void
54 define void @test5(i32* %Q) {
55 %a = load volatile i32, i32* %Q
58 ; CHECK-LABEL: @test5(
59 ; CHECK-NEXT: load volatile
60 ; CHECK-NEXT: ret void
63 ; Should delete store of 10 even though memset is a may-store to P (P and Q may
65 define void @test6(i32 *%p, i8 *%q) {
66 store i32 10, i32* %p, align 4 ;; dead.
67 call void @llvm.memset.p0i8.i64(i8* %q, i8 42, i64 900, i32 1, i1 false)
68 store i32 30, i32* %p, align 4
70 ; CHECK-LABEL: @test6(
71 ; CHECK-NEXT: call void @llvm.memset
74 ; Should delete store of 10 even though memcpy is a may-store to P (P and Q may
76 define void @test7(i32 *%p, i8 *%q, i8* noalias %r) {
77 store i32 10, i32* %p, align 4 ;; dead.
78 call void @llvm.memcpy.p0i8.p0i8.i64(i8* %q, i8* %r, i64 900, i32 1, i1 false)
79 store i32 30, i32* %p, align 4
81 ; CHECK-LABEL: @test7(
82 ; CHECK-NEXT: call void @llvm.memcpy
85 ; Do not delete stores that are only partially killed.
88 store i32 1234567, i32* %V
89 %V2 = bitcast i32* %V to i8*
91 %X = load i32, i32* %V
94 ; CHECK-LABEL: @test8(
95 ; CHECK: store i32 1234567
99 ; Test for byval handling.
100 %struct.x = type { i32, i32, i32, i32 }
101 define void @test9(%struct.x* byval %a) nounwind {
102 %tmp2 = getelementptr %struct.x, %struct.x* %a, i32 0, i32 0
103 store i32 1, i32* %tmp2, align 4
105 ; CHECK-LABEL: @test9(
106 ; CHECK-NEXT: ret void
109 ; Test for inalloca handling.
110 define void @test9_2(%struct.x* inalloca %a) nounwind {
111 %tmp2 = getelementptr %struct.x, %struct.x* %a, i32 0, i32 0
112 store i32 1, i32* %tmp2, align 4
114 ; CHECK-LABEL: @test9_2(
115 ; CHECK-NEXT: ret void
118 ; va_arg has fuzzy dependence, the store shouldn't be zapped.
119 define double @test10(i8* %X) {
121 store i8* %X, i8** %X_addr
122 %tmp.0 = va_arg i8** %X_addr, double
124 ; CHECK-LABEL: @test10(
129 ; DSE should delete the dead trampoline.
130 declare void @test11f()
131 define void @test11() {
132 ; CHECK-LABEL: @test11(
133 %storage = alloca [10 x i8], align 16 ; <[10 x i8]*> [#uses=1]
135 %cast = getelementptr [10 x i8], [10 x i8]* %storage, i32 0, i32 0 ; <i8*> [#uses=1]
136 call void @llvm.init.trampoline( i8* %cast, i8* bitcast (void ()* @test11f to i8*), i8* null ) ; <i8*> [#uses=1]
137 ; CHECK-NOT: trampoline
143 ; PR2599 - load -> store to same address.
144 define void @test12({ i32, i32 }* %x) nounwind {
145 %tmp4 = getelementptr { i32, i32 }, { i32, i32 }* %x, i32 0, i32 0
146 %tmp5 = load i32, i32* %tmp4, align 4
147 %tmp7 = getelementptr { i32, i32 }, { i32, i32 }* %x, i32 0, i32 1
148 %tmp8 = load i32, i32* %tmp7, align 4
149 %tmp17 = sub i32 0, %tmp8
150 store i32 %tmp5, i32* %tmp4, align 4
151 store i32 %tmp17, i32* %tmp7, align 4
153 ; CHECK-LABEL: @test12(
159 ; %P doesn't escape, the DEAD instructions should be removed.
160 declare void @test13f()
161 define i32* @test13() {
162 %p = tail call i8* @malloc(i32 4)
163 %P = bitcast i8* %p to i32*
164 %DEAD = load i32, i32* %P
165 %DEAD2 = add i32 %DEAD, 1
166 store i32 %DEAD2, i32* %P
167 call void @test13f( )
172 ; CHECK-NEXT: bitcast
173 ; CHECK-NEXT: call void
176 define i32 addrspace(1)* @test13_addrspacecast() {
177 %p = tail call i8* @malloc(i32 4)
178 %p.bc = bitcast i8* %p to i32*
179 %P = addrspacecast i32* %p.bc to i32 addrspace(1)*
180 %DEAD = load i32, i32 addrspace(1)* %P
181 %DEAD2 = add i32 %DEAD, 1
182 store i32 %DEAD2, i32 addrspace(1)* %P
183 call void @test13f( )
184 store i32 0, i32 addrspace(1)* %P
185 ret i32 addrspace(1)* %P
186 ; CHECK: @test13_addrspacecast()
188 ; CHECK-NEXT: bitcast
189 ; CHECK-NEXT: addrspacecast
190 ; CHECK-NEXT: call void
193 declare noalias i8* @malloc(i32)
194 declare noalias i8* @calloc(i32, i32)
197 define void @test14(i32* %Q) {
199 %DEAD = load i32, i32* %Q
200 store i32 %DEAD, i32* %P
203 ; CHECK-LABEL: @test14(
204 ; CHECK-NEXT: ret void
210 ;; Fully dead overwrite of memcpy.
211 define void @test15(i8* %P, i8* %Q) nounwind ssp {
212 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
213 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
215 ; CHECK-LABEL: @test15(
216 ; CHECK-NEXT: call void @llvm.memcpy
220 ;; Full overwrite of smaller memcpy.
221 define void @test16(i8* %P, i8* %Q) nounwind ssp {
222 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 8, i32 1, i1 false)
223 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
225 ; CHECK-LABEL: @test16(
226 ; CHECK-NEXT: call void @llvm.memcpy
230 ;; Overwrite of memset by memcpy.
231 define void @test17(i8* %P, i8* noalias %Q) nounwind ssp {
232 tail call void @llvm.memset.p0i8.i64(i8* %P, i8 42, i64 8, i32 1, i1 false)
233 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
235 ; CHECK-LABEL: @test17(
236 ; CHECK-NEXT: call void @llvm.memcpy
240 ; Should not delete the volatile memset.
241 define void @test17v(i8* %P, i8* %Q) nounwind ssp {
242 tail call void @llvm.memset.p0i8.i64(i8* %P, i8 42, i64 8, i32 1, i1 true)
243 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
245 ; CHECK-LABEL: @test17v(
246 ; CHECK-NEXT: call void @llvm.memset
247 ; CHECK-NEXT: call void @llvm.memcpy
252 ; Do not delete instruction where possible situation is:
255 define void @test18(i8* %P, i8* %Q, i8* %R) nounwind ssp {
256 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
257 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %R, i64 12, i32 1, i1 false)
259 ; CHECK-LABEL: @test18(
260 ; CHECK-NEXT: call void @llvm.memcpy
261 ; CHECK-NEXT: call void @llvm.memcpy
266 ; The store here is not dead because the byval call reads it.
267 declare void @test19f({i32}* byval align 4 %P)
269 define void @test19({i32} * nocapture byval align 4 %arg5) nounwind ssp {
271 %tmp7 = getelementptr inbounds {i32}, {i32}* %arg5, i32 0, i32 0
272 store i32 912, i32* %tmp7
273 call void @test19f({i32}* byval align 4 %arg5)
276 ; CHECK-LABEL: @test19(
277 ; CHECK: store i32 912
278 ; CHECK: call void @test19f
281 define void @test20() {
282 %m = call i8* @malloc(i32 24)
286 ; CHECK-LABEL: @test20(
287 ; CHECK-NEXT: ret void
289 ; CHECK-LABEL: @test21(
290 define void @test21() {
291 %m = call i8* @calloc(i32 9, i32 7)
293 ; CHECK-NEXT: ret void
297 ; CHECK-LABEL: @test22(
298 define void @test22(i1 %i, i32 %k, i32 %m) nounwind {
301 %k.addr.m.addr = select i1 %i, i32* %k.addr, i32* %m.addr
302 store i32 0, i32* %k.addr.m.addr, align 4
303 ; CHECK-NEXT: ret void
308 ; CHECK-LABEL: @test23(
311 declare noalias i8* @strdup(i8* nocapture) nounwind
312 define noalias i8* @test23() nounwind uwtable ssp {
313 %x = alloca [2 x i8], align 1
314 %arrayidx = getelementptr inbounds [2 x i8], [2 x i8]* %x, i64 0, i64 0
315 store i8 97, i8* %arrayidx, align 1
316 %arrayidx1 = getelementptr inbounds [2 x i8], [2 x i8]* %x, i64 0, i64 1
317 store i8 0, i8* %arrayidx1, align 1
318 %call = call i8* @strdup(i8* %arrayidx) nounwind
322 ; Make sure same sized store to later element is deleted
323 ; CHECK-LABEL: @test24(
324 ; CHECK-NOT: store i32 0
325 ; CHECK-NOT: store i32 0
326 ; CHECK: store i32 %b
327 ; CHECK: store i32 %c
329 define void @test24([2 x i32]* %a, i32 %b, i32 %c) nounwind {
330 %1 = getelementptr inbounds [2 x i32], [2 x i32]* %a, i64 0, i64 0
331 store i32 0, i32* %1, align 4
332 %2 = getelementptr inbounds [2 x i32], [2 x i32]* %a, i64 0, i64 1
333 store i32 0, i32* %2, align 4
334 %3 = getelementptr inbounds [2 x i32], [2 x i32]* %a, i64 0, i64 0
335 store i32 %b, i32* %3, align 4
336 %4 = getelementptr inbounds [2 x i32], [2 x i32]* %a, i64 0, i64 1
337 store i32 %c, i32* %4, align 4
341 ; Check another case like PR13547 where strdup is not like malloc.
342 ; CHECK-LABEL: @test25(
345 ; CHECK: store i8 %tmp
346 define i8* @test25(i8* %p) nounwind {
347 %p.4 = getelementptr i8, i8* %p, i64 4
348 %tmp = load i8, i8* %p.4, align 1
349 store i8 0, i8* %p.4, align 1
350 %q = call i8* @strdup(i8* %p) nounwind optsize
351 store i8 %tmp, i8* %p.4, align 1
355 ; Remove redundant store if loaded value is in another block.
356 ; CHECK-LABEL: @test26(
359 define i32 @test26(i1 %c, i32* %p) {
361 %v = load i32, i32* %p, align 4
362 br i1 %c, label %bb1, label %bb2
366 store i32 %v, i32* %p, align 4
372 ; Remove redundant store if loaded value is in another block.
373 ; CHECK-LABEL: @test27(
376 define i32 @test27(i1 %c, i32* %p) {
378 %v = load i32, i32* %p, align 4
379 br i1 %c, label %bb1, label %bb2
385 store i32 %v, i32* %p, align 4
389 ; Don't remove redundant store because of may-aliased store.
390 ; CHECK-LABEL: @test28(
392 ; CHECK-NEXT: store i32 %v
393 define i32 @test28(i1 %c, i32* %p, i32* %p2, i32 %i) {
395 %v = load i32, i32* %p, align 4
397 ; Might overwrite value at %p
398 store i32 %i, i32* %p2, align 4
399 br i1 %c, label %bb1, label %bb2
405 store i32 %v, i32* %p, align 4
409 ; Don't remove redundant store because of may-aliased store.
410 ; CHECK-LABEL: @test29(
412 ; CHECK-NEXT: store i32 %v
413 define i32 @test29(i1 %c, i32* %p, i32* %p2, i32 %i) {
415 %v = load i32, i32* %p, align 4
416 br i1 %c, label %bb1, label %bb2
420 ; Might overwrite value at %p
421 store i32 %i, i32* %p2, align 4
424 store i32 %v, i32* %p, align 4
428 declare void @unknown_func()
430 ; Don't remove redundant store because of unknown call.
431 ; CHECK-LABEL: @test30(
433 ; CHECK-NEXT: store i32 %v
434 define i32 @test30(i1 %c, i32* %p, i32 %i) {
436 %v = load i32, i32* %p, align 4
437 br i1 %c, label %bb1, label %bb2
441 ; Might overwrite value at %p
442 call void @unknown_func()
445 store i32 %v, i32* %p, align 4
449 ; Remove redundant store if loaded value is in another block inside a loop.
450 ; CHECK-LABEL: @test31(
453 define i32 @test31(i1 %c, i32* %p, i32 %i) {
455 %v = load i32, i32* %p, align 4
458 store i32 %v, i32* %p, align 4
459 br i1 undef, label %bb1, label %bb2
464 ; Don't remove redundant store in a loop with a may-alias store.
465 ; CHECK-LABEL: @test32(
467 ; CHECK-NEXT: store i32 %v
468 ; CHECK-NEXT: call void @unknown_func
469 define i32 @test32(i1 %c, i32* %p, i32 %i) {
471 %v = load i32, i32* %p, align 4
474 store i32 %v, i32* %p, align 4
475 ; Might read and overwrite value at %p
476 call void @unknown_func()
477 br i1 undef, label %bb1, label %bb2
482 ; Remove redundant store, which is in the lame loop as the load.
483 ; CHECK-LABEL: @test33(
486 define i32 @test33(i1 %c, i32* %p, i32 %i) {
490 %v = load i32, i32* %p, align 4
493 store i32 %v, i32* %p, align 4
494 ; Might read and overwrite value at %p, but doesn't matter.
495 call void @unknown_func()
496 br i1 undef, label %bb1, label %bb3
501 ; Don't remove redundant store: unknown_func could unwind
502 ; CHECK-LABEL: @test34(
506 define void @test34(i32* noalias %p) {
508 call void @unknown_func()
513 ; Remove redundant store even with an unwinding function in the same block
514 ; CHECK-LABEL: @test35(
515 ; CHECK: call void @unknown_func
516 ; CHECK-NEXT: store i32 0
517 ; CHECK-NEXT: ret void
518 define void @test35(i32* noalias %p) {
519 call void @unknown_func()