From: Matt Arsenault Date: Wed, 31 Jul 2013 00:17:33 +0000 (+0000) Subject: Fix ptr vector inconsistency in CreatePointerCast X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=1bf0ec4e1642a532c0121de8ccc0878d6403c9d3;p=oota-llvm.git Fix ptr vector inconsistency in CreatePointerCast One form would accept a vector of pointers, and the other did not. Make both accept vectors of pointers, and add an assertion for the number of elements. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@187464 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/IR/Instructions.cpp b/lib/IR/Instructions.cpp index 49a9e8c18d2..d88fb6d4e3c 100644 --- a/lib/IR/Instructions.cpp +++ b/lib/IR/Instructions.cpp @@ -2415,22 +2415,30 @@ CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd) { - assert(S->getType()->isPointerTy() && "Invalid cast"); - assert((Ty->isIntegerTy() || Ty->isPointerTy()) && + assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); + assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && + "Invalid cast"); + assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast"); + assert(!Ty->isVectorTy() || + Ty->getVectorNumElements() == S->getType()->getVectorNumElements() && "Invalid cast"); - if (Ty->isIntegerTy()) + if (Ty->isIntOrIntVectorTy()) return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); } /// @brief Create a BitCast or a PtrToInt cast instruction -CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, - const Twine &Name, +CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, + const Twine &Name, Instruction *InsertBefore) { assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && "Invalid cast"); + assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast"); + assert(!Ty->isVectorTy() || + Ty->getVectorNumElements() == S->getType()->getVectorNumElements() && + "Invalid cast"); if (Ty->isIntOrIntVectorTy()) return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); diff --git a/unittests/IR/InstructionsTest.cpp b/unittests/IR/InstructionsTest.cpp index c2fdceb7eb5..ce6c465163d 100644 --- a/unittests/IR/InstructionsTest.cpp +++ b/unittests/IR/InstructionsTest.cpp @@ -197,6 +197,17 @@ TEST(InstructionsTest, CastInst) { EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy)); EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty)); EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty)); + + + // Check that assertion is not hit when creating a cast with a vector of + // pointers + // First form + BasicBlock *BB = BasicBlock::Create(C); + Constant *NullV2I32Ptr = Constant::getNullValue(V2Int32PtrTy); + CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty, "foo", BB); + + // Second form + CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty); } TEST(InstructionsTest, VectorGep) {