[CGBuilder] Assert that CreateAddrSpaceCast does not change element type

Address space casts in general may change the element type, but
don't allow it in the method working on Address, so we can
preserve the element type.

CreatePointerBitCastOrAddrSpaceCast() still needs to be addressed.
This commit is contained in:
Nikita Popov 2022-02-16 14:59:42 +01:00
parent 483ae099f0
commit fe3407a91b
2 changed files with 8 additions and 6 deletions

View File

@ -154,8 +154,10 @@ public:
using CGBuilderBaseTy::CreateAddrSpaceCast;
Address CreateAddrSpaceCast(Address Addr, llvm::Type *Ty,
const llvm::Twine &Name = "") {
return Address(CreateAddrSpaceCast(Addr.getPointer(), Ty, Name),
Addr.getAlignment());
assert(cast<llvm::PointerType>(Ty)->isOpaqueOrPointeeTypeMatches(
Addr.getElementType()) &&
"Should not change the element type");
return Addr.withPointer(CreateAddrSpaceCast(Addr.getPointer(), Ty, Name));
}
/// Cast the element type of the given address to a different type,

View File

@ -1114,11 +1114,11 @@ Address CodeGenFunction::EmitPointerWithAlignment(const Expr *E,
CE->getBeginLoc());
}
if (CE->getCastKind() == CK_AddressSpaceConversion)
return Builder.CreateAddrSpaceCast(Addr, ConvertType(E->getType()));
llvm::Type *ElemTy = ConvertTypeForMem(E->getType()->getPointeeType());
return Builder.CreateElementBitCast(Addr, ElemTy);
Addr = Builder.CreateElementBitCast(Addr, ElemTy);
if (CE->getCastKind() == CK_AddressSpaceConversion)
Addr = Builder.CreateAddrSpaceCast(Addr, ConvertType(E->getType()));
return Addr;
}
break;