Skip to content

Commit 3210ce2

Browse files
committed
[X86] Fold (iX bitreverse(bitcast(vXi1 X))) -> (iX bitcast(shuffle(X)))
X86 doesn't have a BITREVERSE instruction, so if we're working with a casted boolean vector, we're better off shuffling the vector instead if we have PSHUFB (SSSE3 or later) Fixes #77459
1 parent 144ae5b commit 3210ce2

File tree

2 files changed

+245
-303
lines changed

2 files changed

+245
-303
lines changed

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,6 +2444,7 @@ X86TargetLowering::X86TargetLowering(const X86TargetMachine &TM,
24442444
ISD::SRL,
24452445
ISD::OR,
24462446
ISD::AND,
2447+
ISD::BITREVERSE,
24472448
ISD::ADD,
24482449
ISD::FADD,
24492450
ISD::FSUB,
@@ -51835,6 +51836,33 @@ static SDValue combineXor(SDNode *N, SelectionDAG &DAG,
5183551836
return combineFneg(N, DAG, DCI, Subtarget);
5183651837
}
5183751838

51839+
static SDValue combineBITREVERSE(SDNode *N, SelectionDAG &DAG,
51840+
TargetLowering::DAGCombinerInfo &DCI,
51841+
const X86Subtarget &Subtarget) {
51842+
SDValue N0 = N->getOperand(0);
51843+
EVT VT = N->getValueType(0);
51844+
51845+
// Convert a (iX bitreverse(bitcast(vXi1 X))) -> (iX bitcast(shuffle(X)))
51846+
if (VT.isInteger() && N0.getOpcode() == ISD::BITCAST && N0.hasOneUse()) {
51847+
SDValue Src = N0.getOperand(0);
51848+
EVT SrcVT = Src.getValueType();
51849+
if (SrcVT.isVector() && SrcVT.getScalarType() == MVT::i1 &&
51850+
(DCI.isBeforeLegalize() ||
51851+
DAG.getTargetLoweringInfo().isTypeLegal(SrcVT)) &&
51852+
Subtarget.hasSSSE3()) {
51853+
unsigned NumElts = SrcVT.getVectorNumElements();
51854+
SmallVector<int, 32> ReverseMask(NumElts);
51855+
for (unsigned I = 0; I != NumElts; ++I)
51856+
ReverseMask[I] = (NumElts - 1) - I;
51857+
SDValue Rev =
51858+
DAG.getVectorShuffle(SrcVT, SDLoc(N), Src, Src, ReverseMask);
51859+
return DAG.getBitcast(VT, Rev);
51860+
}
51861+
}
51862+
51863+
return SDValue();
51864+
}
51865+
5183851866
static SDValue combineBEXTR(SDNode *N, SelectionDAG &DAG,
5183951867
TargetLowering::DAGCombinerInfo &DCI,
5184051868
const X86Subtarget &Subtarget) {
@@ -56124,6 +56152,7 @@ SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
5612456152
case ISD::AND: return combineAnd(N, DAG, DCI, Subtarget);
5612556153
case ISD::OR: return combineOr(N, DAG, DCI, Subtarget);
5612656154
case ISD::XOR: return combineXor(N, DAG, DCI, Subtarget);
56155+
case ISD::BITREVERSE: return combineBITREVERSE(N, DAG, DCI, Subtarget);
5612756156
case X86ISD::BEXTR:
5612856157
case X86ISD::BEXTRI: return combineBEXTR(N, DAG, DCI, Subtarget);
5612956158
case ISD::LOAD: return combineLoad(N, DAG, DCI, Subtarget);

0 commit comments

Comments
 (0)