Skip to content

JIT: narrow masked 64-bit equality comparisons when the comparand uses bit 31 #134380

Open

Description

@AndyAyersMS

fgOptimizeEqualityComparisonWithConst narrows some
EQ/NE(AND(long, constant), constant) expressions to 32-bit operations, but
rejects comparands whose bit 31 is set. This leaves common HRESULT-style masks
as 64-bit operations even when both constants have zero upper 32 bits.

Revalidated on dotnet/runtime main at
4856f0c16f89d8cc625a62ee7bab7ed4afe14f9d (2026-09-21): the self-check passes
and the affected method still emits 23 bytes versus 19 for the hand-narrowed
control.

Minimal repro

using System.Runtime.CompilerServices;

public static class MaskCmp
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    public static bool HasTag(long value) =>
        (value & 0xD0000000L) == 0x90000000L;

    [MethodImpl(MethodImplOptions.NoInlining)]
    public static bool HandNarrowed(long value) =>
        (unchecked((uint)value) & 0xD0000000u) == 0x90000000u;
}

Current codegen

Windows x64, FullOpts:

; HasTag(long), 23 bytes
mov      eax, 0xD0000000
and      rax, rcx
mov      ecx, 0x90000000
cmp      rax, rcx
sete     al
movzx    rax, al

Both constants require registers because they cannot be encoded as
sign-extended 64-bit imm32 operands.

Expected codegen

The mask guarantees the upper 32 bits of the left side are zero, and the
comparand also has zero upper 32 bits. The equality can therefore use the low
32 bits:

; 19 bytes
and      ecx, 0xD0000000
cmp      ecx, 0x90000000
sete     al
movzx    rax, al

This is identical to the hand-narrowed method.

Corpus impact

A prototype was replayed over nine usable Windows x64 SuperPMI collections:

  • 2,186,634 successful compiles
  • 220 changed contexts
  • 220 code-size improvements, 0 regressions
  • Net code-size reduction: 1,383 bytes

The changes are concentrated in Windows interop code, including
System.IO.Win32Marshal, Microsoft.Win32.RegistryKey, and
System.Management.

Notes

The existing mask check already allows bit 31; the rejection comes from a guard
shared with the adjacent cast-narrowing case. The strict bit-31 restriction
appears necessary for that cast case, but not for an AND whose upper 32 bits
are known zero.

No throughput benchmark was run; the measured result is reduced instruction
count and code size.

Prototype patch

Experimental patch (applies cleanly to current main)
diff --git a/src/coreclr/jit/morph.cpp b/src/coreclr/jit/morph.cpp
index 6981b11aa79a..c074277e9411 100644
--- a/src/coreclr/jit/morph.cpp
+++ b/src/coreclr/jit/morph.cpp
@@ -9029,10 +9029,9 @@ GenTree* Compiler::fgOptimizeEqualityComparisonWithConst(GenTreeOp* cmp)
 SKIP:
 
     // Now check for compares with small constant longs that can be cast to int.
-    // Note that we filter out negative values here so that the transformations
-    // below are correct. E. g. "EQ(-1L, CAST_UN(int))" is always "false", but were
-    // we to make it into "EQ(-1, int)", "true" becomes possible for negative inputs.
-    if (!op2->TypeIs(TYP_LONG) || ((op2->LngValue() >> 31) != 0))
+    // Note that we filter out values that do not fit in the low 32 bits here, since all of
+    // the transformations below narrow the comparison to 32 bits.
+    if (!op2->TypeIs(TYP_LONG) || ((op2->LngValue() >> 32) != 0))
     {
         return cmp;
     }
@@ -9040,7 +9039,13 @@ SKIP:
     if (!op1->OperIs(GT_AND))
     {
         // Another interesting case: cast from int.
-        if (op1->OperIs(GT_CAST) && op1->AsCast()->CastOp()->TypeIs(TYP_INT) && !op1->gtOverflow())
+        //
+        // Note that we filter out negative values here so that this transformation is
+        // correct. E. g. "EQ(-1L, CAST_UN(int))" is always "false", but were we to make it
+        // into "EQ(-1, int)", "true" becomes possible for negative inputs. Hence the
+        // comparand must fit in a *signed* int here.
+        if (((op2->LngValue() >> 31) == 0) && op1->OperIs(GT_CAST) && op1->AsCast()->CastOp()->TypeIs(TYP_INT) &&
+            !op1->gtOverflow())
         {
             // Simply make this into an integer comparison.
             cmp->gtOp1 = op1->AsCast()->CastOp();

Note

This issue was generated with GitHub Copilot.

Activity

  1. added
    area-CodeGen-coreclr
    on Sep 21, 2026
  2. added
    untriaged
    on Sep 21, 2026
  3. dotnet-policy-service commented on Sep 21, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  4. EgorBo commented on Sep 21, 2026

    @EgorBo
    Member

    Note this one is expected to be handled by KnownBits naturally. We know bit set by (value & 0xD0000000L) and we can eval the relop

  5. added this to the 12.0.0 milestone on Sep 25, 2026
  6. removed
    untriaged
    on Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrperformance

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions