Skip to content

Commit 458ed5f

Browse files
committed
[TargetLowering][RISCV] Prevent scalarization of fixed vector bswap.
It's better to do the ands, shifts, ors in the vector domain than to scalarize it and do those operations on each element. Reviewed By: RKSimon Differential Revision: https://reviews.llvm.org/D112248
1 parent 5c0369e commit 458ed5f

File tree

2 files changed

+518
-2105
lines changed

2 files changed

+518
-2105
lines changed

llvm/lib/CodeGen/SelectionDAG/LegalizeVectorOps.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,13 +1101,23 @@ SDValue VectorLegalizer::ExpandBSWAP(SDNode *Node) {
11011101
EVT ByteVT = EVT::getVectorVT(*DAG.getContext(), MVT::i8, ShuffleMask.size());
11021102

11031103
// Only emit a shuffle if the mask is legal.
1104-
if (!TLI.isShuffleMaskLegal(ShuffleMask, ByteVT))
1105-
return DAG.UnrollVectorOp(Node);
1104+
if (TLI.isShuffleMaskLegal(ShuffleMask, ByteVT)) {
1105+
SDLoc DL(Node);
1106+
SDValue Op = DAG.getNode(ISD::BITCAST, DL, ByteVT, Node->getOperand(0));
1107+
Op = DAG.getVectorShuffle(ByteVT, DL, Op, DAG.getUNDEF(ByteVT), ShuffleMask);
1108+
return DAG.getNode(ISD::BITCAST, DL, VT, Op);
1109+
}
11061110

1107-
SDLoc DL(Node);
1108-
SDValue Op = DAG.getNode(ISD::BITCAST, DL, ByteVT, Node->getOperand(0));
1109-
Op = DAG.getVectorShuffle(ByteVT, DL, Op, DAG.getUNDEF(ByteVT), ShuffleMask);
1110-
return DAG.getNode(ISD::BITCAST, DL, VT, Op);
1111+
// If we have the appropriate vector bit operations, it is better to use them
1112+
// than unrolling and expanding each component.
1113+
if (TLI.isOperationLegalOrCustom(ISD::SHL, VT) &&
1114+
TLI.isOperationLegalOrCustom(ISD::SRL, VT) &&
1115+
TLI.isOperationLegalOrCustomOrPromote(ISD::AND, VT) &&
1116+
TLI.isOperationLegalOrCustomOrPromote(ISD::OR, VT))
1117+
return TLI.expandBSWAP(Node, DAG);
1118+
1119+
// Otherwise unroll.
1120+
return DAG.UnrollVectorOp(Node);
11111121
}
11121122

11131123
void VectorLegalizer::ExpandBITREVERSE(SDNode *Node,

0 commit comments

Comments
 (0)