[llvm] [X86] Fold generic ADD/SUB with constants to X86ISD::SUB/ADD (PR #164316)

via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 21 07:20:24 PDT 2025


================
@@ -57631,8 +57631,25 @@ static SDValue combineX86AddSub(SDNode *N, SelectionDAG &DAG,
       DCI.CombineTo(GenericAddSub, Op);
     }
   };
-  MatchGeneric(LHS, RHS, false);
-  MatchGeneric(RHS, LHS, X86ISD::SUB == N->getOpcode());
+  MatchGeneric(GenericOpc, LHS, RHS, false);
+  MatchGeneric(GenericOpc, RHS, LHS, X86ISD::SUB == N->getOpcode());
+
+  if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(RHS)) {
+    SDValue NegC = DAG.getConstant(-Const->getAPIntValue(), DL, VT);
+    if (X86ISD::SUB == N->getOpcode()) {
+      // With LHS - C, fold LHS + (-C)
----------------
brandonxin wrote:

I think `LHS - C` will be cananicalized to `LHS + (-C)` by `DAGCombiner::visitSUB` first, so we don't need to handle it here.
https://github.com/llvm/llvm-project/blob/683e2bf059a6e5e0bb9dc2628218b53dc2d1b490/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp#L4160-L4163

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


More information about the llvm-commits mailing list