<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/70578>70578</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [MemCpyOpt] The store instruction should not be removed by DSE. 
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            miscompilation,
            llvm:optimizations
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            DianQK
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          DianQK
      </td>
    </tr>
</table>

<pre>
    This IR is reduced from https://github.com/rust-lang/rust/issues/116976.

```llvm
declare void @f_byval(ptr byval(i32))
declare void @llvm.memcpy.p0.p0.i64(ptr, ptr, i64, i1)

define void @byval_param_noalias_metadata(ptr align 4 byval(i32) %ptr) {
  %tmp = alloca i32, align 4
  store i32 1, ptr %ptr, !noalias !2
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %tmp, ptr align 4 %ptr, i64 4, i1 false)
  call void @f_byval(ptr align 4 byval(i32) %tmp), !alias.scope !2
  ret void
}

!0 = !{!0}
!1 = !{!1, !0}
!2 = !{!1}
```

The DSE removed the store instruction because MemCpyOpt did not update the alias information.

```llvm
declare void @f_byval(ptr byval(i32))

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

define void @byval_param_noalias_metadata(ptr byval(i32) align 4 %ptr) {
  call void @f_byval(ptr byval(i32) align 4 %ptr), !alias.scope !0
  ret void
}

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!0 = !{!1}
!1 = distinct !{!1, !2}
!2 = distinct !{!2}
```

godbolt: https://llvm.godbolt.org/z/qMncEEseK

Similar to #67539.


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0VU1v4zYQ_TX0ZRBDGlm2ddAhsWOgWCyKdnMPKHJss-WHSlIJvL--ECU5sZ1mtygKCJLAGb754HscHoI6WKKal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