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

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 27 01:48:15 PDT 2025


================
@@ -9979,13 +9979,25 @@ 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.
+  // fold (not (sub Y, X)) -> (add X, ~Y) for targets with efficient AND-NOT
+  // when Y is single-use and not constant.
----------------
RKSimon wrote:

This comment makes no sense in conjunction with the below statement that the fold can't be used on hasAndNot targets

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


More information about the llvm-commits mailing list