Skip to content

Commit a43ed49

Browse files
committed
[DAGCombiner][RISCV] Canonicalize (bswap(bitreverse(x))->bitreverse(bswap(x)).
If the bitreverse gets expanded, it will introduce a new bswap. By putting a bswap before the bitreverse, we can ensure it gets cancelled out when this happens. Reviewed By: RKSimon Differential Revision: https://reviews.llvm.org/D118012
1 parent b00ee46 commit a43ed49

File tree

4 files changed

+196
-479
lines changed

4 files changed

+196
-479
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9375,6 +9375,17 @@ SDValue DAGCombiner::visitBSWAP(SDNode *N) {
93759375
// fold (bswap (bswap x)) -> x
93769376
if (N0.getOpcode() == ISD::BSWAP)
93779377
return N0->getOperand(0);
9378+
9379+
// Canonicalize bswap(bitreverse(x)) -> bitreverse(bswap(x)). If bitreverse
9380+
// isn't supported, it will be expanded to bswap followed by a manual reversal
9381+
// of bits in each byte. By placing bswaps before bitreverse, we can remove
9382+
// the two bswaps if the bitreverse gets expanded.
9383+
if (N0.getOpcode() == ISD::BITREVERSE && N0.hasOneUse()) {
9384+
SDLoc DL(N);
9385+
SDValue BSwap = DAG.getNode(ISD::BSWAP, DL, VT, N0.getOperand(0));
9386+
return DAG.getNode(ISD::BITREVERSE, DL, VT, BSwap);
9387+
}
9388+
93789389
return SDValue();
93799390
}
93809391

0 commit comments

Comments
 (0)