1//===- TargetInfo.cpp - Target ABI information ----------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/ABI/TargetInfo.h"
10#include "llvm/ADT/STLExtras.h"
11#include "llvm/ADT/SmallVector.h"
12#include "llvm/Support/Casting.h"
13#include "llvm/Support/MathExtras.h"
14#include <algorithm>
15#include <cstdint>
16
17using namespace llvm::abi;
18using llvm::dyn_cast;
19
20bool TargetInfo::isAggregateTypeForABI(const Type *Ty) const {
21 // Atomic values use the evaluation kind of their underlying value type.
22 if (const auto *AT = dyn_cast<AtomicType>(Val: Ty))
23 return isAggregateTypeForABI(Ty: AT->getValueType());
24
25 // Check for fundamental scalar types.
26 if (Ty->isInteger() || Ty->isFloat() || Ty->isOpaqueFloat8() ||
27 Ty->isPointer() || Ty->isVector() || Ty->isTuple())
28 return false;
29
30 // Data member pointers have scalar evaluation kind.
31 if (const auto *MPT = dyn_cast<MemberPointerType>(Val: Ty))
32 return MPT->isFunctionPointer();
33
34 // A matrix type is modeled as an array but lowers to a single flattened
35 // vector and has scalar evaluation kind in classic CodeGen, so it is not an
36 // aggregate for ABI purposes.
37 if (const auto *AT = dyn_cast<ArrayType>(Val: Ty))
38 if (AT->isMatrixType())
39 return false;
40
41 // Everything else is treated as aggregate.
42 return true;
43}
44
45bool TargetInfo::isPromotableInteger(const IntegerType *IT) const {
46 // TODO: The threshold should be the target's int size rather than a
47 // hardcoded 32.
48 unsigned BitWidth = IT->getSizeInBits().getFixedValue();
49 return BitWidth < 32;
50}
51
52ArgInfo TargetInfo::getNaturalAlignIndirect(const Type *Ty, unsigned AddrSpace,
53 bool ByVal) const {
54 return ArgInfo::getIndirect(Align: Ty->getAlignment(), ByVal, AddrSpace);
55}
56
57const Type *TargetInfo::getI8Array(uint64_t NumBytes) const {
58 assert(NumBytes != 0 && "empty padding");
59 const Type *I8 = TB.getIntegerType(BitWidth: 8, Align: llvm::Align(1), /*Signed=*/false);
60 return TB.getArrayType(ElementType: I8, NumElements: NumBytes, SizeInBits: NumBytes * 8);
61}
62
63const Type *TargetInfo::getStructOfTypes(llvm::ArrayRef<const Type *> Elems,
64 bool Packed) const {
65 assert(!Elems.empty() && "empty coerce sequence");
66 llvm::SmallVector<FieldInfo, 8> Fields;
67 Fields.reserve(N: Elems.size());
68 for (const Type *Elt : Elems)
69 Fields.emplace_back(Args&: Elt, /*OffsetInBits=*/Args: 0);
70
71 StructPacking Pack = Packed ? StructPacking::Packed : StructPacking::Default;
72 return TB.getRecordType(Fields, Size: llvm::TypeSize::getFixed(ExactSize: 0), ABIAlign: llvm::Align(1),
73 /*UnadjustedAlign=*/llvm::Align(1), Pack);
74}
75
76// Returns the alignment of Ty, a type returned by convertTypeForMem, as a
77// member of the struct built there. A packed record has alignment 1. A record
78// that is not packed has the alignment of its most-aligned member. An array has
79// the alignment of its element type. A fixed-length vector is aligned to its
80// size rounded up to a power of two, which for an AArch64 fixed-length SVE type
81// is wider than the 2 or 16 bytes the source language gives it.
82static llvm::Align getConvertedAlign(const Type *Ty) {
83 if (const auto *AT = dyn_cast<ArrayType>(Val: Ty))
84 return getConvertedAlign(Ty: AT->getElementType());
85
86 if (const auto *VT = dyn_cast<VectorType>(Val: Ty); VT && VT->isFixedLength())
87 return llvm::Align(VT->getABISizeInBits() / 8);
88
89 const auto *RT = dyn_cast<RecordType>(Val: Ty);
90 if (!RT)
91 return Ty->getAlignment();
92 if (RT->getPacking() == StructPacking::Packed)
93 return llvm::Align(1);
94
95 llvm::Align MaxAlign(1);
96 for (llvm::ArrayRef<FieldInfo> Members :
97 {RT->getFields(), RT->getBaseClasses()}) {
98 for (const FieldInfo &Member : Members) {
99 if (!Member.isEmpty())
100 MaxAlign = std::max(a: MaxAlign, b: getConvertedAlign(Ty: Member.FieldType));
101 }
102 }
103 return MaxAlign;
104}
105
106const Type *TargetInfo::convertTypeForMem(const Type *Ty) const {
107 if (const auto *AT = dyn_cast<ArrayType>(Val: Ty)) {
108 if (AT->isMatrixType())
109 return Ty;
110 const Type *Elt = convertTypeForMem(Ty: AT->getElementType());
111 if (Elt == AT->getElementType())
112 return Ty;
113 assert(AT->getSizeInBits().isFixed() &&
114 "converted array element changes a scalable size");
115 return TB.getArrayType(ElementType: Elt, NumElements: AT->getNumElements(),
116 SizeInBits: AT->getSizeInBits().getFixedValue());
117 }
118
119 const auto *RT = dyn_cast<RecordType>(Val: Ty);
120 if (!RT || RT->isUnion())
121 return Ty;
122
123 // Current callers can't get here with virtual bases. If we need to handle
124 // virtual bases in the future, we'll need explicit handling for that below.
125 assert(RT->getNumVirtualBaseClasses() == 0 && "record has a virtual base");
126
127 struct ConvertedMember {
128 const Type *Ty;
129 uint64_t Offset;
130 llvm::Align Alignment;
131 };
132 llvm::SmallVector<ConvertedMember, 8> Members;
133 // The record is packed when a member offset or the record size is not a
134 // multiple of the converted alignment.
135 bool Packed = false;
136 llvm::Align MaxAlign(1);
137 auto addMember = [&](const Type *MemberTy, uint64_t Offset) {
138 const Type *ConvertedTy = convertTypeForMem(Ty: MemberTy);
139 assert(!ConvertedTy->getSizeInBits().isScalable() &&
140 "scalable member has no fixed offset");
141 llvm::Align Alignment = getConvertedAlign(Ty: ConvertedTy);
142 if (Offset % (Alignment.value() * 8) != 0)
143 Packed = true;
144 MaxAlign = std::max(a: MaxAlign, b: Alignment);
145 Members.push_back(Elt: {.Ty: ConvertedTy, .Offset: Offset, .Alignment: Alignment});
146 };
147 for (const FieldInfo &Base : RT->getBaseClasses()) {
148 if (!Base.FieldType->isEmptyRecord())
149 addMember(Base.FieldType, Base.OffsetInBits);
150 }
151 for (const FieldInfo &Field : RT->getFields()) {
152 if (!Field.isEmpty())
153 addMember(Field.FieldType, Field.OffsetInBits);
154 }
155 llvm::stable_sort(Range&: Members,
156 C: [](const ConvertedMember &A, const ConvertedMember &B) {
157 return A.Offset < B.Offset;
158 });
159
160 llvm::TypeSize RecordSize = RT->getSizeInBits();
161 if (RecordSize.isFixed() &&
162 RecordSize.getFixedValue() % (MaxAlign.value() * 8) != 0)
163 Packed = true;
164
165 llvm::SmallVector<FieldInfo, 8> Fields;
166 uint64_t Current = 0;
167 // Padding in a packed record is explicit for every gap. Padding in any
168 // other record is explicit only where the converted alignment does not place
169 // the next member.
170 auto needsPadding = [&](uint64_t Offset, llvm::Align Alignment) {
171 uint64_t AlignBits = Packed ? 8 : Alignment.value() * 8;
172 return Offset != llvm::alignTo(Value: Current, Align: AlignBits);
173 };
174 for (const ConvertedMember &Member : Members) {
175 if (Member.Offset > Current &&
176 needsPadding(Member.Offset, Member.Alignment)) {
177 uint64_t PadBits = Member.Offset - Current;
178 assert(PadBits % 8 == 0 && "padding is not a whole number of bytes");
179 Fields.emplace_back(Args: getI8Array(NumBytes: PadBits / 8), Args&: Current);
180 }
181 Fields.emplace_back(Args: Member.Ty, Args: Member.Offset);
182 Current = std::max(a: Current, b: Member.Offset +
183 Member.Ty->getSizeInBits().getFixedValue());
184 }
185
186 // The tail is placed by an integer as wide as the most aligned member, so
187 // the alignment that reaches it is capped at the widest integer's.
188 if (RecordSize.isFixed()) {
189 uint64_t Size = RecordSize.getFixedValue();
190 llvm::Align TailAlign = std::min(a: MaxAlign, b: getMaxIntegerAlign());
191 if (Size > Current && needsPadding(Size, TailAlign)) {
192 uint64_t PadBits = Size - Current;
193 assert(PadBits % 8 == 0 && "tail padding is not a whole number of bytes");
194 Fields.emplace_back(Args: getI8Array(NumBytes: PadBits / 8), Args&: Current);
195 }
196 }
197
198 StructPacking Pack = Packed ? StructPacking::Packed : StructPacking::Default;
199 return TB.getRecordType(Fields, Size: RecordSize, ABIAlign: RT->getAlignment(),
200 UnadjustedAlign: RT->getUnadjustedAlignment(), Pack);
201}
202
203RecordArgABI TargetInfo::getRecordArgABI(const RecordType *RT) const {
204 if (RT && !RT->canPassInRegisters())
205 return RAA_Indirect;
206 return RAA_Default;
207}
208
209RecordArgABI TargetInfo::getRecordArgABI(const Type *Ty) const {
210 // TODO: When Microsoft ABI is supported, CXX records may need different
211 // handling here (see MicrosoftCXXABI::getRecordArgABI in Clang).
212 const RecordType *RT = dyn_cast<RecordType>(Val: Ty);
213 if (!RT)
214 return RAA_Default;
215 return getRecordArgABI(RT);
216}
217
218const Type *TargetInfo::useFirstFieldIfTransparentUnion(const Type *Ty) const {
219 if (const auto *RT = dyn_cast<RecordType>(Val: Ty)) {
220 if (RT->isUnion() && RT->isTransparentUnion()) {
221 auto Fields = RT->getFields();
222 assert(!Fields.empty() && "transparent union cannot be empty");
223 return Fields.front().FieldType;
224 }
225 }
226 return Ty;
227}
228
229const Type *TargetInfo::isSingleElementStruct(const Type *Ty) const {
230 const auto *RT = dyn_cast<RecordType>(Val: Ty);
231 if (!RT)
232 return nullptr;
233
234 if (RT->hasFlexibleArrayMember())
235 return nullptr;
236
237 const Type *Found = nullptr;
238
239 for (const auto &Base : RT->getBaseClasses()) {
240 const Type *BaseTy = Base.FieldType;
241 const auto *BaseRT = dyn_cast<RecordType>(Val: BaseTy);
242
243 if (!BaseRT || BaseRT->isEmpty())
244 continue;
245
246 const Type *Elem = isSingleElementStruct(Ty: BaseTy);
247 if (!Elem || Found)
248 return nullptr;
249 Found = Elem;
250 }
251
252 for (const auto &FI : RT->getFields()) {
253 if (FI.isEmpty())
254 continue;
255
256 const Type *FTy = FI.FieldType;
257
258 // Treat single element arrays as the element.
259 while (const auto *AT = dyn_cast<ArrayType>(Val: FTy)) {
260 if (AT->getNumElements() != 1)
261 break;
262 FTy = AT->getElementType();
263 }
264
265 const Type *Elem;
266 if (!isAggregateTypeForABI(Ty: FTy))
267 Elem = FTy;
268 else
269 Elem = isSingleElementStruct(Ty: FTy);
270 if (!Elem || Found)
271 return nullptr;
272 Found = Elem;
273 }
274
275 if (!Found)
276 return nullptr;
277
278 // We don't consider a struct a single-element struct if it has padding
279 // beyond the element type.
280 if (Found->getABISizeInBits() != Ty->getABISizeInBits())
281 return nullptr;
282
283 return Found;
284}
285
286bool TargetInfo::maybeCommonClassifyReturnType(FunctionInfo &FI) const {
287 const abi::Type *Ty = FI.getReturnType();
288
289 // TODO: When Microsoft ABI is supported, CXX records may need different
290 // handling here (see MicrosoftCXXABI::classifyReturnType in Clang).
291 if (const auto *RT = llvm::dyn_cast<abi::RecordType>(Val: Ty)) {
292 if (!RT->canPassInRegisters()) {
293 // A record that cannot pass in registers (e.g. a non-trivial copy/dtor)
294 // is returned indirectly with ByVal=false. This is the RAA path and is
295 // distinct from getIndirectReturnResult (plain aggregates), which uses
296 // ByVal=true.
297 FI.getReturnInfo() = ArgInfo::getIndirect(
298 Align: RT->getAlignment(), /*ByVal=*/false, AddrSpace: getSRetAddrSpace());
299 return true;
300 }
301 }
302
303 return false;
304}
305
306bool TargetInfo::isHomogeneousAggregate(const Type *Ty, const Type *&Base,
307 uint64_t &Members) const {
308 bool isMatrixHA = getABICompatInfo().IsMatrixHA;
309 if (const auto *AT = dyn_cast<ArrayType>(Val: Ty)) {
310 if (!isMatrixHA && AT->isMatrixType())
311 return false;
312 uint64_t NElements = AT->getNumElements();
313 if (NElements == 0)
314 return false;
315 if (!isHomogeneousAggregate(Ty: AT->getElementType(), Base, Members))
316 return false;
317 Members *= NElements;
318 } else if (const auto *RT = dyn_cast<RecordType>(Val: Ty)) {
319 if (RT->hasFlexibleArrayMember())
320 return false;
321
322 Members = 0;
323
324 // If this is a C++ record, check bases and ABI-specific restrictions.
325 if (RT->isCXXRecord()) {
326 if (!isPermittedToBeHomogeneousAggregate(RT))
327 return false;
328
329 for (const FieldInfo &BaseField : RT->getBaseClasses()) {
330 if (BaseField.FieldType->isEmptyRecord())
331 continue;
332
333 uint64_t FldMembers = 0;
334 if (!isHomogeneousAggregate(Ty: BaseField.FieldType, Base, Members&: FldMembers))
335 return false;
336
337 Members += FldMembers;
338 }
339 }
340
341 for (const FieldInfo &FD : RT->getFields()) {
342 // Ignore (non-zero arrays of) empty records.
343 const Type *FT = FD.FieldType;
344 while (const auto *AT = dyn_cast<ArrayType>(Val: FT)) {
345 // Don't drill down to the element type of a matrix type here.
346 // That should fall through to the element isHomogeneousAggregate check.
347 if (AT->isMatrixType())
348 break;
349 if (AT->getNumElements() == 0)
350 return false;
351 FT = AT->getElementType();
352 }
353 if (FT->isEmptyRecord())
354 continue;
355
356 if (isZeroLengthBitfieldPermittedInHomogeneousAggregate() &&
357 FD.IsBitField && FD.BitFieldWidth == 0)
358 continue;
359
360 uint64_t FldMembers = 0;
361 if (!isHomogeneousAggregate(Ty: FD.FieldType, Base, Members&: FldMembers))
362 return false;
363
364 Members =
365 RT->isUnion() ? std::max(a: Members, b: FldMembers) : Members + FldMembers;
366 }
367
368 if (!Base)
369 return false;
370
371 // Ensure there is no padding.
372 if (Base->getTypeAllocSize() * Members != Ty->getTypeAllocSize())
373 return false;
374 } else {
375 Members = 1;
376 const Type *ElemTy = Ty;
377 if (const auto *CT = dyn_cast<ComplexType>(Val: Ty)) {
378 Members = 2;
379 ElemTy = CT->getElementType();
380 }
381
382 // Most ABIs only support float, double, and some vector type widths.
383 if (!isHomogeneousAggregateBaseType(Ty: ElemTy))
384 return false;
385
386 // The base type must be the same for all members. Types that agree in both
387 // total size and mode (float vs. vector) are treated as equivalent here.
388 if (!Base) {
389 Base = ElemTy;
390 // If it's a non-power-of-2 vector, its ABI size is already a power-of-2,
391 // so widen it explicitly to match Clang.
392 if (const auto *VT = dyn_cast<VectorType>(Val: Base)) {
393 assert(VT->isFixedLength() &&
394 "scalable vectors are never homogeneous aggregates");
395 uint64_t EltSize =
396 VT->getElementType()->getSizeInBits().getFixedValue();
397 unsigned NumElements =
398 VT->getTypeAllocSize().getFixedValue() * 8 / EltSize;
399 if (NumElements != VT->getNumElements().getKnownMinValue())
400 Base = TB.getVectorType(ElementType: VT->getElementType(),
401 NumElements: ElementCount::getFixed(MinVal: NumElements),
402 Align: VT->getAlignment());
403 }
404 }
405
406 if (Base->isVector() != ElemTy->isVector() ||
407 Base->getTypeAllocSize() != ElemTy->getTypeAllocSize())
408 return false;
409 }
410 return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members);
411}
412