LLVM 24.0.0git
LoongArchRegisterInfo.cpp
Go to the documentation of this file.
1//===- LoongArchRegisterInfo.cpp - LoongArch 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 LoongArch implementation of the TargetRegisterInfo
10// class.
11//
12//===----------------------------------------------------------------------===//
13
15#include "LoongArch.h"
16#include "LoongArchInstrInfo.h"
17#include "LoongArchSubtarget.h"
27
28using namespace llvm;
29
30#define GET_REGINFO_TARGET_DESC
31#include "LoongArchGenRegisterInfo.inc"
32
34 : LoongArchGenRegisterInfo(LoongArch::R1, /*DwarfFlavour*/ 0,
35 /*EHFlavor*/ 0,
36 /*PC*/ 0, HwMode) {}
37
38const MCPhysReg *
40 auto &Subtarget = MF->getSubtarget<LoongArchSubtarget>();
41 auto CC = MF->getFunction().getCallingConv();
42
43 if (CC == CallingConv::GHC)
44 return CSR_NoRegs_SaveList;
46 return CSR_NoneRegs_SaveList;
48 return CSR_MostRegs_SaveList;
49 switch (Subtarget.getTargetABI()) {
50 default:
51 llvm_unreachable("Unrecognized ABI");
54 return CSR_ILP32S_LP64S_SaveList;
57 return CSR_ILP32F_LP64F_SaveList;
60 return CSR_ILP32D_LP64D_SaveList;
61 }
62}
63
64const uint32_t *
66 CallingConv::ID CC) const {
67 auto &Subtarget = MF.getSubtarget<LoongArchSubtarget>();
68
69 if (CC == CallingConv::GHC)
70 return CSR_NoRegs_RegMask;
72 return CSR_NoneRegs_RegMask;
74 return CSR_MostRegs_RegMask;
75 switch (Subtarget.getTargetABI()) {
76 default:
77 llvm_unreachable("Unrecognized ABI");
80 return CSR_ILP32S_LP64S_RegMask;
83 return CSR_ILP32F_LP64F_RegMask;
86 return CSR_ILP32D_LP64D_RegMask;
87 }
88}
89
91 return CSR_NoRegs_RegMask;
92}
93
96 const LoongArchFrameLowering *TFI = getFrameLowering(MF);
97 BitVector Reserved(getNumRegs());
98
99 // Use markSuperRegs to ensure any register aliases are also reserved
100 markSuperRegs(Reserved, LoongArch::R0); // zero
101 markSuperRegs(Reserved, LoongArch::R2); // tp
102 markSuperRegs(Reserved, LoongArch::R3); // sp
103 markSuperRegs(Reserved, LoongArch::R21); // non-allocatable
104 if (TFI->hasFP(MF))
105 markSuperRegs(Reserved, LoongArch::R22); // fp
106 // Reserve the base register if we need to realign the stack and allocate
107 // variable-sized objects at runtime.
108 if (TFI->hasBP(MF))
109 markSuperRegs(Reserved, LoongArchABI::getBPReg()); // bp
110
111 assert(checkAllSuperRegsMarked(Reserved));
112 return Reserved;
113}
114
117 const TargetFrameLowering *TFI = getFrameLowering(MF);
118 return TFI->hasFP(MF) ? LoongArch::R22 : LoongArch::R3;
119}
120
122 int SPAdj,
123 unsigned FIOperandNum,
124 RegScavenger *RS) const {
125 // TODO: this implementation is a temporary placeholder which does just
126 // enough to allow other aspects of code generation to be tested.
127
128 assert(SPAdj == 0 && "Unexpected non-zero SPAdj value");
129
130 MachineInstr &MI = *II;
131 assert(MI.getOperand(FIOperandNum + 1).isImm() &&
132 "Unexpected FI-consuming insn");
133
134 MachineBasicBlock &MBB = *MI.getParent();
135 MachineFunction &MF = *MI.getParent()->getParent();
138 const LoongArchInstrInfo *TII = STI.getInstrInfo();
140 DebugLoc DL = MI.getDebugLoc();
141 bool IsLA64 = STI.is64Bit();
142 unsigned MIOpc = MI.getOpcode();
143
144 int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
145 Register FrameReg;
147 TFI->getFrameIndexReference(MF, FrameIndex, FrameReg) +
148 StackOffset::getFixed(MI.getOperand(FIOperandNum + 1).getImm());
149
150 bool FrameRegIsKill = false;
151
152 int FixedOffset = Offset.getFixed();
153 bool OffsetLegal = true;
154 // True only when the instruction's immediate field is si12.
155 bool HasSimm12Field = false;
156 // Replace ld/st with ldptr/stptr if possible.
157 unsigned PtrOpc = 0;
158
159 switch (MIOpc) {
160 case LoongArch::LDPTR_W:
161 case LoongArch::LDPTR_D:
162 case LoongArch::STPTR_W:
163 case LoongArch::STPTR_D:
164 PtrOpc = MIOpc;
165 break;
166 case LoongArch::LD_W:
167 HasSimm12Field = true;
168 PtrOpc = LoongArch::LDPTR_W;
169 break;
170 case LoongArch::LD_D:
171 HasSimm12Field = true;
172 PtrOpc = LoongArch::LDPTR_D;
173 break;
174 case LoongArch::ST_W:
175 HasSimm12Field = true;
176 PtrOpc = LoongArch::STPTR_W;
177 break;
178 case LoongArch::ST_D:
179 HasSimm12Field = true;
180 PtrOpc = LoongArch::STPTR_D;
181 break;
182 case LoongArch::LD_B:
183 case LoongArch::LD_H:
184 case LoongArch::LD_BU:
185 case LoongArch::LD_HU:
186 case LoongArch::LD_WU:
187 case LoongArch::ST_B:
188 case LoongArch::ST_H:
189 case LoongArch::FLD_S:
190 case LoongArch::FLD_D:
191 case LoongArch::FST_S:
192 case LoongArch::FST_D:
193 case LoongArch::VLD:
194 case LoongArch::VST:
195 case LoongArch::XVLD:
196 case LoongArch::XVST:
197 case LoongArch::VLDREPL_B:
198 case LoongArch::XVLDREPL_B:
199 HasSimm12Field = true;
200 break;
201 }
202
203 // Handle offsets that exceed the immediate range of the instruction.
204 switch (MIOpc) {
205 case LoongArch::VSTELM_B:
206 case LoongArch::XVSTELM_B:
207 OffsetLegal = isInt<8>(FixedOffset);
208 break;
209 case LoongArch::VSTELM_H:
210 case LoongArch::XVSTELM_H:
211 OffsetLegal = isShiftedInt<8, 1>(FixedOffset);
212 break;
213 case LoongArch::VSTELM_W:
214 case LoongArch::XVSTELM_W:
215 OffsetLegal = isShiftedInt<8, 2>(FixedOffset);
216 break;
217 case LoongArch::VSTELM_D:
218 case LoongArch::XVSTELM_D:
219 OffsetLegal = isShiftedInt<8, 3>(FixedOffset);
220 break;
221 }
222
223 if (!OffsetLegal && isInt<12>(FixedOffset)) {
224 unsigned Addi = IsLA64 ? LoongArch::ADDI_D : LoongArch::ADDI_W;
225
226 // The offset fits in si12 but is not legal for the instruction,
227 // so use only one scratch register instead.
228 Register ScratchReg = MRI.createVirtualRegister(&LoongArch::GPRRegClass);
229 BuildMI(MBB, II, DL, TII->get(Addi), ScratchReg)
230 .addReg(FrameReg)
231 .addImm(FixedOffset);
233 FrameReg = ScratchReg;
234 FrameRegIsKill = true;
235 }
236
237 // If the offset doesn't fit the si12 field of ld/st but does fit
238 // the si14 << 2 field of ldptr/stptr, replace with the latter one.
239 if (IsLA64 && !isInt<12>(FixedOffset) && isShiftedInt<14, 2>(FixedOffset)) {
240 if (PtrOpc) {
241 MI.setDesc(TII->get(PtrOpc));
242 MI.getOperand(FIOperandNum)
243 .ChangeToRegister(FrameReg, false, false, FrameRegIsKill);
244 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(FixedOffset);
245 return false;
246 }
247 }
248
249 if (!isInt<12>(FixedOffset)) {
250 unsigned Addi = IsLA64 ? LoongArch::ADDI_D : LoongArch::ADDI_W;
251 unsigned Add = IsLA64 ? LoongArch::ADD_D : LoongArch::ADD_W;
252 int64_t Val = FixedOffset;
253 // Keep the old behaviour of materializing the whole offset for instructions
254 // do not have simm12 field or ADDI due to it will be replaced.
255 int64_t Lo12 = !HasSimm12Field ? 0 : SignExtend64<12>(Val);
256 uint64_t HiVal = (uint64_t)Val - (uint64_t)Lo12;
257
258 // The offset won't fit in an immediate, so use a scratch register instead.
259 // Modify Offset and FrameReg appropriately.
260 Register ScratchReg = MRI.createVirtualRegister(&LoongArch::GPRRegClass);
261 // Move lower 12-bit of offset into original instruction.
263
264 if (HasSimm12Field && isShiftedInt<20, 12>(HiVal)) {
265 BuildMI(MBB, II, DL, TII->get(LoongArch::PseudoAddUpperImm), ScratchReg)
266 .addReg(FrameReg)
267 .addImm(static_cast<int64_t>(HiVal) >> 12);
268 } else {
269 TII->movImm(MBB, II, DL, ScratchReg, HiVal);
270 if (MIOpc == Addi) {
271 BuildMI(MBB, II, DL, TII->get(Add), MI.getOperand(0).getReg())
272 .addReg(FrameReg)
273 .addReg(ScratchReg, RegState::Kill);
274 MI.eraseFromParent();
275 return true;
276 }
277 BuildMI(MBB, II, DL, TII->get(Add), ScratchReg)
278 .addReg(FrameReg)
279 .addReg(ScratchReg, RegState::Kill);
280 }
281 FrameReg = ScratchReg;
282 FrameRegIsKill = true;
283 }
284
285 // Spill CFRs.
286 if (MIOpc == LoongArch::PseudoST_CFR) {
287 Register ScratchReg = MRI.createVirtualRegister(&LoongArch::GPRRegClass);
288 BuildMI(MBB, II, DL, TII->get(LoongArch::MOVCF2GR), ScratchReg)
289 .add(MI.getOperand(0));
290 BuildMI(MBB, II, DL, TII->get(IsLA64 ? LoongArch::ST_D : LoongArch::ST_W))
291 .addReg(ScratchReg, RegState::Kill)
292 .addReg(FrameReg)
293 .addImm(Offset.getFixed());
294 MI.eraseFromParent();
295 return true;
296 }
297
298 // Reload CFRs.
299 if (MIOpc == LoongArch::PseudoLD_CFR) {
300 Register ScratchReg = MRI.createVirtualRegister(&LoongArch::GPRRegClass);
301 BuildMI(MBB, II, DL, TII->get(IsLA64 ? LoongArch::LD_D : LoongArch::LD_W),
302 ScratchReg)
303 .addReg(FrameReg)
304 .addImm(Offset.getFixed());
305 BuildMI(MBB, II, DL, TII->get(LoongArch::MOVGR2CF))
306 .add(MI.getOperand(0))
307 .addReg(ScratchReg, RegState::Kill);
308 MI.eraseFromParent();
309 return true;
310 }
311
312 MI.getOperand(FIOperandNum)
313 .ChangeToRegister(FrameReg, false, false, FrameRegIsKill);
314 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset.getFixed());
315 return false;
316}
317
320 return false;
321
322 const MachineRegisterInfo *MRI = &MF.getRegInfo();
323 const LoongArchFrameLowering *TFI = getFrameLowering(MF);
324
325 // Stack realignment requires a frame pointer. If we already started
326 // register allocation with frame pointer elimination, it is too late now.
327 if (!MRI->canReserveReg(LoongArch::R22))
328 return false;
329
330 // We may also need a base pointer if there are dynamic allocas or stack
331 // pointer adjustments around calls.
332 if (TFI->hasReservedCallFrame(MF))
333 return true;
334
335 // A base pointer is required and allowed. Check that it isn't too late to
336 // reserve it.
338}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
uint64_t IntrinsicInst * II
This file declares the machine register scavenger class.
A debug info location.
Definition DebugLoc.h:126
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:273
bool hasReservedCallFrame(const MachineFunction &MF) const override
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required,...
bool hasBP(const MachineFunction &MF) const
const LoongArchInstrInfo * getInstrInfo() const override
MachineInstrBundleIterator< MachineInstr > iterator
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
Representation of each machine instruction.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
bool canReserveReg(MCRegister PhysReg) const
canReserveReg - Returns true if PhysReg can be used as a reserved register.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
StackOffset holds a fixed and a scalable offset in bytes.
Definition TypeSize.h:30
int64_t getFixed() const
Returns the fixed component of the stack.
Definition TypeSize.h:46
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 StackOffset getFrameIndexReference(const MachineFunction &MF, int FI, Register &FrameReg) const
getFrameIndexReference - This method should return the base register and offset used to reference a f...
virtual bool canRealignStack(const MachineFunction &MF) const
True if the stack can be realigned for the target.
virtual const TargetFrameLowering * getFrameLowering() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ PreserveMost
Used for runtime calls that preserves most registers.
Definition CallingConv.h:63
@ GHC
Used by the Glasgow Haskell Compiler (GHC).
Definition CallingConv.h:50
@ PreserveNone
Used for runtime calls that preserves none general registers.
Definition CallingConv.h:90
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
@ Kill
The last use of a register.
@ Add
Sum of integers.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
constexpr bool isShiftedInt(int64_t x)
Checks if a signed integer is an N bit number shifted left by S.
Definition MathExtras.h:183
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:567
bool eliminateFrameIndex(MachineBasicBlock::iterator MI, int SPAdj, unsigned FIOperandNum, RegScavenger *RS=nullptr) const override
const uint32_t * getCallPreservedMask(const MachineFunction &MF, CallingConv::ID) const override
const MCPhysReg * getCalleeSavedRegs(const MachineFunction *MF) const override
Register getFrameRegister(const MachineFunction &MF) const override
const uint32_t * getNoPreservedMask() const override
BitVector getReservedRegs(const MachineFunction &MF) const override
bool canRealignStack(const MachineFunction &MF) const override