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

    <tr>
        <th>Summary</th>
        <td>
            likely incorrect optimization of sitofp/fptosi roundtrip
        </td>
    </tr>

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

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

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

<pre>
    here's a test case that lives in llvm/test/Transforms/InstCombine/sitofp.ll:
```
; This can fold because the 54-bit output is signed and we discard the sign bit.

define i54 @test18(i64 %A) {
; CHECK-LABEL: @test18(
; CHECK-NEXT:    [[C:%.*]] = trunc i64 [[A:%.*]] to i54
; CHECK-NEXT:    ret i54 [[C]]
;
  %B = sitofp i64 %A to double
  %C = fptosi double %B to i54
  ret i54 %C
}
```

it is also performed by the x64 and arm64 backends (https://godbolt.org/z/fT15zMncK). since it is capable of changing the value that is returned, we believe it to be incorrect in the absence of fast math flags.

the alive2 compiler explorer instance times out on it, but this is the output:

```
regehr@john-home:~$ ~/alive2/build/alive-tv --smt-to=0 foo.ll

----------------------------------------
define i54 @test18(i64 %A) {
%0:
  %B = sitofp i64 %A to double, exceptions=ignore
  %C = fptosi double %B to i54, exceptions=ignore
  ret i54 %C
}
=>
define i54 @test18(i64 %A) {
%0:
  %C = trunc i64 %A to i54
  ret i54 %C
}
Transformation doesn't verify!

ERROR: Value mismatch

Example:
i64 %A = #xffdfffffffffffff (18437736874454810623, -9007199254740993)

Source:
double %B = #xc340000000000000 (-9007199254740992)
i54 %C = #x20000000000000 (9007199254740992, -9007199254740992)

Target:
i54 %C = #x1fffffffffffff (9007199254740991)
Source value: #x20000000000000 (9007199254740992, -9007199254740992)
Target value: #x1fffffffffffff (9007199254740991)

Summary:
  0 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors
regehr@john-home:~$ 
```

cc @aqjune @nunoplopes @ryan-berger @nbushehri @zhengyang92 

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJytVluPozYU_jXkxSICA0l4yENuo1a7baVpVPXVGAOeNTa1TTqZX99jkwtJdkaz1SIn2Pj4nO_cXajyuGyYZgGeG0SQZcYiSgxDtiEWCX5gBnGJhDi0AX5y2_DaayJNpXRrYPGrNHaj2oJLYPJkuFVVNxUiSFZBtA2iVTCLTmNYJmu0b7gBKRJVSpSoYJT0XiJDWRoW3CLV2663CKgMryUrEZEl-pehkhtKdOlJ3Q4C4umJr_8vWQU4EM9SFKSRgxsvArzgM1jjbBXgHAXz9RXJ5pfd5kv4dbXefQXAN2fuiX7f_b13NPAE2RrGxqmIs2mAV0G2hYGCZIus7iVFXqCnWj1QWeXwfcBeMztocBLjj13ohwly-qy9xMHk6Kyj41-qvhBsRLnxlFVnleGn3YHBGMxIMpw4CZxvv-9G_8-9j4gwCnVMu4gAXxVH759XwOPcRnQLs4LQb0yWBlgvGms7483yBKNWZaGEnSpdw-oNftU-zt5-k_QLuGsK6kkKHvWSKOmIg64qRBsiay5rL-tARH8KWaACLXoNURPgjQuaggnODp4FaFvARFKlNaPWRbY7TgrDnBBgWxFIgJbYBlWC1OYmuDypSwmMqGo7LphG7LUTSsOEQxoQx8TyFnIGIhgpCTIdiAIW1sU8DMdkCO9rhnzPwJrVrNEQkC-qkWGjWubo57sAg3vc62mAApOi56I8fwjtAYWhaW1oFfg8ghxTLh1HksJPPv8jpXAWXdT6VIiCddgrZZ3lSkJMbCGpwZw_ELkfM_gooJNtkOx-ho6b-8Q_qfi5xLoUU-JUABWZkVCNLTowzatjgOOx73bPz388uzrxlw_5lhs4R5sbklfSdoJdMF4gOZQBTl6rqqzGj8vJeJEm83kyW8zTNEsXcTTDibNtmEfRPM5znKXzNMpz-JiPhf2pek2vssYOOoujSRqNHyfuni2-sD1b6XIcPxx-PPuIE9_h3BNds2vKPUiJHyxyxzC-MBxUHm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