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

Optimization for "~x + 1" to "-x" (#69003) #69600

Merged
merged 4 commits into from
Jun 7, 2022
Merged
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12874,6 +12874,15 @@ GenTree* Compiler::fgOptimizeAddition(GenTreeOp* add)
return op1;
}

// Fold (~x + 1) to -x.
if (op1->OperIs(GT_NOT) && op2->IsIntegralConst(1))
{
op1->SetOper(GT_NEG);
Copy link
Member

Choose a reason for hiding this comment

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

It's good practice here to call op1->SetVNsFromNode(add) after this to inherit the VN from the add. Also, I suggest we move this under OptimizationEnabled() below.

DEBUG_DESTROY_NODE(op2);
DEBUG_DESTROY_NODE(add);
return op1;
}

// Note that these transformations are legal for floating-point ADDs as well.
if (opts.OptimizationEnabled())
{
Expand Down