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

    <tr>
        <th>Summary</th>
        <td>
            [X86] Possible wrong compilation of scalar comparison into cmpnltpd
        </td>
    </tr>

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

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

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

<pre>
    Hello,

I experienced a bug in code generation for the x86-64 target. For the minimal test case
[test.txt](https://github.com/llvm/llvm-project/files/11885949/test.txt)
compiled on
```
$ clang++-16 -v
Ubuntu clang version 16.0.0 (1~exp5ubuntu3)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/13
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/13
Candidate multilib: .;@m64
Selected multilib: .;@m64
```
with
```
 clang++-16 -Og -S test.cc
```
I get the assembly code
```
f(double, double, unsigned int, bool):                               # @f(double, double, unsigned int, bool)
        test    esi, esi
        jne .LBB0_5
        mov     eax, edi
        cvtsi2sd        xmm2, rax
 mulsd   xmm2, xmm1
        addsd   xmm1, xmm2
        movapd xmm3, xmm0
        cmpnltpd        xmm3, xmm2
        cmpnltpd xmm1, xmm0
        ....
```
see also https://godbolt.org/z/s3v3rdfsd
Prior content in the register `xmm2` from before entering this function in the upper lane can trigger a floating point exception in the second to last line for `cmpnltpd xmm3, xmm2`. Specifically, I see
```
(gdb) p $xmm2
$1 = {v2_double = {0.40000000000000002, nan(0xc000000000000)}}
```
showing that the upper lane contains an invalid entry. The generated code does not give the FPE with clang-15, nor does it with the optimization level `-O0`. Using `-O2`, `-O3` also leads to the invalid code according to godbolt both for clang-15 and clang-16.

Please let me know if I should provide a main function to invoke this. All one needs to do is to set `xmm2` to `_mm_set1_pd(std::numeric_limits<float>::signaling_NaN());` and call `feenableexcept(FE_DIVBYZERO | FE_INVALID);` before calling `f(0.2, 0.2, 2, false);`. I could be wrong and something might be disallowed by my code, but I believe this is valid code and wrong within LLVM.

Note that the code is extracted from a big project, https://github.com/dealii/dealii/issues/15496#issuecomment-1609945214
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0Vl-P4ygS_zTkpRTLxo7bechDJ2nfRZqbGd3sju7uJcJQdtjBYAFOp_fhPvsJ7E7SPa2VVrq1othQfyh-9aMo5pzsNOKGrLZktV-w0Z-M3fywRjeSnxTaRWPEy-bvqJQhdEfSPUkfp_8D4GVAK1FzFMCgGTuQGrgRCB1qtMxLo6E1FvwJ4VKVy7IAz2yHPoF6nu6llj1T4NF54MzhvMRqG2YSf_FktSe0Onk_OJI_EloTWnfSn8Ym4aYntFbq_PpaDtb8htwTWrdSoSO0zrKqWq2LNaH11SNdT6tw0w9SoQCj52XLdP5NQ1oAV0x3hG4J3S6zEpbnSfRrM2o_TlI4o3Vhr1mZpEkKhFbZf_EyrMaolF_X-yVunuSPAY1jWSwHvlRSj5dlp8dZ5WSRCeiNQBUUB-PkZQZcO8-UQrGXNogIrUdnCa0bOYdfm1EL4EwLKZhH-NtuB3Kyisn4yYrWSRKgk00AlXNC6zmyW1gBwr_WPf1r3eeT-2-okHsU_2-_u2vA_ai8DFb5IyQk35Ii7cvi3ep_pPSOfs_Snz4U_ETKLx0sv8UzlHD-ockBOvTxxDHnsG_USzypH-q2hFbCjI1CQndw-xp1rBYCpPZh3BijArfzR_jjh9AcSJH-Wb_TXucnFggAQCeDTni9kf-mEZJP2216XL0V9OYc38gu0VC8M-Rn7yR14nV86XsaFC2bz11IWRS_Si59n731wYR41chmDfpTFGwQQZDPCum7MPpBKz_ch5F_7OqqebfaO2dJkiQfptYhAlPOw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