1 //===- ARM.cpp ------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "ABIInfoImpl.h"
10 #include "TargetInfo.h"
12 using namespace clang
;
13 using namespace clang::CodeGen
;
15 //===----------------------------------------------------------------------===//
16 // ARM ABI Implementation
17 //===----------------------------------------------------------------------===//
21 class ARMABIInfo
: public ABIInfo
{
23 bool IsFloatABISoftFP
;
26 ARMABIInfo(CodeGenTypes
&CGT
, ARMABIKind Kind
) : ABIInfo(CGT
), Kind(Kind
) {
28 IsFloatABISoftFP
= CGT
.getCodeGenOpts().FloatABI
== "softfp" ||
29 CGT
.getCodeGenOpts().FloatABI
== ""; // default
33 switch (getTarget().getTriple().getEnvironment()) {
34 case llvm::Triple::Android
:
35 case llvm::Triple::EABI
:
36 case llvm::Triple::EABIHF
:
37 case llvm::Triple::GNUEABI
:
38 case llvm::Triple::GNUEABIT64
:
39 case llvm::Triple::GNUEABIHF
:
40 case llvm::Triple::GNUEABIHFT64
:
41 case llvm::Triple::MuslEABI
:
42 case llvm::Triple::MuslEABIHF
:
45 return getTarget().getTriple().isOHOSFamily();
49 bool isEABIHF() const {
50 switch (getTarget().getTriple().getEnvironment()) {
51 case llvm::Triple::EABIHF
:
52 case llvm::Triple::GNUEABIHF
:
53 case llvm::Triple::GNUEABIHFT64
:
54 case llvm::Triple::MuslEABIHF
:
61 ARMABIKind
getABIKind() const { return Kind
; }
63 bool allowBFloatArgsAndRet() const override
{
64 return !IsFloatABISoftFP
&& getTarget().hasBFloat16Type();
68 ABIArgInfo
classifyReturnType(QualType RetTy
, bool isVariadic
,
69 unsigned functionCallConv
) const;
70 ABIArgInfo
classifyArgumentType(QualType RetTy
, bool isVariadic
,
71 unsigned functionCallConv
) const;
72 ABIArgInfo
classifyHomogeneousAggregate(QualType Ty
, const Type
*Base
,
73 uint64_t Members
) const;
74 ABIArgInfo
coerceIllegalVector(QualType Ty
) const;
75 bool isIllegalVectorType(QualType Ty
) const;
76 bool containsAnyFP16Vectors(QualType Ty
) const;
78 bool isHomogeneousAggregateBaseType(QualType Ty
) const override
;
79 bool isHomogeneousAggregateSmallEnough(const Type
*Ty
,
80 uint64_t Members
) const override
;
81 bool isZeroLengthBitfieldPermittedInHomogeneousAggregate() const override
;
83 bool isEffectivelyAAPCS_VFP(unsigned callConvention
, bool acceptHalf
) const;
85 void computeInfo(CGFunctionInfo
&FI
) const override
;
87 RValue
EmitVAArg(CodeGenFunction
&CGF
, Address VAListAddr
, QualType Ty
,
88 AggValueSlot Slot
) const override
;
90 llvm::CallingConv::ID
getLLVMDefaultCC() const;
91 llvm::CallingConv::ID
getABIDefaultCC() const;
95 class ARMSwiftABIInfo
: public SwiftABIInfo
{
97 explicit ARMSwiftABIInfo(CodeGenTypes
&CGT
)
98 : SwiftABIInfo(CGT
, /*SwiftErrorInRegister=*/true) {}
100 bool isLegalVectorType(CharUnits VectorSize
, llvm::Type
*EltTy
,
101 unsigned NumElts
) const override
;
104 class ARMTargetCodeGenInfo
: public TargetCodeGenInfo
{
106 ARMTargetCodeGenInfo(CodeGenTypes
&CGT
, ARMABIKind K
)
107 : TargetCodeGenInfo(std::make_unique
<ARMABIInfo
>(CGT
, K
)) {
108 SwiftInfo
= std::make_unique
<ARMSwiftABIInfo
>(CGT
);
111 int getDwarfEHStackPointer(CodeGen::CodeGenModule
&M
) const override
{
115 StringRef
getARCRetainAutoreleasedReturnValueMarker() const override
{
116 return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue";
119 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction
&CGF
,
120 llvm::Value
*Address
) const override
{
121 llvm::Value
*Four8
= llvm::ConstantInt::get(CGF
.Int8Ty
, 4);
123 // 0-15 are the 16 integer registers.
124 AssignToArrayRange(CGF
.Builder
, Address
, Four8
, 0, 15);
128 unsigned getSizeOfUnwindException() const override
{
129 if (getABIInfo
<ARMABIInfo
>().isEABI())
131 return TargetCodeGenInfo::getSizeOfUnwindException();
134 void setTargetAttributes(const Decl
*D
, llvm::GlobalValue
*GV
,
135 CodeGen::CodeGenModule
&CGM
) const override
{
136 if (GV
->isDeclaration())
138 const FunctionDecl
*FD
= dyn_cast_or_null
<FunctionDecl
>(D
);
141 auto *Fn
= cast
<llvm::Function
>(GV
);
143 if (const auto *TA
= FD
->getAttr
<TargetAttr
>()) {
144 ParsedTargetAttr Attr
=
145 CGM
.getTarget().parseTargetAttr(TA
->getFeaturesStr());
146 if (!Attr
.BranchProtection
.empty()) {
147 TargetInfo::BranchProtectionInfo BPI
{};
150 Attr
.CPU
.empty() ? CGM
.getTarget().getTargetOpts().CPU
: Attr
.CPU
;
151 if (!CGM
.getTarget().validateBranchProtection(Attr
.BranchProtection
,
152 Arch
, BPI
, DiagMsg
)) {
153 CGM
.getDiags().Report(
155 diag::warn_target_unsupported_branch_protection_attribute
)
158 setBranchProtectionFnAttributes(BPI
, (*Fn
));
159 } else if (CGM
.getLangOpts().BranchTargetEnforcement
||
160 CGM
.getLangOpts().hasSignReturnAddress()) {
161 // If the Branch Protection attribute is missing, validate the target
162 // Architecture attribute against Branch Protection command line
164 if (!CGM
.getTarget().isBranchProtectionSupportedArch(Attr
.CPU
))
165 CGM
.getDiags().Report(
167 diag::warn_target_unsupported_branch_protection_attribute
)
170 } else if (CGM
.getTarget().isBranchProtectionSupportedArch(
171 CGM
.getTarget().getTargetOpts().CPU
)) {
172 TargetInfo::BranchProtectionInfo
BPI(CGM
.getLangOpts());
173 setBranchProtectionFnAttributes(BPI
, (*Fn
));
176 const ARMInterruptAttr
*Attr
= FD
->getAttr
<ARMInterruptAttr
>();
181 switch (Attr
->getInterrupt()) {
182 case ARMInterruptAttr::Generic
: Kind
= ""; break;
183 case ARMInterruptAttr::IRQ
: Kind
= "IRQ"; break;
184 case ARMInterruptAttr::FIQ
: Kind
= "FIQ"; break;
185 case ARMInterruptAttr::SWI
: Kind
= "SWI"; break;
186 case ARMInterruptAttr::ABORT
: Kind
= "ABORT"; break;
187 case ARMInterruptAttr::UNDEF
: Kind
= "UNDEF"; break;
190 Fn
->addFnAttr("interrupt", Kind
);
192 ARMABIKind ABI
= getABIInfo
<ARMABIInfo
>().getABIKind();
193 if (ABI
== ARMABIKind::APCS
)
196 // AAPCS guarantees that sp will be 8-byte aligned on any public interface,
197 // however this is not necessarily true on taking any interrupt. Instruct
198 // the backend to perform a realignment as part of the function prologue.
199 llvm::AttrBuilder
B(Fn
->getContext());
200 B
.addStackAlignmentAttr(8);
205 class WindowsARMTargetCodeGenInfo
: public ARMTargetCodeGenInfo
{
207 WindowsARMTargetCodeGenInfo(CodeGenTypes
&CGT
, ARMABIKind K
)
208 : ARMTargetCodeGenInfo(CGT
, K
) {}
210 void setTargetAttributes(const Decl
*D
, llvm::GlobalValue
*GV
,
211 CodeGen::CodeGenModule
&CGM
) const override
;
213 void getDependentLibraryOption(llvm::StringRef Lib
,
214 llvm::SmallString
<24> &Opt
) const override
{
215 Opt
= "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib
);
218 void getDetectMismatchOption(llvm::StringRef Name
, llvm::StringRef Value
,
219 llvm::SmallString
<32> &Opt
) const override
{
220 Opt
= "/FAILIFMISMATCH:\"" + Name
.str() + "=" + Value
.str() + "\"";
224 void WindowsARMTargetCodeGenInfo::setTargetAttributes(
225 const Decl
*D
, llvm::GlobalValue
*GV
, CodeGen::CodeGenModule
&CGM
) const {
226 ARMTargetCodeGenInfo::setTargetAttributes(D
, GV
, CGM
);
227 if (GV
->isDeclaration())
229 addStackProbeTargetAttributes(D
, GV
, CGM
);
233 void ARMABIInfo::computeInfo(CGFunctionInfo
&FI
) const {
234 if (!::classifyReturnType(getCXXABI(), FI
, *this))
235 FI
.getReturnInfo() = classifyReturnType(FI
.getReturnType(), FI
.isVariadic(),
236 FI
.getCallingConvention());
238 for (auto &I
: FI
.arguments())
239 I
.info
= classifyArgumentType(I
.type
, FI
.isVariadic(),
240 FI
.getCallingConvention());
243 // Always honor user-specified calling convention.
244 if (FI
.getCallingConvention() != llvm::CallingConv::C
)
247 llvm::CallingConv::ID cc
= getRuntimeCC();
248 if (cc
!= llvm::CallingConv::C
)
249 FI
.setEffectiveCallingConvention(cc
);
252 /// Return the default calling convention that LLVM will use.
253 llvm::CallingConv::ID
ARMABIInfo::getLLVMDefaultCC() const {
254 // The default calling convention that LLVM will infer.
255 if (isEABIHF() || getTarget().getTriple().isWatchABI())
256 return llvm::CallingConv::ARM_AAPCS_VFP
;
258 return llvm::CallingConv::ARM_AAPCS
;
260 return llvm::CallingConv::ARM_APCS
;
263 /// Return the calling convention that our ABI would like us to use
264 /// as the C calling convention.
265 llvm::CallingConv::ID
ARMABIInfo::getABIDefaultCC() const {
266 switch (getABIKind()) {
267 case ARMABIKind::APCS
:
268 return llvm::CallingConv::ARM_APCS
;
269 case ARMABIKind::AAPCS
:
270 return llvm::CallingConv::ARM_AAPCS
;
271 case ARMABIKind::AAPCS_VFP
:
272 return llvm::CallingConv::ARM_AAPCS_VFP
;
273 case ARMABIKind::AAPCS16_VFP
:
274 return llvm::CallingConv::ARM_AAPCS_VFP
;
276 llvm_unreachable("bad ABI kind");
279 void ARMABIInfo::setCCs() {
280 assert(getRuntimeCC() == llvm::CallingConv::C
);
282 // Don't muddy up the IR with a ton of explicit annotations if
283 // they'd just match what LLVM will infer from the triple.
284 llvm::CallingConv::ID abiCC
= getABIDefaultCC();
285 if (abiCC
!= getLLVMDefaultCC())
289 ABIArgInfo
ARMABIInfo::coerceIllegalVector(QualType Ty
) const {
290 uint64_t Size
= getContext().getTypeSize(Ty
);
292 llvm::Type
*ResType
=
293 llvm::Type::getInt32Ty(getVMContext());
294 return ABIArgInfo::getDirect(ResType
);
296 if (Size
== 64 || Size
== 128) {
297 auto *ResType
= llvm::FixedVectorType::get(
298 llvm::Type::getInt32Ty(getVMContext()), Size
/ 32);
299 return ABIArgInfo::getDirect(ResType
);
301 return getNaturalAlignIndirect(Ty
, /*ByVal=*/false);
304 ABIArgInfo
ARMABIInfo::classifyHomogeneousAggregate(QualType Ty
,
306 uint64_t Members
) const {
307 assert(Base
&& "Base class should be set for homogeneous aggregate");
308 // Base can be a floating-point or a vector.
309 if (const VectorType
*VT
= Base
->getAs
<VectorType
>()) {
310 // FP16 vectors should be converted to integer vectors
311 if (!getTarget().hasLegalHalfType() && containsAnyFP16Vectors(Ty
)) {
312 uint64_t Size
= getContext().getTypeSize(VT
);
313 auto *NewVecTy
= llvm::FixedVectorType::get(
314 llvm::Type::getInt32Ty(getVMContext()), Size
/ 32);
315 llvm::Type
*Ty
= llvm::ArrayType::get(NewVecTy
, Members
);
316 return ABIArgInfo::getDirect(Ty
, 0, nullptr, false);
320 if (getABIKind() == ARMABIKind::AAPCS
||
321 getABIKind() == ARMABIKind::AAPCS_VFP
) {
322 // For alignment adjusted HFAs, cap the argument alignment to 8, leave it
323 // default otherwise.
324 Align
= getContext().getTypeUnadjustedAlignInChars(Ty
).getQuantity();
325 unsigned BaseAlign
= getContext().getTypeAlignInChars(Base
).getQuantity();
326 Align
= (Align
> BaseAlign
&& Align
>= 8) ? 8 : 0;
328 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false, Align
);
331 ABIArgInfo
ARMABIInfo::classifyArgumentType(QualType Ty
, bool isVariadic
,
332 unsigned functionCallConv
) const {
333 // 6.1.2.1 The following argument types are VFP CPRCs:
334 // A single-precision floating-point type (including promoted
335 // half-precision types); A double-precision floating-point type;
336 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
337 // with a Base Type of a single- or double-precision floating-point type,
338 // 64-bit containerized vectors or 128-bit containerized vectors with one
340 // Variadic functions should always marshal to the base standard.
342 !isVariadic
&& isEffectivelyAAPCS_VFP(functionCallConv
, /* AAPCS16 */ false);
344 Ty
= useFirstFieldIfTransparentUnion(Ty
);
346 // Handle illegal vector types here.
347 if (isIllegalVectorType(Ty
))
348 return coerceIllegalVector(Ty
);
350 if (!isAggregateTypeForABI(Ty
)) {
351 // Treat an enum type as its underlying type.
352 if (const EnumType
*EnumTy
= Ty
->getAs
<EnumType
>()) {
353 Ty
= EnumTy
->getDecl()->getIntegerType();
356 if (const auto *EIT
= Ty
->getAs
<BitIntType
>())
357 if (EIT
->getNumBits() > 64)
358 return getNaturalAlignIndirect(Ty
, /*ByVal=*/true);
360 return (isPromotableIntegerTypeForABI(Ty
)
361 ? ABIArgInfo::getExtend(Ty
, CGT
.ConvertType(Ty
))
362 : ABIArgInfo::getDirect());
365 if (CGCXXABI::RecordArgABI RAA
= getRecordArgABI(Ty
, getCXXABI())) {
366 return getNaturalAlignIndirect(Ty
, RAA
== CGCXXABI::RAA_DirectInMemory
);
369 // Ignore empty records.
370 if (isEmptyRecord(getContext(), Ty
, true))
371 return ABIArgInfo::getIgnore();
374 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
375 // into VFP registers.
376 const Type
*Base
= nullptr;
377 uint64_t Members
= 0;
378 if (isHomogeneousAggregate(Ty
, Base
, Members
))
379 return classifyHomogeneousAggregate(Ty
, Base
, Members
);
380 } else if (getABIKind() == ARMABIKind::AAPCS16_VFP
) {
381 // WatchOS does have homogeneous aggregates. Note that we intentionally use
382 // this convention even for a variadic function: the backend will use GPRs
384 const Type
*Base
= nullptr;
385 uint64_t Members
= 0;
386 if (isHomogeneousAggregate(Ty
, Base
, Members
)) {
387 assert(Base
&& Members
<= 4 && "unexpected homogeneous aggregate");
389 llvm::ArrayType::get(CGT
.ConvertType(QualType(Base
, 0)), Members
);
390 return ABIArgInfo::getDirect(Ty
, 0, nullptr, false);
394 if (getABIKind() == ARMABIKind::AAPCS16_VFP
&&
395 getContext().getTypeSizeInChars(Ty
) > CharUnits::fromQuantity(16)) {
396 // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're
397 // bigger than 128-bits, they get placed in space allocated by the caller,
398 // and a pointer is passed.
399 return ABIArgInfo::getIndirect(
400 CharUnits::fromQuantity(getContext().getTypeAlign(Ty
) / 8), false);
403 // Support byval for ARM.
404 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
405 // most 8-byte. We realign the indirect argument if type alignment is bigger
406 // than ABI alignment.
407 uint64_t ABIAlign
= 4;
409 if (getABIKind() == ARMABIKind::AAPCS_VFP
||
410 getABIKind() == ARMABIKind::AAPCS
) {
411 TyAlign
= getContext().getTypeUnadjustedAlignInChars(Ty
).getQuantity();
412 ABIAlign
= std::clamp(TyAlign
, (uint64_t)4, (uint64_t)8);
414 TyAlign
= getContext().getTypeAlignInChars(Ty
).getQuantity();
416 if (getContext().getTypeSizeInChars(Ty
) > CharUnits::fromQuantity(64)) {
417 assert(getABIKind() != ARMABIKind::AAPCS16_VFP
&& "unexpected byval");
418 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign
),
420 /*Realign=*/TyAlign
> ABIAlign
);
423 // Otherwise, pass by coercing to a structure of the appropriate size.
426 // FIXME: Try to match the types of the arguments more accurately where
429 ElemTy
= llvm::Type::getInt32Ty(getVMContext());
430 SizeRegs
= (getContext().getTypeSize(Ty
) + 31) / 32;
432 ElemTy
= llvm::Type::getInt64Ty(getVMContext());
433 SizeRegs
= (getContext().getTypeSize(Ty
) + 63) / 64;
436 return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy
, SizeRegs
));
439 static bool isIntegerLikeType(QualType Ty
, ASTContext
&Context
,
440 llvm::LLVMContext
&VMContext
) {
441 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
442 // is called integer-like if its size is less than or equal to one word, and
443 // the offset of each of its addressable sub-fields is zero.
445 uint64_t Size
= Context
.getTypeSize(Ty
);
447 // Check that the type fits in a word.
451 // FIXME: Handle vector types!
452 if (Ty
->isVectorType())
455 // Float types are never treated as "integer like".
456 if (Ty
->isRealFloatingType())
459 // If this is a builtin or pointer type then it is ok.
460 if (Ty
->getAs
<BuiltinType
>() || Ty
->isPointerType())
463 // Small complex integer types are "integer like".
464 if (const ComplexType
*CT
= Ty
->getAs
<ComplexType
>())
465 return isIntegerLikeType(CT
->getElementType(), Context
, VMContext
);
467 // Single element and zero sized arrays should be allowed, by the definition
468 // above, but they are not.
470 // Otherwise, it must be a record type.
471 const RecordType
*RT
= Ty
->getAs
<RecordType
>();
472 if (!RT
) return false;
474 // Ignore records with flexible arrays.
475 const RecordDecl
*RD
= RT
->getDecl();
476 if (RD
->hasFlexibleArrayMember())
479 // Check that all sub-fields are at offset 0, and are themselves "integer
481 const ASTRecordLayout
&Layout
= Context
.getASTRecordLayout(RD
);
483 bool HadField
= false;
485 for (RecordDecl::field_iterator i
= RD
->field_begin(), e
= RD
->field_end();
486 i
!= e
; ++i
, ++idx
) {
487 const FieldDecl
*FD
= *i
;
489 // Bit-fields are not addressable, we only need to verify they are "integer
490 // like". We still have to disallow a subsequent non-bitfield, for example:
491 // struct { int : 0; int x }
492 // is non-integer like according to gcc.
493 if (FD
->isBitField()) {
497 if (!isIntegerLikeType(FD
->getType(), Context
, VMContext
))
503 // Check if this field is at offset 0.
504 if (Layout
.getFieldOffset(idx
) != 0)
507 if (!isIntegerLikeType(FD
->getType(), Context
, VMContext
))
510 // Only allow at most one field in a structure. This doesn't match the
511 // wording above, but follows gcc in situations with a field following an
513 if (!RD
->isUnion()) {
524 ABIArgInfo
ARMABIInfo::classifyReturnType(QualType RetTy
, bool isVariadic
,
525 unsigned functionCallConv
) const {
527 // Variadic functions should always marshal to the base standard.
529 !isVariadic
&& isEffectivelyAAPCS_VFP(functionCallConv
, /* AAPCS16 */ true);
531 if (RetTy
->isVoidType())
532 return ABIArgInfo::getIgnore();
534 if (const VectorType
*VT
= RetTy
->getAs
<VectorType
>()) {
535 // Large vector types should be returned via memory.
536 if (getContext().getTypeSize(RetTy
) > 128)
537 return getNaturalAlignIndirect(RetTy
);
538 // TODO: FP16/BF16 vectors should be converted to integer vectors
539 // This check is similar to isIllegalVectorType - refactor?
540 if ((!getTarget().hasLegalHalfType() &&
541 (VT
->getElementType()->isFloat16Type() ||
542 VT
->getElementType()->isHalfType())) ||
544 VT
->getElementType()->isBFloat16Type()))
545 return coerceIllegalVector(RetTy
);
548 if (!isAggregateTypeForABI(RetTy
)) {
549 // Treat an enum type as its underlying type.
550 if (const EnumType
*EnumTy
= RetTy
->getAs
<EnumType
>())
551 RetTy
= EnumTy
->getDecl()->getIntegerType();
553 if (const auto *EIT
= RetTy
->getAs
<BitIntType
>())
554 if (EIT
->getNumBits() > 64)
555 return getNaturalAlignIndirect(RetTy
, /*ByVal=*/false);
557 return isPromotableIntegerTypeForABI(RetTy
) ? ABIArgInfo::getExtend(RetTy
)
558 : ABIArgInfo::getDirect();
561 // Are we following APCS?
562 if (getABIKind() == ARMABIKind::APCS
) {
563 if (isEmptyRecord(getContext(), RetTy
, false))
564 return ABIArgInfo::getIgnore();
566 // Complex types are all returned as packed integers.
568 // FIXME: Consider using 2 x vector types if the back end handles them
570 if (RetTy
->isAnyComplexType())
571 return ABIArgInfo::getDirect(llvm::IntegerType::get(
572 getVMContext(), getContext().getTypeSize(RetTy
)));
574 // Integer like structures are returned in r0.
575 if (isIntegerLikeType(RetTy
, getContext(), getVMContext())) {
576 // Return in the smallest viable integer type.
577 uint64_t Size
= getContext().getTypeSize(RetTy
);
579 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
581 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
582 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
585 // Otherwise return in memory.
586 return getNaturalAlignIndirect(RetTy
);
589 // Otherwise this is an AAPCS variant.
591 if (isEmptyRecord(getContext(), RetTy
, true))
592 return ABIArgInfo::getIgnore();
594 // Check for homogeneous aggregates with AAPCS-VFP.
596 const Type
*Base
= nullptr;
597 uint64_t Members
= 0;
598 if (isHomogeneousAggregate(RetTy
, Base
, Members
))
599 return classifyHomogeneousAggregate(RetTy
, Base
, Members
);
602 // Aggregates <= 4 bytes are returned in r0; other aggregates
603 // are returned indirectly.
604 uint64_t Size
= getContext().getTypeSize(RetTy
);
606 if (getDataLayout().isBigEndian())
607 // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4)
608 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
610 // Return in the smallest viable integer type.
612 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
614 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
615 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
616 } else if (Size
<= 128 && getABIKind() == ARMABIKind::AAPCS16_VFP
) {
617 llvm::Type
*Int32Ty
= llvm::Type::getInt32Ty(getVMContext());
618 llvm::Type
*CoerceTy
=
619 llvm::ArrayType::get(Int32Ty
, llvm::alignTo(Size
, 32) / 32);
620 return ABIArgInfo::getDirect(CoerceTy
);
623 return getNaturalAlignIndirect(RetTy
);
626 /// isIllegalVector - check whether Ty is an illegal vector type.
627 bool ARMABIInfo::isIllegalVectorType(QualType Ty
) const {
628 if (const VectorType
*VT
= Ty
->getAs
<VectorType
> ()) {
629 // On targets that don't support half, fp16 or bfloat, they are expanded
630 // into float, and we don't want the ABI to depend on whether or not they
631 // are supported in hardware. Thus return false to coerce vectors of these
632 // types into integer vectors.
633 // We do not depend on hasLegalHalfType for bfloat as it is a
635 if ((!getTarget().hasLegalHalfType() &&
636 (VT
->getElementType()->isFloat16Type() ||
637 VT
->getElementType()->isHalfType())) ||
639 VT
->getElementType()->isBFloat16Type()))
642 // Android shipped using Clang 3.1, which supported a slightly different
643 // vector ABI. The primary differences were that 3-element vector types
644 // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path
645 // accepts that legacy behavior for Android only.
646 // Check whether VT is legal.
647 unsigned NumElements
= VT
->getNumElements();
648 // NumElements should be power of 2 or equal to 3.
649 if (!llvm::isPowerOf2_32(NumElements
) && NumElements
!= 3)
652 // Check whether VT is legal.
653 unsigned NumElements
= VT
->getNumElements();
654 uint64_t Size
= getContext().getTypeSize(VT
);
655 // NumElements should be power of 2.
656 if (!llvm::isPowerOf2_32(NumElements
))
658 // Size should be greater than 32 bits.
665 /// Return true if a type contains any 16-bit floating point vectors
666 bool ARMABIInfo::containsAnyFP16Vectors(QualType Ty
) const {
667 if (const ConstantArrayType
*AT
= getContext().getAsConstantArrayType(Ty
)) {
668 uint64_t NElements
= AT
->getZExtSize();
671 return containsAnyFP16Vectors(AT
->getElementType());
672 } else if (const RecordType
*RT
= Ty
->getAs
<RecordType
>()) {
673 const RecordDecl
*RD
= RT
->getDecl();
675 // If this is a C++ record, check the bases first.
676 if (const CXXRecordDecl
*CXXRD
= dyn_cast
<CXXRecordDecl
>(RD
))
677 if (llvm::any_of(CXXRD
->bases(), [this](const CXXBaseSpecifier
&B
) {
678 return containsAnyFP16Vectors(B
.getType());
682 if (llvm::any_of(RD
->fields(), [this](FieldDecl
*FD
) {
683 return FD
&& containsAnyFP16Vectors(FD
->getType());
689 if (const VectorType
*VT
= Ty
->getAs
<VectorType
>())
690 return (VT
->getElementType()->isFloat16Type() ||
691 VT
->getElementType()->isBFloat16Type() ||
692 VT
->getElementType()->isHalfType());
697 bool ARMSwiftABIInfo::isLegalVectorType(CharUnits VectorSize
, llvm::Type
*EltTy
,
698 unsigned NumElts
) const {
699 if (!llvm::isPowerOf2_32(NumElts
))
701 unsigned size
= CGT
.getDataLayout().getTypeStoreSizeInBits(EltTy
);
704 if (VectorSize
.getQuantity() != 8 &&
705 (VectorSize
.getQuantity() != 16 || NumElts
== 1))
710 bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty
) const {
711 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
712 // double, or 64-bit or 128-bit vectors.
713 if (const BuiltinType
*BT
= Ty
->getAs
<BuiltinType
>()) {
714 if (BT
->getKind() == BuiltinType::Float
||
715 BT
->getKind() == BuiltinType::Double
||
716 BT
->getKind() == BuiltinType::LongDouble
)
718 } else if (const VectorType
*VT
= Ty
->getAs
<VectorType
>()) {
719 unsigned VecSize
= getContext().getTypeSize(VT
);
720 if (VecSize
== 64 || VecSize
== 128)
726 bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type
*Base
,
727 uint64_t Members
) const {
731 bool ARMABIInfo::isZeroLengthBitfieldPermittedInHomogeneousAggregate() const {
732 // AAPCS32 says that the rule for whether something is a homogeneous
733 // aggregate is applied to the output of the data layout decision. So
734 // anything that doesn't affect the data layout also does not affect
735 // homogeneity. In particular, zero-length bitfields don't stop a struct
736 // being homogeneous.
740 bool ARMABIInfo::isEffectivelyAAPCS_VFP(unsigned callConvention
,
741 bool acceptHalf
) const {
742 // Give precedence to user-specified calling conventions.
743 if (callConvention
!= llvm::CallingConv::C
)
744 return (callConvention
== llvm::CallingConv::ARM_AAPCS_VFP
);
746 return (getABIKind() == ARMABIKind::AAPCS_VFP
) ||
747 (acceptHalf
&& (getABIKind() == ARMABIKind::AAPCS16_VFP
));
750 RValue
ARMABIInfo::EmitVAArg(CodeGenFunction
&CGF
, Address VAListAddr
,
751 QualType Ty
, AggValueSlot Slot
) const {
752 CharUnits SlotSize
= CharUnits::fromQuantity(4);
754 // Empty records are ignored for parameter passing purposes.
755 if (isEmptyRecord(getContext(), Ty
, true))
756 return Slot
.asRValue();
758 CharUnits TySize
= getContext().getTypeSizeInChars(Ty
);
759 CharUnits TyAlignForABI
= getContext().getTypeUnadjustedAlignInChars(Ty
);
761 // Use indirect if size of the illegal vector is bigger than 16 bytes.
762 bool IsIndirect
= false;
763 const Type
*Base
= nullptr;
764 uint64_t Members
= 0;
765 if (TySize
> CharUnits::fromQuantity(16) && isIllegalVectorType(Ty
)) {
768 // ARMv7k passes structs bigger than 16 bytes indirectly, in space
769 // allocated by the caller.
770 } else if (TySize
> CharUnits::fromQuantity(16) &&
771 getABIKind() == ARMABIKind::AAPCS16_VFP
&&
772 !isHomogeneousAggregate(Ty
, Base
, Members
)) {
775 // Otherwise, bound the type's ABI alignment.
776 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
777 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
778 // Our callers should be prepared to handle an under-aligned address.
779 } else if (getABIKind() == ARMABIKind::AAPCS_VFP
||
780 getABIKind() == ARMABIKind::AAPCS
) {
781 TyAlignForABI
= std::max(TyAlignForABI
, CharUnits::fromQuantity(4));
782 TyAlignForABI
= std::min(TyAlignForABI
, CharUnits::fromQuantity(8));
783 } else if (getABIKind() == ARMABIKind::AAPCS16_VFP
) {
784 // ARMv7k allows type alignment up to 16 bytes.
785 TyAlignForABI
= std::max(TyAlignForABI
, CharUnits::fromQuantity(4));
786 TyAlignForABI
= std::min(TyAlignForABI
, CharUnits::fromQuantity(16));
788 TyAlignForABI
= CharUnits::fromQuantity(4);
791 TypeInfoChars
TyInfo(TySize
, TyAlignForABI
, AlignRequirementKind::None
);
792 return emitVoidPtrVAArg(CGF
, VAListAddr
, Ty
, IsIndirect
, TyInfo
, SlotSize
,
793 /*AllowHigherAlign*/ true, Slot
);
796 std::unique_ptr
<TargetCodeGenInfo
>
797 CodeGen::createARMTargetCodeGenInfo(CodeGenModule
&CGM
, ARMABIKind Kind
) {
798 return std::make_unique
<ARMTargetCodeGenInfo
>(CGM
.getTypes(), Kind
);
801 std::unique_ptr
<TargetCodeGenInfo
>
802 CodeGen::createWindowsARMTargetCodeGenInfo(CodeGenModule
&CGM
, ARMABIKind K
) {
803 return std::make_unique
<WindowsARMTargetCodeGenInfo
>(CGM
.getTypes(), K
);