Skip to content
Open
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
82 changes: 81 additions & 1 deletion src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15097,12 +15097,92 @@ GenTree* Compiler::gtFoldExprBinary(GenTreeOp* tree)
return gtFoldExprSpecial(tree);
}
}
else if (tree->OperIsCompare())

genTreeOps oper = tree->OperGet();

if (GenTree::OperIsCompare(oper))
{
// comparisons of two local variables can sometimes be folded
return gtFoldExprCompare(tree);
}

if (!opts.OptimizationEnabled())
{
return tree;
}
Comment thread
tannergooding marked this conversation as resolved.

switch (oper)
{
case GT_ADD:
{
if (tree->gtOverflow())
{
break;
}

if (op1->OperIs(GT_NEG))
{
JITDUMP("Folding (-a) + b => b - a\n")

tree->gtOp1 = op2;
tree->gtOp2 = op1->gtGetOp1();

tree->SetOper(GT_SUB, GenTree::PRESERVE_VN);
tree->ToggleReverseOp();

return tree;
}

if (op2->OperIs(GT_NEG))
{
JITDUMP("Folding a + (-b) => a - b\n")

tree->gtOp2 = op2->gtGetOp1();
tree->SetOper(GT_SUB, GenTree::PRESERVE_VN);

return tree;
}
break;
}

case GT_SUB:
{
if (tree->gtOverflow())
{
break;
}

if (op2->OperIs(GT_NEG))
{
if (op1->OperIs(GT_NEG))
{
JITDUMP("Folding (-a) - (-b) => b - a\n")

tree->gtOp1 = op2->gtGetOp1();
tree->gtOp2 = op1->gtGetOp1();

tree->ToggleReverseOp();
return tree;
}
else
{
JITDUMP("Folding a - (-b) => a + b\n")

tree->gtOp2 = op2->gtGetOp1();
tree->SetOper(GT_ADD, GenTree::PRESERVE_VN);

return tree;
}
}
break;
}

default:
{
break;
}
}

return tree;
}

Expand Down
5 changes: 5 additions & 0 deletions src/coreclr/jit/gentree.h
Original file line number Diff line number Diff line change
Expand Up @@ -2265,6 +2265,11 @@ struct GenTree
gtFlags &= ~GTF_REVERSE_OPS;
}

void ToggleReverseOp()
{
gtFlags ^= GTF_REVERSE_OPS;
}

#if defined(TARGET_XARCH)
void SetDontExtend()
{
Expand Down
78 changes: 0 additions & 78 deletions src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8003,59 +8003,6 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, bool* optAssertionPropDone)
goto CM_ADD_OP;
}
}

// Skip optimization if non-NEG operand is constant.
// Both op1 and op2 are not constant because it was already checked above.
if (opts.OptimizationEnabled())
{
if (!op2->OperIs(GT_NEG))
{
break;
}

if (op1->OperIs(GT_NEG) && gtCanSwapOrder(op1, op2))
{
// -a - -b = > b - a
// SUB(NEG(a), NEG(b)) => SUB(b, a)

// tree: SUB
// op1: NEG
// op1Child: a
// op2: NEG
// op2Child: b

GenTree* op1Child = op1->AsOp()->gtOp1; // a
GenTree* op2Child = op2->AsOp()->gtOp1; // b
tree->AsOp()->gtOp1 = op2Child;
tree->AsOp()->gtOp2 = op1Child;

DEBUG_DESTROY_NODE(op1);
DEBUG_DESTROY_NODE(op2);

op1 = op2Child;
op2 = op1Child;
}
else
{
// a - -b = > a + b
// SUB(a, NEG(b)) => ADD(a, b)

// tree: SUB
// op1: a
// op2: NEG
// op2Child: b

GenTree* op2Child = op2->AsOp()->gtOp1; // b
oper = GT_ADD;
tree->SetOper(oper, GenTree::PRESERVE_VN);
tree->AsOp()->gtOp2 = op2Child;

DEBUG_DESTROY_NODE(op2);

op2 = op2Child;
}
}

break;

case GT_DIV:
Expand Down Expand Up @@ -10618,31 +10565,6 @@ GenTree* Compiler::fgOptimizeAddition(GenTreeOp* add)
return fgMorphTree(add);
}
}

// - a + b => b - a
// ADD(NEG(a), b) => SUB(b, a)
// Do not do this if "op2" is constant for canonicalization purposes.
if (!op2->IsIntegralConst() && gtCanSwapOrder(op1, op2))
{
add->SetOper(GT_SUB);
add->gtOp1 = op2;
add->gtOp2 = op1->AsOp()->gtGetOp1();

DEBUG_DESTROY_NODE(op1);
return add;
}
}

// a + -b = > a - b
// ADD(a, NEG(b)) => SUB(a, b)
if (!op1->OperIs(GT_NEG) && op2->OperIs(GT_NEG))
{
add->SetOper(GT_SUB);
add->gtOp2 = op2->AsOp()->gtGetOp1();

DEBUG_DESTROY_NODE(op2);

return add;
}

// Fold (~x + 1) to -x.
Expand Down
Loading