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

    <tr>
        <th>Summary</th>
        <td>
            Folding select into operand for "fadd", "fmul", "fsub", "fdiv" not safe for precise math (?)
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          michele-scandale
      </td>
    </tr>
</table>

<pre>
    With 9cd7c534e27c2558ef16e14d4440bd838320334b InstCombine is able to fold select into binary floating point operation operands. For example the following code:
```
 %C = fadd %A, %B
 %D = select %cond, %C, %A
```
is transformed into:
```
%C = select %cond, %B, -0.000000e+00
%D = fadd %A, %C
```

>From the commit message there were Alive2 verification links -- which at the time were timing out:
* FAdd - https://alive2.llvm.org/ce/z/eUxN4Y
* FMul - https://alive2.llvm.org/ce/z/5SWZz4
* FSub - https://alive2.llvm.org/ce/z/Dhj8dU
* FDiv - https://alive2.llvm.org/ce/z/Yj_NA2

However today they report the transformation seems not semantic preserving in the cases of NaNs -- e.g.
```

----------------------------------------
define half @src(i1 %cond, half %A, half %B) {
#0:
  %C = fsub half %A, %B
  %D = select i1 %cond, half %C, half %A
  ret half %D
}
=>
define half @tgt(i1 %cond, half %A, half %B) {
#0:
  %C = select i1 %cond, half %B, half 0x0000
  %D = fsub half %A, %C
  ret half %D
}
Transformation doesn't verify!

ERROR: Value mismatch

Example:
i1 %cond = #x0 (0)
half %A = #xfc80 (SNaN)
half %B = #x0000 (+0.0)

Source:
half %C = #x7e00 (QNaN)
half %D = #xfc80 (SNaN)

Target:
half %C = #x0000 (+0.0)
half %D = #x7c80 (SNaN)
Source value: #xfc80 (SNaN)
Target value: #x7c80 (SNaN)
```

The counter example reported above suggests that the transformation is not sound w.r.t. NaNs.
It is not clear to me if according to https://llvm.org/docs/LangRef.html#behavior-of-floating-point-nan-values `select` is considered a floating point operation or not, hence I wonder if the transformation should be allowed only when `nnan` is being on the `select` instruction.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVltv27gS_jX0y8ACTckXPfjBlxqnwDk52KTdovuyoMiRxS5FGiRlJ_31C1KKLxs3aIE1AkmM5vLNp5mP5N6rvUFckumaTLcj3oXGumWrRIMax15wI7nGUWXly_KLCg2UQs7FNC-QzQWbThdYT2Y4KWRRFLSSi3yRM5rnRQUfjQ8b21bKICgPvNIIwUJttQSPGkUAZYKFShnuXqDWlgdl9nCwygSwB3Q8KGv6JyN9BjvrAJ95e4iRGoyhtD1FH2ElknxF6JbQFZnR4S8tgbDpBki-hZpLGVcrwjbxvr4YbJPBAIuwqbBGDlab4X4_uvIQHDe-tq5FmSr6EZAzjrtp1vE-phlNPyRsTS9-27v4N_fTpOvO2TaRJGzbqgAtes_3iTeHcIqXlVZHZHBEp2olerK1Mn95GI_h1CjRAA8pRlDt4BNUG_m2XbhUyVawW0kJY2hCOPj4gu0I2_EUP9P62GbW7QnbCSRs952wHX5-fii-Xvn_r9O_4j99-vLH9-LK_6mrfsV_23xbyM9X_lt1_BX_r9_-fFixa77_Y094RAfBSv4SSXsBhwfrBgJfW6Rn2SO2HowN4LHlJigBB4ce3TGSq0z_4bhHD7aGB_6QPglm--ydLz7-yV9vLbGOg9lwXQMpqHeCsIWaXDdl_25ot9fFmrASyHz9yl1Oz40AV5Pmu-rW_2rc3szb_bSbWwyDr8Nw_ud2ADF_fci3JP9wt76wD_9mfe8CX58X9DnO8puy77Kz-ZkKP932kbToDWHz0A_xC2GT64748Pj4_0eSr-B3rjuEVvmWB9HcmPRyeq7xUlFCSlj-TIGwBSWs7C3OuM8GtVgkm6cH_vDGbH2JQ2kyi8qWXeL11yfbOXGBce6Bs_cce-_f7iXZvo9l4I67PYZ3UvwA4Nsk83tJ-grgGKmOnP8QTY_j1vBuxLtT_ilJemcCXvbCXmhQAq_sEcF3-z364CE0_K78qEF7bGcknDKXhSyJzK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