LLVM 24.0.0git
MipsRegisterInfo.cpp
Go to the documentation of this file.
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"
15#include "Mips.h"
16#include "MipsMachineFunction.h"
17#include "MipsSubtarget.h"
18#include "MipsTargetMachine.h"
19#include "llvm/ADT/BitVector.h"
27#include "llvm/IR/Function.h"
28#include "llvm/IR/Module.h"
29#include "llvm/Support/Debug.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
44
45unsigned MipsRegisterInfo::getPICCallReg() { return Mips::R25; }
46
47unsigned
49 MachineFunction &MF) const {
50 switch (RC->getID()) {
51 default:
52 return 0;
53 case Mips::GPR32RegClassID:
54 case Mips::GPR64RegClassID:
55 case Mips::DSPRRegClassID: {
57 return 28 - TFI->hasFP(MF);
58 }
59 case Mips::FGR32RegClassID:
60 return 32;
61 case Mips::AFGR64RegClassID:
62 return 16;
63 case Mips::FGR64RegClassID:
64 return 32;
65 }
66}
67
68//===----------------------------------------------------------------------===//
69// Callee Saved Registers methods
70//===----------------------------------------------------------------------===//
71
72/// Check if the user has declared $gp/$28 as a global regiater.
74 const Module *Module = MF.getFunction().getParent();
75
76 return Module->getNamedMetadata("llvm.named.register.$28");
77}
78
79/// Mips Callee Saved Registers
80const MCPhysReg *
83 const Function &F = MF->getFunction();
84 if (F.hasFnAttribute("interrupt")) {
85 if (Subtarget.hasMips64())
86 return Subtarget.hasMips64r6() ? CSR_Interrupt_64R6_SaveList
87 : CSR_Interrupt_64_SaveList;
88 else
89 return Subtarget.hasMips32r6() ? CSR_Interrupt_32R6_SaveList
90 : CSR_Interrupt_32_SaveList;
91 }
92
93 bool GPIsGlobal = isGPUsedAsGlobalRegister(*MF);
94 // N64 ABI
95 if (Subtarget.isABI_N64()) {
96 if (Subtarget.isSingleFloat())
97 return GPIsGlobal ? CSR_N64_SingleFloat_NoGP_SaveList
98 : CSR_N64_SingleFloat_SaveList;
99
100 return GPIsGlobal ? CSR_N64_NoGP_SaveList : CSR_N64_SaveList;
101 }
102
103 // N32 ABI
104 if (Subtarget.isABI_N32()) {
105 if (Subtarget.isSingleFloat())
106 return GPIsGlobal ? CSR_N32_SingleFloat_NoGP_SaveList
107 : CSR_N32_SingleFloat_SaveList;
108
109 return GPIsGlobal ? CSR_N32_NoGP_SaveList : CSR_N32_SaveList;
110 }
111
112 // P32 ABI
113 if (Subtarget.isABI_P32())
114 return CSR_P32_SaveList;
115
116 // O32 ABI
117 if (Subtarget.isSingleFloat())
118 return CSR_O32_SingleFloat_SaveList;
119
120 if (Subtarget.isFP64bit())
121 return CSR_O32_FP64_SaveList;
122
123 if (Subtarget.isFPXX())
124 return CSR_O32_FPXX_SaveList;
125
126 return CSR_O32_SaveList;
127}
128
129const uint32_t *
131 CallingConv::ID) const {
133 // N64 ABI
134 if (Subtarget.isABI_N64()) {
135 if (Subtarget.isSingleFloat())
136 return CSR_N64_SingleFloat_RegMask;
137
138 return CSR_N64_RegMask;
139 }
140
141 // N32 ABI
142 if (Subtarget.isABI_N32()) {
143 if (Subtarget.isSingleFloat())
144 return CSR_N32_SingleFloat_RegMask;
145
146 return CSR_N32_RegMask;
147 }
148
149 // P32 ABI
150 if (Subtarget.isABI_P32())
151 return CSR_P32_RegMask;
152
153 // O32 ABI
154 if (Subtarget.isSingleFloat())
155 return CSR_O32_SingleFloat_RegMask;
156
157 if (Subtarget.isFP64bit())
158 return CSR_O32_FP64_RegMask;
159
160 if (Subtarget.isFPXX())
161 return CSR_O32_FPXX_RegMask;
162
163 return CSR_O32_RegMask;
164}
165
167 return CSR_Mips16RetHelper_RegMask;
168}
169
171getReservedRegs(const MachineFunction &MF) const {
172 static const MCPhysReg ReservedGPR32[] = {Mips::R0, Mips::R26, Mips::R27,
173 Mips::SP};
174
175 static const MCPhysReg ReservedGPR64[] = {Mips::R0_64, Mips::R26_64,
176 Mips::R27_64, Mips::SP_64};
177
178 BitVector Reserved(getNumRegs());
180
181 for (MCPhysReg R : ReservedGPR32)
182 Reserved.set(R);
183
184 for (MCPhysReg R : ReservedGPR64)
185 Reserved.set(R);
186
187 // Reserve AT if ABI is P32.
188 if (Subtarget.isABI_P32())
189 Reserved.set(Mips::R1);
190
191 // Static JIT code uses t9 in RuntimeDyld stubs for R_MIPS_26 relocations,
192 // including jumps within a function. Keep it out of register allocation.
193 const auto &TM = static_cast<const MipsTargetMachine &>(MF.getTarget());
194 if (TM.isJIT() && TM.getRelocationModel() == Reloc::Static)
195 markSuperRegs(Reserved, Mips::R25);
196
197 // Mark user-reserved GPRs and their 64-bit super-registers.
198 for (unsigned I = 1; I < 32; ++I)
199 if (Subtarget.isGPRReservedByUser(I))
200 markSuperRegs(Reserved, Mips::GPR32RegClass.getRegister(I));
201
202 // For mno-abicalls, GP is a program invariant!
203 bool GPIsGlobal = isGPUsedAsGlobalRegister(MF);
204 if (!Subtarget.isABICalls() || GPIsGlobal) {
205 Reserved.set(Mips::GP);
207 }
208
209 if (Subtarget.isFP64bit()) {
210 // Reserve all registers in AFGR64.
211 for (MCPhysReg Reg : Mips::AFGR64RegClass)
212 Reserved.set(Reg);
213 } else {
214 // Reserve all registers in FGR64.
215 for (MCPhysReg Reg : Mips::FGR64RegClass)
216 Reserved.set(Reg);
217 }
218 // Reserve FP if this function should have a dedicated frame pointer register.
219 if (Subtarget.getFrameLowering()->hasFP(MF)) {
220 if (Subtarget.inMips16Mode())
221 Reserved.set(Mips::R16);
222 else {
223 Reserved.set(Mips::FP);
225
226 // Reserve the base register if we need to both realign the stack and
227 // allocate variable-sized objects at runtime. This should test the
228 // same conditions as MipsFrameLowering::hasBP().
229 if (hasStackRealignment(MF) && MF.getFrameInfo().hasVarSizedObjects()) {
230 const MipsABIInfo &ABI = Subtarget.getABI();
231 Reserved.set(ABI.getSavedReg(7, false));
232 Reserved.set(ABI.getSavedReg(7, true));
233 }
234 }
235 }
236 // Reserve fp control and status register
237 Reserved.set(Mips::FCR31);
238
239 // Reserve hardware registers.
240 Reserved.set(Mips::HWR29);
241 Reserved.set(Mips::HWR2);
242
243 // Reserve DSP control register.
244 Reserved.set(Mips::DSPPos);
245 Reserved.set(Mips::DSPSCount);
246 Reserved.set(Mips::DSPCarry);
247 Reserved.set(Mips::DSPEFI);
248 Reserved.set(Mips::DSPOutFlag);
249
250 // Reserve MSA control registers.
251 for (MCPhysReg Reg : Mips::MSACtrlRegClass)
252 Reserved.set(Reg);
253
254 // Reserve RA if in mips16 mode.
255 if (Subtarget.inMips16Mode()) {
256 const MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
257 Reserved.set(Mips::RA);
259 Reserved.set(Mips::R8);
260 Reserved.set(Mips::R9);
261 if (MF.getFunction().hasFnAttribute("saveS2") || MipsFI->hasSaveS2())
262 Reserved.set(Mips::R18);
263 }
264
265 // Reserve GP if small section is used.
266 if (Subtarget.useSmallSection()) {
267 Reserved.set(Mips::GP);
269 }
270
271 return Reserved;
272}
273
274// FrameIndex represent objects inside a abstract stack.
275// We must replace FrameIndex with an stack/frame pointer
276// direct reference.
279 unsigned FIOperandNum, RegScavenger *RS) const {
280 MachineInstr &MI = *II;
281 MachineFunction &MF = *MI.getParent()->getParent();
282
283 LLVM_DEBUG(errs() << "\nFunction : " << MF.getName() << "\n";
284 errs() << "<--------->\n"
285 << MI);
286
287 int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
288 uint64_t stackSize = MF.getFrameInfo().getStackSize();
289 int64_t spOffset = MF.getFrameInfo().getObjectOffset(FrameIndex);
290
291 LLVM_DEBUG(errs() << "FrameIndex : " << FrameIndex << "\n"
292 << "spOffset : " << spOffset << "\n"
293 << "stackSize : " << stackSize << "\n"
294 << "alignment : "
295 << DebugStr(MF.getFrameInfo().getObjectAlign(FrameIndex))
296 << "\n");
297
298 eliminateFI(MI, FIOperandNum, FrameIndex, stackSize, spOffset);
299 return false;
300}
301
303getFrameRegister(const MachineFunction &MF) const {
305 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
306 bool IsN64 = Subtarget.getABI().IsN64();
307
308 if (Subtarget.inMips16Mode())
309 return TFI->hasFP(MF) ? Mips::R16 : Mips::SP;
310 else
311 return TFI->hasFP(MF) ? (IsN64 ? Mips::FP_64 : Mips::FP) :
312 (IsN64 ? Mips::SP_64 : Mips::SP);
313}
314
316 // Avoid realigning functions that explicitly do not want to be realigned.
317 // Normally, we should report an error when a function should be dynamically
318 // realigned but also has the attribute no-realign-stack. Unfortunately,
319 // with this attribute, MachineFrameInfo clamps each new object's alignment
320 // to that of the stack's alignment as specified by the ABI. As a result,
321 // the information of whether we have objects with larger alignment
322 // requirement than the stack's alignment is already lost at this point.
324 return false;
325
327 unsigned FP = Subtarget.isGP32bit() ? Mips::FP : Mips::FP_64;
328 unsigned BP = Subtarget.getABI().getSavedReg(7, Subtarget.isGP64bit());
329
330 // Support dynamic stack realignment for all targets except Mips16.
331 if (Subtarget.inMips16Mode())
332 return false;
333
334 // We can't perform dynamic stack realignment if we can't reserve the
335 // frame pointer register.
336 if (!MF.getRegInfo().canReserveReg(FP))
337 return false;
338
339 // We can realign the stack if we know the maximum call frame size and we
340 // don't have variable sized objects.
341 if (Subtarget.getFrameLowering()->hasReservedCallFrame(MF))
342 return true;
343
344 // We have to reserve the base pointer register in the presence of variable
345 // sized objects.
346 return MF.getRegInfo().canReserveReg(BP);
347}
const AArch64Subtarget & Subtarget
This file implements the BitVector class.
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
bool isGPUsedAsGlobalRegister(const MachineFunction &MF)
Check if the user has declared $gp/$28 as a global regiater.
uint64_t IntrinsicInst * II
SI optimize exec mask operations pre RA
#define LLVM_DEBUG(...)
Definition Debug.h:119
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:731
Module * getParent()
Get the module that this global value is contained inside of...
unsigned getID() const
getID() - Return the register class ID number.
MachineInstrBundleIterator< MachineInstr > iterator
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
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.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
Representation of each machine instruction.
bool canReserveReg(MCRegister PhysReg) const
canReserveReg - Returns true if PhysReg can be used as a reserved register.
MipsFunctionInfo - This class is derived from MachineFunction private Mips target-specific informatio...
static unsigned getPICCallReg()
Get PIC indirect call register.
const uint32_t * getCallPreservedMask(const MachineFunction &MF, CallingConv::ID) const override
Register getFrameRegister(const MachineFunction &MF) const override
Debug information queries.
bool eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj, unsigned FIOperandNum, RegScavenger *RS=nullptr) const override
Stack Frame Processing Methods.
unsigned getRegPressureLimit(const TargetRegisterClass *RC, MachineFunction &MF) const override
Code Generation virtual methods...
static const uint32_t * getMips16RetHelperMask()
bool canRealignStack(const MachineFunction &MF) const override
BitVector getReservedRegs(const MachineFunction &MF) const override
const MCPhysReg * getCalleeSavedRegs(const MachineFunction *MF) const override
Mips Callee Saved Registers.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
NamedMDNode * getNamedMetadata(StringRef Name) const
Return the first NamedMDNode in the module with the specified name.
Definition Module.cpp:320
Wrapper class representing virtual and physical registers.
Definition Register.h:20
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.
virtual bool canRealignStack(const MachineFunction &MF) const
True if the stack can be realigned for the target.
virtual const TargetFrameLowering * getFrameLowering() const
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
void initLLVMToCVRegMapping(MCRegisterInfo *MRI)
constexpr MCPhysReg FP_64
constexpr MCPhysReg GP_64
constexpr MCPhysReg FP
constexpr MCPhysReg GP
constexpr MCPhysReg RA_64
constexpr MCPhysReg SP
constexpr MCPhysReg SP_64
constexpr MCPhysReg RA
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58