+/// CastConstantPacked - Convert the specified ConstantPacked node to the
+/// specified packed type. At this point, we know that the elements of the
+/// input packed constant are all simple integer or FP values.
+static Constant *CastConstantPacked(ConstantPacked *CP,
+ const PackedType *DstTy) {
+ unsigned SrcNumElts = CP->getType()->getNumElements();
+ unsigned DstNumElts = DstTy->getNumElements();
+ const Type *SrcEltTy = CP->getType()->getElementType();
+ const Type *DstEltTy = DstTy->getElementType();
+
+ // If both vectors have the same number of elements (thus, the elements
+ // are the same size), perform the conversion now.
+ if (SrcNumElts == DstNumElts) {
+ std::vector<Constant*> Result;
+
+ // If the src and dest elements are both integers, just cast each one
+ // which will do the appropriate bit-convert.
+ if (SrcEltTy->isIntegral() && DstEltTy->isIntegral()) {
+ for (unsigned i = 0; i != SrcNumElts; ++i)
+ Result.push_back(ConstantExpr::getCast(CP->getOperand(i),
+ DstEltTy));
+ return ConstantPacked::get(Result);
+ }
+
+ if (SrcEltTy->isIntegral()) {
+ // Otherwise, this is an int-to-fp cast.
+ assert(DstEltTy->isFloatingPoint());
+ if (DstEltTy->getTypeID() == Type::DoubleTyID) {
+ for (unsigned i = 0; i != SrcNumElts; ++i) {
+ double V =
+ BitsToDouble(cast<ConstantInt>(CP->getOperand(i))->getZExtValue());
+ Result.push_back(ConstantFP::get(Type::DoubleTy, V));
+ }
+ return ConstantPacked::get(Result);
+ }
+ assert(DstEltTy == Type::FloatTy && "Unknown fp type!");
+ for (unsigned i = 0; i != SrcNumElts; ++i) {
+ float V =
+ BitsToFloat(cast<ConstantInt>(CP->getOperand(i))->getZExtValue());
+ Result.push_back(ConstantFP::get(Type::FloatTy, V));
+ }
+ return ConstantPacked::get(Result);
+ }
+
+ // Otherwise, this is an fp-to-int cast.
+ assert(SrcEltTy->isFloatingPoint() && DstEltTy->isIntegral());
+
+ if (SrcEltTy->getTypeID() == Type::DoubleTyID) {
+ for (unsigned i = 0; i != SrcNumElts; ++i) {
+ uint64_t V =
+ DoubleToBits(cast<ConstantFP>(CP->getOperand(i))->getValue());
+ Constant *C = ConstantInt::get(Type::ULongTy, V);
+ Result.push_back(ConstantExpr::getCast(C, DstEltTy));
+ }
+ return ConstantPacked::get(Result);
+ }
+
+ assert(SrcEltTy->getTypeID() == Type::FloatTyID);
+ for (unsigned i = 0; i != SrcNumElts; ++i) {
+ uint32_t V = FloatToBits(cast<ConstantFP>(CP->getOperand(i))->getValue());
+ Constant *C = ConstantInt::get(Type::UIntTy, V);
+ Result.push_back(ConstantExpr::getCast(C, DstEltTy));
+ }
+ return ConstantPacked::get(Result);
+ }
+
+ // Otherwise, this is a cast that changes element count and size. Handle
+ // casts which shrink the elements here.
+
+ // FIXME: We need to know endianness to do this!
+
+ return 0;
+}
+
+