From 1d7a67937c4624967442dae997a6cd0c0a9c0b72 Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Mon, 14 Dec 2015 19:30:32 +0000 Subject: [PATCH] [ConstantFold] Fix bitcast to gep constant folding transform. Make sure to check that the destination type is sized. A check was present but was incorrectly checking the source type instead. Patch by Amaury SECHET! Differential Revision: http://reviews.llvm.org/D15264 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@255536 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/IR/ConstantFold.cpp | 2 +- test/Assembler/ConstantExprFoldCast.ll | 4 ++++ unittests/IR/ConstantsTest.cpp | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/IR/ConstantFold.cpp b/lib/IR/ConstantFold.cpp index 68ddf096754..979bbbd0ab7 100644 --- a/lib/IR/ConstantFold.cpp +++ b/lib/IR/ConstantFold.cpp @@ -109,7 +109,7 @@ static Constant *FoldBitCast(Constant *V, Type *DestTy) { if (PointerType *PTy = dyn_cast(V->getType())) if (PointerType *DPTy = dyn_cast(DestTy)) if (PTy->getAddressSpace() == DPTy->getAddressSpace() - && DPTy->getElementType()->isSized()) { + && PTy->getElementType()->isSized()) { SmallVector IdxList; Value *Zero = Constant::getNullValue(Type::getInt32Ty(DPTy->getContext())); diff --git a/test/Assembler/ConstantExprFoldCast.ll b/test/Assembler/ConstantExprFoldCast.ll index 094f87be92c..dfe840cc37d 100644 --- a/test/Assembler/ConstantExprFoldCast.ll +++ b/test/Assembler/ConstantExprFoldCast.ll @@ -15,3 +15,7 @@ ; Address space cast AS0 null-> AS1 null @H = global i32 addrspace(1)* addrspacecast(i32* null to i32 addrspace(1)*) + +; Bitcast -> GEP +@I = external global { i32 } +@J = global i32* bitcast ({ i32 }* @I to i32*) diff --git a/unittests/IR/ConstantsTest.cpp b/unittests/IR/ConstantsTest.cpp index 0bf98f35b3c..e3b303a350d 100644 --- a/unittests/IR/ConstantsTest.cpp +++ b/unittests/IR/ConstantsTest.cpp @@ -433,5 +433,22 @@ TEST(ConstantsTest, BuildConstantDataVectors) { } } +TEST(ConstantsTest, BitcastToGEP) { + LLVMContext Context; + std::unique_ptr M(new Module("MyModule", Context)); + + auto *i32 = Type::getInt32Ty(Context); + auto *U = StructType::create(Context, "Unsized"); + Type *EltTys[] = {i32, U}; + auto *S = StructType::create(EltTys); + + auto *G = new GlobalVariable(*M, S, false, + GlobalValue::ExternalLinkage, nullptr); + auto *PtrTy = PointerType::get(i32, 0); + auto *C = ConstantExpr::getBitCast(G, PtrTy); + ASSERT_EQ(dyn_cast(C)->getOpcode(), + Instruction::BitCast); +} + } // end anonymous namespace } // end namespace llvm -- 2.34.1