+ return getFoldedCast(Instruction::ZExt, C, Ty);
+}
+
+Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
+ assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
+ C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
+ "This is an illegal floating point truncation!");
+ return getFoldedCast(Instruction::FPTrunc, C, Ty);
+}
+
+Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
+ assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
+ C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
+ "This is an illegal floating point extension!");
+ return getFoldedCast(Instruction::FPExt, C, Ty);
+}
+
+Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
+ assert(C->getType()->isInteger() && Ty->isFloatingPoint() &&
+ "This is an illegal i32 to floating point cast!");
+ return getFoldedCast(Instruction::UIToFP, C, Ty);
+}
+
+Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
+ assert(C->getType()->isInteger() && Ty->isFloatingPoint() &&
+ "This is an illegal sint to floating point cast!");
+ return getFoldedCast(Instruction::SIToFP, C, Ty);
+}
+
+Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
+ assert(C->getType()->isFloatingPoint() && Ty->isInteger() &&
+ "This is an illegal floating point to i32 cast!");
+ return getFoldedCast(Instruction::FPToUI, C, Ty);
+}
+
+Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
+ assert(C->getType()->isFloatingPoint() && Ty->isInteger() &&
+ "This is an illegal floating point to i32 cast!");
+ return getFoldedCast(Instruction::FPToSI, C, Ty);
+}
+
+Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
+ assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
+ assert(DstTy->isInteger() && "PtrToInt destination must be integral");
+ return getFoldedCast(Instruction::PtrToInt, C, DstTy);
+}
+
+Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
+ assert(C->getType()->isInteger() && "IntToPtr source must be integral");
+ assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
+ return getFoldedCast(Instruction::IntToPtr, C, DstTy);
+}
+
+Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
+ // BitCast implies a no-op cast of type only. No bits change. However, you
+ // can't cast pointers to anything but pointers.
+ const Type *SrcTy = C->getType();
+ assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
+ "BitCast cannot cast pointer to non-pointer and vice versa");
+
+ // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
+ // or nonptr->ptr). For all the other types, the cast is okay if source and
+ // destination bit widths are identical.
+ unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
+ unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
+ assert(SrcBitSize == DstBitSize && "BitCast requies types of same width");
+ return getFoldedCast(Instruction::BitCast, C, DstTy);
+}
+
+Constant *ConstantExpr::getSizeOf(const Type *Ty) {
+ // sizeof is implemented as: (ulong) gep (Ty*)null, 1
+ Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
+ Constant *GEP =
+ getGetElementPtr(getNullValue(PointerType::get(Ty)), &GEPIdx, 1);
+ return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty);