The ADD and ADDK (and all variants) instructions where flip-flopped in the MBlaze...
[oota-llvm.git] / lib / Target / MBlaze / MBlazeRegisterInfo.cpp
1 //===- MBlazeRegisterInfo.cpp - MBlaze Register Information -== -*- C++ -*-===//
2 //DJ
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-reg-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/TargetFrameInfo.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     default: llvm_unreachable("Unknown register number!");
84   }
85   return 0; // Not reached
86 }
87
88 /// getRegisterFromNumbering - Given the enum value for some register, e.g.
89 /// MBlaze::R0, return the number that it corresponds to (e.g. 0).
90 unsigned MBlazeRegisterInfo::getRegisterFromNumbering(unsigned Reg) {
91   switch (Reg) {
92     case 0  : return MBlaze::R0;
93     case 1  : return MBlaze::R1;
94     case 2  : return MBlaze::R2;
95     case 3  : return MBlaze::R3;
96     case 4  : return MBlaze::R4;
97     case 5  : return MBlaze::R5;
98     case 6  : return MBlaze::R6;
99     case 7  : return MBlaze::R7;
100     case 8  : return MBlaze::R8;
101     case 9  : return MBlaze::R9;
102     case 10 : return MBlaze::R10;
103     case 11 : return MBlaze::R11;
104     case 12 : return MBlaze::R12;
105     case 13 : return MBlaze::R13;
106     case 14 : return MBlaze::R14;
107     case 15 : return MBlaze::R15;
108     case 16 : return MBlaze::R16;
109     case 17 : return MBlaze::R17;
110     case 18 : return MBlaze::R18;
111     case 19 : return MBlaze::R19;
112     case 20 : return MBlaze::R20;
113     case 21 : return MBlaze::R21;
114     case 22 : return MBlaze::R22;
115     case 23 : return MBlaze::R23;
116     case 24 : return MBlaze::R24;
117     case 25 : return MBlaze::R25;
118     case 26 : return MBlaze::R26;
119     case 27 : return MBlaze::R27;
120     case 28 : return MBlaze::R28;
121     case 29 : return MBlaze::R29;
122     case 30 : return MBlaze::R30;
123     case 31 : return MBlaze::R31;
124     default: llvm_unreachable("Unknown register number!");
125   }
126   return 0; // Not reached
127 }
128
129 unsigned MBlazeRegisterInfo::getPICCallReg() {
130   return MBlaze::R20;
131 }
132
133 //===----------------------------------------------------------------------===//
134 // Callee Saved Registers methods
135 //===----------------------------------------------------------------------===//
136
137 /// MBlaze Callee Saved Registers
138 const unsigned* MBlazeRegisterInfo::
139 getCalleeSavedRegs(const MachineFunction *MF) const {
140   // MBlaze callee-save register range is R20 - R31
141   static const unsigned CalleeSavedRegs[] = {
142     MBlaze::R20, MBlaze::R21, MBlaze::R22, MBlaze::R23,
143     MBlaze::R24, MBlaze::R25, MBlaze::R26, MBlaze::R27,
144     MBlaze::R28, MBlaze::R29, MBlaze::R30, MBlaze::R31,
145     0
146   };
147
148   return CalleeSavedRegs;
149 }
150
151 BitVector MBlazeRegisterInfo::
152 getReservedRegs(const MachineFunction &MF) const {
153   BitVector Reserved(getNumRegs());
154   Reserved.set(MBlaze::R0);
155   Reserved.set(MBlaze::R1);
156   Reserved.set(MBlaze::R2);
157   Reserved.set(MBlaze::R13);
158   Reserved.set(MBlaze::R14);
159   Reserved.set(MBlaze::R15);
160   Reserved.set(MBlaze::R16);
161   Reserved.set(MBlaze::R17);
162   Reserved.set(MBlaze::R18);
163   Reserved.set(MBlaze::R19);
164   return Reserved;
165 }
166
167 // This function eliminate ADJCALLSTACKDOWN/ADJCALLSTACKUP pseudo instructions
168 void MBlazeRegisterInfo::
169 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
170                               MachineBasicBlock::iterator I) const {
171   const TargetFrameInfo *TFI = MF.getTarget().getFrameInfo();
172
173   if (!TFI->hasReservedCallFrame(MF)) {
174     // If we have a frame pointer, turn the adjcallstackup instruction into a
175     // 'addi r1, r1, -<amt>' and the adjcallstackdown instruction into
176     // 'addi r1, r1, <amt>'
177     MachineInstr *Old = I;
178     int Amount = Old->getOperand(0).getImm() + 4;
179     if (Amount != 0) {
180       // We need to keep the stack aligned properly.  To do this, we round the
181       // amount of space needed for the outgoing arguments up to the next
182       // alignment boundary.
183       unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
184       Amount = (Amount+Align-1)/Align*Align;
185
186       MachineInstr *New;
187       if (Old->getOpcode() == MBlaze::ADJCALLSTACKDOWN) {
188         New = BuildMI(MF,Old->getDebugLoc(),TII.get(MBlaze::ADDIK),MBlaze::R1)
189                 .addReg(MBlaze::R1).addImm(-Amount);
190       } else {
191         assert(Old->getOpcode() == MBlaze::ADJCALLSTACKUP);
192         New = BuildMI(MF,Old->getDebugLoc(),TII.get(MBlaze::ADDIK),MBlaze::R1)
193                 .addReg(MBlaze::R1).addImm(Amount);
194       }
195
196       // Replace the pseudo instruction with a new instruction...
197       MBB.insert(I, New);
198     }
199   }
200
201   // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
202   MBB.erase(I);
203 }
204
205 // FrameIndex represent objects inside a abstract stack.
206 // We must replace FrameIndex with an stack/frame pointer
207 // direct reference.
208 void MBlazeRegisterInfo::
209 eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj,
210                     RegScavenger *RS) const {
211   MachineInstr &MI = *II;
212   MachineFunction &MF = *MI.getParent()->getParent();
213   MachineFrameInfo *MFI = MF.getFrameInfo();
214   MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
215
216   unsigned i = 0;
217   while (!MI.getOperand(i).isFI()) {
218     ++i;
219     assert(i < MI.getNumOperands() &&
220            "Instr doesn't have FrameIndex operand!");
221   }
222
223   unsigned oi = i == 2 ? 1 : 2;
224
225   DEBUG(errs() << "\nFunction : " << MF.getFunction()->getName() << "\n";
226         errs() << "<--------->\n" << MI);
227
228   int FrameIndex = MI.getOperand(i).getIndex();
229   int stackSize  = MF.getFrameInfo()->getStackSize();
230   int spOffset   = MF.getFrameInfo()->getObjectOffset(FrameIndex);
231
232   DEBUG(errs() << "FrameIndex : " << FrameIndex << "\n"
233                << "spOffset   : " << spOffset << "\n"
234                << "stackSize  : " << stackSize << "\n");
235
236   // as explained on LowerFormalArguments, detect negative offsets
237   // and adjust SPOffsets considering the final stack size.
238   int Offset = (spOffset < 0) ? (stackSize - spOffset) : spOffset;
239   Offset += MI.getOperand(oi).getImm();
240   if (!MFI->isFixedObjectIndex(FrameIndex) && !MBlazeFI->isLiveIn(FrameIndex) && spOffset >= 0)
241     Offset -= MBlazeFI->getStackAdjust();
242
243   DEBUG(errs() << "Offset     : " << Offset << "\n" << "<--------->\n");
244
245   MI.getOperand(oi).ChangeToImmediate(Offset);
246   MI.getOperand(i).ChangeToRegister(getFrameRegister(MF), false);
247 }
248
249 void MBlazeRegisterInfo::
250 processFunctionBeforeFrameFinalized(MachineFunction &MF) const {
251   // Set the stack offset where GP must be saved/loaded from.
252   MachineFrameInfo *MFI = MF.getFrameInfo();
253   MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
254   if (MBlazeFI->needGPSaveRestore())
255     MFI->setObjectOffset(MBlazeFI->getGPFI(), MBlazeFI->getGPStackOffset());
256 }
257
258 unsigned MBlazeRegisterInfo::getRARegister() const {
259   return MBlaze::R15;
260 }
261
262 unsigned MBlazeRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
263   const TargetFrameInfo *TFI = MF.getTarget().getFrameInfo();
264
265   return TFI->hasFP(MF) ? MBlaze::R19 : MBlaze::R1;
266 }
267
268 unsigned MBlazeRegisterInfo::getEHExceptionRegister() const {
269   llvm_unreachable("What is the exception register");
270   return 0;
271 }
272
273 unsigned MBlazeRegisterInfo::getEHHandlerRegister() const {
274   llvm_unreachable("What is the exception handler register");
275   return 0;
276 }
277
278 int MBlazeRegisterInfo::getDwarfRegNum(unsigned RegNo, bool isEH) const {
279   return MBlazeGenRegisterInfo::getDwarfRegNumFull(RegNo,0);
280 }
281
282 #include "MBlazeGenRegisterInfo.inc"
283