Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[x86-64 BMI2] Missed BLSR emit when AND is factored out of ternary conditional #133848

Open
Validark opened this issue Apr 1, 2025 · 2 comments

Comments

@Validark
Copy link

Validark commented Apr 1, 2025

Godbolt link

#include <stdint.h>

uint64_t blsr_test_cmove(uint64_t rdi, uint64_t rsi) {
    uint64_t rax = rdi & (rdi - 1);
    return (rsi != 0) ? rax : rdi;
}

Emits:

blsr_test_cmove:
        cmp     rsi, 1
        lea     rcx, [rdi - 1]
        sbb     rax, rax
        or      rax, rcx
        and     rax, rdi
        ret

Should be:

blsr_test_cmove:
        blsr    rax, rdi
        test    rsi, rsi
        cmove   rax, rdi
        ret
@Validark
Copy link
Author

Validark commented Apr 1, 2025

Same issue applies to BLSI (Godbolt link)

@llvmbot
Copy link
Member

llvmbot commented Apr 1, 2025

@llvm/issue-subscribers-backend-x86

Author: Niles Salter (Validark)

[Godbolt link](https://c.godbo.lt/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYM9DgDJ4GmADl3ACNMYhAAdlIAB1QFQlsGZzcPPVj4mwFffyCWUPCoy0xrRKECJmICZPdPLgtMK0yGMoqCbMCQsL0Fcsrq1Lqe1vbc/IkASgtUV2Jkdg4AUgAmAGY/ZDcsAGoFlcce/EEAOgRd7AWNAEELy9c/AgA2SWMCLeDaBWIXzB7jZBZUAA3TAQO6CJ4vLbEfCkLZgx7PV7EeLjHYRABCNy22Lh9whSKYqh2KwAIlD8Dslg8thBoXgtgBaLZcca7TFXHFQzAEGYMGnI%2BlgMC7MkaVG7ABiUMJWxA5LwbJuCwiJJuHEmtE4AFZeJ4OFpSKhOI4tgpprNMJSVjxSARNOrJgBrEBajT6TiSXX2w2cXgKEBuu369WkOCwGCIFCoFjROhhciUNAxuPhZAbQzAF7EVwMR18OgEML%2BiDBb3BPwVACenBt5eYxErAHlgtpikGbUm2IJGwxaNXg6QsMFXMBHGIPjXeFgWBnxAP8MQ23hgf6B5hVMVXIXJ%2BRBA1vbQ8MFiFXnFhvQRiHgWDvgcRgnFMCTMDOjIejPbJlQDMAFAA1PBMAAd0baJGB3fhBBEMR2CkGRBEUFR1AHXQln0DMQFMP59CPf1IEmVBoiaVcGUbFZGRnWZTlJAAvBg7x1Q07yvLA8IgSYihKOwIAcfpajQnw/A6PIuiidIEgEPiJDQ8SmhGToCnqRpSl6KoXBqboGiXAQWkqeSRPCQZVKkozhiE0YuhZKYZjmCZ3Q4Ri9QNI0OC2VQAA4HgZJ4tnTIwaUvHNHVRCBcEIEgrRZXggy0cZJgQTAmCwcJ2NIZ1XXsz1SBvDKnN4Fy/QDW1P1DCMICQJNY3oMgKAgSqUxQX9mGiBQEFQAh81oQtkUoUsBzrKsdwGhtm1bawd07RgCB7PtvSHEcx1oCduCnF9Z3mA0FyXFdvXXTdtxW3dC01AdD2PU8MA26KrxvQ67wfJRn1fYB31AYMvx/f9AJAsC9RtSDhFEcQ4IBxC1G9XQ6gMD8sPMM62IIojEhIsiKIqZBqJJOiGPy5i8FY%2BAOK0rjPB4hgnHUgYBPJ/SxgeGI4gkpJKf4hmMkSWmunpziml0tSUlqJTtOaVTOcMixjJZ7pRfMhS7LNGzYI1bUvQHFy3M87zJC2YA0wC7NcxCsKiGISLxmiz94sS5LKGVjgspyt08p9DhCsDS20pdN0TpWVXnN9Yr3rtpZeBvLgNCd70CsD2LJjveI7EkIA) ```c #include <stdint.h>

uint64_t blsr_test_cmove(uint64_t rdi, uint64_t rsi) {
uint64_t rax = rdi & (rdi - 1);
return (rsi != 0) ? rax : rdi;
}


Emits:

```asm
blsr_test_cmove:
        cmp     rsi, 1
        lea     rcx, [rdi - 1]
        sbb     rax, rax
        or      rax, rcx
        and     rax, rdi
        ret

Should be:

blsr_test_cmove:
        blsr    rax, rdi
        test    rsi, rsi
        cmove   rax, rdi
        ret

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants