[llvm] [DAG] Generalize fold (not (neg x)) -> (add X, -1) (PR #154348)
guan jian via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 5 00:07:47 PDT 2025
================
@@ -9988,13 +9988,26 @@ 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 or the subtract has
+ // a single use.
+ if (N0.getOpcode() == ISD::SUB && isAllOnesConstant(N1)) {
+ SDValue Y = N0.getOperand(0);
+ SDValue X = N0.getOperand(1);
+
+ // Avoid infinite recursion with
+ // Fold (and X, (add (not Y), Z)) -> (and X, (not (sub Y, Z)))
+ if (none_of(N->users(),
+ [](SDNode *User) { return User->getOpcode() == ISD::AND; })) {
+ if (isa<ConstantSDNode>(Y) || N0.hasOneUse()) {
+ SDValue NotY = DAG.getNOT(DL, Y, VT);
+ return DAG.getNode(ISD::ADD, DL, VT, X, NotY, N->getFlags());
+ }
+ } else {
+ if (isa<ConstantSDNode>(Y) && N0.hasOneUse()) {
+ SDValue NotY = DAG.getNOT(DL, Y, VT);
----------------
rez5427 wrote:
You are right
https://github.com/llvm/llvm-project/pull/154348
More information about the llvm-commits
mailing list