17330f729Sjoerg //===--- SwiftCallingConv.cpp - Lowering for the Swift calling convention -===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // Implementation of the abstract lowering for the Swift calling convention.
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
127330f729Sjoerg
137330f729Sjoerg #include "clang/CodeGen/SwiftCallingConv.h"
147330f729Sjoerg #include "clang/Basic/TargetInfo.h"
157330f729Sjoerg #include "CodeGenModule.h"
167330f729Sjoerg #include "TargetInfo.h"
177330f729Sjoerg
187330f729Sjoerg using namespace clang;
197330f729Sjoerg using namespace CodeGen;
207330f729Sjoerg using namespace swiftcall;
217330f729Sjoerg
getSwiftABIInfo(CodeGenModule & CGM)227330f729Sjoerg static const SwiftABIInfo &getSwiftABIInfo(CodeGenModule &CGM) {
237330f729Sjoerg return cast<SwiftABIInfo>(CGM.getTargetCodeGenInfo().getABIInfo());
247330f729Sjoerg }
257330f729Sjoerg
isPowerOf2(unsigned n)267330f729Sjoerg static bool isPowerOf2(unsigned n) {
277330f729Sjoerg return n == (n & -n);
287330f729Sjoerg }
297330f729Sjoerg
307330f729Sjoerg /// Given two types with the same size, try to find a common type.
getCommonType(llvm::Type * first,llvm::Type * second)317330f729Sjoerg static llvm::Type *getCommonType(llvm::Type *first, llvm::Type *second) {
327330f729Sjoerg assert(first != second);
337330f729Sjoerg
347330f729Sjoerg // Allow pointers to merge with integers, but prefer the integer type.
357330f729Sjoerg if (first->isIntegerTy()) {
367330f729Sjoerg if (second->isPointerTy()) return first;
377330f729Sjoerg } else if (first->isPointerTy()) {
387330f729Sjoerg if (second->isIntegerTy()) return second;
397330f729Sjoerg if (second->isPointerTy()) return first;
407330f729Sjoerg
417330f729Sjoerg // Allow two vectors to be merged (given that they have the same size).
427330f729Sjoerg // This assumes that we never have two different vector register sets.
437330f729Sjoerg } else if (auto firstVecTy = dyn_cast<llvm::VectorType>(first)) {
447330f729Sjoerg if (auto secondVecTy = dyn_cast<llvm::VectorType>(second)) {
457330f729Sjoerg if (auto commonTy = getCommonType(firstVecTy->getElementType(),
467330f729Sjoerg secondVecTy->getElementType())) {
477330f729Sjoerg return (commonTy == firstVecTy->getElementType() ? first : second);
487330f729Sjoerg }
497330f729Sjoerg }
507330f729Sjoerg }
517330f729Sjoerg
527330f729Sjoerg return nullptr;
537330f729Sjoerg }
547330f729Sjoerg
getTypeStoreSize(CodeGenModule & CGM,llvm::Type * type)557330f729Sjoerg static CharUnits getTypeStoreSize(CodeGenModule &CGM, llvm::Type *type) {
567330f729Sjoerg return CharUnits::fromQuantity(CGM.getDataLayout().getTypeStoreSize(type));
577330f729Sjoerg }
587330f729Sjoerg
getTypeAllocSize(CodeGenModule & CGM,llvm::Type * type)597330f729Sjoerg static CharUnits getTypeAllocSize(CodeGenModule &CGM, llvm::Type *type) {
607330f729Sjoerg return CharUnits::fromQuantity(CGM.getDataLayout().getTypeAllocSize(type));
617330f729Sjoerg }
627330f729Sjoerg
addTypedData(QualType type,CharUnits begin)637330f729Sjoerg void SwiftAggLowering::addTypedData(QualType type, CharUnits begin) {
647330f729Sjoerg // Deal with various aggregate types as special cases:
657330f729Sjoerg
667330f729Sjoerg // Record types.
677330f729Sjoerg if (auto recType = type->getAs<RecordType>()) {
687330f729Sjoerg addTypedData(recType->getDecl(), begin);
697330f729Sjoerg
707330f729Sjoerg // Array types.
717330f729Sjoerg } else if (type->isArrayType()) {
727330f729Sjoerg // Incomplete array types (flexible array members?) don't provide
737330f729Sjoerg // data to lay out, and the other cases shouldn't be possible.
747330f729Sjoerg auto arrayType = CGM.getContext().getAsConstantArrayType(type);
757330f729Sjoerg if (!arrayType) return;
767330f729Sjoerg
777330f729Sjoerg QualType eltType = arrayType->getElementType();
787330f729Sjoerg auto eltSize = CGM.getContext().getTypeSizeInChars(eltType);
797330f729Sjoerg for (uint64_t i = 0, e = arrayType->getSize().getZExtValue(); i != e; ++i) {
807330f729Sjoerg addTypedData(eltType, begin + i * eltSize);
817330f729Sjoerg }
827330f729Sjoerg
837330f729Sjoerg // Complex types.
847330f729Sjoerg } else if (auto complexType = type->getAs<ComplexType>()) {
857330f729Sjoerg auto eltType = complexType->getElementType();
867330f729Sjoerg auto eltSize = CGM.getContext().getTypeSizeInChars(eltType);
877330f729Sjoerg auto eltLLVMType = CGM.getTypes().ConvertType(eltType);
887330f729Sjoerg addTypedData(eltLLVMType, begin, begin + eltSize);
897330f729Sjoerg addTypedData(eltLLVMType, begin + eltSize, begin + 2 * eltSize);
907330f729Sjoerg
917330f729Sjoerg // Member pointer types.
927330f729Sjoerg } else if (type->getAs<MemberPointerType>()) {
937330f729Sjoerg // Just add it all as opaque.
947330f729Sjoerg addOpaqueData(begin, begin + CGM.getContext().getTypeSizeInChars(type));
957330f729Sjoerg
96*e038c9c4Sjoerg // Atomic types.
97*e038c9c4Sjoerg } else if (const auto *atomicType = type->getAs<AtomicType>()) {
98*e038c9c4Sjoerg auto valueType = atomicType->getValueType();
99*e038c9c4Sjoerg auto atomicSize = CGM.getContext().getTypeSizeInChars(atomicType);
100*e038c9c4Sjoerg auto valueSize = CGM.getContext().getTypeSizeInChars(valueType);
101*e038c9c4Sjoerg
102*e038c9c4Sjoerg addTypedData(atomicType->getValueType(), begin);
103*e038c9c4Sjoerg
104*e038c9c4Sjoerg // Add atomic padding.
105*e038c9c4Sjoerg auto atomicPadding = atomicSize - valueSize;
106*e038c9c4Sjoerg if (atomicPadding > CharUnits::Zero())
107*e038c9c4Sjoerg addOpaqueData(begin + valueSize, begin + atomicSize);
108*e038c9c4Sjoerg
1097330f729Sjoerg // Everything else is scalar and should not convert as an LLVM aggregate.
1107330f729Sjoerg } else {
1117330f729Sjoerg // We intentionally convert as !ForMem because we want to preserve
1127330f729Sjoerg // that a type was an i1.
113*e038c9c4Sjoerg auto *llvmType = CGM.getTypes().ConvertType(type);
1147330f729Sjoerg addTypedData(llvmType, begin);
1157330f729Sjoerg }
1167330f729Sjoerg }
1177330f729Sjoerg
addTypedData(const RecordDecl * record,CharUnits begin)1187330f729Sjoerg void SwiftAggLowering::addTypedData(const RecordDecl *record, CharUnits begin) {
1197330f729Sjoerg addTypedData(record, begin, CGM.getContext().getASTRecordLayout(record));
1207330f729Sjoerg }
1217330f729Sjoerg
addTypedData(const RecordDecl * record,CharUnits begin,const ASTRecordLayout & layout)1227330f729Sjoerg void SwiftAggLowering::addTypedData(const RecordDecl *record, CharUnits begin,
1237330f729Sjoerg const ASTRecordLayout &layout) {
1247330f729Sjoerg // Unions are a special case.
1257330f729Sjoerg if (record->isUnion()) {
1267330f729Sjoerg for (auto field : record->fields()) {
1277330f729Sjoerg if (field->isBitField()) {
1287330f729Sjoerg addBitFieldData(field, begin, 0);
1297330f729Sjoerg } else {
1307330f729Sjoerg addTypedData(field->getType(), begin);
1317330f729Sjoerg }
1327330f729Sjoerg }
1337330f729Sjoerg return;
1347330f729Sjoerg }
1357330f729Sjoerg
1367330f729Sjoerg // Note that correctness does not rely on us adding things in
1377330f729Sjoerg // their actual order of layout; it's just somewhat more efficient
1387330f729Sjoerg // for the builder.
1397330f729Sjoerg
1407330f729Sjoerg // With that in mind, add "early" C++ data.
1417330f729Sjoerg auto cxxRecord = dyn_cast<CXXRecordDecl>(record);
1427330f729Sjoerg if (cxxRecord) {
1437330f729Sjoerg // - a v-table pointer, if the class adds its own
1447330f729Sjoerg if (layout.hasOwnVFPtr()) {
1457330f729Sjoerg addTypedData(CGM.Int8PtrTy, begin);
1467330f729Sjoerg }
1477330f729Sjoerg
1487330f729Sjoerg // - non-virtual bases
1497330f729Sjoerg for (auto &baseSpecifier : cxxRecord->bases()) {
1507330f729Sjoerg if (baseSpecifier.isVirtual()) continue;
1517330f729Sjoerg
1527330f729Sjoerg auto baseRecord = baseSpecifier.getType()->getAsCXXRecordDecl();
1537330f729Sjoerg addTypedData(baseRecord, begin + layout.getBaseClassOffset(baseRecord));
1547330f729Sjoerg }
1557330f729Sjoerg
1567330f729Sjoerg // - a vbptr if the class adds its own
1577330f729Sjoerg if (layout.hasOwnVBPtr()) {
1587330f729Sjoerg addTypedData(CGM.Int8PtrTy, begin + layout.getVBPtrOffset());
1597330f729Sjoerg }
1607330f729Sjoerg }
1617330f729Sjoerg
1627330f729Sjoerg // Add fields.
1637330f729Sjoerg for (auto field : record->fields()) {
1647330f729Sjoerg auto fieldOffsetInBits = layout.getFieldOffset(field->getFieldIndex());
1657330f729Sjoerg if (field->isBitField()) {
1667330f729Sjoerg addBitFieldData(field, begin, fieldOffsetInBits);
1677330f729Sjoerg } else {
1687330f729Sjoerg addTypedData(field->getType(),
1697330f729Sjoerg begin + CGM.getContext().toCharUnitsFromBits(fieldOffsetInBits));
1707330f729Sjoerg }
1717330f729Sjoerg }
1727330f729Sjoerg
1737330f729Sjoerg // Add "late" C++ data:
1747330f729Sjoerg if (cxxRecord) {
1757330f729Sjoerg // - virtual bases
1767330f729Sjoerg for (auto &vbaseSpecifier : cxxRecord->vbases()) {
1777330f729Sjoerg auto baseRecord = vbaseSpecifier.getType()->getAsCXXRecordDecl();
1787330f729Sjoerg addTypedData(baseRecord, begin + layout.getVBaseClassOffset(baseRecord));
1797330f729Sjoerg }
1807330f729Sjoerg }
1817330f729Sjoerg }
1827330f729Sjoerg
addBitFieldData(const FieldDecl * bitfield,CharUnits recordBegin,uint64_t bitfieldBitBegin)1837330f729Sjoerg void SwiftAggLowering::addBitFieldData(const FieldDecl *bitfield,
1847330f729Sjoerg CharUnits recordBegin,
1857330f729Sjoerg uint64_t bitfieldBitBegin) {
1867330f729Sjoerg assert(bitfield->isBitField());
1877330f729Sjoerg auto &ctx = CGM.getContext();
1887330f729Sjoerg auto width = bitfield->getBitWidthValue(ctx);
1897330f729Sjoerg
1907330f729Sjoerg // We can ignore zero-width bit-fields.
1917330f729Sjoerg if (width == 0) return;
1927330f729Sjoerg
1937330f729Sjoerg // toCharUnitsFromBits rounds down.
1947330f729Sjoerg CharUnits bitfieldByteBegin = ctx.toCharUnitsFromBits(bitfieldBitBegin);
1957330f729Sjoerg
1967330f729Sjoerg // Find the offset of the last byte that is partially occupied by the
1977330f729Sjoerg // bit-field; since we otherwise expect exclusive ends, the end is the
1987330f729Sjoerg // next byte.
1997330f729Sjoerg uint64_t bitfieldBitLast = bitfieldBitBegin + width - 1;
2007330f729Sjoerg CharUnits bitfieldByteEnd =
2017330f729Sjoerg ctx.toCharUnitsFromBits(bitfieldBitLast) + CharUnits::One();
2027330f729Sjoerg addOpaqueData(recordBegin + bitfieldByteBegin,
2037330f729Sjoerg recordBegin + bitfieldByteEnd);
2047330f729Sjoerg }
2057330f729Sjoerg
addTypedData(llvm::Type * type,CharUnits begin)2067330f729Sjoerg void SwiftAggLowering::addTypedData(llvm::Type *type, CharUnits begin) {
2077330f729Sjoerg assert(type && "didn't provide type for typed data");
2087330f729Sjoerg addTypedData(type, begin, begin + getTypeStoreSize(CGM, type));
2097330f729Sjoerg }
2107330f729Sjoerg
addTypedData(llvm::Type * type,CharUnits begin,CharUnits end)2117330f729Sjoerg void SwiftAggLowering::addTypedData(llvm::Type *type,
2127330f729Sjoerg CharUnits begin, CharUnits end) {
2137330f729Sjoerg assert(type && "didn't provide type for typed data");
2147330f729Sjoerg assert(getTypeStoreSize(CGM, type) == end - begin);
2157330f729Sjoerg
2167330f729Sjoerg // Legalize vector types.
2177330f729Sjoerg if (auto vecTy = dyn_cast<llvm::VectorType>(type)) {
2187330f729Sjoerg SmallVector<llvm::Type*, 4> componentTys;
2197330f729Sjoerg legalizeVectorType(CGM, end - begin, vecTy, componentTys);
2207330f729Sjoerg assert(componentTys.size() >= 1);
2217330f729Sjoerg
2227330f729Sjoerg // Walk the initial components.
2237330f729Sjoerg for (size_t i = 0, e = componentTys.size(); i != e - 1; ++i) {
2247330f729Sjoerg llvm::Type *componentTy = componentTys[i];
2257330f729Sjoerg auto componentSize = getTypeStoreSize(CGM, componentTy);
2267330f729Sjoerg assert(componentSize < end - begin);
2277330f729Sjoerg addLegalTypedData(componentTy, begin, begin + componentSize);
2287330f729Sjoerg begin += componentSize;
2297330f729Sjoerg }
2307330f729Sjoerg
2317330f729Sjoerg return addLegalTypedData(componentTys.back(), begin, end);
2327330f729Sjoerg }
2337330f729Sjoerg
2347330f729Sjoerg // Legalize integer types.
2357330f729Sjoerg if (auto intTy = dyn_cast<llvm::IntegerType>(type)) {
2367330f729Sjoerg if (!isLegalIntegerType(CGM, intTy))
2377330f729Sjoerg return addOpaqueData(begin, end);
2387330f729Sjoerg }
2397330f729Sjoerg
2407330f729Sjoerg // All other types should be legal.
2417330f729Sjoerg return addLegalTypedData(type, begin, end);
2427330f729Sjoerg }
2437330f729Sjoerg
addLegalTypedData(llvm::Type * type,CharUnits begin,CharUnits end)2447330f729Sjoerg void SwiftAggLowering::addLegalTypedData(llvm::Type *type,
2457330f729Sjoerg CharUnits begin, CharUnits end) {
2467330f729Sjoerg // Require the type to be naturally aligned.
2477330f729Sjoerg if (!begin.isZero() && !begin.isMultipleOf(getNaturalAlignment(CGM, type))) {
2487330f729Sjoerg
2497330f729Sjoerg // Try splitting vector types.
2507330f729Sjoerg if (auto vecTy = dyn_cast<llvm::VectorType>(type)) {
2517330f729Sjoerg auto split = splitLegalVectorType(CGM, end - begin, vecTy);
2527330f729Sjoerg auto eltTy = split.first;
2537330f729Sjoerg auto numElts = split.second;
2547330f729Sjoerg
2557330f729Sjoerg auto eltSize = (end - begin) / numElts;
2567330f729Sjoerg assert(eltSize == getTypeStoreSize(CGM, eltTy));
2577330f729Sjoerg for (size_t i = 0, e = numElts; i != e; ++i) {
2587330f729Sjoerg addLegalTypedData(eltTy, begin, begin + eltSize);
2597330f729Sjoerg begin += eltSize;
2607330f729Sjoerg }
2617330f729Sjoerg assert(begin == end);
2627330f729Sjoerg return;
2637330f729Sjoerg }
2647330f729Sjoerg
2657330f729Sjoerg return addOpaqueData(begin, end);
2667330f729Sjoerg }
2677330f729Sjoerg
2687330f729Sjoerg addEntry(type, begin, end);
2697330f729Sjoerg }
2707330f729Sjoerg
addEntry(llvm::Type * type,CharUnits begin,CharUnits end)2717330f729Sjoerg void SwiftAggLowering::addEntry(llvm::Type *type,
2727330f729Sjoerg CharUnits begin, CharUnits end) {
2737330f729Sjoerg assert((!type ||
2747330f729Sjoerg (!isa<llvm::StructType>(type) && !isa<llvm::ArrayType>(type))) &&
2757330f729Sjoerg "cannot add aggregate-typed data");
2767330f729Sjoerg assert(!type || begin.isMultipleOf(getNaturalAlignment(CGM, type)));
2777330f729Sjoerg
2787330f729Sjoerg // Fast path: we can just add entries to the end.
2797330f729Sjoerg if (Entries.empty() || Entries.back().End <= begin) {
2807330f729Sjoerg Entries.push_back({begin, end, type});
2817330f729Sjoerg return;
2827330f729Sjoerg }
2837330f729Sjoerg
2847330f729Sjoerg // Find the first existing entry that ends after the start of the new data.
2857330f729Sjoerg // TODO: do a binary search if Entries is big enough for it to matter.
2867330f729Sjoerg size_t index = Entries.size() - 1;
2877330f729Sjoerg while (index != 0) {
2887330f729Sjoerg if (Entries[index - 1].End <= begin) break;
2897330f729Sjoerg --index;
2907330f729Sjoerg }
2917330f729Sjoerg
2927330f729Sjoerg // The entry ends after the start of the new data.
2937330f729Sjoerg // If the entry starts after the end of the new data, there's no conflict.
2947330f729Sjoerg if (Entries[index].Begin >= end) {
2957330f729Sjoerg // This insertion is potentially O(n), but the way we generally build
2967330f729Sjoerg // these layouts makes that unlikely to matter: we'd need a union of
2977330f729Sjoerg // several very large types.
2987330f729Sjoerg Entries.insert(Entries.begin() + index, {begin, end, type});
2997330f729Sjoerg return;
3007330f729Sjoerg }
3017330f729Sjoerg
3027330f729Sjoerg // Otherwise, the ranges overlap. The new range might also overlap
3037330f729Sjoerg // with later ranges.
3047330f729Sjoerg restartAfterSplit:
3057330f729Sjoerg
3067330f729Sjoerg // Simplest case: an exact overlap.
3077330f729Sjoerg if (Entries[index].Begin == begin && Entries[index].End == end) {
3087330f729Sjoerg // If the types match exactly, great.
3097330f729Sjoerg if (Entries[index].Type == type) return;
3107330f729Sjoerg
3117330f729Sjoerg // If either type is opaque, make the entry opaque and return.
3127330f729Sjoerg if (Entries[index].Type == nullptr) {
3137330f729Sjoerg return;
3147330f729Sjoerg } else if (type == nullptr) {
3157330f729Sjoerg Entries[index].Type = nullptr;
3167330f729Sjoerg return;
3177330f729Sjoerg }
3187330f729Sjoerg
3197330f729Sjoerg // If they disagree in an ABI-agnostic way, just resolve the conflict
3207330f729Sjoerg // arbitrarily.
3217330f729Sjoerg if (auto entryType = getCommonType(Entries[index].Type, type)) {
3227330f729Sjoerg Entries[index].Type = entryType;
3237330f729Sjoerg return;
3247330f729Sjoerg }
3257330f729Sjoerg
3267330f729Sjoerg // Otherwise, make the entry opaque.
3277330f729Sjoerg Entries[index].Type = nullptr;
3287330f729Sjoerg return;
3297330f729Sjoerg }
3307330f729Sjoerg
3317330f729Sjoerg // Okay, we have an overlapping conflict of some sort.
3327330f729Sjoerg
3337330f729Sjoerg // If we have a vector type, split it.
3347330f729Sjoerg if (auto vecTy = dyn_cast_or_null<llvm::VectorType>(type)) {
3357330f729Sjoerg auto eltTy = vecTy->getElementType();
336*e038c9c4Sjoerg CharUnits eltSize =
337*e038c9c4Sjoerg (end - begin) / cast<llvm::FixedVectorType>(vecTy)->getNumElements();
3387330f729Sjoerg assert(eltSize == getTypeStoreSize(CGM, eltTy));
339*e038c9c4Sjoerg for (unsigned i = 0,
340*e038c9c4Sjoerg e = cast<llvm::FixedVectorType>(vecTy)->getNumElements();
341*e038c9c4Sjoerg i != e; ++i) {
3427330f729Sjoerg addEntry(eltTy, begin, begin + eltSize);
3437330f729Sjoerg begin += eltSize;
3447330f729Sjoerg }
3457330f729Sjoerg assert(begin == end);
3467330f729Sjoerg return;
3477330f729Sjoerg }
3487330f729Sjoerg
3497330f729Sjoerg // If the entry is a vector type, split it and try again.
3507330f729Sjoerg if (Entries[index].Type && Entries[index].Type->isVectorTy()) {
3517330f729Sjoerg splitVectorEntry(index);
3527330f729Sjoerg goto restartAfterSplit;
3537330f729Sjoerg }
3547330f729Sjoerg
3557330f729Sjoerg // Okay, we have no choice but to make the existing entry opaque.
3567330f729Sjoerg
3577330f729Sjoerg Entries[index].Type = nullptr;
3587330f729Sjoerg
3597330f729Sjoerg // Stretch the start of the entry to the beginning of the range.
3607330f729Sjoerg if (begin < Entries[index].Begin) {
3617330f729Sjoerg Entries[index].Begin = begin;
3627330f729Sjoerg assert(index == 0 || begin >= Entries[index - 1].End);
3637330f729Sjoerg }
3647330f729Sjoerg
3657330f729Sjoerg // Stretch the end of the entry to the end of the range; but if we run
3667330f729Sjoerg // into the start of the next entry, just leave the range there and repeat.
3677330f729Sjoerg while (end > Entries[index].End) {
3687330f729Sjoerg assert(Entries[index].Type == nullptr);
3697330f729Sjoerg
3707330f729Sjoerg // If the range doesn't overlap the next entry, we're done.
3717330f729Sjoerg if (index == Entries.size() - 1 || end <= Entries[index + 1].Begin) {
3727330f729Sjoerg Entries[index].End = end;
3737330f729Sjoerg break;
3747330f729Sjoerg }
3757330f729Sjoerg
3767330f729Sjoerg // Otherwise, stretch to the start of the next entry.
3777330f729Sjoerg Entries[index].End = Entries[index + 1].Begin;
3787330f729Sjoerg
3797330f729Sjoerg // Continue with the next entry.
3807330f729Sjoerg index++;
3817330f729Sjoerg
3827330f729Sjoerg // This entry needs to be made opaque if it is not already.
3837330f729Sjoerg if (Entries[index].Type == nullptr)
3847330f729Sjoerg continue;
3857330f729Sjoerg
3867330f729Sjoerg // Split vector entries unless we completely subsume them.
3877330f729Sjoerg if (Entries[index].Type->isVectorTy() &&
3887330f729Sjoerg end < Entries[index].End) {
3897330f729Sjoerg splitVectorEntry(index);
3907330f729Sjoerg }
3917330f729Sjoerg
3927330f729Sjoerg // Make the entry opaque.
3937330f729Sjoerg Entries[index].Type = nullptr;
3947330f729Sjoerg }
3957330f729Sjoerg }
3967330f729Sjoerg
3977330f729Sjoerg /// Replace the entry of vector type at offset 'index' with a sequence
3987330f729Sjoerg /// of its component vectors.
splitVectorEntry(unsigned index)3997330f729Sjoerg void SwiftAggLowering::splitVectorEntry(unsigned index) {
4007330f729Sjoerg auto vecTy = cast<llvm::VectorType>(Entries[index].Type);
4017330f729Sjoerg auto split = splitLegalVectorType(CGM, Entries[index].getWidth(), vecTy);
4027330f729Sjoerg
4037330f729Sjoerg auto eltTy = split.first;
4047330f729Sjoerg CharUnits eltSize = getTypeStoreSize(CGM, eltTy);
4057330f729Sjoerg auto numElts = split.second;
4067330f729Sjoerg Entries.insert(Entries.begin() + index + 1, numElts - 1, StorageEntry());
4077330f729Sjoerg
4087330f729Sjoerg CharUnits begin = Entries[index].Begin;
4097330f729Sjoerg for (unsigned i = 0; i != numElts; ++i) {
4107330f729Sjoerg Entries[index].Type = eltTy;
4117330f729Sjoerg Entries[index].Begin = begin;
4127330f729Sjoerg Entries[index].End = begin + eltSize;
4137330f729Sjoerg begin += eltSize;
4147330f729Sjoerg }
4157330f729Sjoerg }
4167330f729Sjoerg
4177330f729Sjoerg /// Given a power-of-two unit size, return the offset of the aligned unit
4187330f729Sjoerg /// of that size which contains the given offset.
4197330f729Sjoerg ///
4207330f729Sjoerg /// In other words, round down to the nearest multiple of the unit size.
getOffsetAtStartOfUnit(CharUnits offset,CharUnits unitSize)4217330f729Sjoerg static CharUnits getOffsetAtStartOfUnit(CharUnits offset, CharUnits unitSize) {
4227330f729Sjoerg assert(isPowerOf2(unitSize.getQuantity()));
4237330f729Sjoerg auto unitMask = ~(unitSize.getQuantity() - 1);
4247330f729Sjoerg return CharUnits::fromQuantity(offset.getQuantity() & unitMask);
4257330f729Sjoerg }
4267330f729Sjoerg
areBytesInSameUnit(CharUnits first,CharUnits second,CharUnits chunkSize)4277330f729Sjoerg static bool areBytesInSameUnit(CharUnits first, CharUnits second,
4287330f729Sjoerg CharUnits chunkSize) {
4297330f729Sjoerg return getOffsetAtStartOfUnit(first, chunkSize)
4307330f729Sjoerg == getOffsetAtStartOfUnit(second, chunkSize);
4317330f729Sjoerg }
4327330f729Sjoerg
isMergeableEntryType(llvm::Type * type)4337330f729Sjoerg static bool isMergeableEntryType(llvm::Type *type) {
4347330f729Sjoerg // Opaquely-typed memory is always mergeable.
4357330f729Sjoerg if (type == nullptr) return true;
4367330f729Sjoerg
4377330f729Sjoerg // Pointers and integers are always mergeable. In theory we should not
4387330f729Sjoerg // merge pointers, but (1) it doesn't currently matter in practice because
4397330f729Sjoerg // the chunk size is never greater than the size of a pointer and (2)
4407330f729Sjoerg // Swift IRGen uses integer types for a lot of things that are "really"
4417330f729Sjoerg // just storing pointers (like Optional<SomePointer>). If we ever have a
4427330f729Sjoerg // target that would otherwise combine pointers, we should put some effort
4437330f729Sjoerg // into fixing those cases in Swift IRGen and then call out pointer types
4447330f729Sjoerg // here.
4457330f729Sjoerg
4467330f729Sjoerg // Floating-point and vector types should never be merged.
4477330f729Sjoerg // Most such types are too large and highly-aligned to ever trigger merging
4487330f729Sjoerg // in practice, but it's important for the rule to cover at least 'half'
4497330f729Sjoerg // and 'float', as well as things like small vectors of 'i1' or 'i8'.
4507330f729Sjoerg return (!type->isFloatingPointTy() && !type->isVectorTy());
4517330f729Sjoerg }
4527330f729Sjoerg
shouldMergeEntries(const StorageEntry & first,const StorageEntry & second,CharUnits chunkSize)4537330f729Sjoerg bool SwiftAggLowering::shouldMergeEntries(const StorageEntry &first,
4547330f729Sjoerg const StorageEntry &second,
4557330f729Sjoerg CharUnits chunkSize) {
4567330f729Sjoerg // Only merge entries that overlap the same chunk. We test this first
4577330f729Sjoerg // despite being a bit more expensive because this is the condition that
4587330f729Sjoerg // tends to prevent merging.
4597330f729Sjoerg if (!areBytesInSameUnit(first.End - CharUnits::One(), second.Begin,
4607330f729Sjoerg chunkSize))
4617330f729Sjoerg return false;
4627330f729Sjoerg
4637330f729Sjoerg return (isMergeableEntryType(first.Type) &&
4647330f729Sjoerg isMergeableEntryType(second.Type));
4657330f729Sjoerg }
4667330f729Sjoerg
finish()4677330f729Sjoerg void SwiftAggLowering::finish() {
4687330f729Sjoerg if (Entries.empty()) {
4697330f729Sjoerg Finished = true;
4707330f729Sjoerg return;
4717330f729Sjoerg }
4727330f729Sjoerg
4737330f729Sjoerg // We logically split the layout down into a series of chunks of this size,
4747330f729Sjoerg // which is generally the size of a pointer.
4757330f729Sjoerg const CharUnits chunkSize = getMaximumVoluntaryIntegerSize(CGM);
4767330f729Sjoerg
4777330f729Sjoerg // First pass: if two entries should be merged, make them both opaque
4787330f729Sjoerg // and stretch one to meet the next.
4797330f729Sjoerg // Also, remember if there are any opaque entries.
4807330f729Sjoerg bool hasOpaqueEntries = (Entries[0].Type == nullptr);
4817330f729Sjoerg for (size_t i = 1, e = Entries.size(); i != e; ++i) {
4827330f729Sjoerg if (shouldMergeEntries(Entries[i - 1], Entries[i], chunkSize)) {
4837330f729Sjoerg Entries[i - 1].Type = nullptr;
4847330f729Sjoerg Entries[i].Type = nullptr;
4857330f729Sjoerg Entries[i - 1].End = Entries[i].Begin;
4867330f729Sjoerg hasOpaqueEntries = true;
4877330f729Sjoerg
4887330f729Sjoerg } else if (Entries[i].Type == nullptr) {
4897330f729Sjoerg hasOpaqueEntries = true;
4907330f729Sjoerg }
4917330f729Sjoerg }
4927330f729Sjoerg
4937330f729Sjoerg // The rest of the algorithm leaves non-opaque entries alone, so if we
4947330f729Sjoerg // have no opaque entries, we're done.
4957330f729Sjoerg if (!hasOpaqueEntries) {
4967330f729Sjoerg Finished = true;
4977330f729Sjoerg return;
4987330f729Sjoerg }
4997330f729Sjoerg
5007330f729Sjoerg // Okay, move the entries to a temporary and rebuild Entries.
5017330f729Sjoerg auto orig = std::move(Entries);
5027330f729Sjoerg assert(Entries.empty());
5037330f729Sjoerg
5047330f729Sjoerg for (size_t i = 0, e = orig.size(); i != e; ++i) {
5057330f729Sjoerg // Just copy over non-opaque entries.
5067330f729Sjoerg if (orig[i].Type != nullptr) {
5077330f729Sjoerg Entries.push_back(orig[i]);
5087330f729Sjoerg continue;
5097330f729Sjoerg }
5107330f729Sjoerg
5117330f729Sjoerg // Scan forward to determine the full extent of the next opaque range.
5127330f729Sjoerg // We know from the first pass that only contiguous ranges will overlap
5137330f729Sjoerg // the same aligned chunk.
5147330f729Sjoerg auto begin = orig[i].Begin;
5157330f729Sjoerg auto end = orig[i].End;
5167330f729Sjoerg while (i + 1 != e &&
5177330f729Sjoerg orig[i + 1].Type == nullptr &&
5187330f729Sjoerg end == orig[i + 1].Begin) {
5197330f729Sjoerg end = orig[i + 1].End;
5207330f729Sjoerg i++;
5217330f729Sjoerg }
5227330f729Sjoerg
5237330f729Sjoerg // Add an entry per intersected chunk.
5247330f729Sjoerg do {
5257330f729Sjoerg // Find the smallest aligned storage unit in the maximal aligned
5267330f729Sjoerg // storage unit containing 'begin' that contains all the bytes in
5277330f729Sjoerg // the intersection between the range and this chunk.
5287330f729Sjoerg CharUnits localBegin = begin;
5297330f729Sjoerg CharUnits chunkBegin = getOffsetAtStartOfUnit(localBegin, chunkSize);
5307330f729Sjoerg CharUnits chunkEnd = chunkBegin + chunkSize;
5317330f729Sjoerg CharUnits localEnd = std::min(end, chunkEnd);
5327330f729Sjoerg
5337330f729Sjoerg // Just do a simple loop over ever-increasing unit sizes.
5347330f729Sjoerg CharUnits unitSize = CharUnits::One();
5357330f729Sjoerg CharUnits unitBegin, unitEnd;
5367330f729Sjoerg for (; ; unitSize *= 2) {
5377330f729Sjoerg assert(unitSize <= chunkSize);
5387330f729Sjoerg unitBegin = getOffsetAtStartOfUnit(localBegin, unitSize);
5397330f729Sjoerg unitEnd = unitBegin + unitSize;
5407330f729Sjoerg if (unitEnd >= localEnd) break;
5417330f729Sjoerg }
5427330f729Sjoerg
5437330f729Sjoerg // Add an entry for this unit.
5447330f729Sjoerg auto entryTy =
5457330f729Sjoerg llvm::IntegerType::get(CGM.getLLVMContext(),
5467330f729Sjoerg CGM.getContext().toBits(unitSize));
5477330f729Sjoerg Entries.push_back({unitBegin, unitEnd, entryTy});
5487330f729Sjoerg
5497330f729Sjoerg // The next chunk starts where this chunk left off.
5507330f729Sjoerg begin = localEnd;
5517330f729Sjoerg } while (begin != end);
5527330f729Sjoerg }
5537330f729Sjoerg
5547330f729Sjoerg // Okay, finally finished.
5557330f729Sjoerg Finished = true;
5567330f729Sjoerg }
5577330f729Sjoerg
enumerateComponents(EnumerationCallback callback) const5587330f729Sjoerg void SwiftAggLowering::enumerateComponents(EnumerationCallback callback) const {
5597330f729Sjoerg assert(Finished && "haven't yet finished lowering");
5607330f729Sjoerg
5617330f729Sjoerg for (auto &entry : Entries) {
5627330f729Sjoerg callback(entry.Begin, entry.End, entry.Type);
5637330f729Sjoerg }
5647330f729Sjoerg }
5657330f729Sjoerg
5667330f729Sjoerg std::pair<llvm::StructType*, llvm::Type*>
getCoerceAndExpandTypes() const5677330f729Sjoerg SwiftAggLowering::getCoerceAndExpandTypes() const {
5687330f729Sjoerg assert(Finished && "haven't yet finished lowering");
5697330f729Sjoerg
5707330f729Sjoerg auto &ctx = CGM.getLLVMContext();
5717330f729Sjoerg
5727330f729Sjoerg if (Entries.empty()) {
5737330f729Sjoerg auto type = llvm::StructType::get(ctx);
5747330f729Sjoerg return { type, type };
5757330f729Sjoerg }
5767330f729Sjoerg
5777330f729Sjoerg SmallVector<llvm::Type*, 8> elts;
5787330f729Sjoerg CharUnits lastEnd = CharUnits::Zero();
5797330f729Sjoerg bool hasPadding = false;
5807330f729Sjoerg bool packed = false;
5817330f729Sjoerg for (auto &entry : Entries) {
5827330f729Sjoerg if (entry.Begin != lastEnd) {
5837330f729Sjoerg auto paddingSize = entry.Begin - lastEnd;
5847330f729Sjoerg assert(!paddingSize.isNegative());
5857330f729Sjoerg
5867330f729Sjoerg auto padding = llvm::ArrayType::get(llvm::Type::getInt8Ty(ctx),
5877330f729Sjoerg paddingSize.getQuantity());
5887330f729Sjoerg elts.push_back(padding);
5897330f729Sjoerg hasPadding = true;
5907330f729Sjoerg }
5917330f729Sjoerg
5927330f729Sjoerg if (!packed && !entry.Begin.isMultipleOf(
5937330f729Sjoerg CharUnits::fromQuantity(
5947330f729Sjoerg CGM.getDataLayout().getABITypeAlignment(entry.Type))))
5957330f729Sjoerg packed = true;
5967330f729Sjoerg
5977330f729Sjoerg elts.push_back(entry.Type);
5987330f729Sjoerg
5997330f729Sjoerg lastEnd = entry.Begin + getTypeAllocSize(CGM, entry.Type);
6007330f729Sjoerg assert(entry.End <= lastEnd);
6017330f729Sjoerg }
6027330f729Sjoerg
6037330f729Sjoerg // We don't need to adjust 'packed' to deal with possible tail padding
6047330f729Sjoerg // because we never do that kind of access through the coercion type.
6057330f729Sjoerg auto coercionType = llvm::StructType::get(ctx, elts, packed);
6067330f729Sjoerg
6077330f729Sjoerg llvm::Type *unpaddedType = coercionType;
6087330f729Sjoerg if (hasPadding) {
6097330f729Sjoerg elts.clear();
6107330f729Sjoerg for (auto &entry : Entries) {
6117330f729Sjoerg elts.push_back(entry.Type);
6127330f729Sjoerg }
6137330f729Sjoerg if (elts.size() == 1) {
6147330f729Sjoerg unpaddedType = elts[0];
6157330f729Sjoerg } else {
6167330f729Sjoerg unpaddedType = llvm::StructType::get(ctx, elts, /*packed*/ false);
6177330f729Sjoerg }
6187330f729Sjoerg } else if (Entries.size() == 1) {
6197330f729Sjoerg unpaddedType = Entries[0].Type;
6207330f729Sjoerg }
6217330f729Sjoerg
6227330f729Sjoerg return { coercionType, unpaddedType };
6237330f729Sjoerg }
6247330f729Sjoerg
shouldPassIndirectly(bool asReturnValue) const6257330f729Sjoerg bool SwiftAggLowering::shouldPassIndirectly(bool asReturnValue) const {
6267330f729Sjoerg assert(Finished && "haven't yet finished lowering");
6277330f729Sjoerg
6287330f729Sjoerg // Empty types don't need to be passed indirectly.
6297330f729Sjoerg if (Entries.empty()) return false;
6307330f729Sjoerg
6317330f729Sjoerg // Avoid copying the array of types when there's just a single element.
6327330f729Sjoerg if (Entries.size() == 1) {
6337330f729Sjoerg return getSwiftABIInfo(CGM).shouldPassIndirectlyForSwift(
6347330f729Sjoerg Entries.back().Type,
6357330f729Sjoerg asReturnValue);
6367330f729Sjoerg }
6377330f729Sjoerg
6387330f729Sjoerg SmallVector<llvm::Type*, 8> componentTys;
6397330f729Sjoerg componentTys.reserve(Entries.size());
6407330f729Sjoerg for (auto &entry : Entries) {
6417330f729Sjoerg componentTys.push_back(entry.Type);
6427330f729Sjoerg }
6437330f729Sjoerg return getSwiftABIInfo(CGM).shouldPassIndirectlyForSwift(componentTys,
6447330f729Sjoerg asReturnValue);
6457330f729Sjoerg }
6467330f729Sjoerg
shouldPassIndirectly(CodeGenModule & CGM,ArrayRef<llvm::Type * > componentTys,bool asReturnValue)6477330f729Sjoerg bool swiftcall::shouldPassIndirectly(CodeGenModule &CGM,
6487330f729Sjoerg ArrayRef<llvm::Type*> componentTys,
6497330f729Sjoerg bool asReturnValue) {
6507330f729Sjoerg return getSwiftABIInfo(CGM).shouldPassIndirectlyForSwift(componentTys,
6517330f729Sjoerg asReturnValue);
6527330f729Sjoerg }
6537330f729Sjoerg
getMaximumVoluntaryIntegerSize(CodeGenModule & CGM)6547330f729Sjoerg CharUnits swiftcall::getMaximumVoluntaryIntegerSize(CodeGenModule &CGM) {
6557330f729Sjoerg // Currently always the size of an ordinary pointer.
6567330f729Sjoerg return CGM.getContext().toCharUnitsFromBits(
6577330f729Sjoerg CGM.getContext().getTargetInfo().getPointerWidth(0));
6587330f729Sjoerg }
6597330f729Sjoerg
getNaturalAlignment(CodeGenModule & CGM,llvm::Type * type)6607330f729Sjoerg CharUnits swiftcall::getNaturalAlignment(CodeGenModule &CGM, llvm::Type *type) {
6617330f729Sjoerg // For Swift's purposes, this is always just the store size of the type
6627330f729Sjoerg // rounded up to a power of 2.
6637330f729Sjoerg auto size = (unsigned long long) getTypeStoreSize(CGM, type).getQuantity();
6647330f729Sjoerg if (!isPowerOf2(size)) {
6657330f729Sjoerg size = 1ULL << (llvm::findLastSet(size, llvm::ZB_Undefined) + 1);
6667330f729Sjoerg }
6677330f729Sjoerg assert(size >= CGM.getDataLayout().getABITypeAlignment(type));
6687330f729Sjoerg return CharUnits::fromQuantity(size);
6697330f729Sjoerg }
6707330f729Sjoerg
isLegalIntegerType(CodeGenModule & CGM,llvm::IntegerType * intTy)6717330f729Sjoerg bool swiftcall::isLegalIntegerType(CodeGenModule &CGM,
6727330f729Sjoerg llvm::IntegerType *intTy) {
6737330f729Sjoerg auto size = intTy->getBitWidth();
6747330f729Sjoerg switch (size) {
6757330f729Sjoerg case 1:
6767330f729Sjoerg case 8:
6777330f729Sjoerg case 16:
6787330f729Sjoerg case 32:
6797330f729Sjoerg case 64:
6807330f729Sjoerg // Just assume that the above are always legal.
6817330f729Sjoerg return true;
6827330f729Sjoerg
6837330f729Sjoerg case 128:
6847330f729Sjoerg return CGM.getContext().getTargetInfo().hasInt128Type();
6857330f729Sjoerg
6867330f729Sjoerg default:
6877330f729Sjoerg return false;
6887330f729Sjoerg }
6897330f729Sjoerg }
6907330f729Sjoerg
isLegalVectorType(CodeGenModule & CGM,CharUnits vectorSize,llvm::VectorType * vectorTy)6917330f729Sjoerg bool swiftcall::isLegalVectorType(CodeGenModule &CGM, CharUnits vectorSize,
6927330f729Sjoerg llvm::VectorType *vectorTy) {
693*e038c9c4Sjoerg return isLegalVectorType(
694*e038c9c4Sjoerg CGM, vectorSize, vectorTy->getElementType(),
695*e038c9c4Sjoerg cast<llvm::FixedVectorType>(vectorTy)->getNumElements());
6967330f729Sjoerg }
6977330f729Sjoerg
isLegalVectorType(CodeGenModule & CGM,CharUnits vectorSize,llvm::Type * eltTy,unsigned numElts)6987330f729Sjoerg bool swiftcall::isLegalVectorType(CodeGenModule &CGM, CharUnits vectorSize,
6997330f729Sjoerg llvm::Type *eltTy, unsigned numElts) {
7007330f729Sjoerg assert(numElts > 1 && "illegal vector length");
7017330f729Sjoerg return getSwiftABIInfo(CGM)
7027330f729Sjoerg .isLegalVectorTypeForSwift(vectorSize, eltTy, numElts);
7037330f729Sjoerg }
7047330f729Sjoerg
7057330f729Sjoerg std::pair<llvm::Type*, unsigned>
splitLegalVectorType(CodeGenModule & CGM,CharUnits vectorSize,llvm::VectorType * vectorTy)7067330f729Sjoerg swiftcall::splitLegalVectorType(CodeGenModule &CGM, CharUnits vectorSize,
7077330f729Sjoerg llvm::VectorType *vectorTy) {
708*e038c9c4Sjoerg auto numElts = cast<llvm::FixedVectorType>(vectorTy)->getNumElements();
7097330f729Sjoerg auto eltTy = vectorTy->getElementType();
7107330f729Sjoerg
7117330f729Sjoerg // Try to split the vector type in half.
7127330f729Sjoerg if (numElts >= 4 && isPowerOf2(numElts)) {
7137330f729Sjoerg if (isLegalVectorType(CGM, vectorSize / 2, eltTy, numElts / 2))
714*e038c9c4Sjoerg return {llvm::FixedVectorType::get(eltTy, numElts / 2), 2};
7157330f729Sjoerg }
7167330f729Sjoerg
7177330f729Sjoerg return {eltTy, numElts};
7187330f729Sjoerg }
7197330f729Sjoerg
legalizeVectorType(CodeGenModule & CGM,CharUnits origVectorSize,llvm::VectorType * origVectorTy,llvm::SmallVectorImpl<llvm::Type * > & components)7207330f729Sjoerg void swiftcall::legalizeVectorType(CodeGenModule &CGM, CharUnits origVectorSize,
7217330f729Sjoerg llvm::VectorType *origVectorTy,
7227330f729Sjoerg llvm::SmallVectorImpl<llvm::Type*> &components) {
7237330f729Sjoerg // If it's already a legal vector type, use it.
7247330f729Sjoerg if (isLegalVectorType(CGM, origVectorSize, origVectorTy)) {
7257330f729Sjoerg components.push_back(origVectorTy);
7267330f729Sjoerg return;
7277330f729Sjoerg }
7287330f729Sjoerg
7297330f729Sjoerg // Try to split the vector into legal subvectors.
730*e038c9c4Sjoerg auto numElts = cast<llvm::FixedVectorType>(origVectorTy)->getNumElements();
7317330f729Sjoerg auto eltTy = origVectorTy->getElementType();
7327330f729Sjoerg assert(numElts != 1);
7337330f729Sjoerg
7347330f729Sjoerg // The largest size that we're still considering making subvectors of.
7357330f729Sjoerg // Always a power of 2.
7367330f729Sjoerg unsigned logCandidateNumElts = llvm::findLastSet(numElts, llvm::ZB_Undefined);
7377330f729Sjoerg unsigned candidateNumElts = 1U << logCandidateNumElts;
7387330f729Sjoerg assert(candidateNumElts <= numElts && candidateNumElts * 2 > numElts);
7397330f729Sjoerg
7407330f729Sjoerg // Minor optimization: don't check the legality of this exact size twice.
7417330f729Sjoerg if (candidateNumElts == numElts) {
7427330f729Sjoerg logCandidateNumElts--;
7437330f729Sjoerg candidateNumElts >>= 1;
7447330f729Sjoerg }
7457330f729Sjoerg
7467330f729Sjoerg CharUnits eltSize = (origVectorSize / numElts);
7477330f729Sjoerg CharUnits candidateSize = eltSize * candidateNumElts;
7487330f729Sjoerg
7497330f729Sjoerg // The sensibility of this algorithm relies on the fact that we never
7507330f729Sjoerg // have a legal non-power-of-2 vector size without having the power of 2
7517330f729Sjoerg // also be legal.
7527330f729Sjoerg while (logCandidateNumElts > 0) {
7537330f729Sjoerg assert(candidateNumElts == 1U << logCandidateNumElts);
7547330f729Sjoerg assert(candidateNumElts <= numElts);
7557330f729Sjoerg assert(candidateSize == eltSize * candidateNumElts);
7567330f729Sjoerg
7577330f729Sjoerg // Skip illegal vector sizes.
7587330f729Sjoerg if (!isLegalVectorType(CGM, candidateSize, eltTy, candidateNumElts)) {
7597330f729Sjoerg logCandidateNumElts--;
7607330f729Sjoerg candidateNumElts /= 2;
7617330f729Sjoerg candidateSize /= 2;
7627330f729Sjoerg continue;
7637330f729Sjoerg }
7647330f729Sjoerg
7657330f729Sjoerg // Add the right number of vectors of this size.
7667330f729Sjoerg auto numVecs = numElts >> logCandidateNumElts;
767*e038c9c4Sjoerg components.append(numVecs,
768*e038c9c4Sjoerg llvm::FixedVectorType::get(eltTy, candidateNumElts));
7697330f729Sjoerg numElts -= (numVecs << logCandidateNumElts);
7707330f729Sjoerg
7717330f729Sjoerg if (numElts == 0) return;
7727330f729Sjoerg
7737330f729Sjoerg // It's possible that the number of elements remaining will be legal.
7747330f729Sjoerg // This can happen with e.g. <7 x float> when <3 x float> is legal.
7757330f729Sjoerg // This only needs to be separately checked if it's not a power of 2.
7767330f729Sjoerg if (numElts > 2 && !isPowerOf2(numElts) &&
7777330f729Sjoerg isLegalVectorType(CGM, eltSize * numElts, eltTy, numElts)) {
778*e038c9c4Sjoerg components.push_back(llvm::FixedVectorType::get(eltTy, numElts));
7797330f729Sjoerg return;
7807330f729Sjoerg }
7817330f729Sjoerg
7827330f729Sjoerg // Bring vecSize down to something no larger than numElts.
7837330f729Sjoerg do {
7847330f729Sjoerg logCandidateNumElts--;
7857330f729Sjoerg candidateNumElts /= 2;
7867330f729Sjoerg candidateSize /= 2;
7877330f729Sjoerg } while (candidateNumElts > numElts);
7887330f729Sjoerg }
7897330f729Sjoerg
7907330f729Sjoerg // Otherwise, just append a bunch of individual elements.
7917330f729Sjoerg components.append(numElts, eltTy);
7927330f729Sjoerg }
7937330f729Sjoerg
mustPassRecordIndirectly(CodeGenModule & CGM,const RecordDecl * record)7947330f729Sjoerg bool swiftcall::mustPassRecordIndirectly(CodeGenModule &CGM,
7957330f729Sjoerg const RecordDecl *record) {
7967330f729Sjoerg // FIXME: should we not rely on the standard computation in Sema, just in
7977330f729Sjoerg // case we want to diverge from the platform ABI (e.g. on targets where
7987330f729Sjoerg // that uses the MSVC rule)?
7997330f729Sjoerg return !record->canPassInRegisters();
8007330f729Sjoerg }
8017330f729Sjoerg
classifyExpandedType(SwiftAggLowering & lowering,bool forReturn,CharUnits alignmentForIndirect)8027330f729Sjoerg static ABIArgInfo classifyExpandedType(SwiftAggLowering &lowering,
8037330f729Sjoerg bool forReturn,
8047330f729Sjoerg CharUnits alignmentForIndirect) {
8057330f729Sjoerg if (lowering.empty()) {
8067330f729Sjoerg return ABIArgInfo::getIgnore();
8077330f729Sjoerg } else if (lowering.shouldPassIndirectly(forReturn)) {
8087330f729Sjoerg return ABIArgInfo::getIndirect(alignmentForIndirect, /*byval*/ false);
8097330f729Sjoerg } else {
8107330f729Sjoerg auto types = lowering.getCoerceAndExpandTypes();
8117330f729Sjoerg return ABIArgInfo::getCoerceAndExpand(types.first, types.second);
8127330f729Sjoerg }
8137330f729Sjoerg }
8147330f729Sjoerg
classifyType(CodeGenModule & CGM,CanQualType type,bool forReturn)8157330f729Sjoerg static ABIArgInfo classifyType(CodeGenModule &CGM, CanQualType type,
8167330f729Sjoerg bool forReturn) {
8177330f729Sjoerg if (auto recordType = dyn_cast<RecordType>(type)) {
8187330f729Sjoerg auto record = recordType->getDecl();
8197330f729Sjoerg auto &layout = CGM.getContext().getASTRecordLayout(record);
8207330f729Sjoerg
8217330f729Sjoerg if (mustPassRecordIndirectly(CGM, record))
8227330f729Sjoerg return ABIArgInfo::getIndirect(layout.getAlignment(), /*byval*/ false);
8237330f729Sjoerg
8247330f729Sjoerg SwiftAggLowering lowering(CGM);
8257330f729Sjoerg lowering.addTypedData(recordType->getDecl(), CharUnits::Zero(), layout);
8267330f729Sjoerg lowering.finish();
8277330f729Sjoerg
8287330f729Sjoerg return classifyExpandedType(lowering, forReturn, layout.getAlignment());
8297330f729Sjoerg }
8307330f729Sjoerg
8317330f729Sjoerg // Just assume that all of our target ABIs can support returning at least
8327330f729Sjoerg // two integer or floating-point values.
8337330f729Sjoerg if (isa<ComplexType>(type)) {
8347330f729Sjoerg return (forReturn ? ABIArgInfo::getDirect() : ABIArgInfo::getExpand());
8357330f729Sjoerg }
8367330f729Sjoerg
8377330f729Sjoerg // Vector types may need to be legalized.
8387330f729Sjoerg if (isa<VectorType>(type)) {
8397330f729Sjoerg SwiftAggLowering lowering(CGM);
8407330f729Sjoerg lowering.addTypedData(type, CharUnits::Zero());
8417330f729Sjoerg lowering.finish();
8427330f729Sjoerg
8437330f729Sjoerg CharUnits alignment = CGM.getContext().getTypeAlignInChars(type);
8447330f729Sjoerg return classifyExpandedType(lowering, forReturn, alignment);
8457330f729Sjoerg }
8467330f729Sjoerg
8477330f729Sjoerg // Member pointer types need to be expanded, but it's a simple form of
8487330f729Sjoerg // expansion that 'Direct' can handle. Note that CanBeFlattened should be
8497330f729Sjoerg // true for this to work.
8507330f729Sjoerg
8517330f729Sjoerg // 'void' needs to be ignored.
8527330f729Sjoerg if (type->isVoidType()) {
8537330f729Sjoerg return ABIArgInfo::getIgnore();
8547330f729Sjoerg }
8557330f729Sjoerg
8567330f729Sjoerg // Everything else can be passed directly.
8577330f729Sjoerg return ABIArgInfo::getDirect();
8587330f729Sjoerg }
8597330f729Sjoerg
classifyReturnType(CodeGenModule & CGM,CanQualType type)8607330f729Sjoerg ABIArgInfo swiftcall::classifyReturnType(CodeGenModule &CGM, CanQualType type) {
8617330f729Sjoerg return classifyType(CGM, type, /*forReturn*/ true);
8627330f729Sjoerg }
8637330f729Sjoerg
classifyArgumentType(CodeGenModule & CGM,CanQualType type)8647330f729Sjoerg ABIArgInfo swiftcall::classifyArgumentType(CodeGenModule &CGM,
8657330f729Sjoerg CanQualType type) {
8667330f729Sjoerg return classifyType(CGM, type, /*forReturn*/ false);
8677330f729Sjoerg }
8687330f729Sjoerg
computeABIInfo(CodeGenModule & CGM,CGFunctionInfo & FI)8697330f729Sjoerg void swiftcall::computeABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) {
8707330f729Sjoerg auto &retInfo = FI.getReturnInfo();
8717330f729Sjoerg retInfo = classifyReturnType(CGM, FI.getReturnType());
8727330f729Sjoerg
8737330f729Sjoerg for (unsigned i = 0, e = FI.arg_size(); i != e; ++i) {
8747330f729Sjoerg auto &argInfo = FI.arg_begin()[i];
8757330f729Sjoerg argInfo.info = classifyArgumentType(CGM, argInfo.type);
8767330f729Sjoerg }
8777330f729Sjoerg }
8787330f729Sjoerg
8797330f729Sjoerg // Is swifterror lowered to a register by the target ABI.
isSwiftErrorLoweredInRegister(CodeGenModule & CGM)8807330f729Sjoerg bool swiftcall::isSwiftErrorLoweredInRegister(CodeGenModule &CGM) {
8817330f729Sjoerg return getSwiftABIInfo(CGM).isSwiftErrorInRegister();
8827330f729Sjoerg }
883