1 //===- ABIInfoImpl.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"
11 using namespace clang
;
12 using namespace clang::CodeGen
;
14 // Pin the vtable to this file.
15 DefaultABIInfo::~DefaultABIInfo() = default;
17 ABIArgInfo
DefaultABIInfo::classifyArgumentType(QualType Ty
) const {
18 Ty
= useFirstFieldIfTransparentUnion(Ty
);
20 if (isAggregateTypeForABI(Ty
)) {
21 // Records with non-trivial destructors/copy-constructors should not be
23 if (CGCXXABI::RecordArgABI RAA
= getRecordArgABI(Ty
, getCXXABI()))
24 return getNaturalAlignIndirect(Ty
, RAA
== CGCXXABI::RAA_DirectInMemory
);
26 return getNaturalAlignIndirect(Ty
);
29 // Treat an enum type as its underlying type.
30 if (const EnumType
*EnumTy
= Ty
->getAs
<EnumType
>())
31 Ty
= EnumTy
->getDecl()->getIntegerType();
33 ASTContext
&Context
= getContext();
34 if (const auto *EIT
= Ty
->getAs
<BitIntType
>())
35 if (EIT
->getNumBits() >
36 Context
.getTypeSize(Context
.getTargetInfo().hasInt128Type()
38 : Context
.LongLongTy
))
39 return getNaturalAlignIndirect(Ty
);
41 return (isPromotableIntegerTypeForABI(Ty
) ? ABIArgInfo::getExtend(Ty
)
42 : ABIArgInfo::getDirect());
45 ABIArgInfo
DefaultABIInfo::classifyReturnType(QualType RetTy
) const {
46 if (RetTy
->isVoidType())
47 return ABIArgInfo::getIgnore();
49 if (isAggregateTypeForABI(RetTy
))
50 return getNaturalAlignIndirect(RetTy
);
52 // Treat an enum type as its underlying type.
53 if (const EnumType
*EnumTy
= RetTy
->getAs
<EnumType
>())
54 RetTy
= EnumTy
->getDecl()->getIntegerType();
56 if (const auto *EIT
= RetTy
->getAs
<BitIntType
>())
57 if (EIT
->getNumBits() >
58 getContext().getTypeSize(getContext().getTargetInfo().hasInt128Type()
59 ? getContext().Int128Ty
60 : getContext().LongLongTy
))
61 return getNaturalAlignIndirect(RetTy
);
63 return (isPromotableIntegerTypeForABI(RetTy
) ? ABIArgInfo::getExtend(RetTy
)
64 : ABIArgInfo::getDirect());
67 void DefaultABIInfo::computeInfo(CGFunctionInfo
&FI
) const {
68 if (!getCXXABI().classifyReturnType(FI
))
69 FI
.getReturnInfo() = classifyReturnType(FI
.getReturnType());
70 for (auto &I
: FI
.arguments())
71 I
.info
= classifyArgumentType(I
.type
);
74 Address
DefaultABIInfo::EmitVAArg(CodeGenFunction
&CGF
, Address VAListAddr
,
76 return EmitVAArgInstr(CGF
, VAListAddr
, Ty
, classifyArgumentType(Ty
));
79 ABIArgInfo
CodeGen::coerceToIntArray(QualType Ty
, ASTContext
&Context
,
80 llvm::LLVMContext
&LLVMContext
) {
81 // Alignment and Size are measured in bits.
82 const uint64_t Size
= Context
.getTypeSize(Ty
);
83 const uint64_t Alignment
= Context
.getTypeAlign(Ty
);
84 llvm::Type
*IntType
= llvm::Type::getIntNTy(LLVMContext
, Alignment
);
85 const uint64_t NumElements
= (Size
+ Alignment
- 1) / Alignment
;
86 return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType
, NumElements
));
89 void CodeGen::AssignToArrayRange(CodeGen::CGBuilderTy
&Builder
,
90 llvm::Value
*Array
, llvm::Value
*Value
,
91 unsigned FirstIndex
, unsigned LastIndex
) {
92 // Alternatively, we could emit this as a loop in the source.
93 for (unsigned I
= FirstIndex
; I
<= LastIndex
; ++I
) {
95 Builder
.CreateConstInBoundsGEP1_32(Builder
.getInt8Ty(), Array
, I
);
96 Builder
.CreateAlignedStore(Value
, Cell
, CharUnits::One());
100 bool CodeGen::isAggregateTypeForABI(QualType T
) {
101 return !CodeGenFunction::hasScalarEvaluationKind(T
) ||
102 T
->isMemberFunctionPointerType();
105 llvm::Type
*CodeGen::getVAListElementType(CodeGenFunction
&CGF
) {
106 return CGF
.ConvertTypeForMem(
107 CGF
.getContext().getBuiltinVaListType()->getPointeeType());
110 CGCXXABI::RecordArgABI
CodeGen::getRecordArgABI(const RecordType
*RT
,
112 const CXXRecordDecl
*RD
= dyn_cast
<CXXRecordDecl
>(RT
->getDecl());
114 if (!RT
->getDecl()->canPassInRegisters())
115 return CGCXXABI::RAA_Indirect
;
116 return CGCXXABI::RAA_Default
;
118 return CXXABI
.getRecordArgABI(RD
);
121 CGCXXABI::RecordArgABI
CodeGen::getRecordArgABI(QualType T
, CGCXXABI
&CXXABI
) {
122 const RecordType
*RT
= T
->getAs
<RecordType
>();
124 return CGCXXABI::RAA_Default
;
125 return getRecordArgABI(RT
, CXXABI
);
128 bool CodeGen::classifyReturnType(const CGCXXABI
&CXXABI
, CGFunctionInfo
&FI
,
129 const ABIInfo
&Info
) {
130 QualType Ty
= FI
.getReturnType();
132 if (const auto *RT
= Ty
->getAs
<RecordType
>())
133 if (!isa
<CXXRecordDecl
>(RT
->getDecl()) &&
134 !RT
->getDecl()->canPassInRegisters()) {
135 FI
.getReturnInfo() = Info
.getNaturalAlignIndirect(Ty
);
139 return CXXABI
.classifyReturnType(FI
);
142 QualType
CodeGen::useFirstFieldIfTransparentUnion(QualType Ty
) {
143 if (const RecordType
*UT
= Ty
->getAsUnionType()) {
144 const RecordDecl
*UD
= UT
->getDecl();
145 if (UD
->hasAttr
<TransparentUnionAttr
>()) {
146 assert(!UD
->field_empty() && "sema created an empty transparent union");
147 return UD
->field_begin()->getType();
153 llvm::Value
*CodeGen::emitRoundPointerUpToAlignment(CodeGenFunction
&CGF
,
156 // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align;
157 llvm::Value
*RoundUp
= CGF
.Builder
.CreateConstInBoundsGEP1_32(
158 CGF
.Builder
.getInt8Ty(), Ptr
, Align
.getQuantity() - 1);
159 return CGF
.Builder
.CreateIntrinsic(
160 llvm::Intrinsic::ptrmask
, {CGF
.AllocaInt8PtrTy
, CGF
.IntPtrTy
},
161 {RoundUp
, llvm::ConstantInt::get(CGF
.IntPtrTy
, -Align
.getQuantity())},
162 nullptr, Ptr
->getName() + ".aligned");
166 CodeGen::emitVoidPtrDirectVAArg(CodeGenFunction
&CGF
, Address VAListAddr
,
167 llvm::Type
*DirectTy
, CharUnits DirectSize
,
168 CharUnits DirectAlign
, CharUnits SlotSize
,
169 bool AllowHigherAlign
, bool ForceRightAdjust
) {
170 // Cast the element type to i8* if necessary. Some platforms define
171 // va_list as a struct containing an i8* instead of just an i8*.
172 if (VAListAddr
.getElementType() != CGF
.Int8PtrTy
)
173 VAListAddr
= VAListAddr
.withElementType(CGF
.Int8PtrTy
);
175 llvm::Value
*Ptr
= CGF
.Builder
.CreateLoad(VAListAddr
, "argp.cur");
177 // If the CC aligns values higher than the slot size, do so if needed.
178 Address Addr
= Address::invalid();
179 if (AllowHigherAlign
&& DirectAlign
> SlotSize
) {
180 Addr
= Address(emitRoundPointerUpToAlignment(CGF
, Ptr
, DirectAlign
),
181 CGF
.Int8Ty
, DirectAlign
);
183 Addr
= Address(Ptr
, CGF
.Int8Ty
, SlotSize
);
186 // Advance the pointer past the argument, then store that back.
187 CharUnits FullDirectSize
= DirectSize
.alignTo(SlotSize
);
189 CGF
.Builder
.CreateConstInBoundsByteGEP(Addr
, FullDirectSize
, "argp.next");
190 CGF
.Builder
.CreateStore(NextPtr
.getPointer(), VAListAddr
);
192 // If the argument is smaller than a slot, and this is a big-endian
193 // target, the argument will be right-adjusted in its slot.
194 if (DirectSize
< SlotSize
&& CGF
.CGM
.getDataLayout().isBigEndian() &&
195 (!DirectTy
->isStructTy() || ForceRightAdjust
)) {
196 Addr
= CGF
.Builder
.CreateConstInBoundsByteGEP(Addr
, SlotSize
- DirectSize
);
199 return Addr
.withElementType(DirectTy
);
202 Address
CodeGen::emitVoidPtrVAArg(CodeGenFunction
&CGF
, Address VAListAddr
,
203 QualType ValueTy
, bool IsIndirect
,
204 TypeInfoChars ValueInfo
,
205 CharUnits SlotSizeAndAlign
,
206 bool AllowHigherAlign
,
207 bool ForceRightAdjust
) {
208 // The size and alignment of the value that was passed directly.
209 CharUnits DirectSize
, DirectAlign
;
211 DirectSize
= CGF
.getPointerSize();
212 DirectAlign
= CGF
.getPointerAlign();
214 DirectSize
= ValueInfo
.Width
;
215 DirectAlign
= ValueInfo
.Align
;
218 // Cast the address we've calculated to the right type.
219 llvm::Type
*DirectTy
= CGF
.ConvertTypeForMem(ValueTy
), *ElementTy
= DirectTy
;
221 unsigned AllocaAS
= CGF
.CGM
.getDataLayout().getAllocaAddrSpace();
222 DirectTy
= llvm::PointerType::get(CGF
.getLLVMContext(), AllocaAS
);
225 Address Addr
= emitVoidPtrDirectVAArg(CGF
, VAListAddr
, DirectTy
, DirectSize
,
226 DirectAlign
, SlotSizeAndAlign
,
227 AllowHigherAlign
, ForceRightAdjust
);
230 Addr
= Address(CGF
.Builder
.CreateLoad(Addr
), ElementTy
, ValueInfo
.Align
);
236 Address
CodeGen::emitMergePHI(CodeGenFunction
&CGF
, Address Addr1
,
237 llvm::BasicBlock
*Block1
, Address Addr2
,
238 llvm::BasicBlock
*Block2
,
239 const llvm::Twine
&Name
) {
240 assert(Addr1
.getType() == Addr2
.getType());
241 llvm::PHINode
*PHI
= CGF
.Builder
.CreatePHI(Addr1
.getType(), 2, Name
);
242 PHI
->addIncoming(Addr1
.getPointer(), Block1
);
243 PHI
->addIncoming(Addr2
.getPointer(), Block2
);
244 CharUnits Align
= std::min(Addr1
.getAlignment(), Addr2
.getAlignment());
245 return Address(PHI
, Addr1
.getElementType(), Align
);
248 bool CodeGen::isEmptyField(ASTContext
&Context
, const FieldDecl
*FD
,
249 bool AllowArrays
, bool AsIfNoUniqueAddr
) {
250 if (FD
->isUnnamedBitfield())
253 QualType FT
= FD
->getType();
255 // Constant arrays of empty records count as empty, strip them off.
256 // Constant arrays of zero length always count as empty.
257 bool WasArray
= false;
259 while (const ConstantArrayType
*AT
= Context
.getAsConstantArrayType(FT
)) {
260 if (AT
->getSize() == 0)
262 FT
= AT
->getElementType();
263 // The [[no_unique_address]] special case below does not apply to
264 // arrays of C++ empty records, so we need to remember this fact.
268 const RecordType
*RT
= FT
->getAs
<RecordType
>();
272 // C++ record fields are never empty, at least in the Itanium ABI.
274 // FIXME: We should use a predicate for whether this behavior is true in the
277 // The exception to the above rule are fields marked with the
278 // [[no_unique_address]] attribute (since C++20). Those do count as empty
279 // according to the Itanium ABI. The exception applies only to records,
280 // not arrays of records, so we must also check whether we stripped off an
282 if (isa
<CXXRecordDecl
>(RT
->getDecl()) &&
283 (WasArray
|| (!AsIfNoUniqueAddr
&& !FD
->hasAttr
<NoUniqueAddressAttr
>())))
286 return isEmptyRecord(Context
, FT
, AllowArrays
, AsIfNoUniqueAddr
);
289 bool CodeGen::isEmptyRecord(ASTContext
&Context
, QualType T
, bool AllowArrays
,
290 bool AsIfNoUniqueAddr
) {
291 const RecordType
*RT
= T
->getAs
<RecordType
>();
294 const RecordDecl
*RD
= RT
->getDecl();
295 if (RD
->hasFlexibleArrayMember())
298 // If this is a C++ record, check the bases first.
299 if (const CXXRecordDecl
*CXXRD
= dyn_cast
<CXXRecordDecl
>(RD
))
300 for (const auto &I
: CXXRD
->bases())
301 if (!isEmptyRecord(Context
, I
.getType(), true, AsIfNoUniqueAddr
))
304 for (const auto *I
: RD
->fields())
305 if (!isEmptyField(Context
, I
, AllowArrays
, AsIfNoUniqueAddr
))
310 const Type
*CodeGen::isSingleElementStruct(QualType T
, ASTContext
&Context
) {
311 const RecordType
*RT
= T
->getAs
<RecordType
>();
315 const RecordDecl
*RD
= RT
->getDecl();
316 if (RD
->hasFlexibleArrayMember())
319 const Type
*Found
= nullptr;
321 // If this is a C++ record, check the bases first.
322 if (const CXXRecordDecl
*CXXRD
= dyn_cast
<CXXRecordDecl
>(RD
)) {
323 for (const auto &I
: CXXRD
->bases()) {
324 // Ignore empty records.
325 if (isEmptyRecord(Context
, I
.getType(), true))
328 // If we already found an element then this isn't a single-element struct.
332 // If this is non-empty and not a single element struct, the composite
333 // cannot be a single element struct.
334 Found
= isSingleElementStruct(I
.getType(), Context
);
340 // Check for single element.
341 for (const auto *FD
: RD
->fields()) {
342 QualType FT
= FD
->getType();
344 // Ignore empty fields.
345 if (isEmptyField(Context
, FD
, true))
348 // If we already found an element then this isn't a single-element
353 // Treat single element arrays as the element.
354 while (const ConstantArrayType
*AT
= Context
.getAsConstantArrayType(FT
)) {
355 if (AT
->getSize().getZExtValue() != 1)
357 FT
= AT
->getElementType();
360 if (!isAggregateTypeForABI(FT
)) {
361 Found
= FT
.getTypePtr();
363 Found
= isSingleElementStruct(FT
, Context
);
369 // We don't consider a struct a single-element struct if it has
370 // padding beyond the element type.
371 if (Found
&& Context
.getTypeSize(Found
) != Context
.getTypeSize(T
))
377 Address
CodeGen::EmitVAArgInstr(CodeGenFunction
&CGF
, Address VAListAddr
,
378 QualType Ty
, const ABIArgInfo
&AI
) {
379 // This default implementation defers to the llvm backend's va_arg
380 // instruction. It can handle only passing arguments directly
381 // (typically only handled in the backend for primitive types), or
382 // aggregates passed indirectly by pointer (NOTE: if the "byval"
383 // flag has ABI impact in the callee, this implementation cannot
386 // Only a few cases are covered here at the moment -- those needed
387 // by the default abi.
390 if (AI
.isIndirect()) {
391 assert(!AI
.getPaddingType() &&
392 "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
394 !AI
.getIndirectRealign() &&
395 "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!");
397 auto TyInfo
= CGF
.getContext().getTypeInfoInChars(Ty
);
398 CharUnits TyAlignForABI
= TyInfo
.Align
;
400 llvm::Type
*ElementTy
= CGF
.ConvertTypeForMem(Ty
);
401 llvm::Type
*BaseTy
= llvm::PointerType::getUnqual(ElementTy
);
403 CGF
.Builder
.CreateVAArg(VAListAddr
.getPointer(), BaseTy
);
404 return Address(Addr
, ElementTy
, TyAlignForABI
);
406 assert((AI
.isDirect() || AI
.isExtend()) &&
407 "Unexpected ArgInfo Kind in generic VAArg emitter!");
409 assert(!AI
.getInReg() &&
410 "Unexpected InReg seen in arginfo in generic VAArg emitter!");
411 assert(!AI
.getPaddingType() &&
412 "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
413 assert(!AI
.getDirectOffset() &&
414 "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!");
415 assert(!AI
.getCoerceToType() &&
416 "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!");
418 Address Temp
= CGF
.CreateMemTemp(Ty
, "varet");
419 Val
= CGF
.Builder
.CreateVAArg(VAListAddr
.getPointer(),
420 CGF
.ConvertTypeForMem(Ty
));
421 CGF
.Builder
.CreateStore(Val
, Temp
);
426 bool CodeGen::isSIMDVectorType(ASTContext
&Context
, QualType Ty
) {
427 return Ty
->getAs
<VectorType
>() && Context
.getTypeSize(Ty
) == 128;
430 bool CodeGen::isRecordWithSIMDVectorType(ASTContext
&Context
, QualType Ty
) {
431 const RecordType
*RT
= Ty
->getAs
<RecordType
>();
434 const RecordDecl
*RD
= RT
->getDecl();
436 // If this is a C++ record, check the bases first.
437 if (const CXXRecordDecl
*CXXRD
= dyn_cast
<CXXRecordDecl
>(RD
))
438 for (const auto &I
: CXXRD
->bases())
439 if (!isRecordWithSIMDVectorType(Context
, I
.getType()))
442 for (const auto *i
: RD
->fields()) {
443 QualType FT
= i
->getType();
445 if (isSIMDVectorType(Context
, FT
))
448 if (isRecordWithSIMDVectorType(Context
, FT
))