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

    <tr>
        <th>Summary</th>
        <td>
            InstCombine: incorrect sink of FP math through select changes NaN value
        </td>
    </tr>

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

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

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

<pre>
    This optimization changes the NaN payload. It requires the `nnan` flag.

```llvm
; test/Transforms/InstCombine/select-binop-foldable-floating-point.ll

define float @select_fadd(i1 %cond, float %A, float %B) {
  %C = fadd float %A, %B
  %D = select i1 %cond, float %C, float %A
  ret float %D
}
=>
define float @select_fadd(i1 %cond, float %A, float %B) {
  %C = select i1 %cond, float %B, float -0.000000
  %D = fadd float %C, %A
  ret float %D
}
Transformation doesn't verify!

ERROR: Value mismatch

Example:
i1 %cond = #x0 (0)
float %A = #x7f810000 (SNaN)
float %B = #x00000000 (+0.0)

Source:
float %C = #x7fc00000 (QNaN)
float %D = #x7f810000 (SNaN)

Target:
float %C = #x80000000 (-0.0)
float %D = #x7fc00000 (QNaN)
Source value: #x7f810000 (SNaN)
Target value: #x7fc00000 (QNaN)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0VE2TozYQ_TXNRWVKbtnGHDjYZl21l0kyu5VrSkADSoREJDG1k1-f4sODZ3ZnkstSLozEU7_XT-JJ71VjiDLYn2GfR3IIrXWZGYztte3JR4WtnrOvrfLM9kF16h8ZlDWsbKVpyLPQEnuQD6yXz9rKKmafA3P096Dc8hIO3Bhp4MBZrWUTA8-Bn5b7gc8_rZ-6ZUqcWSAfAK9fnTS-tq7zgNfPxoeL7QplCPDqSVMZNoUytt_UVley0LSptZVBmWbTW2VCrPU9V0W1MsQmDIMdn0v8UcuqAjyqLQPcl9ZUgJcbCPenV6MzYMogOc8F2Th1YSByNhZ5s2iCr7h8ws2c7B2yyxvqZbWjsM7mS0vJ7UHkID79_A4_Vn5eRxse8-n6rvnXJl0Wk_5fmy9HYT58lSVvAJPAnsip-hlwe7_Vnx4ff3kEcWK_Sz0Q65TvZCjbV5Bvsus1gVjGa2OTVkDxjTPAIwdMZ8Tq2AsiqY_bsdUR-OVBPnyPPa_VlmvEAp55vFae71_s4MpV0GrUHV35UuK3H9Ll_y1t8VO6hsJHZMc7vZt7tT8ke0fY3BN7Grdh3I8Plc2a3oLfqfySHFGViSoVqYwo2yZ8eziKNOFRm9VpVVFaIBKmMq3FMdliStXhUEhO-0MZqQw5ii3yHU_3qeDxrqgE0SFNBW0r5AXsOHVS6XhMp9i6JlLeD5QlO0yTSMuCtJ9yE7FTvrRdr_R0PgER8AKIU6yJkzI-lLfsWl69zqpxep9HLhtXbIqh8bDjWvngV_KggqbsPgfFiSlTWufGT9Mr8xezNbv-yjoZWhZaZ4emvX25t7wes3qyOBqcztoQej8eA7wCXhsV2qGIS9sBXift89-md_ZPKsdMnhwY83gy4d8AAAD__2pSxtw">