Index: lib/IR/ConstantFold.cpp =================================================================== --- lib/IR/ConstantFold.cpp +++ lib/IR/ConstantFold.cpp @@ -109,7 +109,7 @@ 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())); Index: unittests/IR/ConstantsTest.cpp =================================================================== --- unittests/IR/ConstantsTest.cpp +++ unittests/IR/ConstantsTest.cpp @@ -382,5 +382,31 @@ ASSERT_EQ(unwrap(AliasRef)->getAliasee(), Aliasee); } +TEST(ConstantsTest, BitcastToGEP) { + LLVMContext Context; + std::unique_ptr M(new Module("MyModule", Context)); + + auto *i32 = Type::getInt32Ty(Context); + Type *S1Elts[] = {i32, i32}; + auto *S1 = StructType::create(S1Elts); + + auto *G1 = new GlobalVariable(*M, S1, false, + GlobalValue::ExternalLinkage, nullptr); + auto *PtrTy = PointerType::get(i32, 0); + auto *C1 = ConstantExpr::getBitCast(G1, PtrTy); + ASSERT_EQ(dyn_cast(C1)->getOpcode(), + Instruction::GetElementPtr); + + auto *U = StructType::create(Context, "Unsized"); + Type *S2Elts[] = {i32, U}; + auto *S2 = StructType::create(S2Elts); + + auto *G2 = new GlobalVariable(*M, S2, false, + GlobalValue::ExternalLinkage, nullptr); + auto *C2 = ConstantExpr::getBitCast(G2, PtrTy); + ASSERT_EQ(dyn_cast(C2)->getOpcode(), + Instruction::BitCast); +} + } // end anonymous namespace } // end namespace llvm