diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp index e0aafe3692a8..fd4d7f2e9824 100644 --- a/llvm/lib/Target/X86/X86ISelLowering.cpp +++ b/llvm/lib/Target/X86/X86ISelLowering.cpp @@ -5205,31 +5205,30 @@ static bool getTargetConstantBitsFromNode(SDValue Op, unsigned EltSizeInBits, if (UndefEltBits.getBoolValue() && !AllowPartialUndefs) return false; - APInt Bits = MaskBits.lshr(i * EltSizeInBits); - Bits = Bits.zextOrTrunc(EltSizeInBits); + APInt Bits = MaskBits.lshr(i * EltSizeInBits).zextOrTrunc(EltSizeInBits); EltBits[i] = Bits.getZExtValue(); } return true; }; - auto ExtractConstantBits = [SizeInBits](const Constant *Cst, APInt &Mask, - APInt &Undefs) { + // Collect constant bits and insert into mask/undef bit masks. + auto CollectConstantBits = [SizeInBits](const Constant *Cst, APInt &Mask, + APInt &Undefs, unsigned BitOffset) { if (!Cst) return false; unsigned CstSizeInBits = Cst->getType()->getPrimitiveSizeInBits(); if (isa(Cst)) { - Mask = APInt::getNullValue(SizeInBits); - Undefs = APInt::getLowBitsSet(SizeInBits, CstSizeInBits); + unsigned HiBits = BitOffset + CstSizeInBits; + Undefs |= APInt::getBitsSet(SizeInBits, BitOffset, HiBits); return true; } if (auto *CInt = dyn_cast(Cst)) { - Mask = CInt->getValue().zextOrTrunc(SizeInBits); - Undefs = APInt::getNullValue(SizeInBits); + Mask |= CInt->getValue().zextOrTrunc(SizeInBits).shl(BitOffset); return true; } if (auto *CFP = dyn_cast(Cst)) { - Mask = CFP->getValueAPF().bitcastToAPInt().zextOrTrunc(SizeInBits); - Undefs = APInt::getNullValue(SizeInBits); + APInt CstBits = CFP->getValueAPF().bitcastToAPInt(); + Mask |= CstBits.zextOrTrunc(SizeInBits).shl(BitOffset); return true; } return false; @@ -5259,13 +5258,10 @@ static bool getTargetConstantBitsFromNode(SDValue Op, unsigned EltSizeInBits, return false; unsigned CstEltSizeInBits = CstTy->getScalarSizeInBits(); - for (unsigned i = 0, e = CstTy->getVectorNumElements(); i != e; ++i) { - APInt Bits, Undefs; - if (!ExtractConstantBits(Cst->getAggregateElement(i), Bits, Undefs)) + for (unsigned i = 0, e = CstTy->getVectorNumElements(); i != e; ++i) + if (!CollectConstantBits(Cst->getAggregateElement(i), MaskBits, UndefBits, + i * CstEltSizeInBits)) return false; - MaskBits |= Bits.shl(i * CstEltSizeInBits); - UndefBits |= Undefs.shl(i * CstEltSizeInBits); - } return SplitBitData(); } @@ -5274,8 +5270,9 @@ static bool getTargetConstantBitsFromNode(SDValue Op, unsigned EltSizeInBits, if (Op.getOpcode() == X86ISD::VBROADCAST && EltSizeInBits <= SrcEltSizeInBits) { if (auto *Broadcast = getTargetConstantFromNode(Op.getOperand(0))) { - APInt Bits, Undefs; - if (ExtractConstantBits(Broadcast, Bits, Undefs)) { + APInt Bits(SizeInBits, 0); + APInt Undefs(SizeInBits, 0); + if (CollectConstantBits(Broadcast, Bits, Undefs, 0)) { for (unsigned i = 0; i != NumSrcElts; ++i) { MaskBits |= Bits.shl(i * SrcEltSizeInBits); UndefBits |= Undefs.shl(i * SrcEltSizeInBits);