diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp --- a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp @@ -4731,11 +4731,11 @@ } SDValue DAGTypeLegalizer::WidenVecRes_EXTRACT_SUBVECTOR(SDNode *N) { - EVT VT = N->getValueType(0); - EVT EltVT = VT.getVectorElementType(); - EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT); - SDValue InOp = N->getOperand(0); - SDValue Idx = N->getOperand(1); + EVT VT = N->getValueType(0); + EVT EltVT = VT.getVectorElementType(); + EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT); + SDValue InOp = N->getOperand(0); + SDValue Idx = N->getOperand(1); SDLoc dl(N); auto InOpTypeAction = getTypeAction(InOp.getValueType()); @@ -4752,6 +4752,9 @@ // Check if we can extract from the vector. unsigned WidenNumElts = WidenVT.getVectorMinNumElements(); unsigned InNumElts = InVT.getVectorMinNumElements(); + unsigned VTNumElts = VT.getVectorMinNumElements(); + assert(IdxVal % VTNumElts == 0 && + "Expected Idx to be a multiple of subvector minimum vector length"); if (IdxVal % WidenNumElts == 0 && IdxVal + WidenNumElts < InNumElts) return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, WidenVT, InOp, Idx); @@ -4765,8 +4768,7 @@ // nxv2i64 extract_subvector(nxv16i64, 8) // nxv2i64 extract_subvector(nxv16i64, 10) // undef) - unsigned VTNElts = VT.getVectorMinNumElements(); - unsigned GCD = greatestCommonDivisor(VTNElts, WidenNumElts); + unsigned GCD = greatestCommonDivisor(VTNumElts, WidenNumElts); assert((IdxVal % GCD) == 0 && "Expected Idx to be a multiple of the broken " "down type's element count"); EVT PartVT = EVT::getVectorVT(*DAG.getContext(), EltVT, @@ -4775,7 +4777,7 @@ if (getTypeAction(PartVT) != TargetLowering::TypeWidenVector) { SmallVector Parts; unsigned I = 0; - for (; I < VTNElts / GCD; ++I) + for (; I < VTNumElts / GCD; ++I) Parts.push_back( DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, PartVT, InOp, DAG.getVectorIdxConstant(IdxVal + I * GCD, dl))); @@ -4792,9 +4794,8 @@ // We could try widening the input to the right length but for now, extract // the original elements, fill the rest with undefs and build a vector. SmallVector Ops(WidenNumElts); - unsigned NumElts = VT.getVectorNumElements(); unsigned i; - for (i = 0; i < NumElts; ++i) + for (i = 0; i < VTNumElts; ++i) Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, InOp, DAG.getVectorIdxConstant(IdxVal + i, dl));