LLVM 24.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"
25#include "llvm/IR/Function.h"
26#include "llvm/IR/Type.h"
27#include "llvm/Support/Debug.h"
31
32using namespace llvm;
33
34#define DEBUG_TYPE "mips-reg-info"
35
38 return true;
39}
40
43 return true;
44}
45
48 if (Size == 4)
49 return &Mips::GPR32RegClass;
50
51 assert(Size == 8);
52 return &Mips::GPR64RegClass;
53}
54
55/// Get the size of the offset supported by the given load/store/inline asm.
56/// The result includes the effects of any scale factors applied to the
57/// instruction immediate.
58static inline unsigned
60 const unsigned Opcode, const MachineOperand &MO) {
61 switch (Opcode) {
62 case Mips::LD_B:
63 case Mips::ST_B:
64 return 10;
65 case Mips::LD_H:
66 case Mips::ST_H:
67 return 10 + 1 /* scale factor */;
68 case Mips::LD_W:
69 case Mips::ST_W:
70 return 10 + 2 /* scale factor */;
71 case Mips::LD_D:
72 case Mips::ST_D:
73 return 10 + 3 /* scale factor */;
74 case Mips::LL:
75 case Mips::LL64:
76 case Mips::LLD:
77 case Mips::LLE:
78 case Mips::SC:
79 case Mips::SC64:
80 case Mips::SCD:
81 case Mips::SCE:
82 return 16;
83 case Mips::LLE_MM:
84 case Mips::LL_MM:
85 case Mips::SCE_MM:
86 case Mips::SC_MM:
87 return 12;
88 case Mips::LL64_R6:
89 case Mips::LL_R6:
90 case Mips::LLD_R6:
91 case Mips::SC64_R6:
92 case Mips::SCD_R6:
93 case Mips::SC_R6:
94 case Mips::LL_MMR6:
95 case Mips::SC_MMR6:
96 return 9;
97 case Mips::INLINEASM: {
98 const InlineAsm::Flag F(MO.getImm());
99 switch (F.getMemoryConstraintID()) {
101 if (Subtarget.inMicroMipsMode())
102 return 12;
103
104 if (Subtarget.hasMips32r6())
105 return 9;
106
107 return 16;
108 }
109 default:
110 return 16;
111 }
112 }
113 default:
114 return 16;
115 }
116}
117
118/// Get the scale factor applied to the immediate in the given load/store.
119static inline unsigned getLoadStoreOffsetAlign(const unsigned Opcode) {
120 switch (Opcode) {
121 case Mips::LD_H:
122 case Mips::ST_H:
123 return 2;
124 case Mips::LD_W:
125 case Mips::ST_W:
126 return 4;
127 case Mips::LD_D:
128 case Mips::ST_D:
129 return 8;
130 default:
131 return 1;
132 }
133}
134
135void MipsSERegisterInfo::eliminateFI(MachineBasicBlock::iterator II,
136 unsigned OpNo, int FrameIndex,
137 uint64_t StackSize,
138 int64_t SPOffset) const {
139 MachineInstr &MI = *II;
140 MachineFunction &MF = *MI.getParent()->getParent();
141 MachineFrameInfo &MFI = MF.getFrameInfo();
142 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
143
144 MipsABIInfo ABI = MF.getSubtarget<MipsSubtarget>().getABI();
145 const MipsRegisterInfo *RegInfo =
146 static_cast<const MipsRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
147
148 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
149 int MinCSFI = 0;
150 int MaxCSFI = -1;
151
152 if (CSI.size()) {
153 MinCSFI = CSI[0].getFrameIdx();
154 MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
155 }
156
157 bool EhDataRegFI = MipsFI->isEhDataRegFI(FrameIndex);
158 bool IsISRRegFI = MipsFI->isISRRegFI(FrameIndex);
159 // The following stack frame objects are always referenced relative to $sp:
160 // 1. Outgoing arguments.
161 // 2. Pointer to dynamically allocated stack space.
162 // 3. Locations for callee-saved registers.
163 // 4. Locations for eh data registers.
164 // 5. Locations for ISR saved Coprocessor 0 registers 12 & 14.
165 // Everything else is referenced relative to whatever register
166 // getFrameRegister() returns.
167 unsigned FrameReg;
168
169 if ((FrameIndex >= MinCSFI && FrameIndex <= MaxCSFI) || EhDataRegFI ||
170 IsISRRegFI)
171 FrameReg = ABI.GetStackPtr();
172 else if (RegInfo->hasStackRealignment(MF)) {
173 if (MFI.hasVarSizedObjects() && !MFI.isFixedObjectIndex(FrameIndex))
174 FrameReg = ABI.GetBasePtr();
175 else if (MFI.isFixedObjectIndex(FrameIndex))
176 FrameReg = getFrameRegister(MF);
177 else
178 FrameReg = ABI.GetStackPtr();
179 } else
180 FrameReg = getFrameRegister(MF);
181
182 // Calculate final offset.
183 // - There is no need to change the offset if the frame object is one of the
184 // following: an outgoing argument, pointer to a dynamically allocated
185 // stack space or a $gp restore location,
186 // - If the frame object is any of the following, its offset must be adjusted
187 // by adding the size of the stack:
188 // incoming argument, callee-saved register location or local variable.
189 bool IsKill = false;
190 int64_t Offset;
191
192 Offset = SPOffset + (int64_t)StackSize;
193 Offset += MI.getOperand(OpNo + 1).getImm();
194
195 LLVM_DEBUG(errs() << "Offset : " << Offset << "\n"
196 << "<--------->\n");
197
198 if (!MI.isDebugValue()) {
199 // Make sure Offset fits within the field available.
200 // For MSA instructions, this is a 10-bit signed immediate (scaled by
201 // element size), otherwise it is a 16-bit signed immediate.
202 unsigned OffsetBitSize =
203 getLoadStoreOffsetSizeInBits(MF.getSubtarget<MipsSubtarget>(),
204 MI.getOpcode(), MI.getOperand(OpNo - 1));
205 const Align OffsetAlign(getLoadStoreOffsetAlign(MI.getOpcode()));
206 if (OffsetBitSize < 16 && isInt<16>(Offset) &&
207 (!isIntN(OffsetBitSize, Offset) || !isAligned(OffsetAlign, Offset))) {
208 // If we have an offset that needs to fit into a signed n-bit immediate
209 // (where n < 16) and doesn't, but does fit into 16-bits then use an ADDiu
210 MachineBasicBlock &MBB = *MI.getParent();
211 DebugLoc DL = II->getDebugLoc();
212 const TargetRegisterClass *PtrRC =
213 ABI.ArePtrs64bit() ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
214 MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo();
215 Register Reg = RegInfo.createVirtualRegister(PtrRC);
216 const MipsSEInstrInfo &TII =
217 *static_cast<const MipsSEInstrInfo *>(
218 MBB.getParent()->getSubtarget().getInstrInfo());
219 BuildMI(MBB, II, DL, TII.get(ABI.GetPtrAddiuOp()), Reg)
220 .addReg(FrameReg)
221 .addImm(Offset);
222
223 FrameReg = Reg;
224 Offset = 0;
225 IsKill = true;
226 } else if (!isInt<16>(Offset)) {
227 // Otherwise split the offset into 16-bit pieces and add it in multiple
228 // instructions.
229 MachineBasicBlock &MBB = *MI.getParent();
230 DebugLoc DL = II->getDebugLoc();
231 unsigned NewImm = 0;
232 const MipsSEInstrInfo &TII =
233 *static_cast<const MipsSEInstrInfo *>(
234 MBB.getParent()->getSubtarget().getInstrInfo());
235 unsigned Reg = TII.loadImmediate(Offset, MBB, II, DL,
236 OffsetBitSize == 16 ? &NewImm : nullptr);
237 BuildMI(MBB, II, DL, TII.get(ABI.GetPtrAdduOp()), Reg).addReg(FrameReg)
239
240 FrameReg = Reg;
241 Offset = SignExtend64<16>(NewImm);
242 IsKill = true;
243 }
244 }
245
246 MI.getOperand(OpNo).ChangeToRegister(FrameReg, false, false, IsKill);
247 MI.getOperand(OpNo + 1).ChangeToImmediate(Offset);
248}
const AArch64Subtarget & Subtarget
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:54
Register Reg
Promote Memory to Register
Definition Mem2Reg.cpp:110
static unsigned getLoadStoreOffsetAlign(const unsigned Opcode)
Get the scale factor applied to the immediate in the given load/store.
static unsigned getLoadStoreOffsetSizeInBits(const MipsSubtarget &Subtarget, const unsigned Opcode, const MachineOperand &MO)
Get the size of the offset supported by the given load/store/inline asm.
uint64_t IntrinsicInst * II
#define LLVM_DEBUG(...)
Definition Debug.h:119
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineInstrBundleIterator< MachineInstr > iterator
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 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.
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
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 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
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
WebAssemblyABI getABI(StringRef Name)
Parse an ABI name into the corresponding enum.
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.
bool isAligned(Align Lhs, uint64_t SizeInBytes)
Checks that SizeInBytes is a multiple of the alignment.
Definition Alignment.h:139
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
constexpr bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition MathExtras.h:249
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
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58