clang 22.0.0git
ABIInfo.cpp
Go to the documentation of this file.
1//===- ABIInfo.cpp --------------------------------------------------------===//
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 "ABIInfo.h"
10#include "ABIInfoImpl.h"
11
12using namespace clang;
13using namespace clang::CodeGen;
14
15// Pin the vtable to this file.
16ABIInfo::~ABIInfo() = default;
17
19
21
22llvm::LLVMContext &ABIInfo::getVMContext() const {
23 return CGT.getLLVMContext();
24}
25
26const llvm::DataLayout &ABIInfo::getDataLayout() const {
27 return CGT.getDataLayout();
28}
29
30const TargetInfo &ABIInfo::getTarget() const { return CGT.getTarget(); }
31
33 return CGT.getCodeGenOpts();
34}
35
36bool ABIInfo::isAndroid() const { return getTarget().getTriple().isAndroid(); }
37
39 return getTarget().getTriple().isOHOSFamily();
40}
41
43 QualType Ty, AggValueSlot Slot) const {
44 return RValue::getIgnored();
45}
46
48 return false;
49}
50
52 uint64_t Members) const {
53 return false;
54}
55
57 // For compatibility with GCC, ignore empty bitfields in C++ mode.
58 return getContext().getLangOpts().CPlusPlus;
59}
60
62 uint64_t &Members) const {
63 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
64 uint64_t NElements = AT->getZExtSize();
65 if (NElements == 0)
66 return false;
67 if (!isHomogeneousAggregate(AT->getElementType(), Base, Members))
68 return false;
69 Members *= NElements;
70 } else if (const auto *RD = Ty->getAsRecordDecl()) {
71 if (RD->hasFlexibleArrayMember())
72 return false;
73
74 Members = 0;
75
76 // If this is a C++ record, check the properties of the record such as
77 // bases and ABI specific restrictions
78 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
79 if (!getCXXABI().isPermittedToBeHomogeneousAggregate(CXXRD))
80 return false;
81
82 for (const auto &I : CXXRD->bases()) {
83 // Ignore empty records.
84 if (isEmptyRecord(getContext(), I.getType(), true))
85 continue;
86
87 uint64_t FldMembers;
88 if (!isHomogeneousAggregate(I.getType(), Base, FldMembers))
89 return false;
90
91 Members += FldMembers;
92 }
93 }
94
95 for (const auto *FD : RD->fields()) {
96 // Ignore (non-zero arrays of) empty records.
97 QualType FT = FD->getType();
98 while (const ConstantArrayType *AT =
99 getContext().getAsConstantArrayType(FT)) {
100 if (AT->isZeroSize())
101 return false;
102 FT = AT->getElementType();
103 }
104 if (isEmptyRecord(getContext(), FT, true))
105 continue;
106
108 FD->isZeroLengthBitField())
109 continue;
110
111 uint64_t FldMembers;
112 if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers))
113 return false;
114
115 Members = (RD->isUnion() ?
116 std::max(Members, FldMembers) : Members + FldMembers);
117 }
118
119 if (!Base)
120 return false;
121
122 // Ensure there is no padding.
123 if (getContext().getTypeSize(Base) * Members !=
125 return false;
126 } else {
127 Members = 1;
128 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
129 Members = 2;
130 Ty = CT->getElementType();
131 }
132
133 // Most ABIs only support float, double, and some vector type widths.
135 return false;
136
137 // The base type must be the same for all members. Types that
138 // agree in both total size and mode (float vs. vector) are
139 // treated as being equivalent here.
140 const Type *TyPtr = Ty.getTypePtr();
141 if (!Base) {
142 Base = TyPtr;
143 // If it's a non-power-of-2 vector, its size is already a power-of-2,
144 // so make sure to widen it explicitly.
145 if (const VectorType *VT = Base->getAs<VectorType>()) {
146 QualType EltTy = VT->getElementType();
147 unsigned NumElements =
149 Base = getContext()
150 .getVectorType(EltTy, NumElements, VT->getVectorKind())
151 .getTypePtr();
152 }
153 }
154
155 if (Base->isVectorType() != TyPtr->isVectorType() ||
156 getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr))
157 return false;
158 }
159 return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members);
160}
161
163 if (getContext().isPromotableIntegerType(Ty))
164 return true;
165
166 if (const auto *EIT = Ty->getAs<BitIntType>())
167 if (EIT->getNumBits() < getContext().getTypeSize(getContext().IntTy))
168 return true;
169
170 return false;
171}
172
174 bool ByVal, bool Realign,
175 llvm::Type *Padding) const {
176 return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty),
177 AddrSpace, ByVal, Realign, Padding);
178}
179
181 bool Realign) const {
182 return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty),
183 /*ByVal*/ false, Realign);
184}
185
187 raw_ostream &Out) const {
188 if (Attr->isDefaultVersion())
189 return;
190 appendAttributeMangling(Attr->getFeaturesStr(), Out);
191}
192
194 raw_ostream &Out) const {
195 appendAttributeMangling(Attr->getNamesStr(), Out);
196}
197
198void ABIInfo::appendAttributeMangling(TargetClonesAttr *Attr, unsigned Index,
199 raw_ostream &Out) const {
200 appendAttributeMangling(Attr->getFeatureStr(Index), Out);
201 Out << '.' << Attr->getMangledIndex(Index);
202}
203
204void ABIInfo::appendAttributeMangling(StringRef AttrStr,
205 raw_ostream &Out) const {
206 if (AttrStr == "default") {
207 Out << ".default";
208 return;
209 }
210
211 Out << '.';
212 const TargetInfo &TI = CGT.getTarget();
213 ParsedTargetAttr Info = TI.parseTargetAttr(AttrStr);
214
215 llvm::sort(Info.Features, [&TI](StringRef LHS, StringRef RHS) {
216 // Multiversioning doesn't allow "no-${feature}", so we can
217 // only have "+" prefixes here.
218 assert(LHS.starts_with("+") && RHS.starts_with("+") &&
219 "Features should always have a prefix.");
220 return TI.getFMVPriority({LHS.substr(1)})
221 .ugt(TI.getFMVPriority({RHS.substr(1)}));
222 });
223
224 bool IsFirst = true;
225 if (!Info.CPU.empty()) {
226 IsFirst = false;
227 Out << "arch_" << Info.CPU;
228 }
229
230 for (StringRef Feat : Info.Features) {
231 if (!IsFirst)
232 Out << '_';
233 IsFirst = false;
234 Out << Feat.substr(1);
235 }
236}
237
238llvm::FixedVectorType *
240 const LangOptions &Opt) const {
241 if (T->getNumElements() == 3 && !Opt.PreserveVec3Type)
242 return llvm::FixedVectorType::get(T->getElementType(), 4);
243 return T;
244}
245
246llvm::Value *ABIInfo::createCoercedLoad(Address SrcAddr, const ABIArgInfo &AI,
247 CodeGenFunction &CGF) const {
248 return nullptr;
249}
250
251void ABIInfo::createCoercedStore(llvm::Value *Val, Address DstAddr,
252 const ABIArgInfo &AI, bool DestIsVolatile,
253 CodeGenFunction &CGF) const {}
254// Pin the vtable to this file.
256
257/// Does the given lowering require more than the given number of
258/// registers when expanded?
259///
260/// This is intended to be the basis of a reasonable basic implementation
261/// of should{Pass,Return}Indirectly.
262///
263/// For most targets, a limit of four total registers is reasonable; this
264/// limits the amount of code required in order to move around the value
265/// in case it wasn't produced immediately prior to the call by the caller
266/// (or wasn't produced in exactly the right registers) or isn't used
267/// immediately within the callee. But some targets may need to further
268/// limit the register count due to an inability to support that many
269/// return registers.
271 unsigned maxAllRegisters) const {
272 unsigned intCount = 0, fpCount = 0;
273 for (llvm::Type *type : scalarTypes) {
274 if (type->isPointerTy()) {
275 intCount++;
276 } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) {
277 auto ptrWidth = CGT.getTarget().getPointerWidth(LangAS::Default);
278 intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth;
279 } else {
280 assert(type->isVectorTy() || type->isFloatingPointTy());
281 fpCount++;
282 }
283 }
284
285 return (intCount + fpCount > maxAllRegisters);
286}
287
289 bool AsReturnValue) const {
290 return occupiesMoreThan(ComponentTys, /*total=*/4);
291}
292
293bool SwiftABIInfo::isLegalVectorType(CharUnits VectorSize, llvm::Type *EltTy,
294 unsigned NumElts) const {
295 // The default implementation of this assumes that the target guarantees
296 // 128-bit SIMD support but nothing more.
297 return (VectorSize.getQuantity() > 8 && VectorSize.getQuantity() <= 16);
298}
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
QualType getVectorType(QualType VectorType, unsigned NumElts, VectorKind VecKind) const
Return the unique reference to a vector type of the specified element type and size.
const LangOptions & getLangOpts() const
Definition: ASTContext.h:894
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
Definition: ASTContext.h:2625
Attr - This represents one attribute.
Definition: Attr.h:44
A fixed int type of a specified bitwidth.
Definition: TypeBase.h:8195
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:185
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
ABIArgInfo - Helper class to encapsulate information about how a specific C type should be passed to ...
static ABIArgInfo getIndirectInReg(CharUnits Alignment, bool ByVal=true, bool Realign=false)
static ABIArgInfo getIndirect(CharUnits Alignment, unsigned AddrSpace, bool ByVal=true, bool Realign=false, llvm::Type *Padding=nullptr)
const llvm::DataLayout & getDataLayout() const
Definition: ABIInfo.cpp:26
CodeGen::ABIArgInfo getNaturalAlignIndirect(QualType Ty, unsigned AddrSpace, bool ByVal=true, bool Realign=false, llvm::Type *Padding=nullptr) const
A convenience method to return an indirect ABIArgInfo with an expected alignment equal to the ABI ali...
Definition: ABIInfo.cpp:173
const CodeGenOptions & getCodeGenOpts() const
Definition: ABIInfo.cpp:32
CodeGen::CodeGenTypes & CGT
Definition: ABIInfo.h:50
bool isHomogeneousAggregate(QualType Ty, const Type *&Base, uint64_t &Members) const
isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous aggregate.
Definition: ABIInfo.cpp:61
CodeGen::CGCXXABI & getCXXABI() const
Definition: ABIInfo.cpp:18
ASTContext & getContext() const
Definition: ABIInfo.cpp:20
virtual bool isHomogeneousAggregateBaseType(QualType Ty) const
Definition: ABIInfo.cpp:47
bool isPromotableIntegerTypeForABI(QualType Ty) const
Definition: ABIInfo.cpp:162
virtual void createCoercedStore(llvm::Value *Val, Address DstAddr, const ABIArgInfo &AI, bool DestIsVolatile, CodeGenFunction &CGF) const
Definition: ABIInfo.cpp:251
virtual void appendAttributeMangling(TargetAttr *Attr, raw_ostream &Out) const
Definition: ABIInfo.cpp:186
bool isOHOSFamily() const
Definition: ABIInfo.cpp:38
virtual RValue EmitMSVAArg(CodeGen::CodeGenFunction &CGF, CodeGen::Address VAListAddr, QualType Ty, AggValueSlot Slot) const
Emit the target dependent code to load a value of.
Definition: ABIInfo.cpp:42
virtual llvm::Value * createCoercedLoad(Address SrcAddr, const ABIArgInfo &AI, CodeGenFunction &CGF) const
Definition: ABIInfo.cpp:246
virtual bool isHomogeneousAggregateSmallEnough(const Type *Base, uint64_t Members) const
Definition: ABIInfo.cpp:51
const TargetInfo & getTarget() const
Definition: ABIInfo.cpp:30
virtual bool isZeroLengthBitfieldPermittedInHomogeneousAggregate() const
Definition: ABIInfo.cpp:56
CodeGen::ABIArgInfo getNaturalAlignIndirectInReg(QualType Ty, bool Realign=false) const
Definition: ABIInfo.cpp:180
bool isAndroid() const
Definition: ABIInfo.cpp:36
llvm::LLVMContext & getVMContext() const
Definition: ABIInfo.cpp:22
virtual llvm::FixedVectorType * getOptimalVectorMemoryType(llvm::FixedVectorType *T, const LangOptions &Opt) const
Returns the optimal vector memory type based on the given vector type.
Definition: ABIInfo.cpp:239
Like RawAddress, an abstract representation of an aligned address, but the pointer contained in this ...
Definition: Address.h:128
An aggregate value slot.
Definition: CGValue.h:504
Implements C++ ABI-specific code generation functions.
Definition: CGCXXABI.h:43
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
CGCXXABI & getCXXABI() const
const CodeGenOptions & getCodeGenOpts() const
ASTContext & getContext() const
Definition: CodeGenTypes.h:103
const TargetInfo & getTarget() const
Definition: CodeGenTypes.h:104
llvm::LLVMContext & getLLVMContext()
Definition: CodeGenTypes.h:106
const llvm::DataLayout & getDataLayout() const
Definition: CodeGenTypes.h:99
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
Definition: CGValue.h:42
static RValue getIgnored()
Definition: CGValue.h:93
virtual bool isLegalVectorType(CharUnits VectorSize, llvm::Type *EltTy, unsigned NumElts) const
Returns true if the given vector type is legal from Swift's calling convention perspective.
Definition: ABIInfo.cpp:293
bool occupiesMoreThan(ArrayRef< llvm::Type * > scalarTypes, unsigned maxAllRegisters) const
Does the given lowering require more than the given number of registers when expanded?
Definition: ABIInfo.cpp:270
virtual bool shouldPassIndirectly(ArrayRef< llvm::Type * > ComponentTys, bool AsReturnValue) const
Returns true if an aggregate which expands to the given type sequence should be passed / returned ind...
Definition: ABIInfo.cpp:288
Complex values, per C99 6.2.5p11.
Definition: TypeBase.h:3293
Represents the canonical version of C arrays with a specified constant size.
Definition: TypeBase.h:3776
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:434
A (possibly-)qualified type.
Definition: TypeBase.h:937
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition: TypeBase.h:8343
Exposes information about the current target.
Definition: TargetInfo.h:226
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:1288
uint64_t getPointerWidth(LangAS AddrSpace) const
Return the width of pointers on this target, for the specified address space.
Definition: TargetInfo.h:486
virtual llvm::APInt getFMVPriority(ArrayRef< StringRef > Features) const
Definition: TargetInfo.h:1567
virtual ParsedTargetAttr parseTargetAttr(StringRef Str) const
Definition: TargetInfo.cpp:577
The base class of the type hierarchy.
Definition: TypeBase.h:1833
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition: Type.h:41
bool isVectorType() const
Definition: TypeBase.h:8719
const T * getAs() const
Member-template getAs<specific type>'.
Definition: TypeBase.h:9159
Represents a GCC generic vector type.
Definition: TypeBase.h:4191
bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays, bool AsIfNoUniqueAddr=false)
isEmptyRecord - Return true iff a structure contains only empty fields.
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
Contains information gathered from parsing the contents of TargetAttr.
Definition: TargetInfo.h:60
std::vector< std::string > Features
Definition: TargetInfo.h:61