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

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 9 22:48:01 PDT 2025


================
@@ -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) {
----------------
arsenm wrote:

```suggestion
  if (N0.getOpcode() == ISD::SUB && isAllOnesConstant(N1)) {
```

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


More information about the llvm-commits mailing list