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 ; 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-LABEL: @test10( 115 ; CHECK: store 116 } 117 118 119 ; DSE should delete the dead trampoline. 120 declare void @test11f() 121 define void @test11() { 122 ; CHECK-LABEL: @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-LABEL: @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 declare noalias i8* @calloc(i32, i32) 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-LABEL: @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-LABEL: @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-LABEL: @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-LABEL: @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-LABEL: @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-LABEL: @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-LABEL: @test19( 250 ; CHECK: store i32 912 251 ; CHECK: call void @test19f 252 } 253 254 define void @test20() { 255 %m = call i8* @malloc(i32 24) 256 store i8 0, i8* %m 257 ret void 258 } 259 ; CHECK-LABEL: @test20( 260 ; CHECK-NEXT: ret void 261 262 ; CHECK-LABEL: @test21( 263 define void @test21() { 264 %m = call i8* @calloc(i32 9, i32 7) 265 store i8 0, i8* %m 266 ; CHECK-NEXT: ret void 267 ret void 268 } 269 270 ; CHECK-LABEL: @test22( 271 define void @test22(i1 %i, i32 %k, i32 %m) nounwind { 272 %k.addr = alloca i32 273 %m.addr = alloca i32 274 %k.addr.m.addr = select i1 %i, i32* %k.addr, i32* %m.addr 275 store i32 0, i32* %k.addr.m.addr, align 4 276 ; CHECK-NEXT: ret void 277 ret void 278 } 279 280 ; PR13547 281 ; CHECK-LABEL: @test23( 282 ; CHECK: store i8 97 283 ; CHECK: store i8 0 284 declare noalias i8* @strdup(i8* nocapture) nounwind 285 define noalias i8* @test23() nounwind uwtable ssp { 286 %x = alloca [2 x i8], align 1 287 %arrayidx = getelementptr inbounds [2 x i8]* %x, i64 0, i64 0 288 store i8 97, i8* %arrayidx, align 1 289 %arrayidx1 = getelementptr inbounds [2 x i8]* %x, i64 0, i64 1 290 store i8 0, i8* %arrayidx1, align 1 291 %call = call i8* @strdup(i8* %arrayidx) nounwind 292 ret i8* %call 293 } 294 295 ; Make sure same sized store to later element is deleted 296 ; CHECK-LABEL: @test24( 297 ; CHECK-NOT: store i32 0 298 ; CHECK-NOT: store i32 0 299 ; CHECK: store i32 %b 300 ; CHECK: store i32 %c 301 ; CHECK: ret void 302 define void @test24([2 x i32]* %a, i32 %b, i32 %c) nounwind { 303 %1 = getelementptr inbounds [2 x i32]* %a, i64 0, i64 0 304 store i32 0, i32* %1, align 4 305 %2 = getelementptr inbounds [2 x i32]* %a, i64 0, i64 1 306 store i32 0, i32* %2, align 4 307 %3 = getelementptr inbounds [2 x i32]* %a, i64 0, i64 0 308 store i32 %b, i32* %3, align 4 309 %4 = getelementptr inbounds [2 x i32]* %a, i64 0, i64 1 310 store i32 %c, i32* %4, align 4 311 ret void 312 } 313 314 ; Check another case like PR13547 where strdup is not like malloc. 315 ; CHECK-LABEL: @test25( 316 ; CHECK: load i8 317 ; CHECK: store i8 0 318 ; CHECK: store i8 %tmp 319 define i8* @test25(i8* %p) nounwind { 320 %p.4 = getelementptr i8* %p, i64 4 321 %tmp = load i8* %p.4, align 1 322 store i8 0, i8* %p.4, align 1 323 %q = call i8* @strdup(i8* %p) nounwind optsize 324 store i8 %tmp, i8* %p.4, align 1 325 ret i8* %q 326 } 327