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: test/Assembler/ConstantExprFoldCast.ll =================================================================== --- test/Assembler/ConstantExprFoldCast.ll +++ 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*) Index: unittests/IR/ConstantsTest.cpp =================================================================== --- unittests/IR/ConstantsTest.cpp +++ unittests/IR/ConstantsTest.cpp @@ -382,5 +382,22 @@ ASSERT_EQ(unwrap(AliasRef)->getAliasee(), Aliasee); } +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