Home | History | Annotate | Download | only in DeadStoreElimination
      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"
      3 
      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*)
      7 
      8 define void @test1(i32* %Q, i32* %P) {
      9         %DEAD = load i32* %Q
     10         store i32 %DEAD, i32* %P
     11         store i32 0, i32* %P
     12         ret void
     13 ; CHECK: @test1
     14 ; CHECK-NEXT: store i32 0, i32* %P
     15 ; CHECK-NEXT: ret void
     16 }
     17 
     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
     23   ret void
     24 ; CHECK: @test2
     25 ; CHECK-NEXT: store i32 20
     26 }
     27 
     28 
     29 ; PR8677
     30 @g = global i32 1
     31 
     32 define i32 @test3(i32* %g_addr) nounwind {
     33 ; CHECK: @test3
     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
     39   ret i32 %tmp3
     40 }
     41 
     42 
     43 define void @test4(i32* %Q) {
     44         %a = load i32* %Q
     45         store volatile i32 %a, i32* %Q
     46         ret void
     47 ; CHECK: @test4
     48 ; CHECK-NEXT: load i32
     49 ; CHECK-NEXT: store volatile
     50 ; CHECK-NEXT: ret void
     51 }
     52 
     53 define void @test5(i32* %Q) {
     54         %a = load volatile i32* %Q
     55         store i32 %a, i32* %Q
     56         ret void
     57 ; CHECK: @test5
     58 ; CHECK-NEXT: load volatile
     59 ; CHECK-NEXT: ret void
     60 }
     61 
     62 ; Should delete store of 10 even though memset is a may-store to P (P and Q may
     63 ; alias).
     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
     68   ret void
     69 ; CHECK: @test6
     70 ; CHECK-NEXT: call void @llvm.memset
     71 }
     72 
     73 ; Should delete store of 10 even though memcpy is a may-store to P (P and Q may
     74 ; alias).
     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
     79   ret void
     80 ; CHECK: @test7
     81 ; CHECK-NEXT: call void @llvm.memcpy
     82 }
     83 
     84 ; Do not delete stores that are only partially killed.
     85 define 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         
     93 ; CHECK: @test8
     94 ; CHECK: store i32 1234567
     95 }
     96 
     97 
     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
    103 	ret void
    104 ; CHECK: @test9
    105 ; CHECK-NEXT: ret void
    106 }
    107 
    108 ; va_arg has fuzzy dependence, the store shouldn't be zapped.
    109 define double @test10(i8* %X) {
    110         %X_addr = alloca i8*
    111         store i8* %X, i8** %X_addr
    112         %tmp.0 = va_arg i8** %X_addr, double
    113         ret double %tmp.0
    114 ; CHECK: @test10
    115 ; CHECK: store
    116 }
    117 
    118 
    119 ; DSE should delete the dead trampoline.
    120 declare void @test11f()
    121 define void @test11() {
    122 ; CHECK: @test11
    123 	%storage = alloca [10 x i8], align 16		; <[10 x i8]*> [#uses=1]
    124 ; CHECK-NOT: alloca
    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
    128 	ret void
    129 ; CHECK: ret void
    130 }
    131 
    132 
    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
    142 	ret void
    143 ; CHECK: @test12
    144 ; CHECK-NOT: tmp5
    145 ; CHECK: ret void
    146 }
    147 
    148 
    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*
    154         %DEAD = load i32* %P
    155         %DEAD2 = add i32 %DEAD, 1
    156         store i32 %DEAD2, i32* %P
    157         call void @test13f( )
    158         store i32 0, i32* %P
    159         ret i32* %P
    160 ; CHECK: @test13()
    161 ; CHECK-NEXT: malloc
    162 ; CHECK-NEXT: bitcast
    163 ; CHECK-NEXT: call void
    164 }
    165 
    166 declare noalias i8* @malloc(i32)
    167 
    168 
    169 
    170 define void @test14(i32* %Q) {
    171         %P = alloca i32
    172         %DEAD = load i32* %Q
    173         store i32 %DEAD, i32* %P
    174         ret void
    175 
    176 ; CHECK: @test14
    177 ; CHECK-NEXT: ret void
    178 }
    179 
    180 
    181 ; PR8701
    182 
    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)
    187   ret void
    188 ; CHECK: @test15
    189 ; CHECK-NEXT: call void @llvm.memcpy
    190 ; CHECK-NEXT: ret
    191 }
    192 
    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)
    197   ret void
    198 ; CHECK: @test16
    199 ; CHECK-NEXT: call void @llvm.memcpy
    200 ; CHECK-NEXT: ret
    201 }
    202 
    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)
    207   ret void
    208 ; CHECK: @test17
    209 ; CHECK-NEXT: call void @llvm.memcpy
    210 ; CHECK-NEXT: ret
    211 }
    212 
    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)
    217   ret void
    218 ; CHECK: @test17v
    219 ; CHECK-NEXT: call void @llvm.memset
    220 ; CHECK-NEXT: call void @llvm.memcpy
    221 ; CHECK-NEXT: ret
    222 }
    223 
    224 ; PR8728
    225 ; Do not delete instruction where possible situation is:
    226 ; A = B
    227 ; A = A
    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)
    231   ret void
    232 ; CHECK: @test18
    233 ; CHECK-NEXT: call void @llvm.memcpy
    234 ; CHECK-NEXT: call void @llvm.memcpy
    235 ; CHECK-NEXT: ret
    236 }
    237 
    238 
    239 ; The store here is not dead because the byval call reads it.
    240 declare void @test19f({i32}* byval align 4 %P)
    241 
    242 define void @test19({i32} * nocapture byval align 4 %arg5) nounwind ssp {
    243 bb:
    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)
    247   ret void
    248 
    249 ; CHECK: @test19(
    250 ; CHECK: store i32 912
    251 ; CHECK: call void @test19f
    252 }
    253 
    254