Skip to content

Commit 169c205

Browse files
authored
[RISCV] Relax some Zvbb patterns and lowerings to Zvkb (llvm#68115)
vandn, vrev8 and vro{l,r} are now part of Zvkb, which Zvbb now implies. This patch updates the predicates to check for Zvkb instead of Zvbb in the tablegen patterns for the SD and VL nodes, as well as some of the lowering logic in RISCVISelLowering.
1 parent 4c37372 commit 169c205

12 files changed

+3037
-3032
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -779,15 +779,22 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
779779
// Splice
780780
setOperationAction(ISD::VECTOR_SPLICE, VT, Custom);
781781

782+
if (Subtarget.hasStdExtZvkb()) {
783+
setOperationAction(ISD::BSWAP, VT, Legal);
784+
setOperationAction(ISD::VP_BSWAP, VT, Custom);
785+
} else {
786+
setOperationAction({ISD::BSWAP, ISD::VP_BSWAP}, VT, Expand);
787+
setOperationAction({ISD::ROTL, ISD::ROTR}, VT, Expand);
788+
}
789+
782790
if (Subtarget.hasStdExtZvbb()) {
783-
setOperationAction({ISD::BITREVERSE, ISD::BSWAP}, VT, Legal);
784-
setOperationAction({ISD::VP_BITREVERSE, ISD::VP_BSWAP}, VT, Custom);
791+
setOperationAction(ISD::BITREVERSE, VT, Legal);
792+
setOperationAction(ISD::VP_BITREVERSE, VT, Custom);
785793
setOperationAction({ISD::VP_CTLZ, ISD::VP_CTLZ_ZERO_UNDEF, ISD::VP_CTTZ,
786794
ISD::VP_CTTZ_ZERO_UNDEF, ISD::VP_CTPOP},
787795
VT, Custom);
788796
} else {
789-
setOperationAction({ISD::BITREVERSE, ISD::BSWAP}, VT, Expand);
790-
setOperationAction({ISD::VP_BITREVERSE, ISD::VP_BSWAP}, VT, Expand);
797+
setOperationAction({ISD::BITREVERSE, ISD::VP_BITREVERSE}, VT, Expand);
791798
setOperationAction({ISD::CTLZ, ISD::CTTZ, ISD::CTPOP}, VT, Expand);
792799
setOperationAction({ISD::VP_CTLZ, ISD::VP_CTLZ_ZERO_UNDEF, ISD::VP_CTTZ,
793800
ISD::VP_CTTZ_ZERO_UNDEF, ISD::VP_CTPOP},
@@ -802,8 +809,6 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
802809
ISD::VP_CTLZ_ZERO_UNDEF, ISD::VP_CTTZ_ZERO_UNDEF},
803810
VT, Custom);
804811
}
805-
806-
setOperationAction({ISD::ROTL, ISD::ROTR}, VT, Expand);
807812
}
808813
}
809814

@@ -1109,11 +1114,12 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
11091114

11101115
setOperationAction(IntegerVPOps, VT, Custom);
11111116

