Skip to content

JIT: missing opportunity for RBO #126703

@EgorBo

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);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIuntriagedNew issue has not been triaged by the area owner

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions