1//===- MipsRegisterInfo.cpp - MIPS Register Information -------------------===//
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 MIPS implementation of the TargetRegisterInfo class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "MipsRegisterInfo.h"
14#include "MCTargetDesc/MipsABIInfo.h"
15#include "Mips.h"
16#include "MipsMachineFunction.h"
17#include "MipsSubtarget.h"
18#include "MipsTargetMachine.h"
19#include "llvm/ADT/BitVector.h"
20#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstr.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
24#include "llvm/CodeGen/TargetFrameLowering.h"
25#include "llvm/CodeGen/TargetRegisterInfo.h"
26#include "llvm/CodeGen/TargetSubtargetInfo.h"
27#include "llvm/IR/Function.h"
28#include "llvm/IR/Module.h"
29#include "llvm/Support/Debug.h"
30#include "llvm/Support/ErrorHandling.h"
31#include "llvm/Support/raw_ostream.h"
32#include <cstdint>
33
34using namespace llvm;
35
36#define DEBUG_TYPE "mips-reg-info"
37
38#define GET_REGINFO_TARGET_DESC
39#include "MipsGenRegisterInfo.inc"
40
41MipsRegisterInfo::MipsRegisterInfo(const MipsSubtarget &STI)
42 : MipsGenRegisterInfo(Mips::RA), ArePtrs64bit(STI.getABI().ArePtrs64bit()) {
43 MIPS_MC::initLLVMToCVRegMapping(MRI: this);
44}
45
46unsigned MipsRegisterInfo::getPICCallReg() { return Mips::R25; }
47
48unsigned
49MipsRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
50 MachineFunction &MF) const {
51 switch (RC->getID()) {
52 default:
53 return 0;
54 case Mips::GPR32RegClassID:
55 case Mips::GPR64RegClassID:
56 case Mips::DSPRRegClassID: {
57 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
58 return 28 - TFI->hasFP(MF);
59 }
60 case Mips::FGR32RegClassID:
61 return 32;
62 case Mips::AFGR64RegClassID:
63 return 16;
64 case Mips::FGR64RegClassID:
65 return 32;
66 }
67}
68
69//===----------------------------------------------------------------------===//
70// Callee Saved Registers methods
71//===----------------------------------------------------------------------===//
72
73/// Check if the user has declared $gp/$28 as a global regiater.
74bool isGPUsedAsGlobalRegister(const MachineFunction &MF) {
75 const Module *Module = MF.getFunction().getParent();
76
77 return Module->getNamedMetadata(Name: "llvm.named.register.$28");
78}
79
80/// Mips Callee Saved Registers
81const MCPhysReg *
82MipsRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
83 const MipsSubtarget &Subtarget = MF->getSubtarget<MipsSubtarget>();
84 const Function &F = MF->getFunction();
85 if (F.hasFnAttribute(Kind: "interrupt")) {
86 if (Subtarget.hasMips64())
87 return Subtarget.hasMips64r6() ? CSR_Interrupt_64R6_SaveList
88 : CSR_Interrupt_64_SaveList;
89 else
90 return Subtarget.hasMips32r6() ? CSR_Interrupt_32R6_SaveList
91 : CSR_Interrupt_32_SaveList;
92 }
93
94 bool GPIsGlobal = isGPUsedAsGlobalRegister(MF: *MF);
95 // N64 ABI
96 if (Subtarget.isABI_N64()) {
97 if (Subtarget.isSingleFloat())
98 return GPIsGlobal ? CSR_N64_SingleFloat_NoGP_SaveList
99 : CSR_N64_SingleFloat_SaveList;
100
101 return GPIsGlobal ? CSR_N64_NoGP_SaveList : CSR_N64_SaveList;
102 }
103
104 // N32 ABI
105 if (Subtarget.isABI_N32()) {
106 if (Subtarget.isSingleFloat())
107 return GPIsGlobal ? CSR_N32_SingleFloat_NoGP_SaveList
108 : CSR_N32_SingleFloat_SaveList;
109
110 return GPIsGlobal ? CSR_N32_NoGP_SaveList : CSR_N32_SaveList;
111 }
112
113 // P32 ABI
114 if (Subtarget.isABI_P32())
115 return CSR_P32_SaveList;
116
117 // O32 ABI
118 if (Subtarget.isSingleFloat())
119 return CSR_O32_SingleFloat_SaveList;
120
121 if (Subtarget.isFP64bit())
122 return CSR_O32_FP64_SaveList;
123
124 if (Subtarget.isFPXX())
125 return CSR_O32_FPXX_SaveList;
126
127 return CSR_O32_SaveList;
128}
129
130const uint32_t *
131MipsRegisterInfo::getCallPreservedMask(const MachineFunction &MF,
132 CallingConv::ID) const {
133 const MipsSubtarget &Subtarget = MF.getSubtarget<MipsSubtarget>();
134 // N64 ABI
135 if (Subtarget.isABI_N64()) {
136 if (Subtarget.isSingleFloat())
137 return CSR_N64_SingleFloat_RegMask;
138
139 return CSR_N64_RegMask;
140 }
141
142 // N32 ABI
143 if (Subtarget.isABI_N32()) {
144 if (Subtarget.isSingleFloat())
145 return CSR_N32_SingleFloat_RegMask;
146
147 return CSR_N32_RegMask;
148 }
149
150 // P32 ABI
151 if (Subtarget.isABI_P32())
152 return CSR_P32_RegMask;
153
154 // O32 ABI
155 if (Subtarget.isSingleFloat())
156 return CSR_O32_SingleFloat_RegMask;
157
158 if (Subtarget.isFP64bit())
159 return CSR_O32_FP64_RegMask;
160
161 if (Subtarget.isFPXX())
162 return CSR_O32_FPXX_RegMask;
163
164 return CSR_O32_RegMask;
165}
166
167const uint32_t *MipsRegisterInfo::getMips16RetHelperMask() {
168 return CSR_Mips16RetHelper_RegMask;
169}
170
171BitVector MipsRegisterInfo::
172getReservedRegs(const MachineFunction &MF) const {
173 static const MCPhysReg ReservedGPR32[] = {Mips::R0, Mips::R26, Mips::R27,
174 Mips::SP};
175
176 static const MCPhysReg ReservedGPR64[] = {Mips::R0_64, Mips::R26_64,
177 Mips::R27_64, Mips::SP_64};
178
179 BitVector Reserved(getNumRegs());
180 const MipsSubtarget &Subtarget = MF.getSubtarget<MipsSubtarget>();
181
182 for (MCPhysReg R : ReservedGPR32)
183 Reserved.set(R);
184
185 for (MCPhysReg R : ReservedGPR64)
186 Reserved.set(R);
187
188 // Reserve AT if ABI is P32.
189 if (Subtarget.isABI_P32())
190 Reserved.set(Mips::R1);
191
192 // Static JIT code uses t9 in RuntimeDyld stubs for R_MIPS_26 relocations,
193 // including jumps within a function. Keep it out of register allocation.
194 const auto &TM = static_cast<const MipsTargetMachine &>(MF.getTarget());
195 if (TM.isJIT() && TM.getRelocationModel() == Reloc::Static)
196 markSuperRegs(RegisterSet&: Reserved, Reg: Mips::R25);
197
198 // Mark user-reserved GPRs and their 64-bit super-registers.
199 for (unsigned I = 1; I < 32; ++I)
200 if (Subtarget.isGPRReservedByUser(GPR: I))
201 markSuperRegs(RegisterSet&: Reserved, Reg: Mips::GPR32RegClass.getRegister(i: I));
202
203 // For mno-abicalls, GP is a program invariant!
204 bool GPIsGlobal = isGPUsedAsGlobalRegister(MF);
205 if (!Subtarget.isABICalls() || GPIsGlobal) {
206 Reserved.set(Mips::GP);
207 Reserved.set(Mips::GP_64);
208 }
209
210 if (Subtarget.isFP64bit()) {
211 // Reserve all registers in AFGR64.
212 for (MCPhysReg Reg : Mips::AFGR64RegClass)
213 Reserved.set(Reg);
214 } else {
215 // Reserve all registers in FGR64.
216 for (MCPhysReg Reg : Mips::FGR64RegClass)
217 Reserved.set(Reg);
218 }
219 // Reserve FP if this function should have a dedicated frame pointer register.
220 if (Subtarget.getFrameLowering()->hasFP(MF)) {
221 if (Subtarget.inMips16Mode())
222 Reserved.set(Mips::R16);
223 else {
224 Reserved.set(Mips::FP);
225 Reserved.set(Mips::FP_64);
226
227 // Reserve the base register if we need to both realign the stack and
228 // allocate variable-sized objects at runtime. This should test the
229 // same conditions as MipsFrameLowering::hasBP().
230 if (hasStackRealignment(MF) && MF.getFrameInfo().hasVarSizedObjects()) {
231 const MipsABIInfo &ABI = Subtarget.getABI();
232 Reserved.set(ABI.getSavedReg(I: 7, Is64Bit: false));
233 Reserved.set(ABI.getSavedReg(I: 7, Is64Bit: true));
234 }
235 }
236 }
237 // Reserve fp control and status register
238 Reserved.set(Mips::FCR31);
239
240 // Reserve hardware registers.
241 Reserved.set(Mips::HWR29);
242 Reserved.set(Mips::HWR2);
243
244 // Reserve DSP control register.
245 Reserved.set(Mips::DSPPos);
246 Reserved.set(Mips::DSPSCount);
247 Reserved.set(Mips::DSPCarry);
248 Reserved.set(Mips::DSPEFI);
249 Reserved.set(Mips::DSPOutFlag);
250
251 // Reserve MSA control registers.
252 for (MCPhysReg Reg : Mips::MSACtrlRegClass)
253 Reserved.set(Reg);
254
255 // Reserve RA if in mips16 mode.
256 if (Subtarget.inMips16Mode()) {
257 const MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
258 Reserved.set(Mips::RA);
259 Reserved.set(Mips::RA_64);
260 Reserved.set(Mips::R8);
261 Reserved.set(Mips::R9);
262 if (MF.getFunction().hasFnAttribute(Kind: "saveS2") || MipsFI->hasSaveS2())
263 Reserved.set(Mips::R18);
264 }
265
266 // Reserve GP if small section is used.
267 if (Subtarget.useSmallSection()) {
268 Reserved.set(Mips::GP);
269 Reserved.set(Mips::GP_64);
270 }
271
272 return Reserved;
273}
274
275// FrameIndex represent objects inside a abstract stack.
276// We must replace FrameIndex with an stack/frame pointer
277// direct reference.
278bool MipsRegisterInfo::
279eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj,
280 unsigned FIOperandNum, RegScavenger *RS) const {
281 MachineInstr &MI = *II;
282 MachineFunction &MF = *MI.getParent()->getParent();
283
284 LLVM_DEBUG(errs() << "\nFunction : " << MF.getName() << "\n";
285 errs() << "<--------->\n"
286 << MI);
287
288 int FrameIndex = MI.getOperand(i: FIOperandNum).getIndex();
289 uint64_t stackSize = MF.getFrameInfo().getStackSize();
290 int64_t spOffset = MF.getFrameInfo().getObjectOffset(ObjectIdx: FrameIndex);
291
292 LLVM_DEBUG(errs() << "FrameIndex : " << FrameIndex << "\n"
293 << "spOffset : " << spOffset << "\n"
294 << "stackSize : " << stackSize << "\n"
295 << "alignment : "
296 << DebugStr(MF.getFrameInfo().getObjectAlign(FrameIndex))
297 << "\n");
298
299 eliminateFI(II: MI, OpNo: FIOperandNum, FrameIndex, StackSize: stackSize, SPOffset: spOffset);
300 return false;
301}
302
303Register MipsRegisterInfo::
304getFrameRegister(const MachineFunction &MF) const {
305 const MipsSubtarget &Subtarget = MF.getSubtarget<MipsSubtarget>();
306 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
307 bool IsN64 = Subtarget.getABI().IsN64();
308
309 if (Subtarget.inMips16Mode())
310 return TFI->hasFP(MF) ? Mips::R16 : Mips::SP;
311 else
312 return TFI->hasFP(MF) ? (IsN64 ? Mips::FP_64 : Mips::FP) :
313 (IsN64 ? Mips::SP_64 : Mips::SP);
314}
315
316bool MipsRegisterInfo::canRealignStack(const MachineFunction &MF) const {
317 // Avoid realigning functions that explicitly do not want to be realigned.
318 // Normally, we should report an error when a function should be dynamically
319 // realigned but also has the attribute no-realign-stack. Unfortunately,
320 // with this attribute, MachineFrameInfo clamps each new object's alignment
321 // to that of the stack's alignment as specified by the ABI. As a result,
322 // the information of whether we have objects with larger alignment
323 // requirement than the stack's alignment is already lost at this point.
324 if (!TargetRegisterInfo::canRealignStack(MF))
325 return false;
326
327 const MipsSubtarget &Subtarget = MF.getSubtarget<MipsSubtarget>();
328 unsigned FP = Subtarget.isGP32bit() ? Mips::FP : Mips::FP_64;
329 unsigned BP = Subtarget.getABI().getSavedReg(I: 7, Is64Bit: Subtarget.isGP64bit());
330
331 // Support dynamic stack realignment for all targets except Mips16.
332 if (Subtarget.inMips16Mode())
333 return false;
334
335 // We can't perform dynamic stack realignment if we can't reserve the
336 // frame pointer register.
337 if (!MF.getRegInfo().canReserveReg(PhysReg: FP))
338 return false;
339
340 // We can realign the stack if we know the maximum call frame size and we
341 // don't have variable sized objects.
342 if (Subtarget.getFrameLowering()->hasReservedCallFrame(MF))
343 return true;
344
345 // We have to reserve the base pointer register in the presence of variable
346 // sized objects.
347 return MF.getRegInfo().canReserveReg(PhysReg: BP);
348}
349