[llvm] [AArch64] Use CNEG for absolute difference patterns. (PR #151177)

Paul Walker via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 4 07:39:59 PDT 2025


================
@@ -11386,6 +11386,22 @@ SDValue AArch64TargetLowering::LowerSELECT_CC(
       return DAG.getNode(ISD::AND, DL, VT, LHS, Shift);
     }
 
+    // Canonicalise absolute difference patterns:
+    //   select_cc lhs, rhs, sub(lhs, rhs), sub(rhs, lhs), cc ->
+    //   select_cc lhs, rhs, sub(lhs, rhs), neg(sub(lhs, rhs)), cc
+    //
+    //   select_cc lhs, rhs, sub(rhs, lhs), sub(lhs, rhs), cc ->
+    //   select_cc lhs, rhs, neg(sub(lhs, rhs)), sub(lhs, rhs), cc
+    // The second forms can be matched into subs+cneg.
+    if (TVal.getOpcode() == ISD::SUB && FVal.getOpcode() == ISD::SUB) {
+      if (TVal.getOperand(0) == LHS && TVal.getOperand(1) == RHS &&
+          FVal.getOperand(0) == RHS && FVal.getOperand(1) == LHS)
+        FVal = DAG.getNegative(TVal, DL, TVal.getValueType());
+      else if (TVal.getOperand(0) == RHS && TVal.getOperand(1) == LHS &&
+               FVal.getOperand(0) == LHS && FVal.getOperand(1) == RHS)
+        TVal = DAG.getNegative(FVal, DL, FVal.getValueType());
----------------
paulwalker-arm wrote:

Oh, I cannot say I give poison much consideration during lowering, with poison itself being relatively new to SelectionDAG. Given the intent is to reuse the existing ISD::SUB, if you're worried then perhaps just remove the wrap flags from the negated operand? as they've likely served their purpose by this point.

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


More information about the llvm-commits mailing list