1; RUN: opt < %s -basicaa -dse -S | FileCheck %s 2target 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 4declare 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 6declare void @llvm.init.trampoline(i8*, i8*, i8*) 7 8define void @test1(i32* %Q, i32* %P) { 9 %DEAD = load i32, 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. 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 24; CHECK-LABEL: @test2( 25; CHECK-NEXT: store i32 20 26} 27 28 29; PR8677 30@g = global i32 1 31 32define i32 @test3(i32* %g_addr) nounwind { 33; CHECK-LABEL: @test3( 34; CHECK: load i32, i32* %g_addr 35 %g_value = load i32, 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, i32* @g, align 4 39 ret i32 %tmp3 40} 41 42 43define void @test4(i32* %Q) { 44 %a = load i32, 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 53define void @test5(i32* %Q) { 54 %a = load volatile i32, 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). 64define 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). 75define 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. 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, 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 } 100define void @test9(%struct.x* byval %a) nounwind { 101 %tmp2 = getelementptr %struct.x, %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. 109define void @test9_2(%struct.x* inalloca %a) nounwind { 110 %tmp2 = getelementptr %struct.x, %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. 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 123; CHECK-LABEL: @test10( 124; CHECK: store 125} 126 127 128; DSE should delete the dead trampoline. 129declare void @test11f() 130define 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], [10 x i8]* %storage, i32 0, i32 0 ; <i8*> [#uses=1] 135 call void @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 }, { i32, i32 }* %x, i32 0, i32 0 145 %tmp5 = load i32, i32* %tmp4, align 4 146 %tmp7 = getelementptr { i32, i32 }, { i32, i32 }* %x, i32 0, i32 1 147 %tmp8 = load i32, 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. 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, 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 175define 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, 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 192declare noalias i8* @malloc(i32) 193declare noalias i8* @calloc(i32, i32) 194 195 196define void @test14(i32* %Q) { 197 %P = alloca i32 198 %DEAD = load i32, i32* %Q 199 store i32 %DEAD, i32* %P 200 ret void 201 202; CHECK-LABEL: @test14( 203; CHECK-NEXT: ret void 204} 205 206 207; PR8701 208 209;; Fully dead overwrite of memcpy. 210define void @test15(i8* %P, i8* %Q) nounwind ssp { 211 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) 213 ret void 214; CHECK-LABEL: @test15( 215; 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 { 221 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) 223 ret void 224; CHECK-LABEL: @test16( 225; CHECK-NEXT: call void @llvm.memcpy 226; CHECK-NEXT: ret 227} 228 229;; Overwrite of memset by memcpy. 230define void @test17(i8* %P, i8* noalias %Q) nounwind ssp { 231 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) 233 ret void 234; CHECK-LABEL: @test17( 235; CHECK-NEXT: call void @llvm.memcpy 236; CHECK-NEXT: ret 237} 238 239; 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 244; CHECK-LABEL: @test17v( 245; CHECK-NEXT: call void @llvm.memset 246; CHECK-NEXT: call void @llvm.memcpy 247; CHECK-NEXT: ret 248} 249 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 { 255 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) 257 ret void 258; CHECK-LABEL: @test18( 259; CHECK-NEXT: call void @llvm.memcpy 260; CHECK-NEXT: call void @llvm.memcpy 261; CHECK-NEXT: ret 262} 263 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}, {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 275; CHECK-LABEL: @test19( 276; CHECK: store i32 912 277; CHECK: call void @test19f 278} 279 280define void @test20() { 281 %m = call i8* @malloc(i32 24) 282 store i8 0, i8* %m 283 ret void 284} 285; CHECK-LABEL: @test20( 286; CHECK-NEXT: ret void 287 288; CHECK-LABEL: @test21( 289define 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} 295 296; CHECK-LABEL: @test22( 297define 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} 305 306; PR13547 307; CHECK-LABEL: @test23( 308; 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], [2 x i8]* %x, i64 0, i64 0 314 store i8 97, i8* %arrayidx, align 1 315 %arrayidx1 = getelementptr inbounds [2 x i8], [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} 320 321; Make sure same sized store to later element is deleted 322; CHECK-LABEL: @test24( 323; 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], [2 x i32]* %a, i64 0, i64 0 330 store i32 0, i32* %1, align 4 331 %2 = getelementptr inbounds [2 x i32], [2 x i32]* %a, i64 0, i64 1 332 store i32 0, i32* %2, align 4 333 %3 = getelementptr inbounds [2 x i32], [2 x i32]* %a, i64 0, i64 0 334 store i32 %b, i32* %3, align 4 335 %4 = getelementptr inbounds [2 x i32], [2 x i32]* %a, i64 0, i64 1 336 store i32 %c, i32* %4, align 4 337 ret void 338} 339 340; Check another case like PR13547 where strdup is not like malloc. 341; CHECK-LABEL: @test25( 342; CHECK: load i8 343; CHECK: store i8 0 344; CHECK: store i8 %tmp 345define i8* @test25(i8* %p) nounwind { 346 %p.4 = getelementptr i8, i8* %p, i64 4 347 %tmp = load i8, 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} 353 354; Remove redundant store if loaded value is in another block. 355; CHECK-LABEL: @test26( 356; CHECK-NOT: store 357; CHECK: ret 358define i32 @test26(i1 %c, i32* %p) { 359entry: 360 %v = load i32, i32* %p, align 4 361 br i1 %c, label %bb1, label %bb2 362bb1: 363 br label %bb3 364bb2: 365 store i32 %v, i32* %p, align 4 366 br label %bb3 367bb3: 368 ret i32 0 369} 370 371; Remove redundant store if loaded value is in another block. 372; CHECK-LABEL: @test27( 373; CHECK-NOT: store 374; CHECK: ret 375define i32 @test27(i1 %c, i32* %p) { 376entry: 377 %v = load i32, i32* %p, align 4 378 br i1 %c, label %bb1, label %bb2 379bb1: 380 br label %bb3 381bb2: 382 br label %bb3 383bb3: 384 store i32 %v, i32* %p, align 4 385 ret i32 0 386} 387 388; Don't remove redundant store because of may-aliased store. 389; CHECK-LABEL: @test28( 390; CHECK: bb3: 391; CHECK-NEXT: store i32 %v 392define i32 @test28(i1 %c, i32* %p, i32* %p2, i32 %i) { 393entry: 394 %v = load i32, i32* %p, align 4 395 396 ; Might overwrite value at %p 397 store i32 %i, i32* %p2, align 4 398 br i1 %c, label %bb1, label %bb2 399bb1: 400 br label %bb3 401bb2: 402 br label %bb3 403bb3: 404 store i32 %v, i32* %p, align 4 405 ret i32 0 406} 407 408; Don't remove redundant store because of may-aliased store. 409; CHECK-LABEL: @test29( 410; CHECK: bb3: 411; CHECK-NEXT: store i32 %v 412define i32 @test29(i1 %c, i32* %p, i32* %p2, i32 %i) { 413entry: 414 %v = load i32, i32* %p, align 4 415 br i1 %c, label %bb1, label %bb2 416bb1: 417 br label %bb3 418bb2: 419 ; Might overwrite value at %p 420 store i32 %i, i32* %p2, align 4 421 br label %bb3 422bb3: 423 store i32 %v, i32* %p, align 4 424 ret i32 0 425} 426 427declare void @unknown_func() 428 429; Don't remove redundant store because of unknown call. 430; CHECK-LABEL: @test30( 431; CHECK: bb3: 432; CHECK-NEXT: store i32 %v 433define i32 @test30(i1 %c, i32* %p, i32 %i) { 434entry: 435 %v = load i32, i32* %p, align 4 436 br i1 %c, label %bb1, label %bb2 437bb1: 438 br label %bb3 439bb2: 440 ; Might overwrite value at %p 441 call void @unknown_func() 442 br label %bb3 443bb3: 444 store i32 %v, i32* %p, align 4 445 ret i32 0 446} 447 448; Remove redundant store if loaded value is in another block inside a loop. 449; CHECK-LABEL: @test31( 450; CHECK-NOT: store 451; CHECK: ret 452define i32 @test31(i1 %c, i32* %p, i32 %i) { 453entry: 454 %v = load i32, i32* %p, align 4 455 br label %bb1 456bb1: 457 store i32 %v, i32* %p, align 4 458 br i1 undef, label %bb1, label %bb2 459bb2: 460 ret i32 0 461} 462 463; Don't remove redundant store in a loop with a may-alias store. 464; CHECK-LABEL: @test32( 465; CHECK: bb1: 466; CHECK-NEXT: store i32 %v 467; CHECK-NEXT: call void @unknown_func 468define i32 @test32(i1 %c, i32* %p, i32 %i) { 469entry: 470 %v = load i32, i32* %p, align 4 471 br label %bb1 472bb1: 473 store i32 %v, i32* %p, align 4 474 ; Might read and overwrite value at %p 475 call void @unknown_func() 476 br i1 undef, label %bb1, label %bb2 477bb2: 478 ret i32 0 479} 480 481; Remove redundant store, which is in the lame loop as the load. 482; CHECK-LABEL: @test33( 483; CHECK-NOT: store 484; CHECK: ret 485define i32 @test33(i1 %c, i32* %p, i32 %i) { 486entry: 487 br label %bb1 488bb1: 489 %v = load i32, i32* %p, align 4 490 br label %bb2 491bb2: 492 store i32 %v, i32* %p, align 4 493 ; Might read and overwrite value at %p, but doesn't matter. 494 call void @unknown_func() 495 br i1 undef, label %bb1, label %bb3 496bb3: 497 ret i32 0 498} 499 500