1 //===-- RISCVRegisterInfo.cpp - RISCV 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 RISCV implementation of the TargetRegisterInfo class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "RISCVRegisterInfo.h"
14 #include "RISCV.h"
15 #include "RISCVMachineFunctionInfo.h"
16 #include "RISCVSubtarget.h"
17 #include "llvm/CodeGen/MachineFrameInfo.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/RegisterScavenging.h"
21 #include "llvm/CodeGen/TargetFrameLowering.h"
22 #include "llvm/CodeGen/TargetInstrInfo.h"
23 #include "llvm/Support/ErrorHandling.h"
24 
25 #define GET_REGINFO_TARGET_DESC
26 #include "RISCVGenRegisterInfo.inc"
27 
28 using namespace llvm;
29 
30 static_assert(RISCV::X1 == RISCV::X0 + 1, "Register list not consecutive");
31 static_assert(RISCV::X31 == RISCV::X0 + 31, "Register list not consecutive");
32 static_assert(RISCV::F1_H == RISCV::F0_H + 1, "Register list not consecutive");
33 static_assert(RISCV::F31_H == RISCV::F0_H + 31,
34               "Register list not consecutive");
35 static_assert(RISCV::F1_F == RISCV::F0_F + 1, "Register list not consecutive");
36 static_assert(RISCV::F31_F == RISCV::F0_F + 31,
37               "Register list not consecutive");
38 static_assert(RISCV::F1_D == RISCV::F0_D + 1, "Register list not consecutive");
39 static_assert(RISCV::F31_D == RISCV::F0_D + 31,
40               "Register list not consecutive");
41 static_assert(RISCV::V1 == RISCV::V0 + 1, "Register list not consecutive");
42 static_assert(RISCV::V31 == RISCV::V0 + 31, "Register list not consecutive");
43 
44 RISCVRegisterInfo::RISCVRegisterInfo(unsigned HwMode)
45     : RISCVGenRegisterInfo(RISCV::X1, /*DwarfFlavour*/0, /*EHFlavor*/0,
46                            /*PC*/0, HwMode) {}
47 
48 const MCPhysReg *
49 RISCVRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
50   auto &Subtarget = MF->getSubtarget<RISCVSubtarget>();
51   if (MF->getFunction().getCallingConv() == CallingConv::GHC)
52     return CSR_NoRegs_SaveList;
53   if (MF->getFunction().hasFnAttribute("interrupt")) {
54     if (Subtarget.hasStdExtD())
55       return CSR_XLEN_F64_Interrupt_SaveList;
56     if (Subtarget.hasStdExtF())
57       return CSR_XLEN_F32_Interrupt_SaveList;
58     return CSR_Interrupt_SaveList;
59   }
60 
61   switch (Subtarget.getTargetABI()) {
62   default:
63     llvm_unreachable("Unrecognized ABI");
64   case RISCVABI::ABI_ILP32:
65   case RISCVABI::ABI_LP64:
66     return CSR_ILP32_LP64_SaveList;
67   case RISCVABI::ABI_ILP32F:
68   case RISCVABI::ABI_LP64F:
69     return CSR_ILP32F_LP64F_SaveList;
70   case RISCVABI::ABI_ILP32D:
71   case RISCVABI::ABI_LP64D:
72     return CSR_ILP32D_LP64D_SaveList;
73   }
74 }
75 
76 BitVector RISCVRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
77   const RISCVFrameLowering *TFI = getFrameLowering(MF);
78   BitVector Reserved(getNumRegs());
79 
80   // Mark any registers requested to be reserved as such
81   for (size_t Reg = 0; Reg < getNumRegs(); Reg++) {
82     if (MF.getSubtarget<RISCVSubtarget>().isRegisterReservedByUser(Reg))
83       markSuperRegs(Reserved, Reg);
84   }
85 
86   // Use markSuperRegs to ensure any register aliases are also reserved
87   markSuperRegs(Reserved, RISCV::X0); // zero
88   markSuperRegs(Reserved, RISCV::X2); // sp
89   markSuperRegs(Reserved, RISCV::X3); // gp
90   markSuperRegs(Reserved, RISCV::X4); // tp
91   if (TFI->hasFP(MF))
92     markSuperRegs(Reserved, RISCV::X8); // fp
93   // Reserve the base register if we need to realign the stack and allocate
94   // variable-sized objects at runtime.
95   if (TFI->hasBP(MF))
96     markSuperRegs(Reserved, RISCVABI::getBPReg()); // bp
97 
98   // V registers for code generation. We handle them manually.
99   markSuperRegs(Reserved, RISCV::VL);
100   markSuperRegs(Reserved, RISCV::VTYPE);
101   markSuperRegs(Reserved, RISCV::VXSAT);
102   markSuperRegs(Reserved, RISCV::VXRM);
103 
104   assert(checkAllSuperRegsMarked(Reserved));
105   return Reserved;
106 }
107 
108 bool RISCVRegisterInfo::isAsmClobberable(const MachineFunction &MF,
109                                          MCRegister PhysReg) const {
110   return !MF.getSubtarget<RISCVSubtarget>().isRegisterReservedByUser(PhysReg);
111 }
112 
113 bool RISCVRegisterInfo::isConstantPhysReg(MCRegister PhysReg) const {
114   return PhysReg == RISCV::X0;
115 }
116 
117 const uint32_t *RISCVRegisterInfo::getNoPreservedMask() const {
118   return CSR_NoRegs_RegMask;
119 }
120 
121 // Frame indexes representing locations of CSRs which are given a fixed location
122 // by save/restore libcalls.
123 static const std::map<unsigned, int> FixedCSRFIMap = {
124   {/*ra*/  RISCV::X1,   -1},
125   {/*s0*/  RISCV::X8,   -2},
126   {/*s1*/  RISCV::X9,   -3},
127   {/*s2*/  RISCV::X18,  -4},
128   {/*s3*/  RISCV::X19,  -5},
129   {/*s4*/  RISCV::X20,  -6},
130   {/*s5*/  RISCV::X21,  -7},
131   {/*s6*/  RISCV::X22,  -8},
132   {/*s7*/  RISCV::X23,  -9},
133   {/*s8*/  RISCV::X24,  -10},
134   {/*s9*/  RISCV::X25,  -11},
135   {/*s10*/ RISCV::X26,  -12},
136   {/*s11*/ RISCV::X27,  -13}
137 };
138 
139 bool RISCVRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF,
140                                              Register Reg,
141                                              int &FrameIdx) const {
142   const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
143   if (!RVFI->useSaveRestoreLibCalls(MF))
144     return false;
145 
146   auto FII = FixedCSRFIMap.find(Reg);
147   if (FII == FixedCSRFIMap.end())
148     return false;
149 
150   FrameIdx = FII->second;
151   return true;
152 }
153 
154 static bool isRVVWholeLoadStore(unsigned Opcode) {
155   switch (Opcode) {
156   default:
157     return false;
158   case RISCV::VS1R_V:
159   case RISCV::VS2R_V:
160   case RISCV::VS4R_V:
161   case RISCV::VS8R_V:
162   case RISCV::VL1RE8_V:
163   case RISCV::VL2RE8_V:
164   case RISCV::VL4RE8_V:
165   case RISCV::VL8RE8_V:
166   case RISCV::VL1RE16_V:
167   case RISCV::VL2RE16_V:
168   case RISCV::VL4RE16_V:
169   case RISCV::VL8RE16_V:
170   case RISCV::VL1RE32_V:
171   case RISCV::VL2RE32_V:
172   case RISCV::VL4RE32_V:
173   case RISCV::VL8RE32_V:
174   case RISCV::VL1RE64_V:
175   case RISCV::VL2RE64_V:
176   case RISCV::VL4RE64_V:
177   case RISCV::VL8RE64_V:
178     return true;
179   }
180 }
181 
182 void RISCVRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
183                                             int SPAdj, unsigned FIOperandNum,
184                                             RegScavenger *RS) const {
185   assert(SPAdj == 0 && "Unexpected non-zero SPAdj value");
186 
187   MachineInstr &MI = *II;
188   MachineFunction &MF = *MI.getParent()->getParent();
189   MachineRegisterInfo &MRI = MF.getRegInfo();
190   const RISCVInstrInfo *TII = MF.getSubtarget<RISCVSubtarget>().getInstrInfo();
191   DebugLoc DL = MI.getDebugLoc();
192 
193   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
194   Register FrameReg;
195   StackOffset Offset =
196       getFrameLowering(MF)->getFrameIndexReference(MF, FrameIndex, FrameReg);
197   bool isRVV = RISCVVPseudosTable::getPseudoInfo(MI.getOpcode()) ||
198                isRVVWholeLoadStore(MI.getOpcode());
199   if (!isRVV)
200     Offset += StackOffset::getFixed(MI.getOperand(FIOperandNum + 1).getImm());
201 
202   if (!isInt<32>(Offset.getFixed())) {
203     report_fatal_error(
204         "Frame offsets outside of the signed 32-bit range not supported");
205   }
206 
207   MachineBasicBlock &MBB = *MI.getParent();
208   bool FrameRegIsKill = false;
209 
210   if (!isInt<12>(Offset.getFixed())) {
211     // The offset won't fit in an immediate, so use a scratch register instead
212     // Modify Offset and FrameReg appropriately
213     Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
214     TII->movImm(MBB, II, DL, ScratchReg, Offset.getFixed());
215     BuildMI(MBB, II, DL, TII->get(RISCV::ADD), ScratchReg)
216         .addReg(FrameReg)
217         .addReg(ScratchReg, RegState::Kill);
218     Offset = StackOffset::get(0, Offset.getScalable());
219     FrameReg = ScratchReg;
220     FrameRegIsKill = true;
221   }
222 
223   if (!Offset.getScalable()) {
224     // Offset = (fixed offset, 0)
225     MI.getOperand(FIOperandNum)
226         .ChangeToRegister(FrameReg, false, false, FrameRegIsKill);
227     if (!isRVV)
228       MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset.getFixed());
229     else {
230       if (Offset.getFixed()) {
231         Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
232         BuildMI(MBB, II, DL, TII->get(RISCV::ADDI), ScratchReg)
233           .addReg(FrameReg, getKillRegState(FrameRegIsKill))
234           .addImm(Offset.getFixed());
235         MI.getOperand(FIOperandNum)
236           .ChangeToRegister(ScratchReg, false, false, true);
237       }
238     }
239   } else {
240     // Offset = (fixed offset, scalable offset)
241     unsigned Opc = RISCV::ADD;
242     int64_t ScalableValue = Offset.getScalable();
243     if (ScalableValue < 0) {
244       ScalableValue = -ScalableValue;
245       Opc = RISCV::SUB;
246     }
247 
248     // 1. Get vlenb && multiply vlen with number of vector register.
249     Register FactorRegister =
250         TII->getVLENFactoredAmount(MF, MBB, II, ScalableValue);
251 
252     // 2. Calculate address: FrameReg + result of multiply
253     Register VL = MRI.createVirtualRegister(&RISCV::GPRRegClass);
254     BuildMI(MBB, II, DL, TII->get(Opc), VL)
255         .addReg(FrameReg, getKillRegState(FrameRegIsKill))
256         .addReg(FactorRegister, RegState::Kill);
257 
258     if (isRVV && Offset.getFixed()) {
259       // Scalable load/store has no immediate argument. We need to add the
260       // fixed part into the load/store base address.
261       BuildMI(MBB, II, DL, TII->get(RISCV::ADDI), VL)
262           .addReg(VL)
263           .addImm(Offset.getFixed());
264     }
265 
266     // 3. Replace address register with calculated address register
267     MI.getOperand(FIOperandNum).ChangeToRegister(VL, false, false, true);
268     if (!isRVV)
269       MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset.getFixed());
270   }
271 }
272 
273 Register RISCVRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
274   const TargetFrameLowering *TFI = getFrameLowering(MF);
275   return TFI->hasFP(MF) ? RISCV::X8 : RISCV::X2;
276 }
277 
278 const uint32_t *
279 RISCVRegisterInfo::getCallPreservedMask(const MachineFunction & MF,
280                                         CallingConv::ID CC) const {
281   auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
282 
283   if (CC == CallingConv::GHC)
284     return CSR_NoRegs_RegMask;
285   switch (Subtarget.getTargetABI()) {
286   default:
287     llvm_unreachable("Unrecognized ABI");
288   case RISCVABI::ABI_ILP32:
289   case RISCVABI::ABI_LP64:
290     return CSR_ILP32_LP64_RegMask;
291   case RISCVABI::ABI_ILP32F:
292   case RISCVABI::ABI_LP64F:
293     return CSR_ILP32F_LP64F_RegMask;
294   case RISCVABI::ABI_ILP32D:
295   case RISCVABI::ABI_LP64D:
296     return CSR_ILP32D_LP64D_RegMask;
297   }
298 }
299 
300 const TargetRegisterClass *
301 RISCVRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC,
302                                              const MachineFunction &) const {
303   if (RC == &RISCV::VMV0RegClass)
304     return &RISCV::VRRegClass;
305   return RC;
306 }
307