LLVM 22.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
43 return CSR_NoRegs_SaveList;
45 return CSR_MostRegs_SaveList;
46 switch (Subtarget.getTargetABI()) {
47 default:
48 llvm_unreachable("Unrecognized ABI");
51 return CSR_ILP32S_LP64S_SaveList;
54 return CSR_ILP32F_LP64F_SaveList;
57 return CSR_ILP32D_LP64D_SaveList;
58 }
59}
60
61const uint32_t *
63 CallingConv::ID CC) const {
64 auto &Subtarget = MF.getSubtarget<LoongArchSubtarget>();
65
66 if (CC == CallingConv::GHC)
67 return CSR_NoRegs_RegMask;
69 return CSR_MostRegs_RegMask;
70 switch (Subtarget.getTargetABI()) {
71 default:
72 llvm_unreachable("Unrecognized ABI");
75 return CSR_ILP32S_LP64S_RegMask;
78 return CSR_ILP32F_LP64F_RegMask;
81 return CSR_ILP32D_LP64D_RegMask;
82 }
83}
84
86 return CSR_NoRegs_RegMask;
87}
88
91 const LoongArchFrameLowering *TFI = getFrameLowering(MF);
92 BitVector Reserved(getNumRegs());
93
94 // Use markSuperRegs to ensure any register aliases are also reserved
95 markSuperRegs(Reserved, LoongArch::R0); // zero
96 markSuperRegs(Reserved, LoongArch::R2); // tp
97 markSuperRegs(Reserved, LoongArch::R3); // sp
98 markSuperRegs(Reserved, LoongArch::R21); // non-allocatable
99 if (TFI->hasFP(MF))
100 markSuperRegs(Reserved, LoongArch::R22); // fp
101 // Reserve the base register if we need to realign the stack and allocate
102 // variable-sized objects at runtime.
103 if (TFI->hasBP(MF))
104 markSuperRegs(Reserved, LoongArchABI::getBPReg()); // bp
105
106 assert(checkAllSuperRegsMarked(Reserved));
107 return Reserved;
108}
109
112 const TargetFrameLowering *TFI = getFrameLowering(MF);
113 return TFI->hasFP(MF) ? LoongArch::R22 : LoongArch::R3;
114}
115
117 int SPAdj,
118 unsigned FIOperandNum,
119 RegScavenger *RS) const {
120 // TODO: this implementation is a temporary placeholder which does just
121 // enough to allow other aspects of code generation to be tested.
122
123 assert(SPAdj == 0 && "Unexpected non-zero SPAdj value");
124
125 MachineInstr &MI = *II;
126 assert(MI.getOperand(FIOperandNum + 1).isImm() &&
127 "Unexpected FI-consuming insn");
128
129 MachineBasicBlock &MBB = *MI.getParent();
130 MachineFunction &MF = *MI.getParent()->getParent();
133 const LoongArchInstrInfo *TII = STI.getInstrInfo();
135 DebugLoc DL = MI.getDebugLoc();
136 bool IsLA64 = STI.is64Bit();
137 unsigned MIOpc = MI.getOpcode();
138
139 int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
140 Register FrameReg;
142 TFI->getFrameIndexReference(MF, FrameIndex, FrameReg) +
143 StackOffset::getFixed(MI.getOperand(FIOperandNum + 1).getImm());
144
145 bool FrameRegIsKill = false;
146
147 int FixedOffset = Offset.getFixed();
148 bool OffsetLegal = true;
149
150 // Handle offsets that exceed the immediate range of the instruction.
151 switch (MIOpc) {
152 case LoongArch::VSTELM_B:
153 case LoongArch::XVSTELM_B:
154 OffsetLegal = isInt<8>(FixedOffset);
155 break;
156 case LoongArch::VSTELM_H:
157 case LoongArch::XVSTELM_H:
158 OffsetLegal = isShiftedInt<8, 1>(FixedOffset);
159 break;
160 case LoongArch::VSTELM_W:
161 case LoongArch::XVSTELM_W:
162 OffsetLegal = isShiftedInt<8, 2>(FixedOffset);
163 break;
164 case LoongArch::VSTELM_D:
165 case LoongArch::XVSTELM_D:
166 OffsetLegal = isShiftedInt<8, 3>(FixedOffset);
167 break;
168 }
169
170 if (!OffsetLegal && isInt<12>(FixedOffset)) {
171 unsigned Addi = IsLA64 ? LoongArch::ADDI_D : LoongArch::ADDI_W;
172
173 // The offset fits in si12 but is not legal for the instruction,
174 // so use only one scratch register instead.
175 Register ScratchReg = MRI.createVirtualRegister(&LoongArch::GPRRegClass);
176 BuildMI(MBB, II, DL, TII->get(Addi), ScratchReg)
177 .addReg(FrameReg)
178 .addImm(FixedOffset);
180 FrameReg = ScratchReg;
181 FrameRegIsKill = true;
182 }
183
184 if (!isInt<12>(FixedOffset)) {
185 unsigned Addi = IsLA64 ? LoongArch::ADDI_D : LoongArch::ADDI_W;
186 unsigned Add = IsLA64 ? LoongArch::ADD_D : LoongArch::ADD_W;
187
188 // The offset won't fit in an immediate, so use a scratch register instead.
189 // Modify Offset and FrameReg appropriately.
190 Register ScratchReg = MRI.createVirtualRegister(&LoongArch::GPRRegClass);
191 TII->movImm(MBB, II, DL, ScratchReg, Offset.getFixed());
192 if (MIOpc == Addi) {
193 BuildMI(MBB, II, DL, TII->get(Add), MI.getOperand(0).getReg())
194 .addReg(FrameReg)
195 .addReg(ScratchReg, RegState::Kill);
196 MI.eraseFromParent();
197 return true;
198 }
199 BuildMI(MBB, II, DL, TII->get(Add), ScratchReg)
200 .addReg(FrameReg)
201 .addReg(ScratchReg, RegState::Kill);
203 FrameReg = ScratchReg;
204 FrameRegIsKill = true;
205 }
206
207 // Spill CFRs.
208 if (MIOpc == LoongArch::PseudoST_CFR) {
209 Register ScratchReg = MRI.createVirtualRegister(&LoongArch::GPRRegClass);
210 BuildMI(MBB, II, DL, TII->get(LoongArch::MOVCF2GR), ScratchReg)
211 .add(MI.getOperand(0));
212 BuildMI(MBB, II, DL, TII->get(IsLA64 ? LoongArch::ST_D : LoongArch::ST_W))
213 .addReg(ScratchReg, RegState::Kill)
214 .addReg(FrameReg)
215 .addImm(Offset.getFixed());
216 MI.eraseFromParent();
217 return true;
218 }
219
220 // Reload CFRs.
221 if (MIOpc == LoongArch::PseudoLD_CFR) {
222 Register ScratchReg = MRI.createVirtualRegister(&LoongArch::GPRRegClass);
223 BuildMI(MBB, II, DL, TII->get(IsLA64 ? LoongArch::LD_D : LoongArch::LD_W),
224 ScratchReg)
225 .addReg(FrameReg)
226 .addImm(Offset.getFixed());
227 BuildMI(MBB, II, DL, TII->get(LoongArch::MOVGR2CF))
228 .add(MI.getOperand(0))
229 .addReg(ScratchReg, RegState::Kill);
230 MI.eraseFromParent();
231 return true;
232 }
233
234 MI.getOperand(FIOperandNum)
235 .ChangeToRegister(FrameReg, false, false, FrameRegIsKill);
236 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset.getFixed());
237 return false;
238}
239
242 return false;
243
244 const MachineRegisterInfo *MRI = &MF.getRegInfo();
245 const LoongArchFrameLowering *TFI = getFrameLowering(MF);
246
247 // Stack realignment requires a frame pointer. If we already started
248 // register allocation with frame pointer elimination, it is too late now.
249 if (!MRI->canReserveReg(LoongArch::R22))
250 return false;
251
252 // We may also need a base pointer if there are dynamic allocas or stack
253 // pointer adjustments around calls.
254 if (TFI->hasReservedCallFrame(MF))
255 return true;
256
257 // A base pointer is required and allowed. Check that it isn't too late to
258 // reserve it.
259 return MRI->canReserveReg(LoongArchABI::getBPReg());
260}
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:124
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 & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
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:19
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
@ Kill
The last use of a register.
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
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:174
@ 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:191
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