Merge XXXGenRegisterDesc.inc XXXGenRegisterNames.inc XXXGenRegisterInfo.h.inc
[oota-llvm.git] / lib / Target / MBlaze / MBlazeRegisterInfo.cpp
1 //===- MBlazeRegisterInfo.cpp - MBlaze Register Information -== -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the MBlaze implementation of the TargetRegisterInfo
11 // class.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "mblaze-frame-info"
16
17 #include "MBlaze.h"
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"
39
40 #define GET_REGINFO_MC_DESC
41 #define GET_REGINFO_TARGET_DESC
42 #include "MBlazeGenRegisterInfo.inc"
43
44 using namespace llvm;
45
46 MBlazeRegisterInfo::
47 MBlazeRegisterInfo(const MBlazeSubtarget &ST, const TargetInstrInfo &tii)
48   : MBlazeGenRegisterInfo(MBlazeRegDesc, MBlazeRegInfoDesc,
49                           MBlaze::ADJCALLSTACKDOWN, MBlaze::ADJCALLSTACKUP),
50     Subtarget(ST), TII(tii) {}
51
52 /// getRegisterNumbering - Given the enum value for some register, e.g.
53 /// MBlaze::R0, return the number that it corresponds to (e.g. 0).
54 unsigned MBlazeRegisterInfo::getRegisterNumbering(unsigned RegEnum) {
55   switch (RegEnum) {
56     case MBlaze::R0     : return 0;
57     case MBlaze::R1     : return 1;
58     case MBlaze::R2     : return 2;
59     case MBlaze::R3     : return 3;
60     case MBlaze::R4     : return 4;
61     case MBlaze::R5     : return 5;
62     case MBlaze::R6     : return 6;
63     case MBlaze::R7     : return 7;
64     case MBlaze::R8     : return 8;
65     case MBlaze::R9     : return 9;
66     case MBlaze::R10    : return 10;
67     case MBlaze::R11    : return 11;
68     case MBlaze::R12    : return 12;
69     case MBlaze::R13    : return 13;
70     case MBlaze::R14    : return 14;
71     case MBlaze::R15    : return 15;
72     case MBlaze::R16    : return 16;
73     case MBlaze::R17    : return 17;
74     case MBlaze::R18    : return 18;
75     case MBlaze::R19    : return 19;
76     case MBlaze::R20    : return 20;
77     case MBlaze::R21    : return 21;
78     case MBlaze::R22    : return 22;
79     case MBlaze::R23    : return 23;
80     case MBlaze::R24    : return 24;
81     case MBlaze::R25    : return 25;
82     case MBlaze::R26    : return 26;
83     case MBlaze::R27    : return 27;
84     case MBlaze::R28    : return 28;
85     case MBlaze::R29    : return 29;
86     case MBlaze::R30    : return 30;
87     case MBlaze::R31    : return 31;
88     case MBlaze::RPC    : return 0x0000;
89     case MBlaze::RMSR   : return 0x0001;
90     case MBlaze::REAR   : return 0x0003;
91     case MBlaze::RESR   : return 0x0005;
92     case MBlaze::RFSR   : return 0x0007;
93     case MBlaze::RBTR   : return 0x000B;
94     case MBlaze::REDR   : return 0x000D;
95     case MBlaze::RPID   : return 0x1000;
96     case MBlaze::RZPR   : return 0x1001;
97     case MBlaze::RTLBX  : return 0x1002;
98     case MBlaze::RTLBLO : return 0x1003;
99     case MBlaze::RTLBHI : return 0x1004;
100     case MBlaze::RPVR0  : return 0x2000;
101     case MBlaze::RPVR1  : return 0x2001;
102     case MBlaze::RPVR2  : return 0x2002;
103     case MBlaze::RPVR3  : return 0x2003;
104     case MBlaze::RPVR4  : return 0x2004;
105     case MBlaze::RPVR5  : return 0x2005;
106     case MBlaze::RPVR6  : return 0x2006;
107     case MBlaze::RPVR7  : return 0x2007;
108     case MBlaze::RPVR8  : return 0x2008;
109     case MBlaze::RPVR9  : return 0x2009;
110     case MBlaze::RPVR10 : return 0x200A;
111     case MBlaze::RPVR11 : return 0x200B;
112     default: llvm_unreachable("Unknown register number!");
113   }
114   return 0; // Not reached
115 }
116
117 /// getRegisterFromNumbering - Given the enum value for some register, e.g.
118 /// MBlaze::R0, return the number that it corresponds to (e.g. 0).
119 unsigned MBlazeRegisterInfo::getRegisterFromNumbering(unsigned Reg) {
120   switch (Reg) {
121     case 0  : return MBlaze::R0;
122     case 1  : return MBlaze::R1;
123     case 2  : return MBlaze::R2;
124     case 3  : return MBlaze::R3;
125     case 4  : return MBlaze::R4;
126     case 5  : return MBlaze::R5;
127     case 6  : return MBlaze::R6;
128     case 7  : return MBlaze::R7;
129     case 8  : return MBlaze::R8;
130     case 9  : return MBlaze::R9;
131     case 10 : return MBlaze::R10;
132     case 11 : return MBlaze::R11;
133     case 12 : return MBlaze::R12;
134     case 13 : return MBlaze::R13;
135     case 14 : return MBlaze::R14;
136     case 15 : return MBlaze::R15;
137     case 16 : return MBlaze::R16;
138     case 17 : return MBlaze::R17;
139     case 18 : return MBlaze::R18;
140     case 19 : return MBlaze::R19;
141     case 20 : return MBlaze::R20;
142     case 21 : return MBlaze::R21;
143     case 22 : return MBlaze::R22;
144     case 23 : return MBlaze::R23;
145     case 24 : return MBlaze::R24;
146     case 25 : return MBlaze::R25;
147     case 26 : return MBlaze::R26;
148     case 27 : return MBlaze::R27;
149     case 28 : return MBlaze::R28;
150     case 29 : return MBlaze::R29;
151     case 30 : return MBlaze::R30;
152     case 31 : return MBlaze::R31;
153     default: llvm_unreachable("Unknown register number!");
154   }
155   return 0; // Not reached
156 }
157
158 unsigned MBlazeRegisterInfo::getSpecialRegisterFromNumbering(unsigned Reg) {
159   switch (Reg) {
160     case 0x0000 : return MBlaze::RPC;
161     case 0x0001 : return MBlaze::RMSR;
162     case 0x0003 : return MBlaze::REAR;
163     case 0x0005 : return MBlaze::RESR;
164     case 0x0007 : return MBlaze::RFSR;
165     case 0x000B : return MBlaze::RBTR;
166     case 0x000D : return MBlaze::REDR;
167     case 0x1000 : return MBlaze::RPID;
168     case 0x1001 : return MBlaze::RZPR;
169     case 0x1002 : return MBlaze::RTLBX;
170     case 0x1003 : return MBlaze::RTLBLO;
171     case 0x1004 : return MBlaze::RTLBHI;
172     case 0x2000 : return MBlaze::RPVR0;
173     case 0x2001 : return MBlaze::RPVR1;
174     case 0x2002 : return MBlaze::RPVR2;
175     case 0x2003 : return MBlaze::RPVR3;
176     case 0x2004 : return MBlaze::RPVR4;
177     case 0x2005 : return MBlaze::RPVR5;
178     case 0x2006 : return MBlaze::RPVR6;
179     case 0x2007 : return MBlaze::RPVR7;
180     case 0x2008 : return MBlaze::RPVR8;
181     case 0x2009 : return MBlaze::RPVR9;
182     case 0x200A : return MBlaze::RPVR10;
183     case 0x200B : return MBlaze::RPVR11;
184     default: llvm_unreachable("Unknown register number!");
185   }
186   return 0; // Not reached
187 }
188
189 bool MBlazeRegisterInfo::isRegister(unsigned Reg) {
190   return Reg <= 31;
191 }
192
193 bool MBlazeRegisterInfo::isSpecialRegister(unsigned Reg) {
194   switch (Reg) {
195     case 0x0000 : case 0x0001 : case 0x0003 : case 0x0005 : 
196     case 0x0007 : case 0x000B : case 0x000D : case 0x1000 : 
197     case 0x1001 : case 0x1002 : case 0x1003 : case 0x1004 : 
198     case 0x2000 : case 0x2001 : case 0x2002 : case 0x2003 : 
199     case 0x2004 : case 0x2005 : case 0x2006 : case 0x2007 : 
200     case 0x2008 : case 0x2009 : case 0x200A : case 0x200B : 
201       return true;
202
203     default:
204       return false;
205   }
206   return false; // Not reached
207 }
208
209 unsigned MBlazeRegisterInfo::getPICCallReg() {
210   return MBlaze::R20;
211 }
212
213 //===----------------------------------------------------------------------===//
214 // Callee Saved Registers methods
215 //===----------------------------------------------------------------------===//
216
217 /// MBlaze Callee Saved Registers
218 const unsigned* MBlazeRegisterInfo::
219 getCalleeSavedRegs(const MachineFunction *MF) const {
220   // MBlaze callee-save register range is R20 - R31
221   static const unsigned CalleeSavedRegs[] = {
222     MBlaze::R20, MBlaze::R21, MBlaze::R22, MBlaze::R23,
223     MBlaze::R24, MBlaze::R25, MBlaze::R26, MBlaze::R27,
224     MBlaze::R28, MBlaze::R29, MBlaze::R30, MBlaze::R31,
225     0
226   };
227
228   return CalleeSavedRegs;
229 }
230
231 BitVector MBlazeRegisterInfo::
232 getReservedRegs(const MachineFunction &MF) const {
233   BitVector Reserved(getNumRegs());
234   Reserved.set(MBlaze::R0);
235   Reserved.set(MBlaze::R1);
236   Reserved.set(MBlaze::R2);
237   Reserved.set(MBlaze::R13);
238   Reserved.set(MBlaze::R14);
239   Reserved.set(MBlaze::R15);
240   Reserved.set(MBlaze::R16);
241   Reserved.set(MBlaze::R17);
242   Reserved.set(MBlaze::R18);
243   Reserved.set(MBlaze::R19);
244   return Reserved;
245 }
246
247 // This function eliminate ADJCALLSTACKDOWN/ADJCALLSTACKUP pseudo instructions
248 void MBlazeRegisterInfo::
249 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
250                               MachineBasicBlock::iterator I) const {
251   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
252
253   if (!TFI->hasReservedCallFrame(MF)) {
254     // If we have a frame pointer, turn the adjcallstackup instruction into a
255     // 'addi r1, r1, -<amt>' and the adjcallstackdown instruction into
256     // 'addi r1, r1, <amt>'
257     MachineInstr *Old = I;
258     int Amount = Old->getOperand(0).getImm() + 4;
259     if (Amount != 0) {
260       // We need to keep the stack aligned properly.  To do this, we round the
261       // amount of space needed for the outgoing arguments up to the next
262       // alignment boundary.
263       unsigned Align = TFI->getStackAlignment();
264       Amount = (Amount+Align-1)/Align*Align;
265
266       MachineInstr *New;
267       if (Old->getOpcode() == MBlaze::ADJCALLSTACKDOWN) {
268         New = BuildMI(MF,Old->getDebugLoc(),TII.get(MBlaze::ADDIK),MBlaze::R1)
269                 .addReg(MBlaze::R1).addImm(-Amount);
270       } else {
271         assert(Old->getOpcode() == MBlaze::ADJCALLSTACKUP);
272         New = BuildMI(MF,Old->getDebugLoc(),TII.get(MBlaze::ADDIK),MBlaze::R1)
273                 .addReg(MBlaze::R1).addImm(Amount);
274       }
275
276       // Replace the pseudo instruction with a new instruction...
277       MBB.insert(I, New);
278     }
279   }
280
281   // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
282   MBB.erase(I);
283 }
284
285 // FrameIndex represent objects inside a abstract stack.
286 // We must replace FrameIndex with an stack/frame pointer
287 // direct reference.
288 void MBlazeRegisterInfo::
289 eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj,
290                     RegScavenger *RS) const {
291   MachineInstr &MI = *II;
292   MachineFunction &MF = *MI.getParent()->getParent();
293   MachineFrameInfo *MFI = MF.getFrameInfo();
294
295   unsigned i = 0;
296   while (!MI.getOperand(i).isFI()) {
297     ++i;
298     assert(i < MI.getNumOperands() &&
299            "Instr doesn't have FrameIndex operand!");
300   }
301
302   unsigned oi = i == 2 ? 1 : 2;
303
304   DEBUG(dbgs() << "\nFunction : " << MF.getFunction()->getName() << "\n";
305         dbgs() << "<--------->\n" << MI);
306
307   int FrameIndex = MI.getOperand(i).getIndex();
308   int stackSize  = MFI->getStackSize();
309   int spOffset   = MFI->getObjectOffset(FrameIndex);
310
311   DEBUG(MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
312         dbgs() << "FrameIndex : " << FrameIndex << "\n"
313                << "spOffset   : " << spOffset << "\n"
314                << "stackSize  : " << stackSize << "\n"
315                << "isFixed    : " << MFI->isFixedObjectIndex(FrameIndex) << "\n"
316                << "isLiveIn   : " << MBlazeFI->isLiveIn(FrameIndex) << "\n"
317                << "isSpill    : " << MFI->isSpillSlotObjectIndex(FrameIndex)
318                << "\n" );
319
320   // as explained on LowerFormalArguments, detect negative offsets
321   // and adjust SPOffsets considering the final stack size.
322   int Offset = (spOffset < 0) ? (stackSize - spOffset) : spOffset;
323   Offset += MI.getOperand(oi).getImm();
324
325   DEBUG(dbgs() << "Offset     : " << Offset << "\n" << "<--------->\n");
326
327   MI.getOperand(oi).ChangeToImmediate(Offset);
328   MI.getOperand(i).ChangeToRegister(getFrameRegister(MF), false);
329 }
330
331 void MBlazeRegisterInfo::
332 processFunctionBeforeFrameFinalized(MachineFunction &MF) const {
333   // Set the stack offset where GP must be saved/loaded from.
334   MachineFrameInfo *MFI = MF.getFrameInfo();
335   MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
336   if (MBlazeFI->needGPSaveRestore())
337     MFI->setObjectOffset(MBlazeFI->getGPFI(), MBlazeFI->getGPStackOffset());
338 }
339
340 unsigned MBlazeRegisterInfo::getRARegister() const {
341   return MBlaze::R15;
342 }
343
344 unsigned MBlazeRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
345   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
346
347   return TFI->hasFP(MF) ? MBlaze::R19 : MBlaze::R1;
348 }
349
350 unsigned MBlazeRegisterInfo::getEHExceptionRegister() const {
351   llvm_unreachable("What is the exception register");
352   return 0;
353 }
354
355 unsigned MBlazeRegisterInfo::getEHHandlerRegister() const {
356   llvm_unreachable("What is the exception handler register");
357   return 0;
358 }
359
360 int MBlazeRegisterInfo::getDwarfRegNum(unsigned RegNo, bool isEH) const {
361   return MBlazeGenRegisterInfo::getDwarfRegNumFull(RegNo,0);
362 }
363
364 int MBlazeRegisterInfo::getLLVMRegNum(unsigned DwarfRegNo, bool isEH) const {
365   return MBlazeGenRegisterInfo::getLLVMRegNumFull(DwarfRegNo,0);
366 }