Skip to content

Commit 4bc96cb

Browse files
committed
[RISCV]DAG combine special vp.select to logic
vp.select Cond, 0, F, EVL --> and (not Cond), F, EVL vp.select Cond, T, 0, EVL --> and Cond, T, EVL vp.select Cond, 1, F, EVL --> or Cond, F, EVL vp.select Cond, T, 1, EVL --> or (not Cond), T, EVL
1 parent 86f0547 commit 4bc96cb

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1402,7 +1402,7 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
14021402
ISD::SHL, ISD::STORE, ISD::SPLAT_VECTOR,
14031403
ISD::BUILD_VECTOR, ISD::CONCAT_VECTORS,
14041404
ISD::EXPERIMENTAL_VP_REVERSE, ISD::MUL,
1405-
ISD::INSERT_VECTOR_ELT});
1405+
ISD::INSERT_VECTOR_ELT, ISD::VP_SELECT});
14061406
if (Subtarget.hasVendorXTHeadMemPair())
14071407
setTargetDAGCombine({ISD::LOAD, ISD::STORE});
14081408
if (Subtarget.useRVVForFixedLengthVectors())
@@ -15556,6 +15556,35 @@ SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N,
1555615556
}
1555715557
break;
1555815558
}
15559+
case ISD::VP_SELECT: {
15560+
EVT VT = N->getOperand(1).getValueType();
15561+
if (VT.isSimple() && VT.isScalableVector() &&
15562+
VT.getVectorElementType() == MVT::i1) {
15563+
SDValue N0 = N->getOperand(0);
15564+
SDValue N1 = N->getOperand(1);
15565+
SDValue N2 = N->getOperand(2);
15566+
SDValue VL = N->getOperand(3);
15567+
SDLoc DL(N);
15568+
// vp.select Cond, 0, F, EVL --> and (not Cond), F, EVL
15569+
// vp.select Cond, T, 0, EVL --> and Cond, T, EVL
15570+
// vp.select Cond, 1, F, EVL --> or Cond, F, EVL
15571+
// vp.select Cond, T, 1, EVL --> or (not Cond), T, EVL
15572+
if (isNullOrNullSplat(N1)) {
15573+
SDValue Not = DAG.getNode(RISCVISD::VMXOR_VL, DL, VT, N0,
15574+
DAG.getAllOnesConstant(DL, VT), VL);
15575+
return DAG.getNode(RISCVISD::VMAND_VL, DL, VT, Not, N2, VL);
15576+
} else if (isNullOrNullSplat(N2)) {
15577+
return DAG.getNode(RISCVISD::VMAND_VL, DL, VT, N0, N1, VL);
15578+
} else if (isOneOrOneSplat(N1)) {
15579+
return DAG.getNode(RISCVISD::VMOR_VL, DL, VT, N0, N1, VL);
15580+
} else if (isOneOrOneSplat(N2)) {
15581+
SDValue Not = DAG.getNode(RISCVISD::VMXOR_VL, DL, VT, N0,
15582+
DAG.getAllOnesConstant(DL, VT), VL);
15583+
return DAG.getNode(RISCVISD::VMOR_VL, DL, VT, Not, N1, VL);
15584+
}
15585+
}
15586+
return SDValue();
15587+
}
1555915588
case ISD::VP_GATHER: {
1556015589
const auto *VPGN = dyn_cast<VPGatherSDNode>(N);
1556115590
SDValue Index = VPGN->getIndex();

llvm/test/CodeGen/RISCV/rvv/vselect-vp.ll

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,3 +745,65 @@ define <vscale x 16 x double> @select_nxv16f64(<vscale x 16 x i1> %a, <vscale x
745745
%v = call <vscale x 16 x double> @llvm.vp.select.nxv16f64(<vscale x 16 x i1> %a, <vscale x 16 x double> %b, <vscale x 16 x double> %c, i32 %evl)
746746
ret <vscale x 16 x double> %v
747747
}
748+
749+
define <vscale x 2 x i1> @select_zero(<vscale x 2 x i1> %x, <vscale x 2 x i1> %y, <vscale x 2 x i1> %m, i32 zeroext %evl) {
750+
; CHECK-LABEL: select_zero:
751+
; CHECK: # %bb.0:
752+
; CHECK-NEXT: vsetvli zero, a0, e8, mf4, ta, ma
753+
; CHECK-NEXT: vmand.mm v0, v8, v0
754+
; CHECK-NEXT: ret
755+
%a = call <vscale x 2 x i1> @llvm.vp.select.nxv2i1(<vscale x 2 x i1> %x, <vscale x 2 x i1> %y, <vscale x 2 x i1> zeroinitializer, i32 %evl)
756+
ret <vscale x 2 x i1> %a
757+
}
758+
759+
define <vscale x 2 x i1> @select_one(<vscale x 2 x i1> %x, <vscale x 2 x i1> %y, <vscale x 2 x i1> %m, i32 zeroext %evl) {
760+
; CHECK-LABEL: select_one:
761+
; CHECK: # %bb.0:
762+
; CHECK-NEXT: vsetvli zero, a0, e8, mf4, ta, ma
763+
; CHECK-NEXT: vmorn.mm v0, v8, v0
764+
; CHECK-NEXT: ret
765+
%a = call <vscale x 2 x i1> @llvm.vp.select.nxv2i1(<vscale x 2 x i1> %x, <vscale x 2 x i1> %y, <vscale x 2 x i1> shufflevector (<vscale x 2 x i1> insertelement (<vscale x 2 x i1> undef, i1 true, i32 0), <vscale x 2 x i1> undef, <vscale x 2 x i32> zeroinitializer), i32 %evl)
766+
ret <vscale x 2 x i1> %a
767+
}
768+
769+
define <vscale x 2 x i1> @select_x_zero(<vscale x 2 x i1> %x, <vscale x 2 x i1> %y, i32 zeroext %evl) {
770+
; CHECK-LABEL: select_x_zero:
771+
; CHECK: # %bb.0:
772+
; CHECK-NEXT: vsetvli zero, a0, e8, mf4, ta, ma
773+
; CHECK-NEXT: vmand.mm v0, v0, v8
774+
; CHECK-NEXT: ret
775+
%a = call <vscale x 2 x i1> @llvm.vp.select.nxv2i1(<vscale x 2 x i1> %x, <vscale x 2 x i1> %y, <vscale x 2 x i1> zeroinitializer, i32 %evl)
776+
ret <vscale x 2 x i1> %a
777+
}
778+
779+
define <vscale x 2 x i1> @select_x_one(<vscale x 2 x i1> %x, <vscale x 2 x i1> %y, i32 zeroext %evl) {
780+
; CHECK-LABEL: select_x_one:
781+
; CHECK: # %bb.0:
782+
; CHECK-NEXT: vsetvli zero, a0, e8, mf4, ta, ma
783+
; CHECK-NEXT: vmorn.mm v0, v8, v0
784+
; CHECK-NEXT: ret
785+
%a = call <vscale x 2 x i1> @llvm.vp.select.nxv2i1(<vscale x 2 x i1> %x, <vscale x 2 x i1> %y, <vscale x 2 x i1> shufflevector (<vscale x 2 x i1> insertelement (<vscale x 2 x i1> undef, i1 true, i32 0), <vscale x 2 x i1> undef, <vscale x 2 x i32> zeroinitializer), i32 %evl)
786+
ret <vscale x 2 x i1> %a
787+
}
788+
789+
define <vscale x 2 x i1> @select_zero_x(<vscale x 2 x i1> %x, <vscale x 2 x i1> %y, i32 zeroext %evl) {
790+
; CHECK-LABEL: select_zero_x:
791+
; CHECK: # %bb.0:
792+
; CHECK-NEXT: vsetvli zero, a0, e8, mf4, ta, ma
793+
; CHECK-NEXT: vmandn.mm v0, v8, v0
794+
; CHECK-NEXT: ret
795+
%a = call <vscale x 2 x i1> @llvm.vp.select.nxv2i1(<vscale x 2 x i1> %x, <vscale x 2 x i1> zeroinitializer, <vscale x 2 x i1> %y, i32 %evl)
796+
ret <vscale x 2 x i1> %a
797+
}
798+
799+
define <vscale x 2 x i1> @select_one_x(<vscale x 2 x i1> %x, <vscale x 2 x i1> %y, i32 zeroext %evl) {
800+
; CHECK-LABEL: select_one_x:
801+
; CHECK: # %bb.0:
802+
; CHECK-NEXT: vsetvli a1, zero, e8, mf4, ta, ma
803+
; CHECK-NEXT: vmset.m v8
804+
; CHECK-NEXT: vsetvli zero, a0, e8, mf4, ta, ma
805+
; CHECK-NEXT: vmor.mm v0, v0, v8
806+
; CHECK-NEXT: ret
807+
%a = call <vscale x 2 x i1> @llvm.vp.select.nxv2i1(<vscale x 2 x i1> %x, <vscale x 2 x i1> shufflevector (<vscale x 2 x i1> insertelement (<vscale x 2 x i1> undef, i1 true, i32 0), <vscale x 2 x i1> undef, <vscale x 2 x i32> zeroinitializer), <vscale x 2 x i1> %y, i32 %evl)
808+
ret <vscale x 2 x i1> %a
809+
}

0 commit comments

Comments
 (0)