(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
(Related to but distinct from #37462.)
Repro code
Discussion
When comparing an int against
int.MaxValue, or a uint againstuint.MaxValue, or an nint againstnint.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.runtime/src/coreclr/System.Private.CoreLib/src/System/GC.cs
Lines 128 to 143 in e6bb456
category:cq
theme:type-intrinsics
skill-level:beginner
cost:small
impact:small