Skip to content

Loop condition i != T.Length bounds check not eliminated #84697

@xtqqczze

Description

@xtqqczze

This is not a particularly common pattern, but it is a missed optimization all the same.

Consider:

    public static int Array_NE(byte[] src)
    {    
        int sum = 0;
        for (int i = 0; i != src.Length; i++)
            sum += src[i];
        return sum;
    }
    public static int Array_LT(byte[] src)
    {    
        int sum = 0;
        for (int i = 0; i < src.Length; i++)
            sum += src[i];
        return sum;
    }

A bounds check is eliminated for the second method but not the first.

Applies to T[], string, Span<T>, ReadOnlySpan<T>, etc.

https://csharp.godbolt.org/z/bKzrE9Wjq

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions