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

    <tr>
        <th>Summary</th>
        <td>
            Missed optimization: failure to use div-by-zero UB to delete redundant select
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

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

<pre>
    Given this function:

```ll
define i64 @incorrect_safe_div(i64 %n, i64 %d) {
  %1 = icmp eq i64 %d, 0
  %2 = udiv i64 %n, %d
  %3 = select i1 %1, i64 -1, i64 %2
  ret i64 %3
}
```

LLVM does not seem to notice that this `select` is unconditional, because `%2` should imply that `%d != 0` in the entire block, and therefore `%1` can be replaced with `false`. Alive2 confirms this optimization is valid: https://alive2.llvm.org/ce/z/7nLDRe.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJxcUkGPozwM_TXhYrUKgUI5cJiZiu8yc_mk3esoJKZ4NyTdJLDq_PpVoN3prFSVyH5-9nu2DIHOFrFlh2d2OGVyjqPz7aSuWe_0tf2PFrQQRwowzFZFcpYVT4yfGL__V3z7GbMFNA5kEagqgZWcrHLeo4rvQQ74rmlh4rjmxMEy8QK3t2aiAVY_bxyQQjmw4gSkpgvgrwfcC_AHlFhRs6YFHmlX6CeqWFEBDaoIlK_09-67_GEOcS_yGO-x4qa0Pv0j-dGH19fvb6AdBrAuQkCcILr0JoUQRxk3F1nFtylYxYECzFY5qykZK02ao0cl54AJuI5TcQijm40Gmi7mulFtSQ1M5EkXX8nSnhDQRvIIvXHqZ-KTVqe4x8H5O2ue8Epa6BE8XoxUqOE3xTHlB2kCsorv4cnQggKUswP5KWwC3CXSRB8yTZwELNKQZsUTjDFeQjoO0THRybV2b8wy7Z0_M9EpZKL7YKKr7evpf9xnui10UzQywzavGl6XXFQiG9uDbvK6qodh6POyz6vDUUnVHIdmOJaylzyjVnBR8Lrg-ZGXQuz7w9DnAg8lNn1dVJqVHCdJ5m_7jEKYsa1KUfLMyB5NuF-8bxNo18_nwEpuKMTwWRYpGmzfKATUX5QnwYMkM3tMa04L07Ts-uvuA72Db88pqtFgTA7r2Wpp4-3-stmb9qtbZ4rj3O-Vm5joUvfbZ3fx7kc6FtGtCgIT3SriTwAAAP__QM4dZQ">