1117+
if (Subtarget.hasStdExtZvkb())
1118+
setOperationAction({ISD::BSWAP, ISD::ROTL, ISD::ROTR}, VT, Custom);
1119+
11121120
if (Subtarget.hasStdExtZvbb()) {
1113-
setOperationAction({ISD::BITREVERSE, ISD::BSWAP, ISD::CTLZ,
1114-
ISD::CTLZ_ZERO_UNDEF, ISD::CTTZ,
1115-
ISD::CTTZ_ZERO_UNDEF, ISD::CTPOP, ISD::ROTL,
1116-
ISD::ROTR},
1121+
setOperationAction({ISD::BITREVERSE, ISD::CTLZ, ISD::CTLZ_ZERO_UNDEF,
1122+
ISD::CTTZ, ISD::CTTZ_ZERO_UNDEF, ISD::CTPOP},
11171123
VT, Custom);
11181124
} else {
11191125
// Lower CTLZ_ZERO_UNDEF and CTTZ_ZERO_UNDEF if element of VT in the
@@ -4376,7 +4382,7 @@ static SDValue lowerBitreverseShuffle(ShuffleVectorSDNode *SVN,
43764382

43774383
// Given a shuffle mask like <3, 0, 1, 2, 7, 4, 5, 6> for v8i8, we can
43784384
// reinterpret it as a v2i32 and rotate it right by 8 instead. We can lower this
4379-
// as a vror.vi if we have zvbb, or otherwise as a vsll, vsrl and vor.
4385+
// as a vror.vi if we have Zvkb, or otherwise as a vsll, vsrl and vor.
43804386
static SDValue lowerVECTOR_SHUFFLEAsRotate(ShuffleVectorSDNode *SVN,
43814387
SelectionDAG &DAG,
43824388
const RISCVSubtarget &Subtarget) {
@@ -4527,9 +4533,9 @@ static SDValue lowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG,
45274533
lowerVECTOR_SHUFFLEAsVSlidedown(DL, VT, V1, V2, Mask, Subtarget, DAG))
45284534
return V;
45294535

4530-
// A bitrotate will be one instruction on zvbb, so try to lower to it first if
4536+
// A bitrotate will be one instruction on Zvkb, so try to lower to it first if
45314537
// available.
4532-
if (Subtarget.hasStdExtZvbb())
4538+
if (Subtarget.hasStdExtZvkb())
45334539
if (SDValue V = lowerVECTOR_SHUFFLEAsRotate(SVN, DAG, Subtarget))
45344540
return V;
45354541

@@ -4660,7 +4666,7 @@ static SDValue lowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG,
46604666
return DAG.getNode(ISD::VSELECT, DL, VT, SelectMask, V1, V2);
46614667

46624668
// We might be able to express the shuffle as a bitrotate. But even if we
4663-
// don't have zvbb and have to expand, the expanded sequence of approx. 2
4669+
// don't have Zvkb and have to expand, the expanded sequence of approx. 2
46644670
// shifts and a vor will have a higher throughput than a vrgather.
46654671
if (SDValue V = lowerVECTOR_SHUFFLEAsRotate(SVN, DAG, Subtarget))
46664672
return V;
@@ -5482,7 +5488,7 @@ SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
54825488
case ISD::ROTL:
54835489
case ISD::ROTR:
54845490
if (Op.getValueType().isFixedLengthVector()) {
5485-
assert(Subtarget.hasStdExtZvbb());
5491+
assert(Subtarget.hasStdExtZvkb());
54865492
return lowerToScalableOp(Op, DAG);
54875493
}
54885494
assert(Subtarget.hasVendorXTHeadBb() &&

llvm/lib/Target/RISCV/RISCVInstrInfoZvk.td

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,10 @@ let Predicates = [HasStdExtZvksh] in {
410410
// SDNode patterns
411411
//===----------------------------------------------------------------------===//
412412

413-
multiclass VPatUnarySDNode_V<SDPatternOperator op, string instruction_name> {
413+
multiclass VPatUnarySDNode_V<SDPatternOperator op, string instruction_name,
414+
Predicate predicate = HasStdExtZvbb> {
414415
foreach vti = AllIntegerVectors in {
415-
let Predicates = !listconcat([HasStdExtZvbb],
416+
let Predicates = !listconcat([predicate],
416417
GetVTypePredicates<vti>.Predicates) in {
417418
def : Pat<(vti.Vector (op (vti.Vector vti.RegClass:$rs1))),
418419
(!cast<Instruction>(instruction_name#"_V_"#vti.LMul.MX)
@@ -431,7 +432,7 @@ def riscv_vnot : PatFrag<(ops node:$rs1), (xor node:$rs1,
431432
(riscv_splat_vector -1))>;
432433

433434
foreach vti = AllIntegerVectors in {
434-
let Predicates = !listconcat([HasStdExtZvbb],
435+
let Predicates = !listconcat([HasStdExtZvkb],
435436
GetVTypePredicates<vti>.Predicates) in {
436437
def : Pat<(vti.Vector (and (riscv_vnot vti.RegClass:$rs1),
437438
vti.RegClass:$rs2)),
@@ -452,7 +453,7 @@ foreach vti = AllIntegerVectors in {
452453
}
453454

454455
defm : VPatUnarySDNode_V<bitreverse, "PseudoVBREV">;
455-
defm : VPatUnarySDNode_V<bswap, "PseudoVREV8">;
456+
defm : VPatUnarySDNode_V<bswap, "PseudoVREV8", HasStdExtZvkb>;
456457
defm : VPatUnarySDNode_V<ctlz, "PseudoVCLZ">;
457458
defm : VPatUnarySDNode_V<cttz, "PseudoVCTZ">;
458459
defm : VPatUnarySDNode_V<ctpop, "PseudoVCPOP">;
@@ -480,7 +481,7 @@ def InvRot64Imm : SDNodeXForm<imm, [{
480481
// Although there is no vrol.vi, an immediate rotate left can be achieved by
481482
// negating the immediate in vror.vi
482483
foreach vti = AllIntegerVectors in {
483-
let Predicates = !listconcat([HasStdExtZvbb],
484+
let Predicates = !listconcat([HasStdExtZvkb],
484485
GetVTypePredicates<vti>.Predicates) in {
485486
def : Pat<(vti.Vector (rotl vti.RegClass:$rs2,
486487
(vti.Vector (SplatPat_uimm6 uimm6:$rs1)))),
@@ -526,9 +527,10 @@ foreach vtiToWti = AllWidenableIntVectors in {
526527
// VL patterns
527528
//===----------------------------------------------------------------------===//
528529

529-
multiclass VPatUnaryVL_V<SDPatternOperator op, string instruction_name> {
530+
multiclass VPatUnaryVL_V<SDPatternOperator op, string instruction_name,
531+
Predicate predicate = HasStdExtZvbb> {
530532
foreach vti = AllIntegerVectors in {
531-
let Predicates = !listconcat([HasStdExtZvbb],
533+
let Predicates = !listconcat([predicate],
532534
GetVTypePredicates<vti>.Predicates) in {
533535
def : Pat<(vti.Vector (op (vti.Vector vti.RegClass:$rs1),
534536
(vti.Vector vti.RegClass:$merge),
@@ -546,7 +548,7 @@ multiclass VPatUnaryVL_V<SDPatternOperator op, string instruction_name> {
546548
}
547549

548550
foreach vti = AllIntegerVectors in {
549-
let Predicates = !listconcat([HasStdExtZvbb],
551+
let Predicates = !listconcat([HasStdExtZvkb],
550552
GetVTypePredicates<vti>.Predicates) in {
551553
def : Pat<(vti.Vector (riscv_and_vl (riscv_xor_vl
552554
(vti.Vector vti.RegClass:$rs1),
@@ -585,7 +587,7 @@ foreach vti = AllIntegerVectors in {
585587
}
586588

587589
defm : VPatUnaryVL_V<riscv_bitreverse_vl, "PseudoVBREV">;
588-
defm : VPatUnaryVL_V<riscv_bswap_vl, "PseudoVREV8">;
590+
defm : VPatUnaryVL_V<riscv_bswap_vl, "PseudoVREV8", HasStdExtZvkb>;
589591
defm : VPatUnaryVL_V<riscv_ctlz_vl, "PseudoVCLZ">;
590592
defm : VPatUnaryVL_V<riscv_cttz_vl, "PseudoVCTZ">;
591593
defm : VPatUnaryVL_V<riscv_ctpop_vl, "PseudoVCPOP">;
@@ -594,7 +596,7 @@ defm : VPatBinaryVL_VV_VX<riscv_rotl_vl, "PseudoVROL">;
594596
// Although there is no vrol.vi, an immediate rotate left can be achieved by
595597
// negating the immediate in vror.vi
596598
foreach vti = AllIntegerVectors in {
597-
let Predicates = !listconcat([HasStdExtZvbb],
599+
let Predicates = !listconcat([HasStdExtZvkb],
598600
GetVTypePredicates<vti>.Predicates) in {
599601
def : Pat<(riscv_rotl_vl vti.RegClass:$rs2,
600602
(vti.Vector (SplatPat_uimm6 uimm6:$rs1)),

0 commit comments

Comments
 (0)