llvm-project
878 строк · 30.2 Кб
1//===--- SwiftCallingConv.cpp - Lowering for the Swift calling convention -===//
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// Implementation of the abstract lowering for the Swift calling convention.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/CodeGen/SwiftCallingConv.h"
14#include "ABIInfo.h"
15#include "CodeGenModule.h"
16#include "TargetInfo.h"
17#include "clang/Basic/TargetInfo.h"
18#include <optional>
19
20using namespace clang;
21using namespace CodeGen;
22using namespace swiftcall;
23
24static const SwiftABIInfo &getSwiftABIInfo(CodeGenModule &CGM) {
25return CGM.getTargetCodeGenInfo().getSwiftABIInfo();
26}
27
28static bool isPowerOf2(unsigned n) {
29return n == (n & -n);
30}
31
32/// Given two types with the same size, try to find a common type.
33static llvm::Type *getCommonType(llvm::Type *first, llvm::Type *second) {
34assert(first != second);
35
36// Allow pointers to merge with integers, but prefer the integer type.
37if (first->isIntegerTy()) {
38if (second->isPointerTy()) return first;
39} else if (first->isPointerTy()) {
40if (second->isIntegerTy()) return second;
41if (second->isPointerTy()) return first;
42
43// Allow two vectors to be merged (given that they have the same size).
44// This assumes that we never have two different vector register sets.
45} else if (auto firstVecTy = dyn_cast<llvm::VectorType>(first)) {
46if (auto secondVecTy = dyn_cast<llvm::VectorType>(second)) {
47if (auto commonTy = getCommonType(firstVecTy->getElementType(),
48secondVecTy->getElementType())) {
49return (commonTy == firstVecTy->getElementType() ? first : second);
50}
51}
52}
53
54return nullptr;
55}
56
57static CharUnits getTypeStoreSize(CodeGenModule &CGM, llvm::Type *type) {
58return CharUnits::fromQuantity(CGM.getDataLayout().getTypeStoreSize(type));
59}
60
61static CharUnits getTypeAllocSize(CodeGenModule &CGM, llvm::Type *type) {
62return CharUnits::fromQuantity(CGM.getDataLayout().getTypeAllocSize(type));
63}
64
65void SwiftAggLowering::addTypedData(QualType type, CharUnits begin) {
66// Deal with various aggregate types as special cases:
67
68// Record types.
69if (auto recType = type->getAs<RecordType>()) {
70addTypedData(recType->getDecl(), begin);
71
72// Array types.
73} else if (type->isArrayType()) {
74// Incomplete array types (flexible array members?) don't provide
75// data to lay out, and the other cases shouldn't be possible.
76auto arrayType = CGM.getContext().getAsConstantArrayType(type);
77if (!arrayType) return;
78
79QualType eltType = arrayType->getElementType();
80auto eltSize = CGM.getContext().getTypeSizeInChars(eltType);
81for (uint64_t i = 0, e = arrayType->getZExtSize(); i != e; ++i) {
82addTypedData(eltType, begin + i * eltSize);
83}
84
85// Complex types.
86} else if (auto complexType = type->getAs<ComplexType>()) {
87auto eltType = complexType->getElementType();
88auto eltSize = CGM.getContext().getTypeSizeInChars(eltType);
89auto eltLLVMType = CGM.getTypes().ConvertType(eltType);
90addTypedData(eltLLVMType, begin, begin + eltSize);
91addTypedData(eltLLVMType, begin + eltSize, begin + 2 * eltSize);
92
93// Member pointer types.
94} else if (type->getAs<MemberPointerType>()) {
95// Just add it all as opaque.
96addOpaqueData(begin, begin + CGM.getContext().getTypeSizeInChars(type));
97
98// Atomic types.
99} else if (const auto *atomicType = type->getAs<AtomicType>()) {
100auto valueType = atomicType->getValueType();
101auto atomicSize = CGM.getContext().getTypeSizeInChars(atomicType);
102auto valueSize = CGM.getContext().getTypeSizeInChars(valueType);
103
104addTypedData(atomicType->getValueType(), begin);
105
106// Add atomic padding.
107auto atomicPadding = atomicSize - valueSize;
108if (atomicPadding > CharUnits::Zero())
109addOpaqueData(begin + valueSize, begin + atomicSize);
110
111// Everything else is scalar and should not convert as an LLVM aggregate.
112} else {
113// We intentionally convert as !ForMem because we want to preserve
114// that a type was an i1.
115auto *llvmType = CGM.getTypes().ConvertType(type);
116addTypedData(llvmType, begin);
117}
118}
119
120void SwiftAggLowering::addTypedData(const RecordDecl *record, CharUnits begin) {
121addTypedData(record, begin, CGM.getContext().getASTRecordLayout(record));
122}
123
124void SwiftAggLowering::addTypedData(const RecordDecl *record, CharUnits begin,
125const ASTRecordLayout &layout) {
126// Unions are a special case.
127if (record->isUnion()) {
128for (auto *field : record->fields()) {
129if (field->isBitField()) {
130addBitFieldData(field, begin, 0);
131} else {
132addTypedData(field->getType(), begin);
133}
134}
135return;
136}
137
138// Note that correctness does not rely on us adding things in
139// their actual order of layout; it's just somewhat more efficient
140// for the builder.
141
142// With that in mind, add "early" C++ data.
143auto cxxRecord = dyn_cast<CXXRecordDecl>(record);
144if (cxxRecord) {
145// - a v-table pointer, if the class adds its own
146if (layout.hasOwnVFPtr()) {
147addTypedData(CGM.Int8PtrTy, begin);
148}
149
150// - non-virtual bases
151for (auto &baseSpecifier : cxxRecord->bases()) {
152if (baseSpecifier.isVirtual()) continue;
153
154auto baseRecord = baseSpecifier.getType()->getAsCXXRecordDecl();
155addTypedData(baseRecord, begin + layout.getBaseClassOffset(baseRecord));
156}
157
158// - a vbptr if the class adds its own
159if (layout.hasOwnVBPtr()) {
160addTypedData(CGM.Int8PtrTy, begin + layout.getVBPtrOffset());
161}
162}
163
164// Add fields.
165for (auto *field : record->fields()) {
166auto fieldOffsetInBits = layout.getFieldOffset(field->getFieldIndex());
167if (field->isBitField()) {
168addBitFieldData(field, begin, fieldOffsetInBits);
169} else {
170addTypedData(field->getType(),
171begin + CGM.getContext().toCharUnitsFromBits(fieldOffsetInBits));
172}
173}
174
175// Add "late" C++ data:
176if (cxxRecord) {
177// - virtual bases
178for (auto &vbaseSpecifier : cxxRecord->vbases()) {
179auto baseRecord = vbaseSpecifier.getType()->getAsCXXRecordDecl();
180addTypedData(baseRecord, begin + layout.getVBaseClassOffset(baseRecord));
181}
182}
183}
184
185void SwiftAggLowering::addBitFieldData(const FieldDecl *bitfield,
186CharUnits recordBegin,
187uint64_t bitfieldBitBegin) {
188assert(bitfield->isBitField());
189auto &ctx = CGM.getContext();
190auto width = bitfield->getBitWidthValue(ctx);
191
192// We can ignore zero-width bit-fields.
193if (width == 0) return;
194
195// toCharUnitsFromBits rounds down.
196CharUnits bitfieldByteBegin = ctx.toCharUnitsFromBits(bitfieldBitBegin);
197
198// Find the offset of the last byte that is partially occupied by the
199// bit-field; since we otherwise expect exclusive ends, the end is the
200// next byte.
201uint64_t bitfieldBitLast = bitfieldBitBegin + width - 1;
202CharUnits bitfieldByteEnd =
203ctx.toCharUnitsFromBits(bitfieldBitLast) + CharUnits::One();
204addOpaqueData(recordBegin + bitfieldByteBegin,
205recordBegin + bitfieldByteEnd);
206}
207
208void SwiftAggLowering::addTypedData(llvm::Type *type, CharUnits begin) {
209assert(type && "didn't provide type for typed data");
210addTypedData(type, begin, begin + getTypeStoreSize(CGM, type));
211}
212
213void SwiftAggLowering::addTypedData(llvm::Type *type,
214CharUnits begin, CharUnits end) {
215assert(type && "didn't provide type for typed data");
216assert(getTypeStoreSize(CGM, type) == end - begin);
217
218// Legalize vector types.
219if (auto vecTy = dyn_cast<llvm::VectorType>(type)) {
220SmallVector<llvm::Type*, 4> componentTys;
221legalizeVectorType(CGM, end - begin, vecTy, componentTys);
222assert(componentTys.size() >= 1);
223
224// Walk the initial components.
225for (size_t i = 0, e = componentTys.size(); i != e - 1; ++i) {
226llvm::Type *componentTy = componentTys[i];
227auto componentSize = getTypeStoreSize(CGM, componentTy);
228assert(componentSize < end - begin);
229addLegalTypedData(componentTy, begin, begin + componentSize);
230begin += componentSize;
231}
232
233return addLegalTypedData(componentTys.back(), begin, end);
234}
235
236// Legalize integer types.
237if (auto intTy = dyn_cast<llvm::IntegerType>(type)) {
238if (!isLegalIntegerType(CGM, intTy))
239return addOpaqueData(begin, end);
240}
241
242// All other types should be legal.
243return addLegalTypedData(type, begin, end);
244}
245
246void SwiftAggLowering::addLegalTypedData(llvm::Type *type,
247CharUnits begin, CharUnits end) {
248// Require the type to be naturally aligned.
249if (!begin.isZero() && !begin.isMultipleOf(getNaturalAlignment(CGM, type))) {
250
251// Try splitting vector types.
252if (auto vecTy = dyn_cast<llvm::VectorType>(type)) {
253auto split = splitLegalVectorType(CGM, end - begin, vecTy);
254auto eltTy = split.first;
255auto numElts = split.second;
256
257auto eltSize = (end - begin) / numElts;
258assert(eltSize == getTypeStoreSize(CGM, eltTy));
259for (size_t i = 0, e = numElts; i != e; ++i) {
260addLegalTypedData(eltTy, begin, begin + eltSize);
261begin += eltSize;
262}
263assert(begin == end);
264return;
265}
266
267return addOpaqueData(begin, end);
268}
269
270addEntry(type, begin, end);
271}
272
273void SwiftAggLowering::addEntry(llvm::Type *type,
274CharUnits begin, CharUnits end) {
275assert((!type ||
276(!isa<llvm::StructType>(type) && !isa<llvm::ArrayType>(type))) &&
277"cannot add aggregate-typed data");
278assert(!type || begin.isMultipleOf(getNaturalAlignment(CGM, type)));
279
280// Fast path: we can just add entries to the end.
281if (Entries.empty() || Entries.back().End <= begin) {
282Entries.push_back({begin, end, type});
283return;
284}
285
286// Find the first existing entry that ends after the start of the new data.
287// TODO: do a binary search if Entries is big enough for it to matter.
288size_t index = Entries.size() - 1;
289while (index != 0) {
290if (Entries[index - 1].End <= begin) break;
291--index;
292}
293
294// The entry ends after the start of the new data.
295// If the entry starts after the end of the new data, there's no conflict.
296if (Entries[index].Begin >= end) {
297// This insertion is potentially O(n), but the way we generally build
298// these layouts makes that unlikely to matter: we'd need a union of
299// several very large types.
300Entries.insert(Entries.begin() + index, {begin, end, type});
301return;
302}
303
304// Otherwise, the ranges overlap. The new range might also overlap
305// with later ranges.
306restartAfterSplit:
307
308// Simplest case: an exact overlap.
309if (Entries[index].Begin == begin && Entries[index].End == end) {
310// If the types match exactly, great.
311if (Entries[index].Type == type) return;
312
313// If either type is opaque, make the entry opaque and return.
314if (Entries[index].Type == nullptr) {
315return;
316} else if (type == nullptr) {
317Entries[index].Type = nullptr;
318return;
319}
320
321// If they disagree in an ABI-agnostic way, just resolve the conflict
322// arbitrarily.
323if (auto entryType = getCommonType(Entries[index].Type, type)) {
324Entries[index].Type = entryType;
325return;
326}
327
328// Otherwise, make the entry opaque.
329Entries[index].Type = nullptr;
330return;
331}
332
333// Okay, we have an overlapping conflict of some sort.
334
335// If we have a vector type, split it.
336if (auto vecTy = dyn_cast_or_null<llvm::VectorType>(type)) {
337auto eltTy = vecTy->getElementType();
338CharUnits eltSize =
339(end - begin) / cast<llvm::FixedVectorType>(vecTy)->getNumElements();
340assert(eltSize == getTypeStoreSize(CGM, eltTy));
341for (unsigned i = 0,
342e = cast<llvm::FixedVectorType>(vecTy)->getNumElements();
343i != e; ++i) {
344addEntry(eltTy, begin, begin + eltSize);
345begin += eltSize;
346}
347assert(begin == end);
348return;
349}
350
351// If the entry is a vector type, split it and try again.
352if (Entries[index].Type && Entries[index].Type->isVectorTy()) {
353splitVectorEntry(index);
354goto restartAfterSplit;
355}
356
357// Okay, we have no choice but to make the existing entry opaque.
358
359Entries[index].Type = nullptr;
360
361// Stretch the start of the entry to the beginning of the range.
362if (begin < Entries[index].Begin) {
363Entries[index].Begin = begin;
364assert(index == 0 || begin >= Entries[index - 1].End);
365}
366
367// Stretch the end of the entry to the end of the range; but if we run
368// into the start of the next entry, just leave the range there and repeat.
369while (end > Entries[index].End) {
370assert(Entries[index].Type == nullptr);
371
372// If the range doesn't overlap the next entry, we're done.
373if (index == Entries.size() - 1 || end <= Entries[index + 1].Begin) {
374Entries[index].End = end;
375break;
376}
377
378// Otherwise, stretch to the start of the next entry.
379Entries[index].End = Entries[index + 1].Begin;
380
381// Continue with the next entry.
382index++;
383
384// This entry needs to be made opaque if it is not already.
385if (Entries[index].Type == nullptr)
386continue;
387
388// Split vector entries unless we completely subsume them.
389if (Entries[index].Type->isVectorTy() &&
390end < Entries[index].End) {
391splitVectorEntry(index);
392}
393
394// Make the entry opaque.
395Entries[index].Type = nullptr;
396}
397}
398
399/// Replace the entry of vector type at offset 'index' with a sequence
400/// of its component vectors.
401void SwiftAggLowering::splitVectorEntry(unsigned index) {
402auto vecTy = cast<llvm::VectorType>(Entries[index].Type);
403auto split = splitLegalVectorType(CGM, Entries[index].getWidth(), vecTy);
404
405auto eltTy = split.first;
406CharUnits eltSize = getTypeStoreSize(CGM, eltTy);
407auto numElts = split.second;
408Entries.insert(Entries.begin() + index + 1, numElts - 1, StorageEntry());
409
410CharUnits begin = Entries[index].Begin;
411for (unsigned i = 0; i != numElts; ++i) {
412unsigned idx = index + i;
413Entries[idx].Type = eltTy;
414Entries[idx].Begin = begin;
415Entries[idx].End = begin + eltSize;
416begin += eltSize;
417}
418}
419
420/// Given a power-of-two unit size, return the offset of the aligned unit
421/// of that size which contains the given offset.
422///
423/// In other words, round down to the nearest multiple of the unit size.
424static CharUnits getOffsetAtStartOfUnit(CharUnits offset, CharUnits unitSize) {
425assert(isPowerOf2(unitSize.getQuantity()));
426auto unitMask = ~(unitSize.getQuantity() - 1);
427return CharUnits::fromQuantity(offset.getQuantity() & unitMask);
428}
429
430static bool areBytesInSameUnit(CharUnits first, CharUnits second,
431CharUnits chunkSize) {
432return getOffsetAtStartOfUnit(first, chunkSize)
433== getOffsetAtStartOfUnit(second, chunkSize);
434}
435
436static bool isMergeableEntryType(llvm::Type *type) {
437// Opaquely-typed memory is always mergeable.
438if (type == nullptr) return true;
439
440// Pointers and integers are always mergeable. In theory we should not
441// merge pointers, but (1) it doesn't currently matter in practice because
442// the chunk size is never greater than the size of a pointer and (2)
443// Swift IRGen uses integer types for a lot of things that are "really"
444// just storing pointers (like std::optional<SomePointer>). If we ever have a
445// target that would otherwise combine pointers, we should put some effort
446// into fixing those cases in Swift IRGen and then call out pointer types
447// here.
448
449// Floating-point and vector types should never be merged.
450// Most such types are too large and highly-aligned to ever trigger merging
451// in practice, but it's important for the rule to cover at least 'half'
452// and 'float', as well as things like small vectors of 'i1' or 'i8'.
453return (!type->isFloatingPointTy() && !type->isVectorTy());
454}
455
456bool SwiftAggLowering::shouldMergeEntries(const StorageEntry &first,
457const StorageEntry &second,
458CharUnits chunkSize) {
459// Only merge entries that overlap the same chunk. We test this first
460// despite being a bit more expensive because this is the condition that
461// tends to prevent merging.
462if (!areBytesInSameUnit(first.End - CharUnits::One(), second.Begin,
463chunkSize))
464return false;
465
466return (isMergeableEntryType(first.Type) &&
467isMergeableEntryType(second.Type));
468}
469
470void SwiftAggLowering::finish() {
471if (Entries.empty()) {
472Finished = true;
473return;
474}
475
476// We logically split the layout down into a series of chunks of this size,
477// which is generally the size of a pointer.
478const CharUnits chunkSize = getMaximumVoluntaryIntegerSize(CGM);
479
480// First pass: if two entries should be merged, make them both opaque
481// and stretch one to meet the next.
482// Also, remember if there are any opaque entries.
483bool hasOpaqueEntries = (Entries[0].Type == nullptr);
484for (size_t i = 1, e = Entries.size(); i != e; ++i) {
485if (shouldMergeEntries(Entries[i - 1], Entries[i], chunkSize)) {
486Entries[i - 1].Type = nullptr;
487Entries[i].Type = nullptr;
488Entries[i - 1].End = Entries[i].Begin;
489hasOpaqueEntries = true;
490
491} else if (Entries[i].Type == nullptr) {
492hasOpaqueEntries = true;
493}
494}
495
496// The rest of the algorithm leaves non-opaque entries alone, so if we
497// have no opaque entries, we're done.
498if (!hasOpaqueEntries) {
499Finished = true;
500return;
501}
502
503// Okay, move the entries to a temporary and rebuild Entries.
504auto orig = std::move(Entries);
505assert(Entries.empty());
506
507for (size_t i = 0, e = orig.size(); i != e; ++i) {
508// Just copy over non-opaque entries.
509if (orig[i].Type != nullptr) {
510Entries.push_back(orig[i]);
511continue;
512}
513
514// Scan forward to determine the full extent of the next opaque range.
515// We know from the first pass that only contiguous ranges will overlap
516// the same aligned chunk.
517auto begin = orig[i].Begin;
518auto end = orig[i].End;
519while (i + 1 != e &&
520orig[i + 1].Type == nullptr &&
521end == orig[i + 1].Begin) {
522end = orig[i + 1].End;
523i++;
524}
525
526// Add an entry per intersected chunk.
527do {
528// Find the smallest aligned storage unit in the maximal aligned
529// storage unit containing 'begin' that contains all the bytes in
530// the intersection between the range and this chunk.
531CharUnits localBegin = begin;
532CharUnits chunkBegin = getOffsetAtStartOfUnit(localBegin, chunkSize);
533CharUnits chunkEnd = chunkBegin + chunkSize;
534CharUnits localEnd = std::min(end, chunkEnd);
535
536// Just do a simple loop over ever-increasing unit sizes.
537CharUnits unitSize = CharUnits::One();
538CharUnits unitBegin, unitEnd;
539for (; ; unitSize *= 2) {
540assert(unitSize <= chunkSize);
541unitBegin = getOffsetAtStartOfUnit(localBegin, unitSize);
542unitEnd = unitBegin + unitSize;
543if (unitEnd >= localEnd) break;
544}
545
546// Add an entry for this unit.
547auto entryTy =
548llvm::IntegerType::get(CGM.getLLVMContext(),
549CGM.getContext().toBits(unitSize));
550Entries.push_back({unitBegin, unitEnd, entryTy});
551
552// The next chunk starts where this chunk left off.
553begin = localEnd;
554} while (begin != end);
555}
556
557// Okay, finally finished.
558Finished = true;
559}
560
561void SwiftAggLowering::enumerateComponents(EnumerationCallback callback) const {
562assert(Finished && "haven't yet finished lowering");
563
564for (auto &entry : Entries) {
565callback(entry.Begin, entry.End, entry.Type);
566}
567}
568
569std::pair<llvm::StructType*, llvm::Type*>
570SwiftAggLowering::getCoerceAndExpandTypes() const {
571assert(Finished && "haven't yet finished lowering");
572
573auto &ctx = CGM.getLLVMContext();
574
575if (Entries.empty()) {
576auto type = llvm::StructType::get(ctx);
577return { type, type };
578}
579
580SmallVector<llvm::Type*, 8> elts;
581CharUnits lastEnd = CharUnits::Zero();
582bool hasPadding = false;
583bool packed = false;
584for (auto &entry : Entries) {
585if (entry.Begin != lastEnd) {
586auto paddingSize = entry.Begin - lastEnd;
587assert(!paddingSize.isNegative());
588
589auto padding = llvm::ArrayType::get(llvm::Type::getInt8Ty(ctx),
590paddingSize.getQuantity());
591elts.push_back(padding);
592hasPadding = true;
593}
594
595if (!packed && !entry.Begin.isMultipleOf(CharUnits::fromQuantity(
596CGM.getDataLayout().getABITypeAlign(entry.Type))))
597packed = true;
598
599elts.push_back(entry.Type);
600
601lastEnd = entry.Begin + getTypeAllocSize(CGM, entry.Type);
602assert(entry.End <= lastEnd);
603}
604
605// We don't need to adjust 'packed' to deal with possible tail padding
606// because we never do that kind of access through the coercion type.
607auto coercionType = llvm::StructType::get(ctx, elts, packed);
608
609llvm::Type *unpaddedType = coercionType;
610if (hasPadding) {
611elts.clear();
612for (auto &entry : Entries) {
613elts.push_back(entry.Type);
614}
615if (elts.size() == 1) {
616unpaddedType = elts[0];
617} else {
618unpaddedType = llvm::StructType::get(ctx, elts, /*packed*/ false);
619}
620} else if (Entries.size() == 1) {
621unpaddedType = Entries[0].Type;
622}
623
624return { coercionType, unpaddedType };
625}
626
627bool SwiftAggLowering::shouldPassIndirectly(bool asReturnValue) const {
628assert(Finished && "haven't yet finished lowering");
629
630// Empty types don't need to be passed indirectly.
631if (Entries.empty()) return false;
632
633// Avoid copying the array of types when there's just a single element.
634if (Entries.size() == 1) {
635return getSwiftABIInfo(CGM).shouldPassIndirectly(Entries.back().Type,
636asReturnValue);
637}
638
639SmallVector<llvm::Type*, 8> componentTys;
640componentTys.reserve(Entries.size());
641for (auto &entry : Entries) {
642componentTys.push_back(entry.Type);
643}
644return getSwiftABIInfo(CGM).shouldPassIndirectly(componentTys, asReturnValue);
645}
646
647bool swiftcall::shouldPassIndirectly(CodeGenModule &CGM,
648ArrayRef<llvm::Type*> componentTys,
649bool asReturnValue) {
650return getSwiftABIInfo(CGM).shouldPassIndirectly(componentTys, asReturnValue);
651}
652
653CharUnits swiftcall::getMaximumVoluntaryIntegerSize(CodeGenModule &CGM) {
654// Currently always the size of an ordinary pointer.
655return CGM.getContext().toCharUnitsFromBits(
656CGM.getContext().getTargetInfo().getPointerWidth(LangAS::Default));
657}
658
659CharUnits swiftcall::getNaturalAlignment(CodeGenModule &CGM, llvm::Type *type) {
660// For Swift's purposes, this is always just the store size of the type
661// rounded up to a power of 2.
662auto size = (unsigned long long) getTypeStoreSize(CGM, type).getQuantity();
663size = llvm::bit_ceil(size);
664assert(CGM.getDataLayout().getABITypeAlign(type) <= size);
665return CharUnits::fromQuantity(size);
666}
667
668bool swiftcall::isLegalIntegerType(CodeGenModule &CGM,
669llvm::IntegerType *intTy) {
670auto size = intTy->getBitWidth();
671switch (size) {
672case 1:
673case 8:
674case 16:
675case 32:
676case 64:
677// Just assume that the above are always legal.
678return true;
679
680case 128:
681return CGM.getContext().getTargetInfo().hasInt128Type();
682
683default:
684return false;
685}
686}
687
688bool swiftcall::isLegalVectorType(CodeGenModule &CGM, CharUnits vectorSize,
689llvm::VectorType *vectorTy) {
690return isLegalVectorType(
691CGM, vectorSize, vectorTy->getElementType(),
692cast<llvm::FixedVectorType>(vectorTy)->getNumElements());
693}
694
695bool swiftcall::isLegalVectorType(CodeGenModule &CGM, CharUnits vectorSize,
696llvm::Type *eltTy, unsigned numElts) {
697assert(numElts > 1 && "illegal vector length");
698return getSwiftABIInfo(CGM).isLegalVectorType(vectorSize, eltTy, numElts);
699}
700
701std::pair<llvm::Type*, unsigned>
702swiftcall::splitLegalVectorType(CodeGenModule &CGM, CharUnits vectorSize,
703llvm::VectorType *vectorTy) {
704auto numElts = cast<llvm::FixedVectorType>(vectorTy)->getNumElements();
705auto eltTy = vectorTy->getElementType();
706
707// Try to split the vector type in half.
708if (numElts >= 4 && isPowerOf2(numElts)) {
709if (isLegalVectorType(CGM, vectorSize / 2, eltTy, numElts / 2))
710return {llvm::FixedVectorType::get(eltTy, numElts / 2), 2};
711}
712
713return {eltTy, numElts};
714}
715
716void swiftcall::legalizeVectorType(CodeGenModule &CGM, CharUnits origVectorSize,
717llvm::VectorType *origVectorTy,
718llvm::SmallVectorImpl<llvm::Type*> &components) {
719// If it's already a legal vector type, use it.
720if (isLegalVectorType(CGM, origVectorSize, origVectorTy)) {
721components.push_back(origVectorTy);
722return;
723}
724
725// Try to split the vector into legal subvectors.
726auto numElts = cast<llvm::FixedVectorType>(origVectorTy)->getNumElements();
727auto eltTy = origVectorTy->getElementType();
728assert(numElts != 1);
729
730// The largest size that we're still considering making subvectors of.
731// Always a power of 2.
732unsigned logCandidateNumElts = llvm::Log2_32(numElts);
733unsigned candidateNumElts = 1U << logCandidateNumElts;
734assert(candidateNumElts <= numElts && candidateNumElts * 2 > numElts);
735
736// Minor optimization: don't check the legality of this exact size twice.
737if (candidateNumElts == numElts) {
738logCandidateNumElts--;
739candidateNumElts >>= 1;
740}
741
742CharUnits eltSize = (origVectorSize / numElts);
743CharUnits candidateSize = eltSize * candidateNumElts;
744
745// The sensibility of this algorithm relies on the fact that we never
746// have a legal non-power-of-2 vector size without having the power of 2
747// also be legal.
748while (logCandidateNumElts > 0) {
749assert(candidateNumElts == 1U << logCandidateNumElts);
750assert(candidateNumElts <= numElts);
751assert(candidateSize == eltSize * candidateNumElts);
752
753// Skip illegal vector sizes.
754if (!isLegalVectorType(CGM, candidateSize, eltTy, candidateNumElts)) {
755logCandidateNumElts--;
756candidateNumElts /= 2;
757candidateSize /= 2;
758continue;
759}
760
761// Add the right number of vectors of this size.
762auto numVecs = numElts >> logCandidateNumElts;
763components.append(numVecs,
764llvm::FixedVectorType::get(eltTy, candidateNumElts));
765numElts -= (numVecs << logCandidateNumElts);
766
767if (numElts == 0) return;
768
769// It's possible that the number of elements remaining will be legal.
770// This can happen with e.g. <7 x float> when <3 x float> is legal.
771// This only needs to be separately checked if it's not a power of 2.
772if (numElts > 2 && !isPowerOf2(numElts) &&
773isLegalVectorType(CGM, eltSize * numElts, eltTy, numElts)) {
774components.push_back(llvm::FixedVectorType::get(eltTy, numElts));
775return;
776}
777
778// Bring vecSize down to something no larger than numElts.
779do {
780logCandidateNumElts--;
781candidateNumElts /= 2;
782candidateSize /= 2;
783} while (candidateNumElts > numElts);
784}
785
786// Otherwise, just append a bunch of individual elements.
787components.append(numElts, eltTy);
788}
789
790bool swiftcall::mustPassRecordIndirectly(CodeGenModule &CGM,
791const RecordDecl *record) {
792// FIXME: should we not rely on the standard computation in Sema, just in
793// case we want to diverge from the platform ABI (e.g. on targets where
794// that uses the MSVC rule)?
795return !record->canPassInRegisters();
796}
797
798static ABIArgInfo classifyExpandedType(SwiftAggLowering &lowering,
799bool forReturn,
800CharUnits alignmentForIndirect) {
801if (lowering.empty()) {
802return ABIArgInfo::getIgnore();
803} else if (lowering.shouldPassIndirectly(forReturn)) {
804return ABIArgInfo::getIndirect(alignmentForIndirect, /*byval*/ false);
805} else {
806auto types = lowering.getCoerceAndExpandTypes();
807return ABIArgInfo::getCoerceAndExpand(types.first, types.second);
808}
809}
810
811static ABIArgInfo classifyType(CodeGenModule &CGM, CanQualType type,
812bool forReturn) {
813if (auto recordType = dyn_cast<RecordType>(type)) {
814auto record = recordType->getDecl();
815auto &layout = CGM.getContext().getASTRecordLayout(record);
816
817if (mustPassRecordIndirectly(CGM, record))
818return ABIArgInfo::getIndirect(layout.getAlignment(), /*byval*/ false);
819
820SwiftAggLowering lowering(CGM);
821lowering.addTypedData(recordType->getDecl(), CharUnits::Zero(), layout);
822lowering.finish();
823
824return classifyExpandedType(lowering, forReturn, layout.getAlignment());
825}
826
827// Just assume that all of our target ABIs can support returning at least
828// two integer or floating-point values.
829if (isa<ComplexType>(type)) {
830return (forReturn ? ABIArgInfo::getDirect() : ABIArgInfo::getExpand());
831}
832
833// Vector types may need to be legalized.
834if (isa<VectorType>(type)) {
835SwiftAggLowering lowering(CGM);
836lowering.addTypedData(type, CharUnits::Zero());
837lowering.finish();
838
839CharUnits alignment = CGM.getContext().getTypeAlignInChars(type);
840return classifyExpandedType(lowering, forReturn, alignment);
841}
842
843// Member pointer types need to be expanded, but it's a simple form of
844// expansion that 'Direct' can handle. Note that CanBeFlattened should be
845// true for this to work.
846
847// 'void' needs to be ignored.
848if (type->isVoidType()) {
849return ABIArgInfo::getIgnore();
850}
851
852// Everything else can be passed directly.
853return ABIArgInfo::getDirect();
854}
855
856ABIArgInfo swiftcall::classifyReturnType(CodeGenModule &CGM, CanQualType type) {
857return classifyType(CGM, type, /*forReturn*/ true);
858}
859
860ABIArgInfo swiftcall::classifyArgumentType(CodeGenModule &CGM,
861CanQualType type) {
862return classifyType(CGM, type, /*forReturn*/ false);
863}
864
865void swiftcall::computeABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) {
866auto &retInfo = FI.getReturnInfo();
867retInfo = classifyReturnType(CGM, FI.getReturnType());
868
869for (unsigned i = 0, e = FI.arg_size(); i != e; ++i) {
870auto &argInfo = FI.arg_begin()[i];
871argInfo.info = classifyArgumentType(CGM, argInfo.type);
872}
873}
874
875// Is swifterror lowered to a register by the target ABI.
876bool swiftcall::isSwiftErrorLoweredInRegister(CodeGenModule &CGM) {
877return getSwiftABIInfo(CGM).isSwiftErrorInRegister();
878}
879