[llvm] [DAGCombiner] Add combine avg from shifts (PR #113909)

Sam Parker via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 28 07:00:17 PDT 2024


================
@@ -5354,6 +5356,18 @@ SDValue DAGCombiner::visitAVG(SDNode *N) {
           DAG.getNode(ISD::ADD, DL, VT, N0, DAG.getAllOnesConstant(DL, VT)));
   }
 
+  // Fold avgfloor((add nw x,y), 1) -> avgceil(x,y)
+  if (Opcode == ISD::AVGFLOORU || Opcode == ISD::AVGFLOORS) {
+    SDValue Add;
+    if(sd_match(N, m_c_BinOp(Opcode, m_AllOf(m_Value(Add), m_Add(m_Value(X), m_Value(Y))), m_One()))) {
+      if (IsSigned) {
+        if (hasOperation(ISD::AVGCEILS, VT) && Add->getFlags().hasNoSignedWrap())
+          return DAG.getNode(ISD::AVGCEILS, DL, VT, X, Y);
+        } else if (hasOperation(ISD::AVGCEILU, VT) && Add->getFlags().hasNoUnsignedWrap())
----------------
sparker-arm wrote:

The indentation is wrong here. I think it also makes sense to perform the `hasOperation` calls before trying to match anything.

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


More information about the llvm-commits mailing list