Skip to content
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

Optimize "(vec & cns) == zero" on arm64 #102705

Closed
wants to merge 3 commits into from
Closed
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
39 changes: 36 additions & 3 deletions src/coreclr/jit/lowerarmarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1460,17 +1460,37 @@ GenTree* Lowering::LowerHWIntrinsicCmpOp(GenTreeHWIntrinsic* node, genTreeOps cm
// Special case: "vec ==/!= zero_vector"
if (!varTypeIsFloating(simdBaseType) && (op != nullptr) && (simdSize != 12))
{
GenTree* cmp = op;
uint64_t scalarAndMask = UINT64_MAX;
GenTree* cmp = op;
if (simdSize != 8) // we don't need compression for Vector64
{
CorInfoType pairwiseMaxType = CORINFO_TYPE_UINT;

// If op is "vec & cnsVec" where both u64 components in that cnsVec are the same (for both SIMD12 and
// SIMD16) then we'd better do this AND on top of TYP_LONG NI_AdvSimd_Extract in the end - it produces a
// more optimal codegen.
if (op->OperIsHWIntrinsic(NI_AdvSimd_And) && op->AsHWIntrinsic()->Op(2)->OperIs(GT_CNS_VEC))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't have to be a constant right?

Just any (x & y) == zero or (x & y) != zero can be optimzied down to a tst (on both xarch and arm64).

{
GenTreeVecCon* andMask = op->AsHWIntrinsic()->Op(2)->AsVecCon();
simd16_t val = andMask->gtSimd16Val;
if (ElementsAreSame(val.i8, 16) && emitter::emitIns_valid_imm_for_alu(val.i64[0], EA_8BYTE))
{
pairwiseMaxType = CORINFO_TYPE_UBYTE;
scalarAndMask = val.u64[0];
BlockRange().Remove(op);
BlockRange().Remove(andMask);
op = op->AsHWIntrinsic()->Op(1);
}
}

node->Op(1) = op;
LIR::Use tmp1Use(BlockRange(), &node->Op(1), node);
ReplaceWithLclVar(tmp1Use);
op = node->Op(1);
GenTree* opClone = comp->gtClone(op);
BlockRange().InsertAfter(op, opClone);

cmp = comp->gtNewSimdHWIntrinsicNode(simdType, op, opClone, NI_AdvSimd_Arm64_MaxPairwise, CORINFO_TYPE_UINT,
cmp = comp->gtNewSimdHWIntrinsicNode(simdType, op, opClone, NI_AdvSimd_Arm64_MaxPairwise, pairwiseMaxType,
simdSize);
BlockRange().InsertBefore(node, cmp);
LowerNode(cmp);
Expand All @@ -1483,7 +1503,20 @@ GenTree* Lowering::LowerHWIntrinsicCmpOp(GenTreeHWIntrinsic* node, genTreeOps cm

GenTree* val =
comp->gtNewSimdHWIntrinsicNode(TYP_LONG, cmp, zroCns, NI_AdvSimd_Extract, CORINFO_TYPE_ULONG, simdSize);
BlockRange().InsertAfter(zroCns, val);

// Apply the scalar AND mask
if (scalarAndMask != UINT64_MAX)
{
GenTree* andMaskNode = comp->gtNewIconNode(static_cast<ssize_t>(scalarAndMask), TYP_LONG);
GenTree* andNode = comp->gtNewOperNode(GT_AND, TYP_LONG, val, andMaskNode);
BlockRange().InsertAfter(zroCns, val, andMaskNode, andNode);
LowerNode(val);
val = andNode;
}
else
{
BlockRange().InsertAfter(zroCns, val);
}
LowerNode(val);

GenTree* cmpZeroCns = comp->gtNewIconNode(0, TYP_LONG);
Expand Down
Loading