Skip to content

Commit 83b5f97

Browse files
committed
Try canonicalizing the tree from x - y to x + (-y)
1 parent 52090b9 commit 83b5f97

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

src/coreclr/jit/gentree.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15006,6 +15006,23 @@ GenTree* Compiler::gtFoldExpr(GenTree* tree)
1500615006
{
1500715007
GenTree* op2 = tree->AsOp()->gtOp2;
1500815008

15009+
if ((tree->gtOper == GT_SUB) && !tree->gtOverflow() && !op2->TypeIs(TYP_BYREF) && !op2->IsIconHandle())
15010+
{
15011+
// Canonicalize the tree from `x - y` to `x + (-y)`
15012+
// as this allows various other optimizations to kick in
15013+
15014+
tree->ChangeOper(GT_ADD, GenTree::PRESERVE_VN);
15015+
op2 = gtNewOperNode(GT_NEG, genActualType(op2->gtType), op2);
15016+
15017+
if (fgGlobalMorph)
15018+
{
15019+
fgMorphTreeDone(op2);
15020+
}
15021+
op2 = gtFoldExpr(op2);
15022+
15023+
tree->AsOp()->gtOp2 = op2;
15024+
}
15025+
1500915026
// The atomic operations are exempted here because they are never computable statically;
1501015027
// one of their arguments is an address.
1501115028
if (op1->OperIsConst() && op2->OperIsConst() && !tree->OperIsAtomicOp())

0 commit comments

Comments
 (0)