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
Changes from 3 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
55 changes: 54 additions & 1 deletion src/coreclr/src/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11843,7 +11843,22 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac)
case GT_CAST:
return fgMorphCast(tree);

case GT_MUL:
case GT_MUL:
// -a * C => a * -C, where C is constant
// MUL(NEG(a), C) => MUL(a, NEG(C))
if (op1->OperIs(GT_NEG) && !op1->IsCnsIntOrI() && op2->IsCnsIntOrI())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!op1->IsCnsIntOrI() - I think you meant op1->gtOp1()->IsCnsIntOrI() here.

{
// tree: MUL
// op1: a
// op2: NEG
// op2Child: C
op1 = op1->AsOp()->gtOp1; // a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rewrite to be:

    tree->AsOp()->gtOp1 = op1->gtGetOp1();
    DEBUG_DESTROY_NODE(op1);
    op2->AsIntCon()->SetIconValue(-op2->AsIntCon()->IconValue());
    return tree;

op2->AsIntCon()->SetIconValue(-op2->AsIntCon()->IconValue()); // -C

tree->AsOp()->gtOp1 = op1;
tree->AsOp()->gtOp2 = op2;
return tree;
}

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

// -a / C => a / -C, where C is constant
// DIV(NEG(a), C) => DIV(a, NEG(C))
if (op1->OperIs(GT_NEG) && !op1->IsCnsIntOrI() && op2->IsCnsIntOrI())
{
// tree: DIV
// op1: a
// op2: NEG
// op2Child: C
op1 = op1->AsOp()->gtOp1; // a
op2->AsIntCon()->SetIconValue(-op2->AsIntCon()->IconValue()); // -C

tree->AsOp()->gtOp1 = op1;
tree->AsOp()->gtOp2 = op2;
return tree;
}

#ifndef TARGET_64BIT
if (typ == TYP_LONG)
{
Expand All @@ -12011,6 +12042,7 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac)
{
op2 = gtFoldExprConst(op2);
}

break;

case GT_UDIV:
Expand Down Expand Up @@ -13367,6 +13399,7 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac)

case GT_MUL:


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

// Distribute negation over simple multiplication/division expressions
if (op1->OperIs(GT_MUL) || op1->OperIs(GT_DIV)) {
if (!op1->AsOp()->gtOp1->IsCnsIntOrI() && op1->AsOp()->gtOp2->IsCnsIntOrI())
{
// -(a * C) => a * -C
// -(a / C) => a / -C
oper = op1->gtOper;
tree->SetOper(op1->gtOper);
GenTree* newOp1 = op1->AsOp()->gtOp1; // a
GenTree* newOp2 = op1->AsOp()->gtOp2; // C
newOp2->AsIntCon()->SetIconValue(-newOp2->AsIntCon()->IconValue());

// Only need to destroy op1 since op2 will be null already
DEBUG_DESTROY_NODE(op1);

tree->AsOp()->gtOp1 = op1 = newOp1;
tree->AsOp()->gtOp2 = op2 = newOp2;
}
}

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