Skip to content
Closed
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
18 changes: 18 additions & 0 deletions src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12751,6 +12751,24 @@ GenTree* Compiler::fgOptimizeMultiply(GenTreeOp* mul)
fgUpdateConstTreeValueNumber(op2);
mul->ChangeOper(GT_LSH, GenTree::PRESERVE_VN);

if (mul->gtGetOp1()->OperIs(GT_LSH) && mul->gtGetOp1()->gtGetOp2()->IsIntegralConst())
{
GenTree* lsOp = mul->gtGetOp1();
GenTree* rsOp = mul->gtGetOp2();
GenTree* lsOpChOp1 = lsOp->gtGetOp1();
GenTree* lsOpChOp2 = lsOp->gtGetOp2();

int64_t child_val = lsOpChOp2->AsIntConCommon()->IntegralValue();
int64_t root_val = rsOp->AsIntConCommon()->IntegralValue();

mul->gtOp1 = lsOpChOp1;
mul->gtOp2 = lsOpChOp2;
mul->gtOp2->AsIntConCommon()->SetIntegralValue(root_val + child_val);

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.

Should you check if root_val+child_val can overflow here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It seems there must be some checks, but I can't think of any right now.

The tricky part is that multiplications become shifts and the value transforms from a number to a count of shifts. I don't know if it actually possible to make
X >> 2,147,483,647

There is another problem with the PR which made me mark it as a draft. The problem is that the PR causes 9 regressions (along with some improvements) I need some help in addressing them.

It would be very nice if someone from the team became a reviewer.


DEBUG_DESTROY_NODE(lsOp);
DEBUG_DESTROY_NODE(rsOp);
}

return mul;
}
}
Expand Down