Skip to content

arm64: Add support for bitwise NOT #111904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
6 changes: 4 additions & 2 deletions src/coreclr/jit/codegenarm64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3490,13 +3490,15 @@ void CodeGen::genCodeForNegNot(GenTree* tree)

GenTree* operand = tree->gtGetOp1();
// The src must be a register.
if (tree->OperIs(GT_NEG) && operand->isContained())
if (tree->OperIs(GT_NEG, GT_NOT) && operand->isContained())
{
genTreeOps oper = operand->OperGet();
switch (oper)
{
case GT_MUL:
{
assert(tree->OperIs(GT_NEG));

ins = INS_mneg;
GenTree* op1 = tree->gtGetOp1();
GenTree* a = op1->gtGetOp1();
Expand All @@ -3510,7 +3512,7 @@ void CodeGen::genCodeForNegNot(GenTree* tree)
case GT_RSH:
case GT_RSZ:
{
assert(ins == INS_neg || ins == INS_negs);
assert(ins == INS_neg || ins == INS_negs || ins == INS_mvn);
assert(operand->gtGetOp2()->IsCnsIntOrI());
assert(operand->gtGetOp2()->isContained());

Expand Down
5 changes: 5 additions & 0 deletions src/coreclr/jit/lower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,11 @@ GenTree* Lowering::LowerNode(GenTree* node)
}
#endif
break;
case GT_NOT:
#ifdef TARGET_ARM64
ContainCheckNot(node->AsOp());
#endif
break;
case GT_SELECT:
return LowerSelect(node->AsConditional());

Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/lower.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class Lowering final : public Phase
insCflags TruthifyingFlags(GenCondition cond);
void ContainCheckConditionalCompare(GenTreeCCMP* ccmp);
void ContainCheckNeg(GenTreeOp* neg);
void ContainCheckNot(GenTreeOp* notOp);
void TryLowerCnsIntCselToCinc(GenTreeOp* select, GenTree* cond);
void TryLowerCselToCSOp(GenTreeOp* select, GenTree* cond);
bool TryLowerAddSubToMulLongOp(GenTreeOp* op, GenTree** next);
Expand Down
27 changes: 26 additions & 1 deletion src/coreclr/jit/lowerarmarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ bool Lowering::IsContainableUnaryOrBinaryOp(GenTree* parentNode, GenTree* childN
}
}

if (childNode->OperIs(GT_LSH, GT_RSH, GT_RSZ) && parentNode->OperIs(GT_AND_NOT))
if (childNode->OperIs(GT_LSH, GT_RSH, GT_RSZ) && parentNode->OperIs(GT_NOT, GT_AND_NOT))
{
return true;
}
Expand Down Expand Up @@ -3290,6 +3290,31 @@ void Lowering::ContainCheckNeg(GenTreeOp* neg)
}
}

//------------------------------------------------------------------------
// ContainCheckNot : determine whether the source of a not should be contained.
//
// Arguments:
// notOp - pointer to the node
//
void Lowering::ContainCheckNot(GenTreeOp* notOp)
{
if (notOp->isContained())
return;

if (!varTypeIsIntegral(notOp))
return;

if ((notOp->gtFlags & GTF_SET_FLAGS))
return;

GenTree* childNode = notOp->gtGetOp1();
if (comp->opts.OptimizationEnabled() && childNode->OperIs(GT_LSH, GT_RSH, GT_RSZ) &&
IsContainableUnaryOrBinaryOp(notOp, childNode))
{
MakeSrcContained(notOp, childNode);
}
}

//----------------------------------------------------------------------------------------------
// TryLowerCselToCSOp: Try converting SELECT/SELECTCC to SELECT_?/SELECT_?CC. Conversion is possible only if
// one of the operands of the select node is one of GT_NEG, GT_NOT or GT_ADD.
Expand Down
97 changes: 97 additions & 0 deletions src/tests/JIT/opt/InstructionCombining/Mvn.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// 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;

namespace TestMvn
{
public class Program
{
[MethodImpl(MethodImplOptions.NoInlining)]
[Fact]
public static int CheckMvn()
{
bool fail = false;

if (Mvn(5) != 0xFFFFFFFA)
{
fail = true;
}

if (MvnLSL(10) != 0xFFFAFFFF)
{
fail = true;
}

if (MvnLSR(0x76543210) != 0xFFFFFF89)
{
fail = true;
}

if (MvnASR(0xACE1234) != -0x5670A)
{
fail = true;
}

if (MvnLargeShift(0x1A1A) != 0x5FFFFFFf)
{
fail = true;
}

if (MvnLargeShift64Bit(0x2B3C2B3C2B3C2B3C) != 0xFFFFFFFFD4C3D4C3)
{
fail = true;
}

if (fail)
{
return 101;
}
return 100;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static uint Mvn(uint a)
{
//ARM64-FULL-LINE: mvn {{w[0-9]+}}, {{w[0-9]+}}
return ~a;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static uint MvnLSL(uint a)
{
//ARM64-FULL-LINE: mvn {{w[0-9]+}}, {{w[0-9]+}}, LSL #15
return ~(a<<15);
}

[MethodImpl(MethodImplOptions.NoInlining)]
static uint MvnLSR(uint a)
{
//ARM64-FULL-LINE: mvn {{w[0-9]+}}, {{w[0-9]+}}, LSR #24
return ~(a>>24);
}

[MethodImpl(MethodImplOptions.NoInlining)]
static int MvnASR(int a)
{
//ARM64-FULL-LINE: mvn {{w[0-9]+}}, {{w[0-9]+}}, ASR #9
return ~(a>>9);
}

[MethodImpl(MethodImplOptions.NoInlining)]
static uint MvnLargeShift(uint a)
{
//ARM64-FULL-LINE: mvn {{w[0-9]+}}, {{w[0-9]+}}, LSL #28
return ~(a<<60);
}

[MethodImpl(MethodImplOptions.NoInlining)]
static ulong MvnLargeShift64Bit(ulong a)
{
//ARM64-FULL-LINE: mvn {{x[0-9]+}}, {{x[0-9]+}}, LSR #32
return ~(a>>160);
}
}
}
17 changes: 17 additions & 0 deletions src/tests/JIT/opt/InstructionCombining/Mvn.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="Mvn.cs">
<HasDisasmCheck>true</HasDisasmCheck>
</Compile>
<CLRTestEnvironmentVariable Include="DOTNET_TieredCompilation" Value="0" />
<CLRTestEnvironmentVariable Include="DOTNET_JITMinOpts" Value="0" />
</ItemGroup>
</Project>
Loading