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:
43 case TypeKind::Array:
44 case TypeKind::Tuple:
47 }
48 llvm_unreachable("unknown ABI type kind");
49}
50
52 if (getKind() == TypeKind::Vector) {
53 const VectorType *VT = static_cast<const VectorType *>(this);
54 return VT->isSVEType() && VT->isScalable();
55 }
56 if (getKind() == TypeKind::Tuple) {
57 const VectorType *VT =
58 static_cast<const TupleType *>(this)->getVectorType();
59 return VT->isSVEType() && VT->isScalable();
60 }
61 return false;
62}
63
65 const auto *RT = dyn_cast<RecordType>(this);
66 return RT && RT->isEmpty();
67}
68
69bool RecordType::isEmpty() const {
71 return false;
72
73 // We shouldn't need to check for emptiness if the record has virtual bases
74 // because it can't be passed in registers. This assertion is here to enforce
75 // that assumption.
77
79 return false;
80
81 for (const FieldInfo &Base : getBaseClasses()) {
82 if (!Base.FieldType->isEmptyRecord())
83 return false;
84 }
85
86 for (const FieldInfo &FI : getFields())
87 if (!FI.isEmpty())
88 return false;
89
90 return true;
91}
92
93const FieldInfo *
94RecordType::getElementContainingOffset(unsigned OffsetInBits) const {
95 auto Contains = [&](const FieldInfo &Element) {
96 unsigned Start = Element.OffsetInBits;
97 unsigned Size = Element.FieldType->getSizeInBits().getFixedValue();
98 return OffsetInBits >= Start && OffsetInBits < Start + Size;
99 };
100
101 for (const FieldInfo &Base : getBaseClasses())
102 if (!Base.FieldType->isEmptyRecord() && Contains(Base))
103 return &Base;
104
105 for (const FieldInfo &VBase : getVirtualBaseClasses())
106 if (!VBase.FieldType->isEmptyRecord() && Contains(VBase))
107 return &VBase;
108
109 for (const FieldInfo &Field : getFields()) {
110 if (Field.IsUnnamedBitfield)
111 continue;
112 if (Contains(Field))
113 return &Field;
114 }
115
116 return nullptr;
117}
118
119bool FieldInfo::isEmpty() const {
121 return true;
122
123 const Type *Ty = FieldType;
124 bool WasArray = false;
125 while (const auto *AT = dyn_cast<ArrayType>(Ty)) {
126 // Constant arrays of zero length always count as empty.
127 if (AT->getNumElements() == 0)
128 return true;
129 Ty = AT->getElementType();
130 WasArray = true;
131 }
132
133 const auto *RT = dyn_cast<RecordType>(Ty);
134 if (!RT)
135 return false;
136
137 // C++ record fields are never empty unless [[no_unique_address]] applies.
138 // That exception does not apply to arrays of C++ empty records.
139 if (RT->isCXXRecord() && (WasArray || !HasNoUniqueAddress))
140 return false;
141
142 return RT->isEmpty();
143}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::values(clEnumValN(DefaultIT, "arm-default-it", "Generate any type of IT block"), clEnumValN(RestrictedIT, "arm-restrict-it", "Disallow complex IT blocks")))
OptimizedStructLayoutField Field
static bool Contains(directive::VersionRange V, int P)
Definition Spelling.cpp:18
This file implements the C++20 <bit> header.
bool canPassInRegisters() const
Definition Types.h:419
ArrayRef< FieldInfo > getBaseClasses() const
Definition Types.h:434
ArrayRef< FieldInfo > getFields() const
Definition Types.h:433
uint32_t getNumVirtualBaseClasses() const
Definition Types.h:427
bool hasFlexibleArrayMember() const
Definition Types.h:422
ArrayRef< FieldInfo > getVirtualBaseClasses() const
Definition Types.h:435
A homogeneous tuple of 2, 3, or 4 identical vectors, such as the AArch64 SVE types svint32x3_t and sv...
Definition Types.h:342
Represents the ABI-specific view of a type in LLVM.
Definition Types.h:47
TypeSize getTypeAllocSize() const
Definition Types.h:102
LLVM_ABI bool isSVESizelessType() const
Definition Types.cpp:51
LLVM_ABI bool isEmptyRecord() const
True if this type is a record that is empty for ABI purposes.
Definition Types.cpp:64
Type(TypeKind K, TypeSize SizeInBits, Align ABIAlign)
Definition Types.h:67
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:78
TypeKind getKind() const
Definition Types.h:74
bool isSVEType() const
Returns true for any of the AArch64 SVE flavors.
Definition Types.h:329
bool isScalable() const
Definition Types.h:316
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
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:365
const Type * FieldType
Definition Types.h:360
LLVM_ABI bool isEmpty() const
Definition Types.cpp:119