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