1//===----------------------------------------------------------------------===//
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#include "llvm/ABI/Types.h"
10#include "llvm/ADT/bit.h"
11#include "llvm/Support/Casting.h"
12#include "llvm/Support/ErrorHandling.h"
13#include <algorithm>
14
15using namespace llvm;
16using namespace llvm::abi;
17
18uint64_t llvm::abi::Type::getABISizeInBits() const {
19 switch (getKind()) {
20 case TypeKind::Integer:
21 if (cast<IntegerType>(Val: this)->isBitInt())
22 return getTypeAllocSize().getFixedValue() * 8;
23 return getTypeStoreSizeInBits().getFixedValue();
24 case TypeKind::Float:
25 return getTypeAllocSize().getFixedValue() * 8;
26 case TypeKind::Complex:
27 return 2 * cast<ComplexType>(Val: this)->getElementType()->getABISizeInBits();
28 case TypeKind::Vector: {
29 const auto *VT = cast<VectorType>(Val: this);
30 if (VT->isScalable())
31 return 0;
32 const Type *EltTy = VT->getElementType();
33 const auto *IT = dyn_cast<IntegerType>(Val: EltTy);
34 uint64_t EltWidth = IT && !IT->isBitInt() ? IT->getFixedSizeInBitsOrZero()
35 : EltTy->getABISizeInBits();
36 uint64_t Width = EltWidth * VT->getNumElements().getFixedValue();
37 return bit_ceil(Value: std::max<uint64_t>(a: Width, b: 8));
38 }
39 case TypeKind::Void:
40 case TypeKind::OpaqueFloat8:
41 case TypeKind::Atomic:
42 case TypeKind::MemberPointer:
43 case TypeKind::Pointer:
44 case TypeKind::Array:
45 case TypeKind::Tuple:
46 case TypeKind::Record:
47 return getFixedSizeInBitsOrZero();
48 }
49 llvm_unreachable("unknown ABI type kind");
50}
51
52bool llvm::abi::Type::isSVESizelessType() const {
53 if (getKind() == TypeKind::Vector) {
54 const VectorType *VT = static_cast<const VectorType *>(this);
55 return VT->isSVEType() && VT->isScalable();
56 }
57 if (getKind() == TypeKind::Tuple) {
58 const VectorType *VT =
59 static_cast<const TupleType *>(this)->getVectorType();
60 return VT->isSVEType() && VT->isScalable();
61 }
62 return false;
63}
64
65bool llvm::abi::Type::isEmptyRecord() const {
66 const auto *RT = dyn_cast<RecordType>(Val: this);
67 return RT && RT->isEmpty();
68}
69
70bool RecordType::isEmpty() const {
71 if (hasFlexibleArrayMember())
72 return false;
73
74 // We shouldn't need to check for emptiness if the record has virtual bases
75 // because it can't be passed in registers. This assertion is here to enforce
76 // that assumption.
77 assert(getNumVirtualBaseClasses() == 0 || !canPassInRegisters());
78
79 if (getNumVirtualBaseClasses() > 0)
80 return false;
81
82 for (const FieldInfo &Base : getBaseClasses()) {
83 if (!Base.FieldType->isEmptyRecord())
84 return false;
85 }
86
87 for (const FieldInfo &FI : getFields())
88 if (!FI.isEmpty())
89 return false;
90
91 return true;
92}
93
94const FieldInfo *
95RecordType::getElementContainingOffset(unsigned OffsetInBits) const {
96 auto Contains = [&](const FieldInfo &Element) {
97 unsigned Start = Element.OffsetInBits;
98 unsigned Size = Element.FieldType->getSizeInBits().getFixedValue();
99 return OffsetInBits >= Start && OffsetInBits < Start + Size;
100 };
101
102 for (const FieldInfo &Base : getBaseClasses())
103 if (!Base.FieldType->isEmptyRecord() && Contains(Base))
104 return &Base;
105
106 for (const FieldInfo &VBase : getVirtualBaseClasses())
107 if (!VBase.FieldType->isEmptyRecord() && Contains(VBase))
108 return &VBase;
109
110 for (const FieldInfo &Field : getFields()) {
111 if (Field.IsUnnamedBitfield)
112 continue;
113 if (Contains(Field))
114 return &Field;
115 }
116
117 return nullptr;
118}
119
120bool FieldInfo::isEmpty() const {
121 if (IsUnnamedBitfield)
122 return true;
123
124 const Type *Ty = FieldType;
125 bool WasArray = false;
126 while (const auto *AT = dyn_cast<ArrayType>(Val: Ty)) {
127 // Constant arrays of zero length always count as empty.
128 if (AT->getNumElements() == 0)
129 return true;
130 Ty = AT->getElementType();
131 WasArray = true;
132 }
133
134 const auto *RT = dyn_cast<RecordType>(Val: Ty);
135 if (!RT)
136 return false;
137
138 // C++ record fields are never empty unless [[no_unique_address]] applies.
139 // That exception does not apply to arrays of C++ empty records.
140 if (RT->isCXXRecord() && (WasArray || !HasNoUniqueAddress))
141 return false;
142
143 return RT->isEmpty();
144}
145