diff --git a/llvm/include/llvm/CodeGen/TargetLowering.h b/llvm/include/llvm/CodeGen/TargetLowering.h --- a/llvm/include/llvm/CodeGen/TargetLowering.h +++ b/llvm/include/llvm/CodeGen/TargetLowering.h @@ -372,10 +372,18 @@ return getPointerTy(DL); } - /// EVT is not used in-tree, but is used by out-of-tree target. - /// A documentation for this function would be nice... + /// Return the type to use for a scalar shift opcode, given the shifted amount + /// type. Targets should return a legal type if the input type is legal. + /// Targets can return a type that is too small if the input type is illegal. virtual MVT getScalarShiftAmountTy(const DataLayout &, EVT) const; + /// Returns the type for the shift amount of a shift opcode. For vectors, + /// returns the input type. For scalars, behavior depends on \p LegalTypes. If + /// \p LegalTypes is true, calls getScalarShiftAmountTy, otherwise uses + /// pointer type. If getScalarShiftAmountTy or pointer type cannot represent + /// all possible shift amounts, returns MVT::i32. In general, \p LegalTypes + /// should be set to true for calls during type legalization and after type + /// legalization has been completed. EVT getShiftAmountTy(EVT LHSTy, const DataLayout &DL, bool LegalTypes = true) const; diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp --- a/llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp @@ -491,19 +491,6 @@ CreateStackStoreLoad(InOp, OutVT)); } -// Helper for BSWAP/BITREVERSE promotion to ensure we can fit any shift amount -// in the VT returned by getShiftAmountTy and to return a safe VT if we can't. -static EVT getShiftAmountTyForConstant(EVT VT, const TargetLowering &TLI, - SelectionDAG &DAG) { - EVT ShiftVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout()); - // If any possible shift value won't fit in the prefered type, just use - // something safe. It will be legalized when the shift is expanded. - if (!ShiftVT.isVector() && - ShiftVT.getSizeInBits() < Log2_32_Ceil(VT.getSizeInBits())) - ShiftVT = MVT::i32; - return ShiftVT; -} - SDValue DAGTypeLegalizer::PromoteIntRes_FREEZE(SDNode *N) { SDValue V = GetPromotedInteger(N->getOperand(0)); return DAG.getNode(ISD::FREEZE, SDLoc(N), @@ -527,7 +514,7 @@ } unsigned DiffBits = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits(); - EVT ShiftVT = getShiftAmountTyForConstant(NVT, TLI, DAG); + EVT ShiftVT = TLI.getShiftAmountTy(NVT, DAG.getDataLayout()); return DAG.getNode(ISD::SRL, dl, NVT, DAG.getNode(ISD::BSWAP, dl, NVT, Op), DAG.getConstant(DiffBits, dl, ShiftVT)); } @@ -549,7 +536,7 @@ } unsigned DiffBits = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits(); - EVT ShiftVT = getShiftAmountTyForConstant(NVT, TLI, DAG); + EVT ShiftVT = TLI.getShiftAmountTy(NVT, DAG.getDataLayout()); return DAG.getNode(ISD::SRL, dl, NVT, DAG.getNode(ISD::BITREVERSE, dl, NVT, Op), DAG.getConstant(DiffBits, dl, ShiftVT)); @@ -1493,7 +1480,7 @@ if (N->getOpcode() == ISD::UMULO) { // Unsigned overflow occurred if the high part is non-zero. unsigned Shift = SmallVT.getScalarSizeInBits(); - EVT ShiftTy = getShiftAmountTyForConstant(Mul.getValueType(), TLI, DAG); + EVT ShiftTy = TLI.getShiftAmountTy(Mul.getValueType(), DAG.getDataLayout()); SDValue Hi = DAG.getNode(ISD::SRL, DL, Mul.getValueType(), Mul, DAG.getConstant(Shift, DL, ShiftTy)); Overflow = DAG.getSetCC(DL, N->getValueType(1), Hi, @@ -3153,7 +3140,7 @@ bool HasAddCarry = TLI.isOperationLegalOrCustom( ISD::ADDCARRY, TLI.getTypeToExpandTo(*DAG.getContext(), NVT)); if (HasAddCarry) { - EVT ShiftAmtTy = getShiftAmountTyForConstant(NVT, TLI, DAG); + EVT ShiftAmtTy = TLI.getShiftAmountTy(NVT, DAG.getDataLayout()); SDValue Sign = DAG.getNode(ISD::SRA, dl, NVT, Hi, DAG.getConstant(NVT.getSizeInBits() - 1, dl, ShiftAmtTy)); @@ -3548,11 +3535,6 @@ SDValue TL = DAG.getNode(ISD::AND, dl, NVT, T, Mask); EVT ShiftAmtTy = TLI.getShiftAmountTy(NVT, DAG.getDataLayout()); - if (APInt::getMaxValue(ShiftAmtTy.getSizeInBits()).ult(HalfBits)) { - // The type from TLI is too small to fit the shift amount we want. - // Override it with i32. The shift will have to be legalized. - ShiftAmtTy = MVT::i32; - } SDValue Shift = DAG.getConstant(HalfBits, dl, ShiftAmtTy); SDValue TH = DAG.getNode(ISD::SRL, dl, NVT, T, Shift); SDValue LLH = DAG.getNode(ISD::SRL, dl, NVT, LL, Shift); @@ -3992,9 +3974,6 @@ // the new SHL_PARTS operation would need further legalization. SDValue ShiftOp = N->getOperand(1); EVT ShiftTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout()); - assert(ShiftTy.getScalarSizeInBits() >= - Log2_32_Ceil(VT.getScalarSizeInBits()) && - "ShiftAmountTy is too small to cover the range of this type!"); if (ShiftOp.getValueType() != ShiftTy) ShiftOp = DAG.getZExtOrTrunc(ShiftOp, dl, ShiftTy); diff --git a/llvm/lib/CodeGen/TargetLoweringBase.cpp b/llvm/lib/CodeGen/TargetLoweringBase.cpp --- a/llvm/lib/CodeGen/TargetLoweringBase.cpp +++ b/llvm/lib/CodeGen/TargetLoweringBase.cpp @@ -925,8 +925,15 @@ assert(LHSTy.isInteger() && "Shift amount is not an integer type!"); if (LHSTy.isVector()) return LHSTy; - return LegalTypes ? getScalarShiftAmountTy(DL, LHSTy) - : getPointerTy(DL); + MVT ShiftVT = + LegalTypes ? getScalarShiftAmountTy(DL, LHSTy) : getPointerTy(DL); + // If any possible shift value won't fit in the prefered type, just use + // something safe. Assume it will be legalized when the shift is expanded. + if (ShiftVT.getSizeInBits() < Log2_32_Ceil(LHSTy.getSizeInBits())) + ShiftVT = MVT::i32; + assert(ShiftVT.getSizeInBits() >= Log2_32_Ceil(LHSTy.getSizeInBits()) && + "ShiftVT is still too small!"); + return ShiftVT; } bool TargetLoweringBase::canOpTrap(unsigned Op, EVT VT) const {