[llvm] [SDAG] (abs (add nsw a, -b)) -> (abds a, b) (PR #175801)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 28 09:51:42 PST 2026


================
@@ -11673,16 +11675,26 @@ SDValue DAGCombiner::foldABSToABD(SDNode *N, const SDLoc &DL) {
       (Opc0 != ISD::ZERO_EXTEND && Opc0 != ISD::SIGN_EXTEND &&
        Opc0 != ISD::SIGN_EXTEND_INREG)) {
     // fold (abs (sub nsw x, y)) -> abds(x, y)
+    // fold (abs (add nsw x, -y)) -> abds(x, y)
     // Don't fold this for unsupported types as we lose the NSW handling.
+    bool IsSub = AbsOp0.getOpcode() == ISD::SUB;
+    bool AbsOpWillNSW =
+        AbsOp0->getFlags().hasNoSignedWrap() || IsSub
+            ? DAG.willNotOverflowSub(/*IsSigned=*/true, Op0, Op1)
+            : DAG.willNotOverflowAdd(/*IsSigned=*/true, Op0, Op1);
+
     if (hasOperation(ISD::ABDS, VT) && TLI.preferABDSToABSWithNSW(VT) &&
-        (AbsOp0->getFlags().hasNoSignedWrap() ||
-         DAG.willNotOverflowSub(/*IsSigned=*/true, Op0, Op1))) {
+        AbsOpWillNSW) {
+      if (!IsSub)
+        Op1 = DAG.getNegative(Op1, SDLoc(Op1), VT);
----------------
DaKnig wrote:

no, not sure how to deal with this. please advice.

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


More information about the llvm-commits mailing list