[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:48:41 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:
What we see here is an `X86ISD::SUB`, which sets `eflags` differently than `X86ISD::ADD`, so an `X86ISD::SUB LHS, C` cannot be canonicalized to `X86ISD::ADD LHS, -C`. Therefore we explicitly check for the `X86ISD::SUB` pattern here.
In contrast, a generic `ISD::SUB LHS, C` is equivalent to `ISD::ADD LHS, -C` and is canonicalized by the DAG, so we don't need to look for the `ISD::SUB LHS, C` to fold.
https://github.com/llvm/llvm-project/pull/164316
More information about the llvm-commits
mailing list