1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-19 02:52:53 +02:00

fix CastInst::castIsValid to reject aggregate types, fixing PR6153:

llvm-as: t.ll:1:25: error: invalid cast opcode for cast from '[4 x i8]' to '[1 x i32]'
@x = constant [1 x i32] bitcast ([4 x i8] c"abcd" to [1 x i32])
                        ^

llvm-svn: 94595
This commit is contained in:
Chris Lattner 2010-01-26 21:51:43 +00:00
parent 81ca658dae
commit c2717878e8
2 changed files with 20 additions and 31 deletions

View File

@ -1150,24 +1150,24 @@ Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Instruction::CastOps opc = Instruction::CastOps(oc);
assert(Instruction::isCast(opc) && "opcode out of range");
assert(C && Ty && "Null arguments to getCast");
assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!");
switch (opc) {
default:
llvm_unreachable("Invalid cast opcode");
break;
case Instruction::Trunc: return getTrunc(C, Ty);
case Instruction::ZExt: return getZExt(C, Ty);
case Instruction::SExt: return getSExt(C, Ty);
case Instruction::FPTrunc: return getFPTrunc(C, Ty);
case Instruction::FPExt: return getFPExtend(C, Ty);
case Instruction::UIToFP: return getUIToFP(C, Ty);
case Instruction::SIToFP: return getSIToFP(C, Ty);
case Instruction::FPToUI: return getFPToUI(C, Ty);
case Instruction::FPToSI: return getFPToSI(C, Ty);
case Instruction::PtrToInt: return getPtrToInt(C, Ty);
case Instruction::IntToPtr: return getIntToPtr(C, Ty);
case Instruction::BitCast: return getBitCast(C, Ty);
default:
llvm_unreachable("Invalid cast opcode");
break;
case Instruction::Trunc: return getTrunc(C, Ty);
case Instruction::ZExt: return getZExt(C, Ty);
case Instruction::SExt: return getSExt(C, Ty);
case Instruction::FPTrunc: return getFPTrunc(C, Ty);
case Instruction::FPExt: return getFPExtend(C, Ty);
case Instruction::UIToFP: return getUIToFP(C, Ty);
case Instruction::SIToFP: return getSIToFP(C, Ty);
case Instruction::FPToUI: return getFPToUI(C, Ty);
case Instruction::FPToSI: return getFPToSI(C, Ty);
case Instruction::PtrToInt: return getPtrToInt(C, Ty);
case Instruction::IntToPtr: return getIntToPtr(C, Ty);
case Instruction::BitCast: return getBitCast(C, Ty);
}
return 0;
}
@ -1347,20 +1347,8 @@ Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *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.
#ifndef NDEBUG
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();
#endif
assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) &&
"Invalid constantexpr bitcast!");
// It is common to ask for a bitcast of a value to its own type, handle this
// speedily.

View File

@ -2504,7 +2504,8 @@ CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
// Check for type sanity on the arguments
const Type *SrcTy = S->getType();
if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
SrcTy->isAggregateType() || DstTy->isAggregateType())
return false;
// Get the size of the types in bits, we'll need this later