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
47 changes: 47 additions & 0 deletions src/coreclr/jit/assertionprop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,53 @@ static void optAssertionProp_HWIntrinsic(Compiler* comp, GenTreeHWIntrinsic* tre
break;
}

case GT_RSH:
case GT_RSZ:
case GT_UDIV:
{
// A non-negative op1 produces a non-negative result that cannot exceed op1.
IntegralRange op1Range = IntegralRange::ForNode(node->gtGetOp1(), compiler);
if (op1Range.IsNonNegative())
{
return {SymbolicIntegerValue::Zero, op1Range.GetUpperBound()};
}

GenTree* op2 = node->gtGetOp2();
if (node->OperIs(GT_RSH) || !op2->IsIntegralConst())
{
break;
}

int64_t op2Value = op2->AsIntConCommon()->IntegralValue();

if (node->OperIs(GT_RSZ))
{
// A logical right shift by a non-zero amount always clears the sign bit.
const int64_t bitWidth = static_cast<int64_t>(genTypeSize(node) * BITS_PER_BYTE);
if ((op2Value > 0) && (op2Value < bitWidth))
{
if (varTypeIsLong(node) && (op2Value >= 32))
{
return {SymbolicIntegerValue::Zero, SymbolicIntegerValue::UIntMax};
}

return {SymbolicIntegerValue::Zero, UpperBoundForType(rangeType)};
}
}
else
{
// An unsigned division by a constant greater than one always clears the sign bit.
uint64_t divisor =
varTypeIsLong(node) ? static_cast<uint64_t>(op2Value) : static_cast<uint32_t>(op2Value);
if (divisor > 1)
{
return {SymbolicIntegerValue::Zero, UpperBoundForType(rangeType)};
}
}

break;
}

case GT_ARR_LENGTH:
case GT_MDARR_LENGTH:
return {SymbolicIntegerValue::Zero, SymbolicIntegerValue::ArrayLenMax};
Expand Down
76 changes: 76 additions & 0 deletions src/tests/JIT/opt/Casts/NeverNegativeCast.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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.Numerics;
using System.Runtime.CompilerServices;
using Xunit;

namespace CodeGenTests
{
// Casts from int to long of values that are known to never be negative
// should use a zero-extension instead of a sign-extension.
public class NeverNegativeCast
{
[MethodImpl(MethodImplOptions.NoInlining)]
static nint TrailingZeroCount_Div(uint mask)
{
// X64-NOT: movsxd
// X64-NOT: cdqe
return BitOperations.TrailingZeroCount(mask) / sizeof(ushort);
}

[MethodImpl(MethodImplOptions.NoInlining)]
static long LeadingZeroCount_Rsh(uint value)
{
// X64-NOT: movsxd
// X64-NOT: cdqe
return BitOperations.LeadingZeroCount(value) >> 1;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static long PopCount_Rsz(uint value)
{
// X64-NOT: movsxd
// X64-NOT: cdqe
return BitOperations.PopCount(value) >>> 2;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static long Rsz_ByConstant(int value)
{
// X64-NOT: movsxd
// X64-NOT: cdqe
return value >>> 1;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static long UDiv_ByConstant(int value)
{
// X64-NOT: movsxd
// X64-NOT: cdqe
return (int)((uint)value / 3);
}

[Fact]
public static void TestEntryPoint()
{
Assert.Equal(0, TrailingZeroCount_Div(1));
Assert.Equal(2, TrailingZeroCount_Div(0x10));
Assert.Equal(16, TrailingZeroCount_Div(0));

Assert.Equal(16, LeadingZeroCount_Rsh(0));
Assert.Equal(0, LeadingZeroCount_Rsh(uint.MaxValue));

Assert.Equal(8, PopCount_Rsz(uint.MaxValue));
Assert.Equal(0, PopCount_Rsz(0));

Assert.Equal(int.MaxValue, Rsz_ByConstant(-1));
Assert.Equal(0x40000000, Rsz_ByConstant(int.MinValue));
Assert.Equal(21, Rsz_ByConstant(42));

Assert.Equal(1431655765, UDiv_ByConstant(-1));
Assert.Equal(14, UDiv_ByConstant(42));
}
}
}
18 changes: 18 additions & 0 deletions src/tests/JIT/opt/Casts/NeverNegativeCast.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- Needed for CLRTestEnvironmentVariable -->
<RequiresProcessIsolation>true</RequiresProcessIsolation>
</PropertyGroup>
<PropertyGroup>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs">
<HasDisasmCheck>true</HasDisasmCheck>
</Compile>

<CLRTestEnvironmentVariable Include="DOTNET_TieredCompilation" Value="0" />
<CLRTestEnvironmentVariable Include="DOTNET_JITMinOpts" Value="0" />
</ItemGroup>
</Project>
Loading