diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp index f6b5d4af5ba4e..db8247a7f3058 100644 --- a/llvm/lib/Target/X86/X86ISelLowering.cpp +++ b/llvm/lib/Target/X86/X86ISelLowering.cpp @@ -56427,34 +56427,34 @@ static SDValue combineGatherScatter(SDNode *N, SelectionDAG &DAG, if (DCI.isBeforeLegalize()) { unsigned IndexWidth = Index.getScalarValueSizeInBits(); - // Shrink constant indices if they are larger than 32-bits. + // Shrink indices if they are larger than 32-bits. // Only do this before legalize types since v2i64 could become v2i32. // FIXME: We could check that the type is legal if we're after legalize // types, but then we would need to construct test cases where that happens. - // FIXME: We could support more than just constant vectors, but we need to - // careful with costing. A truncate that can be optimized out would be fine. - // Otherwise we might only want to create a truncate if it avoids a split. - if (auto *BV = dyn_cast(Index)) { - if (BV->isConstant() && IndexWidth > 32 && - DAG.ComputeNumSignBits(Index) > (IndexWidth - 32)) { - EVT NewVT = IndexVT.changeVectorElementType(MVT::i32); + if (IndexWidth > 32 && DAG.ComputeNumSignBits(Index) > (IndexWidth - 32)) { + EVT NewVT = IndexVT.changeVectorElementType(MVT::i32); + + // FIXME: We could support more than just constant vectors, but we need to + // careful with costing. A truncate that can be optimized out would be + // fine. Otherwise we might only want to create a truncate if it avoids a + // split. + if (auto *BV = dyn_cast(Index)) { + if (BV->isConstant()) { + Index = DAG.getNode(ISD::TRUNCATE, DL, NewVT, Index); + return rebuildGatherScatter(GorS, Index, Base, Scale, DAG); + } + } + + // Shrink any sign/zero extends from 32 or smaller to larger than 32 if + // there are sufficient sign bits. Only do this before legalize types to + // avoid creating illegal types in truncate. + if ((Index.getOpcode() == ISD::SIGN_EXTEND || + Index.getOpcode() == ISD::ZERO_EXTEND) && + Index.getOperand(0).getScalarValueSizeInBits() <= 32) { Index = DAG.getNode(ISD::TRUNCATE, DL, NewVT, Index); return rebuildGatherScatter(GorS, Index, Base, Scale, DAG); } } - - // Shrink any sign/zero extends from 32 or smaller to larger than 32 if - // there are sufficient sign bits. Only do this before legalize types to - // avoid creating illegal types in truncate. - if ((Index.getOpcode() == ISD::SIGN_EXTEND || - Index.getOpcode() == ISD::ZERO_EXTEND) && - IndexWidth > 32 && - Index.getOperand(0).getScalarValueSizeInBits() <= 32 && - DAG.ComputeNumSignBits(Index) > (IndexWidth - 32)) { - EVT NewVT = IndexVT.changeVectorElementType(MVT::i32); - Index = DAG.getNode(ISD::TRUNCATE, DL, NewVT, Index); - return rebuildGatherScatter(GorS, Index, Base, Scale, DAG); - } } EVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());