-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Open
Labels
Priority:2Work that is important, but not critical for the releaseWork that is important, but not critical for the releasearea-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMICLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIoptimization
Milestone
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.
runtime/src/coreclr/System.Private.CoreLib/src/System/GC.cs
Lines 128 to 143 in e6bb456
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
YuhanKun, Nyrest, EgorBo and hez2010Nyrest
Metadata
Metadata
Assignees
Labels
Priority:2Work that is important, but not critical for the releaseWork that is important, but not critical for the releasearea-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMICLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIoptimization