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

    <tr>
        <th>Summary</th>
        <td>
            [InstCombine] Unsound transform introducing `fshr`: Value mismatch
        </td>
    </tr>

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

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

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

<pre>
    Running `llvm/test/Transforms/PhaseOrdering/rotate.ll` through Alive2 (with `opt-alive.sh`) shows that InstCombine performs an unsound transformation, after which there is a value mismatch

Reproducer IR for just running Instcombine:

```
define i32 @rotl(i32 %a, i32 %b) {
entry:
 %cmp = icmp eq i32 %b, 0
  %sub = sub i32 32, %b
  %shr = lshr i32 %a, %sub
  %shl = shl i32 %a, %b
  %or = or i32 %shr, %shl
  %cond = select i1 %cmp, i32 %a, i32 %or
  ret i32 %cond
}
```


Alive2 Report:

```
define i32 @rotl(i32 %a, i32 %b) {
%entry:
  %cmp = icmp eq i32 %b, 0
  %sub = sub i32 32, %b
  %shr = lshr i32 %a, %sub
  %shl = shl i32 %a, %b
 %or = or i32 %shr, %shl
  %cond = select i1 %cmp, i32 %a, i32 %or
  ret i32 %cond
}
=>
define i32 @rotl(i32 %a, i32 %b) {
%entry:
 %cond = fshr i32 %a, i32 %a, i32 %b
  ret i32 %cond
}
Transformation doesn't verify! (unsound)
ERROR: Value mismatch

Example:
i32 %a = #x40000000 (1073741824)
i32 %b = #x00000011 (17)

Source:
i1 %cmp = #x0 (0)
i32 %sub = #x0000000f (15)
i32 %shr = #x00008000 (32768)
i32 %shl = #x00000000 (0)
i32 %or = #x00008000 (32768)
i32 %cond = #x00008000 (32768)

Target:
i32 %cond = #x00002000 (8192)
Source value: #x00008000 (32768)
Target value: #x00002000 (8192)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email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