llvm/llvm/test/Transforms/DeadStoreElimination/stores-of-existing-values.ll

; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -passes=dse -dse-optimize-memoryssa=false -S %s | FileCheck --check-prefixes=CHECK,UNOPT %s
; RUN: opt -passes=dse -dse-optimize-memoryssa -S %s | FileCheck --check-prefixes=CHECK,OPT %s
; RUN: opt -passes=dse -S %s | FileCheck --check-prefixes=CHECK,OPT %s

target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"

@a = external global [32 x i8], align 16

declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg)

; Test case for PR16520. The store in %if.then is redundant, because the same value
; has been stored earlier to the same location.
define void @test1_pr16520(i1 %b, ptr nocapture %r) {
; CHECK-LABEL: @test1_pr16520(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    store i8 1, ptr [[R:%.*]], align 1
; CHECK-NEXT:    br i1 [[B:%.*]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK:       if.then:
; CHECK-NEXT:    tail call void @fn_mayread_or_clobber()
; CHECK-NEXT:    br label [[IF_END:%.*]]
; CHECK:       if.else:
; CHECK-NEXT:    tail call void @fn_mayread_or_clobber()
; CHECK-NEXT:    br label [[IF_END]]
; CHECK:       if.end:
; CHECK-NEXT:    ret void
;
entry:
  store i8 1, ptr %r, align 1
  br i1 %b, label %if.then, label %if.else

if.then:                                          ; preds = %entry
  store i8 1, ptr %r, align 1
  tail call void @fn_mayread_or_clobber()
  br label %if.end

if.else:                                          ; preds = %entry
  tail call void @fn_mayread_or_clobber()
  br label %if.end

if.end:                                           ; preds = %if.else, %if.then
  ret void
}

declare void @fn_mayread_or_clobber()
declare void @fn_readonly() readonly

define void @test2(i1 %b, ptr nocapture %r) {
; CHECK-LABEL: @test2(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    store i8 1, ptr [[R:%.*]], align 1
; CHECK-NEXT:    br i1 [[B:%.*]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK:       if.then:
; CHECK-NEXT:    tail call void @fn_readonly()
; CHECK-NEXT:    br label [[IF_END:%.*]]
; CHECK:       if.else:
; CHECK-NEXT:    tail call void @fn_readonly()
; CHECK-NEXT:    br label [[IF_END]]
; CHECK:       if.end:
; CHECK-NEXT:    ret void
;
entry:
  store i8 1, ptr %r, align 1
  br i1 %b, label %if.then, label %if.else

if.then:                                          ; preds = %entry
  tail call void @fn_readonly()
  br label %if.end

if.else:                                          ; preds = %entry
  tail call void @fn_readonly()
  br label %if.end

if.end:                                           ; preds = %if.else, %if.then
  store i8 1, ptr %r, align 1
  ret void
}

; Make sure volatile stores are not removed.
define void @test2_volatile(i1 %b, ptr nocapture %r) {
; CHECK-LABEL: @test2_volatile(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    store volatile i8 1, ptr [[R:%.*]], align 1
; CHECK-NEXT:    br i1 [[B:%.*]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK:       if.then:
; CHECK-NEXT:    tail call void @fn_readonly()
; CHECK-NEXT:    br label [[IF_END:%.*]]
; CHECK:       if.else:
; CHECK-NEXT:    tail call void @fn_readonly()
; CHECK-NEXT:    br label [[IF_END]]
; CHECK:       if.end:
; CHECK-NEXT:    store volatile i8 1, ptr [[R]], align 1
; CHECK-NEXT:    ret void
;
entry:
  store volatile i8 1, ptr %r, align 1
  br i1 %b, label %if.then, label %if.else

if.then:                                          ; preds = %entry
  tail call void @fn_readonly()
  br label %if.end

if.else:                                          ; preds = %entry
  tail call void @fn_readonly()
  br label %if.end

if.end:                                           ; preds = %if.else, %if.then
  store volatile i8 1, ptr %r, align 1
  ret void
}

define void @test3(i1 %b, ptr nocapture %r) {
; CHECK-LABEL: @test3(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    store i8 1, ptr [[R:%.*]], align 1
; CHECK-NEXT:    br i1 [[B:%.*]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK:       if.then:
; CHECK-NEXT:    tail call void @fn_mayread_or_clobber()
; CHECK-NEXT:    br label [[IF_END:%.*]]
; CHECK:       if.else:
; CHECK-NEXT:    tail call void @fn_readonly()
; CHECK-NEXT:    br label [[IF_END]]
; CHECK:       if.end:
; CHECK-NEXT:    store i8 1, ptr [[R]], align 1
; CHECK-NEXT:    ret void
;
entry:
  store i8 1, ptr %r, align 1
  br i1 %b, label %if.then, label %if.else

if.then:                                          ; preds = %entry
  tail call void @fn_mayread_or_clobber()
  br label %if.end

if.else:                                          ; preds = %entry
  tail call void @fn_readonly()
  br label %if.end

if.end:                                           ; preds = %if.else, %if.then
  store i8 1, ptr %r, align 1
  ret void
}

define void @test4(i1 %b, ptr nocapture %r) {
; CHECK-LABEL: @test4(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    store i8 1, ptr [[R:%.*]], align 1
; CHECK-NEXT:    br i1 [[B:%.*]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK:       if.then:
; CHECK-NEXT:    tail call void @fn_readonly()
; CHECK-NEXT:    br label [[IF_END:%.*]]
; CHECK:       if.else:
; CHECK-NEXT:    tail call void @fn_mayread_or_clobber()
; CHECK-NEXT:    br label [[IF_END]]
; CHECK:       if.end:
; CHECK-NEXT:    store i8 1, ptr [[R]], align 1
; CHECK-NEXT:    ret void
;
entry:
  store i8 1, ptr %r, align 1
  br i1 %b, label %if.then, label %if.else

if.then:                                          ; preds = %entry
  tail call void @fn_readonly()
  br label %if.end

if.else:                                          ; preds = %entry
  tail call void @fn_mayread_or_clobber()
  br label %if.end

if.end:                                           ; preds = %if.else, %if.then
  store i8 1, ptr %r, align 1
  ret void
}

define void @test5(i1 %b, ptr nocapture %r) {
; CHECK-LABEL: @test5(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    store i8 1, ptr [[R:%.*]], align 1
; CHECK-NEXT:    br i1 [[B:%.*]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK:       if.then:
; CHECK-NEXT:    tail call void @fn_readonly()
; CHECK-NEXT:    br label [[IF_END:%.*]]
; CHECK:       if.else:
; CHECK-NEXT:    tail call void @fn_mayread_or_clobber()
; CHECK-NEXT:    br label [[IF_END]]
; CHECK:       if.end:
; CHECK-NEXT:    store i8 1, ptr [[R]], align 1
; CHECK-NEXT:    ret void
;
entry:
  store i8 1, ptr %r, align 1
  br i1 %b, label %if.then, label %if.else

if.then:                                          ; preds = %entry
  tail call void @fn_readonly()
  br label %if.end

if.else:                                          ; preds = %entry
  tail call void @fn_mayread_or_clobber()
  br label %if.end

if.end:                                           ; preds = %if.else, %if.then
  store i8 1, ptr %r, align 1
  ret void
}

declare i1 @cond() readnone

define void @test6(ptr noalias %P) {
; CHECK-LABEL: @test6(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    br label [[FOR_HEADER:%.*]]
; CHECK:       for.header:
; CHECK-NEXT:    store i32 1, ptr [[P:%.*]], align 4
; CHECK-NEXT:    [[C1:%.*]] = call i1 @cond()
; CHECK-NEXT:    br i1 [[C1]], label [[FOR_BODY:%.*]], label [[END:%.*]]
; CHECK:       for.body:
; CHECK-NEXT:    [[LV:%.*]] = load i32, ptr [[P]], align 4
; CHECK-NEXT:    br label [[FOR_HEADER]]
; CHECK:       end:
; CHECK-NEXT:    store i32 3, ptr [[P]], align 4
; CHECK-NEXT:    ret void
;
entry:
  br label %for.header

for.header:
  store i32 1, ptr %P, align 4
  %c1 = call i1 @cond()
  br i1 %c1, label %for.body, label %end

for.body:
  store i32 1, ptr %P, align 4
  %lv = load i32, ptr %P
  br label %for.header

end:
  store i32 3, ptr %P, align 4
  ret void
}

; Make sure the store in %bb3 can be eliminated in the presences of early returns.
define void @test7(ptr noalias %P) {
; CHECK-LABEL: @test7(
; CHECK-NEXT:    store i32 0, ptr [[P:%.*]], align 4
; CHECK-NEXT:    br i1 true, label [[BB1:%.*]], label [[BB2:%.*]]
; CHECK:       bb1:
; CHECK-NEXT:    br label [[BB3:%.*]]
; CHECK:       bb2:
; CHECK-NEXT:    ret void
; CHECK:       bb3:
; CHECK-NEXT:    ret void
;
  store i32 0, ptr %P
  br i1 true, label %bb1, label %bb2
bb1:
  br label %bb3
bb2:
  ret void
bb3:
  store i32 0, ptr %P
  ret void
}

; Make sure the store in %bb3 won't be eliminated because it may be clobbered before.
define void @test8(ptr %P) {
; CHECK-LABEL: @test8(
; CHECK-NEXT:    store i32 0, ptr [[P:%.*]], align 4
; CHECK-NEXT:    br i1 true, label [[BB1:%.*]], label [[BB2:%.*]]
; CHECK:       bb1:
; CHECK-NEXT:    call void @fn_mayread_or_clobber()
; CHECK-NEXT:    br label [[BB3:%.*]]
; CHECK:       bb2:
; CHECK-NEXT:    ret void
; CHECK:       bb3:
; CHECK-NEXT:    store i32 0, ptr [[P]], align 4
; CHECK-NEXT:    ret void
;
  store i32 0, ptr %P
  br i1 true, label %bb1, label %bb2
bb1:
  call void @fn_mayread_or_clobber()
  br label %bb3
bb2:
  ret void
bb3:
  store i32 0, ptr %P
  ret void
}

; Make sure the store in %bb3 will be eliminated because only the early exit path
; may be clobbered.
define void @test9(ptr noalias %P) {
; CHECK-LABEL: @test9(
; CHECK-NEXT:    store i32 0, ptr [[P:%.*]], align 4
; CHECK-NEXT:    br i1 true, label [[BB1:%.*]], label [[BB2:%.*]]
; CHECK:       bb1:
; CHECK-NEXT:    br label [[BB3:%.*]]
; CHECK:       bb2:
; CHECK-NEXT:    call void @fn_mayread_or_clobber()
; CHECK-NEXT:    ret void
; CHECK:       bb3:
; CHECK-NEXT:    ret void
;
  store i32 0, ptr %P
  br i1 true, label %bb1, label %bb2
bb1:
  br label %bb3
bb2:
  call void @fn_mayread_or_clobber()
  ret void
bb3:
  store i32 0, ptr %P
  ret void
}

; The store in bb3 can be eliminated, because the store in bb1 cannot alias it.
define void @test10(ptr noalias %P, ptr %Q, i1 %c) {
; UNOPT-LABEL: @test10(
; UNOPT-NEXT:    store i32 0, ptr [[P:%.*]], align 4
; UNOPT-NEXT:    br i1 [[C:%.*]], label [[BB1:%.*]], label [[BB2:%.*]]
; UNOPT:       bb1:
; UNOPT-NEXT:    store i32 10, ptr [[Q:%.*]], align 4
; UNOPT-NEXT:    br label [[BB3:%.*]]
; UNOPT:       bb2:
; UNOPT-NEXT:    ret void
; UNOPT:       bb3:
; UNOPT-NEXT:    store i32 0, ptr [[P]], align 4
; UNOPT-NEXT:    ret void
;
; OPT-LABEL: @test10(
; OPT-NEXT:    store i32 0, ptr [[P:%.*]], align 4
; OPT-NEXT:    br i1 [[C:%.*]], label [[BB1:%.*]], label [[BB2:%.*]]
; OPT:       bb1:
; OPT-NEXT:    store i32 10, ptr [[Q:%.*]], align 4
; OPT-NEXT:    br label [[BB3:%.*]]
; OPT:       bb2:
; OPT-NEXT:    ret void
; OPT:       bb3:
; OPT-NEXT:    ret void
;
  store i32 0, ptr %P
  br i1 %c, label %bb1, label %bb2

bb1:
  store i32 10, ptr %Q
  br label %bb3

bb2:
  ret void

bb3:
  store i32 0, ptr %P
  ret void
}

define void @test11_smaller_later_store(ptr noalias %P, ptr %Q, i1 %c) {
; CHECK-LABEL: @test11_smaller_later_store(
; CHECK-NEXT:    store i32 0, ptr [[P:%.*]], align 4
; CHECK-NEXT:    br i1 [[C:%.*]], label [[BB1:%.*]], label [[BB2:%.*]]
; CHECK:       bb1:
; CHECK-NEXT:    br label [[BB3:%.*]]
; CHECK:       bb2:
; CHECK-NEXT:    ret void
; CHECK:       bb3:
; CHECK-NEXT:    store i8 0, ptr [[P]], align 1
; CHECK-NEXT:    ret void
;
  store i32 0, ptr %P
  br i1 %c, label %bb1, label %bb2

bb1:
  br label %bb3

bb2:
  ret void

bb3:
  store i8 0, ptr %P
  ret void
}

define void @test11_smaller_earlier_store(ptr noalias %P, ptr %Q, i1 %c) {
; CHECK-LABEL: @test11_smaller_earlier_store(
; CHECK-NEXT:    store i8 0, ptr [[P:%.*]], align 1
; CHECK-NEXT:    br i1 [[C:%.*]], label [[BB1:%.*]], label [[BB2:%.*]]
; CHECK:       bb1:
; CHECK-NEXT:    br label [[BB3:%.*]]
; CHECK:       bb2:
; CHECK-NEXT:    ret void
; CHECK:       bb3:
; CHECK-NEXT:    store i32 0, ptr [[P]], align 4
; CHECK-NEXT:    ret void
;
  store i8 0, ptr %P
  br i1 %c, label %bb1, label %bb2

bb1:
  br label %bb3

bb2:
  ret void

bb3:
  store i32 0, ptr %P
  ret void
}

declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

define void @test12_memset_simple(ptr %ptr) {
; CHECK-LABEL: @test12_memset_simple(
; CHECK-NEXT:    call void @llvm.memset.p0.i64(ptr [[PTR:%.*]], i8 0, i64 10, i1 false)
; CHECK-NEXT:    ret void
;
  call void @llvm.memset.p0.i64(ptr %ptr, i8 0, i64 10, i1 false)
  %ptr.5 = getelementptr i8, ptr %ptr, i64 4
  store i8 0, ptr %ptr.5
  ret void
}

define void @test12_memset_other_store_in_between(ptr %ptr) {
; UNOPT-LABEL: @test12_memset_other_store_in_between(
; UNOPT-NEXT:    call void @llvm.memset.p0.i64(ptr [[PTR:%.*]], i8 0, i64 10, i1 false)
; UNOPT-NEXT:    [[PTR_4:%.*]] = getelementptr i8, ptr [[PTR]], i64 4
; UNOPT-NEXT:    store i8 8, ptr [[PTR_4]], align 1
; UNOPT-NEXT:    [[PTR_5:%.*]] = getelementptr i8, ptr [[PTR]], i64 5
; UNOPT-NEXT:    store i8 0, ptr [[PTR_5]], align 1
; UNOPT-NEXT:    ret void
;
; OPT-LABEL: @test12_memset_other_store_in_between(
; OPT-NEXT:    call void @llvm.memset.p0.i64(ptr [[PTR:%.*]], i8 0, i64 10, i1 false)
; OPT-NEXT:    [[PTR_4:%.*]] = getelementptr i8, ptr [[PTR]], i64 4
; OPT-NEXT:    store i8 8, ptr [[PTR_4]], align 1
; OPT-NEXT:    ret void
;
  call void @llvm.memset.p0.i64(ptr %ptr, i8 0, i64 10, i1 false)
  %ptr.4 = getelementptr i8, ptr %ptr, i64 4
  store i8 8, ptr %ptr.4
  %ptr.5 = getelementptr i8, ptr %ptr, i64 5
  store i8 0, ptr %ptr.5
  ret void
}

declare ptr @__memset_chk(ptr writeonly, i32, i64, i64) argmemonly writeonly nofree nounwind

define void @test12_memset_chk_other_store_in_between(ptr %ptr) {
; CHECK-LABEL: @test12_memset_chk_other_store_in_between(
; CHECK-NEXT:    [[CALL:%.*]] = tail call ptr @__memset_chk(ptr [[PTR:%.*]], i32 0, i64 10, i64 -1)
; CHECK-NEXT:    [[PTR_4:%.*]] = getelementptr i8, ptr [[PTR]], i64 4
; CHECK-NEXT:    store i8 8, ptr [[PTR_4]], align 1
; CHECK-NEXT:    [[PTR_5:%.*]] = getelementptr i8, ptr [[PTR]], i64 5
; CHECK-NEXT:    store i8 0, ptr [[PTR_5]], align 1
; CHECK-NEXT:    ret void
;
  %call = tail call ptr @__memset_chk(ptr %ptr, i32 0, i64 10, i64 -1)
  %ptr.4 = getelementptr i8, ptr %ptr, i64 4
  store i8 8, ptr %ptr.4
  %ptr.5 = getelementptr i8, ptr %ptr, i64 5
  store i8 0, ptr %ptr.5
  ret void
}

declare void @use(ptr)

define void @test12_memset_chk_other_store_in_between_stack_obj_escape_after(i64 %n) {
; CHECK-LABEL: @test12_memset_chk_other_store_in_between_stack_obj_escape_after(
; CHECK-NEXT:    [[OBJ:%.*]] = alloca [200 x i8], align 1
; CHECK-NEXT:    [[CALL:%.*]] = tail call ptr @__memset_chk(ptr [[OBJ]], i32 0, i64 10, i64 [[N:%.*]])
; CHECK-NEXT:    [[PTR_4:%.*]] = getelementptr i8, ptr [[OBJ]], i64 4
; CHECK-NEXT:    store i8 8, ptr [[PTR_4]], align 1
; CHECK-NEXT:    [[PTR_5:%.*]] = getelementptr i8, ptr [[OBJ]], i64 5
; CHECK-NEXT:    store i8 0, ptr [[PTR_5]], align 1
; CHECK-NEXT:    call void @use(ptr [[OBJ]])
; CHECK-NEXT:    ret void
;
  %obj = alloca [200 x i8]
  %call = tail call ptr @__memset_chk(ptr %obj, i32 0, i64 10, i64 %n)
  %ptr.4 = getelementptr i8, ptr %obj, i64 4
  store i8 8, ptr %ptr.4
  %ptr.5 = getelementptr i8, ptr %obj, i64 5
  store i8 0, ptr %ptr.5
  call void @use(ptr %obj)
  ret void
}

define void @test12_memset_chk_other_store_in_between_stack_obj_escape_before(i64 %n) {
; CHECK-LABEL: @test12_memset_chk_other_store_in_between_stack_obj_escape_before(
; CHECK-NEXT:    [[OBJ:%.*]] = alloca [200 x i8], align 1
; CHECK-NEXT:    call void @use(ptr [[OBJ]])
; CHECK-NEXT:    [[CALL:%.*]] = tail call ptr @__memset_chk(ptr [[OBJ]], i32 0, i64 10, i64 [[N:%.*]])
; CHECK-NEXT:    [[PTR_4:%.*]] = getelementptr i8, ptr [[OBJ]], i64 4
; CHECK-NEXT:    store i8 8, ptr [[PTR_4]], align 1
; CHECK-NEXT:    [[PTR_5:%.*]] = getelementptr i8, ptr [[OBJ]], i64 5
; CHECK-NEXT:    store i8 0, ptr [[PTR_5]], align 1
; CHECK-NEXT:    [[PTR_10:%.*]] = getelementptr i8, ptr [[OBJ]], i64 10
; CHECK-NEXT:    store i8 0, ptr [[PTR_10]], align 1
; CHECK-NEXT:    call void @use(ptr [[OBJ]])
; CHECK-NEXT:    ret void
;
  %obj = alloca [200 x i8]
  call void @use(ptr %obj)
  %call = tail call ptr @__memset_chk(ptr %obj, i32 0, i64 10, i64 %n)
  %ptr.4 = getelementptr i8, ptr %obj, i64 4
  store i8 8, ptr %ptr.4
  %ptr.5 = getelementptr i8, ptr %obj, i64 5
  store i8 0, ptr %ptr.5
  %ptr.10 = getelementptr i8, ptr %obj, i64 10
  store i8 0, ptr %ptr.10
  call void @use(ptr %obj)
  ret void
}

define void @test12_memset_other_store_in_between_partial_overlap(ptr %ptr) {
; CHECK-LABEL: @test12_memset_other_store_in_between_partial_overlap(
; CHECK-NEXT:    call void @llvm.memset.p0.i64(ptr [[PTR:%.*]], i8 0, i64 10, i1 false)
; CHECK-NEXT:    [[PTR_4:%.*]] = getelementptr i8, ptr [[PTR]], i64 4
; CHECK-NEXT:    store i16 8, ptr [[PTR_4]], align 2
; CHECK-NEXT:    [[PTR_5:%.*]] = getelementptr i8, ptr [[PTR]], i64 5
; CHECK-NEXT:    store i16 0, ptr [[PTR_5]], align 2
; CHECK-NEXT:    ret void
;
  call void @llvm.memset.p0.i64(ptr %ptr, i8 0, i64 10, i1 false)
  %ptr.4 = getelementptr i8, ptr %ptr, i64 4
  store i16 8, ptr %ptr.4
  %ptr.5 = getelementptr i8, ptr %ptr, i64 5
  store i16 0, ptr %ptr.5
  ret void
}

define void @test12_memset_later_store_exceeds_memset(ptr %ptr) {
; CHECK-LABEL: @test12_memset_later_store_exceeds_memset(
; CHECK-NEXT:    call void @llvm.memset.p0.i64(ptr align 1 [[PTR:%.*]], i8 0, i64 8, i1 false)
; CHECK-NEXT:    [[PTR_4:%.*]] = getelementptr i8, ptr [[PTR]], i64 4
; CHECK-NEXT:    store i8 8, ptr [[PTR_4]], align 1
; CHECK-NEXT:    [[PTR_5:%.*]] = getelementptr i8, ptr [[PTR]], i64 8
; CHECK-NEXT:    store i64 0, ptr [[PTR_5]], align 8
; CHECK-NEXT:    ret void
;
  call void @llvm.memset.p0.i64(ptr %ptr, i8 0, i64 10, i1 false)
  %ptr.4 = getelementptr i8, ptr %ptr, i64 4
  store i8 8, ptr %ptr.4
  %ptr.5 = getelementptr i8, ptr %ptr, i64 8
  store i64 0, ptr %ptr.5
  ret void
}

define void @test12_memset_later_store_before_memset(ptr %ptr) {
; CHECK-LABEL: @test12_memset_later_store_before_memset(
; CHECK-NEXT:    [[PTR_1:%.*]] = getelementptr i8, ptr [[PTR:%.*]], i64 1
; CHECK-NEXT:    [[TMP1:%.*]] = getelementptr inbounds i8, ptr [[PTR_1]], i64 7
; CHECK-NEXT:    call void @llvm.memset.p0.i64(ptr align 1 [[TMP1]], i8 0, i64 3, i1 false)
; CHECK-NEXT:    store i64 0, ptr [[PTR]], align 8
; CHECK-NEXT:    ret void
;
  %ptr.1 = getelementptr i8, ptr %ptr, i64 1
  call void @llvm.memset.p0.i64(ptr %ptr.1, i8 0, i64 10, i1 false)
  %ptr.4 = getelementptr i8, ptr %ptr, i64 4
  store i8 8, ptr %ptr.4
  store i64 0, ptr %ptr
  ret void
}

; The memset will be shortened and the store will not be redundant afterwards.
; It cannot be eliminated.
define void @test13_memset_shortened(ptr %ptr) {
; CHECK-LABEL: @test13_memset_shortened(
; CHECK-NEXT:    [[TMP1:%.*]] = getelementptr inbounds i8, ptr [[PTR:%.*]], i64 8
; CHECK-NEXT:    call void @llvm.memset.p0.i64(ptr align 1 [[TMP1]], i8 0, i64 16, i1 false)
; CHECK-NEXT:    store i64 0, ptr [[PTR]], align 8
; CHECK-NEXT:    ret void
;
  call void @llvm.memset.p0.i64(ptr %ptr, i8 0, i64 24, i1 false)
  store i64 0, ptr %ptr
  ret void
}

declare ptr @strcat(ptr, ptr) nounwind argmemonly

define void @test14_strcat(ptr noalias %P, ptr noalias %Q) {
; CHECK-LABEL: @test14_strcat(
; CHECK-NEXT:    [[CALL1:%.*]] = call ptr @strcat(ptr [[P:%.*]], ptr [[Q:%.*]])
; CHECK-NEXT:    [[CALL2:%.*]] = call ptr @strcat(ptr [[P]], ptr [[Q]])
; CHECK-NEXT:    ret void
;
  %call1 = call ptr @strcat(ptr %P, ptr %Q)
  ; FIXME: Eliminate the second strcat as a "store of existing value" for this particular case, where both strcat's are identical (same source, not just same dest).
  %call2 = call ptr @strcat(ptr %P, ptr %Q)
  ret void
}

define void @pr49927(ptr %q, ptr %p) {
; CHECK-LABEL: @pr49927(
; CHECK-NEXT:    [[V:%.*]] = load i32, ptr [[P:%.*]], align 4
; CHECK-NEXT:    store i32 [[V]], ptr [[Q:%.*]], align 4
; CHECK-NEXT:    store i32 [[V]], ptr [[P]], align 4
; CHECK-NEXT:    ret void
;
  %v = load i32, ptr %p, align 4
  store i32 %v, ptr %q, align 4
  store i32 %v, ptr %p, align 4
  ret void
}

define void @pr50339(ptr nocapture readonly %0) {
; CHECK-LABEL: @pr50339(
; CHECK-NEXT:    tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) @a, ptr noundef nonnull align 1 dereferenceable(16) [[TMP0:%.*]], i64 16, i1 false)
; CHECK-NEXT:    tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) @a, ptr noundef nonnull align 1 dereferenceable(16) [[TMP0]], i64 16, i1 false)
; CHECK-NEXT:    ret void
;
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) @a, ptr noundef nonnull align 1 dereferenceable(16) %0, i64 16, i1 false)
  ; FIXME: Eliminate the second memcpy as a "store of existing value" for this particular case, where both memcpy's are identical (same source, not just same dest).
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) @a, ptr noundef nonnull align 1 dereferenceable(16) %0, i64 16, i1 false)
  ret void
}

; Cannot remove the second memcpy as redundant store, because %src is modified
; in between.
define i8 @memset_optimized_access(ptr noalias %dst, ptr noalias %src) {
; CHECK-LABEL: @memset_optimized_access(
; CHECK-NEXT:    tail call void @llvm.memcpy.p0.p0.i64(ptr [[DST:%.*]], ptr [[SRC:%.*]], i64 16, i1 false)
; CHECK-NEXT:    store i8 99, ptr [[SRC]], align 1
; CHECK-NEXT:    [[L:%.*]] = load i8, ptr [[DST]], align 1
; CHECK-NEXT:    tail call void @llvm.memcpy.p0.p0.i64(ptr [[DST]], ptr [[SRC]], i64 16, i1 false)
; CHECK-NEXT:    ret i8 [[L]]
;
  tail call void @llvm.memcpy.p0.p0.i64(ptr %dst, ptr %src, i64 16, i1 false)
  store i8 99, ptr %src
  %l = load i8, ptr %dst
  tail call void @llvm.memcpy.p0.p0.i64(ptr %dst, ptr %src, i64 16, i1 false)
  ret i8 %l
}

; The @use() call is a later non-removable store, but should not affect the
; removal of the store in the if block.
define void @later_non_removable_store(i1 %c, ptr %p) {
; CHECK-LABEL: @later_non_removable_store(
; CHECK-NEXT:    store i8 1, ptr [[P:%.*]], align 1
; CHECK-NEXT:    br i1 [[C:%.*]], label [[IF:%.*]], label [[EXIT:%.*]]
; CHECK:       if:
; CHECK-NEXT:    br label [[EXIT]]
; CHECK:       exit:
; CHECK-NEXT:    call void @use(ptr [[P]]) #[[ATTR6:[0-9]+]]
; CHECK-NEXT:    ret void
;
  store i8 1, ptr %p
  br i1 %c, label %if, label %exit

if:
  store i8 1, ptr %p
  br label %exit

exit:
  call void @use(ptr %p) argmemonly
  ret void
}