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

Gtneg mul divoptimizations #45604

Merged
merged 19 commits into from
Jan 12, 2021
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
62 changes: 62 additions & 0 deletions src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11909,6 +11909,21 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac)
return fgMorphCast(tree);

case GT_MUL:
if (opts.OptimizationEnabled() && !optValnumCSE_phase && !tree->gtOverflow())
{
// MUL(NEG(a), C) => MUL(a, NEG(C))
if (op1->OperIs(GT_NEG) && !op1->gtGetOp1()->IsCnsIntOrI() && op2->IsCnsIntOrI() &&
!op2->IsIconHandle())
{
GenTree* newOp1 = op1->gtGetOp1();
GenTree* newConst = gtNewIconNode(-op2->AsIntCon()->IconValue(), op2->TypeGet());
DEBUG_DESTROY_NODE(op1);
DEBUG_DESTROY_NODE(op2);
tree->AsOp()->gtOp1 = newOp1;
tree->AsOp()->gtOp2 = newConst;
return fgMorphSmpOp(tree, mac);
}
}

#ifndef TARGET_64BIT
if (typ == TYP_LONG)
Expand Down Expand Up @@ -12056,6 +12071,24 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac)
return fgMorphSmpOp(tree, mac);
}

if (opts.OptimizationEnabled() && !optValnumCSE_phase)
{
// DIV(NEG(a), C) => DIV(a, NEG(C))
if (op1->OperIs(GT_NEG) && !op1->gtGetOp1()->IsCnsIntOrI() && op2->IsCnsIntOrI() &&
!op2->IsIconHandle())
{
ssize_t op2Value = op2->AsIntCon()->IconValue();
if (op2Value != 1 && op2Value != -1) // Div must throw exception for int(long).MinValue / -1.
{
tree->AsOp()->gtOp1 = op1->gtGetOp1();
DEBUG_DESTROY_NODE(op1);
tree->AsOp()->gtOp2 = gtNewIconNode(-op2Value, op2->TypeGet());
DEBUG_DESTROY_NODE(op2);
return fgMorphSmpOp(tree, mac);
}
}
}

#ifndef TARGET_64BIT
if (typ == TYP_LONG)
{
Expand Down Expand Up @@ -13920,6 +13953,35 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac)
return child;
}

// Distribute negation over simple multiplication/division expressions
if (opts.OptimizationEnabled() && !optValnumCSE_phase && tree->OperIs(GT_NEG) &&
op1->OperIs(GT_MUL, GT_DIV))
{
GenTreeOp* mulOrDiv = op1->AsOp();
GenTree* op1op1 = mulOrDiv->gtGetOp1();
GenTree* op1op2 = mulOrDiv->gtGetOp2();

if (!op1op1->IsCnsIntOrI() && op1op2->IsCnsIntOrI() && !op1op2->IsIconHandle())
{
// NEG(MUL(a, C)) => MUL(a, -C)
// NEG(DIV(a, C)) => DIV(a, -C), except when C = {-1, 1}
ssize_t constVal = op1op2->AsIntCon()->IconValue();
if ((mulOrDiv->OperIs(GT_DIV) && (constVal != -1) && (constVal != 1)) ||
(mulOrDiv->OperIs(GT_MUL) && !mulOrDiv->gtOverflow()))
{
GenTree* newOp1 = op1op1; // a
GenTree* newOp2 = gtNewIconNode(-constVal, op1op2->TypeGet()); // -C
mulOrDiv->gtOp1 = newOp1;
mulOrDiv->gtOp2 = newOp2;

DEBUG_DESTROY_NODE(tree);
DEBUG_DESTROY_NODE(op1op2);

return mulOrDiv;
}
}
}

/* Any constant cases should have been folded earlier */
noway_assert(!op1->OperIsConst() || !opts.OptEnabled(CLFLG_CONSTANTFOLD) || optValnumCSE_phase);
break;
Expand Down
Loading