LLVM 22.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/Support/Debug.h"
31#include <cstdint>
32
33using namespace llvm;
34
35#define DEBUG_TYPE "mips-reg-info"
36
37#define GET_REGINFO_TARGET_DESC
38#include "MipsGenRegisterInfo.inc"
39
41 : MipsGenRegisterInfo(Mips::RA), ArePtrs64bit(STI.getABI().ArePtrs64bit()) {
43}
44
45unsigned MipsRegisterInfo::getPICCallReg() { return Mips::T9; }
46
49 assert(Kind == 0 && "this should only be used for default case");
50 return ArePtrs64bit ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
51}
52
53unsigned
55 MachineFunction &MF) const {
56 switch (RC->getID()) {
57 default:
58 return 0;
59 case Mips::GPR32RegClassID:
60 case Mips::GPR64RegClassID:
61 case Mips::DSPRRegClassID: {
63 return 28 - TFI->hasFP(MF);
64 }
65 case Mips::FGR32RegClassID:
66 return 32;
67 case Mips::AFGR64RegClassID:
68 return 16;
69 case Mips::FGR64RegClassID:
70 return 32;
71 }
72}
73
74//===----------------------------------------------------------------------===//
75// Callee Saved Registers methods
76//===----------------------------------------------------------------------===//
77
78/// Mips Callee Saved Registers
79const MCPhysReg *
81 const MipsSubtarget &Subtarget = MF->getSubtarget<MipsSubtarget>();
82 const Function &F = MF->getFunction();
83 if (F.hasFnAttribute("interrupt")) {
84 if (Subtarget.hasMips64())
85 return Subtarget.hasMips64r6() ? CSR_Interrupt_64R6_SaveList
86 : CSR_Interrupt_64_SaveList;
87 else
88 return Subtarget.hasMips32r6() ? CSR_Interrupt_32R6_SaveList
89 : CSR_Interrupt_32_SaveList;
90 }
91
92 if (Subtarget.isSingleFloat())
93 return CSR_SingleFloatOnly_SaveList;
94
95 if (Subtarget.isABI_N64())
96 return CSR_N64_SaveList;
97
98 if (Subtarget.isABI_N32())
99 return CSR_N32_SaveList;
100
101 if (Subtarget.isFP64bit())
102 return CSR_O32_FP64_SaveList;
103
104 if (Subtarget.isFPXX())
105 return CSR_O32_FPXX_SaveList;
106
107 return CSR_O32_SaveList;
108}
109
110const uint32_t *
112 CallingConv::ID) const {
113 const MipsSubtarget &Subtarget = MF.getSubtarget<MipsSubtarget>();
114 if (Subtarget.isSingleFloat())
115 return CSR_SingleFloatOnly_RegMask;
116
117 if (Subtarget.isABI_N64())
118 return CSR_N64_RegMask;
119
120 if (Subtarget.isABI_N32())
121 return CSR_N32_RegMask;
122
123 if (Subtarget.isFP64bit())
124 return CSR_O32_FP64_RegMask;
125
126 if (Subtarget.isFPXX())
127 return CSR_O32_FPXX_RegMask;
128
129 return CSR_O32_RegMask;
130}
131
133 return CSR_Mips16RetHelper_RegMask;
134}
135
137getReservedRegs(const MachineFunction &MF) const {
138 static const MCPhysReg ReservedGPR32[] = {
139 Mips::ZERO, Mips::K0, Mips::K1, Mips::SP
140 };
141
142 static const MCPhysReg ReservedGPR64[] = {
143 Mips::ZERO_64, Mips::K0_64, Mips::K1_64, Mips::SP_64
144 };
145
146 BitVector Reserved(getNumRegs());
147 const MipsSubtarget &Subtarget = MF.getSubtarget<MipsSubtarget>();
148
149 for (MCPhysReg R : ReservedGPR32)
150 Reserved.set(R);
151
152 for (MCPhysReg R : ReservedGPR64)
153 Reserved.set(R);
154
155 // For mno-abicalls, GP is a program invariant!
156 if (!Subtarget.isABICalls()) {
157 Reserved.set(Mips::GP);
158 Reserved.set(Mips::GP_64);
159 }
160
161 if (Subtarget.isFP64bit()) {
162 // Reserve all registers in AFGR64.
163 for (MCPhysReg Reg : Mips::AFGR64RegClass)
164 Reserved.set(Reg);
165 } else {
166 // Reserve all registers in FGR64.
167 for (MCPhysReg Reg : Mips::FGR64RegClass)
168 Reserved.set(Reg);
169 }
170 // Reserve FP if this function should have a dedicated frame pointer register.
171 if (Subtarget.getFrameLowering()->hasFP(MF)) {
172 if (Subtarget.inMips16Mode())
173 Reserved.set(Mips::S0);
174 else {
175 Reserved.set(Mips::FP);
176 Reserved.set(Mips::FP_64);
177
178 // Reserve the base register if we need to both realign the stack and
179 // allocate variable-sized objects at runtime. This should test the
180 // same conditions as MipsFrameLowering::hasBP().
181 if (hasStackRealignment(MF) && MF.getFrameInfo().hasVarSizedObjects()) {
182 Reserved.set(Mips::S7);
183 Reserved.set(Mips::S7_64);
184 }
185 }
186 }
187
188 // Reserve hardware registers.
189 Reserved.set(Mips::HWR29);
190 Reserved.set(Mips::HWR2);
191
192 // Reserve DSP control register.
193 Reserved.set(Mips::DSPPos);
194 Reserved.set(Mips::DSPSCount);
195 Reserved.set(Mips::DSPCarry);
196 Reserved.set(Mips::DSPEFI);
197 Reserved.set(Mips::DSPOutFlag);
198
199 // Reserve MSA control registers.
200 for (MCPhysReg Reg : Mips::MSACtrlRegClass)
201 Reserved.set(Reg);
202
203 // Reserve RA if in mips16 mode.
204 if (Subtarget.inMips16Mode()) {
205 const MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
206 Reserved.set(Mips::RA);
207 Reserved.set(Mips::RA_64);
208 Reserved.set(Mips::T0);
209 Reserved.set(Mips::T1);
210 if (MF.getFunction().hasFnAttribute("saveS2") || MipsFI->hasSaveS2())
211 Reserved.set(Mips::S2);
212 }
213
214 // Reserve GP if small section is used.
215 if (Subtarget.useSmallSection()) {
216 Reserved.set(Mips::GP);
217 Reserved.set(Mips::GP_64);
218 }
219
220 return Reserved;
221}
222
223// FrameIndex represent objects inside a abstract stack.
224// We must replace FrameIndex with an stack/frame pointer
225// direct reference.
228 unsigned FIOperandNum, RegScavenger *RS) const {
229 MachineInstr &MI = *II;
230 MachineFunction &MF = *MI.getParent()->getParent();
231
232 LLVM_DEBUG(errs() << "\nFunction : " << MF.getName() << "\n";
233 errs() << "<--------->\n"
234 << MI);
235
236 int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
237 uint64_t stackSize = MF.getFrameInfo().getStackSize();
238 int64_t spOffset = MF.getFrameInfo().getObjectOffset(FrameIndex);
239
240 LLVM_DEBUG(errs() << "FrameIndex : " << FrameIndex << "\n"
241 << "spOffset : " << spOffset << "\n"
242 << "stackSize : " << stackSize << "\n"
243 << "alignment : "
244 << DebugStr(MF.getFrameInfo().getObjectAlign(FrameIndex))
245 << "\n");
246
247 eliminateFI(MI, FIOperandNum, FrameIndex, stackSize, spOffset);
248 return false;
249}
250
252getFrameRegister(const MachineFunction &MF) const {
253 const MipsSubtarget &Subtarget = MF.getSubtarget<MipsSubtarget>();
254 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
255 bool IsN64 =
256 static_cast<const MipsTargetMachine &>(MF.getTarget()).getABI().IsN64();
257
258 if (Subtarget.inMips16Mode())
259 return TFI->hasFP(MF) ? Mips::S0 : Mips::SP;
260 else
261 return TFI->hasFP(MF) ? (IsN64 ? Mips::FP_64 : Mips::FP) :
262 (IsN64 ? Mips::SP_64 : Mips::SP);
263}
264
266 // Avoid realigning functions that explicitly do not want to be realigned.
267 // Normally, we should report an error when a function should be dynamically
268 // realigned but also has the attribute no-realign-stack. Unfortunately,
269 // with this attribute, MachineFrameInfo clamps each new object's alignment
270 // to that of the stack's alignment as specified by the ABI. As a result,
271 // the information of whether we have objects with larger alignment
272 // requirement than the stack's alignment is already lost at this point.
274 return false;
275
276 const MipsSubtarget &Subtarget = MF.getSubtarget<MipsSubtarget>();
277 unsigned FP = Subtarget.isGP32bit() ? Mips::FP : Mips::FP_64;
278 unsigned BP = Subtarget.isGP32bit() ? Mips::S7 : Mips::S7_64;
279
280 // Support dynamic stack realignment for all targets except Mips16.
281 if (Subtarget.inMips16Mode())
282 return false;
283
284 // We can't perform dynamic stack realignment if we can't reserve the
285 // frame pointer register.
286 if (!MF.getRegInfo().canReserveReg(FP))
287 return false;
288
289 // We can realign the stack if we know the maximum call frame size and we
290 // don't have variable sized objects.
291 if (Subtarget.getFrameLowering()->hasReservedCallFrame(MF))
292 return true;
293
294 // We have to reserve the base pointer register in the presence of variable
295 // sized objects.
296 return MF.getRegInfo().canReserveReg(BP);
297}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements the BitVector class.
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:55
uint64_t IntrinsicInst * II
SI optimize exec mask operations pre RA
#define LLVM_DEBUG(...)
Definition Debug.h:114
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:727
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.
bool IsN64() const
Definition MipsABIInfo.h:41
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
static const uint32_t * getMips16RetHelperMask()
bool canRealignStack(const MachineFunction &MF) const override
BitVector getReservedRegs(const MachineFunction &MF) const override
MipsRegisterInfo(const MipsSubtarget &STI)
const MCPhysReg * getCalleeSavedRegs(const MachineFunction *MF) const override
Mips Callee Saved Registers.
const TargetRegisterClass * getPointerRegClass(unsigned Kind) const override
Code Generation virtual methods...
bool hasMips32r6() const
bool isFP64bit() const
bool isGP32bit() const
bool hasMips64r6() const
bool inMips16Mode() const
bool hasMips64() const
bool isABICalls() const
bool isSingleFloat() const
bool useSmallSection() const
const TargetFrameLowering * getFrameLowering() const override
const MipsABIInfo & getABI() const
Wrapper class representing virtual and physical registers.
Definition Register.h:19
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 hasReservedCallFrame(const MachineFunction &MF) const
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required,...
unsigned getID() const
Return the register class ID number.
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)
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