Skip to content

Commit ec94756

Browse files
committed
[SelectionDAG] fold (not (sub Y, X)) -> (add X, ~Y)
This replaces (not (neg x)) -> (add X, -1) because that is covered by this.
1 parent a8280c4 commit ec94756

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9979,13 +9979,15 @@ SDValue DAGCombiner::visitXOR(SDNode *N) {
99799979
}
99809980
}
99819981

9982-
// fold (not (neg x)) -> (add X, -1)
9983-
// FIXME: This can be generalized to (not (sub Y, X)) -> (add X, ~Y) if
9984-
// Y is a constant or the subtract has a single use.
9985-
if (isAllOnesConstant(N1) && N0.getOpcode() == ISD::SUB &&
9986-
isNullConstant(N0.getOperand(0))) {
9987-
return DAG.getNode(ISD::ADD, DL, VT, N0.getOperand(1),
9988-
DAG.getAllOnesConstant(DL, VT));
9982+
// fold (not (sub Y, X)) -> (add X, ~Y) is subtract is a single use
9983+
// or Y is a constant.
9984+
if (isAllOnesConstant(N1) && N0.getOpcode() == ISD::SUB) {
9985+
SDValue N00 = N0.getOperand(0), N01 = N0.getOperand(1);
9986+
if (isa<ConstantSDNode>(N00) || N0.hasOneUse()) {
9987+
SDValue NotY = DAG.getNOT(DL, N00, VT);
9988+
AddToWorklist(NotY.getNode());
9989+
return DAG.getNode(ISD::ADD, DL, VT, N01, NotY);
9990+
}
99899991
}
99909992

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

0 commit comments

Comments
 (0)