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

    <tr>
        <th>Summary</th>
        <td>
            [InstCombine][X86] LLVM >= 18 folding bitcast -> and -> bitcast to `@llvm.fabs.f16` generates call to `__extendhfsf2`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            regression,
            llvm:instcombine
      </td>
    </tr>

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

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

<pre>
    https://godbolt.org/z/on6Pj6Tsc

Starting from https://github.com/llvm/llvm-project/commit/5c0da5839de1bdc08f411a04305a9bdadf538ad5, InstCombine transforms this IR:

```llvm
define linkonce_odr hidden noundef half @_Float16 __llvm_libc_20_0_0_git::fputil::abs<_Float16, 0>(_Float16)(half noundef %x) local_unnamed_addr #1 comdat {
entry:
 %0 = bitcast half %x to i16
  %1 = and i16 %0, 32767
  %2 = bitcast i16 %1 to half
  ret half %2
}
```

into:

```llvm
define linkonce_odr hidden noundef half @_Float16 __llvm_libc_20_0_0_git::fputil::abs<_Float16, 0>(_Float16)(half noundef %x) local_unnamed_addr #1 comdat {
entry:
  %0 = call half @llvm.fabs.f16(half %x)
  ret half %0
}
```

On x86, when AVX-512 FP16 is not available, this generates something like:

```asm
.LCPI0_0:
        .long 0x7fffffff
__llvm_libc_20_0_0_git::fabsf16(_Float16):
        push rbp
        mov     rbp, rsp
        call    __extendhfsf2@PLT
 vbroadcastss    xmm1, dword ptr [rip + .LCPI0_0]
        vandps  xmm0, xmm0, xmm1
        call    __truncsfhf2@PLT
        pop     rbp
 ret
```

whereas with LLVM 16, `bitcast` -> `and` -> `bitcast` generates something like:

```asm
__llvm_libc_20_0_0_git::fabsf16(_Float16):
 push    rbp
        mov     rbp, rsp
        vpextrw eax, xmm0, 0
 and     eax, 32767
        vpinsrw xmm0, xmm0, eax, 0
        pop rbp
        ret
```

Related:

- https://github.com/llvm/llvm-project/issues/104914
- https://github.com/llvm/llvm-project/pull/104869#issuecomment-2297563670

cc @arsenm @lntue
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkVl9v4ygQ_zTkBTUCHLD9kIc22Ugr9XTV3mq1bxY22GYXgwU4Se_Tn8BJm6S31e7e47lV7Q7z5zfzYwa496ozUq4BfQB0u-BT6K1b2710g-r68LyorXhe9yGMHmT3gOwA2XVW1FaHpXUdILu_AdlZw56-sc--AWgL0P3896_AXVCmg62zA7xxoUI_1cvGDoDstN6fX3ejs99kEwDZNXYYVPygDRKcFlkpJK5Fg4p2hTFHqwxRXtaCi5ZmBRcUkA38aHzY2KFWRsLguPGtdYOHoVcefvwUw18ABAzNvwlAEgnZRlOtzHdrGllZ4WCvhJAGGjsZIVvYc91CsELVTlseMINVFe0rreqmIqiKP50KMVZ2345TUHr-5rUH2eZsFtEikH0ApHgVlYAUyf85GCD0CEgJtW24riZj-CBFxYVwEJAMw8YOggcI8ocZvzTBPb-kGc0RBNkW1io03IcTeEKPMFioMDvpRRFOityIKE-WEWJGcpZfaJErdydNHL1F12dFJ19DkVOx8-1N1S-pUCbY_x07r_Q0XOsX6BHvsuW1X7YxavHCWUTwLwVGP1PgPw08FimtQy8NvP_y9Y5iAndPmEHlobEB8j1XmtdaRq3UMZ000vEgPfR2kKGPzazVd_kjprg_EbV83Dx9RBW6yHV-ltqaDqJj3s7PvPouR7z2cx0uibj1O06-h64er6WD3ad3XCAb6PzNeio7hLCq5DFII_rWtwSs0NPj55PivnaWi7jXvY-ax2HA0ZU4WCfgGBwE9MGpEQLyAF-yptvrOHtuxOiTdWqqizf-EaLgJtP4tr8BdM7Xji-ZzQtOhnfoP_TSSe7hQYUePj5--QPOexwwdOplwBC8A9mHKOJGXP57ofHbO-I_UJy4vcr1J-ndj_IY3AFKfrys-qkwadTF57R8NenODpTx7vCGt5MFesvJG4zv0_JJah6kuKne3a8flsr7SXpAdhit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