Skip to content

Commit

Permalink
Fixing build
Browse files Browse the repository at this point in the history
  • Loading branch information
TIHan committed May 16, 2022
1 parent 6b3b7e5 commit 574c17d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 33 deletions.
89 changes: 56 additions & 33 deletions src/coreclr/jit/rationalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,61 @@ void Rationalizer::RewriteIntrinsicAsUserCall(GenTree** use, ArrayStack<GenTree*
arg1, arg2);
}

#ifdef TARGET_ARM64
// RewriteSubLshDiv: Possibly rewrite a SubLshDiv node into a Mod.
//
// Arguments:
// use - A use of a node.
//
// Transform: a - (a / cns1) >> shift => a % cns1
// where cns1 is an signed integer constant that is a power of 2.
// We do this transformation because Lowering has a specific optimization
// for 'a % cns1' that is not easily reduced by other means.
//
void Rationalizer::RewriteSubLshDiv(GenTree** use) {
GenTree* const node = *use;

if (!node->OperIs(GT_SUB))
return;

GenTree* op1 = node->gtGetOp1();
GenTree* op2 = node->gtGetOp2();

if (!(node->TypeIs(TYP_INT, TYP_LONG) && !node->IsUnsigned() && op1->OperIs(GT_LCL_VAR)))
return;

if (!op2->OperIs(GT_LSH))
return;

GenTree* lsh = op2;
GenTree* div = lsh->gtGetOp1();
GenTree* cns1 = lsh->gtGetOp2();
if (div->OperIs(GT_DIV) && cns1->IsIntegralConst())
{
GenTree* a = div->gtGetOp1();
GenTree* cns2 = div->gtGetOp2();
if (a->OperIs(GT_LCL_VAR) && cns2->IsIntegralConstPow2() &&
op1->AsLclVar()->GetLclNum() == a->AsLclVar()->GetLclNum())
{
size_t cnsValue1 = cns1->AsIntConCommon()->IntegralValue();
size_t cnsValue2 = cns2->AsIntConCommon()->IntegralValue();
if ((cnsValue2 >> cnsValue1) == 1)
{
GenTree* const treeFirstNode = comp->fgGetFirstNode(node);
GenTree* const insertionPoint = treeFirstNode->gtPrev;
BlockRange().Remove(treeFirstNode, node);

node->ChangeOper(GT_MOD);
node->AsOp()->gtOp2 = cns1;

comp->gtSetEvalOrder(node);
BlockRange().InsertAfter(insertionPoint, LIR::Range(comp->fgSetTreeSeq(node), node));
}
}
}
}
#endif

#ifdef DEBUG

void Rationalizer::ValidateStatement(Statement* stmt, BasicBlock* block)
Expand Down Expand Up @@ -849,41 +904,9 @@ PhaseStatus Rationalizer::DoPhase()
}

#ifdef TARGET_ARM64
// Transform: a - (a / cns1) * cns1 => a % cns1
// where cns1 is an signed integer constant that is a power of 2.
// We do this transformation because Lowering has a specific optimization
// for 'a % cns1' that is not easily reduced by other means.
if (node->OperGet() == GT_SUB)
{
GenTree* op1 = node->gtGetOp1();
GenTree* op2 = node->gtGetOp2();
if (node->TypeIs(TYP_INT, TYP_LONG) && !node->IsUnsigned() && op1->OperIs(GT_LCL_VAR))
{
GenTree* mul = op2;
if (mul->OperIs(GT_MUL))
{
GenTree* div = mul->gtGetOp1();
GenTree* cns1 = mul->gtGetOp2();
if (div->OperIs(GT_DIV) && cns1->IsIntegralConst())
{
GenTree* a = div->gtGetOp1();
GenTree* cns2 = div->gtGetOp2();
if (a->OperIs(GT_LCL_VAR) && cns2->IsIntegralConstPow2() &&
op1->AsLclVar()->GetLclNum() == a->AsLclVar()->GetLclNum())
{
size_t cnsValue1 = cns1->AsIntConCommon()->IntegralValue();
size_t cnsValue2 = cns2->AsIntConCommon()->IntegralValue();
if (cnsValue1 == cnsValue2)
{
node->ChangeOper(GT_MOD);
node->AsOp()->gtOp2 = cns1;

m_rationalizer.comp->fgSetTreeSeq(node);
}
}
}
}
}
m_rationalizer.RewriteSubLshDiv(use);
}
#endif

Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/jit/rationalize.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class Rationalizer final : public Phase
void RewriteAssignment(LIR::Use& use);
void RewriteAddress(LIR::Use& use);

#ifdef TARGET_ARM64
void RewriteSubLshDiv(GenTree** use);
#endif

// Root visitor
Compiler::fgWalkResult RewriteNode(GenTree** useEdge, Compiler::GenTreeStack& parents);
};
Expand Down

0 comments on commit 574c17d

Please sign in to comment.