Skip to content

Commit 35ef766

Browse files
authored
[JIT] ARM64 - Combine cmp and shift ops into a single cmp op (#84605)
* Combine compare and shift ops into a single compare op * Fix comment * Expanding IsContainableBinaryOp. * Remove commented code * Added more cases * Update codegenarm64.cpp * Feedback * Feedback
1 parent 048467b commit 35ef766

File tree

3 files changed

+68
-27
lines changed

3 files changed

+68
-27
lines changed

src/coreclr/jit/codegen.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,7 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
15731573

15741574
#if defined(TARGET_ARM64)
15751575
static insCond JumpKindToInsCond(emitJumpKind condition);
1576+
static insOpts ShiftOpToInsOpts(genTreeOps op);
15761577
#elif defined(TARGET_XARCH)
15771578
static instruction JumpKindToCmov(emitJumpKind condition);
15781579
#endif

src/coreclr/jit/codegenarm64.cpp

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2626,31 +2626,7 @@ void CodeGen::genCodeForBinary(GenTreeOp* tree)
26262626
}
26272627
}
26282628

2629-
switch (op2->gtOper)
2630-
{
2631-
case GT_LSH:
2632-
{
2633-
opt = INS_OPTS_LSL;
2634-
break;
2635-
}
2636-
2637-
case GT_RSH:
2638-
{
2639-
opt = INS_OPTS_ASR;
2640-
break;
2641-
}
2642-
2643-
case GT_RSZ:
2644-
{
2645-
opt = INS_OPTS_LSR;
2646-
break;
2647-
}
2648-
2649-
default:
2650-
{
2651-
unreached();
2652-
}
2653-
}
2629+
opt = ShiftOpToInsOpts(op2->gtOper);
26542630

26552631
emit->emitIns_R_R_R_I(ins, emitActualTypeSize(tree), targetReg, a->GetRegNum(), b->GetRegNum(),
26562632
c->AsIntConCommon()->IconValue(), opt);
@@ -4546,6 +4522,15 @@ void CodeGen::genCodeForCompare(GenTreeOp* tree)
45464522

45474523
emit->emitIns_R_I(ins, cmpSize, op1Reg, intConst->IconValue());
45484524
}
4525+
else if (op2->isContained())
4526+
{
4527+
assert(op2->OperIs(GT_LSH, GT_RSH, GT_RSZ));
4528+
assert(op2->gtGetOp2()->IsCnsIntOrI());
4529+
assert(op2->gtGetOp2()->isContained());
4530+
4531+
emit->emitIns_R_R_I(ins, cmpSize, op1->GetRegNum(), op2->gtGetOp1()->GetRegNum(),
4532+
op2->gtGetOp2()->AsIntConCommon()->IntegralValue(), ShiftOpToInsOpts(op2->gtOper));
4533+
}
45494534
else
45504535
{
45514536
emit->emitIns_R_R(ins, cmpSize, op1->GetRegNum(), op2->GetRegNum());
@@ -10389,4 +10374,29 @@ insCond CodeGen::JumpKindToInsCond(emitJumpKind condition)
1038910374
}
1039010375
}
1039110376

10377+
//------------------------------------------------------------------------
10378+
// ShiftOpToInsOpts: Convert a shift-op to a insOpts.
10379+
//
10380+
// Arguments:
10381+
// shiftOp - the shift-op
10382+
//
10383+
insOpts CodeGen::ShiftOpToInsOpts(genTreeOps shiftOp)
10384+
{
10385+
switch (shiftOp)
10386+
{
10387+
case GT_LSH:
10388+
return INS_OPTS_LSL;
10389+
10390+
case GT_RSH:
10391+
return INS_OPTS_ASR;
10392+
10393+
case GT_RSZ:
10394+
return INS_OPTS_LSR;
10395+
10396+
default:
10397+
NO_WAY("expected a shift-op");
10398+
return INS_OPTS_NONE;
10399+
}
10400+
}
10401+
1039210402
#endif // TARGET_ARM64

src/coreclr/jit/lowerarmarch.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ bool Lowering::IsContainableBinaryOp(GenTree* parentNode, GenTree* childNode) co
269269
return false;
270270
}
271271

272-
if (parentNode->OperIs(GT_CMP, GT_OR, GT_XOR))
272+
if (parentNode->OperIs(GT_CMP, GT_OR, GT_XOR) || parentNode->OperIsCompare())
273273
{
274274
if (IsInvariantInRange(childNode, parentNode))
275275
{
@@ -2268,7 +2268,37 @@ void Lowering::ContainCheckCast(GenTreeCast* node)
22682268
//
22692269
void Lowering::ContainCheckCompare(GenTreeOp* cmp)
22702270
{
2271-
CheckImmedAndMakeContained(cmp, cmp->gtOp2);
2271+
GenTree* op1 = cmp->gtGetOp1();
2272+
GenTree* op2 = cmp->gtGetOp2();
2273+
2274+
if (CheckImmedAndMakeContained(cmp, op2))
2275+
return;
2276+
2277+
if (cmp->OperIsCompare() && CheckImmedAndMakeContained(cmp, op1))
2278+
{
2279+
std::swap(cmp->gtOp1, cmp->gtOp2);
2280+
cmp->ChangeOper(cmp->SwapRelop(cmp->gtOper));
2281+
return;
2282+
}
2283+
2284+
#ifdef TARGET_ARM64
2285+
if (comp->opts.OptimizationEnabled() && cmp->OperIsCompare())
2286+
{
2287+
if (IsContainableBinaryOp(cmp, op2))
2288+
{
2289+
MakeSrcContained(cmp, op2);
2290+
return;
2291+
}
2292+
2293+
if (IsContainableBinaryOp(cmp, op1))
2294+
{
2295+
MakeSrcContained(cmp, op1);
2296+
std::swap(cmp->gtOp1, cmp->gtOp2);
2297+
cmp->ChangeOper(cmp->SwapRelop(cmp->gtOper));
2298+
return;
2299+
}
2300+
}
2301+
#endif
22722302
}
22732303

22742304
#ifdef TARGET_ARM64

0 commit comments

Comments
 (0)