Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[x86-64 BMI2] Generating a bitstring with a min(64, LEN) 1 bits at the bottom should use BZHI #132714

Open
Validark opened this issue Mar 24, 2025 · 2 comments

Comments

@Validark
Copy link

Validark commented Mar 24, 2025

This code: Zig Godbolt C Godbolt

export fn foo(len: u64) u64 {
    return ~if (len >= 64)
        @as(u64, 0)
    else
        ~@as(u64, 0) << @intCast(len);
}

Gives me this emit:

        mov     rcx, -1
        cmp     rdi, 64
        shlx    rax, rcx, rdi
        not     rax
        cmovae  rax, rcx

Should be:

        mov     rcx, -1
        bzhi    rax, rcx, rdi
        cmp     rdi, 64
        cmovae  rax, rcx
@llvmbot
Copy link
Member

llvmbot commented Mar 24, 2025

@llvm/issue-subscribers-backend-x86

Author: Niles Salter (Validark)

This code: [Zig Godbolt](https://zig.godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:18,fontUsePx:'0',j:3,lang:zig,selection:(endColumn:27,endLineNumber:2,positionColumn:27,positionLineNumber:2,selectionStartColumn:27,selectionStartLineNumber:2,startColumn:27,startLineNumber:2),source:'export+fn+foo(len:+u64)+u64+%7B%0A++++return+~if+(len+%3E%3D+64)+@as(u64,+0)+else+~@as(u64,+0)+%3C%3C+@intCast(len)%3B%0A%7D%0A%0Aexport+fn+bar(len:+u64)+u64+%7B%0A++++const+m+%3D+~@as(u64,+0)%3B%0A++++const+x+%3D+bzhi_builtin(m,+len)%3B%0A++++return+if+(len+%3E%3D+64)+m+else+x%3B%0A%7D%0A%0Afn+bzhi_builtin(src:+u64,+mask:+u64)+u64+%7B%0A++++return+struct+%7B%0A++++++++extern+fn+@%22llvm.x86.bmi.bzhi.64%22(u64,+u64)+u64%3B%0A++++%7D.@%22llvm.x86.bmi.bzhi.64%22(src,+mask)%3B%0A%7D%0A'),l:'5',n:'0',o:'Zig+source+%233',t:'0')),header:(),k:62.7177700348432,l:'4',m:100,n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:ztrunk,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:2,lang:zig,libs:!(),options:'-O+ReleaseFast+-target+x86_64-linux+-mcpu%3Dznver5',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:3),l:'5',n:'0',o:'+zig+trunk+(Editor+%233)',t:'0')),header:(),k:37.2822299651568,l:'4',m:100,n:'0',o:'',s:0,t:'0')),l:'2',m:100,n:'0',o:'',t:'0')),version:4)
export fn foo(len: u64) u64 {
    return ~if (len &gt;= 64)
        @<!-- -->as(u64, 0)
    else
        ~@<!-- -->as(u64, 0) &lt;&lt; @<!-- -->intCast(len);
}

Gives me this emit:

        mov     rcx, -1
        cmp     rdi, 64
        shlx    rax, rcx, rdi
        not     rax
        cmovae  rax, rcx

Should be:

        mov     rcx, -1
        bzhi    rax, rcx, rdi
        cmp     rdi, 64
        cmovae  rax, rcx

@Validark
Copy link
Author

Also if len were a u8, it should just emit a BZHI instruction and nothing else

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

No branches or pull requests

3 participants