Skip to content

Remove double negation in VN#123160

Closed
EgorBo wants to merge 8 commits intodotnet:mainfrom
EgorBo:double-neg-vn
Closed

Remove double negation in VN#123160
EgorBo wants to merge 8 commits intodotnet:mainfrom
EgorBo:double-neg-vn

Conversation

@EgorBo
Copy link
Member

@EgorBo EgorBo commented Jan 13, 2026

A couple of diffs from copying this optimization from global morph to VN

// Remove double negation/not.
if (op1->OperIs(oper))
{
JITDUMP("Remove double negation/not\n")
GenTree* op1op1 = op1->gtGetOp1();
DEBUG_DESTROY_NODE(tree);
DEBUG_DESTROY_NODE(op1);
return op1op1;
}

Copilot AI review requested due to automatic review settings January 13, 2026 23:16
@github-actions github-actions bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Jan 13, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request adds a double negation/not removal optimization to the Value Numbering (VN) phase of the JIT compiler. The optimization removes redundant consecutive NOT or NEG operations (e.g., NOT(NOT(x)) → x, NEG(NEG(x)) → x), matching similar logic already implemented in the global morph phase.

Changes:

  • Added double negation removal optimization for VNF_NOT and VNF_NEG operations in VNForFunc

@AndyAyersMS
Copy link
Member

I had some similar VN things in #88527 including using De Morgan's laws to push NOT down in boolean VN "trees" ... feel free to poach or ignore.

Comment on lines +2583 to +2589
// NOT(AND(x,y)) ==> OR(NOT(x), NOT(y))
//
if (funcApp.m_func == VNFunc(GT_AND))
{
return VNForFunc(typ, VNFunc(GT_OR), VNForFunc(typ, VNFunc(GT_NOT), funcApp.m_args[0]),
VNForFunc(typ, VNFunc(GT_NOT), funcApp.m_args[1]));
}
Copy link
Member

Choose a reason for hiding this comment

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

Is this one better?

It's going from 2 operations to 3 operations. Same for the one just below (although that one can potentially be 2->2 if ANDN exists).

Copy link
Member Author

Choose a reason for hiding this comment

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

I think in fact it produces no diffs so I'll remove it

Copy link
Member

Choose a reason for hiding this comment

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

Note this is in the "VN" space so number of operations is not that significant. The idea is to propagate nots (either up or down, here down as it's a bit easier to recognize) so they are more likely to reach other nots (again in VN space).

This was part of a bigger PR to build up more complex boolean expressions by back substituting reaching predicates and then to try and simplify them.

Copy link
Member

@AndyAyersMS AndyAyersMS left a comment

Choose a reason for hiding this comment

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

I'd prefer the more extensive change here, as helps put VN trees in "canonical" form, but if you want to go back to the simpler change that's fine too.

Copilot AI review requested due to automatic review settings February 4, 2026 01:08
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@EgorBo
Copy link
Member Author

EgorBo commented Feb 9, 2026

For some reason even from just NOT(NOT(X)) there are size/perfscore regressions 😐 needs to be investigated, closing for now

@EgorBo EgorBo closed this Feb 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants