Skip to content

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

Description

@xtqqczze

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

Consider:

publicstaticintArray_NE(byte[]src){intsum=0;for(inti=0;i!=src.Length;i++)sum+=src[i];returnsum;}publicstaticintArray_LT(byte[]src){intsum=0;for(inti=0;i<src.Length;i++)sum+=src[i];returnsum;}

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