Skip to content

Bound check with uint casts no longer elided #123085

Description

@MihaZupan
public static int Foo(string str)
{
    int length = str.Length;
    
    while ((uint)(length - 1) < (uint)str.Length && str[length - 1] == ' ')
    {
        length--;
    }
    
    return length;
}

public static int Bar(string str)
{
    int length = str.Length;
    
    while (length > 0 && str[length - 1] == ' ')
    {
        length--;
    }
    
    return length;
}

In .NET 9, Foo elided the extra bounds check, but godbolt is showing it again on main https://godbolt.org/z/qnYEzcdne.

Bar also seems like something where the JIT could prove that an extra check isn't needed.

Activity

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

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIreduce-unsafe

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions