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

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


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

None

>From 93e396339b1561ec4fb31d20f5c6226859d6299a 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 | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 5eb53d57c9c2bf..f0cd23a4e65b9d 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -5083,7 +5083,12 @@ 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 (avg x, 0) -> x >> 1
+  if (isNullOrNullSplat(N0))
+    return DAG.getNode(ISD::SRL, DL, VT, N1, DAG.getConstant(1, DL, VT));
+
+  if (isNullOrNullSplat(N1))
+    return DAG.getNode(ISD::SRL, DL, VT, N0, DAG.getConstant(1, DL, VT));
 
   return SDValue();
 }



More information about the llvm-commits mailing list