JIT should eliminate useless comparisons against int.MaxValue / nint.MaxValue / long.MaxValue #52217
Open
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
category:cq
theme:type-intrinsics
skill-level:beginner
cost:small
impact:small
Activity