+static Instruction *foldVecTruncToExtElt(Value *VecInput, Type *DestTy,
+ unsigned ShiftAmt, InstCombiner &IC,
+ const DataLayout &DL) {
+ VectorType *VecTy = cast<VectorType>(VecInput->getType());
+ unsigned DestWidth = DestTy->getPrimitiveSizeInBits();
+ unsigned VecWidth = VecTy->getPrimitiveSizeInBits();
+
+ if ((VecWidth % DestWidth != 0) || (ShiftAmt % DestWidth != 0))
+ return nullptr;
+
+ // If the element type of the vector doesn't match the result type,
+ // bitcast it to be a vector type we can extract from.
+ unsigned NumVecElts = VecWidth / DestWidth;
+ if (VecTy->getElementType() != DestTy) {
+ VecTy = VectorType::get(DestTy, NumVecElts);
+ VecInput = IC.Builder->CreateBitCast(VecInput, VecTy);
+ }
+
+ unsigned Elt = ShiftAmt / DestWidth;
+ if (DL.isBigEndian())
+ Elt = NumVecElts - 1 - Elt;
+
+ return ExtractElementInst::Create(VecInput, IC.Builder->getInt32(Elt));
+}