Skip to content

JIT should eliminate useless comparisons against int.MaxValue / nint.MaxValue / long.MaxValue #52217

Open
@GrabYourPitchforks

Description

(Related to but distinct from #37462.)

Repro code

public static bool M(int x) {
    return (x > int.MaxValue);
}
M(Int32)
    L0000: cmp ecx, 0x7fffffff
    L0006: setg al
    L0009: movzx eax, al
    L000c: ret

Discussion

When comparing an int against int.MaxValue, or a uint against uint.MaxValue, or an nint against nint.MaxValue, etc., the JIT should eliminate comparisons that it knows will always evaluate to a constant true or false value.

This can help simplify code like the following, where line 136 below can be replaced with a simple if (bytesAllocated > nint.MaxValue), with the JIT eliminating the branch entirely if it knows the branch can never possibly be taken.

public static void AddMemoryPressure(long bytesAllocated)
{
if (bytesAllocated <= 0)
{
throw new ArgumentOutOfRangeException(nameof(bytesAllocated),
SR.ArgumentOutOfRange_NeedPosNum);
}
if ((4 == IntPtr.Size) && (bytesAllocated > int.MaxValue))
{
throw new ArgumentOutOfRangeException(nameof(bytesAllocated),
SR.ArgumentOutOfRange_MustBeNonNegInt32);
}
_AddMemoryPressure((ulong)bytesAllocated);
}

category:cq
theme:type-intrinsics
skill-level:beginner
cost:small
impact:small

Activity

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

Metadata

Assignees

Labels

Priority:2Work that is important, but not critical for the releasearea-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIoptimization

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions