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-LABEL: @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-LABEL: @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-LABEL: @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-LABEL: @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-LABEL: @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-LABEL: @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-LABEL: @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-LABEL: @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-LABEL: @test9(
    105 ; CHECK-NEXT: ret void
    106 }
    107 
    108 ; Test for inalloca handling.
    109 define 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 
    117 ; va_arg has fuzzy dependence, the store shouldn't be zapped.
    118 define 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
    123 ; CHECK-LABEL: @test10(
    124 ; CHECK: store
    125 }
    126 
    127 
    128 ; DSE should delete the dead trampoline.
    129 declare void @test11f()
    130 define void @test11() {
    131 ; CHECK-LABEL: @test11(
    132 	%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.
    143 define 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
    152 ; CHECK-LABEL: @test12(
    153 ; CHECK-NOT: tmp5
    154 ; CHECK: ret void
    155 }
    156 
    157 
    158 ; %P doesn't escape, the DEAD instructions should be removed.
    159 declare void @test13f()
    160 define 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 
    175 declare noalias i8* @malloc(i32)
    176 declare noalias i8* @calloc(i32, i32)
    177 
    178 
    179 define void @test14(i32* %Q) {
    180         %P = alloca i32
    181         %DEAD = load i32* %Q
    182         store i32 %DEAD, i32* %P
    183         ret void
    184 
    185 ; CHECK-LABEL: @test14(
    186 ; CHECK-NEXT: ret void
    187 }
    188 
    189 
    190 ; PR8701
    191 
    192 ;; Fully dead overwrite of memcpy.
    193 define void @test15(i8* %P, i8* %Q) nounwind ssp {
    194   tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
    195   tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
    196   ret void
    197 ; CHECK-LABEL: @test15(
    198 ; CHECK-NEXT: call void @llvm.memcpy
    199 ; CHECK-NEXT: ret
    200 }
    201 
    202 ;; Full overwrite of smaller memcpy.
    203 define void @test16(i8* %P, i8* %Q) nounwind ssp {
    204   tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 8, i32 1, i1 false)
    205   tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
    206   ret void
    207 ; CHECK-LABEL: @test16(
    208 ; CHECK-NEXT: call void @llvm.memcpy
    209 ; CHECK-NEXT: ret
    210 }
    211 
    212 ;; Overwrite of memset by memcpy.
    213 define void @test17(i8* %P, i8* noalias %Q) nounwind ssp {
    214   tail call void @llvm.memset.p0i8.i64(i8* %P, i8 42, i64 8, i32 1, i1 false)
    215   tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
    216   ret void
    217 ; CHECK-LABEL: @test17(
    218 ; CHECK-NEXT: call void @llvm.memcpy
    219 ; CHECK-NEXT: ret
    220 }
    221 
    222 ; Should not delete the volatile memset.
    223 define void @test17v(i8* %P, i8* %Q) nounwind ssp {
    224   tail call void @llvm.memset.p0i8.i64(i8* %P, i8 42, i64 8, i32 1, i1 true)
    225   tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
    226   ret void
    227 ; CHECK-LABEL: @test17v(
    228 ; CHECK-NEXT: call void @llvm.memset
    229 ; CHECK-NEXT: call void @llvm.memcpy
    230 ; CHECK-NEXT: ret
    231 }
    232 
    233 ; PR8728
    234 ; Do not delete instruction where possible situation is:
    235 ; A = B
    236 ; A = A
    237 define void @test18(i8* %P, i8* %Q, i8* %R) nounwind ssp {
    238   tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %Q, i64 12, i32 1, i1 false)
    239   tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %R, i64 12, i32 1, i1 false)
    240   ret void
    241 ; CHECK-LABEL: @test18(
    242 ; CHECK-NEXT: call void @llvm.memcpy
    243 ; CHECK-NEXT: call void @llvm.memcpy
    244 ; CHECK-NEXT: ret
    245 }
    246 
    247 
    248 ; The store here is not dead because the byval call reads it.
    249 declare void @test19f({i32}* byval align 4 %P)
    250 
    251 define void @test19({i32} * nocapture byval align 4 %arg5) nounwind ssp {
    252 bb:
    253   %tmp7 = getelementptr inbounds {i32}* %arg5, i32 0, i32 0
    254   store i32 912, i32* %tmp7
    255   call void @test19f({i32}* byval align 4 %arg5)
    256   ret void
    257 
    258 ; CHECK-LABEL: @test19(
    259 ; CHECK: store i32 912
    260 ; CHECK: call void @test19f
    261 }
    262 
    263 define void @test20() {
    264   %m = call i8* @malloc(i32 24)
    265   store i8 0, i8* %m
    266   ret void
    267 }
    268 ; CHECK-LABEL: @test20(
    269 ; CHECK-NEXT: ret void
    270 
    271 ; CHECK-LABEL: @test21(
    272 define void @test21() {
    273   %m = call i8* @calloc(i32 9, i32 7)
    274   store i8 0, i8* %m
    275 ; CHECK-NEXT: ret void
    276   ret void
    277 }
    278 
    279 ; CHECK-LABEL: @test22(
    280 define void @test22(i1 %i, i32 %k, i32 %m) nounwind {
    281   %k.addr = alloca i32
    282   %m.addr = alloca i32
    283   %k.addr.m.addr = select i1 %i, i32* %k.addr, i32* %m.addr
    284   store i32 0, i32* %k.addr.m.addr, align 4
    285 ; CHECK-NEXT: ret void
    286   ret void
    287 }
    288 
    289 ; PR13547
    290 ; CHECK-LABEL: @test23(
    291 ; CHECK: store i8 97
    292 ; CHECK: store i8 0
    293 declare noalias i8* @strdup(i8* nocapture) nounwind
    294 define noalias i8* @test23() nounwind uwtable ssp {
    295   %x = alloca [2 x i8], align 1
    296   %arrayidx = getelementptr inbounds [2 x i8]* %x, i64 0, i64 0
    297   store i8 97, i8* %arrayidx, align 1
    298   %arrayidx1 = getelementptr inbounds [2 x i8]* %x, i64 0, i64 1
    299   store i8 0, i8* %arrayidx1, align 1
    300   %call = call i8* @strdup(i8* %arrayidx) nounwind
    301   ret i8* %call
    302 }
    303 
    304 ; Make sure same sized store to later element is deleted
    305 ; CHECK-LABEL: @test24(
    306 ; CHECK-NOT: store i32 0
    307 ; CHECK-NOT: store i32 0
    308 ; CHECK: store i32 %b
    309 ; CHECK: store i32 %c
    310 ; CHECK: ret void
    311 define void @test24([2 x i32]* %a, i32 %b, i32 %c) nounwind {
    312   %1 = getelementptr inbounds [2 x i32]* %a, i64 0, i64 0
    313   store i32 0, i32* %1, align 4
    314   %2 = getelementptr inbounds [2 x i32]* %a, i64 0, i64 1
    315   store i32 0, i32* %2, align 4
    316   %3 = getelementptr inbounds [2 x i32]* %a, i64 0, i64 0
    317   store i32 %b, i32* %3, align 4
    318   %4 = getelementptr inbounds [2 x i32]* %a, i64 0, i64 1
    319   store i32 %c, i32* %4, align 4
    320   ret void
    321 }
    322 
    323 ; Check another case like PR13547 where strdup is not like malloc.
    324 ; CHECK-LABEL: @test25(
    325 ; CHECK: load i8
    326 ; CHECK: store i8 0
    327 ; CHECK: store i8 %tmp
    328 define i8* @test25(i8* %p) nounwind {
    329   %p.4 = getelementptr i8* %p, i64 4
    330   %tmp = load i8* %p.4, align 1
    331   store i8 0, i8* %p.4, align 1
    332   %q = call i8* @strdup(i8* %p) nounwind optsize
    333   store i8 %tmp, i8* %p.4, align 1
    334   ret i8* %q
    335 }
    336