Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/coreclr/jit/lowerarmarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
100 changes: 100 additions & 0 deletions src/tests/JIT/opt/InstructionCombining/AddExtendedOperand.cs
Original file line number Diff line number Diff line change
@@ -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<NullReferenceException>(() => LoadedUnsigned(0, ref Unsafe.NullRef<uint>()));
Assert.Throws<NullReferenceException>(() => LoadedSigned(0, ref Unsafe.NullRef<uint>()));
Assert.Equal(0L, Checked(-1, 0));
Assert.Throws<OverflowException>(() => Checked(0x100000000, 0));
Assert.Throws<OverflowException>(() => Checked(long.MinValue, 0));
}
}
17 changes: 17 additions & 0 deletions src/tests/JIT/opt/InstructionCombining/AddExtendedOperand.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- Needed for CLRTestEnvironmentVariable -->
<RequiresProcessIsolation>true</RequiresProcessIsolation>
</PropertyGroup>
<PropertyGroup>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="AddExtendedOperand.cs">
<HasDisasmCheck>true</HasDisasmCheck>
</Compile>
<CLRTestEnvironmentVariable Include="DOTNET_TieredCompilation" Value="0" />
<CLRTestEnvironmentVariable Include="DOTNET_JITMinOpts" Value="0" />
</ItemGroup>
</Project>
Loading