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..0ed78173838325 --- /dev/null +++ b/src/tests/JIT/opt/InstructionCombining/AddExtendedOperand.csproj @@ -0,0 +1,17 @@ + + + + true + + + None + True + + + + true + + + + +