LLVM 24.0.0git
Types.cpp
Go to the documentation of this file.
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"
13#include <algorithm>
14
15using namespace llvm;
16using namespace llvm::abi;
17
19 switch (getKind()) {
21 if (cast<IntegerType>(this)->isBitInt())
22 return getTypeAllocSize().getFixedValue() * 8;
23 return getTypeStoreSizeInBits().getFixedValue();
24 case TypeKind::Float:
25 return getTypeAllocSize().getFixedValue() * 8;
27 return 2 * cast<ComplexType>(this)->getElementType()->getABISizeInBits();
28 case TypeKind::Vector: {
29 const auto *VT = cast<VectorType>(this);
30 if (VT->isScalable())
31 return 0;
32 const Type *EltTy = VT->getElementType();
33 const auto *IT = dyn_cast<IntegerType>(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(std::max<uint64_t>(Width, 8));
38 }
39 case TypeKind::Void:
45 case TypeKind::Array:
46 case TypeKind::Tuple:
49 }
50 llvm_unreachable("unknown ABI type kind");
51}
52
55 return true;
56 if (getKind() == TypeKind::Vector) {
57 const VectorType *VT = static_cast<const VectorType *>(this);
58 return VT->isSVEType() && VT->isScalable();
59 }
60 if (getKind() == TypeKind::Tuple) {
61 const VectorType *VT =
62 static_cast<const TupleType *>(this)->getVectorType();
63 return VT->isSVEType() && VT->isScalable();
64 }
65 return false;
66}
67
69 const auto *RT = dyn_cast<RecordType>(this);
70 return RT && RT->isEmpty();
71}
72
73bool RecordType::isEmpty() const {
75 return false;
76
77 // We shouldn't need to check for emptiness if the record has virtual bases
78 // because it can't be passed in registers. This assertion is here to enforce
79 // that assumption.
81
83 return false;
84
85 for (const FieldInfo &Base : getBaseClasses()) {
86 if (!Base.FieldType->isEmptyRecord())
87 return false;
88 }
89
90 for (const FieldInfo &FI : getFields())
91 if (!FI.isEmpty())
92 return false;
93
94 return true;
95}
96
97const FieldInfo *
98RecordType::getElementContainingOffset(unsigned OffsetInBits) const {
99 auto Contains = [&](const FieldInfo &Element) {
100 unsigned Start = Element.OffsetInBits;
101 unsigned Size = Element.FieldType->getSizeInBits().getFixedValue();
102 return OffsetInBits >= Start && OffsetInBits < Start + Size;
103 };
104
105 for (const FieldInfo &Base : getBaseClasses())
106 if (!Base.FieldType->isEmptyRecord() && Contains(Base))
107 return &Base;
108
109 for (const FieldInfo &VBase : getVirtualBaseClasses())
110 if (!VBase.FieldType->isEmptyRecord() && Contains(VBase))
111 return &VBase;
112
113 for (const FieldInfo &Field : getFields()) {
114 if (Field.IsUnnamedBitfield)
115 continue;
116 if (Contains(Field))
117 return &Field;
118 }
119
120 return nullptr;
121}
122
123bool FieldInfo::isEmpty() const {
125 return true;
126
127 const Type *Ty = FieldType;
128 bool WasArray = false;
129 while (const auto *AT = dyn_cast<ArrayType>(Ty)) {
130 // Constant arrays of zero length always count as empty.
131 if (AT->getNumElements() == 0)
132 return true;
133 Ty = AT->getElementType();
134 WasArray = true;
135 }
136
137 const auto *RT = dyn_cast<RecordType>(Ty);
138 if (!RT)
139 return false;
140
141 // C++ record fields are never empty unless [[no_unique_address]] applies.
142 // That exception does not apply to arrays of C++ empty records.
143 if (RT->isCXXRecord() && (WasArray || !HasNoUniqueAddress))
144 return false;
145
146 return RT->isEmpty();
147}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
OptimizedStructLayoutField Field
This file implements the C++20 <bit> header.
bool canPassInRegisters() const
Definition Types.h:458
ArrayRef< FieldInfo > getBaseClasses() const
Definition Types.h:491
ArrayRef< FieldInfo > getFields() const
Definition Types.h:490
uint32_t getNumVirtualBaseClasses() const
Definition Types.h:484
bool hasFlexibleArrayMember() const
Definition Types.h:461
ArrayRef< FieldInfo > getVirtualBaseClasses() const
Definition Types.h:492
A homogeneous tuple of 2, 3, or 4 identical vectors, such as the AArch64 SVE types svint32x3_t and sv...
Definition Types.h:376
Represents the ABI-specific view of a type in LLVM.
Definition Types.h:49
TypeSize getTypeAllocSize() const
Definition Types.h:104
LLVM_ABI bool isSVESizelessType() const
Definition Types.cpp:53
LLVM_ABI bool isEmptyRecord() const
True if this type is a record that is empty for ABI purposes.
Definition Types.cpp:68
Type(TypeKind K, TypeSize SizeInBits, Align ABIAlign)
Definition Types.h:69
LLVM_ABI uint64_t getABISizeInBits() const
Returns the size in bits that the source language gives this type, including any padding.
Definition Types.cpp:18
uint64_t getFixedSizeInBitsOrZero() const
Returns the size in bits if it is fixed, otherwise 0.
Definition Types.h:80
TypeKind getKind() const
Definition Types.h:76
bool isSVEType() const
Returns true for any of the AArch64 SVE flavors.
Definition Types.h:363
bool isScalable() const
Definition Types.h:351
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:201
This file defines the type system for the LLVMABI library, which mirrors ABI-relevant aspects of fron...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
T bit_ceil(T Value)
Returns the smallest integral power of two no smaller than Value if Value is nonzero.
Definition bit.h:362
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
bool HasNoUniqueAddress
Definition Types.h:399
const Type * FieldType
Definition Types.h:394
LLVM_ABI bool isEmpty() const
Definition Types.cpp:123