[llvm] [DAG] Fold trunc(abdu(x, y)) and trunc(abds(x, y)) if they have sufficient leading zero/sign bits (PR #151471)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 1 01:17:11 PDT 2025


================
@@ -3925,6 +3926,47 @@ SDValue DAGCombiner::foldSubToUSubSat(EVT DstVT, SDNode *N, const SDLoc &DL) {
   return SDValue();
 }
 
+// trunc (ABDU/S A, B)) → ABDU/S (trunc A), (trunc B)
+SDValue DAGCombiner::foldAbdToNarrowType(EVT VT, SDNode *N, const SDLoc &DL) {
+  SDValue Op = N->getOperand(0);
+
+  unsigned Opcode = Op.getOpcode();
+  if (Opcode != ISD::ABDU && Opcode != ISD::ABDS)
+    return SDValue();
+
+  SDValue Operand0 = Op.getOperand(0);
+  SDValue Operand1 = Op.getOperand(1);
+
+  // Early exit if either operand is zero.
+  if (ISD::isBuildVectorAllZeros(Operand0.getNode()) ||
+      ISD::isBuildVectorAllZeros(Operand1.getNode()))
+    return SDValue();
+
+  EVT SrcVT = Op.getValueType();
+  EVT TruncVT = N->getValueType(0);
+  unsigned NumSrcBits = SrcVT.getScalarSizeInBits();
+  unsigned NumTruncBits = TruncVT.getScalarSizeInBits();
+  unsigned NeededBits = NumSrcBits - NumTruncBits;
+
+  bool CanFold = false;
+
+  if (Opcode == ISD::ABDU) {
+    KnownBits Known = DAG.computeKnownBits(Op);
----------------
RKSimon wrote:

Why are you testing the ABD result instead of the operands like the alive2 tests? Is the fold still correct?

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


More information about the llvm-commits mailing list