LLVM 20.0.0git
MipsSERegisterInfo.cpp
Go to the documentation of this file.
1//===-- MipsSERegisterInfo.cpp - MIPS32/64 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 MIPS32/64 implementation of the TargetRegisterInfo
10// class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MipsSERegisterInfo.h"
15#include "Mips.h"
16#include "MipsMachineFunction.h"
17#include "MipsSEInstrInfo.h"
18#include "MipsSubtarget.h"
19#include "MipsTargetMachine.h"
26#include "llvm/IR/Function.h"
27#include "llvm/IR/Type.h"
28#include "llvm/Support/Debug.h"
32
33using namespace llvm;
34
35#define DEBUG_TYPE "mips-reg-info"
36
38
41 return true;
42}
43
46 return true;
47}
48
51 if (Size == 4)
52 return &Mips::GPR32RegClass;
53
54 assert(Size == 8);
55 return &Mips::GPR64RegClass;
56}
57
58/// Get the size of the offset supported by the given load/store/inline asm.
59/// The result includes the effects of any scale factors applied to the
60/// instruction immediate.
61static inline unsigned getLoadStoreOffsetSizeInBits(const unsigned Opcode,
62 MachineOperand MO) {
63 switch (Opcode) {
64 case Mips::LD_B:
65 case Mips::ST_B:
66 return 10;
67 case Mips::LD_H:
68 case Mips::ST_H:
69 return 10 + 1 /* scale factor */;
70 case Mips::LD_W:
71 case Mips::ST_W:
72 return 10 + 2 /* scale factor */;
73 case Mips::LD_D:
74 case Mips::ST_D:
75 return 10 + 3 /* scale factor */;
76 case Mips::LL:
77 case Mips::LL64:
78 case Mips::LLD:
79 case Mips::LLE:
80 case Mips::SC:
81 case Mips::SC64:
82 case Mips::SCD:
83 case Mips::SCE:
84 return 16;
85 case Mips::LLE_MM:
86 case Mips::LL_MM:
87 case Mips::SCE_MM:
88 case Mips::SC_MM:
89 return 12;
90 case Mips::LL64_R6:
91 case Mips::LL_R6:
92 case Mips::LLD_R6:
93 case Mips::SC64_R6:
94 case Mips::SCD_R6:
95 case Mips::SC_R6:
96 case Mips::LL_MMR6:
97 case Mips::SC_MMR6:
98 return 9;
99 case Mips::INLINEASM: {
100 const InlineAsm::Flag F(MO.getImm());
101 switch (F.getMemoryConstraintID()) {
103 const MipsSubtarget &Subtarget = MO.getParent()
104 ->getParent()
105 ->getParent()
107 if (Subtarget.inMicroMipsMode())
108 return 12;
109
110 if (Subtarget.hasMips32r6())
111 return 9;
112
113 return 16;
114 }
115 default:
116 return 16;
117 }
118 }
119 default:
120 return 16;
121 }
122}
123
124/// Get the scale factor applied to the immediate in the given load/store.
125static inline unsigned getLoadStoreOffsetAlign(const unsigned Opcode) {
126 switch (Opcode) {
127 case Mips::LD_H:
128 case Mips::ST_H:
129 return 2;
130 case Mips::LD_W:
131 case Mips::ST_W:
132 return 4;
133 case Mips::LD_D:
134 case Mips::ST_D:
135 return 8;
136 default:
137 return 1;
138 }
139}
140
141void MipsSERegisterInfo::eliminateFI(MachineBasicBlock::iterator II,
142 unsigned OpNo, int FrameIndex,
143 uint64_t StackSize,
144 int64_t SPOffset) const {
145 MachineInstr &MI = *II;
146 MachineFunction &MF = *MI.getParent()->getParent();
147 MachineFrameInfo &MFI = MF.getFrameInfo();
149
151 static_cast<const MipsTargetMachine &>(MF.getTarget()).getABI();
153 static_cast<const MipsRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
154
155 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
156 int MinCSFI = 0;
157 int MaxCSFI = -1;
158
159 if (CSI.size()) {
160 MinCSFI = CSI[0].getFrameIdx();
161 MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
162 }
163
164 bool EhDataRegFI = MipsFI->isEhDataRegFI(FrameIndex);
165 bool IsISRRegFI = MipsFI->isISRRegFI(FrameIndex);
166 // The following stack frame objects are always referenced relative to $sp:
167 // 1. Outgoing arguments.
168 // 2. Pointer to dynamically allocated stack space.
169 // 3. Locations for callee-saved registers.
170 // 4. Locations for eh data registers.
171 // 5. Locations for ISR saved Coprocessor 0 registers 12 & 14.
172 // Everything else is referenced relative to whatever register
173 // getFrameRegister() returns.
174 unsigned FrameReg;
175
176 if ((FrameIndex >= MinCSFI && FrameIndex <= MaxCSFI) || EhDataRegFI ||
177 IsISRRegFI)
178 FrameReg = ABI.GetStackPtr();
179 else if (RegInfo->hasStackRealignment(MF)) {
180 if (MFI.hasVarSizedObjects() && !MFI.isFixedObjectIndex(FrameIndex))
181 FrameReg = ABI.GetBasePtr();
182 else if (MFI.isFixedObjectIndex(FrameIndex))
183 FrameReg = getFrameRegister(MF);
184 else
185 FrameReg = ABI.GetStackPtr();
186 } else
187 FrameReg = getFrameRegister(MF);
188
189 // Calculate final offset.
190 // - There is no need to change the offset if the frame object is one of the
191 // following: an outgoing argument, pointer to a dynamically allocated
192 // stack space or a $gp restore location,
193 // - If the frame object is any of the following, its offset must be adjusted
194 // by adding the size of the stack:
195 // incoming argument, callee-saved register location or local variable.
196 bool IsKill = false;
197 int64_t Offset;
198
199 Offset = SPOffset + (int64_t)StackSize;
200 Offset += MI.getOperand(OpNo + 1).getImm();
201
202 LLVM_DEBUG(errs() << "Offset : " << Offset << "\n"
203 << "<--------->\n");
204
205 if (!MI.isDebugValue()) {
206 // Make sure Offset fits within the field available.
207 // For MSA instructions, this is a 10-bit signed immediate (scaled by
208 // element size), otherwise it is a 16-bit signed immediate.
209 unsigned OffsetBitSize =
210 getLoadStoreOffsetSizeInBits(MI.getOpcode(), MI.getOperand(OpNo - 1));
211 const Align OffsetAlign(getLoadStoreOffsetAlign(MI.getOpcode()));
212 if (OffsetBitSize < 16 && isInt<16>(Offset) &&
213 (!isIntN(OffsetBitSize, Offset) || !isAligned(OffsetAlign, Offset))) {
214 // If we have an offset that needs to fit into a signed n-bit immediate
215 // (where n < 16) and doesn't, but does fit into 16-bits then use an ADDiu
216 MachineBasicBlock &MBB = *MI.getParent();
217 DebugLoc DL = II->getDebugLoc();
218 const TargetRegisterClass *PtrRC =
219 ABI.ArePtrs64bit() ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
221 Register Reg = RegInfo.createVirtualRegister(PtrRC);
222 const MipsSEInstrInfo &TII =
223 *static_cast<const MipsSEInstrInfo *>(
225 BuildMI(MBB, II, DL, TII.get(ABI.GetPtrAddiuOp()), Reg)
226 .addReg(FrameReg)
227 .addImm(Offset);
228
229 FrameReg = Reg;
230 Offset = 0;
231 IsKill = true;
232 } else if (!isInt<16>(Offset)) {
233 // Otherwise split the offset into 16-bit pieces and add it in multiple
234 // instructions.
235 MachineBasicBlock &MBB = *MI.getParent();
236 DebugLoc DL = II->getDebugLoc();
237 unsigned NewImm = 0;
238 const MipsSEInstrInfo &TII =
239 *static_cast<const MipsSEInstrInfo *>(
241 unsigned Reg = TII.loadImmediate(Offset, MBB, II, DL,
242 OffsetBitSize == 16 ? &NewImm : nullptr);
243 BuildMI(MBB, II, DL, TII.get(ABI.GetPtrAdduOp()), Reg).addReg(FrameReg)
244 .addReg(Reg, RegState::Kill);
245
246 FrameReg = Reg;
247 Offset = SignExtend64<16>(NewImm);
248 IsKill = true;
249 }
250 }
251
252 MI.getOperand(OpNo).ChangeToRegister(FrameReg, false, false, IsKill);
253 MI.getOperand(OpNo + 1).ChangeToImmediate(Offset);
254}
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define LLVM_DEBUG(...)
Definition: Debug.h:106
uint64_t Size
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
static unsigned getLoadStoreOffsetAlign(const unsigned Opcode)
Get the scale factor applied to the immediate in the given load/store.
static unsigned getLoadStoreOffsetSizeInBits(const unsigned Opcode, MachineOperand MO)
Get the size of the offset supported by the given load/store/inline asm.
uint64_t IntrinsicInst * II
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A debug info location.
Definition: DebugLoc.h:33
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
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
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
Definition: MachineInstr.h:69
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:347
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
MipsFunctionInfo - This class is derived from MachineFunction private Mips target-specific informatio...
bool isISRRegFI(int FI) const
bool isEhDataRegFI(int FI) const
Register getFrameRegister(const MachineFunction &MF) const override
Debug information queries.
const TargetRegisterClass * intRegClass(unsigned Size) const override
Return GPR register class.
bool requiresRegisterScavenging(const MachineFunction &MF) const override
bool requiresFrameIndexScavenging(const MachineFunction &MF) const override
bool hasMips32r6() const
bool inMicroMipsMode() const
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
virtual const TargetInstrInfo * getInstrInfo() const
@ Kill
The last use of a register.
Reg
All possible values of the reg field in the ModR/M byte.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
bool isAligned(Align Lhs, uint64_t SizeInBytes)
Checks that SizeInBytes is a multiple of the alignment.
Definition: Alignment.h:145
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:260
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39