1 //===-- MipsSERegisterInfo.cpp - MIPS32/64 Register Information -== -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the MIPS32/64 implementation of the TargetRegisterInfo
13 //===----------------------------------------------------------------------===//
15 #include "MipsSERegisterInfo.h"
17 #include "MipsAnalyzeImmediate.h"
18 #include "MipsMachineFunction.h"
19 #include "MipsSEInstrInfo.h"
20 #include "MipsSubtarget.h"
21 #include "MipsTargetMachine.h"
22 #include "llvm/ADT/BitVector.h"
23 #include "llvm/ADT/STLExtras.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/DebugInfo.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/Type.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include "llvm/Target/TargetFrameLowering.h"
37 #include "llvm/Target/TargetInstrInfo.h"
38 #include "llvm/Target/TargetMachine.h"
39 #include "llvm/Target/TargetOptions.h"
43 #define DEBUG_TYPE "mips-reg-info"
45 MipsSERegisterInfo::MipsSERegisterInfo() : MipsRegisterInfo() {}
47 bool MipsSERegisterInfo::
48 requiresRegisterScavenging(const MachineFunction &MF) const {
52 bool MipsSERegisterInfo::
53 requiresFrameIndexScavenging(const MachineFunction &MF) const {
57 const TargetRegisterClass *
58 MipsSERegisterInfo::intRegClass(unsigned Size) const {
60 return &Mips::GPR32RegClass;
63 return &Mips::GPR64RegClass;
66 /// Get the size of the offset supported by the given load/store.
67 /// The result includes the effects of any scale factors applied to the
68 /// instruction immediate.
69 static inline unsigned getLoadStoreOffsetSizeInBits(const unsigned Opcode) {
76 return 10 + 1 /* scale factor */;
79 return 10 + 2 /* scale factor */;
82 return 10 + 3 /* scale factor */;
88 /// Get the scale factor applied to the immediate in the given load/store.
89 static inline unsigned getLoadStoreOffsetAlign(const unsigned Opcode) {
105 void MipsSERegisterInfo::eliminateFI(MachineBasicBlock::iterator II,
106 unsigned OpNo, int FrameIndex,
108 int64_t SPOffset) const {
109 MachineInstr &MI = *II;
110 MachineFunction &MF = *MI.getParent()->getParent();
111 MachineFrameInfo *MFI = MF.getFrameInfo();
112 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
115 static_cast<const MipsTargetMachine &>(MF.getTarget()).getABI();
116 const MipsRegisterInfo *RegInfo =
117 static_cast<const MipsRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
119 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
124 MinCSFI = CSI[0].getFrameIdx();
125 MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
128 bool EhDataRegFI = MipsFI->isEhDataRegFI(FrameIndex);
130 // The following stack frame objects are always referenced relative to $sp:
131 // 1. Outgoing arguments.
132 // 2. Pointer to dynamically allocated stack space.
133 // 3. Locations for callee-saved registers.
134 // 4. Locations for eh data registers.
135 // Everything else is referenced relative to whatever register
136 // getFrameRegister() returns.
139 if ((FrameIndex >= MinCSFI && FrameIndex <= MaxCSFI) || EhDataRegFI)
140 FrameReg = ABI.GetStackPtr();
141 else if (RegInfo->needsStackRealignment(MF)) {
142 if (MFI->hasVarSizedObjects() && !MFI->isFixedObjectIndex(FrameIndex))
143 FrameReg = ABI.GetBasePtr();
144 else if (MFI->isFixedObjectIndex(FrameIndex))
145 FrameReg = getFrameRegister(MF);
147 FrameReg = ABI.GetStackPtr();
149 FrameReg = getFrameRegister(MF);
151 // Calculate final offset.
152 // - There is no need to change the offset if the frame object is one of the
153 // following: an outgoing argument, pointer to a dynamically allocated
154 // stack space or a $gp restore location,
155 // - If the frame object is any of the following, its offset must be adjusted
156 // by adding the size of the stack:
157 // incoming argument, callee-saved register location or local variable.
161 Offset = SPOffset + (int64_t)StackSize;
162 Offset += MI.getOperand(OpNo + 1).getImm();
164 DEBUG(errs() << "Offset : " << Offset << "\n" << "<--------->\n");
166 if (!MI.isDebugValue()) {
167 // Make sure Offset fits within the field available.
168 // For MSA instructions, this is a 10-bit signed immediate (scaled by
169 // element size), otherwise it is a 16-bit signed immediate.
170 unsigned OffsetBitSize = getLoadStoreOffsetSizeInBits(MI.getOpcode());
171 unsigned OffsetAlign = getLoadStoreOffsetAlign(MI.getOpcode());
173 if (OffsetBitSize < 16 && isInt<16>(Offset) &&
174 (!isIntN(OffsetBitSize, Offset) ||
175 OffsetToAlignment(Offset, OffsetAlign) != 0)) {
176 // If we have an offset that needs to fit into a signed n-bit immediate
177 // (where n < 16) and doesn't, but does fit into 16-bits then use an ADDiu
178 MachineBasicBlock &MBB = *MI.getParent();
179 DebugLoc DL = II->getDebugLoc();
180 const TargetRegisterClass *PtrRC =
181 ABI.ArePtrs64bit() ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
182 MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo();
183 unsigned Reg = RegInfo.createVirtualRegister(PtrRC);
184 const MipsSEInstrInfo &TII =
185 *static_cast<const MipsSEInstrInfo *>(
186 MBB.getParent()->getSubtarget().getInstrInfo());
187 BuildMI(MBB, II, DL, TII.get(ABI.GetPtrAddiuOp()), Reg)
194 } else if (!isInt<16>(Offset)) {
195 // Otherwise split the offset into 16-bit pieces and add it in multiple
197 MachineBasicBlock &MBB = *MI.getParent();
198 DebugLoc DL = II->getDebugLoc();
200 const MipsSEInstrInfo &TII =
201 *static_cast<const MipsSEInstrInfo *>(
202 MBB.getParent()->getSubtarget().getInstrInfo());
203 unsigned Reg = TII.loadImmediate(Offset, MBB, II, DL,
204 OffsetBitSize == 16 ? &NewImm : nullptr);
205 BuildMI(MBB, II, DL, TII.get(ABI.GetPtrAdduOp()), Reg).addReg(FrameReg)
206 .addReg(Reg, RegState::Kill);
209 Offset = SignExtend64<16>(NewImm);
214 MI.getOperand(OpNo).ChangeToRegister(FrameReg, false, false, IsKill);
215 MI.getOperand(OpNo + 1).ChangeToImmediate(Offset);