[llvm] [SelectionDAG] Fold (avg x, 0) -> x >> 1 (PR #85581)

via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 24 17:43:07 PDT 2024


https://github.com/AtariDreams updated https://github.com/llvm/llvm-project/pull/85581

>From 06317df559bc36274abfd90667fdf90e722fe15a Mon Sep 17 00:00:00 2001
From: Rose <gfunni234 at gmail.com>
Date: Sun, 17 Mar 2024 15:31:39 -0400
Subject: [PATCH] [SelectionDAG] Fold (avg x, 0) -> x >> 1

---
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 05b4ce3aaa2cae..42e0474e733d02 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -5135,7 +5135,13 @@ SDValue DAGCombiner::visitAVG(SDNode *N) {
   if (N0 == N1 && Level >= AfterLegalizeTypes)
     return N0;
 
-  // TODO If we use avg for scalars anywhere, we can add (avgfl x, 0) -> x >> 1
+  // fold (avgfloor x, 0) -> x >> 1
+  if (isNullOrNullSplat(N1)) {
+    if (Opcode == ISD::AVGFLOORS)
+      return DAG.getNode(ISD::SRA, DL, VT, N0, DAG.getConstant(1, DL, VT));
+    if (Opcode == ISD::AVGFLOORU)
+      return DAG.getNode(ISD::SRL, DL, VT, N0, DAG.getConstant(1, DL, VT));
+  }
 
   return SDValue();
 }



More information about the llvm-commits mailing list