Skip to content
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
11 changes: 11 additions & 0 deletions src/coreclr/jit/codegenxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,17 @@ void CodeGen::genCodeForBinary(GenTreeOp* treeNode)
src = op2;
}

// We can skip emitting 'and reg0, -1` if we know that the upper 32bits of 'reg0' are zero'ed.
if (compiler->opts.OptimizationEnabled())
{
if ((oper == GT_AND) && (targetType == TYP_INT) && !treeNode->gtSetFlags() && op2->IsIntegralConst(-1) &&
emit->AreUpper32BitsZero(targetReg))
{
genProduceReg(treeNode);
return;
}
}

// try to use an inc or dec
if (oper == GT_ADD && !varTypeIsFloating(treeNode) && src->isContainedIntOrIImmed() && !treeNode->gtOverflowEx())
{
Expand Down
28 changes: 28 additions & 0 deletions src/tests/JIT/opt/And/IntAnd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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;

namespace CodeGenTests
{
class IntAnd
{
[MethodImpl(MethodImplOptions.NoInlining)]
static uint Test_And_UInt32_MaxValue(uint i)
{
// X64: mov

// X64-NOT: and
return i & UInt32.MaxValue;
}

static int Main()
{
if (Test_And_UInt32_MaxValue(1234) != 1234)
return 0;

return 100;
}
}
}
17 changes: 17 additions & 0 deletions src/tests/JIT/opt/And/IntAnd.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
</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>