Skip to content

Commit f067dd8

Browse files
committed
[LegalizeTypes] When promoting BITREVERSE/BSWAP don't take the shift amount into account when determining the shift amount VT.
If the target's preferred shift amount VT can't hold any shift amount for the promoted VT, we should use i32. The specific shift amount shouldn't matter. The type will be adjusted later when the shift itself is type legalized. This avoids an assert in getNode. Fixes PR43820.
1 parent 73f255b commit f067dd8

File tree

2 files changed

+392
-10
lines changed

2 files changed

+392
-10
lines changed

llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -365,15 +365,15 @@ SDValue DAGTypeLegalizer::PromoteIntRes_BITCAST(SDNode *N) {
365365
CreateStackStoreLoad(InOp, OutVT));
366366
}
367367

368-
// Helper for BSWAP/BITREVERSE promotion to ensure we can fit the shift amount
368+
// Helper for BSWAP/BITREVERSE promotion to ensure we can fit any shift amount
369369
// in the VT returned by getShiftAmountTy and to return a safe VT if we can't.
370-
static EVT getShiftAmountTyForConstant(unsigned Val, EVT VT,
371-
const TargetLowering &TLI,
370+
static EVT getShiftAmountTyForConstant(EVT VT, const TargetLowering &TLI,
372371
SelectionDAG &DAG) {
373372
EVT ShiftVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
374-
// If the value won't fit in the prefered type, just use something safe. It
375-
// will be legalized when the shift is expanded.
376-
if ((Log2_32(Val) + 1) > ShiftVT.getScalarSizeInBits())
373+
// If any possible shift value won't fit in the prefered type, just use
374+
// something safe. It will be legalized when the shift is expanded.
375+
if (!ShiftVT.isVector() &&
376+
ShiftVT.getSizeInBits() < Log2_32_Ceil(VT.getSizeInBits()))
377377
ShiftVT = MVT::i32;
378378
return ShiftVT;
379379
}
@@ -385,7 +385,7 @@ SDValue DAGTypeLegalizer::PromoteIntRes_BSWAP(SDNode *N) {
385385
SDLoc dl(N);
386386

387387
unsigned DiffBits = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits();
388-
EVT ShiftVT = getShiftAmountTyForConstant(DiffBits, NVT, TLI, DAG);
388+
EVT ShiftVT = getShiftAmountTyForConstant(NVT, TLI, DAG);
389389
return DAG.getNode(ISD::SRL, dl, NVT, DAG.getNode(ISD::BSWAP, dl, NVT, Op),
390390
DAG.getConstant(DiffBits, dl, ShiftVT));
391391
}
@@ -397,7 +397,7 @@ SDValue DAGTypeLegalizer::PromoteIntRes_BITREVERSE(SDNode *N) {
397397
SDLoc dl(N);
398398

399399
unsigned DiffBits = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits();
400-
EVT ShiftVT = getShiftAmountTyForConstant(DiffBits, NVT, TLI, DAG);
400+
EVT ShiftVT = getShiftAmountTyForConstant(NVT, TLI, DAG);
401401
return DAG.getNode(ISD::SRL, dl, NVT,
402402
DAG.getNode(ISD::BITREVERSE, dl, NVT, Op),
403403
DAG.getConstant(DiffBits, dl, ShiftVT));
@@ -1058,8 +1058,7 @@ SDValue DAGTypeLegalizer::PromoteIntRes_XMULO(SDNode *N, unsigned ResNo) {
10581058
if (N->getOpcode() == ISD::UMULO) {
10591059
// Unsigned overflow occurred if the high part is non-zero.
10601060
unsigned Shift = SmallVT.getScalarSizeInBits();
1061-
EVT ShiftTy = getShiftAmountTyForConstant(Shift, Mul.getValueType(),
1062-
TLI, DAG);
1061+
EVT ShiftTy = getShiftAmountTyForConstant(Mul.getValueType(), TLI, DAG);
10631062
SDValue Hi = DAG.getNode(ISD::SRL, DL, Mul.getValueType(), Mul,
10641063
DAG.getConstant(Shift, DL, ShiftTy));
10651064
Overflow = DAG.getSetCC(DL, N->getValueType(1), Hi,

0 commit comments

Comments
 (0)