Skip to content

Unnecessary comparisons not eliminated for full range checks #70145

@stephentoub

Description

@stephentoub

Repro:

public static class C
{
    public static bool M0(byte b) => b >= 0;
    public static bool M1(byte b) => b <= byte.MaxValue;
    public static bool M2(int i) => i <= int.MaxValue;
    public static bool M3(long l) => l <= long.MaxValue;
}

At least according to SharpLab, this produces:

; Core CLR 6.0.322.12309 on amd64

C.M0(Byte)
    L0000: test cl, cl
    L0002: setae al
    L0005: movzx eax, al
    L0008: ret

C.M1(Byte)
    L0000: cmp cl, 0xff
    L0003: setbe al
    L0006: movzx eax, al
    L0009: ret

C.M2(Int32)
    L0000: cmp ecx, 0x7fffffff
    L0006: setle al
    L0009: movzx eax, al
    L000c: ret

C.M3(Int64)
    L0000: mov rax, 0x7fffffffffffffff
    L000a: cmp rcx, rax
    L000d: setle al
    L0010: movzx eax, al
    L0013: ret

Is there a reason that couldn't instead be:

C.M0(Byte)
    L0000: mov eax, 1
    L0005: ret

C.M1(Byte)
    L0000: mov eax, 1
    L0005: ret

C.M2(Int32)
    L0000: mov eax, 1
    L0005: ret

C.M3(Int64)
    L0000: mov eax, 1
    L0005: ret

?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIgood first issueIssue should be easy to implement, good for first-time contributorshelp wanted[up-for-grabs] Good issue for external contributors

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions