LLVM 23.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
155 // Handle offsets that exceed the immediate range of the instruction.
156 switch (MIOpc) {
157 case LoongArch::VSTELM_B:
158 case LoongArch::XVSTELM_B:
159 OffsetLegal = isInt<8>(FixedOffset);
160 break;
161 case LoongArch::VSTELM_H:
162 case LoongArch::XVSTELM_H:
163 OffsetLegal = isShiftedInt<8, 1>(FixedOffset);
164 break;
165 case LoongArch::VSTELM_W:
166 case LoongArch::XVSTELM_W:
167 OffsetLegal = isShiftedInt<8, 2>(FixedOffset);
168 break;
169 case LoongArch::VSTELM_D:
170 case LoongArch::XVSTELM_D:
171 OffsetLegal = isShiftedInt<8, 3>(FixedOffset);
172 break;
173 }
174
175 if (!OffsetLegal && isInt<12>(FixedOffset)) {
176 unsigned Addi = IsLA64 ? LoongArch::ADDI_D : LoongArch::ADDI_W;
177
178 // The offset fits in si12 but is not legal for the instruction,
179 // so use only one scratch register instead.
180 Register ScratchReg = MRI.createVirtualRegister(&LoongArch::GPRRegClass);
181 BuildMI(MBB, II, DL, TII->get(Addi), ScratchReg)
182 .addReg(FrameReg)
183 .addImm(FixedOffset);
185 FrameReg = ScratchReg;
186 FrameRegIsKill = true;
187 }
188
189 if (!isInt<12>(FixedOffset)) {
190 unsigned Addi = IsLA64 ? LoongArch::ADDI_D : LoongArch::ADDI_W;
191 unsigned Add = IsLA64 ? LoongArch::ADD_D : LoongArch::ADD_W;
192
193 // The offset won't fit in an immediate, so use a scratch register instead.
194 // Modify Offset and FrameReg appropriately.
195 Register ScratchReg = MRI.createVirtualRegister(&LoongArch::GPRRegClass);
196 TII->movImm(MBB, II, DL, ScratchReg, Offset.getFixed());
197 if (MIOpc == Addi) {
198 BuildMI(MBB, II, DL, TII->get(Add), MI.getOperand(0).getReg())
199 .addReg(FrameReg)
200 .addReg(ScratchReg, RegState::Kill);
201 MI.eraseFromParent();
202 return true;
203 }
204 BuildMI(MBB, II, DL, TII->get(Add), ScratchReg)
205 .addReg(FrameReg)
206 .addReg(ScratchReg, RegState::Kill);
208 FrameReg = ScratchReg;
209 FrameRegIsKill = true;
210 }
211
212 // Spill CFRs.
213 if (MIOpc == LoongArch::PseudoST_CFR) {
214 Register ScratchReg = MRI.createVirtualRegister(&LoongArch::GPRRegClass);
215 BuildMI(MBB, II, DL, TII->get(LoongArch::MOVCF2GR), ScratchReg)
216 .add(MI.getOperand(0));
217 BuildMI(MBB, II, DL, TII->get(IsLA64 ? LoongArch::ST_D : LoongArch::ST_W))
218 .addReg(ScratchReg, RegState::Kill)
219 .addReg(FrameReg)
220 .addImm(Offset.getFixed());
221 MI.eraseFromParent();
222 return true;
223 }
224
225 // Reload CFRs.
226 if (MIOpc == LoongArch::PseudoLD_CFR) {
227 Register ScratchReg = MRI.createVirtualRegister(&LoongArch::GPRRegClass);
228 BuildMI(MBB, II, DL, TII->get(IsLA64 ? LoongArch::LD_D : LoongArch::LD_W),
229 ScratchReg)
230 .addReg(FrameReg)
231 .addImm(Offset.getFixed());
232 BuildMI(MBB, II, DL, TII->get(LoongArch::MOVGR2CF))
233 .add(MI.getOperand(0))
234 .addReg(ScratchReg, RegState::Kill);
235 MI.eraseFromParent();
236 return true;
237 }
238
239 MI.getOperand(FIOperandNum)
240 .ChangeToRegister(FrameReg, false, false, FrameRegIsKill);
241 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset.getFixed());
242 return false;
243}
244
247 return false;
248
249 const MachineRegisterInfo *MRI = &MF.getRegInfo();
250 const LoongArchFrameLowering *TFI = getFrameLowering(MF);
251
252 // Stack realignment requires a frame pointer. If we already started
253 // register allocation with frame pointer elimination, it is too late now.
254 if (!MRI->canReserveReg(LoongArch::R22))
255 return false;
256
257 // We may also need a base pointer if there are dynamic allocas or stack
258 // pointer adjustments around calls.
259 if (TFI->hasReservedCallFrame(MF))
260 return true;
261
262 // A base pointer is required and allowed. Check that it isn't too late to
263 // reserve it.
264 return MRI->canReserveReg(LoongArchABI::getBPReg());
265}
unsigned const MachineRegisterInfo * MRI
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:123
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:270
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: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.
Definition Types.h:26
@ Offset
Definition DWP.cpp:532
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:165
@ 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:182
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