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

[JIT] Always transform AND(X, CNS(-1)) to X #82276

Merged
merged 8 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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: 0 additions & 11 deletions src/coreclr/jit/codegenxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -998,17 +998,6 @@ 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
6 changes: 2 additions & 4 deletions src/coreclr/jit/emitxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ bool emitter::IsFlagsAlwaysModified(instrDesc* id)
return true;
}

#ifdef TARGET_64BIT
//------------------------------------------------------------------------
// AreUpper32BitsZero: check if some previously emitted
// instruction set the upper 32 bits of reg to zero.
Expand Down Expand Up @@ -594,12 +595,10 @@ bool emitter::AreUpper32BitsZero(regNumber reg)
return false;
}

#ifdef TARGET_AMD64
if (id->idIns() == INS_movsxd)
{
return false;
}
#endif

// movzx always zeroes the upper 32 bits.
if (id->idIns() == INS_movzx)
Expand Down Expand Up @@ -655,16 +654,15 @@ bool emitter::AreUpper32BitsSignExtended(regNumber reg)
return true;
}

#ifdef TARGET_AMD64
// movsxd is always an 8 byte operation. W-bit is set.
if (id->idIns() == INS_movsxd)
{
return true;
}
#endif

return false;
}
#endif // TARGET_64BIT

//------------------------------------------------------------------------
// AreFlagsSetToZeroCmp: Checks if the previous instruction set the SZ, and optionally OC, flags to
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/jit/emitxarch.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ bool IsRedundantStackMov(instruction ins, insFormat fmt, emitAttr size, regNumbe
static bool IsJccInstruction(instruction ins);
static bool IsJmpInstruction(instruction ins);

#ifdef TARGET_64BIT
bool AreUpper32BitsZero(regNumber reg);
bool AreUpper32BitsSignExtended(regNumber reg);
#endif // TARGET_64BIT

bool AreFlagsSetToZeroCmp(regNumber reg, emitAttr opSize, genTreeOps treeOps);
bool AreFlagsSetForSignJumpOpt(regNumber reg, emitAttr opSize, GenTree* tree);
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 @@ -307,6 +307,7 @@ class Lowering final : public Phase
void LowerStoreIndir(GenTreeStoreInd* node);
GenTree* LowerAdd(GenTreeOp* node);
GenTree* LowerMul(GenTreeOp* mul);
GenTree* TryLowerAndNegativeOne(GenTreeOp* node);
GenTree* LowerBinaryArithmetic(GenTreeOp* binOp);
bool LowerUnsignedDivOrMod(GenTreeOp* divMod);
GenTree* LowerConstIntDivOrMod(GenTree* node);
Expand Down
57 changes: 57 additions & 0 deletions src/coreclr/jit/lowerxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,54 @@ GenTree* Lowering::LowerMul(GenTreeOp* mul)
return mul->gtNext;
}

//----------------------------------------------------------------------------------------------
// Lowering::TryLowerAndNegativeOne:
// If safe, lowers a tree AND(X, CNS(-1)) to X.
//
// Arguments:
// node - GT_AND node of integral type
//
// Return Value:
// Returns the replacement node if one is created else nullptr indicating no replacement
GenTree* Lowering::TryLowerAndNegativeOne(GenTreeOp* node)
TIHan marked this conversation as resolved.
Show resolved Hide resolved
{
assert(node->OperIs(GT_AND));

if (!varTypeIsIntegral(node))
return nullptr;

if (node->gtSetFlags())
return nullptr;

GenTree* op2 = node->gtGetOp2();

if (!op2->IsIntegralConst(-1))
return nullptr;

#ifdef TARGET_64BIT
if (!node->TypeIs(TYP_INT, TYP_LONG))
return nullptr;
#else // TARGET_64BIT
assert(op2->TypeIs(TYP_INT));

if (!node->TypeIs(TYP_INT))
return nullptr;
#endif // !TARGET_64BIT

LIR::Use use;
if (!BlockRange().TryGetUse(node, &use))
return nullptr;

GenTree* op1 = node->gtGetOp1();

use.ReplaceWith(op1);

BlockRange().Remove(op2);
BlockRange().Remove(node);

return op1;
}

//------------------------------------------------------------------------
// LowerBinaryArithmetic: lowers the given binary arithmetic node.
//
Expand Down Expand Up @@ -289,6 +337,15 @@ GenTree* Lowering::LowerBinaryArithmetic(GenTreeOp* binOp)
}
#endif

if (comp->opts.OptimizationEnabled() && binOp->OperIs(GT_AND))
{
GenTree* replacementNode = TryLowerAndNegativeOne(binOp);
if (replacementNode != nullptr)
{
return replacementNode->gtNext;
}
}

ContainCheckBinary(binOp);

return binOp->gtNext;
Expand Down