clang 20.0.0git
RISCV.cpp
Go to the documentation of this file.
1//===- RISCV.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 "ABIInfoImpl.h"
10#include "TargetInfo.h"
11
12using namespace clang;
13using namespace clang::CodeGen;
14
15//===----------------------------------------------------------------------===//
16// RISC-V ABI Implementation
17//===----------------------------------------------------------------------===//
18
19namespace {
20class RISCVABIInfo : public DefaultABIInfo {
21private:
22 // Size of the integer ('x') registers in bits.
23 unsigned XLen;
24 // Size of the floating point ('f') registers in bits. Note that the target
25 // ISA might have a wider FLen than the selected ABI (e.g. an RV32IF target
26 // with soft float ABI has FLen==0).
27 unsigned FLen;
28 const int NumArgGPRs;
29 const int NumArgFPRs;
30 const bool EABI;
31 bool detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff,
32 llvm::Type *&Field1Ty,
33 CharUnits &Field1Off,
34 llvm::Type *&Field2Ty,
35 CharUnits &Field2Off) const;
36
37public:
38 RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen, unsigned FLen,
39 bool EABI)
40 : DefaultABIInfo(CGT), XLen(XLen), FLen(FLen), NumArgGPRs(EABI ? 6 : 8),
41 NumArgFPRs(FLen != 0 ? 8 : 0), EABI(EABI) {}
42
43 // DefaultABIInfo's classifyReturnType and classifyArgumentType are
44 // non-virtual, but computeInfo is virtual, so we overload it.
45 void computeInfo(CGFunctionInfo &FI) const override;
46
47 ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, int &ArgGPRsLeft,
48 int &ArgFPRsLeft) const;
50
51 RValue EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
52 AggValueSlot Slot) const override;
53
54 ABIArgInfo extendType(QualType Ty, llvm::Type *CoerceTy = nullptr) const;
55
56 bool detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty,
57 CharUnits &Field1Off, llvm::Type *&Field2Ty,
58 CharUnits &Field2Off, int &NeededArgGPRs,
59 int &NeededArgFPRs) const;
60 ABIArgInfo coerceAndExpandFPCCEligibleStruct(llvm::Type *Field1Ty,
61 CharUnits Field1Off,
62 llvm::Type *Field2Ty,
63 CharUnits Field2Off) const;
64
65 ABIArgInfo coerceVLSVector(QualType Ty) const;
66
68 void appendAttributeMangling(TargetClonesAttr *Attr, unsigned Index,
69 raw_ostream &Out) const override;
70 void appendAttributeMangling(StringRef AttrStr,
71 raw_ostream &Out) const override;
72};
73} // end anonymous namespace
74
75void RISCVABIInfo::appendAttributeMangling(TargetClonesAttr *Attr,
76 unsigned Index,
77 raw_ostream &Out) const {
78 appendAttributeMangling(Attr->getFeatureStr(Index), Out);
79}
80
81void RISCVABIInfo::appendAttributeMangling(StringRef AttrStr,
82 raw_ostream &Out) const {
83 if (AttrStr == "default") {
84 Out << ".default";
85 return;
86 }
87
88 Out << '.';
89
91 AttrStr.split(Attrs, ';');
92
93 // Only consider the arch string.
94 StringRef ArchStr;
95 for (auto &Attr : Attrs) {
96 if (Attr.starts_with("arch="))
97 ArchStr = Attr;
98 }
99
100 // Extract features string.
102 ArchStr.consume_front("arch=");
103 ArchStr.split(Features, ',');
104
105 llvm::stable_sort(Features);
106
107 for (auto Feat : Features) {
108 Feat.consume_front("+");
109 Out << "_" << Feat;
110 }
111}
112
113void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const {
114 QualType RetTy = FI.getReturnType();
115 if (!getCXXABI().classifyReturnType(FI))
116 FI.getReturnInfo() = classifyReturnType(RetTy);
117
118 // IsRetIndirect is true if classifyArgumentType indicated the value should
119 // be passed indirect, or if the type size is a scalar greater than 2*XLen
120 // and not a complex type with elements <= FLen. e.g. fp128 is passed direct
121 // in LLVM IR, relying on the backend lowering code to rewrite the argument
122 // list and pass indirectly on RV32.
123 bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect;
124 if (!IsRetIndirect && RetTy->isScalarType() &&
125 getContext().getTypeSize(RetTy) > (2 * XLen)) {
126 if (RetTy->isComplexType() && FLen) {
127 QualType EltTy = RetTy->castAs<ComplexType>()->getElementType();
128 IsRetIndirect = getContext().getTypeSize(EltTy) > FLen;
129 } else {
130 // This is a normal scalar > 2*XLen, such as fp128 on RV32.
131 IsRetIndirect = true;
132 }
133 }
134
135 int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs;
136 int ArgFPRsLeft = NumArgFPRs;
137 int NumFixedArgs = FI.getNumRequiredArgs();
138
139 int ArgNum = 0;
140 for (auto &ArgInfo : FI.arguments()) {
141 bool IsFixed = ArgNum < NumFixedArgs;
142 ArgInfo.info =
143 classifyArgumentType(ArgInfo.type, IsFixed, ArgGPRsLeft, ArgFPRsLeft);
144 ArgNum++;
145 }
146}
147
148// Returns true if the struct is a potential candidate for the floating point
149// calling convention. If this function returns true, the caller is
150// responsible for checking that if there is only a single field then that
151// field is a float.
152bool RISCVABIInfo::detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff,
153 llvm::Type *&Field1Ty,
154 CharUnits &Field1Off,
155 llvm::Type *&Field2Ty,
156 CharUnits &Field2Off) const {
157 bool IsInt = Ty->isIntegralOrEnumerationType();
158 bool IsFloat = Ty->isRealFloatingType();
159
160 if (IsInt || IsFloat) {
161 uint64_t Size = getContext().getTypeSize(Ty);
162 if (IsInt && Size > XLen)
163 return false;
164 // Can't be eligible if larger than the FP registers. Handling of half
165 // precision values has been specified in the ABI, so don't block those.
166 if (IsFloat && Size > FLen)
167 return false;
168 // Can't be eligible if an integer type was already found (int+int pairs
169 // are not eligible).
170 if (IsInt && Field1Ty && Field1Ty->isIntegerTy())
171 return false;
172 if (!Field1Ty) {
173 Field1Ty = CGT.ConvertType(Ty);
174 Field1Off = CurOff;
175 return true;
176 }
177 if (!Field2Ty) {
178 Field2Ty = CGT.ConvertType(Ty);
179 Field2Off = CurOff;
180 return true;
181 }
182 return false;
183 }
184
185 if (auto CTy = Ty->getAs<ComplexType>()) {
186 if (Field1Ty)
187 return false;
188 QualType EltTy = CTy->getElementType();
189 if (getContext().getTypeSize(EltTy) > FLen)
190 return false;
191 Field1Ty = CGT.ConvertType(EltTy);
192 Field1Off = CurOff;
193 Field2Ty = Field1Ty;
194 Field2Off = Field1Off + getContext().getTypeSizeInChars(EltTy);
195 return true;
196 }
197
198 if (const ConstantArrayType *ATy = getContext().getAsConstantArrayType(Ty)) {
199 uint64_t ArraySize = ATy->getZExtSize();
200 QualType EltTy = ATy->getElementType();
201 // Non-zero-length arrays of empty records make the struct ineligible for
202 // the FP calling convention in C++.
203 if (const auto *RTy = EltTy->getAs<RecordType>()) {
204 if (ArraySize != 0 && isa<CXXRecordDecl>(RTy->getDecl()) &&
205 isEmptyRecord(getContext(), EltTy, true, true))
206 return false;
207 }
208 CharUnits EltSize = getContext().getTypeSizeInChars(EltTy);
209 for (uint64_t i = 0; i < ArraySize; ++i) {
210 bool Ret = detectFPCCEligibleStructHelper(EltTy, CurOff, Field1Ty,
211 Field1Off, Field2Ty, Field2Off);
212 if (!Ret)
213 return false;
214 CurOff += EltSize;
215 }
216 return true;
217 }
218
219 if (const auto *RTy = Ty->getAs<RecordType>()) {
220 // Structures with either a non-trivial destructor or a non-trivial
221 // copy constructor are not eligible for the FP calling convention.
222 if (getRecordArgABI(Ty, CGT.getCXXABI()))
223 return false;
224 if (isEmptyRecord(getContext(), Ty, true, true))
225 return true;
226 const RecordDecl *RD = RTy->getDecl();
227 // Unions aren't eligible unless they're empty (which is caught above).
228 if (RD->isUnion())
229 return false;
230 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
231 // If this is a C++ record, check the bases first.
232 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
233 for (const CXXBaseSpecifier &B : CXXRD->bases()) {
234 const auto *BDecl =
235 cast<CXXRecordDecl>(B.getType()->castAs<RecordType>()->getDecl());
236 CharUnits BaseOff = Layout.getBaseClassOffset(BDecl);
237 bool Ret = detectFPCCEligibleStructHelper(B.getType(), CurOff + BaseOff,
238 Field1Ty, Field1Off, Field2Ty,
239 Field2Off);
240 if (!Ret)
241 return false;
242 }
243 }
244 int ZeroWidthBitFieldCount = 0;
245 for (const FieldDecl *FD : RD->fields()) {
246 uint64_t FieldOffInBits = Layout.getFieldOffset(FD->getFieldIndex());
247 QualType QTy = FD->getType();
248 if (FD->isBitField()) {
249 unsigned BitWidth = FD->getBitWidthValue(getContext());
250 // Allow a bitfield with a type greater than XLen as long as the
251 // bitwidth is XLen or less.
252 if (getContext().getTypeSize(QTy) > XLen && BitWidth <= XLen)
253 QTy = getContext().getIntTypeForBitwidth(XLen, false);
254 if (BitWidth == 0) {
255 ZeroWidthBitFieldCount++;
256 continue;
257 }
258 }
259
260 bool Ret = detectFPCCEligibleStructHelper(
261 QTy, CurOff + getContext().toCharUnitsFromBits(FieldOffInBits),
262 Field1Ty, Field1Off, Field2Ty, Field2Off);
263 if (!Ret)
264 return false;
265
266 // As a quirk of the ABI, zero-width bitfields aren't ignored for fp+fp
267 // or int+fp structs, but are ignored for a struct with an fp field and
268 // any number of zero-width bitfields.
269 if (Field2Ty && ZeroWidthBitFieldCount > 0)
270 return false;
271 }
272 return Field1Ty != nullptr;
273 }
274
275 return false;
276}
277
278// Determine if a struct is eligible for passing according to the floating
279// point calling convention (i.e., when flattened it contains a single fp
280// value, fp+fp, or int+fp of appropriate size). If so, NeededArgFPRs and
281// NeededArgGPRs are incremented appropriately.
282bool RISCVABIInfo::detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty,
283 CharUnits &Field1Off,
284 llvm::Type *&Field2Ty,
285 CharUnits &Field2Off,
286 int &NeededArgGPRs,
287 int &NeededArgFPRs) const {
288 Field1Ty = nullptr;
289 Field2Ty = nullptr;
290 NeededArgGPRs = 0;
291 NeededArgFPRs = 0;
292 bool IsCandidate = detectFPCCEligibleStructHelper(
293 Ty, CharUnits::Zero(), Field1Ty, Field1Off, Field2Ty, Field2Off);
294 if (!Field1Ty)
295 return false;
296 // Not really a candidate if we have a single int but no float.
297 if (Field1Ty && !Field2Ty && !Field1Ty->isFloatingPointTy())
298 return false;
299 if (!IsCandidate)
300 return false;
301 if (Field1Ty && Field1Ty->isFloatingPointTy())
302 NeededArgFPRs++;
303 else if (Field1Ty)
304 NeededArgGPRs++;
305 if (Field2Ty && Field2Ty->isFloatingPointTy())
306 NeededArgFPRs++;
307 else if (Field2Ty)
308 NeededArgGPRs++;
309 return true;
310}
311
312// Call getCoerceAndExpand for the two-element flattened struct described by
313// Field1Ty, Field1Off, Field2Ty, Field2Off. This method will create an
314// appropriate coerceToType and unpaddedCoerceToType.
315ABIArgInfo RISCVABIInfo::coerceAndExpandFPCCEligibleStruct(
316 llvm::Type *Field1Ty, CharUnits Field1Off, llvm::Type *Field2Ty,
317 CharUnits Field2Off) const {
319 SmallVector<llvm::Type *, 2> UnpaddedCoerceElts;
320 if (!Field1Off.isZero())
321 CoerceElts.push_back(llvm::ArrayType::get(
322 llvm::Type::getInt8Ty(getVMContext()), Field1Off.getQuantity()));
323
324 CoerceElts.push_back(Field1Ty);
325 UnpaddedCoerceElts.push_back(Field1Ty);
326
327 if (!Field2Ty) {
329 llvm::StructType::get(getVMContext(), CoerceElts, !Field1Off.isZero()),
330 UnpaddedCoerceElts[0]);
331 }
332
333 CharUnits Field2Align =
334 CharUnits::fromQuantity(getDataLayout().getABITypeAlign(Field2Ty));
335 CharUnits Field1End = Field1Off +
336 CharUnits::fromQuantity(getDataLayout().getTypeStoreSize(Field1Ty));
337 CharUnits Field2OffNoPadNoPack = Field1End.alignTo(Field2Align);
338
339 CharUnits Padding = CharUnits::Zero();
340 if (Field2Off > Field2OffNoPadNoPack)
341 Padding = Field2Off - Field2OffNoPadNoPack;
342 else if (Field2Off != Field2Align && Field2Off > Field1End)
343 Padding = Field2Off - Field1End;
344
345 bool IsPacked = !Field2Off.isMultipleOf(Field2Align);
346
347 if (!Padding.isZero())
348 CoerceElts.push_back(llvm::ArrayType::get(
349 llvm::Type::getInt8Ty(getVMContext()), Padding.getQuantity()));
350
351 CoerceElts.push_back(Field2Ty);
352 UnpaddedCoerceElts.push_back(Field2Ty);
353
354 auto CoerceToType =
355 llvm::StructType::get(getVMContext(), CoerceElts, IsPacked);
356 auto UnpaddedCoerceToType =
357 llvm::StructType::get(getVMContext(), UnpaddedCoerceElts, IsPacked);
358
359 return ABIArgInfo::getCoerceAndExpand(CoerceToType, UnpaddedCoerceToType);
360}
361
362// Fixed-length RVV vectors are represented as scalable vectors in function
363// args/return and must be coerced from fixed vectors.
364ABIArgInfo RISCVABIInfo::coerceVLSVector(QualType Ty) const {
365 assert(Ty->isVectorType() && "expected vector type!");
366
367 const auto *VT = Ty->castAs<VectorType>();
368 assert(VT->getElementType()->isBuiltinType() && "expected builtin type!");
369
370 auto VScale =
371 getContext().getTargetInfo().getVScaleRange(getContext().getLangOpts());
372
373 unsigned NumElts = VT->getNumElements();
374 llvm::Type *EltType = llvm::Type::getInt1Ty(getVMContext());
375 switch (VT->getVectorKind()) {
376 case VectorKind::RVVFixedLengthMask_1:
377 break;
378 case VectorKind::RVVFixedLengthMask_2:
379 NumElts *= 2;
380 break;
381 case VectorKind::RVVFixedLengthMask_4:
382 NumElts *= 4;
383 break;
384 case VectorKind::RVVFixedLengthMask:
385 NumElts *= 8;
386 break;
387 default:
388 assert(VT->getVectorKind() == VectorKind::RVVFixedLengthData &&
389 "Unexpected vector kind");
390 EltType = CGT.ConvertType(VT->getElementType());
391 }
392
393 // The MinNumElts is simplified from equation:
394 // NumElts / VScale =
395 // (EltSize * NumElts / (VScale * RVVBitsPerBlock))
396 // * (RVVBitsPerBlock / EltSize)
397 llvm::ScalableVectorType *ResType =
398 llvm::ScalableVectorType::get(EltType, NumElts / VScale->first);
399 return ABIArgInfo::getDirect(ResType);
400}
401
402ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed,
403 int &ArgGPRsLeft,
404 int &ArgFPRsLeft) const {
405 assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow");
407
408 // Structures with either a non-trivial destructor or a non-trivial
409 // copy constructor are always passed indirectly.
410 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
411 if (ArgGPRsLeft)
412 ArgGPRsLeft -= 1;
413 return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
415 }
416
417 uint64_t Size = getContext().getTypeSize(Ty);
418
419 // Ignore empty structs/unions whose size is zero. According to the calling
420 // convention empty structs/unions are required to be sized types in C++.
421 if (isEmptyRecord(getContext(), Ty, true) && Size == 0)
422 return ABIArgInfo::getIgnore();
423
424 // Pass floating point values via FPRs if possible.
425 if (IsFixed && Ty->isFloatingType() && !Ty->isComplexType() &&
426 FLen >= Size && ArgFPRsLeft) {
427 ArgFPRsLeft--;
428 return ABIArgInfo::getDirect();
429 }
430
431 // Complex types for the hard float ABI must be passed direct rather than
432 // using CoerceAndExpand.
433 if (IsFixed && Ty->isComplexType() && FLen && ArgFPRsLeft >= 2) {
434 QualType EltTy = Ty->castAs<ComplexType>()->getElementType();
435 if (getContext().getTypeSize(EltTy) <= FLen) {
436 ArgFPRsLeft -= 2;
437 return ABIArgInfo::getDirect();
438 }
439 }
440
441 if (IsFixed && FLen && Ty->isStructureOrClassType()) {
442 llvm::Type *Field1Ty = nullptr;
443 llvm::Type *Field2Ty = nullptr;
444 CharUnits Field1Off = CharUnits::Zero();
445 CharUnits Field2Off = CharUnits::Zero();
446 int NeededArgGPRs = 0;
447 int NeededArgFPRs = 0;
448 bool IsCandidate =
449 detectFPCCEligibleStruct(Ty, Field1Ty, Field1Off, Field2Ty, Field2Off,
450 NeededArgGPRs, NeededArgFPRs);
451 if (IsCandidate && NeededArgGPRs <= ArgGPRsLeft &&
452 NeededArgFPRs <= ArgFPRsLeft) {
453 ArgGPRsLeft -= NeededArgGPRs;
454 ArgFPRsLeft -= NeededArgFPRs;
455 return coerceAndExpandFPCCEligibleStruct(Field1Ty, Field1Off, Field2Ty,
456 Field2Off);
457 }
458 }
459
460 uint64_t NeededAlign = getContext().getTypeAlign(Ty);
461 // Determine the number of GPRs needed to pass the current argument
462 // according to the ABI. 2*XLen-aligned varargs are passed in "aligned"
463 // register pairs, so may consume 3 registers.
464 // TODO: To be compatible with GCC's behaviors, we don't align registers
465 // currently if we are using ILP32E calling convention. This behavior may be
466 // changed when RV32E/ILP32E is ratified.
467 int NeededArgGPRs = 1;
468 if (!IsFixed && NeededAlign == 2 * XLen)
469 NeededArgGPRs = 2 + (EABI && XLen == 32 ? 0 : (ArgGPRsLeft % 2));
470 else if (Size > XLen && Size <= 2 * XLen)
471 NeededArgGPRs = 2;
472
473 if (NeededArgGPRs > ArgGPRsLeft) {
474 NeededArgGPRs = ArgGPRsLeft;
475 }
476
477 ArgGPRsLeft -= NeededArgGPRs;
478
479 if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) {
480 // Treat an enum type as its underlying type.
481 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
482 Ty = EnumTy->getDecl()->getIntegerType();
483
484 // All integral types are promoted to XLen width
485 if (Size < XLen && Ty->isIntegralOrEnumerationType()) {
486 return extendType(Ty, CGT.ConvertType(Ty));
487 }
488
489 if (const auto *EIT = Ty->getAs<BitIntType>()) {
490 if (EIT->getNumBits() < XLen)
491 return extendType(Ty, CGT.ConvertType(Ty));
492 if (EIT->getNumBits() > 128 ||
493 (!getContext().getTargetInfo().hasInt128Type() &&
494 EIT->getNumBits() > 64))
495 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
496 }
497
499
500 // If it is tuple type, it can't be flattened.
501 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)))
502 Info.setCanBeFlattened(!STy->containsHomogeneousScalableVectorTypes());
503
504 return Info;
505 }
506
507 if (const VectorType *VT = Ty->getAs<VectorType>())
508 if (VT->getVectorKind() == VectorKind::RVVFixedLengthData ||
509 VT->getVectorKind() == VectorKind::RVVFixedLengthMask ||
510 VT->getVectorKind() == VectorKind::RVVFixedLengthMask_1 ||
511 VT->getVectorKind() == VectorKind::RVVFixedLengthMask_2 ||
512 VT->getVectorKind() == VectorKind::RVVFixedLengthMask_4)
513 return coerceVLSVector(Ty);
514
515 // Aggregates which are <= 2*XLen will be passed in registers if possible,
516 // so coerce to integers.
517 if (Size <= 2 * XLen) {
518 unsigned Alignment = getContext().getTypeAlign(Ty);
519
520 // Use a single XLen int if possible, 2*XLen if 2*XLen alignment is
521 // required, and a 2-element XLen array if only XLen alignment is required.
522 if (Size <= XLen) {
524 llvm::IntegerType::get(getVMContext(), XLen));
525 } else if (Alignment == 2 * XLen) {
527 llvm::IntegerType::get(getVMContext(), 2 * XLen));
528 } else {
529 return ABIArgInfo::getDirect(llvm::ArrayType::get(
530 llvm::IntegerType::get(getVMContext(), XLen), 2));
531 }
532 }
533 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
534}
535
536ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy) const {
537 if (RetTy->isVoidType())
538 return ABIArgInfo::getIgnore();
539
540 int ArgGPRsLeft = 2;
541 int ArgFPRsLeft = FLen ? 2 : 0;
542
543 // The rules for return and argument types are the same, so defer to
544 // classifyArgumentType.
545 return classifyArgumentType(RetTy, /*IsFixed=*/true, ArgGPRsLeft,
546 ArgFPRsLeft);
547}
548
549RValue RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
550 QualType Ty, AggValueSlot Slot) const {
551 CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8);
552
553 // Empty records are ignored for parameter passing purposes.
554 if (isEmptyRecord(getContext(), Ty, true))
555 return Slot.asRValue();
556
557 auto TInfo = getContext().getTypeInfoInChars(Ty);
558
559 // TODO: To be compatible with GCC's behaviors, we force arguments with
560 // 2×XLEN-bit alignment and size at most 2×XLEN bits like `long long`,
561 // `unsigned long long` and `double` to have 4-byte alignment. This
562 // behavior may be changed when RV32E/ILP32E is ratified.
563 if (EABI && XLen == 32)
564 TInfo.Align = std::min(TInfo.Align, CharUnits::fromQuantity(4));
565
566 // Arguments bigger than 2*Xlen bytes are passed indirectly.
567 bool IsIndirect = TInfo.Width > 2 * SlotSize;
568
569 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TInfo, SlotSize,
570 /*AllowHigherAlign=*/true, Slot);
571}
572
573ABIArgInfo RISCVABIInfo::extendType(QualType Ty, llvm::Type *CoerceTy) const {
574 int TySize = getContext().getTypeSize(Ty);
575 // RV64 ABI requires unsigned 32 bit integers to be sign extended.
576 if (XLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
577 return ABIArgInfo::getSignExtend(Ty, CoerceTy);
578 return ABIArgInfo::getExtend(Ty, CoerceTy);
579}
580
581namespace {
582class RISCVTargetCodeGenInfo : public TargetCodeGenInfo {
583public:
584 RISCVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen,
585 unsigned FLen, bool EABI)
587 std::make_unique<RISCVABIInfo>(CGT, XLen, FLen, EABI)) {
588 SwiftInfo =
589 std::make_unique<SwiftABIInfo>(CGT, /*SwiftErrorInRegister=*/false);
590 }
591
592 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
593 CodeGen::CodeGenModule &CGM) const override {
594 const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
595 if (!FD) return;
596
597 auto *Fn = cast<llvm::Function>(GV);
598
599 if (CGM.getCodeGenOpts().CFProtectionReturn)
600 Fn->addFnAttr("hw-shadow-stack");
601
602 const auto *Attr = FD->getAttr<RISCVInterruptAttr>();
603 if (!Attr)
604 return;
605
606 const char *Kind;
607 switch (Attr->getInterrupt()) {
608 case RISCVInterruptAttr::supervisor: Kind = "supervisor"; break;
609 case RISCVInterruptAttr::machine: Kind = "machine"; break;
610 }
611
612 Fn->addFnAttr("interrupt", Kind);
613 }
614};
615} // namespace
616
617std::unique_ptr<TargetCodeGenInfo>
619 unsigned FLen, bool EABI) {
620 return std::make_unique<RISCVTargetCodeGenInfo>(CGM.getTypes(), XLen, FLen,
621 EABI);
622}
const Decl * D
static CharUnits getTypeStoreSize(CodeGenModule &CGM, llvm::Type *type)
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
Definition: RecordLayout.h:38
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
Definition: RecordLayout.h:200
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
Definition: RecordLayout.h:249
Attr - This represents one attribute.
Definition: Attr.h:43
A fixed int type of a specified bitwidth.
Definition: Type.h:7814
Represents a base class of a C++ class.
Definition: DeclCXX.h:146
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
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition: CharUnits.h:122
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:185
bool isMultipleOf(CharUnits N) const
Test whether this is a multiple of the other value.
Definition: CharUnits.h:143
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition: CharUnits.h:63
CharUnits alignTo(const CharUnits &Align) const
alignTo - Returns the next integer (mod 2**64) that is greater than or equal to this quantity and is ...
Definition: CharUnits.h:201
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition: CharUnits.h:53
ABIArgInfo - Helper class to encapsulate information about how a specific C type should be passed to ...
static ABIArgInfo getIgnore()
static ABIArgInfo getDirect(llvm::Type *T=nullptr, unsigned Offset=0, llvm::Type *Padding=nullptr, bool CanBeFlattened=true, unsigned Align=0)
@ Indirect
Indirect - Pass the argument indirectly via a hidden pointer with the specified alignment (0 indicate...
static ABIArgInfo getExtend(QualType Ty, llvm::Type *T=nullptr)
static ABIArgInfo getCoerceAndExpand(llvm::StructType *coerceToType, llvm::Type *unpaddedCoerceToType)
void setCanBeFlattened(bool Flatten)
static ABIArgInfo getSignExtend(QualType Ty, llvm::Type *T=nullptr)
virtual void appendAttributeMangling(TargetAttr *Attr, raw_ostream &Out) const
Definition: ABIInfo.cpp:187
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
RValue asRValue() const
Definition: CGValue.h:666
RecordArgABI
Specify how one should pass an argument of a record type.
Definition: CGCXXABI.h:150
@ RAA_DirectInMemory
Pass it on the stack using its defined layout.
Definition: CGCXXABI.h:158
CGFunctionInfo - Class to encapsulate the information about a function definition.
CanQualType getReturnType() const
MutableArrayRef< ArgInfo > arguments()
unsigned getNumRequiredArgs() const
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
This class organizes the cross-function state that is used while generating LLVM code.
const CodeGenOptions & getCodeGenOpts() const
This class organizes the cross-module state that is used while lowering AST types to LLVM types.
Definition: CodeGenTypes.h:54
DefaultABIInfo - The default implementation for ABI specific details.
Definition: ABIInfoImpl.h:21
ABIArgInfo classifyArgumentType(QualType RetTy) const
Definition: ABIInfoImpl.cpp:17
RValue EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, QualType Ty, AggValueSlot Slot) const override
EmitVAArg - Emit the target dependent code to load a value of.
Definition: ABIInfoImpl.cpp:75
ABIArgInfo classifyReturnType(QualType RetTy) const
Definition: ABIInfoImpl.cpp:46
void computeInfo(CGFunctionInfo &FI) const override
Definition: ABIInfoImpl.cpp:68
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
Definition: CGValue.h:42
TargetCodeGenInfo - This class organizes various target-specific codegeneration issues,...
Definition: TargetInfo.h:47
virtual void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const
setTargetAttributes - Provides a convenient hook to handle extra target-specific attributes for the g...
Definition: TargetInfo.h:76
Complex values, per C99 6.2.5p11.
Definition: Type.h:3145
Represents the canonical version of C arrays with a specified constant size.
Definition: Type.h:3615
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of enums.
Definition: Type.h:6098
Represents a member of a struct/union/class.
Definition: Decl.h:3033
A (possibly-)qualified type.
Definition: Type.h:929
Represents a struct/union/class.
Definition: Decl.h:4148
field_range fields() const
Definition: Decl.h:4354
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:6072
RecordDecl * getDecl() const
Definition: Type.h:6082
bool isUnion() const
Definition: Decl.h:3770
bool isVoidType() const
Definition: Type.h:8510
bool isComplexType() const
isComplexType() does not include complex integers (a GCC extension).
Definition: Type.cpp:710
bool isUnsignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is unsigned or an enumeration types whose underlying ...
Definition: Type.cpp:2251
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:8800
bool isScalarType() const
Definition: Type.h:8609
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition: Type.h:8625
bool isStructureOrClassType() const
Definition: Type.cpp:690
bool isVectorType() const
Definition: Type.h:8298
bool isRealFloatingType() const
Floating point categories.
Definition: Type.cpp:2300
bool isFloatingType() const
Definition: Type.cpp:2283
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:8731
Represents a GCC generic vector type.
Definition: Type.h:4034
ABIArgInfo classifyArgumentType(CodeGenModule &CGM, CanQualType type)
Classify the rules for how to pass a particular type.
CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, CGCXXABI &CXXABI)
bool classifyReturnType(const CGCXXABI &CXXABI, CGFunctionInfo &FI, const ABIInfo &Info)
RValue emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr, QualType ValueTy, bool IsIndirect, TypeInfoChars ValueInfo, CharUnits SlotSizeAndAlign, bool AllowHigherAlign, AggValueSlot Slot, bool ForceRightAdjust=false)
Emit va_arg for a platform using the common void* representation, where arguments are simply emitted ...
bool isAggregateTypeForABI(QualType T)
Definition: ABIInfoImpl.cpp:94
QualType useFirstFieldIfTransparentUnion(QualType Ty)
Pass transparent unions as if they were the type of the first element.
std::unique_ptr< TargetCodeGenInfo > createRISCVTargetCodeGenInfo(CodeGenModule &CGM, unsigned XLen, unsigned FLen, bool EABI)
Definition: RISCV.cpp:618
bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays, bool AsIfNoUniqueAddr=false)
isEmptyRecord - Return true iff a structure contains only empty fields.
bool Ret(InterpState &S, CodePtr &PC)
Definition: Interp.h:318
The JSON file list parser is used to communicate input to InstallAPI.
unsigned long uint64_t