LLVM 24.0.0git
RISCVRegisterInfo.cpp
Go to the documentation of this file.
1//===-- RISCVRegisterInfo.cpp - RISC-V Register Information -----*- C++ -*-===//
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 RISC-V implementation of the TargetRegisterInfo class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "RISCVRegisterInfo.h"
14#include "RISCV.h"
15#include "RISCVSubtarget.h"
16#include "llvm/ADT/SmallSet.h"
26
27#define GET_REGINFO_TARGET_DESC
28#include "RISCVGenRegisterInfo.inc"
29
30using namespace llvm;
31
32static cl::opt<bool> DisableCostPerUse("riscv-disable-cost-per-use",
33 cl::init(false), cl::Hidden);
34static cl::opt<bool>
35 DisableRegAllocHints("riscv-disable-regalloc-hints", cl::Hidden,
36 cl::init(false),
37 cl::desc("Disable two address hints for register "
38 "allocation"));
39
40static_assert(RISCV::X1 == RISCV::X0 + 1, "Register list not consecutive");
41static_assert(RISCV::X31 == RISCV::X0 + 31, "Register list not consecutive");
42static_assert(RISCV::F1_H == RISCV::F0_H + 1, "Register list not consecutive");
43static_assert(RISCV::F31_H == RISCV::F0_H + 31,
44 "Register list not consecutive");
45static_assert(RISCV::F1_F == RISCV::F0_F + 1, "Register list not consecutive");
46static_assert(RISCV::F31_F == RISCV::F0_F + 31,
47 "Register list not consecutive");
48static_assert(RISCV::F1_D == RISCV::F0_D + 1, "Register list not consecutive");
49static_assert(RISCV::F31_D == RISCV::F0_D + 31,
50 "Register list not consecutive");
51static_assert(RISCV::F1_Q == RISCV::F0_Q + 1, "Register list not consecutive");
52static_assert(RISCV::F31_Q == RISCV::F0_Q + 31,
53 "Register list not consecutive");
54static_assert(RISCV::V1 == RISCV::V0 + 1, "Register list not consecutive");
55static_assert(RISCV::V31 == RISCV::V0 + 31, "Register list not consecutive");
56
58 : RISCVGenRegisterInfo(RISCV::X1, /*DwarfFlavour*/0, /*EHFlavor*/0,
59 /*PC*/0, HwMode) {}
60
61const MCPhysReg *
63 return CSR_IPRA_SaveList;
64}
65
66const MCPhysReg *
68 auto &Subtarget = MF->getSubtarget<RISCVSubtarget>();
70 return CSR_NoRegs_SaveList;
72 return Subtarget.hasStdExtE() ? CSR_RT_MostRegs_RVE_SaveList
73 : CSR_RT_MostRegs_SaveList;
74 if (MF->getFunction().hasFnAttribute("interrupt")) {
75 if (Subtarget.hasVInstructions()) {
76 if (Subtarget.hasStdExtD())
77 return Subtarget.hasStdExtE() ? CSR_XLEN_F64_V_Interrupt_RVE_SaveList
78 : CSR_XLEN_F64_V_Interrupt_SaveList;
79 if (Subtarget.hasStdExtF())
80 return Subtarget.hasStdExtE() ? CSR_XLEN_F32_V_Interrupt_RVE_SaveList
81 : CSR_XLEN_F32_V_Interrupt_SaveList;
82 return Subtarget.hasStdExtE() ? CSR_XLEN_V_Interrupt_RVE_SaveList
83 : CSR_XLEN_V_Interrupt_SaveList;
84 }
85 if (Subtarget.hasStdExtD())
86 return Subtarget.hasStdExtE() ? CSR_XLEN_F64_Interrupt_RVE_SaveList
87 : CSR_XLEN_F64_Interrupt_SaveList;
88 if (Subtarget.hasStdExtF())
89 return Subtarget.hasStdExtE() ? CSR_XLEN_F32_Interrupt_RVE_SaveList
90 : CSR_XLEN_F32_Interrupt_SaveList;
91 return Subtarget.hasStdExtE() ? CSR_Interrupt_RVE_SaveList
92 : CSR_Interrupt_SaveList;
93 }
94
95 bool HasVectorCSR =
97 Subtarget.hasVInstructions();
98
99 switch (Subtarget.getTargetABI()) {
100 default:
101 llvm_unreachable("Unrecognized ABI");
104 return CSR_ILP32E_LP64E_SaveList;
107 if (HasVectorCSR)
108 return CSR_ILP32_LP64_V_SaveList;
109 return CSR_ILP32_LP64_SaveList;
112 if (HasVectorCSR)
113 return CSR_ILP32F_LP64F_V_SaveList;
114 return CSR_ILP32F_LP64F_SaveList;
117 if (HasVectorCSR)
118 return CSR_ILP32D_LP64D_V_SaveList;
119 return CSR_ILP32D_LP64D_SaveList;
120 }
121}
122
124 const MachineOperand &MO, const MachineRegisterInfo &MRI) const {
125 const RISCVSubtarget &STI = MRI.getMF().getSubtarget<RISCVSubtarget>();
126
127 const RegClassOrRegBank &RCOrRB = MRI.getRegClassOrRegBank(MO.getReg());
128 if (const RegisterBank *RB = dyn_cast<const RegisterBank *>(RCOrRB))
129 return getRegClassForTypeOnBank(MRI.getType(MO.getReg()), *RB,
130 STI.is64Bit());
131
132 if (const auto *RC = dyn_cast<const TargetRegisterClass *>(RCOrRB)) {
133 return getAllocatableClass(RC);
134 }
135
136 return nullptr;
137}
138
141 bool Is64Bit) const {
142 if (RB.getID() == RISCV::GPRBRegBankID) {
143 if (Ty.getSizeInBits() <= 32 || (Is64Bit && Ty.getSizeInBits() == 64))
144 return &RISCV::GPRRegClass;
145 }
146
147 if (RB.getID() == RISCV::FPRBRegBankID) {
148 if (Ty.getSizeInBits() == 16)
149 return &RISCV::FPR16RegClass;
150 if (Ty.getSizeInBits() == 32)
151 return &RISCV::FPR32RegClass;
152 if (Ty.getSizeInBits() == 64)
153 return &RISCV::FPR64RegClass;
154 }
155
156 if (RB.getID() == RISCV::VRBRegBankID) {
157 if (Ty.getSizeInBits().getKnownMinValue() <= 64)
158 return &RISCV::VRRegClass;
159
160 if (Ty.getSizeInBits().getKnownMinValue() == 128)
161 return &RISCV::VRM2RegClass;
162
163 if (Ty.getSizeInBits().getKnownMinValue() == 256)
164 return &RISCV::VRM4RegClass;
165
166 if (Ty.getSizeInBits().getKnownMinValue() == 512)
167 return &RISCV::VRM8RegClass;
168 }
169
170 return nullptr;
171}
172
174 const RISCVFrameLowering *TFI = getFrameLowering(MF);
175 BitVector Reserved(getNumRegs());
176 auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
177
178 for (size_t Reg = 0; Reg < getNumRegs(); Reg++) {
179 // Mark any GPRs requested to be reserved as such
180 if (Subtarget.isRegisterReservedByUser(Reg)) {
181 for (MCPhysReg Sub : subregs_inclusive(Reg))
182 markSuperRegs(Reserved, Sub);
183 }
184
185 // Mark all the registers defined as constant in TableGen as reserved.
186 if (isConstantPhysReg(Reg)) {
187 for (MCPhysReg Sub : subregs_inclusive(Reg))
188 markSuperRegs(Reserved, Sub);
189 }
190 }
191
192 // Use markSuperRegs to ensure any register aliases are also reserved
193 markSuperRegs(Reserved, RISCV::X2_H); // sp
194 markSuperRegs(Reserved, RISCV::X3_H); // gp
195 markSuperRegs(Reserved, RISCV::X4_H); // tp
196 if (TFI->hasFP(MF))
197 markSuperRegs(Reserved, RISCV::X8_H); // fp
198 // Reserve the base register if we need to realign the stack and allocate
199 // variable-sized objects at runtime.
200 if (TFI->hasBP(MF))
201 markSuperRegs(Reserved, RISCVABI::getBPReg()); // bp
202
203 // Additionally reserve dummy register used to form the register pair
204 // beginning with 'x0' for instructions that take register pairs.
205 markSuperRegs(Reserved, RISCV::DUMMY_REG_PAIR_WITH_X0);
206
207 // There are only 16 GPRs for RVE.
208 if (Subtarget.hasStdExtE())
209 for (MCPhysReg Reg = RISCV::X16_H; Reg <= RISCV::X31_H; Reg++)
210 markSuperRegs(Reserved, Reg);
211
212 // V registers for code generation. We handle them manually.
213 markSuperRegs(Reserved, RISCV::VL);
214 markSuperRegs(Reserved, RISCV::VTYPE);
215 markSuperRegs(Reserved, RISCV::VXSAT);
216 markSuperRegs(Reserved, RISCV::VXRM);
217
218 // Floating point environment registers.
219 markSuperRegs(Reserved, RISCV::FRM);
220 markSuperRegs(Reserved, RISCV::FFLAGS);
221
222 // SiFive VCIX state registers.
223 markSuperRegs(Reserved, RISCV::SF_VCIX_STATE);
224
226 if (Subtarget.hasStdExtE())
227 reportFatalUsageError("Graal reserved registers do not exist in RVE");
228 markSuperRegs(Reserved, RISCV::X23_H);
229 markSuperRegs(Reserved, RISCV::X27_H);
230 }
231
232 // Shadow stack pointer.
233 markSuperRegs(Reserved, RISCV::SSP);
234
235 // XSfmmbase
236 for (MCPhysReg Reg = RISCV::T0; Reg <= RISCV::T15; Reg++)
237 markSuperRegs(Reserved, Reg);
238
239 assert(checkAllSuperRegsMarked(Reserved));
240 return Reserved;
241}
242
244 MCRegister PhysReg) const {
245 return !MF.getSubtarget().isRegisterReservedByUser(PhysReg);
246}
247
249 return CSR_NoRegs_RegMask;
250}
251
254 const DebugLoc &DL, Register DestReg,
257 MaybeAlign RequiredAlign) const {
258
259 if (DestReg == SrcReg && !Offset.getFixed() && !Offset.getScalable())
260 return;
261
262 MachineFunction &MF = *MBB.getParent();
265 const RISCVInstrInfo *TII = ST.getInstrInfo();
266
267 // Optimize compile time offset case
268 if (Offset.getScalable()) {
269 if (auto VLEN = ST.getRealVLen()) {
270 // 1. Multiply the number of v-slots by the (constant) length of register
271 const int64_t VLENB = *VLEN / 8;
272 assert(Offset.getScalable() % RISCV::RVVBytesPerBlock == 0 &&
273 "Reserve the stack by the multiple of one vector size.");
274 const int64_t NumOfVReg = Offset.getScalable() / 8;
275 const int64_t FixedOffset = NumOfVReg * VLENB;
276 if (!isInt<32>(FixedOffset)) {
277 // This check might also need to be updated to 64bit.
278 // However mulImm() still assumes 32bit. For now only support fixed
279 // 64bit frame offsets, since scalable offsets would require the number
280 // of spilled registers to exceed 2^31, which is unlikely.
281 reportFatalUsageError("Scalable frame size outside of the signed "
282 "32-bit range not supported");
283 }
284 Offset = StackOffset::getFixed(FixedOffset + Offset.getFixed());
285 }
286 }
287
288 bool KillSrcReg = false;
289
290 if (Offset.getScalable()) {
291 unsigned ScalableAdjOpc = RISCV::ADD;
292 int64_t ScalableValue = Offset.getScalable();
293 if (ScalableValue < 0) {
294 ScalableValue = -ScalableValue;
295 ScalableAdjOpc = RISCV::SUB;
296 }
297 // Get vlenb and multiply vlen with the number of vector registers.
298 Register ScratchReg = DestReg;
299 if (DestReg == SrcReg)
300 ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
301
302 assert(ScalableValue > 0 && "There is no need to get VLEN scaled value.");
303 assert(ScalableValue % RISCV::RVVBytesPerBlock == 0 &&
304 "Reserve the stack by the multiple of one vector size.");
305 assert(isInt<32>(ScalableValue / RISCV::RVVBytesPerBlock) &&
306 "Expect the number of vector registers within 32-bits.");
307 uint32_t NumOfVReg = ScalableValue / RISCV::RVVBytesPerBlock;
308 // Only use vsetvli rather than vlenb if adjusting in the prologue or
309 // epilogue, otherwise it may disturb the VTYPE and VL status.
310 bool IsPrologueOrEpilogue =
312 bool UseVsetvliRatherThanVlenb =
313 IsPrologueOrEpilogue && ST.preferVsetvliOverReadVLENB();
314 if (UseVsetvliRatherThanVlenb && (NumOfVReg == 1 || NumOfVReg == 2 ||
315 NumOfVReg == 4 || NumOfVReg == 8)) {
316 BuildMI(MBB, II, DL, TII->get(RISCV::PseudoReadVLENBViaVSETVLIX0),
317 ScratchReg)
318 .addImm(NumOfVReg)
319 .setMIFlag(Flag);
320 BuildMI(MBB, II, DL, TII->get(ScalableAdjOpc), DestReg)
321 .addReg(SrcReg)
322 .addReg(ScratchReg, RegState::Kill)
323 .setMIFlag(Flag);
324 } else {
325 if (UseVsetvliRatherThanVlenb)
326 BuildMI(MBB, II, DL, TII->get(RISCV::PseudoReadVLENBViaVSETVLIX0),
327 ScratchReg)
328 .addImm(1)
329 .setMIFlag(Flag);
330 else
331 BuildMI(MBB, II, DL, TII->get(RISCV::PseudoReadVLENB), ScratchReg)
332 .setMIFlag(Flag);
333
334 if (ScalableAdjOpc == RISCV::ADD && ST.hasStdExtZba() &&
335 (NumOfVReg == 2 || NumOfVReg == 4 || NumOfVReg == 8)) {
336 unsigned Opc = NumOfVReg == 2
337 ? RISCV::SH1ADD
338 : (NumOfVReg == 4 ? RISCV::SH2ADD : RISCV::SH3ADD);
339 BuildMI(MBB, II, DL, TII->get(Opc), DestReg)
340 .addReg(ScratchReg, RegState::Kill)
341 .addReg(SrcReg)
342 .setMIFlag(Flag);
343 } else {
344 TII->mulImm(MF, MBB, II, DL, ScratchReg, NumOfVReg, Flag);
345 BuildMI(MBB, II, DL, TII->get(ScalableAdjOpc), DestReg)
346 .addReg(SrcReg)
347 .addReg(ScratchReg, RegState::Kill)
348 .setMIFlag(Flag);
349 }
350 }
351 SrcReg = DestReg;
352 KillSrcReg = true;
353 }
354
355 int64_t Val = Offset.getFixed();
356 if (DestReg == SrcReg && Val == 0)
357 return;
358
359 const uint64_t Align = RequiredAlign.valueOrOne().value();
360
361 if (isInt<12>(Val)) {
362 BuildMI(MBB, II, DL, TII->get(RISCV::ADDI), DestReg)
363 .addReg(SrcReg, getKillRegState(KillSrcReg))
364 .addImm(Val)
365 .setMIFlag(Flag);
366 return;
367 }
368
369 // Use the QC_E_ADDI instruction from the Xqcilia extension that can take a
370 // signed 26-bit immediate.
371 if (ST.hasVendorXqcilia() && isInt<26>(Val)) {
372 // The one case where using this instruction is sub-optimal is if Val can be
373 // materialized with a single compressible LUI and following add/sub is also
374 // compressible. Avoid doing this if that is the case.
375 int Hi20 = (Val & 0xFFFFF000) >> 12;
376 bool IsCompressLUI =
377 ((Val & 0xFFF) == 0) && (Hi20 != 0) &&
378 (isUInt<5>(Hi20) || (Hi20 >= 0xfffe0 && Hi20 <= 0xfffff));
379 bool IsCompressAddSub =
380 (SrcReg == DestReg) &&
381 ((Val > 0 && RISCV::GPRNoX0RegClass.contains(SrcReg)) ||
382 (Val < 0 && RISCV::GPRCRegClass.contains(SrcReg)));
383
384 if (!(IsCompressLUI && IsCompressAddSub)) {
385 BuildMI(MBB, II, DL, TII->get(RISCV::QC_E_ADDI), DestReg)
386 .addReg(SrcReg, getKillRegState(KillSrcReg))
387 .addImm(Val)
388 .setMIFlag(Flag);
389 return;
390 }
391 }
392
393 // Try to split the offset across two ADDIs. We need to keep the intermediate
394 // result aligned after each ADDI. We need to determine the maximum value we
395 // can put in each ADDI. In the negative direction, we can use -2048 which is
396 // always sufficiently aligned. In the positive direction, we need to find the
397 // largest 12-bit immediate that is aligned. Exclude -4096 since it can be
398 // created with LUI.
399 assert(Align < 2048 && "Required alignment too large");
400 int64_t MaxPosAdjStep = 2048 - Align;
401 if (Val > -4096 && Val <= (2 * MaxPosAdjStep)) {
402 int64_t FirstAdj = Val < 0 ? -2048 : MaxPosAdjStep;
403 Val -= FirstAdj;
404 BuildMI(MBB, II, DL, TII->get(RISCV::ADDI), DestReg)
405 .addReg(SrcReg, getKillRegState(KillSrcReg))
406 .addImm(FirstAdj)
407 .setMIFlag(Flag);
408 BuildMI(MBB, II, DL, TII->get(RISCV::ADDI), DestReg)
409 .addReg(DestReg, RegState::Kill)
410 .addImm(Val)
411 .setMIFlag(Flag);
412 return;
413 }
414
415 // Use shNadd if doing so lets us materialize a 12 bit immediate with a single
416 // instruction. This saves 1 instruction over the full lui/addi+add fallback
417 // path. We avoid anything which can be done with a single lui as it might
418 // be compressible. Note that the sh1add case is fully covered by the 2x addi
419 // case just above and is thus omitted.
420 if (ST.hasStdExtZba() && (Val & 0xFFF) != 0) {
421 unsigned Opc = 0;
422 if (isShiftedInt<12, 3>(Val)) {
423 Opc = RISCV::SH3ADD;
424 Val = Val >> 3;
425 } else if (isShiftedInt<12, 2>(Val)) {
426 Opc = RISCV::SH2ADD;
427 Val = Val >> 2;
428 }
429 if (Opc) {
430 Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
431 TII->movImm(MBB, II, DL, ScratchReg, Val, Flag);
432 BuildMI(MBB, II, DL, TII->get(Opc), DestReg)
433 .addReg(ScratchReg, RegState::Kill)
434 .addReg(SrcReg, getKillRegState(KillSrcReg))
435 .setMIFlag(Flag);
436 return;
437 }
438 }
439
440 // Emit a PseudoAddUpperImm instead of LUI+ADD when the offset is a multiple
441 // of 4096 and the source is the frame register. The frame register is
442 // invariant after PEI, so MachineLateInstrsCleanup can CSE identical pseudos.
443 // The pseudo is later expanded back to LUI+ADD.
444 if (Flag == MachineInstr::NoFlags && !KillSrcReg && DestReg != SrcReg &&
445 SrcReg == getFrameRegister(MF) && isShiftedInt<20, 12>(Val)) {
446 BuildMI(MBB, II, DL, TII->get(RISCV::PseudoAddUpperImm), DestReg)
447 .addReg(SrcReg)
448 .addImm(static_cast<uint32_t>(Val) >> 12);
449 return;
450 }
451
452 unsigned Opc = RISCV::ADD;
453 if (Val < 0) {
454 Val = -Val;
455 Opc = RISCV::SUB;
456 }
457
458 Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
459 TII->movImm(MBB, II, DL, ScratchReg, Val, Flag);
460 BuildMI(MBB, II, DL, TII->get(Opc), DestReg)
461 .addReg(SrcReg, getKillRegState(KillSrcReg))
462 .addReg(ScratchReg, RegState::Kill)
463 .setMIFlag(Flag);
464}
465
466static std::tuple<RISCVVType::VLMUL, const TargetRegisterClass &, unsigned>
467getSpillReloadInfo(unsigned NumRemaining, uint16_t RegEncoding, bool IsSpill) {
468 if (NumRemaining >= 8 && RegEncoding % 8 == 0)
469 return {RISCVVType::LMUL_8, RISCV::VRM8RegClass,
470 IsSpill ? RISCV::VS8R_V : RISCV::VL8RE8_V};
471 if (NumRemaining >= 4 && RegEncoding % 4 == 0)
472 return {RISCVVType::LMUL_4, RISCV::VRM4RegClass,
473 IsSpill ? RISCV::VS4R_V : RISCV::VL4RE8_V};
474 if (NumRemaining >= 2 && RegEncoding % 2 == 0)
475 return {RISCVVType::LMUL_2, RISCV::VRM2RegClass,
476 IsSpill ? RISCV::VS2R_V : RISCV::VL2RE8_V};
477 return {RISCVVType::LMUL_1, RISCV::VRRegClass,
478 IsSpill ? RISCV::VS1R_V : RISCV::VL1RE8_V};
479}
480
481// Split a VSPILLx_Mx/VSPILLx_Mx pseudo into multiple whole register stores
482// separated by LMUL*VLENB bytes.
484 bool IsSpill) const {
485 DebugLoc DL = II->getDebugLoc();
486 MachineBasicBlock &MBB = *II->getParent();
487 MachineFunction &MF = *MBB.getParent();
489 const RISCVSubtarget &STI = MF.getSubtarget<RISCVSubtarget>();
490 const TargetInstrInfo *TII = STI.getInstrInfo();
492
493 auto ZvlssegInfo = RISCV::isRVVSpillForZvlsseg(II->getOpcode());
494 unsigned NF = ZvlssegInfo->first;
495 unsigned LMUL = ZvlssegInfo->second;
496 unsigned NumRegs = NF * LMUL;
497 assert(NumRegs <= 8 && "Invalid NF/LMUL combinations.");
498
499 Register Reg = II->getOperand(0).getReg();
500 uint16_t RegEncoding = TRI->getEncodingValue(Reg);
501 Register Base = II->getOperand(1).getReg();
502 bool IsBaseKill = II->getOperand(1).isKill();
503 Register NewBase = MRI.createVirtualRegister(&RISCV::GPRRegClass);
504
505 auto *OldMMO = *(II->memoperands_begin());
506 LocationSize OldLoc = OldMMO->getSize();
507 assert(OldLoc.isPrecise() && OldLoc.getValue().isKnownMultipleOf(NF));
508 TypeSize VRegSize = OldLoc.getValue().divideCoefficientBy(NumRegs);
509
510 Register VLENB = 0;
511 unsigned VLENBShift = 0;
512 unsigned PrevHandledNum = 0;
513 unsigned I = 0;
514 while (I != NumRegs) {
515 auto [LMulHandled, RegClass, Opcode] =
516 getSpillReloadInfo(NumRegs - I, RegEncoding, IsSpill);
517 auto [RegNumHandled, _] = RISCVVType::decodeVLMUL(LMulHandled);
518 bool IsLast = I + RegNumHandled == NumRegs;
519 if (PrevHandledNum) {
520 Register Step;
521 // Optimize for constant VLEN.
522 if (auto VLEN = STI.getRealVLen()) {
523 int64_t Offset = *VLEN / 8 * PrevHandledNum;
524 Step = MRI.createVirtualRegister(&RISCV::GPRRegClass);
525 STI.getInstrInfo()->movImm(MBB, II, DL, Step, Offset);
526 } else {
527 if (!VLENB) {
528 VLENB = MRI.createVirtualRegister(&RISCV::GPRRegClass);
529 BuildMI(MBB, II, DL, TII->get(RISCV::PseudoReadVLENB), VLENB);
530 }
531 uint32_t ShiftAmount = Log2_32(PrevHandledNum);
532 // To avoid using an extra register, we shift the VLENB register and
533 // remember how much it has been shifted. We can then use relative
534 // shifts to adjust to the desired shift amount.
535 if (VLENBShift > ShiftAmount) {
536 BuildMI(MBB, II, DL, TII->get(RISCV::SRLI), VLENB)
537 .addReg(VLENB, RegState::Kill)
538 .addImm(VLENBShift - ShiftAmount);
539 } else if (VLENBShift < ShiftAmount) {
540 BuildMI(MBB, II, DL, TII->get(RISCV::SLLI), VLENB)
541 .addReg(VLENB, RegState::Kill)
542 .addImm(ShiftAmount - VLENBShift);
543 }
544 VLENBShift = ShiftAmount;
545 Step = VLENB;
546 }
547
548 BuildMI(MBB, II, DL, TII->get(RISCV::ADD), NewBase)
549 .addReg(Base, getKillRegState(I != 0 || IsBaseKill))
550 .addReg(Step, getKillRegState(Step != VLENB || IsLast));
551 Base = NewBase;
552 }
553
554 MCRegister ActualReg = findVRegWithEncoding(RegClass, RegEncoding);
556 BuildMI(MBB, II, DL, TII->get(Opcode))
557 .addReg(ActualReg, getDefRegState(!IsSpill))
558 .addReg(Base, getKillRegState(IsLast))
559 .addMemOperand(MF.getMachineMemOperand(OldMMO, OldMMO->getOffset(),
560 VRegSize * RegNumHandled));
561
562 // Adding implicit-use of super register to describe we are using part of
563 // super register, that prevents machine verifier complaining when part of
564 // subreg is undef, see comment in MachineVerifier::checkLiveness for more
565 // detail.
566 if (IsSpill)
567 MIB.addReg(Reg, RegState::Implicit);
568
569 PrevHandledNum = RegNumHandled;
570 RegEncoding += RegNumHandled;
571 I += RegNumHandled;
572 }
573 II->eraseFromParent();
574}
575
576static unsigned getXqciloWideOpcode(unsigned Opc) {
577 switch (Opc) {
578 case RISCV::LW:
579 return RISCV::QC_E_LW;
580 case RISCV::SW:
581 return RISCV::QC_E_SW;
582 case RISCV::LB:
583 return RISCV::QC_E_LB;
584 case RISCV::LBU:
585 return RISCV::QC_E_LBU;
586 case RISCV::LH:
587 return RISCV::QC_E_LH;
588 case RISCV::LHU:
589 return RISCV::QC_E_LHU;
590 case RISCV::SB:
591 return RISCV::QC_E_SB;
592 case RISCV::SH:
593 return RISCV::QC_E_SH;
594 default:
595 return 0;
596 }
597}
598
600 int SPAdj, unsigned FIOperandNum,
601 RegScavenger *RS) const {
602 assert(SPAdj == 0 && "Unexpected non-zero SPAdj value");
603
604 MachineInstr &MI = *II;
605 MachineFunction &MF = *MI.getParent()->getParent();
608 const RISCVInstrInfo *TII = ST.getInstrInfo();
609 bool Is64Bit = ST.is64Bit();
610 DebugLoc DL = MI.getDebugLoc();
611
612 int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
613 Register FrameReg;
615 getFrameLowering(MF)->getFrameIndexReference(MF, FrameIndex, FrameReg);
616 bool IsRVVSpill = RISCV::isRVVSpill(MI);
617 if (!IsRVVSpill)
618 Offset += StackOffset::getFixed(MI.getOperand(FIOperandNum + 1).getImm());
619
620 if (!Is64Bit && !isInt<32>(Offset.getFixed())) {
621 reportFatalUsageError("Frame offsets outside of the signed 32-bit range "
622 "not supported on RV32");
623 }
624
625 if (!IsRVVSpill) {
626 int64_t Val = Offset.getFixed();
627 int64_t Lo12 = SignExtend64<12>(Val);
628 int64_t Lo26 = SignExtend64<26>(Val);
629 unsigned Opc = MI.getOpcode();
630
631 if (Opc == RISCV::ADDI && !isInt<12>(Val)) {
632 // We chose to emit the canonical immediate sequence rather than folding
633 // the offset into the using add under the theory that doing so doesn't
634 // save dynamic instruction count and some target may fuse the canonical
635 // 32 bit immediate sequence. We still need to clear the portion of the
636 // offset encoded in the immediate.
637 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(0);
638 } else if ((Opc == RISCV::PREFETCH_I || Opc == RISCV::PREFETCH_R ||
639 Opc == RISCV::PREFETCH_W) &&
640 (Lo12 & 0b11111) != 0) {
641 // Prefetch instructions require the offset to be 32 byte aligned.
642 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(0);
643 } else if (Opc == RISCV::MIPS_PREF && !isUInt<9>(Val)) {
644 // MIPS Prefetch instructions require the offset to be 9 bits encoded.
645 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(0);
646 } else if ((Opc == RISCV::PseudoRV32ZdinxLD ||
647 Opc == RISCV::PseudoRV32ZdinxSD ||
648 Opc == RISCV::PseudoLD_RV32_OPT ||
649 Opc == RISCV::PseudoSD_RV32_OPT) &&
650 Lo12 >= 2044) {
651 // This instruction will/might be split into 2 instructions. The second
652 // instruction will add 4 to the immediate. If that would overflow 12
653 // bits, we can't fold the offset.
654 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(0);
655 } else if (unsigned WideOpc = getXqciloWideOpcode(Opc);
656 !isInt<12>(Val) && ST.hasVendorXqcilo() && WideOpc) {
657 // The resolved frame offset exceeds simm12 but the instruction is a
658 // standard load/store (LW/SW/etc). Promote to the wide Xqcilo equivalent
659 // so the full 26-bit offset folds directly, avoiding a separate
660 // base-adjust instruction. This runs post-RA and does not affect
661 // register allocation decisions.
662 MI.setDesc(TII->get(WideOpc));
663 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Lo26);
665 Offset.getScalable());
666 } else if (Opc == RISCV::QC_E_ADDI || RISCVInstrInfo::isBaseQCLoad(MI) ||
667 RISCVInstrInfo::isBaseQCStore(MI)) {
668 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Lo26);
670 Offset.getScalable());
671 } else {
672 // We can encode an add with 12 bit signed immediate in the immediate
673 // operand of our user instruction. As a result, the remaining
674 // offset can by construction, at worst, a LUI and a ADD.
675 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Lo12);
677 Offset.getScalable());
678 }
679 }
680
681 if (Offset.getScalable() || Offset.getFixed()) {
682 Register DestReg;
683 if (MI.getOpcode() == RISCV::ADDI)
684 DestReg = MI.getOperand(0).getReg();
685 else
686 DestReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
687 adjustReg(*II->getParent(), II, DL, DestReg, FrameReg, Offset,
688 MachineInstr::NoFlags, std::nullopt);
689 MI.getOperand(FIOperandNum).ChangeToRegister(DestReg, /*IsDef*/false,
690 /*IsImp*/false,
691 /*IsKill*/true);
692 } else {
693 MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, /*IsDef*/false,
694 /*IsImp*/false,
695 /*IsKill*/false);
696 }
697
698 // If after materializing the adjustment, we have a pointless ADDI, remove it
699 if (MI.getOpcode() == RISCV::ADDI &&
700 MI.getOperand(0).getReg() == MI.getOperand(1).getReg() &&
701 MI.getOperand(2).getImm() == 0) {
702 MI.eraseFromParent();
703 return true;
704 }
705
706 // Handle spill/fill of synthetic register classes for segment operations to
707 // ensure correctness in the edge case one gets spilled.
708 switch (MI.getOpcode()) {
709 case RISCV::PseudoVSPILL2_M1:
710 case RISCV::PseudoVSPILL2_M2:
711 case RISCV::PseudoVSPILL2_M4:
712 case RISCV::PseudoVSPILL3_M1:
713 case RISCV::PseudoVSPILL3_M2:
714 case RISCV::PseudoVSPILL4_M1:
715 case RISCV::PseudoVSPILL4_M2:
716 case RISCV::PseudoVSPILL5_M1:
717 case RISCV::PseudoVSPILL6_M1:
718 case RISCV::PseudoVSPILL7_M1:
719 case RISCV::PseudoVSPILL8_M1:
720 lowerSegmentSpillReload(II, /*IsSpill=*/true);
721 return true;
722 case RISCV::PseudoVRELOAD2_M1:
723 case RISCV::PseudoVRELOAD2_M2:
724 case RISCV::PseudoVRELOAD2_M4:
725 case RISCV::PseudoVRELOAD3_M1:
726 case RISCV::PseudoVRELOAD3_M2:
727 case RISCV::PseudoVRELOAD4_M1:
728 case RISCV::PseudoVRELOAD4_M2:
729 case RISCV::PseudoVRELOAD5_M1:
730 case RISCV::PseudoVRELOAD6_M1:
731 case RISCV::PseudoVRELOAD7_M1:
732 case RISCV::PseudoVRELOAD8_M1:
733 lowerSegmentSpillReload(II, /*IsSpill=*/false);
734 return true;
735 }
736
737 return false;
738}
739
741 const MachineFunction &MF) const {
742 return true;
743}
744
745// Returns true if the instruction's frame index reference would be better
746// served by a base register other than FP or SP.
747// Used by LocalStackSlotAllocation pass to determine which frame index
748// references it should create new base registers for.
750 int64_t Offset) const {
751 unsigned FIOperandNum = 0;
752 for (; !MI->getOperand(FIOperandNum).isFI(); FIOperandNum++)
753 assert(FIOperandNum < MI->getNumOperands() &&
754 "Instr doesn't have FrameIndex operand");
755
756 // For RISC-V, The machine instructions that include a FrameIndex operand
757 // are load/store, ADDI instructions.
758 unsigned MIFrm = RISCVII::getFormat(MI->getDesc().TSFlags);
759 if (MIFrm != RISCVII::InstFormatI && MIFrm != RISCVII::InstFormatS)
760 return false;
761 // We only generate virtual base registers for loads and stores, so
762 // return false for everything else.
763 if (!MI->mayLoad() && !MI->mayStore())
764 return false;
765
766 const MachineFunction &MF = *MI->getMF();
767 const MachineFrameInfo &MFI = MF.getFrameInfo();
768 const RISCVFrameLowering *TFI = getFrameLowering(MF);
769 const MachineRegisterInfo &MRI = MF.getRegInfo();
770
771 if (TFI->hasFP(MF) && !shouldRealignStack(MF)) {
772 auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
773 // Estimate the stack size used to store callee saved registers(
774 // excludes reserved registers).
775 unsigned CalleeSavedSize = 0;
776 for (const MCPhysReg *R = MRI.getCalleeSavedRegs(); MCPhysReg Reg = *R;
777 ++R) {
778 if (Subtarget.isRegisterReservedByUser(Reg))
779 continue;
780
781 if (RISCV::GPRRegClass.contains(Reg))
782 CalleeSavedSize += getSpillSize(RISCV::GPRRegClass);
783 else if (RISCV::FPR64RegClass.contains(Reg))
784 CalleeSavedSize += getSpillSize(RISCV::FPR64RegClass);
785 else if (RISCV::FPR32RegClass.contains(Reg))
786 CalleeSavedSize += getSpillSize(RISCV::FPR32RegClass);
787 // Ignore vector registers.
788 }
789
790 int64_t MaxFPOffset = Offset - CalleeSavedSize;
791 if (isFrameOffsetLegal(MI, RISCV::X8, MaxFPOffset))
792 return false;
793
794 // If the FP-relative offset doesn't fit, fall through to check the
795 // SP-relative offset. getFrameIndexReference may select SP over FP when
796 // the SP offset fits in the compressed instruction immediate range, so a
797 // base register might not be needed.
798 }
799
800 // Assume 128 bytes spill slots size to estimate the maximum possible
801 // offset relative to the stack pointer.
802 // FIXME: The 128 is copied from ARM. We should run some statistics and pick a
803 // real one for RISC-V.
804 int64_t MaxSPOffset = Offset + 128;
805 MaxSPOffset += MFI.getLocalFrameSize();
806 return !isFrameOffsetLegal(MI, RISCV::X2, MaxSPOffset);
807}
808
809// Determine whether a given base register plus offset immediate is
810// encodable to resolve a frame index.
812 Register BaseReg,
813 int64_t Offset) const {
814 unsigned FIOperandNum = 0;
815 while (!MI->getOperand(FIOperandNum).isFI()) {
816 FIOperandNum++;
817 assert(FIOperandNum < MI->getNumOperands() &&
818 "Instr does not have a FrameIndex operand!");
819 }
820
821 Offset += getFrameIndexInstrOffset(MI, FIOperandNum);
822 return isInt<12>(Offset);
823}
824
825// Insert defining instruction(s) for a pointer to FrameIdx before
826// insertion point I.
827// Return materialized frame pointer.
829 int FrameIdx,
830 int64_t Offset) const {
832 DebugLoc DL;
833 if (MBBI != MBB->end())
834 DL = MBBI->getDebugLoc();
835 MachineFunction *MF = MBB->getParent();
836 MachineRegisterInfo &MFI = MF->getRegInfo();
838
839 Register BaseReg = MFI.createVirtualRegister(&RISCV::GPRRegClass);
840 BuildMI(*MBB, MBBI, DL, TII->get(RISCV::ADDI), BaseReg)
841 .addFrameIndex(FrameIdx)
842 .addImm(Offset);
843 return BaseReg;
844}
845
846// Resolve a frame index operand of an instruction to reference the
847// indicated base register plus offset instead.
849 int64_t Offset) const {
850 unsigned FIOperandNum = 0;
851 while (!MI.getOperand(FIOperandNum).isFI()) {
852 FIOperandNum++;
853 assert(FIOperandNum < MI.getNumOperands() &&
854 "Instr does not have a FrameIndex operand!");
855 }
856
857 Offset += getFrameIndexInstrOffset(&MI, FIOperandNum);
858 // FrameIndex Operands are always represented as a
859 // register followed by an immediate.
860 MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false);
861 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
862}
863
864// Get the offset from the referenced frame index in the instruction,
865// if there is one.
867 int Idx) const {
868 assert((RISCVII::getFormat(MI->getDesc().TSFlags) == RISCVII::InstFormatI ||
869 RISCVII::getFormat(MI->getDesc().TSFlags) == RISCVII::InstFormatS) &&
870 "The MI must be I or S format.");
871 assert(MI->getOperand(Idx).isFI() && "The Idx'th operand of MI is not a "
872 "FrameIndex operand");
873 return MI->getOperand(Idx + 1).getImm();
874}
875
877 const TargetFrameLowering *TFI = getFrameLowering(MF);
878 return TFI->hasFP(MF) ? RISCV::X8 : RISCV::X2;
879}
880
882 MCRegister Reg) const {
883 auto const &STI = MF.getSubtarget<RISCVSubtarget>();
884 const RISCVRegisterInfo *TRI = STI.getRegisterInfo();
885
886 if (TRI->isGeneralPurposeRegister(MF, Reg))
887 return llvm::is_contained(RISCV::getArgGPRs(STI), Reg);
888
889 if (TRI->isFPRegister(Reg))
890 return llvm::is_contained(RISCV::getArgFPRs(STI), Reg);
891
892 if (RISCV::VRRegClass.contains(Reg))
893 return llvm::is_contained(RISCV::getArgVRs(STI), Reg);
894
895 return false;
896}
897
899 if (Reg == RISCV::SF_VCIX_STATE)
900 return "sf.vcix_state";
902}
903
904const uint32_t *
906 CallingConv::ID CC) const {
907 auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
908
909 if (CC == CallingConv::GHC)
910 return CSR_NoRegs_RegMask;
911 RISCVABI::ABI ABI = Subtarget.getTargetABI();
912 if (CC == CallingConv::PreserveMost) {
913 if (ABI == RISCVABI::ABI_ILP32E || ABI == RISCVABI::ABI_LP64E)
914 return CSR_RT_MostRegs_RVE_RegMask;
915 return CSR_RT_MostRegs_RegMask;
916 }
917 switch (ABI) {
918 default:
919 llvm_unreachable("Unrecognized ABI");
922 return CSR_ILP32E_LP64E_RegMask;
926 return CSR_ILP32_LP64_V_RegMask;
927 return CSR_ILP32_LP64_RegMask;
931 return CSR_ILP32F_LP64F_V_RegMask;
932 return CSR_ILP32F_LP64F_RegMask;
936 return CSR_ILP32D_LP64D_V_RegMask;
937 return CSR_ILP32D_LP64D_RegMask;
938 }
939}
940
943 const MachineFunction &) const {
944 if (RC == &RISCV::VMV0RegClass)
945 return &RISCV::VRRegClass;
946 if (RC == &RISCV::VRNoV0RegClass)
947 return &RISCV::VRRegClass;
948 if (RC == &RISCV::VRM2NoV0RegClass)
949 return &RISCV::VRM2RegClass;
950 if (RC == &RISCV::VRM4NoV0RegClass)
951 return &RISCV::VRM4RegClass;
952 if (RC == &RISCV::VRM8NoV0RegClass)
953 return &RISCV::VRM8RegClass;
954 return RC;
955}
956
959 // VLENB is the length of a vector register in bytes. We use <vscale x 8 x i8>
960 // to represent one vector register. The dwarf offset is
961 // VLENB * scalable_offset / 8.
962 assert(Offset.getScalable() % 8 == 0 && "Invalid frame offset");
963
964 // Add fixed-sized offset using existing DIExpression interface.
966
967 unsigned VLENB = getDwarfRegNum(RISCV::VLENB, true);
968 int64_t VLENBSized = Offset.getScalable() / 8;
969 if (VLENBSized > 0) {
970 Ops.push_back(dwarf::DW_OP_constu);
971 Ops.push_back(VLENBSized);
972 Ops.append({dwarf::DW_OP_bregx, VLENB, 0ULL});
973 Ops.push_back(dwarf::DW_OP_mul);
974 Ops.push_back(dwarf::DW_OP_plus);
975 } else if (VLENBSized < 0) {
976 Ops.push_back(dwarf::DW_OP_constu);
977 Ops.push_back(-VLENBSized);
978 Ops.append({dwarf::DW_OP_bregx, VLENB, 0ULL});
979 Ops.push_back(dwarf::DW_OP_mul);
980 Ops.push_back(dwarf::DW_OP_minus);
981 }
982}
983
984unsigned
986 // Set CostPerUse to 1 only when optimizing for size and RVC exists.
987 return MF.getFunction().hasOptSize() &&
988 MF.getSubtarget<RISCVSubtarget>().hasStdExtZca() &&
990 ? 1
991 : 0;
992}
993
995 const TargetRegisterClass *RC) const {
996 return getRegClassWeight(RC).RegWeight;
997}
998
999// Add two address hints to improve chances of being able to use a compressed
1000// instruction.
1002 Register VirtReg, ArrayRef<MCPhysReg> Order,
1004 const VirtRegMap *VRM, const LiveRegMatrix *Matrix) const {
1005 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1006 auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
1007
1008 // Handle RegPairEven/RegPairOdd hints for Zilsd register pairs
1009 std::pair<unsigned, Register> Hint = MRI->getRegAllocationHint(VirtReg);
1010 unsigned HintType = Hint.first;
1011 Register Partner = Hint.second;
1012
1013 MCRegister TargetReg;
1014 if (HintType == RISCVRI::RegPairEven || HintType == RISCVRI::RegPairOdd) {
1015 // Check if we want the even or odd register of a consecutive pair
1016 bool WantOdd = (HintType == RISCVRI::RegPairOdd);
1017
1018 // First priority: Check if partner is already allocated
1019 if (Partner.isVirtual() && VRM && VRM->hasPhys(Partner)) {
1020 MCRegister PartnerPhys = VRM->getPhys(Partner);
1021 // Calculate the exact register we need for consecutive pairing
1022 TargetReg = PartnerPhys.id() + (WantOdd ? 1 : -1);
1023
1024 // Verify it's valid and available
1025 if (RISCV::GPRRegClass.contains(TargetReg) &&
1026 is_contained(Order, TargetReg))
1027 Hints.push_back(TargetReg.id());
1028 }
1029
1030 // Second priority: Try to find consecutive register pairs in the allocation
1031 // order
1032 for (MCPhysReg PhysReg : Order) {
1033 // Don't add the hint if we already added above.
1034 if (TargetReg == PhysReg)
1035 continue;
1036
1037 unsigned RegNum = getEncodingValue(PhysReg);
1038 // Check if this register matches the even/odd requirement
1039 bool IsOdd = (RegNum % 2 != 0);
1040
1041 // Don't provide hints that are paired to a reserved register.
1042 MCRegister Paired = PhysReg + (IsOdd ? -1 : 1);
1043 if (WantOdd == IsOdd && !MRI->isReserved(Paired))
1044 Hints.push_back(PhysReg);
1045 }
1046 }
1047
1048 bool BaseImplRetVal = TargetRegisterInfo::getRegAllocationHints(
1049 VirtReg, Order, Hints, MF, VRM, Matrix);
1050
1051 if (!VRM || DisableRegAllocHints)
1052 return BaseImplRetVal;
1053
1054 // Add any two address hints after any copy hints.
1055 SmallSet<Register, 4> TwoAddrHints;
1056
1057 auto tryAddHint = [&](const MachineOperand &VRRegMO, const MachineOperand &MO,
1058 bool NeedGPRC) -> void {
1059 Register Reg = MO.getReg();
1060 Register PhysReg = Reg.isPhysical() ? Reg : Register(VRM->getPhys(Reg));
1061 // TODO: Support GPRPair subregisters? Need to be careful with even/odd
1062 // registers. If the virtual register is an odd register of a pair and the
1063 // physical register is even (or vice versa), we should not add the hint.
1064 if (PhysReg && (!NeedGPRC || RISCV::GPRCRegClass.contains(PhysReg)) &&
1065 !MO.getSubReg() && !VRRegMO.getSubReg()) {
1066 if (!MRI->isReserved(PhysReg) && !is_contained(Hints, PhysReg))
1067 TwoAddrHints.insert(PhysReg);
1068 }
1069 };
1070
1071 // This is all of the compressible binary instructions. If an instruction
1072 // needs GPRC register class operands \p NeedGPRC will be set to true.
1073 auto isCompressible = [&Subtarget](const MachineInstr &MI, bool &NeedGPRC) {
1074 NeedGPRC = false;
1075 switch (MI.getOpcode()) {
1076 default:
1077 return false;
1078 case RISCV::AND:
1079 case RISCV::OR:
1080 case RISCV::XOR:
1081 case RISCV::SUB:
1082 case RISCV::ADDW:
1083 case RISCV::SUBW:
1084 NeedGPRC = true;
1085 return true;
1086 case RISCV::ANDI: {
1087 NeedGPRC = true;
1088 if (!MI.getOperand(2).isImm())
1089 return false;
1090 int64_t Imm = MI.getOperand(2).getImm();
1091 if (isInt<6>(Imm))
1092 return true;
1093 // c.zext.b
1094 return Subtarget.hasStdExtZcb() && Imm == 255;
1095 }
1096 case RISCV::SRAI:
1097 case RISCV::SRLI:
1098 NeedGPRC = true;
1099 return true;
1100 case RISCV::ADD:
1101 case RISCV::SLLI:
1102 return true;
1103 case RISCV::ADDI:
1104 case RISCV::ADDIW:
1105 return MI.getOperand(2).isImm() && isInt<6>(MI.getOperand(2).getImm());
1106 case RISCV::MUL:
1107 // c.mul
1108 NeedGPRC = true;
1109 return Subtarget.hasStdExtZcb();
1110 case RISCV::SEXT_B:
1111 case RISCV::SEXT_H:
1112 case RISCV::ZEXT_H_RV32:
1113 case RISCV::ZEXT_H_RV64:
1114 // c.sext.b, c.sext.h, c.zext.h
1115 NeedGPRC = true;
1116 return Subtarget.hasStdExtZcb() && Subtarget.hasStdExtZbb();
1117 case RISCV::ADD_UW:
1118 // c.zext.w
1119 NeedGPRC = true;
1120 return Subtarget.hasStdExtZcb() && MI.getOperand(2).isReg() &&
1121 MI.getOperand(2).getReg() == RISCV::X0;
1122 case RISCV::XORI:
1123 // c.not
1124 NeedGPRC = true;
1125 return Subtarget.hasStdExtZcb() && MI.getOperand(2).isImm() &&
1126 MI.getOperand(2).getImm() == -1;
1127 case RISCV::QC_EXTU:
1128 return MI.getOperand(2).getImm() >= 6 && MI.getOperand(3).getImm() == 0;
1129 case RISCV::BSETI:
1130 case RISCV::BEXTI:
1131 // qc.c.bseti, qc.c.bexti
1132 NeedGPRC = true;
1133 return Subtarget.hasVendorXqcibm() && MI.getOperand(2).getImm() != 0;
1134 }
1135 };
1136
1137 // Returns true if this operand is compressible. For non-registers it always
1138 // returns true. Immediate range was already checked in isCompressible.
1139 // For registers, it checks if the register is a GPRC register. reg-reg
1140 // instructions that require GPRC need all register operands to be GPRC.
1141 auto isCompressibleOpnd = [&](const MachineOperand &MO) {
1142 if (!MO.isReg())
1143 return true;
1144 Register Reg = MO.getReg();
1145 Register PhysReg = Reg.isPhysical() ? Reg : Register(VRM->getPhys(Reg));
1146 return PhysReg && RISCV::GPRCRegClass.contains(PhysReg);
1147 };
1148
1149 for (auto &MO : MRI->reg_nodbg_operands(VirtReg)) {
1150 const MachineInstr &MI = *MO.getParent();
1151 unsigned OpIdx = MO.getOperandNo();
1152 bool NeedGPRC;
1153 if (isCompressible(MI, NeedGPRC)) {
1154 if (OpIdx == 0 && MI.getOperand(1).isReg()) {
1155 if (!NeedGPRC || MI.getNumExplicitOperands() < 3 ||
1156 MI.getOpcode() == RISCV::ADD_UW ||
1157 isCompressibleOpnd(MI.getOperand(2)))
1158 tryAddHint(MO, MI.getOperand(1), NeedGPRC);
1159 if (MI.isCommutable() && MI.getOperand(2).isReg() &&
1160 (!NeedGPRC || isCompressibleOpnd(MI.getOperand(1))))
1161 tryAddHint(MO, MI.getOperand(2), NeedGPRC);
1162 } else if (OpIdx == 1 && (!NeedGPRC || MI.getNumExplicitOperands() < 3 ||
1163 isCompressibleOpnd(MI.getOperand(2)))) {
1164 tryAddHint(MO, MI.getOperand(0), NeedGPRC);
1165 } else if (MI.isCommutable() && OpIdx == 2 &&
1166 (!NeedGPRC || isCompressibleOpnd(MI.getOperand(1)))) {
1167 tryAddHint(MO, MI.getOperand(0), NeedGPRC);
1168 }
1169 }
1170
1171 // Add a hint if it would allow auipc/lui+addi(w) fusion. We do this even
1172 // without the fusions explicitly enabled as the impact is rarely negative
1173 // and some cores do implement this fusion.
1174 if ((MI.getOpcode() == RISCV::ADDIW || MI.getOpcode() == RISCV::ADDI) &&
1175 MI.getOperand(1).isReg()) {
1176 const MachineBasicBlock &MBB = *MI.getParent();
1177 MachineBasicBlock::const_iterator I = MI.getIterator();
1178 // Is the previous instruction a LUI or AUIPC that can be fused?
1179 if (I != MBB.begin()) {
1180 I = skipDebugInstructionsBackward(std::prev(I), MBB.begin());
1181 if ((I->getOpcode() == RISCV::LUI || I->getOpcode() == RISCV::AUIPC) &&
1182 I->getOperand(0).getReg() == MI.getOperand(1).getReg()) {
1183 if (OpIdx == 0)
1184 tryAddHint(MO, MI.getOperand(1), /*NeedGPRC=*/false);
1185 else
1186 tryAddHint(MO, MI.getOperand(0), /*NeedGPRC=*/false);
1187 }
1188 }
1189 }
1190 }
1191
1192 for (MCPhysReg OrderReg : Order)
1193 if (TwoAddrHints.count(OrderReg))
1194 Hints.push_back(OrderReg);
1195
1196 return BaseImplRetVal;
1197}
1198
1200 MachineFunction &MF) const {
1201 MachineRegisterInfo *MRI = &MF.getRegInfo();
1202 std::pair<unsigned, Register> Hint = MRI->getRegAllocationHint(Reg);
1203
1204 // Handle RegPairEven/RegPairOdd hints for Zilsd register pairs
1205 if ((Hint.first == RISCVRI::RegPairOdd ||
1206 Hint.first == RISCVRI::RegPairEven) &&
1207 Hint.second.isVirtual()) {
1208 // If 'Reg' is one of the even/odd register pair and it's now changed
1209 // (e.g. coalesced) into a different register, the other register of the
1210 // pair allocation hint must be updated to reflect the relationship change.
1211 Register Partner = Hint.second;
1212 std::pair<unsigned, Register> PartnerHint =
1213 MRI->getRegAllocationHint(Partner);
1214
1215 // Make sure partner still points to us
1216 if (PartnerHint.second == Reg) {
1217 // Update partner to point to NewReg instead of Reg
1218 MRI->setRegAllocationHint(Partner, PartnerHint.first, NewReg);
1219
1220 // If NewReg is virtual, set up the reciprocal hint
1221 // NewReg takes over Reg's role, so it gets the SAME hint type as Reg
1222 if (NewReg.isVirtual())
1223 MRI->setRegAllocationHint(NewReg, Hint.first, Partner);
1224 }
1225 }
1226}
1227
1230 uint16_t Encoding) const {
1231 MCRegister Reg = RISCV::V0 + Encoding;
1233 return Reg;
1234 return getMatchingSuperReg(Reg, RISCV::sub_vrm1_0, &RegClass);
1235}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
This file contains constants used for implementing Dwarf debug support.
const HexagonInstrInfo * TII
#define _
IRTranslator LLVM IR MI
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
Live Register Matrix
#define I(x, y, z)
Definition MD5.cpp:57
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
MachineInstr unsigned OpIdx
uint64_t IntrinsicInst * II
static unsigned getXqciloWideOpcode(unsigned Opc)
static cl::opt< bool > DisableRegAllocHints("riscv-disable-regalloc-hints", cl::Hidden, cl::init(false), cl::desc("Disable two address hints for register " "allocation"))
static cl::opt< bool > DisableCostPerUse("riscv-disable-cost-per-use", cl::init(false), cl::Hidden)
static std::tuple< RISCVVType::VLMUL, const TargetRegisterClass &, unsigned > getSpillReloadInfo(unsigned NumRemaining, uint16_t RegEncoding, bool IsSpill)
This file declares the machine register scavenger class.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:484
This file defines the SmallSet class.
static unsigned getDwarfRegNum(MCRegister Reg, const TargetRegisterInfo *TRI)
Go up the super-register chain until we hit a valid dwarf register number.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
static LLVM_ABI void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)
Append Ops with operations to apply the Offset.
A debug info location.
Definition DebugLoc.h:126
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
Definition Function.h:691
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:272
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:727
TypeSize getValue() const
bool isPrecise() const
const uint8_t TSFlags
Configurable target specific flags.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
constexpr unsigned id() const
Definition MCRegister.h:82
MachineInstrBundleIterator< const MachineInstr > const_iterator
MachineInstrBundleIterator< MachineInstr > iterator
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
int64_t getLocalFrameSize() const
Get the size of the local object blob.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags F, LLT MemTy, Align BaseAlignment, const MMOMetadata &Metadata=MMOMetadata(), SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addFrameIndex(int Idx) const
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const RegClassOrRegBank & getRegClassOrRegBank(Register Reg) const
Return the register bank or register class of Reg.
bool isReserved(MCRegister PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
LLT getType(Register Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register.
LLVM_ABI const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
std::pair< unsigned, Register > getRegAllocationHint(Register VReg) const
getRegAllocationHint - Return the register allocation hint for the specified virtual register.
void setRegAllocationHint(Register VReg, unsigned Type, Register PrefReg)
setRegAllocationHint - Specify a register allocation hint for the specified virtual register.
const MachineFunction & getMF() const
iterator_range< reg_nodbg_iterator > reg_nodbg_operands(Register Reg) const
bool hasBP(const MachineFunction &MF) const
void movImm(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, Register DstReg, uint64_t Val, MachineInstr::MIFlag Flag=MachineInstr::NoFlags, bool DstRenamable=false, bool DstIsDead=false) const
std::optional< unsigned > getRealVLen() const
const RISCVRegisterInfo * getRegisterInfo() const override
const RISCVInstrInfo * getInstrInfo() const override
This class implements the register bank concept.
unsigned getID() const
Get the identifier of this register bank.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition SmallSet.h:134
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition SmallSet.h:176
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
StackOffset holds a fixed and a scalable offset in bytes.
Definition TypeSize.h:30
int64_t getFixed() const
Returns the fixed component of the stack.
Definition TypeSize.h:46
static StackOffset get(int64_t Fixed, int64_t Scalable)
Definition TypeSize.h:41
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Information about stack frame layout on the target.
bool hasFP(const MachineFunction &MF) const
hasFP - Return true if the specified function should have a dedicated frame pointer register.
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual StringRef getRegAsmName(MCRegister Reg) const
Return the assembly name for Reg.
virtual bool getRegAllocationHints(Register VirtReg, ArrayRef< MCPhysReg > Order, SmallVectorImpl< MCPhysReg > &Hints, const MachineFunction &MF, const VirtRegMap *VRM=nullptr, const LiveRegMatrix *Matrix=nullptr) const
Get a list of 'hint' registers that the register allocator should try first when allocating a physica...
virtual bool isRegisterReservedByUser(Register R) const
virtual const TargetInstrInfo * getInstrInfo() const
MCRegister getPhys(Register virtReg) const
returns the physical register mapped to the specified virtual register
Definition VirtRegMap.h:91
bool hasPhys(Register virtReg) const
returns true if the specified virtual register is mapped to a physical register
Definition VirtRegMap.h:87
constexpr bool isKnownMultipleOf(ScalarTy RHS) const
This function tells the caller whether the element count is known at compile time to be a multiple of...
Definition TypeSize.h:180
constexpr LeafTy divideCoefficientBy(ScalarTy RHS) const
We do not provide the '/' operator here because division for polynomial types does not work in the sa...
Definition TypeSize.h:252
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ RISCV_VectorCall
Calling convention used for RISC-V V-extension.
@ PreserveMost
Used for runtime calls that preserves most registers.
Definition CallingConv.h:63
@ GHC
Used by the Glasgow Haskell Compiler (GHC).
Definition CallingConv.h:50
@ GRAAL
Used by GraalVM. Two additional registers are reserved.
MCRegister getBPReg()
static unsigned getFormat(uint64_t TSFlags)
static RISCVVType::VLMUL getLMul(uint8_t TSFlags)
LLVM_ABI std::pair< unsigned, bool > decodeVLMUL(VLMUL VLMul)
ArrayRef< MCPhysReg > getArgFPRs(const RISCVSubtarget &STI)
ArrayRef< MCPhysReg > getArgGPRs(const RISCVSubtarget &STI)
ArrayRef< MCPhysReg > getArgVRs(const RISCVSubtarget &STI)
std::optional< std::pair< unsigned, unsigned > > isRVVSpillForZvlsseg(unsigned Opcode)
bool isRVVSpill(const MachineInstr &MI)
static constexpr unsigned RVVBytesPerBlock
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:578
PointerUnion< const TargetRegisterClass *, const RegisterBank * > RegClassOrRegBank
Convenient type to represent either a register class or a register bank.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Kill
The last use of a register.
constexpr RegState getKillRegState(bool B)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:332
constexpr RegState getDefRegState(bool B)
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
IterT skipDebugInstructionsBackward(IterT It, IterT Begin, bool SkipPseudoOp=true)
Decrement It until it points to a non-debug instruction or to Begin and return the resulting iterator...
@ Sub
Subtraction of integers.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
constexpr bool isShiftedInt(int64_t x)
Checks if a signed integer is an N bit number shifted left by S.
Definition MathExtras.h:183
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:573
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition Alignment.h:130
bool needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const override
bool requiresVirtualBaseRegisters(const MachineFunction &MF) const override
Register findVRegWithEncoding(const TargetRegisterClass &RegClass, uint16_t Encoding) const
const TargetRegisterClass * getLargestLegalSuperClass(const TargetRegisterClass *RC, const MachineFunction &) const override
bool isArgumentRegister(const MachineFunction &MF, MCRegister Reg) const override
const MCPhysReg * getCalleeSavedRegs(const MachineFunction *MF) const override
BitVector getReservedRegs(const MachineFunction &MF) const override
const uint32_t * getCallPreservedMask(const MachineFunction &MF, CallingConv::ID) const override
Register materializeFrameBaseRegister(MachineBasicBlock *MBB, int FrameIdx, int64_t Offset) const override
RISCVRegisterInfo(unsigned HwMode)
void getOffsetOpcodes(const StackOffset &Offset, SmallVectorImpl< uint64_t > &Ops) const override
const TargetRegisterClass * getConstrainedRegClassForOperand(const MachineOperand &MO, const MachineRegisterInfo &MRI) const override
bool isFrameOffsetLegal(const MachineInstr *MI, Register BaseReg, int64_t Offset) const override
Register getFrameRegister(const MachineFunction &MF) const override
const MCPhysReg * getIPRACSRegs(const MachineFunction *MF) const override
void lowerSegmentSpillReload(MachineBasicBlock::iterator II, bool IsSpill) const
const TargetRegisterClass * getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB, bool Is64Bit) const
void adjustReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator II, const DebugLoc &DL, Register DestReg, Register SrcReg, StackOffset Offset, MachineInstr::MIFlag Flag, MaybeAlign RequiredAlign) const
void updateRegAllocHint(Register Reg, Register NewReg, MachineFunction &MF) const override
bool isAsmClobberable(const MachineFunction &MF, MCRegister PhysReg) const override
const uint32_t * getNoPreservedMask() const override
float getSpillWeightScaleFactor(const TargetRegisterClass *RC) const override
void resolveFrameIndex(MachineInstr &MI, Register BaseReg, int64_t Offset) const override
bool getRegAllocationHints(Register VirtReg, ArrayRef< MCPhysReg > Order, SmallVectorImpl< MCPhysReg > &Hints, const MachineFunction &MF, const VirtRegMap *VRM, const LiveRegMatrix *Matrix) const override
int64_t getFrameIndexInstrOffset(const MachineInstr *MI, int Idx) const override
unsigned getRegisterCostTableIndex(const MachineFunction &MF) const override
StringRef getRegAsmName(MCRegister Reg) const override
bool eliminateFrameIndex(MachineBasicBlock::iterator MI, int SPAdj, unsigned FIOperandNum, RegScavenger *RS=nullptr) const override