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

    <tr>
        <th>Summary</th>
        <td>
            [InstCombine] miscompile of a signed division into a logical shift right
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    The following function:

```llvm
define i32 @f(i32 %h) {
0:
  %sd = sdiv i32 %h, 2
  %t = icmp sgt i32 %sd, 1
  %sd.i = sdiv i32 %sd, 2
  %t.i = icmp sgt i32 %sd.i, 1
  %r.i = select i1 %t.i, i32 %sd.i, i32 1
  %r = select i1 %t, i32 %sd, i32 %r.i
  ret i32 %r
}
```

is miscompiled by opt on -O2 and above. The optimized test case has its signed division replaced with a logical shift right. If `%h` is undef, then the value of `%t` can differ across executions due to potential differences in the signed bit of `%sd`. This consequently leads to different results from the select instruction.

Here is the Alive2 output: https://alive2.llvm.org/ce/z/XW_PC8

>From inspecting the opt debug dumps, it looks like the sequence of optimizations that causes this is:
InstCombine -> CorrelatedValuePropagation -> InstCombine
Where CorrelatedValuePropagation replaces the `sdiv` with `udiv`, and then InstCombine replaces `udiv` with `lshr`.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8VE1v4zYQ_TX0ZRCBpvx50GE3qdE9dQ9Ft7eCIkfSdClS5ZBOs7--oGQ7TrIoYMii-d4zZ94bambqPWIjtp_F9mmlcxpCbDCScZTzqg32pfl9QOiCc-GZfA9d9iZR8KL-JOSTkNfnTi4f587j8pPFjjwC1QrERnZCHeZXtR2EOoLYf15g8qYEZZMtiPoJ2NIZXvGPoO4waYaQGSfgPl1hbAtu_Uarog9qC-yN3AX1UbCi95LxqogOTQJaXwQK7h2tLN9Qf0J8Q7tbxIquzIi3A8VLr_dP75p-7wQxjM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