Skip to content

Commit 9f369a4

Browse files
committed
[RISCV] Lower reverse shuffles of fixed i1 vectors to vbrev.v
If we can fit an entire vector of i1 into a single element, e.g. v32i1 -> v1i32, then we can reverse it via vbrev.v. We need to handle the case where the vector doesn't exactly fit into the larger element type, e.g. v4i1 -> v1i8. In this case we shift up the reversed bits afterwards. Reviewed By: fakepaper56, 4vtomat Differential Revision: https://reviews.llvm.org/D157614
1 parent 58fd1de commit 9f369a4

File tree

2 files changed

+340
-96
lines changed

2 files changed

+340
-96
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4118,6 +4118,56 @@ static SDValue getWideningInterleave(SDValue EvenV, SDValue OddV,
41184118
return Interleaved;
41194119
}
41204120

4121+
// If we have a vector of bits that we want to reverse, we can use a vbrev on a
4122+
// larger element type, e.g. v32i1 can be reversed with a v1i32 bitreverse.
4123+
static SDValue lowerBitreverseShuffle(ShuffleVectorSDNode *SVN,
4124+
SelectionDAG &DAG,
4125+
const RISCVSubtarget &Subtarget) {
4126+
SDLoc DL(SVN);
4127+
MVT VT = SVN->getSimpleValueType(0);
4128+
SDValue V = SVN->getOperand(0);
4129+
unsigned NumElts = VT.getVectorNumElements();
4130+
4131+
assert(VT.getVectorElementType() == MVT::i1);
4132+
4133+
if (!ShuffleVectorInst::isReverseMask(SVN->getMask()) ||
4134+
!SVN->getOperand(1).isUndef())
4135+
return SDValue();
4136+
4137+
unsigned ViaEltSize = std::max((uint64_t)8, PowerOf2Ceil(NumElts));
4138+
MVT ViaVT = MVT::getVectorVT(MVT::getIntegerVT(ViaEltSize), 1);
4139+
MVT ViaBitVT = MVT::getVectorVT(MVT::i1, ViaVT.getScalarSizeInBits());
4140+
4141+
// If we don't have zvbb or the larger element type > ELEN, the operation will
4142+
// be illegal.
4143+
if (!Subtarget.getTargetLowering()->isOperationLegalOrCustom(ISD::BITREVERSE,
4144+
ViaVT))
4145+
return SDValue();
4146+
4147+
// If the bit vector doesn't fit exactly into the larger element type, we need
4148+
// to insert it into the larger vector and then shift up the reversed bits
4149+
// afterwards to get rid of the gap introduced.
4150+
if (ViaEltSize > NumElts)
4151+
V = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, ViaBitVT, DAG.getUNDEF(ViaBitVT),
4152+
V, DAG.getVectorIdxConstant(0, DL));
4153+
4154+
SDValue Res =
4155+
DAG.getNode(ISD::BITREVERSE, DL, ViaVT, DAG.getBitcast(ViaVT, V));
4156+
4157+
// Shift up the reversed bits if the vector didn't exactly fit into the larger
4158+
// element type.
4159+
if (ViaEltSize > NumElts)
4160+
Res = DAG.getNode(ISD::SRL, DL, ViaVT, Res,
4161+
DAG.getConstant(ViaEltSize - NumElts, DL, ViaVT));
4162+
4163+
Res = DAG.getBitcast(ViaBitVT, Res);
4164+
4165+
if (ViaEltSize > NumElts)
4166+
Res = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Res,
4167+
DAG.getVectorIdxConstant(0, DL));
4168+
return Res;
4169+
}
4170+
41214171
static SDValue lowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG,
41224172
const RISCVSubtarget &Subtarget) {
41234173
SDValue V1 = Op.getOperand(0);
@@ -4128,8 +4178,11 @@ static SDValue lowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG,
41284178
unsigned NumElts = VT.getVectorNumElements();
41294179
ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
41304180

4131-
// Promote i1 shuffle to i8 shuffle.
41324181
if (VT.getVectorElementType() == MVT::i1) {
4182+
if (SDValue V = lowerBitreverseShuffle(SVN, DAG, Subtarget))
4183+
return V;
4184+
4185+
// Promote i1 shuffle to i8 shuffle.
41334186
MVT WidenVT = MVT::getVectorVT(MVT::i8, VT.getVectorElementCount());
41344187
V1 = DAG.getNode(ISD::ZERO_EXTEND, DL, WidenVT, V1);
41354188
V2 = V2.isUndef() ? DAG.getUNDEF(WidenVT)

0 commit comments

Comments
 (0)