diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index 053c9f6e06d7f5..0fb7ec26a85e5a 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -4699,12 +4699,9 @@ LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) { if (LV.isSimple()) { Address V = LV.getAddress(*this); if (V.isValid()) { - llvm::Type *T = - ConvertTypeForMem(E->getType()) - ->getPointerTo( - cast(V.getType())->getAddressSpace()); - if (V.getType() != T) - LV.setAddress(Builder.CreateBitCast(V, T)); + llvm::Type *T = ConvertTypeForMem(E->getType()); + if (V.getElementType() != T) + LV.setAddress(Builder.CreateElementBitCast(V, T)); } } return LV; @@ -4763,8 +4760,9 @@ LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) { CGM.EmitExplicitCastExprType(CE, this); LValue LV = EmitLValue(E->getSubExpr()); - Address V = Builder.CreateBitCast(LV.getAddress(*this), - ConvertType(CE->getTypeAsWritten())); + Address V = Builder.CreateElementBitCast( + LV.getAddress(*this), + ConvertTypeForMem(CE->getTypeAsWritten()->getPointeeType())); if (SanOpts.has(SanitizerKind::CFIUnrelatedCast)) EmitVTablePtrCheckForCast(E->getType(), V.getPointer(),