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 *
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 {
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 // Frame index elimination may change a previously encodable displacement
154 // into one that does not fit si14 << 2. Use the equivalent unscaled
155 // instruction and let the existing si12 handling legalize the final offset.
156 if (!isShiftedInt<14, 2>(FixedOffset)) {
157 unsigned NewOpc = 0;
158 switch (MIOpc) {
159 default:
160 break;
161 case LoongArch::LDPTR_W:
162 NewOpc = LoongArch::LD_W;
163 break;
164 case LoongArch::LDPTR_D:
165 NewOpc = LoongArch::LD_D;
166 break;
167 case LoongArch::STPTR_W:
168 NewOpc = LoongArch::ST_W;
169 break;
170 case LoongArch::STPTR_D:
171 NewOpc = LoongArch::ST_D;
172 break;
173 }
174 if (NewOpc) {
175 MI.setDesc(TII->get(NewOpc));
176 MIOpc = NewOpc;
177 }
178 }
179
180 bool OffsetLegal = true;
181 // True only when the instruction's immediate field is si12.
182 bool HasSimm12Field = false;
183 // Replace ld/st with ldptr/stptr if possible.
184 unsigned PtrOpc = 0;
185
186 switch (MIOpc) {
187 case LoongArch::LDPTR_W:
188 case LoongArch::LDPTR_D:
189 case LoongArch::STPTR_W:
190 case LoongArch::STPTR_D:
191 PtrOpc = MIOpc;
192 break;
193 case LoongArch::LD_W:
194 HasSimm12Field = true;
195 PtrOpc = LoongArch::LDPTR_W;
196 break;
197 case LoongArch::LD_D:
198 HasSimm12Field = true;
199 PtrOpc = LoongArch::LDPTR_D;
200 break;
201 case LoongArch::ST_W:
202 HasSimm12Field = true;
203 PtrOpc = LoongArch::STPTR_W;
204 break;
205 case LoongArch::ST_D:
206 HasSimm12Field = true;
207 PtrOpc = LoongArch::STPTR_D;
208 break;
209 case LoongArch::LD_B:
210 case LoongArch::LD_H:
211 case LoongArch::LD_BU:
212 case LoongArch::LD_HU:
213 case LoongArch::LD_WU:
214 case LoongArch::ST_B:
215 case LoongArch::ST_H:
216 case LoongArch::FLD_S:
217 case LoongArch::FLD_D:
218 case LoongArch::FST_S:
219 case LoongArch::FST_D:
220 case LoongArch::VLD:
221 case LoongArch::VST:
222 case LoongArch::XVLD:
223 case LoongArch::XVST:
224 case LoongArch::VLDREPL_B:
225 case LoongArch::XVLDREPL_B:
226 HasSimm12Field = true;
227 break;
228 }
229
230 // Handle offsets that exceed the immediate range of the instruction.
231 switch (MIOpc) {
232 case LoongArch::VLDREPL_H:
233 case LoongArch::XVLDREPL_H:
234 OffsetLegal = isShiftedInt<11, 1>(FixedOffset);
235 break;
236 case LoongArch::VLDREPL_W:
237 case LoongArch::XVLDREPL_W:
238 OffsetLegal = isShiftedInt<10, 2>(FixedOffset);
239 break;
240 case LoongArch::VLDREPL_D:
241 case LoongArch::XVLDREPL_D:
242 OffsetLegal = isShiftedInt<9, 3>(FixedOffset);
243 break;
244 case LoongArch::VSTELM_B:
245 case LoongArch::XVSTELM_B:
246 OffsetLegal = isInt<8>(FixedOffset);
247 break;
248 case LoongArch::VSTELM_H:
249 case LoongArch::XVSTELM_H:
250 OffsetLegal = isShiftedInt<8, 1>(FixedOffset);
251 break;
252 case LoongArch::VSTELM_W:
253 case LoongArch::XVSTELM_W:
254 OffsetLegal = isShiftedInt<8, 2>(FixedOffset);
255 break;
256 case LoongArch::VSTELM_D:
257 case LoongArch::XVSTELM_D:
258 OffsetLegal = isShiftedInt<8, 3>(FixedOffset);
259 break;
260 }
261
262 if (!OffsetLegal && isInt<12>(FixedOffset)) {
263 unsigned Addi = IsLA64 ? LoongArch::ADDI_D : LoongArch::ADDI_W;
264
265 // The offset fits in si12 but is not legal for the instruction,
266 // so use only one scratch register instead.
267 Register ScratchReg = MRI.createVirtualRegister(&LoongArch::GPRRegClass);
268 BuildMI(MBB, II, DL, TII->get(Addi), ScratchReg)
269 .addReg(FrameReg)
270 .addImm(FixedOffset);
272 FrameReg = ScratchReg;
273 FrameRegIsKill = true;
274 }
275
276 // If the offset doesn't fit the si12 field of ld/st but does fit
277 // the si14 << 2 field of ldptr/stptr, replace with the latter one.
278 if (IsLA64 && !isInt<12>(FixedOffset) && isShiftedInt<14, 2>(FixedOffset)) {
279 if (PtrOpc) {
280 MI.setDesc(TII->get(PtrOpc));
281 MI.getOperand(FIOperandNum)
282 .ChangeToRegister(FrameReg, false, false, FrameRegIsKill);
283 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(FixedOffset);
284 return false;
285 }
286 }
287
288 if (!isInt<12>(FixedOffset)) {
289 unsigned Addi = IsLA64 ? LoongArch::ADDI_D : LoongArch::ADDI_W;
290 unsigned Add = IsLA64 ? LoongArch::ADD_D : LoongArch::ADD_W;
291 int64_t Val = FixedOffset;
292 // Keep the old behaviour of materializing the whole offset for instructions
293 // do not have simm12 field or ADDI due to it will be replaced.
294 int64_t Lo12 = !HasSimm12Field ? 0 : SignExtend64<12>(Val);
295 uint64_t HiVal = (uint64_t)Val - (uint64_t)Lo12;
296
297 // The offset won't fit in an immediate, so use a scratch register instead.
298 // Modify Offset and FrameReg appropriately.
299 Register ScratchReg = MRI.createVirtualRegister(&LoongArch::GPRRegClass);
300 // Move lower 12-bit of offset into original instruction.
302
303 if (HasSimm12Field && isShiftedInt<20, 12>(HiVal)) {
304 BuildMI(MBB, II, DL, TII->get(LoongArch::PseudoAddUpperImm), ScratchReg)
305 .addReg(FrameReg)
306 .addImm(static_cast<int64_t>(HiVal) >> 12);
307 } else {
308 TII->movImm(MBB, II, DL, ScratchReg, HiVal);
309 if (MIOpc == Addi) {
310 BuildMI(MBB, II, DL, TII->get(Add), MI.getOperand(0).getReg())
311 .addReg(FrameReg)
312 .addReg(ScratchReg, RegState::Kill);
313 MI.eraseFromParent();
314 return true;
315 }
316 BuildMI(MBB, II, DL, TII->get(Add), ScratchReg)
317 .addReg(FrameReg)
318 .addReg(ScratchReg, RegState::Kill);
319 }
320 FrameReg = ScratchReg;
321 FrameRegIsKill = true;
322 }
323
324 // Spill CFRs.
325 if (MIOpc == LoongArch::PseudoST_CFR) {
326 Register ScratchReg = MRI.createVirtualRegister(&LoongArch::GPRRegClass);
327 BuildMI(MBB, II, DL, TII->get(LoongArch::MOVCF2GR), ScratchReg)
328 .add(MI.getOperand(0));
329 BuildMI(MBB, II, DL, TII->get(IsLA64 ? LoongArch::ST_D : LoongArch::ST_W))
330 .addReg(ScratchReg, RegState::Kill)
331 .addReg(FrameReg)
332 .addImm(Offset.getFixed());
333 MI.eraseFromParent();
334 return true;
335 }
336
337 // Reload CFRs.
338 if (MIOpc == LoongArch::PseudoLD_CFR) {
339 Register ScratchReg = MRI.createVirtualRegister(&LoongArch::GPRRegClass);
340 BuildMI(MBB, II, DL, TII->get(IsLA64 ? LoongArch::LD_D : LoongArch::LD_W),
341 ScratchReg)
342 .addReg(FrameReg)
343 .addImm(Offset.getFixed());
344 BuildMI(MBB, II, DL, TII->get(LoongArch::MOVGR2CF))
345 .add(MI.getOperand(0))
346 .addReg(ScratchReg, RegState::Kill);
347 MI.eraseFromParent();
348 return true;
349 }
350
351 MI.getOperand(FIOperandNum)
352 .ChangeToRegister(FrameReg, false, false, FrameRegIsKill);
353 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset.getFixed());
354 return false;
355}
356
359 return false;
360
361 const MachineRegisterInfo *MRI = &MF.getRegInfo();
362 const LoongArchFrameLowering *TFI = getFrameLowering(MF);
363
364 // Stack realignment requires a frame pointer. If we already started
365 // register allocation with frame pointer elimination, it is too late now.
366 if (!MRI->canReserveReg(LoongArch::R22))
367 return false;
368
369 // We may also need a base pointer if there are dynamic allocas or stack
370 // pointer adjustments around calls.
371 if (TFI->hasReservedCallFrame(MF))
372 return true;
373
374 // A base pointer is required and allowed. Check that it isn't too late to
375 // reserve it.
376 return MRI->canReserveReg(LoongArchABI::getBPReg());
377}
const AArch64Subtarget & Subtarget
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
RISCVCC::CondCode CC
MachineRegisterInfo & MRI
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,...
Wrapper class representing virtual and physical registers.
Definition Register.h:20
StackOffset holds a fixed and a scalable offset in bytes.
Definition TypeSize.h:31
int64_t getFixed() const
Returns the fixed component of the stack.
Definition TypeSize.h:47
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:572
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