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
4 changes: 2 additions & 2 deletions src/coreclr/jit/codegenxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4897,7 +4897,7 @@ void CodeGen::genCodeForShift(GenTree* tree)
{
int shiftByValue = (int)shiftBy->AsIntConCommon()->IconValue();

if (tree->OperIsRotate() && compiler->compOpportunisticallyDependsOn(InstructionSet_BMI2))
if (tree->OperIsRotate() && compiler->compOpportunisticallyDependsOn(InstructionSet_BMI2) && !tree->gtSetFlags())
{
// If we have a contained source operand, we must emit rorx.
// We may also use rorx for 64bit values when a mov would otherwise be required,
Expand All @@ -4924,7 +4924,7 @@ void CodeGen::genCodeForShift(GenTree* tree)
return;
}
}
else if (tree->OperIsShift() && compiler->compOpportunisticallyDependsOn(InstructionSet_BMI2))
else if (tree->OperIsShift() && compiler->compOpportunisticallyDependsOn(InstructionSet_BMI2) && !tree->gtSetFlags())
{
// Emit shlx, sarx, shrx if BMI2 is available instead of mov+shl, mov+sar, mov+shr.
switch (tree->OperGet())
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20009,7 +20009,7 @@ bool GenTree::IsArrayAddr(GenTreeArrAddr** pArrAddr)
bool GenTree::SupportsSettingZeroFlag()
{
#if defined(TARGET_XARCH)
if (OperIs(GT_AND, GT_OR, GT_XOR, GT_ADD, GT_SUB, GT_NEG))
if (OperIs(GT_AND, GT_OR, GT_XOR, GT_ADD, GT_SUB, GT_NEG, GT_LSH, GT_RSH, GT_RSZ, GT_ROL, GT_ROR))
{
return true;
}
Expand Down
29 changes: 28 additions & 1 deletion src/coreclr/jit/lower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,33 @@ bool Lowering::IsSafeToMarkRegOptional(GenTree* parentNode, GenTree* childNode)
return false;
}

//------------------------------------------------------------------------
// IsProfitableToSetZeroFlag: Checks if it's profitable to optimize an shift
// and rotate operations to set the zero flag.
//
// Arguments:
// op - The operation node to check.
//
// Return value:
// true if it's profitable to set the zero flag; otherwise false.
//
bool Lowering::IsProfitableToSetZeroFlag(GenTree* op) const
{
#ifdef TARGET_XARCH
if (op->OperIs(GT_LSH, GT_RSH, GT_RSZ, GT_ROR, GT_ROL))
{
// BMI2 instructions (SHLX, SARX, SHRX, RORX) do not set zero flag.
if (!op->AsOp()->gtGetOp2()->OperIsConst())
{
return false;
}
}

#endif // TARGET_XARCH

return true;
}

//------------------------------------------------------------------------
// LowerNode: this is the main entry point for Lowering.
//
Expand Down Expand Up @@ -4204,7 +4231,7 @@ GenTree* Lowering::OptimizeConstCompare(GenTree* cmp)
// Optimize EQ/NE(op_that_sets_zf, 0) into op_that_sets_zf with GTF_SET_FLAGS + SETCC.
LIR::Use use;
if (cmp->OperIs(GT_EQ, GT_NE) && op2->IsIntegralConst(0) && op1->SupportsSettingZeroFlag() &&
BlockRange().TryGetUse(cmp, &use))
BlockRange().TryGetUse(cmp, &use) && IsProfitableToSetZeroFlag(op1))
{
op1->gtFlags |= GTF_SET_FLAGS;
op1->SetUnusedValue();
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/jit/lower.h
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,9 @@ class Lowering final : public Phase
// Check if marking an operand of a node as reg-optional is safe.
bool IsSafeToMarkRegOptional(GenTree* parentNode, GenTree* node) const;

// Checks if it's profitable to optimize an shift and rotate operations to set the zero flag.
bool IsProfitableToSetZeroFlag(GenTree* op) const;

inline LIR::Range& BlockRange() const
{
return LIR::AsRange(m_block);
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/lsraxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,7 @@ int LinearScan::BuildShiftRotate(GenTree* tree)
#endif
}
else if (!tree->isContained() && (tree->OperIsShift() || source->isContained()) &&
compiler->compOpportunisticallyDependsOn(InstructionSet_BMI2))
compiler->compOpportunisticallyDependsOn(InstructionSet_BMI2) && !tree->gtSetFlags())
{
// We don'thave any specific register requirements here, so skip the logic that
// reserves RCX or preferences the source reg.
Expand Down
17 changes: 14 additions & 3 deletions src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7874,9 +7874,8 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac, bool* optA
{
tree = fgOptimizeRelationalComparisonWithConst(tree->AsOp());
oper = tree->OperGet();

assert(op1 == tree->AsOp()->gtGetOp1());
assert(op2 == tree->AsOp()->gtGetOp2());
op1 = tree->gtGetOp1();
op2 = tree->gtGetOp2();
}

if (opts.OptimizationEnabled() && fgGlobalMorph && tree->OperIs(GT_GT, GT_LT, GT_LE, GT_GE))
Expand Down Expand Up @@ -9177,6 +9176,18 @@ GenTree* Compiler::fgOptimizeRelationalComparisonWithConst(GenTreeOp* cmp)
oper = (oper == GT_LE) ? GT_GE : GT_LT;
cmp->gtFlags &= ~GTF_UNSIGNED;
}
// LE_UN/GT_UN(expr, int.MaxValue) => EQ/NE(RSZ(expr, 32), 0).
else if (opts.OptimizationEnabled() && (op1->TypeIs(TYP_LONG) && (op2Value == UINT_MAX)))
{
oper = (oper == GT_GT) ? GT_NE : GT_EQ;
GenTree* icon32 = gtNewIconNode(32, TYP_INT);
icon32->SetMorphed(this);

GenTreeOp* shiftNode = gtNewOperNode(GT_RSZ, TYP_LONG, op1, icon32);
shiftNode->SetMorphed(this);

cmp->gtOp1 = shiftNode;
}
}
}

Expand Down
Loading