Skip to content

[SelectionDAG] fold (not (sub Y, X)) -> (add X, ~Y) #147825

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 9 additions & 7 deletions llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9979,13 +9979,15 @@ SDValue DAGCombiner::visitXOR(SDNode *N) {
}
}

// fold (not (neg x)) -> (add X, -1)
// FIXME: This can be generalized to (not (sub Y, X)) -> (add X, ~Y) if
// Y is a constant or the subtract has a single use.
if (isAllOnesConstant(N1) && N0.getOpcode() == ISD::SUB &&
isNullConstant(N0.getOperand(0))) {
return DAG.getNode(ISD::ADD, DL, VT, N0.getOperand(1),
DAG.getAllOnesConstant(DL, VT));
// fold (not (sub Y, X)) -> (add X, ~Y) if Y is a constant.
// FIXME: We can also do this with single-use sub, but this causes an infinite
// loop
if (isAllOnesConstant(N1) && N0.getOpcode() == ISD::SUB) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (isAllOnesConstant(N1) && N0.getOpcode() == ISD::SUB) {
if (N0.getOpcode() == ISD::SUB && isAllOnesConstant(N1)) {

SDValue N00 = N0.getOperand(0), N01 = N0.getOperand(1);
if (isa<ConstantSDNode>(N00)) {
SDValue NotY = DAG.getNOT(DL, N00, VT); // N00 = ~N00
return DAG.getNode(ISD::ADD, DL, VT, N01, NotY);
}
}

// fold (not (add X, -1)) -> (neg X)
Expand Down