[llvm] [LoongArch][DAG] Custom lowering for vector SETCC operations (PR #177904)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Jan 25 23:25:30 PST 2026
https://github.com/woruyu created https://github.com/llvm/llvm-project/pull/177904
### Summary
This PR resolves https://github.com/llvm/llvm-project/issues/177863
>From da1366ab65157cc30833632c160b9e00ae31860a Mon Sep 17 00:00:00 2001
From: woruyu <1214539920 at qq.com>
Date: Mon, 26 Jan 2026 15:22:07 +0800
Subject: [PATCH] [LoongArch][DAG] Custom lowering for vector SETCC operations
---
.../LoongArch/LoongArchISelLowering.cpp | 38 ++++++++++++++-
.../Target/LoongArch/LoongArchISelLowering.h | 1 +
llvm/test/CodeGen/LoongArch/setcc.ll | 47 +++++++++++++++++++
3 files changed, 84 insertions(+), 2 deletions(-)
create mode 100644 llvm/test/CodeGen/LoongArch/setcc.ll
diff --git a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
index 6cb0983780d9f..a45928a03ccf0 100644
--- a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
+++ b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
@@ -322,7 +322,7 @@ LoongArchTargetLowering::LoongArchTargetLowering(const TargetMachine &TM,
setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Legal);
setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
- setOperationAction(ISD::SETCC, VT, Legal);
+ setOperationAction(ISD::SETCC, VT, Custom);
setOperationAction(ISD::VSELECT, VT, Legal);
setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
@@ -411,7 +411,7 @@ LoongArchTargetLowering::LoongArchTargetLowering(const TargetMachine &TM,
setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
setOperationAction(ISD::INSERT_SUBVECTOR, VT, Legal);
- setOperationAction(ISD::SETCC, VT, Legal);
+ setOperationAction(ISD::SETCC, VT, Custom);
setOperationAction(ISD::VSELECT, VT, Legal);
setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
}
@@ -613,6 +613,8 @@ SDValue LoongArchTargetLowering::LowerOperation(SDValue Op,
return lowerVECREDUCE(Op, DAG);
case ISD::ConstantFP:
return lowerConstantFP(Op, DAG);
+ case ISD::SETCC:
+ return lowerSETCC(Op, DAG);
}
return SDValue();
}
@@ -725,6 +727,38 @@ SDValue LoongArchTargetLowering::lowerConstantFP(SDValue Op,
return SDValue();
}
+SDValue LoongArchTargetLowering::lowerSETCC(SDValue Op,
+ SelectionDAG &DAG) const {
+ SDLoc DL(Op);
+ EVT ResultVT = Op.getValueType();
+ EVT OperandVT = Op.getOperand(0).getValueType();
+
+ EVT SetCCResultVT =
+ getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), OperandVT);
+ // Check if the result type and operand type are already consistent
+ if (ResultVT == SetCCResultVT) {
+ return Op;
+ }
+
+ // Ensure operands have the same type
+ assert(Op.getOperand(0).getValueType() == Op.getOperand(1).getValueType() &&
+ "SETCC operands must have the same type!");
+
+ // Create the SETCC node with the result type derived from the operand type
+ SDValue SetCCNode =
+ DAG.getNode(ISD::SETCC, DL, SetCCResultVT, Op.getOperand(0),
+ Op.getOperand(1), Op.getOperand(2));
+
+ // Otherwise, extend or truncate the result to match the desired result type
+ if (ResultVT.bitsGT(SetCCResultVT)) {
+ SetCCNode = DAG.getNode(ISD::ANY_EXTEND, DL, ResultVT, SetCCNode);
+ } else if (ResultVT.bitsLT(SetCCResultVT)) {
+ SetCCNode = DAG.getNode(ISD::TRUNCATE, DL, ResultVT, SetCCNode);
+ }
+
+ return SetCCNode;
+}
+
// Lower vecreduce_add using vhaddw instructions.
// For Example:
// call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %a)
diff --git a/llvm/lib/Target/LoongArch/LoongArchISelLowering.h b/llvm/lib/Target/LoongArch/LoongArchISelLowering.h
index 74d2b7dff0db5..126ea055829eb 100644
--- a/llvm/lib/Target/LoongArch/LoongArchISelLowering.h
+++ b/llvm/lib/Target/LoongArch/LoongArchISelLowering.h
@@ -239,6 +239,7 @@ class LoongArchTargetLowering : public TargetLowering {
SDValue lowerVECREDUCE_ADD(SDValue Op, SelectionDAG &DAG) const;
SDValue lowerVECREDUCE(SDValue Op, SelectionDAG &DAG) const;
SDValue lowerConstantFP(SDValue Op, SelectionDAG &DAG) const;
+ SDValue lowerSETCC(SDValue Op, SelectionDAG &DAG) const;
SDValue lowerRotate(SDValue Op, SelectionDAG &DAG) const;
bool isFPImmLegal(const APFloat &Imm, EVT VT,
diff --git a/llvm/test/CodeGen/LoongArch/setcc.ll b/llvm/test/CodeGen/LoongArch/setcc.ll
new file mode 100644
index 0000000000000..112c9333228c0
--- /dev/null
+++ b/llvm/test/CodeGen/LoongArch/setcc.ll
@@ -0,0 +1,47 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+
+; RUN: llc --mtriple=loongarch32 -mattr=+d --verify-machineinstrs < %s | FileCheck %s --check-prefix=LA32
+; RUN: llc --mtriple=loongarch64 -mattr=+d --verify-machineinstrs < %s | FileCheck %s --check-prefix=LA64
+
+define <4 x i1> @backsmith_pure_1(<4 x i64> %shuffle2, <4 x i64> %shuffle4) #0 {
+; LA32-LABEL: backsmith_pure_1:
+; LA32: # %bb.0: # %entry
+; LA32-NEXT: ld.w $a3, $a1, 8
+; LA32-NEXT: ld.w $a4, $a2, 8
+; LA32-NEXT: ld.w $a5, $a1, 0
+; LA32-NEXT: ld.w $a6, $a1, 24
+; LA32-NEXT: ld.w $a7, $a2, 24
+; LA32-NEXT: ld.w $a1, $a1, 16
+; LA32-NEXT: ld.w $t0, $a2, 16
+; LA32-NEXT: ld.w $a2, $a2, 0
+; LA32-NEXT: xor $a6, $a7, $a6
+; LA32-NEXT: sltu $a6, $zero, $a6
+; LA32-NEXT: xor $a1, $t0, $a1
+; LA32-NEXT: sltu $a1, $zero, $a1
+; LA32-NEXT: xor $a2, $a2, $a5
+; LA32-NEXT: sltu $a2, $zero, $a2
+; LA32-NEXT: xor $a3, $a4, $a3
+; LA32-NEXT: sltu $a3, $zero, $a3
+; LA32-NEXT: slli.w $a3, $a3, 1
+; LA32-NEXT: or $a2, $a2, $a3
+; LA32-NEXT: slli.w $a1, $a1, 2
+; LA32-NEXT: or $a1, $a2, $a1
+; LA32-NEXT: slli.w $a2, $a6, 3
+; LA32-NEXT: or $a1, $a1, $a2
+; LA32-NEXT: st.b $a1, $a0, 0
+; LA32-NEXT: ret
+;
+; LA64-LABEL: backsmith_pure_1:
+; LA64: # %bb.0: # %entry
+; LA64-NEXT: vpickev.w $vr2, $vr3, $vr2
+; LA64-NEXT: vpickev.w $vr0, $vr1, $vr0
+; LA64-NEXT: vseq.w $vr0, $vr2, $vr0
+; LA64-NEXT: vrepli.b $vr1, -1
+; LA64-NEXT: vxor.v $vr0, $vr0, $vr1
+; LA64-NEXT: ret
+entry:
+ %conv5 = trunc nuw <4 x i64> %shuffle4 to <4 x i32>
+ %conv3 = trunc nuw <4 x i64> %shuffle2 to <4 x i32>
+ %cmp = icmp ne <4 x i32> %conv5, %conv3
+ ret <4 x i1> %cmp
+}
More information about the llvm-commits
mailing list