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"
11
12using namespace llvm;
13using namespace llvm::abi;
14
15bool RecordType::isEmpty() const {
17 return false;
18
19 // We shouldn't need to check for emptiness if the record has virtual bases
20 // because it can't be passed in registers. This assertion is here to enforce
21 // that assumption.
23
25 return false;
26
27 for (const FieldInfo &Base : getBaseClasses()) {
28 const auto *BaseRT = dyn_cast<RecordType>(Base.FieldType);
29 if (!BaseRT || !BaseRT->isEmpty())
30 return false;
31 }
32
33 for (const FieldInfo &FI : getFields())
34 if (!FI.isEmpty())
35 return false;
36
37 return true;
38}
39
40const FieldInfo *
41RecordType::getElementContainingOffset(unsigned OffsetInBits) const {
42 auto Contains = [&](const FieldInfo &Element) {
43 unsigned Start = Element.OffsetInBits;
44 unsigned Size = Element.FieldType->getSizeInBits().getFixedValue();
45 return OffsetInBits >= Start && OffsetInBits < Start + Size;
46 };
47
48 for (const FieldInfo &Base : getBaseClasses()) {
49 const auto *BaseRT = dyn_cast<RecordType>(Base.FieldType);
50 if ((!BaseRT || !BaseRT->isEmpty()) && Contains(Base))
51 return &Base;
52 }
53
54 for (const FieldInfo &VBase : getVirtualBaseClasses()) {
55 const auto *VBaseRT = dyn_cast<RecordType>(VBase.FieldType);
56 if ((!VBaseRT || !VBaseRT->isEmpty()) && Contains(VBase))
57 return &VBase;
58 }
59
60 for (const FieldInfo &Field : getFields()) {
61 if (Field.IsUnnamedBitfield)
62 continue;
63 if (Contains(Field))
64 return &Field;
65 }
66
67 return nullptr;
68}
69
70bool FieldInfo::isEmpty() const {
72 return true;
73
74 const Type *Ty = FieldType;
75 bool WasArray = false;
76 while (const auto *AT = dyn_cast<ArrayType>(Ty)) {
77 // Constant arrays of zero length always count as empty.
78 if (AT->getNumElements() == 0)
79 return true;
80 Ty = AT->getElementType();
81 WasArray = true;
82 }
83
84 const auto *RT = dyn_cast<RecordType>(Ty);
85 if (!RT)
86 return false;
87
88 // C++ record fields are never empty unless [[no_unique_address]] applies.
89 // That exception does not apply to arrays of C++ empty records.
90 if (RT->isCXXRecord() && (WasArray || !HasNoUniqueAddress))
91 return false;
92
93 return RT->isEmpty();
94}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
OptimizedStructLayoutField Field
static bool Contains(directive::VersionRange V, int P)
Definition Spelling.cpp:18
bool canPassInRegisters() const
Definition Types.h:294
ArrayRef< FieldInfo > getBaseClasses() const
Definition Types.h:309
ArrayRef< FieldInfo > getFields() const
Definition Types.h:308
uint32_t getNumVirtualBaseClasses() const
Definition Types.h:302
bool hasFlexibleArrayMember() const
Definition Types.h:297
ArrayRef< FieldInfo > getVirtualBaseClasses() const
Definition Types.h:310
Represents the ABI-specific view of a type in LLVM.
Definition Types.h:44
This file defines the type system for the LLVMABI library, which mirrors ABI-relevant aspects of fron...
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
bool HasNoUniqueAddress
Definition Types.h:240
const Type * FieldType
Definition Types.h:235
LLVM_ABI bool isEmpty() const
Definition Types.cpp:70