From 79e3fb53d618d12e239275ef055200bbd6f8253e Mon Sep 17 00:00:00 2001 From: Matt Arsenault Date: Wed, 22 Jan 2014 19:21:33 +0000 Subject: Bug 18228 - Fix accepting bitcasts between vectors of pointers with a different number of elements. Bitcasts were passing with vectors of pointers with different number of elements since the number of elements was checking SrcTy->getVectorNumElements() == SrcTy->getVectorNumElements() which isn't helpful. The addrspacecast was also wrong, but that case at least is caught by the verifier. Refactor bitcast and addrspacecast handling in castIsValid to be more readable and fix this problem. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199821 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/IR/Instructions.cpp | 57 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 16 deletions(-) (limited to 'lib/IR/Instructions.cpp') diff --git a/lib/IR/Instructions.cpp b/lib/IR/Instructions.cpp index 5a50b73650..c805b72d92 100644 --- a/lib/IR/Instructions.cpp +++ b/lib/IR/Instructions.cpp @@ -2818,30 +2818,55 @@ CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) { return false; return SrcTy->getScalarType()->isIntegerTy() && DstTy->getScalarType()->isPointerTy(); - case Instruction::BitCast: + case Instruction::BitCast: { + PointerType *SrcPtrTy = dyn_cast(SrcTy->getScalarType()); + PointerType *DstPtrTy = dyn_cast(DstTy->getScalarType()); + // BitCast implies a no-op cast of type only. No bits change. // However, you can't cast pointers to anything but pointers. - if (SrcTy->isPtrOrPtrVectorTy() != DstTy->isPtrOrPtrVectorTy()) + if (!SrcPtrTy != !DstPtrTy) return false; // For non-pointer cases, the cast is okay if the source and destination bit // widths are identical. - if (!SrcTy->isPtrOrPtrVectorTy()) + if (!SrcPtrTy) return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits(); - // If both are pointers then the address spaces must match and vector of - // pointers must have the same number of elements. - return SrcTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() && - SrcTy->isVectorTy() == DstTy->isVectorTy() && - (!SrcTy->isVectorTy() || - SrcTy->getVectorNumElements() == SrcTy->getVectorNumElements()); - - case Instruction::AddrSpaceCast: - return SrcTy->isPtrOrPtrVectorTy() && DstTy->isPtrOrPtrVectorTy() && - SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace() && - SrcTy->isVectorTy() == DstTy->isVectorTy() && - (!SrcTy->isVectorTy() || - SrcTy->getVectorNumElements() == SrcTy->getVectorNumElements()); + // If both are pointers then the address spaces must match. + if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace()) + return false; + + // A vector of pointers must have the same number of elements. + if (VectorType *SrcVecTy = dyn_cast(SrcTy)) { + if (VectorType *DstVecTy = dyn_cast(DstTy)) + return (SrcVecTy->getNumElements() == DstVecTy->getNumElements()); + + return false; + } + + return true; + } + case Instruction::AddrSpaceCast: { + PointerType *SrcPtrTy = dyn_cast(SrcTy->getScalarType()); + if (!SrcPtrTy) + return false; + + PointerType *DstPtrTy = dyn_cast(DstTy->getScalarType()); + if (!DstPtrTy) + return false; + + if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace()) + return false; + + if (VectorType *SrcVecTy = dyn_cast(SrcTy)) { + if (VectorType *DstVecTy = dyn_cast(DstTy)) + return (SrcVecTy->getNumElements() == DstVecTy->getNumElements()); + + return false; + } + + return true; + } } } -- cgit v1.2.3