clang 20.0.0git
CSKY.cpp
Go to the documentation of this file.
1//===- CSKY.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// CSKY ABI Implementation
17//===----------------------------------------------------------------------===//
18namespace {
19class CSKYABIInfo : public DefaultABIInfo {
20 static const int NumArgGPRs = 4;
21 static const int NumArgFPRs = 4;
22
23 static const unsigned XLen = 32;
24 unsigned FLen;
25
26public:
27 CSKYABIInfo(CodeGen::CodeGenTypes &CGT, unsigned FLen)
28 : DefaultABIInfo(CGT), FLen(FLen) {}
29
30 void computeInfo(CGFunctionInfo &FI) const override;
31 ABIArgInfo classifyArgumentType(QualType Ty, int &ArgGPRsLeft,
32 int &ArgFPRsLeft,
33 bool isReturnType = false) const;
35
36 RValue EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
37 AggValueSlot Slot) const override;
38};
39
40} // end anonymous namespace
41
42void CSKYABIInfo::computeInfo(CGFunctionInfo &FI) const {
43 QualType RetTy = FI.getReturnType();
44 if (!getCXXABI().classifyReturnType(FI))
46
47 bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect;
48
49 // We must track the number of GPRs used in order to conform to the CSKY
50 // ABI, as integer scalars passed in registers should have signext/zeroext
51 // when promoted.
52 int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs;
53 int ArgFPRsLeft = FLen ? NumArgFPRs : 0;
54
55 for (auto &ArgInfo : FI.arguments()) {
56 ArgInfo.info = classifyArgumentType(ArgInfo.type, ArgGPRsLeft, ArgFPRsLeft);
57 }
58}
59
60RValue CSKYABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
61 QualType Ty, AggValueSlot Slot) const {
62 CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8);
63
64 // Empty records are ignored for parameter passing purposes.
65 if (isEmptyRecord(getContext(), Ty, true))
66 return Slot.asRValue();
67
68 auto TInfo = getContext().getTypeInfoInChars(Ty);
69
70 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, false, TInfo, SlotSize,
71 /*AllowHigherAlign=*/true, Slot);
72}
73
74ABIArgInfo CSKYABIInfo::classifyArgumentType(QualType Ty, int &ArgGPRsLeft,
75 int &ArgFPRsLeft,
76 bool isReturnType) const {
77 assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow");
79
80 // Structures with either a non-trivial destructor or a non-trivial
81 // copy constructor are always passed indirectly.
82 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
83 if (ArgGPRsLeft)
84 ArgGPRsLeft -= 1;
85 return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
87 }
88
89 // Ignore empty structs/unions.
90 if (isEmptyRecord(getContext(), Ty, true))
91 return ABIArgInfo::getIgnore();
92
93 if (!Ty->getAsUnionType())
94 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
95 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
96
97 uint64_t Size = getContext().getTypeSize(Ty);
98 // Pass floating point values via FPRs if possible.
99 if (Ty->isFloatingType() && !Ty->isComplexType() && FLen >= Size &&
100 ArgFPRsLeft) {
101 ArgFPRsLeft--;
102 return ABIArgInfo::getDirect();
103 }
104
105 // Complex types for the hard float ABI must be passed direct rather than
106 // using CoerceAndExpand.
107 if (Ty->isComplexType() && FLen && !isReturnType) {
108 QualType EltTy = Ty->castAs<ComplexType>()->getElementType();
109 if (getContext().getTypeSize(EltTy) <= FLen) {
110 ArgFPRsLeft -= 2;
111 return ABIArgInfo::getDirect();
112 }
113 }
114
115 if (!isAggregateTypeForABI(Ty)) {
116 // Treat an enum type as its underlying type.
117 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
118 Ty = EnumTy->getDecl()->getIntegerType();
119
120 // All integral types are promoted to XLen width, unless passed on the
121 // stack.
122 if (Size < XLen && Ty->isIntegralOrEnumerationType())
123 return ABIArgInfo::getExtend(Ty);
124
125 if (const auto *EIT = Ty->getAs<BitIntType>()) {
126 if (EIT->getNumBits() < XLen)
127 return ABIArgInfo::getExtend(Ty);
128 }
129
130 return ABIArgInfo::getDirect();
131 }
132
133 // For argument type, the first 4*XLen parts of aggregate will be passed
134 // in registers, and the rest will be passed in stack.
135 // So we can coerce to integers directly and let backend handle it correctly.
136 // For return type, aggregate which <= 2*XLen will be returned in registers.
137 // Otherwise, aggregate will be returned indirectly.
138 if (!isReturnType || (isReturnType && Size <= 2 * XLen)) {
139 if (Size <= XLen) {
141 llvm::IntegerType::get(getVMContext(), XLen));
142 } else {
143 return ABIArgInfo::getDirect(llvm::ArrayType::get(
144 llvm::IntegerType::get(getVMContext(), XLen), (Size + 31) / XLen));
145 }
146 }
147 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
148}
149
150ABIArgInfo CSKYABIInfo::classifyReturnType(QualType RetTy) const {
151 if (RetTy->isVoidType())
152 return ABIArgInfo::getIgnore();
153
154 int ArgGPRsLeft = 2;
155 int ArgFPRsLeft = FLen ? 1 : 0;
156
157 // The rules for return and argument types are the same, so defer to
158 // classifyArgumentType.
159 return classifyArgumentType(RetTy, ArgGPRsLeft, ArgFPRsLeft, true);
160}
161
162namespace {
163class CSKYTargetCodeGenInfo : public TargetCodeGenInfo {
164public:
165 CSKYTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned FLen)
166 : TargetCodeGenInfo(std::make_unique<CSKYABIInfo>(CGT, FLen)) {}
167};
168} // end anonymous namespace
169
170std::unique_ptr<TargetCodeGenInfo>
172 return std::make_unique<CSKYTargetCodeGenInfo>(CGM.getTypes(), FLen);
173}
A fixed int type of a specified bitwidth.
Definition: Type.h:7633
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition: CharUnits.h:63
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)
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()
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.
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
Complex values, per C99 6.2.5p11.
Definition: Type.h:3134
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of enums.
Definition: Type.h:5991
A (possibly-)qualified type.
Definition: Type.h:941
The base class of the type hierarchy.
Definition: Type.h:1829
bool isVoidType() const
Definition: Type.h:8319
const RecordType * getAsUnionType() const
NOTE: getAs*ArrayType are methods on ASTContext.
Definition: Type.cpp:740
bool isComplexType() const
isComplexType() does not include complex integers (a GCC extension).
Definition: Type.cpp:677
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:8607
bool isFloatingType() const
Definition: Type.cpp:2249
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:8540
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)
const Type * isSingleElementStruct(QualType T, ASTContext &Context)
isSingleElementStruct - Determine if a structure is a "single element struct", i.e.
QualType useFirstFieldIfTransparentUnion(QualType Ty)
Pass transparent unions as if they were the type of the first element.
bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays, bool AsIfNoUniqueAddr=false)
isEmptyRecord - Return true iff a structure contains only empty fields.
std::unique_ptr< TargetCodeGenInfo > createCSKYTargetCodeGenInfo(CodeGenModule &CGM, unsigned FLen)
Definition: CSKY.cpp:171
The JSON file list parser is used to communicate input to InstallAPI.
unsigned long uint64_t