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

    <tr>
        <th>Summary</th>
        <td>
            [x86] Using fesetround to set rounding mode in loop, it will cause wrong orders of float calculate IR in some opt passes 
        </td>
    </tr>

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

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

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

<pre>
    clang-version: 13

I want to set different rounding in a loop to calculate a temp result. (below example)

`clang++ test.cpp -c -O2 -S -mllvm -print-after-all -o test.s &> print`

```
#include <fenv.h>
unsigned round_modes[4] = {FE_UPWARD, FE_TOWARDZERO, FE_DOWNWARD, FE_TONEAREST};
void test(float* m_out, float* rhs, float* lhs) {
  float binary_ret1[2] = {0.0};
 fesetround(FE_UPWARD);
 // part 1: float calc 
  binary_ret1[0] = (m_out[0] * rhs[0]);
  binary_ret1[1] = (m_out[1] * rhs[1]);

 // part 2: float calc
  for (int i = 0; i < 3; ++i) {
 fesetround(round_modes[i]);
    binary_ret1[0] += binary_ret1[0];
 }
 // part 3: float calc
  fesetround(FE_DOWNWARD);
  float binary_ret2[2];
  binary_ret2[0] = (m_out[0] * rhs[0]) + binary_ret1[0];
  binary_ret2[1] = (m_out[1] * rhs[1]) + binary_ret1[1];
  m_out[0] = (binary_ret2[0] + lhs[0]);
  m_out[1] = (binary_ret2[1] + lhs[1]);
}
```

After InstCombine pass:
part 1 : `binary_ret1[1] = (m_out[1] * rhs[1]);` will be removed to part3, which rounding mode is changed to FE_TONEAREST from FE_UPWARD.

![image](https://github.com/llvm/llvm-project/assets/31645239/d5a42999-e731-4e20-b8f8-a73261b4dc76)

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVV1v4jgU_TXm5SrIsZNAHniAAlJfpqvOjEbal8pJLsQrx45sp7T_fmWHFkL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