[llvm] [DAG] Generalize fold (not (neg x)) -> (add X, -1) (PR #154348)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 21 11:46:16 PDT 2025


================
@@ -9993,10 +9993,29 @@ SDValue DAGCombiner::visitXOR(SDNode *N) {
   if (N0.getOpcode() == ISD::SUB && isAllOnesConstant(N1)) {
     SDValue Y = N0.getOperand(0);
     SDValue X = N0.getOperand(1);
-    if (isa<ConstantSDNode>(Y) || N0.hasOneUse()) {
-      SDValue NotY =
-          DAG.getNode(ISD::XOR, DL, VT, Y, DAG.getAllOnesConstant(DL, VT));
-      return DAG.getNode(ISD::ADD, DL, VT, X, NotY);
+
+    // Avoid infinite recursion with
+    // Fold (and X, (add (not Y), Z)) -> (and X, (not (sub Y, Z)))
+    bool hasAndUsers = false;
+    for (SDUse &Use : N->uses()) {
+      SDNode *User = Use.getUser();
+      if (User->getOpcode() == ISD::AND) {
+        hasAndUsers = true;
+        break;
+      }
+    }
+    if (!hasAndUsers) {
----------------
topperc wrote:

```
if (none_of(N->users(), [](SDNode *User) {
  return User->getOpcode() == ISD::AND;
})) {
```

https://github.com/llvm/llvm-project/pull/154348


More information about the llvm-commits mailing list