[llvm] [DAG] Fold mismatched widened avg idioms to narrow form (#147946) (PR #163366)

via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 23 04:20:26 PDT 2025


================
@@ -16482,10 +16482,32 @@ SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
                                  DAG, DL);
     }
     break;
-  case ISD::AVGFLOORS:
-  case ISD::AVGFLOORU:
   case ISD::AVGCEILS:
   case ISD::AVGCEILU:
+    // trunc (avgceilu (sext (x), sext (y))) -> avgceils(x, y)
+    // trunc (avgceils (zext (x), zext (y))) -> avgceilu(x, y)
+    if (N0.hasOneUse()) {
+      SDValue Op0 = N0.getOperand(0);
+      SDValue Op1 = N0.getOperand(1);
+      if (N0.getOpcode() == ISD::AVGCEILU) {
+        if (TLI.isOperationLegalOrCustom(ISD::AVGCEILS, VT) &&
+            (Op0.getOpcode() == ISD::SIGN_EXTEND ||
+             Op0.getOpcode() == ISD::SIGN_EXTEND_INREG) &&
+            (Op1.getOpcode() == ISD::SIGN_EXTEND ||
+             Op1.getOpcode() == ISD::SIGN_EXTEND_INREG))
+          return DAG.getNode(ISD::AVGCEILS, DL, VT,
+                             N0.getOperand(0).getOperand(0),
+                             N0.getOperand(1).getOperand(0));
+      } else {
+        if (TLI.isOperationLegalOrCustom(ISD::AVGCEILU, VT) &&
+            N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
+            N0.getOperand(1).getOpcode() == ISD::ZERO_EXTEND)
+          return DAG.getNode(ISD::AVGCEILU, DL, VT,
+                             N0.getOperand(0).getOperand(0),
+                             N0.getOperand(1).getOperand(0));
+      }
+    }
+    [[fallthrough]];
----------------
laurenmchin wrote:

I just added them in - apologies for missing that earlier and thanks for the reminder!

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


More information about the llvm-commits mailing list