[clang] [AArch64] Change the coercion type of structs with pointer members. (PR #135064)
Eli Friedman via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 14 14:12:35 PDT 2025
================
@@ -485,6 +485,39 @@ ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty, bool IsVariadicFn,
}
Size = llvm::alignTo(Size, Alignment);
+ // If the Aggregate is made up of pointers, use an array of pointers for the
+ // coerced type. This prevents having to convert ptr2int->int2ptr through
+ // the call, allowing alias analysis to produce better code.
+ std::function<bool(QualType Ty)> ContainsOnlyPointers = [&](QualType Ty) {
+ if (isEmptyRecord(getContext(), Ty, true))
+ return false;
+ const RecordType *RT = Ty->getAs<RecordType>();
+ if (!RT)
+ return false;
+ const RecordDecl *RD = RT->getDecl();
+ if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
+ for (const auto &I : CXXRD->bases())
+ if (!ContainsOnlyPointers(I.getType()))
+ return false;
+ }
+ return all_of(RD->fields(), [&](FieldDecl *FD) {
+ QualType FDTy = FD->getType();
+ if (FDTy->isArrayType())
+ FDTy = QualType(FDTy->getBaseElementTypeUnsafe(), 0);
+ return (FDTy->isPointerOrReferenceType() &&
+ getContext().getTypeSize(FDTy) == 64) ||
+ ContainsOnlyPointers(FDTy);
+ });
+ };
+ if (ContainsOnlyPointers(Ty)) {
+ assert((Size == 64 || Size == 128) &&
+ "Expected a 64 or 128bit struct containing pointers");
+ llvm::Type *PtrTy = llvm::PointerType::getUnqual(getVMContext());
+ if (Size == 128)
+ PtrTy = llvm::ArrayType::get(PtrTy, 2);
----------------
efriedma-quic wrote:
`llvm::ArrayType::get(PtrTy, 2);` is unfortunately not correct if the alignment of the struct is 16 bytes. (Usually a struct containing only pointers should be 8-byte-aligned, but you can override it with attributes.)
https://github.com/llvm/llvm-project/pull/135064
More information about the cfe-commits
mailing list