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

via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 28 09:11:08 PDT 2024


================
@@ -5354,6 +5356,20 @@ 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)
+  // Fold avgfloor((add nw x,1), y) -> 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())) ||
+       sd_match(N, m_c_BinOp(Opcode, m_AllOf(m_Value(Add), m_Add(m_Value(X), m_One())), m_Value(Y)))) {
+      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())
----------------
dnsampaio wrote:

The else was for the outer if, not for the internal one. But using `IsSigned` and `!IsSigned` actually makes it cleaner.

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


More information about the llvm-commits mailing list