Skip to content
Draft
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
1 change: 1 addition & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -6661,6 +6661,7 @@ class Compiler
GenTree* fgOptimizeHWIntrinsic(GenTreeHWIntrinsic* node);
GenTree* fgOptimizeHWIntrinsicAssociative(GenTreeHWIntrinsic* node);
#endif // FEATURE_HW_INTRINSICS
GenTree* fgOptimizeDistributiveArithemtic(GenTreeOp* tree);
GenTree* fgOptimizeCommutativeArithmetic(GenTreeOp* tree);
GenTree* fgOptimizeRelationalComparisonWithCasts(GenTreeOp* cmp);
GenTree* fgOptimizeAddition(GenTreeOp* add);
Expand Down
74 changes: 73 additions & 1 deletion src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7862,6 +7862,8 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac, bool* optA
goto CM_OVF_OP;
}

// TODO: Call fgOptimizeDistributiveArithemtic + fgOptimizeCommutativeArithmetic

if (!fgGlobalMorph)
{
break;
Expand Down Expand Up @@ -8068,6 +8070,10 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac, bool* optA
case GT_OR:
case GT_XOR:
case GT_AND:
if (oper == GT_ADD || oper == GT_OR || oper == GT_AND)
{
tree = fgOptimizeDistributiveArithemtic(tree->AsOp());
}
tree = fgOptimizeCommutativeArithmetic(tree->AsOp());
if (!tree->OperIsSimple())
{
Expand Down Expand Up @@ -10269,7 +10275,73 @@ GenTree* Compiler::fgOptimizeHWIntrinsicAssociative(GenTreeHWIntrinsic* tree)
#endif // FEATURE_HW_INTRINSICS

//------------------------------------------------------------------------
// fgOptimizeCommutativeArithmetic: Optimizes commutative operations.
// fgOptimizeDistributiveArithemtic: Optimizes distributive arithemtic.
//
// Arguments:
// tree - the unchecked GT_ADD/GT_SUB/GT_OR/GT_AND tree to optimize.
//
// Return Value:
// The unchanged tree or optimized tree with oper GT_MUL/GT_OR/GT_AND.
//
GenTree* Compiler::fgOptimizeDistributiveArithemtic(GenTreeOp* tree)
{
assert(tree->OperIs(GT_ADD, GT_SUB, GT_OR, GT_AND));
assert(!tree->gtOverflowEx());

if (opts.OptimizationDisabled())
{
return tree;
}

if (!varTypeIsIntegralOrI(tree))
{
return tree;
}

if (((tree->gtFlags & GTF_PERSISTENT_SIDE_EFFECTS) != 0) || ((tree->gtFlags & GTF_ORDER_SIDEEFF) != 0))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This could use the same optimization you are trying to apply😅... Probably handled by c++ though, so this is purely documentation ;-)

{
return tree;
}

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

auto isDistributiveOver = [](genTreeOps op1, genTreeOps op2) {
// op1 is distributive over op2 iff:
// ((A op1 B) op2 (A op1 C)) <==> (A op1 (B op2 C))

switch (op1)
{
case GT_MUL:
return op2 == GT_ADD || op2 == GT_SUB;

case GT_AND:
return op2 == GT_OR || op2 == GT_AND;

case GT_OR:
return op2 == GT_AND || op2 == GT_OR;

default:
return false;
}
};

if ((op1->OperGet() == op2->OperGet()) && isDistributiveOver(op1->OperGet(), tree->OperGet()))
{
if (GenTree::Compare(op1->gtGetOp1(), op2->gtGetOp1()))
{
tree->AsOp()->gtOp1 = op1->gtGetOp1();
tree->AsOp()->gtOp2 = gtNewOperNode(tree->OperGet(), tree->TypeGet(), op1->gtGetOp2(), op2->gtGetOp2());
tree->SetOper(op1->OperGet(), GenTree::PRESERVE_VN);
fgMorphTreeDone(tree->gtGetOp2());
}
}

return tree;
}

//------------------------------------------------------------------------
// fgOptimizeCommutativeArithmetic: Optimizes commutative arithemtic.
//
// Arguments:
// tree - the unchecked GT_ADD/GT_MUL/GT_OR/GT_XOR/GT_AND tree to optimize.
Expand Down
Loading