clang 24.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
18CGCXXABI &ABIInfo::getCXXABI() const { return CGT.getCXXABI(); }
19
20ASTContext &ABIInfo::getContext() const { return CGT.getContext(); }
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 QualType Ty, AggValueSlot Slot) const {
49 return RValue::getIgnored();
50}
51
53 return false;
54}
55
57 uint64_t Members) const {
58 return false;
59}
60
62 // For compatibility with GCC, ignore empty bitfields in C++ mode.
63 return getContext().getLangOpts().CPlusPlus;
64}
65
67 uint64_t &Members) const {
68 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
69 uint64_t NElements = AT->getZExtSize();
70 if (NElements == 0)
71 return false;
72 if (!isHomogeneousAggregate(AT->getElementType(), Base, Members))
73 return false;
74 Members *= NElements;
75 } else if (const auto *RD = Ty->getAsRecordDecl()) {
76 if (RD->hasFlexibleArrayMember())
77 return false;
78
79 Members = 0;
80
81 // If this is a C++ record, check the properties of the record such as
82 // bases and ABI specific restrictions
83 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
84 if (!getCXXABI().isPermittedToBeHomogeneousAggregate(CXXRD))
85 return false;
86
87 for (const auto &I : CXXRD->bases()) {
88 // Ignore empty records.
89 if (isEmptyRecord(getContext(), I.getType(), true))
90 continue;
91
92 uint64_t FldMembers;
93 if (!isHomogeneousAggregate(I.getType(), Base, FldMembers))
94 return false;
95
96 Members += FldMembers;
97 }
98 }
99
100 for (const auto *FD : RD->fields()) {
101 // Ignore (non-zero arrays of) empty records.
102 QualType FT = FD->getType();
103 while (const ConstantArrayType *AT =
104 getContext().getAsConstantArrayType(FT)) {
105 if (AT->isZeroSize())
106 return false;
107 FT = AT->getElementType();
108 }
109 if (isEmptyRecord(getContext(), FT, true))
110 continue;
111
113 FD->isZeroLengthBitField())
114 continue;
115
116 uint64_t FldMembers;
117 if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers))
118 return false;
119
120 Members = (RD->isUnion() ?
121 std::max(Members, FldMembers) : Members + FldMembers);
122 }
123
124 if (!Base)
125 return false;
126
127 // Ensure there is no padding.
128 if (getContext().getTypeSize(Base) * Members !=
129 getContext().getTypeSize(Ty))
130 return false;
131 } else {
132 Members = 1;
133 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
134 Members = 2;
135 Ty = CT->getElementType();
136 }
137
138 // Most ABIs only support float, double, and some vector type widths.
140 return false;
141
142 // The base type must be the same for all members. Types that
143 // agree in both total size and mode (float vs. vector) are
144 // treated as being equivalent here.
145 const Type *TyPtr = Ty.getTypePtr();
146 if (!Base) {
147 Base = TyPtr;
148 // If it's a non-power-of-2 vector, its size is already a power-of-2,
149 // so make sure to widen it explicitly.
150 if (const VectorType *VT = Base->getAs<VectorType>()) {
151 QualType EltTy = VT->getElementType();
152 unsigned NumElements =
154 Base = getContext()
155 .getVectorType(EltTy, NumElements, VT->getVectorKind())
156 .getTypePtr();
157 }
158 }
159
160 if (Base->isVectorType() != TyPtr->isVectorType() ||
161 getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr))
162 return false;
163 }
164 return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members);
165}
166
168 if (getContext().isPromotableIntegerType(Ty))
169 return true;
170
171 if (const auto *EIT = Ty->getAs<BitIntType>())
172 if (EIT->getNumBits() < getContext().getTypeSize(getContext().IntTy))
173 return true;
174
175 return false;
176}
177
179 bool ByVal, bool Realign,
180 llvm::Type *Padding) const {
181 return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty),
182 AddrSpace, ByVal, Realign, Padding);
183}
184
186 bool Realign) const {
187 return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty),
188 /*ByVal*/ false, Realign);
189}
190
192 raw_ostream &Out) const {
193 if (Attr->isDefaultVersion())
194 return;
195 appendAttributeMangling(Attr->getFeaturesStr(), Out);
196}
197
199 raw_ostream &Out) const {
200 appendAttributeMangling(Attr->getNamesStr(), Out);
201}
202
203void ABIInfo::appendAttributeMangling(TargetClonesAttr *Attr, unsigned Index,
204 raw_ostream &Out) const {
205 appendAttributeMangling(Attr->getFeatureStr(Index), Out);
206 Out << '.' << Attr->getMangledIndex(Index);
207}
208
209void ABIInfo::appendAttributeMangling(StringRef AttrStr,
210 raw_ostream &Out) const {
211 if (AttrStr == "default") {
212 Out << ".default";
213 return;
214 }
215
216 Out << '.';
217 const TargetInfo &TI = CGT.getTarget();
218 ParsedTargetAttr Info = TI.parseTargetAttr(AttrStr);
219
220 llvm::sort(Info.Features, [&TI](StringRef LHS, StringRef RHS) {
221 // Multiversioning doesn't allow "no-${feature}", so we can
222 // only have "+" prefixes here.
223 assert(LHS.starts_with("+") && RHS.starts_with("+") &&
224 "Features should always have a prefix.");
225 return TI.getFMVPriority({LHS.substr(1)})
226 .ugt(TI.getFMVPriority({RHS.substr(1)}));
227 });
228
229 bool IsFirst = true;
230 if (!Info.CPU.empty()) {
231 IsFirst = false;
232 Out << "arch_" << Info.CPU;
233 }
234
235 for (StringRef Feat : Info.Features) {
236 if (!IsFirst)
237 Out << '_';
238 IsFirst = false;
239 Out << Feat.substr(1);
240 }
241}
242
243llvm::FixedVectorType *
244ABIInfo::getOptimalVectorMemoryType(llvm::FixedVectorType *T,
245 const LangOptions &Opt) const {
246 if (T->getNumElements() == 3 && !Opt.PreserveVec3Type)
247 return llvm::FixedVectorType::get(T->getElementType(), 4);
248 return T;
249}
250
251llvm::Value *ABIInfo::createCoercedLoad(Address SrcAddr, const ABIArgInfo &AI,
252 CodeGenFunction &CGF) const {
253 return nullptr;
254}
255
256void ABIInfo::createCoercedStore(llvm::Value *Val, Address DstAddr,
257 const ABIArgInfo &AI, bool DestIsVolatile,
258 CodeGenFunction &CGF) const {}
259
261 llvm_unreachable("Only implemented for x86");
262}
263
264// Pin the vtable to this file.
266
267/// Does the given lowering require more than the given number of
268/// registers when expanded?
269///
270/// This is intended to be the basis of a reasonable basic implementation
271/// of should{Pass,Return}Indirectly.
272///
273/// For most targets, a limit of four total registers is reasonable; this
274/// limits the amount of code required in order to move around the value
275/// in case it wasn't produced immediately prior to the call by the caller
276/// (or wasn't produced in exactly the right registers) or isn't used
277/// immediately within the callee. But some targets may need to further
278/// limit the register count due to an inability to support that many
279/// return registers.
281 unsigned maxAllRegisters) const {
282 unsigned intCount = 0, fpCount = 0;
283 for (llvm::Type *type : scalarTypes) {
284 if (type->isPointerTy()) {
285 intCount++;
286 } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) {
287 auto ptrWidth = CGT.getTarget().getPointerWidth(LangAS::Default);
288 intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth;
289 } else {
290 assert(type->isVectorTy() || type->isFloatingPointTy());
291 fpCount++;
292 }
293 }
294
295 return (intCount + fpCount > maxAllRegisters);
296}
297
299 bool AsReturnValue) const {
300 return occupiesMoreThan(ComponentTys, /*total=*/4);
301}
302
303bool SwiftABIInfo::isLegalVectorType(CharUnits VectorSize, llvm::Type *EltTy,
304 unsigned NumElts) const {
305 // The default implementation of this assumes that the target guarantees
306 // 128-bit SIMD support but nothing more.
307 return (VectorSize.getQuantity() > 8 && VectorSize.getQuantity() <= 16);
308}
unsigned IsFirst
Indicates that this is the first token of the file.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
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:965
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
Attr - This represents one attribute.
Definition Attr.h:46
A fixed int type of a specified bitwidth.
Definition TypeBase.h:8299
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:178
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:66
CodeGen::CGCXXABI & getCXXABI() const
Definition ABIInfo.cpp:18
ASTContext & getContext() const
Definition ABIInfo.cpp:20
virtual bool isHomogeneousAggregateBaseType(QualType Ty) const
Definition ABIInfo.cpp:52
bool isPromotableIntegerTypeForABI(QualType Ty) const
Definition ABIInfo.cpp:167
virtual void createCoercedStore(llvm::Value *Val, Address DstAddr, const ABIArgInfo &AI, bool DestIsVolatile, CodeGenFunction &CGF) const
Definition ABIInfo.cpp:256
virtual void appendAttributeMangling(TargetAttr *Attr, raw_ostream &Out) const
Definition ABIInfo.cpp:191
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 ABIArgInfo classifyArgForArm64ECVarArg(QualType Ty) const
Used by Arm64EC calling convention code to call into x86 calling convention code for varargs function...
Definition ABIInfo.cpp:260
virtual RValue EmitZOSVAArg(CodeGen::CodeGenFunction &CGF, CodeGen::Address VAListAddr, QualType Ty, AggValueSlot Slot) const
Emit the target dependent code to load a value of.
Definition ABIInfo.cpp:47
virtual llvm::Value * createCoercedLoad(Address SrcAddr, const ABIArgInfo &AI, CodeGenFunction &CGF) const
Definition ABIInfo.cpp:251
virtual bool isHomogeneousAggregateSmallEnough(const Type *Base, uint64_t Members) const
Definition ABIInfo.cpp:56
const TargetInfo & getTarget() const
Definition ABIInfo.cpp:30
virtual bool isZeroLengthBitfieldPermittedInHomogeneousAggregate() const
Definition ABIInfo.cpp:61
CodeGen::ABIArgInfo getNaturalAlignIndirectInReg(QualType Ty, bool Realign=false) const
Definition ABIInfo.cpp:185
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:244
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:551
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...
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:94
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:303
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:280
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:298
Complex values, per C99 6.2.5p11.
Definition TypeBase.h:3339
Represents the canonical version of C arrays with a specified constant size.
Definition TypeBase.h:3824
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
A (possibly-)qualified type.
Definition TypeBase.h:937
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition TypeBase.h:8447
Exposes information about the current target.
Definition TargetInfo.h:227
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
virtual llvm::APInt getFMVPriority(ArrayRef< StringRef > Features) const
virtual ParsedTargetAttr parseTargetAttr(StringRef Str) const
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition Type.h:41
bool isVectorType() const
Definition TypeBase.h:8823
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9277
Represents a GCC generic vector type.
Definition TypeBase.h:4239
@ Type
The l-value was considered opaque, so the alignment was determined from a type.
Definition CGValue.h:155
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.
Contains information gathered from parsing the contents of TargetAttr.
Definition TargetInfo.h:60
std::vector< std::string > Features
Definition TargetInfo.h:61