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"
17 #include "llvm/BinaryFormat/Dwarf.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/RegisterScavenging.h"
22 #include "llvm/CodeGen/TargetFrameLowering.h"
23 #include "llvm/CodeGen/TargetInstrInfo.h"
24 #include "llvm/IR/DebugInfoMetadata.h"
25 #include "llvm/Support/ErrorHandling.h"
26
27 #define GET_REGINFO_TARGET_DESC
28 #include "RISCVGenRegisterInfo.inc"
29
30 using namespace llvm;
31
32 static cl::opt<bool> DisableCostPerUse("riscv-disable-cost-per-use",
33 cl::init(false), cl::Hidden);
34 static 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
40 static_assert(RISCV::X1 == RISCV::X0 + 1, "Register list not consecutive");
41 static_assert(RISCV::X31 == RISCV::X0 + 31, "Register list not consecutive");
42 static_assert(RISCV::F1_H == RISCV::F0_H + 1, "Register list not consecutive");
43 static_assert(RISCV::F31_H == RISCV::F0_H + 31,
44 "Register list not consecutive");
45 static_assert(RISCV::F1_F == RISCV::F0_F + 1, "Register list not consecutive");
46 static_assert(RISCV::F31_F == RISCV::F0_F + 31,
47 "Register list not consecutive");
48 static_assert(RISCV::F1_D == RISCV::F0_D + 1, "Register list not consecutive");
49 static_assert(RISCV::F31_D == RISCV::F0_D + 31,
50 "Register list not consecutive");
51 static_assert(RISCV::F1_Q == RISCV::F0_Q + 1, "Register list not consecutive");
52 static_assert(RISCV::F31_Q == RISCV::F0_Q + 31,
53 "Register list not consecutive");
54 static_assert(RISCV::V1 == RISCV::V0 + 1, "Register list not consecutive");
55 static_assert(RISCV::V31 == RISCV::V0 + 31, "Register list not consecutive");
56
RISCVRegisterInfo(unsigned HwMode)57 RISCVRegisterInfo::RISCVRegisterInfo(unsigned HwMode)
58 : RISCVGenRegisterInfo(RISCV::X1, /*DwarfFlavour*/0, /*EHFlavor*/0,
59 /*PC*/0, HwMode) {}
60
61 const MCPhysReg *
getIPRACSRegs(const MachineFunction * MF) const62 RISCVRegisterInfo::getIPRACSRegs(const MachineFunction *MF) const {
63 return CSR_IPRA_SaveList;
64 }
65
66 const MCPhysReg *
getCalleeSavedRegs(const MachineFunction * MF) const67 RISCVRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
68 auto &Subtarget = MF->getSubtarget<RISCVSubtarget>();
69 if (MF->getFunction().getCallingConv() == CallingConv::GHC)
70 return CSR_NoRegs_SaveList;
71 if (MF->getFunction().getCallingConv() == CallingConv::PreserveMost)
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 =
96 MF->getFunction().getCallingConv() == CallingConv::RISCV_VectorCall &&
97 Subtarget.hasVInstructions();
98
99 switch (Subtarget.getTargetABI()) {
100 default:
101 llvm_unreachable("Unrecognized ABI");
102 case RISCVABI::ABI_ILP32E:
103 case RISCVABI::ABI_LP64E:
104 return CSR_ILP32E_LP64E_SaveList;
105 case RISCVABI::ABI_ILP32:
106 case RISCVABI::ABI_LP64:
107 if (HasVectorCSR)
108 return CSR_ILP32_LP64_V_SaveList;
109 return CSR_ILP32_LP64_SaveList;
110 case RISCVABI::ABI_ILP32F:
111 case RISCVABI::ABI_LP64F:
112 if (HasVectorCSR)
113 return CSR_ILP32F_LP64F_V_SaveList;
114 return CSR_ILP32F_LP64F_SaveList;
115 case RISCVABI::ABI_ILP32D:
116 case RISCVABI::ABI_LP64D:
117 if (HasVectorCSR)
118 return CSR_ILP32D_LP64D_V_SaveList;
119 return CSR_ILP32D_LP64D_SaveList;
120 }
121 }
122
getReservedRegs(const MachineFunction & MF) const123 BitVector RISCVRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
124 const RISCVFrameLowering *TFI = getFrameLowering(MF);
125 BitVector Reserved(getNumRegs());
126 auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
127
128 for (size_t Reg = 0; Reg < getNumRegs(); Reg++) {
129 // Mark any GPRs requested to be reserved as such
130 if (Subtarget.isRegisterReservedByUser(Reg))
131 markSuperRegs(Reserved, Reg);
132
133 // Mark all the registers defined as constant in TableGen as reserved.
134 if (isConstantPhysReg(Reg))
135 markSuperRegs(Reserved, Reg);
136 }
137
138 // Use markSuperRegs to ensure any register aliases are also reserved
139 markSuperRegs(Reserved, RISCV::X2_H); // sp
140 markSuperRegs(Reserved, RISCV::X3_H); // gp
141 markSuperRegs(Reserved, RISCV::X4_H); // tp
142 if (TFI->hasFP(MF))
143 markSuperRegs(Reserved, RISCV::X8_H); // fp
144 // Reserve the base register if we need to realign the stack and allocate
145 // variable-sized objects at runtime.
146 if (TFI->hasBP(MF))
147 markSuperRegs(Reserved, RISCVABI::getBPReg()); // bp
148
149 // Additionally reserve dummy register used to form the register pair
150 // beginning with 'x0' for instructions that take register pairs.
151 markSuperRegs(Reserved, RISCV::DUMMY_REG_PAIR_WITH_X0);
152
153 // There are only 16 GPRs for RVE.
154 if (Subtarget.hasStdExtE())
155 for (MCPhysReg Reg = RISCV::X16_H; Reg <= RISCV::X31_H; Reg++)
156 markSuperRegs(Reserved, Reg);
157
158 // V registers for code generation. We handle them manually.
159 markSuperRegs(Reserved, RISCV::VL);
160 markSuperRegs(Reserved, RISCV::VTYPE);
161 markSuperRegs(Reserved, RISCV::VXSAT);
162 markSuperRegs(Reserved, RISCV::VXRM);
163
164 // Floating point environment registers.
165 markSuperRegs(Reserved, RISCV::FRM);
166 markSuperRegs(Reserved, RISCV::FFLAGS);
167
168 // SiFive VCIX state registers.
169 markSuperRegs(Reserved, RISCV::SF_VCIX_STATE);
170
171 if (MF.getFunction().getCallingConv() == CallingConv::GRAAL) {
172 if (Subtarget.hasStdExtE())
173 report_fatal_error("Graal reserved registers do not exist in RVE");
174 markSuperRegs(Reserved, RISCV::X23_H);
175 markSuperRegs(Reserved, RISCV::X27_H);
176 }
177
178 // Shadow stack pointer.
179 markSuperRegs(Reserved, RISCV::SSP);
180
181 assert(checkAllSuperRegsMarked(Reserved));
182 return Reserved;
183 }
184
isAsmClobberable(const MachineFunction & MF,MCRegister PhysReg) const185 bool RISCVRegisterInfo::isAsmClobberable(const MachineFunction &MF,
186 MCRegister PhysReg) const {
187 return !MF.getSubtarget().isRegisterReservedByUser(PhysReg);
188 }
189
getNoPreservedMask() const190 const uint32_t *RISCVRegisterInfo::getNoPreservedMask() const {
191 return CSR_NoRegs_RegMask;
192 }
193
adjustReg(MachineBasicBlock & MBB,MachineBasicBlock::iterator II,const DebugLoc & DL,Register DestReg,Register SrcReg,StackOffset Offset,MachineInstr::MIFlag Flag,MaybeAlign RequiredAlign) const194 void RISCVRegisterInfo::adjustReg(MachineBasicBlock &MBB,
195 MachineBasicBlock::iterator II,
196 const DebugLoc &DL, Register DestReg,
197 Register SrcReg, StackOffset Offset,
198 MachineInstr::MIFlag Flag,
199 MaybeAlign RequiredAlign) const {
200
201 if (DestReg == SrcReg && !Offset.getFixed() && !Offset.getScalable())
202 return;
203
204 MachineFunction &MF = *MBB.getParent();
205 MachineRegisterInfo &MRI = MF.getRegInfo();
206 const RISCVSubtarget &ST = MF.getSubtarget<RISCVSubtarget>();
207 const RISCVInstrInfo *TII = ST.getInstrInfo();
208
209 // Optimize compile time offset case
210 if (Offset.getScalable()) {
211 if (auto VLEN = ST.getRealVLen()) {
212 // 1. Multiply the number of v-slots by the (constant) length of register
213 const int64_t VLENB = *VLEN / 8;
214 assert(Offset.getScalable() % RISCV::RVVBytesPerBlock == 0 &&
215 "Reserve the stack by the multiple of one vector size.");
216 const int64_t NumOfVReg = Offset.getScalable() / 8;
217 const int64_t FixedOffset = NumOfVReg * VLENB;
218 if (!isInt<32>(FixedOffset)) {
219 report_fatal_error(
220 "Frame size outside of the signed 32-bit range not supported");
221 }
222 Offset = StackOffset::getFixed(FixedOffset + Offset.getFixed());
223 }
224 }
225
226 bool KillSrcReg = false;
227
228 if (Offset.getScalable()) {
229 unsigned ScalableAdjOpc = RISCV::ADD;
230 int64_t ScalableValue = Offset.getScalable();
231 if (ScalableValue < 0) {
232 ScalableValue = -ScalableValue;
233 ScalableAdjOpc = RISCV::SUB;
234 }
235 // Get vlenb and multiply vlen with the number of vector registers.
236 Register ScratchReg = DestReg;
237 if (DestReg == SrcReg)
238 ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
239
240 assert(ScalableValue > 0 && "There is no need to get VLEN scaled value.");
241 assert(ScalableValue % RISCV::RVVBytesPerBlock == 0 &&
242 "Reserve the stack by the multiple of one vector size.");
243 assert(isInt<32>(ScalableValue / RISCV::RVVBytesPerBlock) &&
244 "Expect the number of vector registers within 32-bits.");
245 uint32_t NumOfVReg = ScalableValue / RISCV::RVVBytesPerBlock;
246 // Only use vsetvli rather than vlenb if adjusting in the prologue or
247 // epilogue, otherwise it may disturb the VTYPE and VL status.
248 bool IsPrologueOrEpilogue =
249 Flag == MachineInstr::FrameSetup || Flag == MachineInstr::FrameDestroy;
250 bool UseVsetvliRatherThanVlenb =
251 IsPrologueOrEpilogue && ST.preferVsetvliOverReadVLENB();
252 if (UseVsetvliRatherThanVlenb && (NumOfVReg == 1 || NumOfVReg == 2 ||
253 NumOfVReg == 4 || NumOfVReg == 8)) {
254 BuildMI(MBB, II, DL, TII->get(RISCV::PseudoReadVLENBViaVSETVLIX0),
255 ScratchReg)
256 .addImm(NumOfVReg)
257 .setMIFlag(Flag);
258 BuildMI(MBB, II, DL, TII->get(ScalableAdjOpc), DestReg)
259 .addReg(SrcReg)
260 .addReg(ScratchReg, RegState::Kill)
261 .setMIFlag(Flag);
262 } else {
263 if (UseVsetvliRatherThanVlenb)
264 BuildMI(MBB, II, DL, TII->get(RISCV::PseudoReadVLENBViaVSETVLIX0),
265 ScratchReg)
266 .addImm(1)
267 .setMIFlag(Flag);
268 else
269 BuildMI(MBB, II, DL, TII->get(RISCV::PseudoReadVLENB), ScratchReg)
270 .setMIFlag(Flag);
271
272 if (ScalableAdjOpc == RISCV::ADD && ST.hasStdExtZba() &&
273 (NumOfVReg == 2 || NumOfVReg == 4 || NumOfVReg == 8)) {
274 unsigned Opc = NumOfVReg == 2
275 ? RISCV::SH1ADD
276 : (NumOfVReg == 4 ? RISCV::SH2ADD : RISCV::SH3ADD);
277 BuildMI(MBB, II, DL, TII->get(Opc), DestReg)
278 .addReg(ScratchReg, RegState::Kill)
279 .addReg(SrcReg)
280 .setMIFlag(Flag);
281 } else {
282 TII->mulImm(MF, MBB, II, DL, ScratchReg, NumOfVReg, Flag);
283 BuildMI(MBB, II, DL, TII->get(ScalableAdjOpc), DestReg)
284 .addReg(SrcReg)
285 .addReg(ScratchReg, RegState::Kill)
286 .setMIFlag(Flag);
287 }
288 }
289 SrcReg = DestReg;
290 KillSrcReg = true;
291 }
292
293 int64_t Val = Offset.getFixed();
294 if (DestReg == SrcReg && Val == 0)
295 return;
296
297 const uint64_t Align = RequiredAlign.valueOrOne().value();
298
299 if (isInt<12>(Val)) {
300 BuildMI(MBB, II, DL, TII->get(RISCV::ADDI), DestReg)
301 .addReg(SrcReg, getKillRegState(KillSrcReg))
302 .addImm(Val)
303 .setMIFlag(Flag);
304 return;
305 }
306
307 // Use the QC_E_ADDI instruction from the Xqcilia extension that can take a
308 // signed 26-bit immediate.
309 if (ST.hasVendorXqcilia() && isInt<26>(Val)) {
310 // The one case where using this instruction is sub-optimal is if Val can be
311 // materialized with a single compressible LUI and following add/sub is also
312 // compressible. Avoid doing this if that is the case.
313 int Hi20 = (Val & 0xFFFFF000) >> 12;
314 bool IsCompressLUI =
315 ((Val & 0xFFF) == 0) && (Hi20 != 0) &&
316 (isUInt<5>(Hi20) || (Hi20 >= 0xfffe0 && Hi20 <= 0xfffff));
317 bool IsCompressAddSub =
318 (SrcReg == DestReg) &&
319 ((Val > 0 && RISCV::GPRNoX0RegClass.contains(SrcReg)) ||
320 (Val < 0 && RISCV::GPRCRegClass.contains(SrcReg)));
321
322 if (!(IsCompressLUI && IsCompressAddSub)) {
323 BuildMI(MBB, II, DL, TII->get(RISCV::QC_E_ADDI), DestReg)
324 .addReg(SrcReg, getKillRegState(KillSrcReg))
325 .addImm(Val)
326 .setMIFlag(Flag);
327 return;
328 }
329 }
330
331 // Try to split the offset across two ADDIs. We need to keep the intermediate
332 // result aligned after each ADDI. We need to determine the maximum value we
333 // can put in each ADDI. In the negative direction, we can use -2048 which is
334 // always sufficiently aligned. In the positive direction, we need to find the
335 // largest 12-bit immediate that is aligned. Exclude -4096 since it can be
336 // created with LUI.
337 assert(Align < 2048 && "Required alignment too large");
338 int64_t MaxPosAdjStep = 2048 - Align;
339 if (Val > -4096 && Val <= (2 * MaxPosAdjStep)) {
340 int64_t FirstAdj = Val < 0 ? -2048 : MaxPosAdjStep;
341 Val -= FirstAdj;
342 BuildMI(MBB, II, DL, TII->get(RISCV::ADDI), DestReg)
343 .addReg(SrcReg, getKillRegState(KillSrcReg))
344 .addImm(FirstAdj)
345 .setMIFlag(Flag);
346 BuildMI(MBB, II, DL, TII->get(RISCV::ADDI), DestReg)
347 .addReg(DestReg, RegState::Kill)
348 .addImm(Val)
349 .setMIFlag(Flag);
350 return;
351 }
352
353 // Use shNadd if doing so lets us materialize a 12 bit immediate with a single
354 // instruction. This saves 1 instruction over the full lui/addi+add fallback
355 // path. We avoid anything which can be done with a single lui as it might
356 // be compressible. Note that the sh1add case is fully covered by the 2x addi
357 // case just above and is thus omitted.
358 if (ST.hasStdExtZba() && (Val & 0xFFF) != 0) {
359 unsigned Opc = 0;
360 if (isShiftedInt<12, 3>(Val)) {
361 Opc = RISCV::SH3ADD;
362 Val = Val >> 3;
363 } else if (isShiftedInt<12, 2>(Val)) {
364 Opc = RISCV::SH2ADD;
365 Val = Val >> 2;
366 }
367 if (Opc) {
368 Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
369 TII->movImm(MBB, II, DL, ScratchReg, Val, Flag);
370 BuildMI(MBB, II, DL, TII->get(Opc), DestReg)
371 .addReg(ScratchReg, RegState::Kill)
372 .addReg(SrcReg, getKillRegState(KillSrcReg))
373 .setMIFlag(Flag);
374 return;
375 }
376 }
377
378 unsigned Opc = RISCV::ADD;
379 if (Val < 0) {
380 Val = -Val;
381 Opc = RISCV::SUB;
382 }
383
384 Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
385 TII->movImm(MBB, II, DL, ScratchReg, Val, Flag);
386 BuildMI(MBB, II, DL, TII->get(Opc), DestReg)
387 .addReg(SrcReg, getKillRegState(KillSrcReg))
388 .addReg(ScratchReg, RegState::Kill)
389 .setMIFlag(Flag);
390 }
391
392 // Split a VSPILLx_Mx pseudo into multiple whole register stores separated by
393 // LMUL*VLENB bytes.
lowerVSPILL(MachineBasicBlock::iterator II) const394 void RISCVRegisterInfo::lowerVSPILL(MachineBasicBlock::iterator II) const {
395 DebugLoc DL = II->getDebugLoc();
396 MachineBasicBlock &MBB = *II->getParent();
397 MachineFunction &MF = *MBB.getParent();
398 MachineRegisterInfo &MRI = MF.getRegInfo();
399 const RISCVSubtarget &STI = MF.getSubtarget<RISCVSubtarget>();
400 const TargetInstrInfo *TII = STI.getInstrInfo();
401 const TargetRegisterInfo *TRI = STI.getRegisterInfo();
402
403 auto ZvlssegInfo = RISCV::isRVVSpillForZvlsseg(II->getOpcode());
404 unsigned NF = ZvlssegInfo->first;
405 unsigned LMUL = ZvlssegInfo->second;
406 assert(NF * LMUL <= 8 && "Invalid NF/LMUL combinations.");
407 unsigned Opcode, SubRegIdx;
408 switch (LMUL) {
409 default:
410 llvm_unreachable("LMUL must be 1, 2, or 4.");
411 case 1:
412 Opcode = RISCV::VS1R_V;
413 SubRegIdx = RISCV::sub_vrm1_0;
414 break;
415 case 2:
416 Opcode = RISCV::VS2R_V;
417 SubRegIdx = RISCV::sub_vrm2_0;
418 break;
419 case 4:
420 Opcode = RISCV::VS4R_V;
421 SubRegIdx = RISCV::sub_vrm4_0;
422 break;
423 }
424 static_assert(RISCV::sub_vrm1_7 == RISCV::sub_vrm1_0 + 7,
425 "Unexpected subreg numbering");
426 static_assert(RISCV::sub_vrm2_3 == RISCV::sub_vrm2_0 + 3,
427 "Unexpected subreg numbering");
428 static_assert(RISCV::sub_vrm4_1 == RISCV::sub_vrm4_0 + 1,
429 "Unexpected subreg numbering");
430
431 Register VL = MRI.createVirtualRegister(&RISCV::GPRRegClass);
432 // Optimize for constant VLEN.
433 if (auto VLEN = STI.getRealVLen()) {
434 const int64_t VLENB = *VLEN / 8;
435 int64_t Offset = VLENB * LMUL;
436 STI.getInstrInfo()->movImm(MBB, II, DL, VL, Offset);
437 } else {
438 BuildMI(MBB, II, DL, TII->get(RISCV::PseudoReadVLENB), VL);
439 uint32_t ShiftAmount = Log2_32(LMUL);
440 if (ShiftAmount != 0)
441 BuildMI(MBB, II, DL, TII->get(RISCV::SLLI), VL)
442 .addReg(VL)
443 .addImm(ShiftAmount);
444 }
445
446 Register SrcReg = II->getOperand(0).getReg();
447 Register Base = II->getOperand(1).getReg();
448 bool IsBaseKill = II->getOperand(1).isKill();
449 Register NewBase = MRI.createVirtualRegister(&RISCV::GPRRegClass);
450
451 auto *OldMMO = *(II->memoperands_begin());
452 LocationSize OldLoc = OldMMO->getSize();
453 assert(OldLoc.isPrecise() && OldLoc.getValue().isKnownMultipleOf(NF));
454 TypeSize NewSize = OldLoc.getValue().divideCoefficientBy(NF);
455 auto *NewMMO = MF.getMachineMemOperand(OldMMO, OldMMO->getOffset(), NewSize);
456 for (unsigned I = 0; I < NF; ++I) {
457 // Adding implicit-use of super register to describe we are using part of
458 // super register, that prevents machine verifier complaining when part of
459 // subreg is undef, see comment in MachineVerifier::checkLiveness for more
460 // detail.
461 BuildMI(MBB, II, DL, TII->get(Opcode))
462 .addReg(TRI->getSubReg(SrcReg, SubRegIdx + I))
463 .addReg(Base, getKillRegState(I == NF - 1))
464 .addMemOperand(NewMMO)
465 .addReg(SrcReg, RegState::Implicit);
466 if (I != NF - 1)
467 BuildMI(MBB, II, DL, TII->get(RISCV::ADD), NewBase)
468 .addReg(Base, getKillRegState(I != 0 || IsBaseKill))
469 .addReg(VL, getKillRegState(I == NF - 2));
470 Base = NewBase;
471 }
472 II->eraseFromParent();
473 }
474
475 // Split a VSPILLx_Mx pseudo into multiple whole register loads separated by
476 // LMUL*VLENB bytes.
lowerVRELOAD(MachineBasicBlock::iterator II) const477 void RISCVRegisterInfo::lowerVRELOAD(MachineBasicBlock::iterator II) const {
478 DebugLoc DL = II->getDebugLoc();
479 MachineBasicBlock &MBB = *II->getParent();
480 MachineFunction &MF = *MBB.getParent();
481 MachineRegisterInfo &MRI = MF.getRegInfo();
482 const RISCVSubtarget &STI = MF.getSubtarget<RISCVSubtarget>();
483 const TargetInstrInfo *TII = STI.getInstrInfo();
484 const TargetRegisterInfo *TRI = STI.getRegisterInfo();
485
486 auto ZvlssegInfo = RISCV::isRVVSpillForZvlsseg(II->getOpcode());
487 unsigned NF = ZvlssegInfo->first;
488 unsigned LMUL = ZvlssegInfo->second;
489 assert(NF * LMUL <= 8 && "Invalid NF/LMUL combinations.");
490 unsigned Opcode, SubRegIdx;
491 switch (LMUL) {
492 default:
493 llvm_unreachable("LMUL must be 1, 2, or 4.");
494 case 1:
495 Opcode = RISCV::VL1RE8_V;
496 SubRegIdx = RISCV::sub_vrm1_0;
497 break;
498 case 2:
499 Opcode = RISCV::VL2RE8_V;
500 SubRegIdx = RISCV::sub_vrm2_0;
501 break;
502 case 4:
503 Opcode = RISCV::VL4RE8_V;
504 SubRegIdx = RISCV::sub_vrm4_0;
505 break;
506 }
507 static_assert(RISCV::sub_vrm1_7 == RISCV::sub_vrm1_0 + 7,
508 "Unexpected subreg numbering");
509 static_assert(RISCV::sub_vrm2_3 == RISCV::sub_vrm2_0 + 3,
510 "Unexpected subreg numbering");
511 static_assert(RISCV::sub_vrm4_1 == RISCV::sub_vrm4_0 + 1,
512 "Unexpected subreg numbering");
513
514 Register VL = MRI.createVirtualRegister(&RISCV::GPRRegClass);
515 // Optimize for constant VLEN.
516 if (auto VLEN = STI.getRealVLen()) {
517 const int64_t VLENB = *VLEN / 8;
518 int64_t Offset = VLENB * LMUL;
519 STI.getInstrInfo()->movImm(MBB, II, DL, VL, Offset);
520 } else {
521 BuildMI(MBB, II, DL, TII->get(RISCV::PseudoReadVLENB), VL);
522 uint32_t ShiftAmount = Log2_32(LMUL);
523 if (ShiftAmount != 0)
524 BuildMI(MBB, II, DL, TII->get(RISCV::SLLI), VL)
525 .addReg(VL)
526 .addImm(ShiftAmount);
527 }
528
529 Register DestReg = II->getOperand(0).getReg();
530 Register Base = II->getOperand(1).getReg();
531 bool IsBaseKill = II->getOperand(1).isKill();
532 Register NewBase = MRI.createVirtualRegister(&RISCV::GPRRegClass);
533 auto *OldMMO = *(II->memoperands_begin());
534 LocationSize OldLoc = OldMMO->getSize();
535 assert(OldLoc.isPrecise() && OldLoc.getValue().isKnownMultipleOf(NF));
536 TypeSize NewSize = OldLoc.getValue().divideCoefficientBy(NF);
537 auto *NewMMO = MF.getMachineMemOperand(OldMMO, OldMMO->getOffset(), NewSize);
538 for (unsigned I = 0; I < NF; ++I) {
539 BuildMI(MBB, II, DL, TII->get(Opcode),
540 TRI->getSubReg(DestReg, SubRegIdx + I))
541 .addReg(Base, getKillRegState(I == NF - 1))
542 .addMemOperand(NewMMO);
543 if (I != NF - 1)
544 BuildMI(MBB, II, DL, TII->get(RISCV::ADD), NewBase)
545 .addReg(Base, getKillRegState(I != 0 || IsBaseKill))
546 .addReg(VL, getKillRegState(I == NF - 2));
547 Base = NewBase;
548 }
549 II->eraseFromParent();
550 }
551
eliminateFrameIndex(MachineBasicBlock::iterator II,int SPAdj,unsigned FIOperandNum,RegScavenger * RS) const552 bool RISCVRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
553 int SPAdj, unsigned FIOperandNum,
554 RegScavenger *RS) const {
555 assert(SPAdj == 0 && "Unexpected non-zero SPAdj value");
556
557 MachineInstr &MI = *II;
558 MachineFunction &MF = *MI.getParent()->getParent();
559 MachineRegisterInfo &MRI = MF.getRegInfo();
560 DebugLoc DL = MI.getDebugLoc();
561
562 int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
563 Register FrameReg;
564 StackOffset Offset =
565 getFrameLowering(MF)->getFrameIndexReference(MF, FrameIndex, FrameReg);
566 bool IsRVVSpill = RISCV::isRVVSpill(MI);
567 if (!IsRVVSpill)
568 Offset += StackOffset::getFixed(MI.getOperand(FIOperandNum + 1).getImm());
569
570 if (!isInt<32>(Offset.getFixed())) {
571 report_fatal_error(
572 "Frame offsets outside of the signed 32-bit range not supported");
573 }
574
575 if (!IsRVVSpill) {
576 int64_t Val = Offset.getFixed();
577 int64_t Lo12 = SignExtend64<12>(Val);
578 unsigned Opc = MI.getOpcode();
579 if (Opc == RISCV::ADDI && !isInt<12>(Val)) {
580 // We chose to emit the canonical immediate sequence rather than folding
581 // the offset into the using add under the theory that doing so doesn't
582 // save dynamic instruction count and some target may fuse the canonical
583 // 32 bit immediate sequence. We still need to clear the portion of the
584 // offset encoded in the immediate.
585 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(0);
586 } else if ((Opc == RISCV::PREFETCH_I || Opc == RISCV::PREFETCH_R ||
587 Opc == RISCV::PREFETCH_W) &&
588 (Lo12 & 0b11111) != 0) {
589 // Prefetch instructions require the offset to be 32 byte aligned.
590 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(0);
591 } else if ((Opc == RISCV::PseudoRV32ZdinxLD ||
592 Opc == RISCV::PseudoRV32ZdinxSD) &&
593 Lo12 >= 2044) {
594 // This instruction will be split into 2 instructions. The second
595 // instruction will add 4 to the immediate. If that would overflow 12
596 // bits, we can't fold the offset.
597 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(0);
598 } else {
599 // We can encode an add with 12 bit signed immediate in the immediate
600 // operand of our user instruction. As a result, the remaining
601 // offset can by construction, at worst, a LUI and a ADD.
602 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Lo12);
603 Offset = StackOffset::get((uint64_t)Val - (uint64_t)Lo12,
604 Offset.getScalable());
605 }
606 }
607
608 if (Offset.getScalable() || Offset.getFixed()) {
609 Register DestReg;
610 if (MI.getOpcode() == RISCV::ADDI)
611 DestReg = MI.getOperand(0).getReg();
612 else
613 DestReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
614 adjustReg(*II->getParent(), II, DL, DestReg, FrameReg, Offset,
615 MachineInstr::NoFlags, std::nullopt);
616 MI.getOperand(FIOperandNum).ChangeToRegister(DestReg, /*IsDef*/false,
617 /*IsImp*/false,
618 /*IsKill*/true);
619 } else {
620 MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, /*IsDef*/false,
621 /*IsImp*/false,
622 /*IsKill*/false);
623 }
624
625 // If after materializing the adjustment, we have a pointless ADDI, remove it
626 if (MI.getOpcode() == RISCV::ADDI &&
627 MI.getOperand(0).getReg() == MI.getOperand(1).getReg() &&
628 MI.getOperand(2).getImm() == 0) {
629 MI.eraseFromParent();
630 return true;
631 }
632
633 // Handle spill/fill of synthetic register classes for segment operations to
634 // ensure correctness in the edge case one gets spilled. There are many
635 // possible optimizations here, but given the extreme rarity of such spills,
636 // we prefer simplicity of implementation for now.
637 switch (MI.getOpcode()) {
638 case RISCV::PseudoVSPILL2_M1:
639 case RISCV::PseudoVSPILL2_M2:
640 case RISCV::PseudoVSPILL2_M4:
641 case RISCV::PseudoVSPILL3_M1:
642 case RISCV::PseudoVSPILL3_M2:
643 case RISCV::PseudoVSPILL4_M1:
644 case RISCV::PseudoVSPILL4_M2:
645 case RISCV::PseudoVSPILL5_M1:
646 case RISCV::PseudoVSPILL6_M1:
647 case RISCV::PseudoVSPILL7_M1:
648 case RISCV::PseudoVSPILL8_M1:
649 lowerVSPILL(II);
650 return true;
651 case RISCV::PseudoVRELOAD2_M1:
652 case RISCV::PseudoVRELOAD2_M2:
653 case RISCV::PseudoVRELOAD2_M4:
654 case RISCV::PseudoVRELOAD3_M1:
655 case RISCV::PseudoVRELOAD3_M2:
656 case RISCV::PseudoVRELOAD4_M1:
657 case RISCV::PseudoVRELOAD4_M2:
658 case RISCV::PseudoVRELOAD5_M1:
659 case RISCV::PseudoVRELOAD6_M1:
660 case RISCV::PseudoVRELOAD7_M1:
661 case RISCV::PseudoVRELOAD8_M1:
662 lowerVRELOAD(II);
663 return true;
664 }
665
666 return false;
667 }
668
requiresVirtualBaseRegisters(const MachineFunction & MF) const669 bool RISCVRegisterInfo::requiresVirtualBaseRegisters(
670 const MachineFunction &MF) const {
671 return true;
672 }
673
674 // Returns true if the instruction's frame index reference would be better
675 // served by a base register other than FP or SP.
676 // Used by LocalStackSlotAllocation pass to determine which frame index
677 // references it should create new base registers for.
needsFrameBaseReg(MachineInstr * MI,int64_t Offset) const678 bool RISCVRegisterInfo::needsFrameBaseReg(MachineInstr *MI,
679 int64_t Offset) const {
680 unsigned FIOperandNum = 0;
681 for (; !MI->getOperand(FIOperandNum).isFI(); FIOperandNum++)
682 assert(FIOperandNum < MI->getNumOperands() &&
683 "Instr doesn't have FrameIndex operand");
684
685 // For RISC-V, The machine instructions that include a FrameIndex operand
686 // are load/store, ADDI instructions.
687 unsigned MIFrm = RISCVII::getFormat(MI->getDesc().TSFlags);
688 if (MIFrm != RISCVII::InstFormatI && MIFrm != RISCVII::InstFormatS)
689 return false;
690 // We only generate virtual base registers for loads and stores, so
691 // return false for everything else.
692 if (!MI->mayLoad() && !MI->mayStore())
693 return false;
694
695 const MachineFunction &MF = *MI->getMF();
696 const MachineFrameInfo &MFI = MF.getFrameInfo();
697 const RISCVFrameLowering *TFI = getFrameLowering(MF);
698 const MachineRegisterInfo &MRI = MF.getRegInfo();
699
700 if (TFI->hasFP(MF) && !shouldRealignStack(MF)) {
701 auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
702 // Estimate the stack size used to store callee saved registers(
703 // excludes reserved registers).
704 unsigned CalleeSavedSize = 0;
705 for (const MCPhysReg *R = MRI.getCalleeSavedRegs(); MCPhysReg Reg = *R;
706 ++R) {
707 if (Subtarget.isRegisterReservedByUser(Reg))
708 continue;
709
710 if (RISCV::GPRRegClass.contains(Reg))
711 CalleeSavedSize += getSpillSize(RISCV::GPRRegClass);
712 else if (RISCV::FPR64RegClass.contains(Reg))
713 CalleeSavedSize += getSpillSize(RISCV::FPR64RegClass);
714 else if (RISCV::FPR32RegClass.contains(Reg))
715 CalleeSavedSize += getSpillSize(RISCV::FPR32RegClass);
716 // Ignore vector registers.
717 }
718
719 int64_t MaxFPOffset = Offset - CalleeSavedSize;
720 return !isFrameOffsetLegal(MI, RISCV::X8, MaxFPOffset);
721 }
722
723 // Assume 128 bytes spill slots size to estimate the maximum possible
724 // offset relative to the stack pointer.
725 // FIXME: The 128 is copied from ARM. We should run some statistics and pick a
726 // real one for RISC-V.
727 int64_t MaxSPOffset = Offset + 128;
728 MaxSPOffset += MFI.getLocalFrameSize();
729 return !isFrameOffsetLegal(MI, RISCV::X2, MaxSPOffset);
730 }
731
732 // Determine whether a given base register plus offset immediate is
733 // encodable to resolve a frame index.
isFrameOffsetLegal(const MachineInstr * MI,Register BaseReg,int64_t Offset) const734 bool RISCVRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
735 Register BaseReg,
736 int64_t Offset) const {
737 unsigned FIOperandNum = 0;
738 while (!MI->getOperand(FIOperandNum).isFI()) {
739 FIOperandNum++;
740 assert(FIOperandNum < MI->getNumOperands() &&
741 "Instr does not have a FrameIndex operand!");
742 }
743
744 Offset += getFrameIndexInstrOffset(MI, FIOperandNum);
745 return isInt<12>(Offset);
746 }
747
748 // Insert defining instruction(s) for a pointer to FrameIdx before
749 // insertion point I.
750 // Return materialized frame pointer.
materializeFrameBaseRegister(MachineBasicBlock * MBB,int FrameIdx,int64_t Offset) const751 Register RISCVRegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB,
752 int FrameIdx,
753 int64_t Offset) const {
754 MachineBasicBlock::iterator MBBI = MBB->begin();
755 DebugLoc DL;
756 if (MBBI != MBB->end())
757 DL = MBBI->getDebugLoc();
758 MachineFunction *MF = MBB->getParent();
759 MachineRegisterInfo &MFI = MF->getRegInfo();
760 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
761
762 Register BaseReg = MFI.createVirtualRegister(&RISCV::GPRRegClass);
763 BuildMI(*MBB, MBBI, DL, TII->get(RISCV::ADDI), BaseReg)
764 .addFrameIndex(FrameIdx)
765 .addImm(Offset);
766 return BaseReg;
767 }
768
769 // Resolve a frame index operand of an instruction to reference the
770 // indicated base register plus offset instead.
resolveFrameIndex(MachineInstr & MI,Register BaseReg,int64_t Offset) const771 void RISCVRegisterInfo::resolveFrameIndex(MachineInstr &MI, Register BaseReg,
772 int64_t Offset) const {
773 unsigned FIOperandNum = 0;
774 while (!MI.getOperand(FIOperandNum).isFI()) {
775 FIOperandNum++;
776 assert(FIOperandNum < MI.getNumOperands() &&
777 "Instr does not have a FrameIndex operand!");
778 }
779
780 Offset += getFrameIndexInstrOffset(&MI, FIOperandNum);
781 // FrameIndex Operands are always represented as a
782 // register followed by an immediate.
783 MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false);
784 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
785 }
786
787 // Get the offset from the referenced frame index in the instruction,
788 // if there is one.
getFrameIndexInstrOffset(const MachineInstr * MI,int Idx) const789 int64_t RISCVRegisterInfo::getFrameIndexInstrOffset(const MachineInstr *MI,
790 int Idx) const {
791 assert((RISCVII::getFormat(MI->getDesc().TSFlags) == RISCVII::InstFormatI ||
792 RISCVII::getFormat(MI->getDesc().TSFlags) == RISCVII::InstFormatS) &&
793 "The MI must be I or S format.");
794 assert(MI->getOperand(Idx).isFI() && "The Idx'th operand of MI is not a "
795 "FrameIndex operand");
796 return MI->getOperand(Idx + 1).getImm();
797 }
798
getFrameRegister(const MachineFunction & MF) const799 Register RISCVRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
800 const TargetFrameLowering *TFI = getFrameLowering(MF);
801 return TFI->hasFP(MF) ? RISCV::X8 : RISCV::X2;
802 }
803
getRegAsmName(MCRegister Reg) const804 StringRef RISCVRegisterInfo::getRegAsmName(MCRegister Reg) const {
805 if (Reg == RISCV::SF_VCIX_STATE)
806 return "sf.vcix_state";
807 return TargetRegisterInfo::getRegAsmName(Reg);
808 }
809
810 const uint32_t *
getCallPreservedMask(const MachineFunction & MF,CallingConv::ID CC) const811 RISCVRegisterInfo::getCallPreservedMask(const MachineFunction & MF,
812 CallingConv::ID CC) const {
813 auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
814
815 if (CC == CallingConv::GHC)
816 return CSR_NoRegs_RegMask;
817 RISCVABI::ABI ABI = Subtarget.getTargetABI();
818 if (CC == CallingConv::PreserveMost) {
819 if (ABI == RISCVABI::ABI_ILP32E || ABI == RISCVABI::ABI_LP64E)
820 return CSR_RT_MostRegs_RVE_RegMask;
821 return CSR_RT_MostRegs_RegMask;
822 }
823 switch (ABI) {
824 default:
825 llvm_unreachable("Unrecognized ABI");
826 case RISCVABI::ABI_ILP32E:
827 case RISCVABI::ABI_LP64E:
828 return CSR_ILP32E_LP64E_RegMask;
829 case RISCVABI::ABI_ILP32:
830 case RISCVABI::ABI_LP64:
831 if (CC == CallingConv::RISCV_VectorCall)
832 return CSR_ILP32_LP64_V_RegMask;
833 return CSR_ILP32_LP64_RegMask;
834 case RISCVABI::ABI_ILP32F:
835 case RISCVABI::ABI_LP64F:
836 if (CC == CallingConv::RISCV_VectorCall)
837 return CSR_ILP32F_LP64F_V_RegMask;
838 return CSR_ILP32F_LP64F_RegMask;
839 case RISCVABI::ABI_ILP32D:
840 case RISCVABI::ABI_LP64D:
841 if (CC == CallingConv::RISCV_VectorCall)
842 return CSR_ILP32D_LP64D_V_RegMask;
843 return CSR_ILP32D_LP64D_RegMask;
844 }
845 }
846
847 const TargetRegisterClass *
getLargestLegalSuperClass(const TargetRegisterClass * RC,const MachineFunction &) const848 RISCVRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC,
849 const MachineFunction &) const {
850 if (RC == &RISCV::VMV0RegClass)
851 return &RISCV::VRRegClass;
852 if (RC == &RISCV::VRNoV0RegClass)
853 return &RISCV::VRRegClass;
854 if (RC == &RISCV::VRM2NoV0RegClass)
855 return &RISCV::VRM2RegClass;
856 if (RC == &RISCV::VRM4NoV0RegClass)
857 return &RISCV::VRM4RegClass;
858 if (RC == &RISCV::VRM8NoV0RegClass)
859 return &RISCV::VRM8RegClass;
860 return RC;
861 }
862
getOffsetOpcodes(const StackOffset & Offset,SmallVectorImpl<uint64_t> & Ops) const863 void RISCVRegisterInfo::getOffsetOpcodes(const StackOffset &Offset,
864 SmallVectorImpl<uint64_t> &Ops) const {
865 // VLENB is the length of a vector register in bytes. We use <vscale x 8 x i8>
866 // to represent one vector register. The dwarf offset is
867 // VLENB * scalable_offset / 8.
868 assert(Offset.getScalable() % 8 == 0 && "Invalid frame offset");
869
870 // Add fixed-sized offset using existing DIExpression interface.
871 DIExpression::appendOffset(Ops, Offset.getFixed());
872
873 unsigned VLENB = getDwarfRegNum(RISCV::VLENB, true);
874 int64_t VLENBSized = Offset.getScalable() / 8;
875 if (VLENBSized > 0) {
876 Ops.push_back(dwarf::DW_OP_constu);
877 Ops.push_back(VLENBSized);
878 Ops.append({dwarf::DW_OP_bregx, VLENB, 0ULL});
879 Ops.push_back(dwarf::DW_OP_mul);
880 Ops.push_back(dwarf::DW_OP_plus);
881 } else if (VLENBSized < 0) {
882 Ops.push_back(dwarf::DW_OP_constu);
883 Ops.push_back(-VLENBSized);
884 Ops.append({dwarf::DW_OP_bregx, VLENB, 0ULL});
885 Ops.push_back(dwarf::DW_OP_mul);
886 Ops.push_back(dwarf::DW_OP_minus);
887 }
888 }
889
890 unsigned
getRegisterCostTableIndex(const MachineFunction & MF) const891 RISCVRegisterInfo::getRegisterCostTableIndex(const MachineFunction &MF) const {
892 return MF.getSubtarget<RISCVSubtarget>().hasStdExtZca() && !DisableCostPerUse
893 ? 1
894 : 0;
895 }
896
getSpillWeightScaleFactor(const TargetRegisterClass * RC) const897 float RISCVRegisterInfo::getSpillWeightScaleFactor(
898 const TargetRegisterClass *RC) const {
899 return getRegClassWeight(RC).RegWeight;
900 }
901
902 // Add two address hints to improve chances of being able to use a compressed
903 // instruction.
getRegAllocationHints(Register VirtReg,ArrayRef<MCPhysReg> Order,SmallVectorImpl<MCPhysReg> & Hints,const MachineFunction & MF,const VirtRegMap * VRM,const LiveRegMatrix * Matrix) const904 bool RISCVRegisterInfo::getRegAllocationHints(
905 Register VirtReg, ArrayRef<MCPhysReg> Order,
906 SmallVectorImpl<MCPhysReg> &Hints, const MachineFunction &MF,
907 const VirtRegMap *VRM, const LiveRegMatrix *Matrix) const {
908 const MachineRegisterInfo *MRI = &MF.getRegInfo();
909 auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
910
911 bool BaseImplRetVal = TargetRegisterInfo::getRegAllocationHints(
912 VirtReg, Order, Hints, MF, VRM, Matrix);
913
914 if (!VRM || DisableRegAllocHints)
915 return BaseImplRetVal;
916
917 // Add any two address hints after any copy hints.
918 SmallSet<Register, 4> TwoAddrHints;
919
920 auto tryAddHint = [&](const MachineOperand &VRRegMO, const MachineOperand &MO,
921 bool NeedGPRC) -> void {
922 Register Reg = MO.getReg();
923 Register PhysReg = Reg.isPhysical() ? Reg : Register(VRM->getPhys(Reg));
924 // TODO: Support GPRPair subregisters? Need to be careful with even/odd
925 // registers. If the virtual register is an odd register of a pair and the
926 // physical register is even (or vice versa), we should not add the hint.
927 if (PhysReg && (!NeedGPRC || RISCV::GPRCRegClass.contains(PhysReg)) &&
928 !MO.getSubReg() && !VRRegMO.getSubReg()) {
929 if (!MRI->isReserved(PhysReg) && !is_contained(Hints, PhysReg))
930 TwoAddrHints.insert(PhysReg);
931 }
932 };
933
934 // This is all of the compressible binary instructions. If an instruction
935 // needs GPRC register class operands \p NeedGPRC will be set to true.
936 auto isCompressible = [&Subtarget](const MachineInstr &MI, bool &NeedGPRC) {
937 NeedGPRC = false;
938 switch (MI.getOpcode()) {
939 default:
940 return false;
941 case RISCV::AND:
942 case RISCV::OR:
943 case RISCV::XOR:
944 case RISCV::SUB:
945 case RISCV::ADDW:
946 case RISCV::SUBW:
947 NeedGPRC = true;
948 return true;
949 case RISCV::ANDI: {
950 NeedGPRC = true;
951 if (!MI.getOperand(2).isImm())
952 return false;
953 int64_t Imm = MI.getOperand(2).getImm();
954 if (isInt<6>(Imm))
955 return true;
956 // c.zext.b
957 return Subtarget.hasStdExtZcb() && Imm == 255;
958 }
959 case RISCV::SRAI:
960 case RISCV::SRLI:
961 NeedGPRC = true;
962 return true;
963 case RISCV::ADD:
964 case RISCV::SLLI:
965 return true;
966 case RISCV::ADDI:
967 case RISCV::ADDIW:
968 return MI.getOperand(2).isImm() && isInt<6>(MI.getOperand(2).getImm());
969 case RISCV::MUL:
970 case RISCV::SEXT_B:
971 case RISCV::SEXT_H:
972 case RISCV::ZEXT_H_RV32:
973 case RISCV::ZEXT_H_RV64:
974 // c.mul, c.sext.b, c.sext.h, c.zext.h
975 NeedGPRC = true;
976 return Subtarget.hasStdExtZcb();
977 case RISCV::ADD_UW:
978 // c.zext.w
979 NeedGPRC = true;
980 return Subtarget.hasStdExtZcb() && MI.getOperand(2).isReg() &&
981 MI.getOperand(2).getReg() == RISCV::X0;
982 case RISCV::XORI:
983 // c.not
984 NeedGPRC = true;
985 return Subtarget.hasStdExtZcb() && MI.getOperand(2).isImm() &&
986 MI.getOperand(2).getImm() == -1;
987 }
988 };
989
990 // Returns true if this operand is compressible. For non-registers it always
991 // returns true. Immediate range was already checked in isCompressible.
992 // For registers, it checks if the register is a GPRC register. reg-reg
993 // instructions that require GPRC need all register operands to be GPRC.
994 auto isCompressibleOpnd = [&](const MachineOperand &MO) {
995 if (!MO.isReg())
996 return true;
997 Register Reg = MO.getReg();
998 Register PhysReg = Reg.isPhysical() ? Reg : Register(VRM->getPhys(Reg));
999 return PhysReg && RISCV::GPRCRegClass.contains(PhysReg);
1000 };
1001
1002 for (auto &MO : MRI->reg_nodbg_operands(VirtReg)) {
1003 const MachineInstr &MI = *MO.getParent();
1004 unsigned OpIdx = MO.getOperandNo();
1005 bool NeedGPRC;
1006 if (isCompressible(MI, NeedGPRC)) {
1007 if (OpIdx == 0 && MI.getOperand(1).isReg()) {
1008 if (!NeedGPRC || MI.getNumExplicitOperands() < 3 ||
1009 MI.getOpcode() == RISCV::ADD_UW ||
1010 isCompressibleOpnd(MI.getOperand(2)))
1011 tryAddHint(MO, MI.getOperand(1), NeedGPRC);
1012 if (MI.isCommutable() && MI.getOperand(2).isReg() &&
1013 (!NeedGPRC || isCompressibleOpnd(MI.getOperand(1))))
1014 tryAddHint(MO, MI.getOperand(2), NeedGPRC);
1015 } else if (OpIdx == 1 && (!NeedGPRC || MI.getNumExplicitOperands() < 3 ||
1016 isCompressibleOpnd(MI.getOperand(2)))) {
1017 tryAddHint(MO, MI.getOperand(0), NeedGPRC);
1018 } else if (MI.isCommutable() && OpIdx == 2 &&
1019 (!NeedGPRC || isCompressibleOpnd(MI.getOperand(1)))) {
1020 tryAddHint(MO, MI.getOperand(0), NeedGPRC);
1021 }
1022 }
1023
1024 // Add a hint if it would allow auipc/lui+addi(w) fusion.
1025 if ((MI.getOpcode() == RISCV::ADDIW || MI.getOpcode() == RISCV::ADDI) &&
1026 MI.getOperand(1).isReg()) {
1027 const MachineBasicBlock &MBB = *MI.getParent();
1028 MachineBasicBlock::const_iterator I = MI.getIterator();
1029 // Is the previous instruction a LUI or AUIPC that can be fused?
1030 if (I != MBB.begin()) {
1031 I = skipDebugInstructionsBackward(std::prev(I), MBB.begin());
1032 if (((I->getOpcode() == RISCV::LUI && Subtarget.hasLUIADDIFusion()) ||
1033 (I->getOpcode() == RISCV::AUIPC &&
1034 Subtarget.hasAUIPCADDIFusion())) &&
1035 I->getOperand(0).getReg() == MI.getOperand(1).getReg()) {
1036 if (OpIdx == 0)
1037 tryAddHint(MO, MI.getOperand(1), /*NeedGPRC=*/false);
1038 else
1039 tryAddHint(MO, MI.getOperand(0), /*NeedGPRC=*/false);
1040 }
1041 }
1042 }
1043 }
1044
1045 for (MCPhysReg OrderReg : Order)
1046 if (TwoAddrHints.count(OrderReg))
1047 Hints.push_back(OrderReg);
1048
1049 return BaseImplRetVal;
1050 }
1051