21using namespace CodeGen;
22using namespace swiftcall;
33static llvm::Type *
getCommonType(llvm::Type *first, llvm::Type *second) {
34 assert(first != second);
37 if (first->isIntegerTy()) {
38 if (second->isPointerTy())
return first;
39 }
else if (first->isPointerTy()) {
40 if (second->isIntegerTy())
return second;
41 if (second->isPointerTy())
return first;
45 }
else if (
auto firstVecTy = dyn_cast<llvm::VectorType>(first)) {
46 if (
auto secondVecTy = dyn_cast<llvm::VectorType>(second)) {
47 if (
auto commonTy =
getCommonType(firstVecTy->getElementType(),
48 secondVecTy->getElementType())) {
49 return (commonTy == firstVecTy->getElementType() ? first : second);
73 }
else if (
type->isArrayType()) {
81 for (uint64_t i = 0, e =
arrayType->getZExtSize(); i != e; ++i) {
91 addTypedData(eltLLVMType, begin + eltSize, begin + 2 * eltSize);
107 auto atomicPadding = atomicSize - valueSize;
128 for (
auto *field : record->
fields()) {
129 if (field->isBitField()) {
130 addBitFieldData(field, begin, 0);
143 auto cxxRecord = dyn_cast<CXXRecordDecl>(record);
151 for (
auto &baseSpecifier : cxxRecord->bases()) {
152 if (baseSpecifier.isVirtual())
continue;
154 auto baseRecord = baseSpecifier.getType()->getAsCXXRecordDecl();
165 for (
auto *field : record->
fields()) {
166 auto fieldOffsetInBits = layout.
getFieldOffset(field->getFieldIndex());
167 if (field->isBitField()) {
168 addBitFieldData(field, begin, fieldOffsetInBits);
178 for (
auto &vbaseSpecifier : cxxRecord->vbases()) {
179 auto baseRecord = vbaseSpecifier.getType()->getAsCXXRecordDecl();
185void SwiftAggLowering::addBitFieldData(
const FieldDecl *bitfield,
187 uint64_t bitfieldBitBegin) {
193 if (width == 0)
return;
196 CharUnits bitfieldByteBegin = ctx.toCharUnitsFromBits(bitfieldBitBegin);
201 uint64_t bitfieldBitLast = bitfieldBitBegin + width - 1;
205 recordBegin + bitfieldByteEnd);
209 assert(
type &&
"didn't provide type for typed data");
215 assert(
type &&
"didn't provide type for typed data");
219 if (
auto vecTy = dyn_cast<llvm::VectorType>(
type)) {
222 assert(componentTys.size() >= 1);
225 for (
size_t i = 0, e = componentTys.size(); i != e - 1; ++i) {
226 llvm::Type *componentTy = componentTys[i];
228 assert(componentSize < end - begin);
229 addLegalTypedData(componentTy, begin, begin + componentSize);
230 begin += componentSize;
233 return addLegalTypedData(componentTys.back(), begin, end);
237 if (
auto intTy = dyn_cast<llvm::IntegerType>(
type)) {
243 return addLegalTypedData(
type, begin, end);
246void SwiftAggLowering::addLegalTypedData(llvm::Type *
type,
252 if (
auto vecTy = dyn_cast<llvm::VectorType>(
type)) {
254 auto eltTy = split.first;
255 auto numElts = split.second;
257 auto eltSize = (end - begin) / numElts;
259 for (
size_t i = 0, e = numElts; i != e; ++i) {
260 addLegalTypedData(eltTy, begin, begin + eltSize);
263 assert(begin == end);
270 addEntry(
type, begin, end);
273void SwiftAggLowering::addEntry(llvm::Type *
type,
276 (!isa<llvm::StructType>(
type) && !isa<llvm::ArrayType>(
type))) &&
277 "cannot add aggregate-typed data");
281 if (Entries.empty() || Entries.back().End <= begin) {
282 Entries.push_back({begin, end,
type});
288 size_t index = Entries.size() - 1;
290 if (Entries[index - 1].End <= begin)
break;
296 if (Entries[index].
Begin >= end) {
300 Entries.insert(Entries.begin() + index, {begin, end, type});
309 if (Entries[index].
Begin == begin && Entries[index].End == end) {
311 if (Entries[index].
Type ==
type)
return;
314 if (Entries[index].
Type ==
nullptr) {
316 }
else if (
type ==
nullptr) {
317 Entries[index].Type =
nullptr;
324 Entries[index].Type = entryType;
329 Entries[index].Type =
nullptr;
336 if (
auto vecTy = dyn_cast_or_null<llvm::VectorType>(
type)) {
337 auto eltTy = vecTy->getElementType();
339 (end - begin) / cast<llvm::FixedVectorType>(vecTy)->getNumElements();
342 e = cast<llvm::FixedVectorType>(vecTy)->getNumElements();
344 addEntry(eltTy, begin, begin + eltSize);
347 assert(begin == end);
352 if (Entries[index].
Type && Entries[index].
Type->isVectorTy()) {
353 splitVectorEntry(index);
354 goto restartAfterSplit;
359 Entries[index].Type =
nullptr;
362 if (begin < Entries[index].
Begin) {
363 Entries[index].Begin = begin;
364 assert(index == 0 || begin >= Entries[index - 1].End);
369 while (end > Entries[index].End) {
370 assert(Entries[index].
Type ==
nullptr);
373 if (index == Entries.size() - 1 || end <= Entries[index + 1].Begin) {
374 Entries[index].End = end;
379 Entries[index].End = Entries[index + 1].Begin;
385 if (Entries[index].
Type ==
nullptr)
389 if (Entries[index].
Type->isVectorTy() &&
390 end < Entries[index].End) {
391 splitVectorEntry(index);
395 Entries[index].Type =
nullptr;
401void SwiftAggLowering::splitVectorEntry(
unsigned index) {
402 auto vecTy = cast<llvm::VectorType>(Entries[index].
Type);
405 auto eltTy = split.first;
407 auto numElts = split.second;
408 Entries.insert(Entries.begin() + index + 1, numElts - 1, StorageEntry());
411 for (
unsigned i = 0; i != numElts; ++i) {
412 unsigned idx = index + i;
413 Entries[idx].Type = eltTy;
414 Entries[idx].Begin = begin;
415 Entries[idx].End = begin + eltSize;
438 if (
type ==
nullptr)
return true;
453 return (!
type->isFloatingPointTy() && !
type->isVectorTy());
456bool SwiftAggLowering::shouldMergeEntries(
const StorageEntry &first,
457 const StorageEntry &second,
471 if (Entries.empty()) {
483 bool hasOpaqueEntries = (Entries[0].Type ==
nullptr);
484 for (
size_t i = 1, e = Entries.size(); i != e; ++i) {
485 if (shouldMergeEntries(Entries[i - 1], Entries[i], chunkSize)) {
486 Entries[i - 1].Type =
nullptr;
487 Entries[i].Type =
nullptr;
488 Entries[i - 1].End = Entries[i].Begin;
489 hasOpaqueEntries =
true;
491 }
else if (Entries[i].
Type ==
nullptr) {
492 hasOpaqueEntries =
true;
498 if (!hasOpaqueEntries) {
504 auto orig = std::move(Entries);
505 assert(Entries.empty());
507 for (
size_t i = 0, e = orig.size(); i != e; ++i) {
509 if (orig[i].
Type !=
nullptr) {
510 Entries.push_back(orig[i]);
517 auto begin = orig[i].Begin;
518 auto end = orig[i].End;
520 orig[i + 1].
Type ==
nullptr &&
521 end == orig[i + 1].
Begin) {
522 end = orig[i + 1].End;
533 CharUnits chunkEnd = chunkBegin + chunkSize;
534 CharUnits localEnd = std::min(end, chunkEnd);
539 for (; ; unitSize *= 2) {
540 assert(unitSize <= chunkSize);
542 unitEnd = unitBegin + unitSize;
543 if (unitEnd >= localEnd)
break;
550 Entries.push_back({unitBegin, unitEnd, entryTy});
554 }
while (begin != end);
562 assert(Finished &&
"haven't yet finished lowering");
564 for (
auto &entry : Entries) {
565 callback(entry.Begin, entry.End, entry.Type);
569std::pair<llvm::StructType*, llvm::Type*>
571 assert(Finished &&
"haven't yet finished lowering");
575 if (Entries.empty()) {
576 auto type = llvm::StructType::get(ctx);
582 bool hasPadding =
false;
584 for (
auto &entry : Entries) {
585 if (entry.Begin != lastEnd) {
586 auto paddingSize = entry.Begin - lastEnd;
587 assert(!paddingSize.isNegative());
589 auto padding = llvm::ArrayType::get(llvm::Type::getInt8Ty(ctx),
590 paddingSize.getQuantity());
591 elts.push_back(padding);
599 elts.push_back(entry.Type);
602 assert(entry.End <= lastEnd);
607 auto coercionType = llvm::StructType::get(ctx, elts, packed);
609 llvm::Type *unpaddedType = coercionType;
612 for (
auto &entry : Entries) {
613 elts.push_back(entry.Type);
615 if (elts.size() == 1) {
616 unpaddedType = elts[0];
618 unpaddedType = llvm::StructType::get(ctx, elts,
false);
620 }
else if (Entries.size() == 1) {
621 unpaddedType = Entries[0].Type;
624 return { coercionType, unpaddedType };
628 assert(Finished &&
"haven't yet finished lowering");
631 if (Entries.empty())
return false;
634 if (Entries.size() == 1) {
640 componentTys.reserve(Entries.size());
641 for (
auto &entry : Entries) {
642 componentTys.push_back(entry.Type);
649 bool asReturnValue) {
663 size = llvm::bit_ceil(size);
669 llvm::IntegerType *intTy) {
670 auto size = intTy->getBitWidth();
689 llvm::VectorType *vectorTy) {
691 CGM, vectorSize, vectorTy->getElementType(),
692 cast<llvm::FixedVectorType>(vectorTy)->getNumElements());
696 llvm::Type *eltTy,
unsigned numElts) {
697 assert(numElts > 1 &&
"illegal vector length");
701std::pair<llvm::Type*, unsigned>
703 llvm::VectorType *vectorTy) {
704 auto numElts = cast<llvm::FixedVectorType>(vectorTy)->getNumElements();
705 auto eltTy = vectorTy->getElementType();
710 return {llvm::FixedVectorType::get(eltTy, numElts / 2), 2};
713 return {eltTy, numElts};
717 llvm::VectorType *origVectorTy,
721 components.push_back(origVectorTy);
726 auto numElts = cast<llvm::FixedVectorType>(origVectorTy)->getNumElements();
727 auto eltTy = origVectorTy->getElementType();
728 assert(numElts != 1);
732 unsigned logCandidateNumElts = llvm::Log2_32(numElts);
733 unsigned candidateNumElts = 1U << logCandidateNumElts;
734 assert(candidateNumElts <= numElts && candidateNumElts * 2 > numElts);
737 if (candidateNumElts == numElts) {
738 logCandidateNumElts--;
739 candidateNumElts >>= 1;
742 CharUnits eltSize = (origVectorSize / numElts);
743 CharUnits candidateSize = eltSize * candidateNumElts;
748 while (logCandidateNumElts > 0) {
749 assert(candidateNumElts == 1U << logCandidateNumElts);
750 assert(candidateNumElts <= numElts);
751 assert(candidateSize == eltSize * candidateNumElts);
755 logCandidateNumElts--;
756 candidateNumElts /= 2;
762 auto numVecs = numElts >> logCandidateNumElts;
763 components.append(numVecs,
764 llvm::FixedVectorType::get(eltTy, candidateNumElts));
765 numElts -= (numVecs << logCandidateNumElts);
767 if (numElts == 0)
return;
774 components.push_back(llvm::FixedVectorType::get(eltTy, numElts));
780 logCandidateNumElts--;
781 candidateNumElts /= 2;
783 }
while (candidateNumElts > numElts);
787 components.append(numElts, eltTy);
801 if (lowering.
empty()) {
829 if (isa<ComplexType>(
type)) {
834 if (isa<VectorType>(
type)) {
848 if (
type->isVoidType()) {
869 for (
unsigned i = 0, e = FI.
arg_size(); i != e; ++i) {
static ABIArgInfo classifyType(CodeGenModule &CGM, CanQualType type, bool forReturn)
static llvm::Type * getCommonType(llvm::Type *first, llvm::Type *second)
Given two types with the same size, try to find a common type.
static CharUnits getOffsetAtStartOfUnit(CharUnits offset, CharUnits unitSize)
Given a power-of-two unit size, return the offset of the aligned unit of that size which contains the...
static CharUnits getTypeAllocSize(CodeGenModule &CGM, llvm::Type *type)
static bool isPowerOf2(unsigned n)
static ABIArgInfo classifyExpandedType(SwiftAggLowering &lowering, bool forReturn, CharUnits alignmentForIndirect)
static CharUnits getTypeStoreSize(CodeGenModule &CGM, llvm::Type *type)
static bool areBytesInSameUnit(CharUnits first, CharUnits second, CharUnits chunkSize)
static const SwiftABIInfo & getSwiftABIInfo(CodeGenModule &CGM)
static bool isMergeableEntryType(llvm::Type *type)
const ConstantArrayType * getAsConstantArrayType(QualType T) const
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D,...
int64_t toBits(CharUnits CharSize) const
Convert a size in characters to a size in bits.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
const TargetInfo & getTargetInfo() const
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
bool hasOwnVFPtr() const
hasOwnVFPtr - Does this class provide its own virtual-function table pointer, rather than inheriting ...
bool hasOwnVBPtr() const
hasOwnVBPtr - Does this class provide its own virtual-base table pointer, rather than inheriting one ...
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
CharUnits getVBPtrOffset() const
getVBPtrOffset - Get the offset for virtual base table pointer.
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
CharUnits getVBaseClassOffset(const CXXRecordDecl *VBase) const
getVBaseClassOffset - Get the offset, in chars, for the given base class.
CharUnits - This is an opaque type for sizes expressed in character units.
bool isZero() const
isZero - Test whether the quantity equals zero.
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
static CharUnits One()
One - Construct a CharUnits quantity of one.
bool isMultipleOf(CharUnits N) const
Test whether this is a multiple of the other value.
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
ABIArgInfo - Helper class to encapsulate information about how a specific C type should be passed to ...
static ABIArgInfo getIgnore()
static ABIArgInfo getExpand()
static ABIArgInfo getIndirect(CharUnits Alignment, bool ByVal=true, bool Realign=false, llvm::Type *Padding=nullptr)
static ABIArgInfo getDirect(llvm::Type *T=nullptr, unsigned Offset=0, llvm::Type *Padding=nullptr, bool CanBeFlattened=true, unsigned Align=0)
static ABIArgInfo getCoerceAndExpand(llvm::StructType *coerceToType, llvm::Type *unpaddedCoerceToType)
CGFunctionInfo - Class to encapsulate the information about a function definition.
ABIArgInfo & getReturnInfo()
const_arg_iterator arg_begin() const
CanQualType getReturnType() const
unsigned arg_size() const
This class organizes the cross-function state that is used while generating LLVM code.
CodeGenTypes & getTypes()
const llvm::DataLayout & getDataLayout() const
ASTContext & getContext() const
const TargetCodeGenInfo & getTargetCodeGenInfo()
llvm::LLVMContext & getLLVMContext()
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
Target specific hooks for defining how a type should be passed or returned from functions with one of...
virtual bool isLegalVectorType(CharUnits VectorSize, llvm::Type *EltTy, unsigned NumElts) const
Returns true if the given vector type is legal from Swift's calling convention perspective.
bool isSwiftErrorInRegister() const
Returns true if swifterror is lowered to a register by the target ABI.
virtual bool shouldPassIndirectly(ArrayRef< llvm::Type * > ComponentTys, bool AsReturnValue) const
Returns true if an aggregate which expands to the given type sequence should be passed / returned ind...
const SwiftABIInfo & getSwiftABIInfo() const
Returns Swift ABI info helper for the target.
void addOpaqueData(CharUnits begin, CharUnits end)
std::pair< llvm::StructType *, llvm::Type * > getCoerceAndExpandTypes() const
Return the types for a coerce-and-expand operation.
void enumerateComponents(EnumerationCallback callback) const
Enumerate the expanded components of this type.
llvm::function_ref< void(CharUnits offset, CharUnits end, llvm::Type *type)> EnumerationCallback
bool empty() const
Does this lowering require passing any data?
void addTypedData(QualType type, CharUnits begin)
bool shouldPassIndirectly(bool asReturnValue) const
According to the target Swift ABI, should a value with this lowering be passed indirectly?
Complex values, per C99 6.2.5p11.
Represents a member of a struct/union/class.
bool isBitField() const
Determines whether this field is a bitfield.
unsigned getBitWidthValue(const ASTContext &Ctx) const
Computes the bit width of this field, if this is a bit field.
A pointer to member type per C++ 8.3.3 - Pointers to members.
A (possibly-)qualified type.
Represents a struct/union/class.
bool canPassInRegisters() const
Determine whether this class can be passed in registers.
field_range fields() const
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
uint64_t getPointerWidth(LangAS AddrSpace) const
Return the width of pointers on this target, for the specified address space.
virtual bool hasInt128Type() const
Determine whether the __int128 type is supported on this target.
The base class of the type hierarchy.
Defines the clang::TargetInfo interface.
bool isSwiftErrorLoweredInRegister(CodeGenModule &CGM)
Is swifterror lowered to a register by the target ABI?
bool shouldPassIndirectly(CodeGenModule &CGM, ArrayRef< llvm::Type * > types, bool asReturnValue)
Should an aggregate which expands to the given type sequence be passed/returned indirectly under swif...
ABIArgInfo classifyReturnType(CodeGenModule &CGM, CanQualType type)
Classify the rules for how to return a particular type.
bool mustPassRecordIndirectly(CodeGenModule &CGM, const RecordDecl *record)
Is the given record type required to be passed and returned indirectly because of language restrictio...
ABIArgInfo classifyArgumentType(CodeGenModule &CGM, CanQualType type)
Classify the rules for how to pass a particular type.
bool isLegalIntegerType(CodeGenModule &CGM, llvm::IntegerType *type)
Is the given integer type "legal" for Swift's perspective on the current platform?
void legalizeVectorType(CodeGenModule &CGM, CharUnits vectorSize, llvm::VectorType *vectorTy, llvm::SmallVectorImpl< llvm::Type * > &types)
Turn a vector type in a sequence of legal component vector types.
void computeABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI)
Compute the ABI information of a swiftcall function.
bool isLegalVectorType(CodeGenModule &CGM, CharUnits vectorSize, llvm::VectorType *vectorTy)
Is the given vector type "legal" for Swift's perspective on the current platform?
std::pair< llvm::Type *, unsigned > splitLegalVectorType(CodeGenModule &CGM, CharUnits vectorSize, llvm::VectorType *vectorTy)
Minimally split a legal vector type.
CharUnits getNaturalAlignment(CodeGenModule &CGM, llvm::Type *type)
Return the Swift CC's notion of the natural alignment of a type.
CharUnits getMaximumVoluntaryIntegerSize(CodeGenModule &CGM)
Return the maximum voluntary integer size for the current target.
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const AstTypeMatcher< ArrayType > arrayType
Matches all kinds of arrays.
const AstTypeMatcher< AtomicType > atomicType
Matches atomic types.
const AstTypeMatcher< RecordType > recordType
Matches record types (e.g.
const AstTypeMatcher< ComplexType > complexType
Matches C99 complex types.
The JSON file list parser is used to communicate input to InstallAPI.
llvm::PointerType * Int8PtrTy