[llvm] [SelectionDAG] Fold (avg x, 0) -> x >> 1 (PR #85581)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 18 09:05:48 PDT 2024
https://github.com/AtariDreams updated https://github.com/llvm/llvm-project/pull/85581
>From 4dc794da5c3bab65edce62e41e72569cdd3f04ff 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 | 5 ++++-
llvm/test/CodeGen/AArch64/hadd-combine.ll | 6 ++----
2 files changed, 6 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 5eb53d57c9c2bf..8f2d57f0aa0d09 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -5083,7 +5083,10 @@ 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
+ if (isNullOrNullSplat(N1) &&
+ (Opcode == ISD::AVGFLOORS || Opcode == ISD::AVGFLOORU))
+ return DAG.getNode((Opcode == ISD::AVGFLOORS) ? ISD::SRA : ISD::SRL, DL, VT,
+ N0, DAG.getConstant(1, DL, VT));
return SDValue();
}
diff --git a/llvm/test/CodeGen/AArch64/hadd-combine.ll b/llvm/test/CodeGen/AArch64/hadd-combine.ll
index 2269d75cdbb9ed..65b7e7e8b5188c 100644
--- a/llvm/test/CodeGen/AArch64/hadd-combine.ll
+++ b/llvm/test/CodeGen/AArch64/hadd-combine.ll
@@ -464,8 +464,7 @@ define <8 x i16> @rhaddu_i_const_lhs(<8 x i16> %src1) {
define <8 x i16> @rhaddu_i_const_zero(<8 x i16> %src1) {
; CHECK-LABEL: rhaddu_i_const_zero:
; CHECK: // %bb.0:
-; CHECK-NEXT: movi v1.2d, #0000000000000000
-; CHECK-NEXT: urhadd v0.8h, v0.8h, v1.8h
+; CHECK-NEXT: ushr v0.8h, v0.8h, #1
; CHECK-NEXT: ret
%result = call <8 x i16> @llvm.aarch64.neon.urhadd.v8i16(<8 x i16> <i16 0, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0>, <8 x i16> %src1)
ret <8 x i16> %result
@@ -633,8 +632,7 @@ define <8 x i16> @rhadds_i_const_lhs(<8 x i16> %src1) {
define <8 x i16> @rhadds_i_const_zero(<8 x i16> %src1) {
; CHECK-LABEL: rhadds_i_const_zero:
; CHECK: // %bb.0:
-; CHECK-NEXT: movi v1.2d, #0000000000000000
-; CHECK-NEXT: srhadd v0.8h, v0.8h, v1.8h
+; CHECK-NEXT: sshr v0.8h, v0.8h, #1
; CHECK-NEXT: ret
%result = call <8 x i16> @llvm.aarch64.neon.srhadd.v8i16(<8 x i16> <i16 0, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0>, <8 x i16> %src1)
ret <8 x i16> %result
More information about the llvm-commits
mailing list