18 using namespace clang;
19 using namespace CodeGen;
20 using namespace swiftcall;
32 assert(first != second);
35 if (first->isIntegerTy()) {
36 if (second->isPointerTy())
return first;
37 }
else if (first->isPointerTy()) {
38 if (second->isIntegerTy())
return second;
39 if (second->isPointerTy())
return first;
43 }
else if (
auto firstVecTy = dyn_cast<llvm::VectorType>(first)) {
44 if (
auto secondVecTy = dyn_cast<llvm::VectorType>(second)) {
45 if (
auto commonTy =
getCommonType(firstVecTy->getElementType(),
46 secondVecTy->getElementType())) {
47 return (commonTy == firstVecTy->getElementType() ? first : second);
79 for (uint64_t i = 0, e =
arrayType->getSize().getZExtValue(); i != e; ++i) {
89 addTypedData(eltLLVMType, begin + eltSize, begin + 2 * eltSize);
113 for (
auto field : record->
fields()) {
114 if (field->isBitField()) {
115 addBitFieldData(field, begin, 0);
136 for (
auto &baseSpecifier : cxxRecord->bases()) {
137 if (baseSpecifier.isVirtual())
continue;
139 auto baseRecord = baseSpecifier.getType()->getAsCXXRecordDecl();
150 for (
auto field : record->
fields()) {
151 auto fieldOffsetInBits = layout.
getFieldOffset(field->getFieldIndex());
152 if (field->isBitField()) {
153 addBitFieldData(field, begin, fieldOffsetInBits);
163 for (
auto &vbaseSpecifier : cxxRecord->vbases()) {
164 auto baseRecord = vbaseSpecifier.getType()->getAsCXXRecordDecl();
170 void SwiftAggLowering::addBitFieldData(
const FieldDecl *bitfield,
172 uint64_t bitfieldBitBegin) {
178 if (width == 0)
return;
181 CharUnits bitfieldByteBegin = ctx.toCharUnitsFromBits(bitfieldBitBegin);
186 uint64_t bitfieldBitLast = bitfieldBitBegin + width - 1;
190 recordBegin + bitfieldByteEnd);
194 assert(type &&
"didn't provide type for typed data");
200 assert(type &&
"didn't provide type for typed data");
204 if (
auto vecTy = dyn_cast<llvm::VectorType>(type)) {
207 assert(componentTys.size() >= 1);
210 for (
size_t i = 0, e = componentTys.size(); i != e - 1; ++i) {
213 assert(componentSize < end - begin);
214 addLegalTypedData(componentTy, begin, begin + componentSize);
215 begin += componentSize;
218 return addLegalTypedData(componentTys.back(), begin, end);
222 if (
auto intTy = dyn_cast<llvm::IntegerType>(type)) {
228 return addLegalTypedData(type, begin, end);
237 if (
auto vecTy = dyn_cast<llvm::VectorType>(
type)) {
239 auto eltTy = split.first;
240 auto numElts = split.second;
242 auto eltSize = (end - begin) / numElts;
244 for (
size_t i = 0, e = numElts; i != e; ++i) {
245 addLegalTypedData(eltTy, begin, begin + eltSize);
248 assert(begin == end);
255 addEntry(
type, begin, end);
261 (!isa<llvm::StructType>(
type) && !isa<llvm::ArrayType>(
type))) &&
262 "cannot add aggregate-typed data");
266 if (Entries.empty() || Entries.back().End <= begin) {
267 Entries.push_back({begin, end,
type});
273 size_t index = Entries.size() - 1;
275 if (Entries[index - 1].
End <= begin)
break;
281 if (Entries[index].
Begin >= end) {
285 Entries.insert(Entries.begin() + index, {begin, end,
type});
294 if (Entries[index].
Begin == begin && Entries[index].
End == end) {
296 if (Entries[index].
Type ==
type)
return;
299 if (Entries[index].
Type ==
nullptr) {
301 }
else if (
type ==
nullptr) {
302 Entries[index].Type =
nullptr;
309 Entries[index].Type = entryType;
314 Entries[index].Type =
nullptr;
321 if (
auto vecTy = dyn_cast_or_null<llvm::VectorType>(
type)) {
322 auto eltTy = vecTy->getElementType();
323 CharUnits eltSize = (end - begin) / vecTy->getNumElements();
325 for (
unsigned i = 0, e = vecTy->getNumElements(); i != e; ++i) {
326 addEntry(eltTy, begin, begin + eltSize);
329 assert(begin == end);
334 if (Entries[index].
Type && Entries[index].
Type->isVectorTy()) {
335 splitVectorEntry(index);
336 goto restartAfterSplit;
341 Entries[index].Type =
nullptr;
344 if (begin < Entries[index].
Begin) {
345 Entries[index].Begin = begin;
346 assert(index == 0 || begin >= Entries[index - 1].
End);
351 while (end > Entries[index].
End) {
352 assert(Entries[index].
Type ==
nullptr);
355 if (index == Entries.size() - 1 || end <= Entries[index + 1].Begin) {
356 Entries[index].End = end;
361 Entries[index].End = Entries[index + 1].Begin;
367 if (Entries[index].
Type ==
nullptr)
371 if (Entries[index].
Type->isVectorTy() &&
372 end < Entries[index].End) {
373 splitVectorEntry(index);
377 Entries[index].Type =
nullptr;
383 void SwiftAggLowering::splitVectorEntry(
unsigned index) {
384 auto vecTy = cast<llvm::VectorType>(Entries[index].Type);
387 auto eltTy = split.first;
389 auto numElts = split.second;
390 Entries.insert(Entries.begin() + index + 1, numElts - 1, StorageEntry());
393 for (
unsigned i = 0; i != numElts; ++i) {
394 Entries[index].Type = eltTy;
395 Entries[index].Begin = begin;
396 Entries[index].End = begin + eltSize;
419 if (type ==
nullptr)
return true;
434 return (!type->isFloatingPointTy() && !type->isVectorTy());
437 bool SwiftAggLowering::shouldMergeEntries(
const StorageEntry &first,
438 const StorageEntry &second,
452 if (Entries.empty()) {
464 bool hasOpaqueEntries = (Entries[0].Type ==
nullptr);
465 for (
size_t i = 1, e = Entries.size(); i != e; ++i) {
466 if (shouldMergeEntries(Entries[i - 1], Entries[i], chunkSize)) {
467 Entries[i - 1].Type =
nullptr;
468 Entries[i].Type =
nullptr;
469 Entries[i - 1].End = Entries[i].Begin;
470 hasOpaqueEntries =
true;
472 }
else if (Entries[i].
Type ==
nullptr) {
473 hasOpaqueEntries =
true;
479 if (!hasOpaqueEntries) {
485 auto orig = std::move(Entries);
486 assert(Entries.empty());
488 for (
size_t i = 0, e = orig.size(); i != e; ++i) {
490 if (orig[i].
Type !=
nullptr) {
491 Entries.push_back(orig[i]);
498 auto begin = orig[i].Begin;
499 auto end = orig[i].End;
501 orig[i + 1].
Type ==
nullptr &&
502 end == orig[i + 1].
Begin) {
503 end = orig[i + 1].End;
514 CharUnits chunkEnd = chunkBegin + chunkSize;
520 for (; ; unitSize *= 2) {
521 assert(unitSize <= chunkSize);
523 unitEnd = unitBegin + unitSize;
524 if (unitEnd >= localEnd)
break;
531 Entries.push_back({unitBegin, unitEnd, entryTy});
535 }
while (begin != end);
543 assert(Finished &&
"haven't yet finished lowering");
545 for (
auto &entry : Entries) {
546 callback(entry.Begin, entry.End, entry.Type);
550 std::pair<llvm::StructType*, llvm::Type*>
552 assert(Finished &&
"haven't yet finished lowering");
556 if (Entries.empty()) {
557 auto type = llvm::StructType::get(ctx);
563 bool hasPadding =
false;
565 for (
auto &entry : Entries) {
566 if (entry.Begin != lastEnd) {
567 auto paddingSize = entry.Begin - lastEnd;
568 assert(!paddingSize.isNegative());
570 auto padding = llvm::ArrayType::get(llvm::Type::getInt8Ty(ctx),
571 paddingSize.getQuantity());
572 elts.push_back(padding);
576 if (!packed && !entry.Begin.isMultipleOf(
581 elts.push_back(entry.Type);
584 assert(entry.End <= lastEnd);
589 auto coercionType = llvm::StructType::get(ctx, elts, packed);
594 for (
auto &entry : Entries) {
595 elts.push_back(entry.Type);
597 if (elts.size() == 1) {
598 unpaddedType = elts[0];
600 unpaddedType = llvm::StructType::get(ctx, elts,
false);
602 }
else if (Entries.size() == 1) {
603 unpaddedType = Entries[0].Type;
606 return { coercionType, unpaddedType };
610 assert(Finished &&
"haven't yet finished lowering");
613 if (Entries.empty())
return false;
616 if (Entries.size() == 1) {
623 componentTys.reserve(Entries.size());
624 for (
auto &entry : Entries) {
625 componentTys.push_back(entry.Type);
633 bool asReturnValue) {
649 size = 1ULL << (llvm::findLastSet(size, llvm::ZB_Undefined) + 1);
651 assert(size >= CGM.
getDataLayout().getABITypeAlignment(type));
656 llvm::IntegerType *intTy) {
657 auto size = intTy->getBitWidth();
676 llvm::VectorType *vectorTy) {
678 vectorTy->getNumElements());
683 assert(numElts > 1 &&
"illegal vector length");
688 std::pair<llvm::Type*, unsigned>
690 llvm::VectorType *vectorTy) {
691 auto numElts = vectorTy->getNumElements();
692 auto eltTy = vectorTy->getElementType();
697 return {llvm::VectorType::get(eltTy, numElts / 2), 2};
700 return {eltTy, numElts};
704 llvm::VectorType *origVectorTy,
708 components.push_back(origVectorTy);
713 auto numElts = origVectorTy->getNumElements();
714 auto eltTy = origVectorTy->getElementType();
715 assert(numElts != 1);
719 unsigned logCandidateNumElts = llvm::findLastSet(numElts, llvm::ZB_Undefined);
720 unsigned candidateNumElts = 1U << logCandidateNumElts;
721 assert(candidateNumElts <= numElts && candidateNumElts * 2 > numElts);
724 if (candidateNumElts == numElts) {
725 logCandidateNumElts--;
726 candidateNumElts >>= 1;
729 CharUnits eltSize = (origVectorSize / numElts);
730 CharUnits candidateSize = eltSize * candidateNumElts;
735 while (logCandidateNumElts > 0) {
736 assert(candidateNumElts == 1U << logCandidateNumElts);
737 assert(candidateNumElts <= numElts);
738 assert(candidateSize == eltSize * candidateNumElts);
742 logCandidateNumElts--;
743 candidateNumElts /= 2;
749 auto numVecs = numElts >> logCandidateNumElts;
750 components.append(numVecs, llvm::VectorType::get(eltTy, candidateNumElts));
751 numElts -= (numVecs << logCandidateNumElts);
753 if (numElts == 0)
return;
760 components.push_back(llvm::VectorType::get(eltTy, numElts));
766 logCandidateNumElts--;
767 candidateNumElts /= 2;
769 }
while (candidateNumElts > numElts);
773 components.append(numElts, eltTy);
787 if (lowering.
empty()) {
799 if (
auto recordType = dyn_cast<RecordType>(type)) {
815 if (isa<ComplexType>(type)) {
820 if (isa<VectorType>(type)) {
834 if (type->isVoidType()) {
855 for (
unsigned i = 0, e = FI.
arg_size(); i != e; ++i) {
const llvm::DataLayout & getDataLayout() const
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
A (possibly-)qualified type.
CodeGenTypes & getTypes()
llvm::LLVMContext & getLLVMContext()
static ABIArgInfo classifyType(CodeGenModule &CGM, CanQualType type, bool forReturn)
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D...
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
The base class of the type hierarchy.
const ABIInfo & getABIInfo() const
getABIInfo() - Returns ABI info helper for the target.
bool isZero() const
isZero - Test whether the quantity equals zero.
const TargetInfo & getTargetInfo() const
const T * getAs() const
Member-template getAs<specific type>'.
bool hasOwnVFPtr() const
hasOwnVFPtr - Does this class provide its own virtual-function table pointer, rather than inheriting ...
std::pair< llvm::Type *, unsigned > splitLegalVectorType(CodeGenModule &CGM, CharUnits vectorSize, llvm::VectorType *vectorTy)
Minimally split a legal vector type.
static ABIArgInfo getIgnore()
unsigned arg_size() const
const AstTypeMatcher< RecordType > recordType
Matches record types (e.g.
Represents a struct/union/class.
uint64_t getPointerWidth(unsigned AddrSpace) const
Return the width of pointers on this target, for the specified address space.
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
ABIArgInfo classifyArgumentType(CodeGenModule &CGM, CanQualType type)
Classify the rules for how to pass a particular type.
static CharUnits getTypeAllocSize(CodeGenModule &CGM, llvm::Type *type)
field_range fields() const
Represents a member of a struct/union/class.
static bool isPowerOf2(unsigned n)
static bool isMergeableEntryType(llvm::Type *type)
ABIArgInfo classifyReturnType(CodeGenModule &CGM, CanQualType type)
Classify the rules for how to return a particular type.
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
CharUnits getVBaseClassOffset(const CXXRecordDecl *VBase) const
getVBaseClassOffset - Get the offset, in chars, for the given base class.
const AstTypeMatcher< ComplexType > complexType
Matches C99 complex types.
static ABIArgInfo getDirect(llvm::Type *T=nullptr, unsigned Offset=0, llvm::Type *Padding=nullptr, bool CanBeFlattened=true)
bool isBitField() const
Determines whether this field is a bitfield.
CharUnits - This is an opaque type for sizes expressed in character units.
const_arg_iterator arg_begin() const
virtual bool shouldPassIndirectlyForSwift(ArrayRef< llvm::Type *> types, bool asReturnValue) const =0
llvm::function_ref< void(CharUnits offset, CharUnits end, llvm::Type *type)> EnumerationCallback
ABIArgInfo - Helper class to encapsulate information about how a specific C type should be passed to ...
virtual bool isSwiftErrorInRegister() const =0
static ABIArgInfo classifyExpandedType(SwiftAggLowering &lowering, bool forReturn, CharUnits alignmentForIndirect)
unsigned getBitWidthValue(const ASTContext &Ctx) const
static ABIArgInfo getExpand()
static bool areBytesInSameUnit(CharUnits first, CharUnits second, CharUnits chunkSize)
CanQualType getReturnType() const
static CharUnits One()
One - Construct a CharUnits quantity of one.
static llvm::Type * getCommonType(llvm::Type *first, llvm::Type *second)
Given two types with the same size, try to find a common type.
const TargetCodeGenInfo & getTargetCodeGenInfo()
CharUnits getNaturalAlignment(CodeGenModule &CGM, llvm::Type *type)
Return the Swift CC's notion of the natural alignment of a type.
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
void addOpaqueData(CharUnits begin, CharUnits end)
std::pair< llvm::StructType *, llvm::Type * > getCoerceAndExpandTypes() const
Return the types for a coerce-and-expand operation.
bool isSwiftErrorLoweredInRegister(CodeGenModule &CGM)
Is swifterror lowered to a register by the target ABI?
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
const AstTypeMatcher< ArrayType > arrayType
Matches all kinds of arrays.
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...
CharUnits getVBPtrOffset() const
getVBPtrOffset - Get the offset for virtual base table pointer.
ASTContext & getContext() const
The l-value was considered opaque, so the alignment was determined from a type.
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
bool empty() const
Does this lowering require passing any data?
void addTypedData(QualType type, CharUnits begin)
static CharUnits getTypeStoreSize(CodeGenModule &CGM, llvm::Type *type)
void computeABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI)
Compute the ABI information of a swiftcall function.
const ConstantArrayType * getAsConstantArrayType(QualType T) const
static const SwiftABIInfo & getSwiftABIInfo(CodeGenModule &CGM)
bool canPassInRegisters() const
Determine whether this class can be passed in registers.
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...
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.
CGFunctionInfo - Class to encapsulate the information about a function definition.
This class organizes the cross-function state that is used while generating LLVM code.
Dataflow Directional Tag Classes.
void enumerateComponents(EnumerationCallback callback) const
Enumerate the expanded components of this type.
bool isLegalVectorType(CodeGenModule &CGM, CharUnits vectorSize, llvm::VectorType *vectorTy)
Is the given vector type "legal" for Swift's perspective on the current platform? ...
A refining implementation of ABIInfo for targets that support swiftcall.
A pointer to member type per C++ 8.3.3 - Pointers to members.
virtual bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, unsigned elts) const
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Complex values, per C99 6.2.5p11.
bool mustPassRecordIndirectly(CodeGenModule &CGM, const RecordDecl *record)
Is the given record type required to be passed and returned indirectly because of language restrictio...
llvm::PointerType * Int8PtrTy
virtual bool hasInt128Type() const
Determine whether the __int128 type is supported on this target.
ABIArgInfo & getReturnInfo()
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
bool isMultipleOf(CharUnits N) const
Test whether this is a multiple of the other value.
int64_t toBits(CharUnits CharSize) const
Convert a size in characters to a size in bits.
static ABIArgInfo getCoerceAndExpand(llvm::StructType *coerceToType, llvm::Type *unpaddedCoerceToType)
Represents a C++ struct/union/class.
bool isLegalIntegerType(CodeGenModule &CGM, llvm::IntegerType *type)
Is the given integer type "legal" for Swift's perspective on the current platform?
__DEVICE__ int min(int __a, int __b)
Defines the clang::TargetInfo interface.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
CharUnits getMaximumVoluntaryIntegerSize(CodeGenModule &CGM)
Return the maximum voluntary integer size for the current target.
bool hasOwnVBPtr() const
hasOwnVBPtr - Does this class provide its own virtual-base table pointer, rather than inheriting one ...
static ABIArgInfo getIndirect(CharUnits Alignment, bool ByVal=true, bool Realign=false, llvm::Type *Padding=nullptr)
bool shouldPassIndirectly(bool asReturnValue) const
According to the target Swift ABI, should a value with this lowering be passed indirectly?