1 //===- MipsRegisterInfo.cpp - MIPS Register Information -== -----*- C++ -*-===//
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 MIPS implementation of the TargetRegisterInfo class.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "mips-reg-info"
17 #include "MipsSubtarget.h"
18 #include "MipsRegisterInfo.h"
19 #include "MipsMachineFunction.h"
20 #include "llvm/Constants.h"
21 #include "llvm/Type.h"
22 #include "llvm/Function.h"
23 #include "llvm/CodeGen/ValueTypes.h"
24 #include "llvm/CodeGen/MachineInstrBuilder.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/Target/TargetFrameLowering.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Target/TargetOptions.h"
30 #include "llvm/Target/TargetInstrInfo.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/ADT/BitVector.h"
36 #include "llvm/ADT/STLExtras.h"
37 #include "llvm/Analysis/DebugInfo.h"
39 #define GET_REGINFO_TARGET_DESC
40 #include "MipsGenRegisterInfo.inc"
44 MipsRegisterInfo::MipsRegisterInfo(const MipsSubtarget &ST,
45 const TargetInstrInfo &tii)
46 : MipsGenRegisterInfo(Mips::RA), Subtarget(ST), TII(tii) {}
48 /// getRegisterNumbering - Given the enum value for some register, e.g.
49 /// Mips::RA, return the number that it corresponds to (e.g. 31).
50 unsigned MipsRegisterInfo::
51 getRegisterNumbering(unsigned RegEnum)
54 case Mips::ZERO : case Mips::F0 : case Mips::D0 : return 0;
55 case Mips::AT : case Mips::F1 : return 1;
56 case Mips::V0 : case Mips::F2 : case Mips::D1 : return 2;
57 case Mips::V1 : case Mips::F3 : return 3;
58 case Mips::A0 : case Mips::F4 : case Mips::D2 : return 4;
59 case Mips::A1 : case Mips::F5 : return 5;
60 case Mips::A2 : case Mips::F6 : case Mips::D3 : return 6;
61 case Mips::A3 : case Mips::F7 : return 7;
62 case Mips::T0 : case Mips::F8 : case Mips::D4 : return 8;
63 case Mips::T1 : case Mips::F9 : return 9;
64 case Mips::T2 : case Mips::F10: case Mips::D5: return 10;
65 case Mips::T3 : case Mips::F11: return 11;
66 case Mips::T4 : case Mips::F12: case Mips::D6: return 12;
67 case Mips::T5 : case Mips::F13: return 13;
68 case Mips::T6 : case Mips::F14: case Mips::D7: return 14;
69 case Mips::T7 : case Mips::F15: return 15;
70 case Mips::S0 : case Mips::F16: case Mips::D8: return 16;
71 case Mips::S1 : case Mips::F17: return 17;
72 case Mips::S2 : case Mips::F18: case Mips::D9: return 18;
73 case Mips::S3 : case Mips::F19: return 19;
74 case Mips::S4 : case Mips::F20: case Mips::D10: return 20;
75 case Mips::S5 : case Mips::F21: return 21;
76 case Mips::S6 : case Mips::F22: case Mips::D11: return 22;
77 case Mips::S7 : case Mips::F23: return 23;
78 case Mips::T8 : case Mips::F24: case Mips::D12: return 24;
79 case Mips::T9 : case Mips::F25: return 25;
80 case Mips::K0 : case Mips::F26: case Mips::D13: return 26;
81 case Mips::K1 : case Mips::F27: return 27;
82 case Mips::GP : case Mips::F28: case Mips::D14: return 28;
83 case Mips::SP : case Mips::F29: return 29;
84 case Mips::FP : case Mips::F30: case Mips::D15: return 30;
85 case Mips::RA : case Mips::F31: return 31;
86 default: llvm_unreachable("Unknown register number!");
88 return 0; // Not reached
91 unsigned MipsRegisterInfo::getPICCallReg() { return Mips::T9; }
93 //===----------------------------------------------------------------------===//
94 // Callee Saved Registers methods
95 //===----------------------------------------------------------------------===//
97 /// Mips Callee Saved Registers
98 const unsigned* MipsRegisterInfo::
99 getCalleeSavedRegs(const MachineFunction *MF) const
101 // Mips callee-save register range is $16-$23, $f20-$f30
102 static const unsigned SingleFloatOnlyCalleeSavedRegs[] = {
103 Mips::F30, Mips::F29, Mips::F28, Mips::F27, Mips::F26,
104 Mips::F25, Mips::F24, Mips::F23, Mips::F22, Mips::F21, Mips::F20,
105 Mips::RA, Mips::FP, Mips::S7, Mips::S6, Mips::S5, Mips::S4,
106 Mips::S3, Mips::S2, Mips::S1, Mips::S0, 0
109 static const unsigned Mips32CalleeSavedRegs[] = {
110 Mips::D15, Mips::D14, Mips::D13, Mips::D12, Mips::D11, Mips::D10,
111 Mips::RA, Mips::FP, Mips::S7, Mips::S6, Mips::S5, Mips::S4,
112 Mips::S3, Mips::S2, Mips::S1, Mips::S0, 0
115 if (Subtarget.isSingleFloat())
116 return SingleFloatOnlyCalleeSavedRegs;
118 return Mips32CalleeSavedRegs;
121 BitVector MipsRegisterInfo::
122 getReservedRegs(const MachineFunction &MF) const {
123 BitVector Reserved(getNumRegs());
124 Reserved.set(Mips::ZERO);
125 Reserved.set(Mips::AT);
126 Reserved.set(Mips::K0);
127 Reserved.set(Mips::K1);
128 Reserved.set(Mips::GP);
129 Reserved.set(Mips::SP);
130 Reserved.set(Mips::FP);
131 Reserved.set(Mips::RA);
132 Reserved.set(Mips::F31);
133 Reserved.set(Mips::D15);
135 // SRV4 requires that odd register can't be used.
136 if (!Subtarget.isSingleFloat() && !Subtarget.isMips32())
137 for (unsigned FReg=(Mips::F0)+1; FReg < Mips::F30; FReg+=2)
143 // This function eliminate ADJCALLSTACKDOWN,
144 // ADJCALLSTACKUP pseudo instructions
145 void MipsRegisterInfo::
146 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
147 MachineBasicBlock::iterator I) const {
148 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
152 // FrameIndex represent objects inside a abstract stack.
153 // We must replace FrameIndex with an stack/frame pointer
155 void MipsRegisterInfo::
156 eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj,
157 RegScavenger *RS) const {
158 MachineInstr &MI = *II;
159 MachineFunction &MF = *MI.getParent()->getParent();
160 MachineFrameInfo *MFI = MF.getFrameInfo();
161 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
164 while (!MI.getOperand(i).isFI()) {
166 assert(i < MI.getNumOperands() &&
167 "Instr doesn't have FrameIndex operand!");
170 DEBUG(errs() << "\nFunction : " << MF.getFunction()->getName() << "\n";
171 errs() << "<--------->\n" << MI);
173 int FrameIndex = MI.getOperand(i).getIndex();
174 int stackSize = MF.getFrameInfo()->getStackSize();
175 int spOffset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
177 DEBUG(errs() << "FrameIndex : " << FrameIndex << "\n"
178 << "spOffset : " << spOffset << "\n"
179 << "stackSize : " << stackSize << "\n");
181 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
186 MinCSFI = CSI[0].getFrameIdx();
187 MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
190 // The following stack frame objects are always referenced relative to $sp:
191 // 1. Outgoing arguments.
192 // 2. Pointer to dynamically allocated stack space.
193 // 3. Locations for callee-saved registers.
194 // Everything else is referenced relative to whatever register
195 // getFrameRegister() returns.
198 if (MipsFI->isOutArgFI(FrameIndex) || MipsFI->isDynAllocFI(FrameIndex) ||
199 (FrameIndex >= MinCSFI && FrameIndex <= MaxCSFI))
202 FrameReg = getFrameRegister(MF);
204 // Calculate final offset.
205 // - There is no need to change the offset if the frame object is one of the
206 // following: an outgoing argument, pointer to a dynamically allocated
207 // stack space or a $gp restore location,
208 // - If the frame object is any of the following, its offset must be adjusted
209 // by adding the size of the stack:
210 // incoming argument, callee-saved register location or local variable.
213 if (MipsFI->isOutArgFI(FrameIndex) || MipsFI->isGPFI(FrameIndex) ||
214 MipsFI->isDynAllocFI(FrameIndex))
217 Offset = spOffset + stackSize;
219 Offset += MI.getOperand(i+1).getImm();
221 DEBUG(errs() << "Offset : " << Offset << "\n" << "<--------->\n");
223 // If MI is not a debug value, make sure Offset fits in the 16-bit immediate
225 if (!MI.isDebugValue() && (Offset >= 0x8000 || Offset < -0x8000)) {
226 MachineBasicBlock &MBB = *MI.getParent();
227 DebugLoc DL = II->getDebugLoc();
228 int ImmHi = (((unsigned)Offset & 0xffff0000) >> 16) +
229 ((Offset & 0x8000) != 0);
231 // FIXME: change this when mips goes MC".
232 BuildMI(MBB, II, DL, TII.get(Mips::NOAT));
233 BuildMI(MBB, II, DL, TII.get(Mips::LUi), Mips::AT).addImm(ImmHi);
234 BuildMI(MBB, II, DL, TII.get(Mips::ADDu), Mips::AT).addReg(FrameReg)
237 Offset = (short)(Offset & 0xffff);
239 BuildMI(MBB, ++II, MI.getDebugLoc(), TII.get(Mips::ATMACRO));
242 MI.getOperand(i).ChangeToRegister(FrameReg, false);
243 MI.getOperand(i+1).ChangeToImmediate(Offset);
246 unsigned MipsRegisterInfo::
247 getFrameRegister(const MachineFunction &MF) const {
248 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
250 return TFI->hasFP(MF) ? Mips::FP : Mips::SP;
253 unsigned MipsRegisterInfo::
254 getEHExceptionRegister() const {
255 llvm_unreachable("What is the exception register");
259 unsigned MipsRegisterInfo::
260 getEHHandlerRegister() const {
261 llvm_unreachable("What is the exception handler register");