From 044fd8aa9450f9c4ac31a3f3bdf0ca567d1d33b4 Mon Sep 17 00:00:00 2001 From: Evgeniy Stepanov Date: Wed, 16 Jan 2013 14:41:46 +0000 Subject: [PATCH] Allow vectors in CreatePointerCast of constants. llvm-svn: 172615 --- lib/IR/Constants.cpp | 7 ++++--- unittests/IR/ConstantsTest.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/IR/Constants.cpp b/lib/IR/Constants.cpp index 93275549b18..e984aaca33b 100644 --- a/lib/IR/Constants.cpp +++ b/lib/IR/Constants.cpp @@ -1465,10 +1465,11 @@ Constant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) { } Constant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) { - assert(S->getType()->isPointerTy() && "Invalid cast"); - assert((Ty->isIntegerTy() || Ty->isPointerTy()) && "Invalid cast"); + assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); + assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && + "Invalid cast"); - if (Ty->isIntegerTy()) + if (Ty->isIntOrIntVectorTy()) return getPtrToInt(S, Ty); return getBitCast(S, Ty); } diff --git a/unittests/IR/ConstantsTest.cpp b/unittests/IR/ConstantsTest.cpp index be34c1e2a1a..db783f72d46 100644 --- a/unittests/IR/ConstantsTest.cpp +++ b/unittests/IR/ConstantsTest.cpp @@ -121,6 +121,36 @@ TEST(ConstantsTest, FP128Test) { EXPECT_TRUE(isa(X)); } +TEST(ConstantsTest, PointerCast) { + LLVMContext &C(getGlobalContext()); + Type *Int8PtrTy = Type::getInt8PtrTy(C); + Type *Int32PtrTy = Type::getInt32PtrTy(C); + Type *Int64Ty = Type::getInt64Ty(C); + VectorType *Int8PtrVecTy = VectorType::get(Int8PtrTy, 4); + VectorType *Int32PtrVecTy = VectorType::get(Int32PtrTy, 4); + VectorType *Int64VecTy = VectorType::get(Int64Ty, 4); + + // ptrtoint i8* to i64 + EXPECT_EQ(Constant::getNullValue(Int64Ty), + ConstantExpr::getPointerCast( + Constant::getNullValue(Int8PtrTy), Int64Ty)); + + // bitcast i8* to i32* + EXPECT_EQ(Constant::getNullValue(Int32PtrTy), + ConstantExpr::getPointerCast( + Constant::getNullValue(Int8PtrTy), Int32PtrTy)); + + // ptrtoint <4 x i8*> to <4 x i64> + EXPECT_EQ(Constant::getNullValue(Int64VecTy), + ConstantExpr::getPointerCast( + Constant::getNullValue(Int8PtrVecTy), Int64VecTy)); + + // bitcast <4 x i8*> to <4 x i32*> + EXPECT_EQ(Constant::getNullValue(Int32PtrVecTy), + ConstantExpr::getPointerCast( + Constant::getNullValue(Int8PtrVecTy), Int32PtrVecTy)); +} + #define CHECK(x, y) { \ std::string __s; \ raw_string_ostream __o(__s); \