1 //===- MBlazeRegisterInfo.cpp - MBlaze 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 MBlaze implementation of the TargetRegisterInfo
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "mblaze-frame-info"
18 #include "MBlazeSubtarget.h"
19 #include "MBlazeRegisterInfo.h"
20 #include "MBlazeMachineFunction.h"
21 #include "llvm/Constants.h"
22 #include "llvm/Type.h"
23 #include "llvm/Function.h"
24 #include "llvm/CodeGen/ValueTypes.h"
25 #include "llvm/CodeGen/MachineInstrBuilder.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineLocation.h"
29 #include "llvm/Target/TargetFrameLowering.h"
30 #include "llvm/Target/TargetMachine.h"
31 #include "llvm/Target/TargetOptions.h"
32 #include "llvm/Target/TargetInstrInfo.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/ADT/BitVector.h"
38 #include "llvm/ADT/STLExtras.h"
40 #define GET_REGINFO_TARGET_DESC
41 #include "MBlazeGenRegisterInfo.inc"
46 MBlazeRegisterInfo(const MBlazeSubtarget &ST, const TargetInstrInfo &tii)
47 : MBlazeGenRegisterInfo(MBlaze::R15), Subtarget(ST), TII(tii) {}
49 /// getRegisterNumbering - Given the enum value for some register, e.g.
50 /// MBlaze::R0, return the number that it corresponds to (e.g. 0).
51 unsigned MBlazeRegisterInfo::getRegisterNumbering(unsigned RegEnum) {
53 case MBlaze::R0 : return 0;
54 case MBlaze::R1 : return 1;
55 case MBlaze::R2 : return 2;
56 case MBlaze::R3 : return 3;
57 case MBlaze::R4 : return 4;
58 case MBlaze::R5 : return 5;
59 case MBlaze::R6 : return 6;
60 case MBlaze::R7 : return 7;
61 case MBlaze::R8 : return 8;
62 case MBlaze::R9 : return 9;
63 case MBlaze::R10 : return 10;
64 case MBlaze::R11 : return 11;
65 case MBlaze::R12 : return 12;
66 case MBlaze::R13 : return 13;
67 case MBlaze::R14 : return 14;
68 case MBlaze::R15 : return 15;
69 case MBlaze::R16 : return 16;
70 case MBlaze::R17 : return 17;
71 case MBlaze::R18 : return 18;
72 case MBlaze::R19 : return 19;
73 case MBlaze::R20 : return 20;
74 case MBlaze::R21 : return 21;
75 case MBlaze::R22 : return 22;
76 case MBlaze::R23 : return 23;
77 case MBlaze::R24 : return 24;
78 case MBlaze::R25 : return 25;
79 case MBlaze::R26 : return 26;
80 case MBlaze::R27 : return 27;
81 case MBlaze::R28 : return 28;
82 case MBlaze::R29 : return 29;
83 case MBlaze::R30 : return 30;
84 case MBlaze::R31 : return 31;
85 case MBlaze::RPC : return 0x0000;
86 case MBlaze::RMSR : return 0x0001;
87 case MBlaze::REAR : return 0x0003;
88 case MBlaze::RESR : return 0x0005;
89 case MBlaze::RFSR : return 0x0007;
90 case MBlaze::RBTR : return 0x000B;
91 case MBlaze::REDR : return 0x000D;
92 case MBlaze::RPID : return 0x1000;
93 case MBlaze::RZPR : return 0x1001;
94 case MBlaze::RTLBX : return 0x1002;
95 case MBlaze::RTLBLO : return 0x1003;
96 case MBlaze::RTLBHI : return 0x1004;
97 case MBlaze::RPVR0 : return 0x2000;
98 case MBlaze::RPVR1 : return 0x2001;
99 case MBlaze::RPVR2 : return 0x2002;
100 case MBlaze::RPVR3 : return 0x2003;
101 case MBlaze::RPVR4 : return 0x2004;
102 case MBlaze::RPVR5 : return 0x2005;
103 case MBlaze::RPVR6 : return 0x2006;
104 case MBlaze::RPVR7 : return 0x2007;
105 case MBlaze::RPVR8 : return 0x2008;
106 case MBlaze::RPVR9 : return 0x2009;
107 case MBlaze::RPVR10 : return 0x200A;
108 case MBlaze::RPVR11 : return 0x200B;
109 default: llvm_unreachable("Unknown register number!");
111 return 0; // Not reached
114 /// getRegisterFromNumbering - Given the enum value for some register, e.g.
115 /// MBlaze::R0, return the number that it corresponds to (e.g. 0).
116 unsigned MBlazeRegisterInfo::getRegisterFromNumbering(unsigned Reg) {
118 case 0 : return MBlaze::R0;
119 case 1 : return MBlaze::R1;
120 case 2 : return MBlaze::R2;
121 case 3 : return MBlaze::R3;
122 case 4 : return MBlaze::R4;
123 case 5 : return MBlaze::R5;
124 case 6 : return MBlaze::R6;
125 case 7 : return MBlaze::R7;
126 case 8 : return MBlaze::R8;
127 case 9 : return MBlaze::R9;
128 case 10 : return MBlaze::R10;
129 case 11 : return MBlaze::R11;
130 case 12 : return MBlaze::R12;
131 case 13 : return MBlaze::R13;
132 case 14 : return MBlaze::R14;
133 case 15 : return MBlaze::R15;
134 case 16 : return MBlaze::R16;
135 case 17 : return MBlaze::R17;
136 case 18 : return MBlaze::R18;
137 case 19 : return MBlaze::R19;
138 case 20 : return MBlaze::R20;
139 case 21 : return MBlaze::R21;
140 case 22 : return MBlaze::R22;
141 case 23 : return MBlaze::R23;
142 case 24 : return MBlaze::R24;
143 case 25 : return MBlaze::R25;
144 case 26 : return MBlaze::R26;
145 case 27 : return MBlaze::R27;
146 case 28 : return MBlaze::R28;
147 case 29 : return MBlaze::R29;
148 case 30 : return MBlaze::R30;
149 case 31 : return MBlaze::R31;
150 default: llvm_unreachable("Unknown register number!");
152 return 0; // Not reached
155 unsigned MBlazeRegisterInfo::getSpecialRegisterFromNumbering(unsigned Reg) {
157 case 0x0000 : return MBlaze::RPC;
158 case 0x0001 : return MBlaze::RMSR;
159 case 0x0003 : return MBlaze::REAR;
160 case 0x0005 : return MBlaze::RESR;
161 case 0x0007 : return MBlaze::RFSR;
162 case 0x000B : return MBlaze::RBTR;
163 case 0x000D : return MBlaze::REDR;
164 case 0x1000 : return MBlaze::RPID;
165 case 0x1001 : return MBlaze::RZPR;
166 case 0x1002 : return MBlaze::RTLBX;
167 case 0x1003 : return MBlaze::RTLBLO;
168 case 0x1004 : return MBlaze::RTLBHI;
169 case 0x2000 : return MBlaze::RPVR0;
170 case 0x2001 : return MBlaze::RPVR1;
171 case 0x2002 : return MBlaze::RPVR2;
172 case 0x2003 : return MBlaze::RPVR3;
173 case 0x2004 : return MBlaze::RPVR4;
174 case 0x2005 : return MBlaze::RPVR5;
175 case 0x2006 : return MBlaze::RPVR6;
176 case 0x2007 : return MBlaze::RPVR7;
177 case 0x2008 : return MBlaze::RPVR8;
178 case 0x2009 : return MBlaze::RPVR9;
179 case 0x200A : return MBlaze::RPVR10;
180 case 0x200B : return MBlaze::RPVR11;
181 default: llvm_unreachable("Unknown register number!");
183 return 0; // Not reached
186 bool MBlazeRegisterInfo::isRegister(unsigned Reg) {
190 bool MBlazeRegisterInfo::isSpecialRegister(unsigned Reg) {
192 case 0x0000 : case 0x0001 : case 0x0003 : case 0x0005 :
193 case 0x0007 : case 0x000B : case 0x000D : case 0x1000 :
194 case 0x1001 : case 0x1002 : case 0x1003 : case 0x1004 :
195 case 0x2000 : case 0x2001 : case 0x2002 : case 0x2003 :
196 case 0x2004 : case 0x2005 : case 0x2006 : case 0x2007 :
197 case 0x2008 : case 0x2009 : case 0x200A : case 0x200B :
203 return false; // Not reached
206 unsigned MBlazeRegisterInfo::getPICCallReg() {
210 //===----------------------------------------------------------------------===//
211 // Callee Saved Registers methods
212 //===----------------------------------------------------------------------===//
214 /// MBlaze Callee Saved Registers
215 const unsigned* MBlazeRegisterInfo::
216 getCalleeSavedRegs(const MachineFunction *MF) const {
217 // MBlaze callee-save register range is R20 - R31
218 static const unsigned CalleeSavedRegs[] = {
219 MBlaze::R20, MBlaze::R21, MBlaze::R22, MBlaze::R23,
220 MBlaze::R24, MBlaze::R25, MBlaze::R26, MBlaze::R27,
221 MBlaze::R28, MBlaze::R29, MBlaze::R30, MBlaze::R31,
225 return CalleeSavedRegs;
228 BitVector MBlazeRegisterInfo::
229 getReservedRegs(const MachineFunction &MF) const {
230 BitVector Reserved(getNumRegs());
231 Reserved.set(MBlaze::R0);
232 Reserved.set(MBlaze::R1);
233 Reserved.set(MBlaze::R2);
234 Reserved.set(MBlaze::R13);
235 Reserved.set(MBlaze::R14);
236 Reserved.set(MBlaze::R15);
237 Reserved.set(MBlaze::R16);
238 Reserved.set(MBlaze::R17);
239 Reserved.set(MBlaze::R18);
240 Reserved.set(MBlaze::R19);
244 // This function eliminate ADJCALLSTACKDOWN/ADJCALLSTACKUP pseudo instructions
245 void MBlazeRegisterInfo::
246 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
247 MachineBasicBlock::iterator I) const {
248 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
250 if (!TFI->hasReservedCallFrame(MF)) {
251 // If we have a frame pointer, turn the adjcallstackup instruction into a
252 // 'addi r1, r1, -<amt>' and the adjcallstackdown instruction into
253 // 'addi r1, r1, <amt>'
254 MachineInstr *Old = I;
255 int Amount = Old->getOperand(0).getImm() + 4;
257 // We need to keep the stack aligned properly. To do this, we round the
258 // amount of space needed for the outgoing arguments up to the next
259 // alignment boundary.
260 unsigned Align = TFI->getStackAlignment();
261 Amount = (Amount+Align-1)/Align*Align;
264 if (Old->getOpcode() == MBlaze::ADJCALLSTACKDOWN) {
265 New = BuildMI(MF,Old->getDebugLoc(),TII.get(MBlaze::ADDIK),MBlaze::R1)
266 .addReg(MBlaze::R1).addImm(-Amount);
268 assert(Old->getOpcode() == MBlaze::ADJCALLSTACKUP);
269 New = BuildMI(MF,Old->getDebugLoc(),TII.get(MBlaze::ADDIK),MBlaze::R1)
270 .addReg(MBlaze::R1).addImm(Amount);
273 // Replace the pseudo instruction with a new instruction...
278 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
282 // FrameIndex represent objects inside a abstract stack.
283 // We must replace FrameIndex with an stack/frame pointer
285 void MBlazeRegisterInfo::
286 eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj,
287 RegScavenger *RS) const {
288 MachineInstr &MI = *II;
289 MachineFunction &MF = *MI.getParent()->getParent();
290 MachineFrameInfo *MFI = MF.getFrameInfo();
293 while (!MI.getOperand(i).isFI()) {
295 assert(i < MI.getNumOperands() &&
296 "Instr doesn't have FrameIndex operand!");
299 unsigned oi = i == 2 ? 1 : 2;
301 DEBUG(dbgs() << "\nFunction : " << MF.getFunction()->getName() << "\n";
302 dbgs() << "<--------->\n" << MI);
304 int FrameIndex = MI.getOperand(i).getIndex();
305 int stackSize = MFI->getStackSize();
306 int spOffset = MFI->getObjectOffset(FrameIndex);
308 DEBUG(MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
309 dbgs() << "FrameIndex : " << FrameIndex << "\n"
310 << "spOffset : " << spOffset << "\n"
311 << "stackSize : " << stackSize << "\n"
312 << "isFixed : " << MFI->isFixedObjectIndex(FrameIndex) << "\n"
313 << "isLiveIn : " << MBlazeFI->isLiveIn(FrameIndex) << "\n"
314 << "isSpill : " << MFI->isSpillSlotObjectIndex(FrameIndex)
317 // as explained on LowerFormalArguments, detect negative offsets
318 // and adjust SPOffsets considering the final stack size.
319 int Offset = (spOffset < 0) ? (stackSize - spOffset) : spOffset;
320 Offset += MI.getOperand(oi).getImm();
322 DEBUG(dbgs() << "Offset : " << Offset << "\n" << "<--------->\n");
324 MI.getOperand(oi).ChangeToImmediate(Offset);
325 MI.getOperand(i).ChangeToRegister(getFrameRegister(MF), false);
328 void MBlazeRegisterInfo::
329 processFunctionBeforeFrameFinalized(MachineFunction &MF) const {
330 // Set the stack offset where GP must be saved/loaded from.
331 MachineFrameInfo *MFI = MF.getFrameInfo();
332 MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
333 if (MBlazeFI->needGPSaveRestore())
334 MFI->setObjectOffset(MBlazeFI->getGPFI(), MBlazeFI->getGPStackOffset());
337 unsigned MBlazeRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
338 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
340 return TFI->hasFP(MF) ? MBlaze::R19 : MBlaze::R1;
343 unsigned MBlazeRegisterInfo::getEHExceptionRegister() const {
344 llvm_unreachable("What is the exception register");
348 unsigned MBlazeRegisterInfo::getEHHandlerRegister() const {
349 llvm_unreachable("What is the exception handler register");