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

    <tr>
        <th>Summary</th>
        <td>
            Reimplement basic optimizations with fenv_access
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

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

<pre>
    Hello,
first, thanks a lot for implementing fenv_access!
While experimenting with it, I noticed that it still seems to aim for correctness only. For instance,
```c++
double f(double a){return a/2;}
```
generates with "Debian clang version 16.0.0-++20220928062542+48b8dee773f3-1~exp1" and options `-O3 -frounding-math`
```
        movl    $2, %eax
        cvtsi2sd        %eax, %xmm1
        divsd   %xmm1, %xmm0
```
Since we use correctly rounded arithmetic for speed, this kind of code pessimization makes it less useful.
The IR looks like
```
  %2 = tail call double @llvm.experimental.constrained.sitofp.f64.i32(i32 2, metadata !"round.dynamic", metadata !"fpexcept.ignore") #5
  %3 = tail call double @llvm.experimental.constrained.fdiv.f64(double %0, double %2, metadata !"round.dynamic", metadata !"fpexcept.ignore") #5
```
Converting the constant 2 to `double` can be done at compile time, this is exact and the rounding mode has no impact. And replacing `x/2.` with `x*.5` is also safe whatever the current rounding mode.
Again, I understand giving correctness the priority, I just hope that some optimization can still happen.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy1VE2PozgQ_TXOpRRETELCgUO6M62Z00izK-1xZaAAT_sD2SaT7K_fsknSva0-rTSSBdh-VL2qelWN7a71V1TKMv7M8hPLj710PtAOwijMqwcBygborQOpJ4UaTZBmgB7N-W_Rtug945vl179GqRDwMqGTd9wvGUaQyeA3MDbIFrtoOtAh-CCVAo-oPQQLQurkqLXOYRsM2QZr1DWDl-je-CBMiw-mrMyX1TL-FFc67ezcEIue8cPtUzBesf2TwzA7E3cvnBVPbH_6YGbZDmjQiYB-oc44P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