From de62ad727bb8524334e200979fbd17f2fe821f84 Mon Sep 17 00:00:00 2001 From: Nino Floris Date: Mon, 2 Nov 2026 21:39:38 +0100 Subject: [PATCH 1/2] ARM64: Preserve extending loads when selecting ADD operands When both ADD operands have containable extensions, leave an extension folded into its load and contain the other extension in ADD. This avoids standalone sign or zero extensions for ordinary and computed int values, as well as narrowed long values. Prefer extending a narrowed long within ADD when the other operand can reuse its 32-bit source register. Use the existing containment and invariance checks. Cover signed, unsigned and computed operands, operand order, two wide inputs, checked conversions and null loads. Five ARM64 assembly checks and normal/JIT/register/GC stress tests pass. Full local PMI-derived replay: 172,209 successful contexts, two methods smaller by four bytes each and none larger. Checked ARM64 build and jit-format pass. --- src/coreclr/jit/lowerarmarch.cpp | 24 +++++ .../AddExtendedOperand.cs | 100 ++++++++++++++++++ .../AddExtendedOperand.csproj | 9 ++ 3 files changed, 133 insertions(+) create mode 100644 src/tests/JIT/opt/InstructionCombining/AddExtendedOperand.cs create mode 100644 src/tests/JIT/opt/InstructionCombining/AddExtendedOperand.csproj diff --git a/src/coreclr/jit/lowerarmarch.cpp b/src/coreclr/jit/lowerarmarch.cpp index 296a21188b3911..7075e45eaa5c9b 100644 --- a/src/coreclr/jit/lowerarmarch.cpp +++ b/src/coreclr/jit/lowerarmarch.cpp @@ -3120,6 +3120,30 @@ void Lowering::ContainCheckBinary(GenTreeOp* node) { if (IsContainableUnaryOrBinaryOp(node, op2)) { + if (node->OperIs(GT_ADD) && op1->OperIs(GT_CAST) && op2->OperIs(GT_CAST)) + { + GenTree* source1 = op1->AsCast()->CastOp(); + GenTree* source2 = op2->AsCast()->CastOp(); + + // Leave a cast with its contained load when the other extension + // would otherwise need a separate instruction. + bool preserveLoad = source2->isContained() && IsContainableMemoryOp(source2) && + !(source1->isContained() && IsContainableMemoryOp(source1)); + + // Extending a narrowed long may need a separate register, + // whereas zero-extending the other 32-bit operand can reuse its register. + bool reuseRegister = + node->TypeIs(TYP_LONG) && op2->IsUnsigned() && source1->TypeIs(TYP_INT) && + source2->TypeIs(TYP_INT) && source1->OperIs(GT_LCL_VAR) && + m_compiler->lvaGetDesc(source1->AsLclVar())->TypeIs(TYP_LONG) && + (!source2->OperIs(GT_LCL_VAR) || !m_compiler->lvaGetDesc(source2->AsLclVar())->TypeIs(TYP_LONG)); + if ((preserveLoad || reuseRegister) && IsContainableUnaryOrBinaryOp(node, op1)) + { + std::swap(node->gtOp1, node->gtOp2); + std::swap(op1, op2); + } + } + if (node->OperIs(GT_ADD, GT_SUB, GT_CMP) && op2->OperIs(GT_CAST)) { // We want to prefer the combined op here over containment of the cast op diff --git a/src/tests/JIT/opt/InstructionCombining/AddExtendedOperand.cs b/src/tests/JIT/opt/InstructionCombining/AddExtendedOperand.cs new file mode 100644 index 00000000000000..812750672b7982 --- /dev/null +++ b/src/tests/JIT/opt/InstructionCombining/AddExtendedOperand.cs @@ -0,0 +1,100 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.CompilerServices; +using Xunit; + +public class AddExtendedOperand +{ + [MethodImpl(MethodImplOptions.NoInlining)] + private static ulong Unsigned(ulong wide, uint narrow) + { + // Keep the full-width source alive: a standalone zero extension would + // need a separate register to avoid overwriting its upper bits. + return ((ulong)(uint)wide + narrow) ^ wide; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static ulong Reversed(ulong wide, uint narrow) => (narrow + (ulong)(uint)wide) ^ wide; + + [MethodImpl(MethodImplOptions.NoInlining)] + private static long Signed(long wide, uint narrow) => ((long)(int)wide + narrow) ^ wide; + + [MethodImpl(MethodImplOptions.NoInlining)] + private static ulong LoadedUnsigned(ulong wide, ref uint narrow) + { + // Keep the load's zero extension and contain the extension of wide in ADD. + // ARM64: ldr w[[N:[0-9]+]], [x1] + // ARM64-NEXT: add x{{[0-9]+}}, x[[N]], w0, UXTW + return ((ulong)(uint)wide + narrow) ^ wide; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static long LoadedSigned(long wide, ref uint narrow) + { + // ARM64: ldr w[[N:[0-9]+]], [x1] + // ARM64-NEXT: add x{{[0-9]+}}, x[[N]], w0, SXTW + return ((long)(int)wide + narrow) ^ wide; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static long SignedInt(int value, ref uint narrow) + { + // ARM64-NOT: sxtw + // ARM64: add {{x[0-9]+}}, {{x[0-9]+}}, {{w[0-9]+}}, SXTW + return (long)value + narrow; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static long ComputedInt(int value, ref uint narrow) + { + // ARM64-NOT: sxtw + // ARM64: add {{x[0-9]+}}, {{x[0-9]+}}, {{w[0-9]+}}, SXTW + return (long)unchecked(value * 3) + narrow; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static ulong UnsignedInt(uint value, ref uint narrow) + { + // ARM64: ldr w[[N:[0-9]+]], [x1] + // ARM64-NEXT: add x{{[0-9]+}}, x[[N]], w0, UXTW + return (ulong)value + narrow; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static ulong BothWide(ulong first, ulong second) => ((ulong)(uint)first + (uint)second) ^ first ^ second; + + [MethodImpl(MethodImplOptions.NoInlining)] + private static long Checked(long wide, uint narrow) => ((long)checked((int)wide) + narrow) ^ wide; + + [Fact] + public static void TestEntryPoint() + { + ulong[] values = { 0, 1, uint.MaxValue, 0x1234567880000000, 0xFFFFFFFF00000001, ulong.MaxValue }; + uint[] counts = { 0, 1, int.MaxValue, uint.MaxValue }; + foreach (ulong wide in values) + foreach (uint narrow in counts) + { + ulong low = wide & uint.MaxValue; + long signedLow = low <= int.MaxValue ? (long)low : (long)low - 0x100000000L; + Assert.Equal((low + narrow) ^ wide, Unsigned(wide, narrow)); + Assert.Equal((low + narrow) ^ wide, Reversed(wide, narrow)); + Assert.Equal((signedLow + narrow) ^ (long)wide, Signed((long)wide, narrow)); + uint fromMemory = narrow; + Assert.Equal((low + narrow) ^ wide, LoadedUnsigned(wide, ref fromMemory)); + Assert.Equal((signedLow + narrow) ^ (long)wide, LoadedSigned((long)wide, ref fromMemory)); + Assert.Equal(signedLow + narrow, SignedInt((int)wide, ref fromMemory)); + Assert.Equal((long)unchecked((int)wide * 3) + narrow, ComputedInt((int)wide, ref fromMemory)); + Assert.Equal(low + narrow, UnsignedInt((uint)wide, ref fromMemory)); + Assert.Equal((low + narrow) ^ wide ^ (0x1234567800000000UL | narrow), + BothWide(wide, 0x1234567800000000UL | narrow)); + } + + Assert.Throws(() => LoadedUnsigned(0, ref Unsafe.NullRef())); + Assert.Throws(() => LoadedSigned(0, ref Unsafe.NullRef())); + Assert.Equal(0L, Checked(-1, 0)); + Assert.Throws(() => Checked(0x100000000, 0)); + Assert.Throws(() => Checked(long.MinValue, 0)); + } +} diff --git a/src/tests/JIT/opt/InstructionCombining/AddExtendedOperand.csproj b/src/tests/JIT/opt/InstructionCombining/AddExtendedOperand.csproj new file mode 100644 index 00000000000000..b47c3e8e8d9f55 --- /dev/null +++ b/src/tests/JIT/opt/InstructionCombining/AddExtendedOperand.csproj @@ -0,0 +1,9 @@ + + + PdbOnly + True + + + + + From c5d55419e15da53e4cbdb1b8d2ed186da87c7734 Mon Sep 17 00:00:00 2001 From: Nino Floris Date: Tue, 3 Nov 2026 21:17:36 +0100 Subject: [PATCH 2/2] Enable the disassembly checks in the extended operand test --- .../InstructionCombining/AddExtendedOperand.csproj | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/tests/JIT/opt/InstructionCombining/AddExtendedOperand.csproj b/src/tests/JIT/opt/InstructionCombining/AddExtendedOperand.csproj index b47c3e8e8d9f55..0ed78173838325 100644 --- a/src/tests/JIT/opt/InstructionCombining/AddExtendedOperand.csproj +++ b/src/tests/JIT/opt/InstructionCombining/AddExtendedOperand.csproj @@ -1,9 +1,17 @@ - PdbOnly + + true + + + None True - + + true + + +