Skip to content

JIT: missing opportunity for RBO #126703

Description

@EgorBo

The following repro represents a suboptimal codegen for IndexOfWhereAllBitsSet that prevents me from using it:

int idx = Vector128.IndexOfWhereAllBitsSet(cmp);
if (idx != -1)
{
    ...
}

Minimal repro:

static int IndexOf(int x)
{
    int result = BitOperations.TrailingZeroCount(x);
    return (result != 32) ? result : -1;
}

static void Test(int x)
{
    int idx = IndexOf(x);
    if (idx != -1)
        Console.WriteLine(idx);
}

Actual codegen of Test:

G_M44320_IG01:  ;; offset=0x0000
G_M44320_IG02:  ;; offset=0x0000
       tzcnt    ecx, ecx
       mov      eax, -1
       cmp      ecx, 32
       cmove    ecx, eax
       cmp      ecx, -1
       jne      SHORT G_M44320_IG04
G_M44320_IG03:  ;; offset=0x0014
       ret      
G_M44320_IG04:  ;; offset=0x0015
       tail.jmp [System.Console:WriteLine(int)]

Expected codegen of Test:

G_M59698_IG01:  ;; offset=0x0000
G_M59698_IG02:  ;; offset=0x0000
       tzcnt    ecx, ecx
       cmp      ecx, 32
       jne      SHORT G_M59698_IG04
G_M59698_IG03:  ;; offset=0x0009
       ret      
G_M59698_IG04:  ;; offset=0x000A
       tail.jmp [System.Console:WriteLine(int)]

so effectively jit should lower it to:

static void Test(int x)
{
    int idx = BitOperations.TrailingZeroCount(x);
    if (idx != 32)
    {
        Console.WriteLine(idx);
    }
}

Activity

  1. added
    untriaged
    on Apr 9, 2026
  2. EgorBo commented on Apr 9, 2026

    @EgorBo
    MemberAuthor
  3. added
    area-CodeGen-coreclr
    on Apr 9, 2026
  4. dotnet-policy-service commented on Apr 9, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  5. added a commit that references this issue on Apr 9, 2026
  6. EgorBo commented on Apr 9, 2026

    @EgorBo
    MemberAuthor
  7. removed
    untriaged
    on Apr 13, 2026
  8. added a commit that references this issue on Apr 14, 2026
    0a30b5d
  9. added a commit that references this issue on Apr 21, 2026
    6323aec
  10. locked and limited conversation to collaborators on May 18, 2026
  11. added a commit that references this issue on Jul 15, 2026
    6324221
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions