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