1//===-- RISCVCallingConv.cpp - RISC-V Custom CC Routines ------------------===//
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// This file contains the custom routines for the RISC-V Calling Convention.
10//
11//===----------------------------------------------------------------------===//
12
13#include "RISCVCallingConv.h"
14#include "RISCVMachineFunctionInfo.h"
15#include "RISCVSubtarget.h"
16#include "llvm/IR/DataLayout.h"
17#include "llvm/MC/MCRegister.h"
18
19using namespace llvm;
20
21// This does not have the regular `CCAssignFn` signature, it has an extra
22// `bool IsRet` parameter.
23static bool CC_RISCV_Impl(unsigned ValNo, MVT ValVT, MVT LocVT,
24 CCValAssign::LocInfo LocInfo,
25 ISD::ArgFlagsTy ArgFlags, Type *OrigTy,
26 CCState &State, bool IsRet);
27
28/// Used for assigning arguments with CallingConvention::GHC
29static CCAssignFn CC_RISCV_GHC;
30
31/// Used for assigning arguments with CallingConvention::Fast
32static CCAssignFn CC_RISCV_FastCC;
33
34bool llvm::CC_RISCV(unsigned ValNo, MVT ValVT, MVT LocVT,
35 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
36 Type *OrigTy, CCState &State) {
37 if (State.getCallingConv() == CallingConv::GHC)
38 return CC_RISCV_GHC(ValNo, ValVT, LocVT, LocInfo, ArgFlags, OrigTy, State);
39
40 if (State.getCallingConv() == CallingConv::Fast)
41 return CC_RISCV_FastCC(ValNo, ValVT, LocVT, LocInfo, ArgFlags, OrigTy,
42 State);
43
44 // For all other cases, use the standard calling convention
45 return CC_RISCV_Impl(ValNo, ValVT, LocVT, LocInfo, ArgFlags, OrigTy, State,
46 /*IsRet=*/false);
47}
48
49bool llvm::RetCC_RISCV(unsigned ValNo, MVT ValVT, MVT LocVT,
50 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
51 Type *OrigTy, CCState &State) {
52 // Always use the standard calling convention.
53 return CC_RISCV_Impl(ValNo, ValVT, LocVT, LocInfo, ArgFlags, OrigTy, State,
54 /*IsRet=*/true);
55}
56
57// Calling Convention Implementation.
58// The expectations for frontend ABI lowering vary from target to target.
59// Ideally, an LLVM frontend would be able to avoid worrying about many ABI
60// details, but this is a longer term goal. For now, we simply try to keep the
61// role of the frontend as simple and well-defined as possible. The rules can
62// be summarised as:
63// * Never split up large scalar arguments. We handle them here.
64// * If a hardfloat calling convention is being used, and the struct may be
65// passed in a pair of registers (fp+fp, int+fp), and both registers are
66// available, then pass as two separate arguments. If either the GPRs or FPRs
67// are exhausted, then pass according to the rule below.
68// * If a struct could never be passed in registers or directly in a stack
69// slot (as it is larger than 2*XLEN and the floating point rules don't
70// apply), then pass it using a pointer with the byval attribute.
71// * If a struct is less than 2*XLEN, then coerce to either a two-element
72// word-sized array or a 2*XLEN scalar (depending on alignment).
73// * The frontend can determine whether a struct is returned by reference or
74// not based on its size and fields. If it will be returned by reference, the
75// frontend must modify the prototype so a pointer with the sret annotation is
76// passed as the first argument. This is not necessary for large scalar
77// returns.
78// * Struct return values and varargs should be coerced to structs containing
79// register-size fields in the same situations they would be for fixed
80// arguments.
81
82static const MCPhysReg ArgFPR16s[] = {RISCV::F10_H, RISCV::F11_H, RISCV::F12_H,
83 RISCV::F13_H, RISCV::F14_H, RISCV::F15_H,
84 RISCV::F16_H, RISCV::F17_H};
85static const MCPhysReg ArgFPR32s[] = {RISCV::F10_F, RISCV::F11_F, RISCV::F12_F,
86 RISCV::F13_F, RISCV::F14_F, RISCV::F15_F,
87 RISCV::F16_F, RISCV::F17_F};
88static const MCPhysReg ArgFPR64s[] = {RISCV::F10_D, RISCV::F11_D, RISCV::F12_D,
89 RISCV::F13_D, RISCV::F14_D, RISCV::F15_D,
90 RISCV::F16_D, RISCV::F17_D};
91static const MCPhysReg ArgFPR128s[] = {RISCV::F10_Q, RISCV::F11_Q, RISCV::F12_Q,
92 RISCV::F13_Q, RISCV::F14_Q, RISCV::F15_Q,
93 RISCV::F16_Q, RISCV::F17_Q};
94
95// This is an interim calling convention and it may be changed in the future.
96static const MCPhysReg ArgVRs[] = {
97 RISCV::V8, RISCV::V9, RISCV::V10, RISCV::V11, RISCV::V12, RISCV::V13,
98 RISCV::V14, RISCV::V15, RISCV::V16, RISCV::V17, RISCV::V18, RISCV::V19,
99 RISCV::V20, RISCV::V21, RISCV::V22, RISCV::V23};
100static const MCPhysReg ArgVRM2s[] = {RISCV::V8M2, RISCV::V10M2, RISCV::V12M2,
101 RISCV::V14M2, RISCV::V16M2, RISCV::V18M2,
102 RISCV::V20M2, RISCV::V22M2};
103static const MCPhysReg ArgVRM4s[] = {RISCV::V8M4, RISCV::V12M4, RISCV::V16M4,
104 RISCV::V20M4};
105static const MCPhysReg ArgVRM8s[] = {RISCV::V8M8, RISCV::V16M8};
106static const MCPhysReg ArgVRN2M1s[] = {
107 RISCV::V8_V9, RISCV::V9_V10, RISCV::V10_V11, RISCV::V11_V12,
108 RISCV::V12_V13, RISCV::V13_V14, RISCV::V14_V15, RISCV::V15_V16,
109 RISCV::V16_V17, RISCV::V17_V18, RISCV::V18_V19, RISCV::V19_V20,
110 RISCV::V20_V21, RISCV::V21_V22, RISCV::V22_V23};
111static const MCPhysReg ArgVRN3M1s[] = {
112 RISCV::V8_V9_V10, RISCV::V9_V10_V11, RISCV::V10_V11_V12,
113 RISCV::V11_V12_V13, RISCV::V12_V13_V14, RISCV::V13_V14_V15,
114 RISCV::V14_V15_V16, RISCV::V15_V16_V17, RISCV::V16_V17_V18,
115 RISCV::V17_V18_V19, RISCV::V18_V19_V20, RISCV::V19_V20_V21,
116 RISCV::V20_V21_V22, RISCV::V21_V22_V23};
117static const MCPhysReg ArgVRN4M1s[] = {
118 RISCV::V8_V9_V10_V11, RISCV::V9_V10_V11_V12, RISCV::V10_V11_V12_V13,
119 RISCV::V11_V12_V13_V14, RISCV::V12_V13_V14_V15, RISCV::V13_V14_V15_V16,
120 RISCV::V14_V15_V16_V17, RISCV::V15_V16_V17_V18, RISCV::V16_V17_V18_V19,
121 RISCV::V17_V18_V19_V20, RISCV::V18_V19_V20_V21, RISCV::V19_V20_V21_V22,
122 RISCV::V20_V21_V22_V23};
123static const MCPhysReg ArgVRN5M1s[] = {
124 RISCV::V8_V9_V10_V11_V12, RISCV::V9_V10_V11_V12_V13,
125 RISCV::V10_V11_V12_V13_V14, RISCV::V11_V12_V13_V14_V15,
126 RISCV::V12_V13_V14_V15_V16, RISCV::V13_V14_V15_V16_V17,
127 RISCV::V14_V15_V16_V17_V18, RISCV::V15_V16_V17_V18_V19,
128 RISCV::V16_V17_V18_V19_V20, RISCV::V17_V18_V19_V20_V21,
129 RISCV::V18_V19_V20_V21_V22, RISCV::V19_V20_V21_V22_V23};
130static const MCPhysReg ArgVRN6M1s[] = {
131 RISCV::V8_V9_V10_V11_V12_V13, RISCV::V9_V10_V11_V12_V13_V14,
132 RISCV::V10_V11_V12_V13_V14_V15, RISCV::V11_V12_V13_V14_V15_V16,
133 RISCV::V12_V13_V14_V15_V16_V17, RISCV::V13_V14_V15_V16_V17_V18,
134 RISCV::V14_V15_V16_V17_V18_V19, RISCV::V15_V16_V17_V18_V19_V20,
135 RISCV::V16_V17_V18_V19_V20_V21, RISCV::V17_V18_V19_V20_V21_V22,
136 RISCV::V18_V19_V20_V21_V22_V23};
137static const MCPhysReg ArgVRN7M1s[] = {
138 RISCV::V8_V9_V10_V11_V12_V13_V14, RISCV::V9_V10_V11_V12_V13_V14_V15,
139 RISCV::V10_V11_V12_V13_V14_V15_V16, RISCV::V11_V12_V13_V14_V15_V16_V17,
140 RISCV::V12_V13_V14_V15_V16_V17_V18, RISCV::V13_V14_V15_V16_V17_V18_V19,
141 RISCV::V14_V15_V16_V17_V18_V19_V20, RISCV::V15_V16_V17_V18_V19_V20_V21,
142 RISCV::V16_V17_V18_V19_V20_V21_V22, RISCV::V17_V18_V19_V20_V21_V22_V23};
143static const MCPhysReg ArgVRN8M1s[] = {RISCV::V8_V9_V10_V11_V12_V13_V14_V15,
144 RISCV::V9_V10_V11_V12_V13_V14_V15_V16,
145 RISCV::V10_V11_V12_V13_V14_V15_V16_V17,
146 RISCV::V11_V12_V13_V14_V15_V16_V17_V18,
147 RISCV::V12_V13_V14_V15_V16_V17_V18_V19,
148 RISCV::V13_V14_V15_V16_V17_V18_V19_V20,
149 RISCV::V14_V15_V16_V17_V18_V19_V20_V21,
150 RISCV::V15_V16_V17_V18_V19_V20_V21_V22,
151 RISCV::V16_V17_V18_V19_V20_V21_V22_V23};
152static const MCPhysReg ArgVRN2M2s[] = {RISCV::V8M2_V10M2, RISCV::V10M2_V12M2,
153 RISCV::V12M2_V14M2, RISCV::V14M2_V16M2,
154 RISCV::V16M2_V18M2, RISCV::V18M2_V20M2,
155 RISCV::V20M2_V22M2};
156static const MCPhysReg ArgVRN3M2s[] = {
157 RISCV::V8M2_V10M2_V12M2, RISCV::V10M2_V12M2_V14M2,
158 RISCV::V12M2_V14M2_V16M2, RISCV::V14M2_V16M2_V18M2,
159 RISCV::V16M2_V18M2_V20M2, RISCV::V18M2_V20M2_V22M2};
160static const MCPhysReg ArgVRN4M2s[] = {
161 RISCV::V8M2_V10M2_V12M2_V14M2, RISCV::V10M2_V12M2_V14M2_V16M2,
162 RISCV::V12M2_V14M2_V16M2_V18M2, RISCV::V14M2_V16M2_V18M2_V20M2,
163 RISCV::V16M2_V18M2_V20M2_V22M2};
164static const MCPhysReg ArgVRN2M4s[] = {RISCV::V8M4_V12M4, RISCV::V12M4_V16M4,
165 RISCV::V16M4_V20M4};
166
167ArrayRef<MCPhysReg> RISCV::getArgGPRs(const RISCVSubtarget &STI) {
168 RISCVABI::ABI ABI = STI.getTargetABI();
169
170 // The GPRs used for passing arguments in the ILP32* and LP64* ABIs, except
171 // the ILP32E ABI.
172 static const MCPhysReg ArgIGPRs[] = {RISCV::X10, RISCV::X11, RISCV::X12,
173 RISCV::X13, RISCV::X14, RISCV::X15,
174 RISCV::X16, RISCV::X17};
175 // The GPRs used for passing arguments in the ILP32E/LP64E ABI.
176 static const MCPhysReg ArgEGPRs[] = {RISCV::X10, RISCV::X11, RISCV::X12,
177 RISCV::X13, RISCV::X14, RISCV::X15};
178
179 if (RISCVABI::isRVE(TargetABI: ABI))
180 return ArrayRef(ArgEGPRs);
181
182 return ArrayRef(ArgIGPRs);
183}
184
185ArrayRef<MCPhysReg> RISCV::getArgFPRs(const RISCVSubtarget &STI) {
186 if (STI.isSoftFPABI() || !STI.hasStdExtF())
187 return {};
188
189 if (STI.hasStdExtQ())
190 return ArrayRef(ArgFPR128s);
191
192 if (STI.hasStdExtD())
193 return ArrayRef(ArgFPR64s);
194
195 return ArrayRef(ArgFPR32s);
196}
197
198ArrayRef<MCPhysReg> RISCV::getArgVRs(const RISCVSubtarget &STI) {
199 if (STI.hasVInstructions())
200 return ArrayRef(ArgVRs);
201
202 return {};
203}
204
205static ArrayRef<MCPhysReg> getArgGPR16s(const RISCVABI::ABI ABI) {
206 // The GPRs used for passing arguments in the ILP32* and LP64* ABIs, except
207 // the ILP32E ABI.
208 static const MCPhysReg ArgIGPRs[] = {RISCV::X10_H, RISCV::X11_H, RISCV::X12_H,
209 RISCV::X13_H, RISCV::X14_H, RISCV::X15_H,
210 RISCV::X16_H, RISCV::X17_H};
211 // The GPRs used for passing arguments in the ILP32E/LP64E ABI.
212 static const MCPhysReg ArgEGPRs[] = {RISCV::X10_H, RISCV::X11_H,
213 RISCV::X12_H, RISCV::X13_H,
214 RISCV::X14_H, RISCV::X15_H};
215
216 if (RISCVABI::isRVE(TargetABI: ABI))
217 return ArrayRef(ArgEGPRs);
218
219 return ArrayRef(ArgIGPRs);
220}
221
222static ArrayRef<MCPhysReg> getArgGPR32s(const RISCVABI::ABI ABI) {
223 // The GPRs used for passing arguments in the ILP32* and LP64* ABIs, except
224 // the ILP32E ABI.
225 static const MCPhysReg ArgIGPRs[] = {RISCV::X10_W, RISCV::X11_W, RISCV::X12_W,
226 RISCV::X13_W, RISCV::X14_W, RISCV::X15_W,
227 RISCV::X16_W, RISCV::X17_W};
228 // The GPRs used for passing arguments in the ILP32E/LP64E ABI.
229 static const MCPhysReg ArgEGPRs[] = {RISCV::X10_W, RISCV::X11_W,
230 RISCV::X12_W, RISCV::X13_W,
231 RISCV::X14_W, RISCV::X15_W};
232
233 if (RISCVABI::isRVE(TargetABI: ABI))
234 return ArrayRef(ArgEGPRs);
235
236 return ArrayRef(ArgIGPRs);
237}
238
239static ArrayRef<MCPhysReg> getFastCCArgGPRs(const RISCVABI::ABI ABI) {
240 // The GPRs used for passing arguments in the FastCC, X5 and X6 might be used
241 // for save-restore libcall, so we don't use them.
242 // Don't use X7 for fastcc, since Zicfilp uses X7 as the label register.
243 static const MCPhysReg FastCCIGPRs[] = {
244 RISCV::X10, RISCV::X11, RISCV::X12, RISCV::X13, RISCV::X14, RISCV::X15,
245 RISCV::X16, RISCV::X17, RISCV::X28, RISCV::X29, RISCV::X30, RISCV::X31};
246
247 // The GPRs used for passing arguments in the FastCC when using ILP32E/LP64E.
248 static const MCPhysReg FastCCEGPRs[] = {RISCV::X10, RISCV::X11, RISCV::X12,
249 RISCV::X13, RISCV::X14, RISCV::X15};
250
251 if (RISCVABI::isRVE(TargetABI: ABI))
252 return ArrayRef(FastCCEGPRs);
253
254 return ArrayRef(FastCCIGPRs);
255}
256
257static ArrayRef<MCPhysReg> getFastCCArgGPRF16s(const RISCVABI::ABI ABI) {
258 // The GPRs used for passing arguments in the FastCC, X5 and X6 might be used
259 // for save-restore libcall, so we don't use them.
260 // Don't use X7 for fastcc, since Zicfilp uses X7 as the label register.
261 static const MCPhysReg FastCCIGPRs[] = {
262 RISCV::X10_H, RISCV::X11_H, RISCV::X12_H, RISCV::X13_H,
263 RISCV::X14_H, RISCV::X15_H, RISCV::X16_H, RISCV::X17_H,
264 RISCV::X28_H, RISCV::X29_H, RISCV::X30_H, RISCV::X31_H};
265
266 // The GPRs used for passing arguments in the FastCC when using ILP32E/LP64E.
267 static const MCPhysReg FastCCEGPRs[] = {RISCV::X10_H, RISCV::X11_H,
268 RISCV::X12_H, RISCV::X13_H,
269 RISCV::X14_H, RISCV::X15_H};
270
271 if (RISCVABI::isRVE(TargetABI: ABI))
272 return ArrayRef(FastCCEGPRs);
273
274 return ArrayRef(FastCCIGPRs);
275}
276
277static ArrayRef<MCPhysReg> getFastCCArgGPRF32s(const RISCVABI::ABI ABI) {
278 // The GPRs used for passing arguments in the FastCC, X5 and X6 might be used
279 // for save-restore libcall, so we don't use them.
280 // Don't use X7 for fastcc, since Zicfilp uses X7 as the label register.
281 static const MCPhysReg FastCCIGPRs[] = {
282 RISCV::X10_W, RISCV::X11_W, RISCV::X12_W, RISCV::X13_W,
283 RISCV::X14_W, RISCV::X15_W, RISCV::X16_W, RISCV::X17_W,
284 RISCV::X28_W, RISCV::X29_W, RISCV::X30_W, RISCV::X31_W};
285
286 // The GPRs used for passing arguments in the FastCC when using ILP32E/LP64E.
287 static const MCPhysReg FastCCEGPRs[] = {RISCV::X10_W, RISCV::X11_W,
288 RISCV::X12_W, RISCV::X13_W,
289 RISCV::X14_W, RISCV::X15_W};
290
291 if (RISCVABI::isRVE(TargetABI: ABI))
292 return ArrayRef(FastCCEGPRs);
293
294 return ArrayRef(FastCCIGPRs);
295}
296
297// Pass a 2*XLEN argument that has been split into two XLEN values through
298// registers or the stack as necessary.
299static bool CC_RISCVAssign2XLen(CCState &State, CCValAssign VA1,
300 ISD::ArgFlagsTy ArgFlags1, unsigned ValNo2,
301 MVT ValVT2, MVT LocVT2,
302 ISD::ArgFlagsTy ArgFlags2,
303 const RISCVSubtarget &Subtarget) {
304 unsigned XLen = Subtarget.getXLen();
305 unsigned XLenInBytes = XLen / 8;
306
307 ArrayRef<MCPhysReg> ArgGPRs = RISCV::getArgGPRs(STI: Subtarget);
308
309 if (MCRegister Reg = State.AllocateReg(Regs: ArgGPRs)) {
310 // At least one half can be passed via register.
311 State.addLoc(V: CCValAssign::getReg(ValNo: VA1.getValNo(), ValVT: VA1.getValVT(), Reg,
312 LocVT: VA1.getLocVT(), HTP: CCValAssign::Full));
313 } else {
314 // Both halves must be passed on the stack, with proper alignment.
315 // TODO: To be compatible with GCC's behaviors, we clamp the alignment by
316 // the ABI stack alignment. This behavior may be changed when RV32E/ILP32E
317 // is ratified.
318 Align StackAlign =
319 std::min(a: RISCVABI::getStackAlignment(TargetABI: Subtarget.getTargetABI()),
320 b: std::max(a: Align(XLenInBytes), b: ArgFlags1.getNonZeroOrigAlign()));
321 State.addLoc(
322 V: CCValAssign::getMem(ValNo: VA1.getValNo(), ValVT: VA1.getValVT(),
323 Offset: State.AllocateStack(Size: XLenInBytes, Alignment: StackAlign),
324 LocVT: VA1.getLocVT(), HTP: CCValAssign::Full));
325 State.addLoc(V: CCValAssign::getMem(
326 ValNo: ValNo2, ValVT: ValVT2, Offset: State.AllocateStack(Size: XLenInBytes, Alignment: Align(XLenInBytes)),
327 LocVT: LocVT2, HTP: CCValAssign::Full));
328 return false;
329 }
330
331 if (MCRegister Reg = State.AllocateReg(Regs: ArgGPRs)) {
332 // The second half can also be passed via register.
333 State.addLoc(
334 V: CCValAssign::getReg(ValNo: ValNo2, ValVT: ValVT2, Reg, LocVT: LocVT2, HTP: CCValAssign::Full));
335 } else {
336 // The second half is passed via the stack, without additional alignment.
337 State.addLoc(V: CCValAssign::getMem(
338 ValNo: ValNo2, ValVT: ValVT2, Offset: State.AllocateStack(Size: XLenInBytes, Alignment: Align(XLenInBytes)),
339 LocVT: LocVT2, HTP: CCValAssign::Full));
340 }
341
342 return false;
343}
344
345static MCRegister allocateRVVReg(MVT LocVT, unsigned ValNo, CCState &State,
346 const RISCVTargetLowering &TLI) {
347 const TargetRegisterClass *RC = TLI.getRegClassFor(VT: LocVT);
348 if (RC == &RISCV::VRRegClass) {
349 // Assign the first mask argument to V0.
350 // This is an interim calling convention and it may be changed in the
351 // future.
352 if (LocVT.getVectorElementType() == MVT::i1)
353 if (MCRegister Reg = State.AllocateReg(Reg: RISCV::V0))
354 return Reg;
355 return State.AllocateReg(Regs: ArgVRs);
356 }
357 if (RC == &RISCV::VRM2RegClass)
358 return State.AllocateReg(Regs: ArgVRM2s);
359 if (RC == &RISCV::VRM4RegClass)
360 return State.AllocateReg(Regs: ArgVRM4s);
361 if (RC == &RISCV::VRM8RegClass)
362 return State.AllocateReg(Regs: ArgVRM8s);
363 if (RC == &RISCV::VRN2M1RegClass)
364 return State.AllocateReg(Regs: ArgVRN2M1s);
365 if (RC == &RISCV::VRN3M1RegClass)
366 return State.AllocateReg(Regs: ArgVRN3M1s);
367 if (RC == &RISCV::VRN4M1RegClass)
368 return State.AllocateReg(Regs: ArgVRN4M1s);
369 if (RC == &RISCV::VRN5M1RegClass)
370 return State.AllocateReg(Regs: ArgVRN5M1s);
371 if (RC == &RISCV::VRN6M1RegClass)
372 return State.AllocateReg(Regs: ArgVRN6M1s);
373 if (RC == &RISCV::VRN7M1RegClass)
374 return State.AllocateReg(Regs: ArgVRN7M1s);
375 if (RC == &RISCV::VRN8M1RegClass)
376 return State.AllocateReg(Regs: ArgVRN8M1s);
377 if (RC == &RISCV::VRN2M2RegClass)
378 return State.AllocateReg(Regs: ArgVRN2M2s);
379 if (RC == &RISCV::VRN3M2RegClass)
380 return State.AllocateReg(Regs: ArgVRN3M2s);
381 if (RC == &RISCV::VRN4M2RegClass)
382 return State.AllocateReg(Regs: ArgVRN4M2s);
383 if (RC == &RISCV::VRN2M4RegClass)
384 return State.AllocateReg(Regs: ArgVRN2M4s);
385 llvm_unreachable("Unhandled register class for ValueType");
386}
387
388// Implements the RISC-V calling convention. Returns true upon failure.
389//
390// This has a slightly different signature to CCAssignFn - it adds `bool IsRet`.
391static bool CC_RISCV_Impl(unsigned ValNo, MVT ValVT, MVT LocVT,
392 CCValAssign::LocInfo LocInfo,
393 ISD::ArgFlagsTy ArgFlags, Type *OrigTy,
394 CCState &State, bool IsRet) {
395 assert(ValVT == LocVT && "Expected ValVT and LocVT to match");
396 const MachineFunction &MF = State.getMachineFunction();
397 const DataLayout &DL = MF.getDataLayout();
398 const RISCVSubtarget &Subtarget = MF.getSubtarget<RISCVSubtarget>();
399 const RISCVTargetLowering &TLI = *Subtarget.getTargetLowering();
400
401 unsigned XLen = Subtarget.getXLen();
402 MVT XLenVT = Subtarget.getXLenVT();
403
404 if (ArgFlags.isNest()) {
405 // Static chain parameter must not be passed in normal argument registers,
406 // so we assign t2/t3 for it as done in GCC's
407 // __builtin_call_with_static_chain
408 bool HasCFBranch =
409 MF.getInfo<RISCVMachineFunctionInfo>()->hasCFProtectionBranch();
410
411 // Normal: t2, Branch control flow protection: t3
412 const auto StaticChainReg = HasCFBranch ? RISCV::X28 : RISCV::X7;
413
414 RISCVABI::ABI ABI = Subtarget.getTargetABI();
415 if (HasCFBranch && RISCVABI::isRVE(TargetABI: ABI))
416 reportFatalUsageError(
417 reason: "Nested functions with control flow protection are not "
418 "usable with ILP32E or LP64E ABI.");
419 if (MCRegister Reg = State.AllocateReg(Reg: StaticChainReg)) {
420 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
421 return false;
422 }
423 }
424
425 // Any return value split in to more than two values can't be returned
426 // directly. Vectors are returned via the available vector registers.
427 if ((!LocVT.isVector() || Subtarget.isPExtPackedType(VT: LocVT)) && IsRet &&
428 ValNo > 1)
429 return true;
430
431 // Double wide packed types require 2 GPRs so we can only return 1 of them.
432 if (Subtarget.isPExtPackedDoubleType(VT: LocVT) && IsRet && ValNo > 0)
433 return true;
434
435 // AllowFPRForF16_F32 if targeting an FLEN>=32 ABI and the argument isn't
436 // variadic.
437 bool AllowFPRForF16_F32 = false;
438 // UseFPRForF64 if targeting an FLEN>=64 ABI and the argument isn't variadic.
439 bool AllowFPRForF64 = false;
440
441 RISCVABI::ABI ABI = Subtarget.getTargetABI();
442 assert(ABI != RISCVABI::ABI::Unknown && !RISCVABI::isPurecap(ABI) &&
443 "Unexpected ABI");
444 bool IsDoubleFPABI = RISCVABI::isDoubleFP(TargetABI: ABI);
445 if (IsDoubleFPABI || RISCVABI::isSingleFP(TargetABI: ABI))
446 AllowFPRForF16_F32 = !ArgFlags.isVarArg();
447 if (IsDoubleFPABI)
448 AllowFPRForF64 = !ArgFlags.isVarArg();
449
450 if ((LocVT == MVT::f16 || LocVT == MVT::bf16) && AllowFPRForF16_F32) {
451 if (MCRegister Reg = State.AllocateReg(Regs: ArgFPR16s)) {
452 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
453 return false;
454 }
455 }
456
457 if (LocVT == MVT::f32 && AllowFPRForF16_F32) {
458 if (MCRegister Reg = State.AllocateReg(Regs: ArgFPR32s)) {
459 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
460 return false;
461 }
462 }
463
464 if (LocVT == MVT::f64 && AllowFPRForF64) {
465 if (MCRegister Reg = State.AllocateReg(Regs: ArgFPR64s)) {
466 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
467 return false;
468 }
469 }
470
471 if (LocVT == MVT::f16 && Subtarget.hasStdExtZhinxmin()) {
472 if (MCRegister Reg = State.AllocateReg(Regs: getArgGPR16s(ABI))) {
473 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
474 return false;
475 }
476 }
477
478 if (LocVT == MVT::f32 && Subtarget.hasStdExtZfinx()) {
479 if (MCRegister Reg = State.AllocateReg(Regs: getArgGPR32s(ABI))) {
480 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
481 return false;
482 }
483 }
484
485 ArrayRef<MCPhysReg> ArgGPRs = RISCV::getArgGPRs(STI: Subtarget);
486
487 // Zdinx use GPR without a bitcast when possible.
488 if (LocVT == MVT::f64 && XLen == 64 && Subtarget.hasStdExtZdinx()) {
489 if (MCRegister Reg = State.AllocateReg(Regs: ArgGPRs)) {
490 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
491 return false;
492 }
493 }
494
495 // FP smaller than XLen, uses custom GPR.
496 if (LocVT == MVT::f16 || LocVT == MVT::bf16 ||
497 (LocVT == MVT::f32 && XLen == 64)) {
498 if (MCRegister Reg = State.AllocateReg(Regs: ArgGPRs)) {
499 LocVT = XLenVT;
500 State.addLoc(
501 V: CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
502 return false;
503 }
504 }
505
506 // Bitcast FP to GPR if we can use a GPR register.
507 if ((XLen == 32 && LocVT == MVT::f32) || (XLen == 64 && LocVT == MVT::f64)) {
508 if (MCRegister Reg = State.AllocateReg(Regs: ArgGPRs)) {
509 LocVT = XLenVT;
510 LocInfo = CCValAssign::BCvt;
511 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
512 return false;
513 }
514 }
515
516 // If this is a variadic argument, the RISC-V calling convention requires
517 // that it is assigned an 'even' or 'aligned' register if it has 8-byte
518 // alignment (RV32) or 16-byte alignment (RV64). An aligned register should
519 // be used regardless of whether the original argument was split during
520 // legalisation or not. The argument will not be passed by registers if the
521 // original type is larger than 2*XLEN, so the register alignment rule does
522 // not apply.
523 // TODO: To be compatible with GCC's behaviors, we clamp the alignment by the
524 // ABI stack alignment, so registers are never aligned for ILP32E/LP64E. This
525 // behavior may be changed when RV32E/ILP32E is ratified.
526 unsigned TwoXLenInBytes = (2 * XLen) / 8;
527 Align ArgAlign = std::min(a: ArgFlags.getNonZeroOrigAlign(),
528 b: RISCVABI::getStackAlignment(TargetABI: ABI));
529 if (ArgFlags.isVarArg() && ArgAlign == TwoXLenInBytes &&
530 DL.getTypeAllocSize(Ty: OrigTy) == TwoXLenInBytes) {
531 unsigned RegIdx = State.getFirstUnallocated(Regs: ArgGPRs);
532 // Skip 'odd' register if necessary.
533 if (RegIdx != std::size(cont: ArgGPRs) && RegIdx % 2 == 1)
534 State.AllocateReg(Regs: ArgGPRs);
535 }
536
537 SmallVectorImpl<CCValAssign> &PendingLocs = State.getPendingLocs();
538 SmallVectorImpl<ISD::ArgFlagsTy> &PendingArgFlags =
539 State.getPendingArgFlags();
540
541 assert(PendingLocs.size() == PendingArgFlags.size() &&
542 "PendingLocs and PendingArgFlags out of sync");
543
544 // Handle passing f64 on RV32D with a soft float ABI or when floating point
545 // registers are exhausted. Or 64-bit P extension vectors on RV32.
546 if (XLen == 32 &&
547 (LocVT == MVT::f64 || (Subtarget.isPExtPackedDoubleType(VT: LocVT) &&
548 !ArgFlags.isSplit() && PendingLocs.empty()))) {
549 assert(PendingLocs.empty() &&
550 "Can't lower f64 or P extension vector if it is split");
551 // Depending on available argument GPRS, f64 may be passed in a pair of
552 // GPRs, split between a GPR and the stack, or passed completely on the
553 // stack. LowerCall/LowerFormalArguments/LowerReturn must recognise these
554 // cases.
555 MCRegister Reg = State.AllocateReg(Regs: ArgGPRs);
556 if (!Reg) {
557 int64_t StackOffset = State.AllocateStack(Size: 8, Alignment: Align(8));
558 State.addLoc(
559 V: CCValAssign::getMem(ValNo, ValVT, Offset: StackOffset, LocVT, HTP: LocInfo));
560 return false;
561 }
562 LocVT = MVT::i32;
563 State.addLoc(V: CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
564 MCRegister HiReg = State.AllocateReg(Regs: ArgGPRs);
565 if (HiReg) {
566 State.addLoc(
567 V: CCValAssign::getCustomReg(ValNo, ValVT, Reg: HiReg, LocVT, HTP: LocInfo));
568 } else {
569 int64_t StackOffset = State.AllocateStack(Size: 4, Alignment: Align(4));
570 State.addLoc(
571 V: CCValAssign::getCustomMem(ValNo, ValVT, Offset: StackOffset, LocVT, HTP: LocInfo));
572 }
573 return false;
574 }
575
576 // If the split argument only had two elements, it should be passed directly
577 // in registers or on the stack.
578 if ((LocVT.isScalarInteger() ||
579 (Subtarget.isPExtPackedType(VT: LocVT) && LocVT.getSizeInBits() == XLen)) &&
580 ArgFlags.isSplitEnd() && PendingLocs.size() <= 1) {
581 assert(PendingLocs.size() == 1 && "Unexpected PendingLocs.size()");
582 // Apply the normal calling convention rules to the first half of the
583 // split argument.
584 CCValAssign VA = PendingLocs[0];
585 ISD::ArgFlagsTy AF = PendingArgFlags[0];
586 PendingLocs.clear();
587 PendingArgFlags.clear();
588 return CC_RISCVAssign2XLen(State, VA1: VA, ArgFlags1: AF, ValNo2: ValNo, ValVT2: ValVT, LocVT2: LocVT, ArgFlags2: ArgFlags,
589 Subtarget);
590 }
591
592 // Split arguments might be passed indirectly, so keep track of the pending
593 // values. Split vectors excluding P extension packed vectors(see
594 // isPExtPackedType) are passed via a mix of registers and indirectly, so
595 // treat them as we would any other argument.
596 if ((LocVT.isScalarInteger() || Subtarget.isPExtPackedType(VT: LocVT)) &&
597 (ArgFlags.isSplit() || !PendingLocs.empty())) {
598 PendingLocs.push_back(
599 Elt: CCValAssign::getPending(ValNo, ValVT, LocVT, HTP: LocInfo));
600 PendingArgFlags.push_back(Elt: ArgFlags);
601 if (!ArgFlags.isSplitEnd()) {
602 return false;
603 }
604 }
605
606 // Allocate to a register if possible, or else a stack slot.
607 MCRegister Reg;
608 unsigned StoreSizeBytes = XLen / 8;
609 Align StackAlign = Align(XLen / 8);
610
611 // FIXME: If P extension and V extension are enabled at the same time,
612 // who should go first?
613 if (!Subtarget.isPExtPackedType(VT: LocVT) &&
614 (LocVT.isVector() || LocVT.isRISCVVectorTuple())) {
615 Reg = allocateRVVReg(LocVT, ValNo, State, TLI);
616 if (Reg) {
617 // Fixed-length vectors are located in the corresponding scalable-vector
618 // container types.
619 if (LocVT.isFixedLengthVector()) {
620 LocVT = TLI.getContainerForFixedLengthVector(VT: LocVT);
621 State.addLoc(
622 V: CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
623 return false;
624 }
625 } else {
626 // For return values, the vector must be passed fully via registers or
627 // via the stack.
628 if (IsRet)
629 return true;
630 // Try using a GPR to pass the address
631 if ((Reg = State.AllocateReg(Regs: ArgGPRs))) {
632 LocVT = XLenVT;
633 LocInfo = CCValAssign::Indirect;
634 } else if (LocVT.isScalableVector()) {
635 LocVT = XLenVT;
636 LocInfo = CCValAssign::Indirect;
637 } else {
638 StoreSizeBytes = LocVT.getStoreSize();
639 // Align vectors to their element sizes, being careful for vXi1
640 // vectors.
641 StackAlign = MaybeAlign(LocVT.getScalarSizeInBits() / 8).valueOrOne();
642 }
643 }
644 } else {
645 Reg = State.AllocateReg(Regs: ArgGPRs);
646 }
647
648 int64_t StackOffset =
649 Reg ? 0 : State.AllocateStack(Size: StoreSizeBytes, Alignment: StackAlign);
650
651 // If we reach this point and PendingLocs is non-empty, we must be at the
652 // end of a split argument that must be passed indirectly.
653 if (!PendingLocs.empty()) {
654 assert(ArgFlags.isSplitEnd() && "Expected ArgFlags.isSplitEnd()");
655 assert(PendingLocs.size() > 1 && "Unexpected PendingLocs.size()");
656
657 for (auto &It : PendingLocs) {
658 if (Reg)
659 State.addLoc(V: CCValAssign::getReg(ValNo: It.getValNo(), ValVT: It.getValVT(), Reg,
660 LocVT: XLenVT, HTP: CCValAssign::Indirect));
661 else
662 State.addLoc(V: CCValAssign::getMem(ValNo: It.getValNo(), ValVT: It.getValVT(),
663 Offset: StackOffset, LocVT: XLenVT,
664 HTP: CCValAssign::Indirect));
665 }
666 PendingLocs.clear();
667 PendingArgFlags.clear();
668 return false;
669 }
670
671 assert(((LocVT.isFloatingPoint() && !LocVT.isVector()) || LocVT == XLenVT ||
672 Subtarget.isPExtPackedType(LocVT) ||
673 (TLI.getSubtarget().hasVInstructions() &&
674 (LocVT.isVector() || LocVT.isRISCVVectorTuple()))) &&
675 "Expected an XLenVT or vector types at this stage");
676
677 if (Reg) {
678 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
679 return false;
680 }
681
682 State.addLoc(V: CCValAssign::getMem(ValNo, ValVT, Offset: StackOffset, LocVT, HTP: LocInfo));
683 return false;
684}
685
686// FastCC has less than 1% performance improvement for some particular
687// benchmark. But theoretically, it may have benefit for some cases.
688static bool CC_RISCV_FastCC(unsigned ValNo, MVT ValVT, MVT LocVT,
689 CCValAssign::LocInfo LocInfo,
690 ISD::ArgFlagsTy ArgFlags, Type *OrigTy,
691 CCState &State) {
692 const MachineFunction &MF = State.getMachineFunction();
693 const RISCVSubtarget &Subtarget = MF.getSubtarget<RISCVSubtarget>();
694 const RISCVTargetLowering &TLI = *Subtarget.getTargetLowering();
695 RISCVABI::ABI ABI = Subtarget.getTargetABI();
696
697 if ((LocVT == MVT::f16 && Subtarget.hasStdExtZfhmin()) ||
698 (LocVT == MVT::bf16 && Subtarget.hasStdExtZfbfmin())) {
699 static const MCPhysReg FPR16List[] = {
700 RISCV::F10_H, RISCV::F11_H, RISCV::F12_H, RISCV::F13_H, RISCV::F14_H,
701 RISCV::F15_H, RISCV::F16_H, RISCV::F17_H, RISCV::F0_H, RISCV::F1_H,
702 RISCV::F2_H, RISCV::F3_H, RISCV::F4_H, RISCV::F5_H, RISCV::F6_H,
703 RISCV::F7_H, RISCV::F28_H, RISCV::F29_H, RISCV::F30_H, RISCV::F31_H};
704 if (MCRegister Reg = State.AllocateReg(Regs: FPR16List)) {
705 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
706 return false;
707 }
708 }
709
710 if (LocVT == MVT::f32 && Subtarget.hasStdExtF()) {
711 static const MCPhysReg FPR32List[] = {
712 RISCV::F10_F, RISCV::F11_F, RISCV::F12_F, RISCV::F13_F, RISCV::F14_F,
713 RISCV::F15_F, RISCV::F16_F, RISCV::F17_F, RISCV::F0_F, RISCV::F1_F,
714 RISCV::F2_F, RISCV::F3_F, RISCV::F4_F, RISCV::F5_F, RISCV::F6_F,
715 RISCV::F7_F, RISCV::F28_F, RISCV::F29_F, RISCV::F30_F, RISCV::F31_F};
716 if (MCRegister Reg = State.AllocateReg(Regs: FPR32List)) {
717 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
718 return false;
719 }
720 }
721
722 if (LocVT == MVT::f64 && Subtarget.hasStdExtD()) {
723 static const MCPhysReg FPR64List[] = {
724 RISCV::F10_D, RISCV::F11_D, RISCV::F12_D, RISCV::F13_D, RISCV::F14_D,
725 RISCV::F15_D, RISCV::F16_D, RISCV::F17_D, RISCV::F0_D, RISCV::F1_D,
726 RISCV::F2_D, RISCV::F3_D, RISCV::F4_D, RISCV::F5_D, RISCV::F6_D,
727 RISCV::F7_D, RISCV::F28_D, RISCV::F29_D, RISCV::F30_D, RISCV::F31_D};
728 if (MCRegister Reg = State.AllocateReg(Regs: FPR64List)) {
729 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
730 return false;
731 }
732 }
733
734 MVT XLenVT = Subtarget.getXLenVT();
735
736 // Check if there is an available GPRF16 before hitting the stack.
737 if ((LocVT == MVT::f16 && Subtarget.hasStdExtZhinxmin())) {
738 if (MCRegister Reg = State.AllocateReg(Regs: getFastCCArgGPRF16s(ABI))) {
739 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
740 return false;
741 }
742 }
743
744 // Check if there is an available GPRF32 before hitting the stack.
745 if (LocVT == MVT::f32 && Subtarget.hasStdExtZfinx()) {
746 if (MCRegister Reg = State.AllocateReg(Regs: getFastCCArgGPRF32s(ABI))) {
747 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
748 return false;
749 }
750 }
751
752 // Check if there is an available GPR before hitting the stack.
753 if (LocVT == MVT::f64 && Subtarget.is64Bit() && Subtarget.hasStdExtZdinx()) {
754 if (MCRegister Reg = State.AllocateReg(Regs: getFastCCArgGPRs(ABI))) {
755 if (LocVT.getSizeInBits() != Subtarget.getXLen()) {
756 LocVT = XLenVT;
757 State.addLoc(
758 V: CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
759 return false;
760 }
761 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
762 return false;
763 }
764 }
765
766 ArrayRef<MCPhysReg> ArgGPRs = getFastCCArgGPRs(ABI);
767
768 // FIXME: If P extension and V extension are enabled at the same time,
769 // who should go first?
770 if (!Subtarget.isPExtPackedType(VT: LocVT) && LocVT.isVector()) {
771 if (MCRegister Reg = allocateRVVReg(LocVT: ValVT, ValNo, State, TLI)) {
772 // Fixed-length vectors are located in the corresponding scalable-vector
773 // container types.
774 if (LocVT.isFixedLengthVector()) {
775 LocVT = TLI.getContainerForFixedLengthVector(VT: LocVT);
776 State.addLoc(
777 V: CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
778 return false;
779 }
780 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
781 return false;
782 }
783
784 // Pass scalable vectors indirectly. Pass fixed vectors indirectly if we
785 // have a free GPR.
786 if (LocVT.isScalableVector() ||
787 State.getFirstUnallocated(Regs: ArgGPRs) != ArgGPRs.size()) {
788 LocInfo = CCValAssign::Indirect;
789 LocVT = XLenVT;
790 }
791 }
792
793 // Pass 2*XLen P extension vectors on RV32 in a pair of GPRs, split between a
794 // GPR and the stack, or entirely on the stack.
795 if (Subtarget.isPExtPackedDoubleType(VT: LocVT)) {
796 MCRegister Reg = State.AllocateReg(Regs: ArgGPRs);
797 if (Reg) {
798 LocVT = MVT::i32;
799 State.addLoc(
800 V: CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
801 if (MCRegister HiReg = State.AllocateReg(Regs: ArgGPRs)) {
802 State.addLoc(
803 V: CCValAssign::getCustomReg(ValNo, ValVT, Reg: HiReg, LocVT, HTP: LocInfo));
804 } else {
805 int64_t StackOffset = State.AllocateStack(Size: 4, Alignment: Align(4));
806 State.addLoc(V: CCValAssign::getCustomMem(ValNo, ValVT, Offset: StackOffset, LocVT,
807 HTP: LocInfo));
808 }
809 return false;
810 }
811 }
812
813 if (LocVT == XLenVT || Subtarget.isPExtPackedType(VT: LocVT)) {
814 if (MCRegister Reg = State.AllocateReg(Regs: getFastCCArgGPRs(ABI))) {
815 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
816 return false;
817 }
818 }
819
820 if (LocVT == XLenVT || LocVT == MVT::f16 || LocVT == MVT::bf16 ||
821 LocVT == MVT::f32 || LocVT == MVT::f64 || LocVT.isFixedLengthVector()) {
822 Align StackAlign = MaybeAlign(ValVT.getScalarSizeInBits() / 8).valueOrOne();
823 // P extension packed vectors are GPR sized and need XLen alignment.
824 if (Subtarget.isPExtPackedType(VT: LocVT))
825 StackAlign = Align(Subtarget.getXLen() / 8);
826 int64_t Offset = State.AllocateStack(Size: LocVT.getStoreSize(), Alignment: StackAlign);
827 State.addLoc(V: CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, HTP: LocInfo));
828 return false;
829 }
830
831 return true; // CC didn't match.
832}
833
834static bool CC_RISCV_GHC(unsigned ValNo, MVT ValVT, MVT LocVT,
835 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
836 Type *OrigTy, CCState &State) {
837 if (ArgFlags.isNest()) {
838 report_fatal_error(
839 reason: "Attribute 'nest' is not supported in GHC calling convention");
840 }
841
842 static const MCPhysReg GPRList[] = {
843 RISCV::X9, RISCV::X18, RISCV::X19, RISCV::X20, RISCV::X21, RISCV::X22,
844 RISCV::X23, RISCV::X24, RISCV::X25, RISCV::X26, RISCV::X27};
845
846 if (LocVT == MVT::i32 || LocVT == MVT::i64) {
847 // Pass in STG registers: Base, Sp, Hp, R1, R2, R3, R4, R5, R6, R7, SpLim
848 // s1 s2 s3 s4 s5 s6 s7 s8 s9 s10 s11
849 if (MCRegister Reg = State.AllocateReg(Regs: GPRList)) {
850 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
851 return false;
852 }
853 }
854
855 const RISCVSubtarget &Subtarget =
856 State.getMachineFunction().getSubtarget<RISCVSubtarget>();
857
858 if (LocVT == MVT::f32 && Subtarget.hasStdExtF()) {
859 // Pass in STG registers: F1, ..., F6
860 // fs0 ... fs5
861 static const MCPhysReg FPR32List[] = {RISCV::F8_F, RISCV::F9_F,
862 RISCV::F18_F, RISCV::F19_F,
863 RISCV::F20_F, RISCV::F21_F};
864 if (MCRegister Reg = State.AllocateReg(Regs: FPR32List)) {
865 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
866 return false;
867 }
868 }
869
870 if (LocVT == MVT::f64 && Subtarget.hasStdExtD()) {
871 // Pass in STG registers: D1, ..., D6
872 // fs6 ... fs11
873 static const MCPhysReg FPR64List[] = {RISCV::F22_D, RISCV::F23_D,
874 RISCV::F24_D, RISCV::F25_D,
875 RISCV::F26_D, RISCV::F27_D};
876 if (MCRegister Reg = State.AllocateReg(Regs: FPR64List)) {
877 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
878 return false;
879 }
880 }
881
882 if (LocVT == MVT::f32 && Subtarget.hasStdExtZfinx()) {
883 static const MCPhysReg GPR32List[] = {
884 RISCV::X9_W, RISCV::X18_W, RISCV::X19_W, RISCV::X20_W,
885 RISCV::X21_W, RISCV::X22_W, RISCV::X23_W, RISCV::X24_W,
886 RISCV::X25_W, RISCV::X26_W, RISCV::X27_W};
887 if (MCRegister Reg = State.AllocateReg(Regs: GPR32List)) {
888 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
889 return false;
890 }
891 }
892
893 if (LocVT == MVT::f64 && Subtarget.hasStdExtZdinx() && Subtarget.is64Bit()) {
894 if (MCRegister Reg = State.AllocateReg(Regs: GPRList)) {
895 State.addLoc(V: CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, HTP: LocInfo));
896 return false;
897 }
898 }
899
900 report_fatal_error(reason: "No registers left in GHC calling convention");
901 return true;
902}
903