1 //===--- SwiftCallingConv.cpp - Lowering for the Swift calling convention -===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implementation of the abstract lowering for the Swift calling convention.
12 //===----------------------------------------------------------------------===//
14 #include "clang/CodeGen/SwiftCallingConv.h"
15 #include "clang/Basic/TargetInfo.h"
16 #include "CodeGenModule.h"
17 #include "TargetInfo.h"
19 using namespace clang;
20 using namespace CodeGen;
21 using namespace swiftcall;
23 static const SwiftABIInfo &getSwiftABIInfo(CodeGenModule &CGM) {
24 return cast<SwiftABIInfo>(CGM.getTargetCodeGenInfo().getABIInfo());
27 static bool isPowerOf2(unsigned n) {
31 /// Given two types with the same size, try to find a common type.
32 static llvm::Type *getCommonType(llvm::Type *first, llvm::Type *second) {
33 assert(first != second);
35 // Allow pointers to merge with integers, but prefer the integer type.
36 if (first->isIntegerTy()) {
37 if (second->isPointerTy()) return first;
38 } else if (first->isPointerTy()) {
39 if (second->isIntegerTy()) return second;
40 if (second->isPointerTy()) return first;
42 // Allow two vectors to be merged (given that they have the same size).
43 // This assumes that we never have two different vector register sets.
44 } else if (auto firstVecTy = dyn_cast<llvm::VectorType>(first)) {
45 if (auto secondVecTy = dyn_cast<llvm::VectorType>(second)) {
46 if (auto commonTy = getCommonType(firstVecTy->getElementType(),
47 secondVecTy->getElementType())) {
48 return (commonTy == firstVecTy->getElementType() ? first : second);
56 static CharUnits getTypeStoreSize(CodeGenModule &CGM, llvm::Type *type) {
57 return CharUnits::fromQuantity(CGM.getDataLayout().getTypeStoreSize(type));
60 static CharUnits getTypeAllocSize(CodeGenModule &CGM, llvm::Type *type) {
61 return CharUnits::fromQuantity(CGM.getDataLayout().getTypeAllocSize(type));
64 void SwiftAggLowering::addTypedData(QualType type, CharUnits begin) {
65 // Deal with various aggregate types as special cases:
68 if (auto recType = type->getAs<RecordType>()) {
69 addTypedData(recType->getDecl(), begin);
72 } else if (type->isArrayType()) {
73 // Incomplete array types (flexible array members?) don't provide
74 // data to lay out, and the other cases shouldn't be possible.
75 auto arrayType = CGM.getContext().getAsConstantArrayType(type);
76 if (!arrayType) return;
78 QualType eltType = arrayType->getElementType();
79 auto eltSize = CGM.getContext().getTypeSizeInChars(eltType);
80 for (uint64_t i = 0, e = arrayType->getSize().getZExtValue(); i != e; ++i) {
81 addTypedData(eltType, begin + i * eltSize);
85 } else if (auto complexType = type->getAs<ComplexType>()) {
86 auto eltType = complexType->getElementType();
87 auto eltSize = CGM.getContext().getTypeSizeInChars(eltType);
88 auto eltLLVMType = CGM.getTypes().ConvertType(eltType);
89 addTypedData(eltLLVMType, begin, begin + eltSize);
90 addTypedData(eltLLVMType, begin + eltSize, begin + 2 * eltSize);
92 // Member pointer types.
93 } else if (type->getAs<MemberPointerType>()) {
94 // Just add it all as opaque.
95 addOpaqueData(begin, begin + CGM.getContext().getTypeSizeInChars(type));
97 // Everything else is scalar and should not convert as an LLVM aggregate.
99 // We intentionally convert as !ForMem because we want to preserve
100 // that a type was an i1.
101 auto llvmType = CGM.getTypes().ConvertType(type);
102 addTypedData(llvmType, begin);
106 void SwiftAggLowering::addTypedData(const RecordDecl *record, CharUnits begin) {
107 addTypedData(record, begin, CGM.getContext().getASTRecordLayout(record));
110 void SwiftAggLowering::addTypedData(const RecordDecl *record, CharUnits begin,
111 const ASTRecordLayout &layout) {
112 // Unions are a special case.
113 if (record->isUnion()) {
114 for (auto field : record->fields()) {
115 if (field->isBitField()) {
116 addBitFieldData(field, begin, 0);
118 addTypedData(field->getType(), begin);
124 // Note that correctness does not rely on us adding things in
125 // their actual order of layout; it's just somewhat more efficient
128 // With that in mind, add "early" C++ data.
129 auto cxxRecord = dyn_cast<CXXRecordDecl>(record);
131 // - a v-table pointer, if the class adds its own
132 if (layout.hasOwnVFPtr()) {
133 addTypedData(CGM.Int8PtrTy, begin);
136 // - non-virtual bases
137 for (auto &baseSpecifier : cxxRecord->bases()) {
138 if (baseSpecifier.isVirtual()) continue;
140 auto baseRecord = baseSpecifier.getType()->getAsCXXRecordDecl();
141 addTypedData(baseRecord, begin + layout.getBaseClassOffset(baseRecord));
144 // - a vbptr if the class adds its own
145 if (layout.hasOwnVBPtr()) {
146 addTypedData(CGM.Int8PtrTy, begin + layout.getVBPtrOffset());
151 for (auto field : record->fields()) {
152 auto fieldOffsetInBits = layout.getFieldOffset(field->getFieldIndex());
153 if (field->isBitField()) {
154 addBitFieldData(field, begin, fieldOffsetInBits);
156 addTypedData(field->getType(),
157 begin + CGM.getContext().toCharUnitsFromBits(fieldOffsetInBits));
161 // Add "late" C++ data:
164 for (auto &vbaseSpecifier : cxxRecord->vbases()) {
165 auto baseRecord = vbaseSpecifier.getType()->getAsCXXRecordDecl();
166 addTypedData(baseRecord, begin + layout.getVBaseClassOffset(baseRecord));
171 void SwiftAggLowering::addBitFieldData(const FieldDecl *bitfield,
172 CharUnits recordBegin,
173 uint64_t bitfieldBitBegin) {
174 assert(bitfield->isBitField());
175 auto &ctx = CGM.getContext();
176 auto width = bitfield->getBitWidthValue(ctx);
178 // We can ignore zero-width bit-fields.
179 if (width == 0) return;
181 // toCharUnitsFromBits rounds down.
182 CharUnits bitfieldByteBegin = ctx.toCharUnitsFromBits(bitfieldBitBegin);
184 // Find the offset of the last byte that is partially occupied by the
185 // bit-field; since we otherwise expect exclusive ends, the end is the
187 uint64_t bitfieldBitLast = bitfieldBitBegin + width - 1;
188 CharUnits bitfieldByteEnd =
189 ctx.toCharUnitsFromBits(bitfieldBitLast) + CharUnits::One();
190 addOpaqueData(recordBegin + bitfieldByteBegin,
191 recordBegin + bitfieldByteEnd);
194 void SwiftAggLowering::addTypedData(llvm::Type *type, CharUnits begin) {
195 assert(type && "didn't provide type for typed data");
196 addTypedData(type, begin, begin + getTypeStoreSize(CGM, type));
199 void SwiftAggLowering::addTypedData(llvm::Type *type,
200 CharUnits begin, CharUnits end) {
201 assert(type && "didn't provide type for typed data");
202 assert(getTypeStoreSize(CGM, type) == end - begin);
204 // Legalize vector types.
205 if (auto vecTy = dyn_cast<llvm::VectorType>(type)) {
206 SmallVector<llvm::Type*, 4> componentTys;
207 legalizeVectorType(CGM, end - begin, vecTy, componentTys);
208 assert(componentTys.size() >= 1);
210 // Walk the initial components.
211 for (size_t i = 0, e = componentTys.size(); i != e - 1; ++i) {
212 llvm::Type *componentTy = componentTys[i];
213 auto componentSize = getTypeStoreSize(CGM, componentTy);
214 assert(componentSize < end - begin);
215 addLegalTypedData(componentTy, begin, begin + componentSize);
216 begin += componentSize;
219 return addLegalTypedData(componentTys.back(), begin, end);
222 // Legalize integer types.
223 if (auto intTy = dyn_cast<llvm::IntegerType>(type)) {
224 if (!isLegalIntegerType(CGM, intTy))
225 return addOpaqueData(begin, end);
228 // All other types should be legal.
229 return addLegalTypedData(type, begin, end);
232 void SwiftAggLowering::addLegalTypedData(llvm::Type *type,
233 CharUnits begin, CharUnits end) {
234 // Require the type to be naturally aligned.
235 if (!begin.isZero() && !begin.isMultipleOf(getNaturalAlignment(CGM, type))) {
237 // Try splitting vector types.
238 if (auto vecTy = dyn_cast<llvm::VectorType>(type)) {
239 auto split = splitLegalVectorType(CGM, end - begin, vecTy);
240 auto eltTy = split.first;
241 auto numElts = split.second;
243 auto eltSize = (end - begin) / numElts;
244 assert(eltSize == getTypeStoreSize(CGM, eltTy));
245 for (size_t i = 0, e = numElts; i != e; ++i) {
246 addLegalTypedData(eltTy, begin, begin + eltSize);
249 assert(begin == end);
253 return addOpaqueData(begin, end);
256 addEntry(type, begin, end);
259 void SwiftAggLowering::addEntry(llvm::Type *type,
260 CharUnits begin, CharUnits end) {
262 (!isa<llvm::StructType>(type) && !isa<llvm::ArrayType>(type))) &&
263 "cannot add aggregate-typed data");
264 assert(!type || begin.isMultipleOf(getNaturalAlignment(CGM, type)));
266 // Fast path: we can just add entries to the end.
267 if (Entries.empty() || Entries.back().End <= begin) {
268 Entries.push_back({begin, end, type});
272 // Find the first existing entry that ends after the start of the new data.
273 // TODO: do a binary search if Entries is big enough for it to matter.
274 size_t index = Entries.size() - 1;
276 if (Entries[index - 1].End <= begin) break;
280 // The entry ends after the start of the new data.
281 // If the entry starts after the end of the new data, there's no conflict.
282 if (Entries[index].Begin >= end) {
283 // This insertion is potentially O(n), but the way we generally build
284 // these layouts makes that unlikely to matter: we'd need a union of
285 // several very large types.
286 Entries.insert(Entries.begin() + index, {begin, end, type});
290 // Otherwise, the ranges overlap. The new range might also overlap
291 // with later ranges.
294 // Simplest case: an exact overlap.
295 if (Entries[index].Begin == begin && Entries[index].End == end) {
296 // If the types match exactly, great.
297 if (Entries[index].Type == type) return;
299 // If either type is opaque, make the entry opaque and return.
300 if (Entries[index].Type == nullptr) {
302 } else if (type == nullptr) {
303 Entries[index].Type = nullptr;
307 // If they disagree in an ABI-agnostic way, just resolve the conflict
309 if (auto entryType = getCommonType(Entries[index].Type, type)) {
310 Entries[index].Type = entryType;
314 // Otherwise, make the entry opaque.
315 Entries[index].Type = nullptr;
319 // Okay, we have an overlapping conflict of some sort.
321 // If we have a vector type, split it.
322 if (auto vecTy = dyn_cast_or_null<llvm::VectorType>(type)) {
323 auto eltTy = vecTy->getElementType();
324 CharUnits eltSize = (end - begin) / vecTy->getNumElements();
325 assert(eltSize == getTypeStoreSize(CGM, eltTy));
326 for (unsigned i = 0, e = vecTy->getNumElements(); i != e; ++i) {
327 addEntry(eltTy, begin, begin + eltSize);
330 assert(begin == end);
334 // If the entry is a vector type, split it and try again.
335 if (Entries[index].Type && Entries[index].Type->isVectorTy()) {
336 splitVectorEntry(index);
337 goto restartAfterSplit;
340 // Okay, we have no choice but to make the existing entry opaque.
342 Entries[index].Type = nullptr;
344 // Stretch the start of the entry to the beginning of the range.
345 if (begin < Entries[index].Begin) {
346 Entries[index].Begin = begin;
347 assert(index == 0 || begin >= Entries[index - 1].End);
350 // Stretch the end of the entry to the end of the range; but if we run
351 // into the start of the next entry, just leave the range there and repeat.
352 while (end > Entries[index].End) {
353 assert(Entries[index].Type == nullptr);
355 // If the range doesn't overlap the next entry, we're done.
356 if (index == Entries.size() - 1 || end <= Entries[index + 1].Begin) {
357 Entries[index].End = end;
361 // Otherwise, stretch to the start of the next entry.
362 Entries[index].End = Entries[index + 1].Begin;
364 // Continue with the next entry.
367 // This entry needs to be made opaque if it is not already.
368 if (Entries[index].Type == nullptr)
371 // Split vector entries unless we completely subsume them.
372 if (Entries[index].Type->isVectorTy() &&
373 end < Entries[index].End) {
374 splitVectorEntry(index);
377 // Make the entry opaque.
378 Entries[index].Type = nullptr;
382 /// Replace the entry of vector type at offset 'index' with a sequence
383 /// of its component vectors.
384 void SwiftAggLowering::splitVectorEntry(unsigned index) {
385 auto vecTy = cast<llvm::VectorType>(Entries[index].Type);
386 auto split = splitLegalVectorType(CGM, Entries[index].getWidth(), vecTy);
388 auto eltTy = split.first;
389 CharUnits eltSize = getTypeStoreSize(CGM, eltTy);
390 auto numElts = split.second;
391 Entries.insert(Entries.begin() + index + 1, numElts - 1, StorageEntry());
393 CharUnits begin = Entries[index].Begin;
394 for (unsigned i = 0; i != numElts; ++i) {
395 Entries[index].Type = eltTy;
396 Entries[index].Begin = begin;
397 Entries[index].End = begin + eltSize;
402 /// Given a power-of-two unit size, return the offset of the aligned unit
403 /// of that size which contains the given offset.
405 /// In other words, round down to the nearest multiple of the unit size.
406 static CharUnits getOffsetAtStartOfUnit(CharUnits offset, CharUnits unitSize) {
407 assert(isPowerOf2(unitSize.getQuantity()));
408 auto unitMask = ~(unitSize.getQuantity() - 1);
409 return CharUnits::fromQuantity(offset.getQuantity() & unitMask);
412 static bool areBytesInSameUnit(CharUnits first, CharUnits second,
413 CharUnits chunkSize) {
414 return getOffsetAtStartOfUnit(first, chunkSize)
415 == getOffsetAtStartOfUnit(second, chunkSize);
418 void SwiftAggLowering::finish() {
419 if (Entries.empty()) {
424 // We logically split the layout down into a series of chunks of this size,
425 // which is generally the size of a pointer.
426 const CharUnits chunkSize = getMaximumVoluntaryIntegerSize(CGM);
428 // First pass: if two entries share a chunk, make them both opaque
429 // and stretch one to meet the next.
430 bool hasOpaqueEntries = (Entries[0].Type == nullptr);
431 for (size_t i = 1, e = Entries.size(); i != e; ++i) {
432 if (areBytesInSameUnit(Entries[i - 1].End - CharUnits::One(),
433 Entries[i].Begin, chunkSize)) {
434 Entries[i - 1].Type = nullptr;
435 Entries[i].Type = nullptr;
436 Entries[i - 1].End = Entries[i].Begin;
437 hasOpaqueEntries = true;
439 } else if (Entries[i].Type == nullptr) {
440 hasOpaqueEntries = true;
444 // The rest of the algorithm leaves non-opaque entries alone, so if we
445 // have no opaque entries, we're done.
446 if (!hasOpaqueEntries) {
451 // Okay, move the entries to a temporary and rebuild Entries.
452 auto orig = std::move(Entries);
453 assert(Entries.empty());
455 for (size_t i = 0, e = orig.size(); i != e; ++i) {
456 // Just copy over non-opaque entries.
457 if (orig[i].Type != nullptr) {
458 Entries.push_back(orig[i]);
462 // Scan forward to determine the full extent of the next opaque range.
463 // We know from the first pass that only contiguous ranges will overlap
464 // the same aligned chunk.
465 auto begin = orig[i].Begin;
466 auto end = orig[i].End;
468 orig[i + 1].Type == nullptr &&
469 end == orig[i + 1].Begin) {
470 end = orig[i + 1].End;
474 // Add an entry per intersected chunk.
476 // Find the smallest aligned storage unit in the maximal aligned
477 // storage unit containing 'begin' that contains all the bytes in
478 // the intersection between the range and this chunk.
479 CharUnits localBegin = begin;
480 CharUnits chunkBegin = getOffsetAtStartOfUnit(localBegin, chunkSize);
481 CharUnits chunkEnd = chunkBegin + chunkSize;
482 CharUnits localEnd = std::min(end, chunkEnd);
484 // Just do a simple loop over ever-increasing unit sizes.
485 CharUnits unitSize = CharUnits::One();
486 CharUnits unitBegin, unitEnd;
487 for (; ; unitSize *= 2) {
488 assert(unitSize <= chunkSize);
489 unitBegin = getOffsetAtStartOfUnit(localBegin, unitSize);
490 unitEnd = unitBegin + unitSize;
491 if (unitEnd >= localEnd) break;
494 // Add an entry for this unit.
496 llvm::IntegerType::get(CGM.getLLVMContext(),
497 CGM.getContext().toBits(unitSize));
498 Entries.push_back({unitBegin, unitEnd, entryTy});
500 // The next chunk starts where this chunk left off.
502 } while (begin != end);
505 // Okay, finally finished.
509 void SwiftAggLowering::enumerateComponents(EnumerationCallback callback) const {
510 assert(Finished && "haven't yet finished lowering");
512 for (auto &entry : Entries) {
513 callback(entry.Begin, entry.End, entry.Type);
517 std::pair<llvm::StructType*, llvm::Type*>
518 SwiftAggLowering::getCoerceAndExpandTypes() const {
519 assert(Finished && "haven't yet finished lowering");
521 auto &ctx = CGM.getLLVMContext();
523 if (Entries.empty()) {
524 auto type = llvm::StructType::get(ctx);
525 return { type, type };
528 SmallVector<llvm::Type*, 8> elts;
529 CharUnits lastEnd = CharUnits::Zero();
530 bool hasPadding = false;
532 for (auto &entry : Entries) {
533 if (entry.Begin != lastEnd) {
534 auto paddingSize = entry.Begin - lastEnd;
535 assert(!paddingSize.isNegative());
537 auto padding = llvm::ArrayType::get(llvm::Type::getInt8Ty(ctx),
538 paddingSize.getQuantity());
539 elts.push_back(padding);
543 if (!packed && !entry.Begin.isMultipleOf(
544 CharUnits::fromQuantity(
545 CGM.getDataLayout().getABITypeAlignment(entry.Type))))
548 elts.push_back(entry.Type);
550 lastEnd = entry.Begin + getTypeAllocSize(CGM, entry.Type);
551 assert(entry.End <= lastEnd);
554 // We don't need to adjust 'packed' to deal with possible tail padding
555 // because we never do that kind of access through the coercion type.
556 auto coercionType = llvm::StructType::get(ctx, elts, packed);
558 llvm::Type *unpaddedType = coercionType;
561 for (auto &entry : Entries) {
562 elts.push_back(entry.Type);
564 if (elts.size() == 1) {
565 unpaddedType = elts[0];
567 unpaddedType = llvm::StructType::get(ctx, elts, /*packed*/ false);
569 } else if (Entries.size() == 1) {
570 unpaddedType = Entries[0].Type;
573 return { coercionType, unpaddedType };
576 bool SwiftAggLowering::shouldPassIndirectly(bool asReturnValue) const {
577 assert(Finished && "haven't yet finished lowering");
579 // Empty types don't need to be passed indirectly.
580 if (Entries.empty()) return false;
582 CharUnits totalSize = Entries.back().End;
584 // Avoid copying the array of types when there's just a single element.
585 if (Entries.size() == 1) {
586 return getSwiftABIInfo(CGM).shouldPassIndirectlyForSwift(totalSize,
591 SmallVector<llvm::Type*, 8> componentTys;
592 componentTys.reserve(Entries.size());
593 for (auto &entry : Entries) {
594 componentTys.push_back(entry.Type);
596 return getSwiftABIInfo(CGM).shouldPassIndirectlyForSwift(totalSize,
601 CharUnits swiftcall::getMaximumVoluntaryIntegerSize(CodeGenModule &CGM) {
602 // Currently always the size of an ordinary pointer.
603 return CGM.getContext().toCharUnitsFromBits(
604 CGM.getContext().getTargetInfo().getPointerWidth(0));
607 CharUnits swiftcall::getNaturalAlignment(CodeGenModule &CGM, llvm::Type *type) {
608 // For Swift's purposes, this is always just the store size of the type
609 // rounded up to a power of 2.
610 auto size = (unsigned long long) getTypeStoreSize(CGM, type).getQuantity();
611 if (!isPowerOf2(size)) {
612 size = 1ULL << (llvm::findLastSet(size, llvm::ZB_Undefined) + 1);
614 assert(size >= CGM.getDataLayout().getABITypeAlignment(type));
615 return CharUnits::fromQuantity(size);
618 bool swiftcall::isLegalIntegerType(CodeGenModule &CGM,
619 llvm::IntegerType *intTy) {
620 auto size = intTy->getBitWidth();
627 // Just assume that the above are always legal.
631 return CGM.getContext().getTargetInfo().hasInt128Type();
638 bool swiftcall::isLegalVectorType(CodeGenModule &CGM, CharUnits vectorSize,
639 llvm::VectorType *vectorTy) {
640 return isLegalVectorType(CGM, vectorSize, vectorTy->getElementType(),
641 vectorTy->getNumElements());
644 bool swiftcall::isLegalVectorType(CodeGenModule &CGM, CharUnits vectorSize,
645 llvm::Type *eltTy, unsigned numElts) {
646 assert(numElts > 1 && "illegal vector length");
647 return getSwiftABIInfo(CGM)
648 .isLegalVectorTypeForSwift(vectorSize, eltTy, numElts);
651 std::pair<llvm::Type*, unsigned>
652 swiftcall::splitLegalVectorType(CodeGenModule &CGM, CharUnits vectorSize,
653 llvm::VectorType *vectorTy) {
654 auto numElts = vectorTy->getNumElements();
655 auto eltTy = vectorTy->getElementType();
657 // Try to split the vector type in half.
658 if (numElts >= 4 && isPowerOf2(numElts)) {
659 if (isLegalVectorType(CGM, vectorSize / 2, eltTy, numElts / 2))
660 return {llvm::VectorType::get(eltTy, numElts / 2), 2};
663 return {eltTy, numElts};
666 void swiftcall::legalizeVectorType(CodeGenModule &CGM, CharUnits origVectorSize,
667 llvm::VectorType *origVectorTy,
668 llvm::SmallVectorImpl<llvm::Type*> &components) {
669 // If it's already a legal vector type, use it.
670 if (isLegalVectorType(CGM, origVectorSize, origVectorTy)) {
671 components.push_back(origVectorTy);
675 // Try to split the vector into legal subvectors.
676 auto numElts = origVectorTy->getNumElements();
677 auto eltTy = origVectorTy->getElementType();
678 assert(numElts != 1);
680 // The largest size that we're still considering making subvectors of.
681 // Always a power of 2.
682 unsigned logCandidateNumElts = llvm::findLastSet(numElts, llvm::ZB_Undefined);
683 unsigned candidateNumElts = 1U << logCandidateNumElts;
684 assert(candidateNumElts <= numElts && candidateNumElts * 2 > numElts);
686 // Minor optimization: don't check the legality of this exact size twice.
687 if (candidateNumElts == numElts) {
688 logCandidateNumElts--;
689 candidateNumElts >>= 1;
692 CharUnits eltSize = (origVectorSize / numElts);
693 CharUnits candidateSize = eltSize * candidateNumElts;
695 // The sensibility of this algorithm relies on the fact that we never
696 // have a legal non-power-of-2 vector size without having the power of 2
698 while (logCandidateNumElts > 0) {
699 assert(candidateNumElts == 1U << logCandidateNumElts);
700 assert(candidateNumElts <= numElts);
701 assert(candidateSize == eltSize * candidateNumElts);
703 // Skip illegal vector sizes.
704 if (!isLegalVectorType(CGM, candidateSize, eltTy, candidateNumElts)) {
705 logCandidateNumElts--;
706 candidateNumElts /= 2;
711 // Add the right number of vectors of this size.
712 auto numVecs = numElts >> logCandidateNumElts;
713 components.append(numVecs, llvm::VectorType::get(eltTy, candidateNumElts));
714 numElts -= (numVecs << logCandidateNumElts);
716 if (numElts == 0) return;
718 // It's possible that the number of elements remaining will be legal.
719 // This can happen with e.g. <7 x float> when <3 x float> is legal.
720 // This only needs to be separately checked if it's not a power of 2.
721 if (numElts > 2 && !isPowerOf2(numElts) &&
722 isLegalVectorType(CGM, eltSize * numElts, eltTy, numElts)) {
723 components.push_back(llvm::VectorType::get(eltTy, numElts));
727 // Bring vecSize down to something no larger than numElts.
729 logCandidateNumElts--;
730 candidateNumElts /= 2;
732 } while (candidateNumElts > numElts);
735 // Otherwise, just append a bunch of individual elements.
736 components.append(numElts, eltTy);
739 bool swiftcall::shouldPassCXXRecordIndirectly(CodeGenModule &CGM,
740 const CXXRecordDecl *record) {
741 // Following a recommendation from Richard Smith, pass a C++ type
742 // indirectly only if the destructor is non-trivial or *all* of the
743 // copy/move constructors are deleted or non-trivial.
745 if (record->hasNonTrivialDestructor())
748 // It would be nice if this were summarized on the CXXRecordDecl.
749 for (auto ctor : record->ctors()) {
750 if (ctor->isCopyOrMoveConstructor() && !ctor->isDeleted() &&
759 static ABIArgInfo classifyExpandedType(SwiftAggLowering &lowering,
761 CharUnits alignmentForIndirect) {
762 if (lowering.empty()) {
763 return ABIArgInfo::getIgnore();
764 } else if (lowering.shouldPassIndirectly(forReturn)) {
765 return ABIArgInfo::getIndirect(alignmentForIndirect, /*byval*/ false);
767 auto types = lowering.getCoerceAndExpandTypes();
768 return ABIArgInfo::getCoerceAndExpand(types.first, types.second);
772 static ABIArgInfo classifyType(CodeGenModule &CGM, CanQualType type,
774 if (auto recordType = dyn_cast<RecordType>(type)) {
775 auto record = recordType->getDecl();
776 auto &layout = CGM.getContext().getASTRecordLayout(record);
778 if (auto cxxRecord = dyn_cast<CXXRecordDecl>(record)) {
779 if (shouldPassCXXRecordIndirectly(CGM, cxxRecord))
780 return ABIArgInfo::getIndirect(layout.getAlignment(), /*byval*/ false);
783 SwiftAggLowering lowering(CGM);
784 lowering.addTypedData(recordType->getDecl(), CharUnits::Zero(), layout);
787 return classifyExpandedType(lowering, forReturn, layout.getAlignment());
790 // Just assume that all of our target ABIs can support returning at least
791 // two integer or floating-point values.
792 if (isa<ComplexType>(type)) {
793 return (forReturn ? ABIArgInfo::getDirect() : ABIArgInfo::getExpand());
796 // Vector types may need to be legalized.
797 if (isa<VectorType>(type)) {
798 SwiftAggLowering lowering(CGM);
799 lowering.addTypedData(type, CharUnits::Zero());
802 CharUnits alignment = CGM.getContext().getTypeAlignInChars(type);
803 return classifyExpandedType(lowering, forReturn, alignment);
806 // Member pointer types need to be expanded, but it's a simple form of
807 // expansion that 'Direct' can handle. Note that CanBeFlattened should be
808 // true for this to work.
810 // 'void' needs to be ignored.
811 if (type->isVoidType()) {
812 return ABIArgInfo::getIgnore();
815 // Everything else can be passed directly.
816 return ABIArgInfo::getDirect();
819 ABIArgInfo swiftcall::classifyReturnType(CodeGenModule &CGM, CanQualType type) {
820 return classifyType(CGM, type, /*forReturn*/ true);
823 ABIArgInfo swiftcall::classifyArgumentType(CodeGenModule &CGM,
825 return classifyType(CGM, type, /*forReturn*/ false);
828 void swiftcall::computeABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) {
829 auto &retInfo = FI.getReturnInfo();
830 retInfo = classifyReturnType(CGM, FI.getReturnType());
832 for (unsigned i = 0, e = FI.arg_size(); i != e; ++i) {
833 auto &argInfo = FI.arg_begin()[i];
834 argInfo.info = classifyArgumentType(CGM, argInfo.type);
838 // Is swifterror lowered to a register by the target ABI.
839 bool swiftcall::isSwiftErrorLoweredInRegister(CodeGenModule &CGM) {
840 return getSwiftABIInfo(CGM).isSwiftErrorInRegister();