[llvm] [PowerPC] Optimize vector compares not equal to non-zero elements (PR #174361)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 5 00:38:39 PST 2026
https://github.com/Himadhith updated https://github.com/llvm/llvm-project/pull/174361
>From 5b1d23610565a10ad7fee2600d994414879fa602 Mon Sep 17 00:00:00 2001
From: himadhith <himadhith.v at ibm.com>
Date: Mon, 5 Jan 2026 00:38:29 -0500
Subject: [PATCH] [PowerPC] Optimize vector comapres not equal to non-zeros
---
llvm/lib/Target/PowerPC/PPCISelLowering.cpp | 49 +++++++++++++++++++
.../PowerPC/optimize-vector-not-equal.ll | 21 ++++----
2 files changed, 58 insertions(+), 12 deletions(-)
diff --git a/llvm/lib/Target/PowerPC/PPCISelLowering.cpp b/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
index 21297b812968c..841c7c2a2bc8b 100644
--- a/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
+++ b/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
@@ -17135,6 +17135,51 @@ static SDValue DAGCombineAddc(SDNode *N,
return SDValue();
}
+// The following transformation is meant to optimize vector compares not equal
+// to non-zeros Ex: vec[i] != 7, where vec is a vector and the comparison is
+// element non-zero number. (and (anyext (setcc A, B, SETNE)), splat32(1))
+// -> (zext (add (setcc A, B, SETEQ), splat16(1)))
+static SDValue combineVectorZExtCompare(SDNode *N, SelectionDAG &DAG,
+ const PPCSubtarget &Subtarget) {
+ if (!Subtarget.hasVSX() || N->getOpcode() != ISD::AND)
+ return SDValue();
+
+ SDLoc DL(N);
+ EVT VT = N->getValueType(0);
+ SDValue Op0 = N->getOperand(0);
+ SDValue Op1 = N->getOperand(1);
+
+ APInt SplatVal;
+ if (!ISD::isConstantSplatVector(Op1.getNode(), SplatVal) || SplatVal != 1)
+ std::swap(Op0, Op1);
+ if (!ISD::isConstantSplatVector(Op1.getNode(), SplatVal) || SplatVal != 1)
+ return SDValue();
+
+ if (Op0.getOpcode() != ISD::ANY_EXTEND_VECTOR_INREG &&
+ Op0.getOpcode() != ISD::ZERO_EXTEND_VECTOR_INREG)
+ return SDValue();
+
+ SDValue SetCC = Op0.getOperand(0);
+ if (SetCC.getOpcode() != ISD::SETCC)
+ return SDValue();
+
+ ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
+ if (CC != ISD::SETNE)
+ return SDValue();
+
+ EVT SrcVT = SetCC.getValueType();
+ EVT EltVT = SrcVT.getVectorElementType();
+
+ //(zext (add (setcc X, Y, SETEQ), splat(1)))
+ SDValue EqCmp = DAG.getSetCC(DL, SrcVT, SetCC.getOperand(0),
+ SetCC.getOperand(1), ISD::SETEQ);
+ SDValue OneSplat =
+ DAG.getSplatBuildVector(SrcVT, DL, DAG.getConstant(1, DL, EltVT));
+ SDValue Add = DAG.getNode(ISD::ADD, DL, SrcVT, EqCmp, OneSplat);
+
+ return DAG.getNode(ISD::ZERO_EXTEND_VECTOR_INREG, DL, VT, Add);
+}
+
SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
DAGCombinerInfo &DCI) const {
SelectionDAG &DAG = DCI.DAG;
@@ -17144,6 +17189,10 @@ SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
case ISD::ADD:
return combineADD(N, DCI);
case ISD::AND: {
+ // Convert (and (anyext (setcc A, B, SETNE)), splat32(1))
+ // -> (zext (add (setcc A, B, SETEQ), splat16(1)))
+ if (SDValue V = combineVectorZExtCompare(N, DAG, Subtarget))
+ return V;
// We don't want (and (zext (shift...)), C) if C fits in the width of the
// original input as that will prevent us from selecting optimal rotates.
// This only matters if the input to the extend is i32 widened to i64.
diff --git a/llvm/test/CodeGen/PowerPC/optimize-vector-not-equal.ll b/llvm/test/CodeGen/PowerPC/optimize-vector-not-equal.ll
index bfb9ab3356f48..67fbd8028925c 100644
--- a/llvm/test/CodeGen/PowerPC/optimize-vector-not-equal.ll
+++ b/llvm/test/CodeGen/PowerPC/optimize-vector-not-equal.ll
@@ -17,12 +17,11 @@ define i32 @cols_needed(<4 x i16> %wide.load) {
; POWERPC_64LE-LABEL: cols_needed:
; POWERPC_64LE: # %bb.0: # %entry
; POWERPC_64LE-NEXT: xxlxor v3, v3, v3
+; POWERPC_64LE-NEXT: xxleqv v4, v4, v4
; POWERPC_64LE-NEXT: li r3, 0
; POWERPC_64LE-NEXT: vcmpequh v2, v2, v3
-; POWERPC_64LE-NEXT: vspltisw v3, 1
-; POWERPC_64LE-NEXT: xxlnor v2, v2, v2
-; POWERPC_64LE-NEXT: vmrglh v2, v2, v2
-; POWERPC_64LE-NEXT: xxland v2, v2, v3
+; POWERPC_64LE-NEXT: vsubuhm v2, v2, v4
+; POWERPC_64LE-NEXT: vmrglh v2, v3, v2
; POWERPC_64LE-NEXT: xxswapd v3, v2
; POWERPC_64LE-NEXT: vadduwm v2, v2, v3
; POWERPC_64LE-NEXT: xxspltw v3, v2, 2
@@ -33,12 +32,11 @@ define i32 @cols_needed(<4 x i16> %wide.load) {
; POWERPC_64-LABEL: cols_needed:
; POWERPC_64: # %bb.0: # %entry
; POWERPC_64-NEXT: xxlxor v3, v3, v3
+; POWERPC_64-NEXT: xxleqv v4, v4, v4
; POWERPC_64-NEXT: li r3, 0
; POWERPC_64-NEXT: vcmpequh v2, v2, v3
-; POWERPC_64-NEXT: vspltisw v3, 1
-; POWERPC_64-NEXT: xxlnor v2, v2, v2
-; POWERPC_64-NEXT: vmrghh v2, v2, v2
-; POWERPC_64-NEXT: xxland v2, v2, v3
+; POWERPC_64-NEXT: vsubuhm v2, v2, v4
+; POWERPC_64-NEXT: vmrghh v2, v3, v2
; POWERPC_64-NEXT: xxswapd v3, v2
; POWERPC_64-NEXT: vadduwm v2, v2, v3
; POWERPC_64-NEXT: xxspltw v3, v2, 1
@@ -49,11 +47,10 @@ define i32 @cols_needed(<4 x i16> %wide.load) {
; POWERPC_32-LABEL: cols_needed:
; POWERPC_32: # %bb.0: # %entry
; POWERPC_32-NEXT: xxlxor v3, v3, v3
+; POWERPC_32-NEXT: xxleqv v4, v4, v4
; POWERPC_32-NEXT: vcmpequh v2, v2, v3
-; POWERPC_32-NEXT: vspltisw v3, 1
-; POWERPC_32-NEXT: xxlnor v2, v2, v2
-; POWERPC_32-NEXT: vmrghh v2, v2, v2
-; POWERPC_32-NEXT: xxland v2, v2, v3
+; POWERPC_32-NEXT: vsubuhm v2, v2, v4
+; POWERPC_32-NEXT: vmrghh v2, v3, v2
; POWERPC_32-NEXT: xxswapd v3, v2
; POWERPC_32-NEXT: vadduwm v2, v2, v3
; POWERPC_32-NEXT: xxspltw v3, v2, 1
More information about the llvm-commits
mailing list