Move some more instruction creation methods from RegisterInfo into InstrInfo.
[oota-llvm.git] / lib / Target / Mips / MipsRegisterInfo.cpp
1 //===- MipsRegisterInfo.cpp - MIPS 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 MIPS implementation of the MRegisterInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "mips-reg-info"
15
16 #include "Mips.h"
17 #include "MipsRegisterInfo.h"
18 #include "MipsMachineFunction.h"
19 #include "llvm/Constants.h"
20 #include "llvm/Type.h"
21 #include "llvm/Function.h"
22 #include "llvm/CodeGen/ValueTypes.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineLocation.h"
27 #include "llvm/Target/TargetFrameInfo.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/ADT/BitVector.h"
34 #include "llvm/ADT/STLExtras.h"
35 //#include "MipsSubtarget.h"
36
37 using namespace llvm;
38
39 // TODO: add subtarget support
40 MipsRegisterInfo::MipsRegisterInfo(const TargetInstrInfo &tii)
41   : MipsGenRegisterInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP),
42   TII(tii) {}
43
44 /// getRegisterNumbering - Given the enum value for some register, e.g.
45 /// Mips::RA, return the number that it corresponds to (e.g. 31).
46 unsigned MipsRegisterInfo::
47 getRegisterNumbering(unsigned RegEnum) 
48 {
49   switch (RegEnum) {
50     case Mips::ZERO : return 0;
51     case Mips::AT   : return 1;
52     case Mips::V0   : return 2;
53     case Mips::V1   : return 3;
54     case Mips::A0   : return 4;
55     case Mips::A1   : return 5;
56     case Mips::A2   : return 6;
57     case Mips::A3   : return 7;
58     case Mips::T0   : return 8;
59     case Mips::T1   : return 9;
60     case Mips::T2   : return 10;
61     case Mips::T3   : return 11;
62     case Mips::T4   : return 12;
63     case Mips::T5   : return 13;
64     case Mips::T6   : return 14;
65     case Mips::T7   : return 15;
66     case Mips::T8   : return 16;
67     case Mips::T9   : return 17;
68     case Mips::S0   : return 18;
69     case Mips::S1   : return 19;
70     case Mips::S2   : return 20;
71     case Mips::S3   : return 21;
72     case Mips::S4   : return 22;
73     case Mips::S5   : return 23;
74     case Mips::S6   : return 24;
75     case Mips::S7   : return 25;
76     case Mips::K0   : return 26;
77     case Mips::K1   : return 27;
78     case Mips::GP   : return 28;
79     case Mips::SP   : return 29;
80     case Mips::FP   : return 30;
81     case Mips::RA   : return 31;
82     default: assert(0 && "Unknown register number!");
83   }    
84 }
85
86 void MipsRegisterInfo::reMaterialize(MachineBasicBlock &MBB, 
87                                       MachineBasicBlock::iterator I,
88                                       unsigned DestReg, 
89                                       const MachineInstr *Orig) const 
90 {
91     MachineInstr *MI = Orig->clone();
92     MI->getOperand(0).setReg(DestReg);
93     MBB.insert(I, MI);
94 }
95
96 MachineInstr *MipsRegisterInfo::
97 foldMemoryOperand(MachineInstr* MI,
98                   SmallVectorImpl<unsigned> &Ops, int FI) const 
99 {
100   if (Ops.size() != 1) return NULL;
101
102   MachineInstr *NewMI = NULL;
103
104   switch (MI->getOpcode()) 
105   {
106     case Mips::ADDu:
107       if ((MI->getOperand(0).isRegister()) &&
108         (MI->getOperand(1).isRegister()) && 
109         (MI->getOperand(1).getReg() == Mips::ZERO) &&
110         (MI->getOperand(2).isRegister())) 
111       {
112         if (Ops[0] == 0)    // COPY -> STORE
113           NewMI = BuildMI(TII.get(Mips::SW)).addFrameIndex(FI)
114                   .addImm(0).addReg(MI->getOperand(2).getReg());
115         else                   // COPY -> LOAD
116           NewMI = BuildMI(TII.get(Mips::LW), MI->getOperand(0)
117                   .getReg()).addImm(0).addFrameIndex(FI);
118       }
119       break;
120   }
121
122   if (NewMI)
123     NewMI->copyKillDeadInfo(MI);
124   return NewMI;
125 }
126
127 //===----------------------------------------------------------------------===//
128 //
129 // Callee Saved Registers methods 
130 //
131 //===----------------------------------------------------------------------===//
132
133 /// Mips Callee Saved Registers
134 const unsigned* MipsRegisterInfo::
135 getCalleeSavedRegs(const MachineFunction *MF) const 
136 {
137   // Mips calle-save register range is $16-$26(s0-s7)
138   static const unsigned CalleeSavedRegs[] = {  
139     Mips::S0, Mips::S1, Mips::S2, Mips::S3, 
140     Mips::S4, Mips::S5, Mips::S6, Mips::S7, 0
141   };
142   return CalleeSavedRegs;
143 }
144
145 /// Mips Callee Saved Register Classes
146 const TargetRegisterClass* const* 
147 MipsRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const 
148 {
149   static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
150     &Mips::CPURegsRegClass, &Mips::CPURegsRegClass,
151     &Mips::CPURegsRegClass, &Mips::CPURegsRegClass,
152     &Mips::CPURegsRegClass, &Mips::CPURegsRegClass,
153     &Mips::CPURegsRegClass, &Mips::CPURegsRegClass, 0 
154   };
155   return CalleeSavedRegClasses;
156 }
157
158 BitVector MipsRegisterInfo::
159 getReservedRegs(const MachineFunction &MF) const
160 {
161   BitVector Reserved(getNumRegs());
162   Reserved.set(Mips::ZERO);
163   Reserved.set(Mips::AT);
164   Reserved.set(Mips::K0);
165   Reserved.set(Mips::K1);
166   Reserved.set(Mips::GP);
167   Reserved.set(Mips::SP);
168   Reserved.set(Mips::FP);
169   Reserved.set(Mips::RA);
170   return Reserved;
171 }
172
173 //===----------------------------------------------------------------------===//
174 //
175 // Stack Frame Processing methods
176 // +----------------------------+
177 //
178 // The stack is allocated decrementing the stack pointer on
179 // the first instruction of a function prologue. Once decremented,
180 // all stack referencesare are done thought a positive offset
181 // from the stack/frame pointer, so the stack is considering
182 // to grow up! Otherwise terrible hacks would have to be made
183 // to get this stack ABI compliant :)
184 //
185 //  The stack frame required by the ABI:
186 //  Offset
187 //
188 //  0                 ----------
189 //  4                 Args to pass
190 //  .                 saved $GP  (used in PIC - not supported yet)
191 //  .                 Local Area
192 //  .                 saved "Callee Saved" Registers
193 //  .                 saved FP
194 //  .                 saved RA
195 //  StackSize         -----------
196 //
197 // Offset - offset from sp after stack allocation on function prologue
198 //
199 // The sp is the stack pointer subtracted/added from the stack size
200 // at the Prologue/Epilogue
201 //
202 // References to the previous stack (to obtain arguments) are done
203 // with offsets that exceeds the stack size: (stacksize+(4*(num_arg-1))
204 //
205 // Examples:
206 // - reference to the actual stack frame
207 //   for any local area var there is smt like : FI >= 0, StackOffset: 4
208 //     sw REGX, 4(SP)
209 //
210 // - reference to previous stack frame
211 //   suppose there's a load to the 5th arguments : FI < 0, StackOffset: 16.
212 //   The emitted instruction will be something like:
213 //     lw REGX, 16+StackSize(SP)
214 //
215 // Since the total stack size is unknown on LowerFORMAL_ARGUMENTS, all
216 // stack references (ObjectOffset) created to reference the function 
217 // arguments, are negative numbers. This way, on eliminateFrameIndex it's
218 // possible to detect those references and the offsets are adjusted to
219 // their real location.
220 //
221 //
222 //
223 //===----------------------------------------------------------------------===//
224
225 // hasFP - Return true if the specified function should have a dedicated frame
226 // pointer register.  This is true if the function has variable sized allocas or
227 // if frame pointer elimination is disabled.
228 bool MipsRegisterInfo::
229 hasFP(const MachineFunction &MF) const {
230   return (NoFramePointerElim || MF.getFrameInfo()->hasVarSizedObjects());
231 }
232
233 // This function eliminate ADJCALLSTACKDOWN, 
234 // ADJCALLSTACKUP pseudo instructions
235 void MipsRegisterInfo::
236 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
237                               MachineBasicBlock::iterator I) const {
238   // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
239   MBB.erase(I);
240 }
241
242 // FrameIndex represent objects inside a abstract stack.
243 // We must replace FrameIndex with an stack/frame pointer
244 // direct reference.
245 void MipsRegisterInfo::
246 eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj, 
247                     RegScavenger *RS) const 
248 {
249   MachineInstr &MI = *II;
250   MachineFunction &MF = *MI.getParent()->getParent();
251
252   unsigned i = 0;
253   while (!MI.getOperand(i).isFrameIndex()) {
254     ++i;
255     assert(i < MI.getNumOperands() && 
256            "Instr doesn't have FrameIndex operand!");
257   }
258
259   int FrameIndex = MI.getOperand(i).getIndex();
260   int stackSize  = MF.getFrameInfo()->getStackSize();
261   int spOffset   = MF.getFrameInfo()->getObjectOffset(FrameIndex);
262
263   #ifndef NDEBUG
264   DOUT << "\nFunction : " << MF.getFunction()->getName() << "\n";
265   DOUT << "<--------->\n";
266   MI.print(DOUT);
267   DOUT << "FrameIndex : " << FrameIndex << "\n";
268   DOUT << "spOffset   : " << spOffset << "\n";
269   DOUT << "stackSize  : " << stackSize << "\n";
270   #endif
271
272   // as explained on LowerFORMAL_ARGUMENTS, detect negative offsets 
273   // and adjust SPOffsets considering the final stack size.
274   int Offset = ((spOffset < 0) ? (stackSize + (-(spOffset+4))) : (spOffset));
275   Offset    += MI.getOperand(i-1).getImm();
276
277   #ifndef NDEBUG
278   DOUT << "Offset     : " << Offset << "\n";
279   DOUT << "<--------->\n";
280   #endif
281
282   MI.getOperand(i-1).ChangeToImmediate(Offset);
283   MI.getOperand(i).ChangeToRegister(getFrameRegister(MF), false);
284 }
285
286 void MipsRegisterInfo::
287 emitPrologue(MachineFunction &MF) const 
288 {
289   MachineBasicBlock &MBB   = MF.front();
290   MachineFrameInfo *MFI    = MF.getFrameInfo();
291   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
292   MachineBasicBlock::iterator MBBI = MBB.begin();
293   bool isPIC = (MF.getTarget().getRelocationModel() == Reloc::PIC_);
294
295   // Replace the dummy '0' SPOffset by the negative 
296   // offsets, as explained on LowerFORMAL_ARGUMENTS
297   MipsFI->adjustLoadArgsFI(MFI);
298   MipsFI->adjustStoreVarArgsFI(MFI); 
299
300   // Get the number of bytes to allocate from the FrameInfo.
301   int NumBytes = (int) MFI->getStackSize();
302
303   #ifndef NDEBUG
304   DOUT << "\n<--- EMIT PROLOGUE --->\n";
305   DOUT << "Actual Stack size :" << NumBytes << "\n";
306   #endif
307
308   // No need to allocate space on the stack.
309   if (NumBytes == 0) return;
310
311   int FPOffset, RAOffset;
312   
313   // Allocate space for saved RA and FP when needed 
314   if ((hasFP(MF)) && (MFI->hasCalls())) {
315     FPOffset = NumBytes;
316     RAOffset = (NumBytes+4);
317     NumBytes += 8;
318   } else if ((!hasFP(MF)) && (MFI->hasCalls())) {
319     FPOffset = 0;
320     RAOffset = NumBytes;
321     NumBytes += 4;
322   } else if ((hasFP(MF)) && (!MFI->hasCalls())) {
323     FPOffset = NumBytes;
324     RAOffset = 0;
325     NumBytes += 4;
326   }
327
328   MFI->setObjectOffset(MFI->CreateStackObject(4,4), FPOffset);
329   MFI->setObjectOffset(MFI->CreateStackObject(4,4), RAOffset);
330   MipsFI->setFPStackOffset(FPOffset);
331   MipsFI->setRAStackOffset(RAOffset);
332
333   // Align stack. 
334   unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
335   NumBytes = ((NumBytes+Align-1)/Align*Align);
336
337   #ifndef NDEBUG
338   DOUT << "FPOffset :" << FPOffset << "\n";
339   DOUT << "RAOffset :" << RAOffset << "\n";
340   DOUT << "New stack size :" << NumBytes << "\n\n";
341   #endif
342
343   // Update frame info
344   MFI->setStackSize(NumBytes);
345
346   // PIC speficic function prologue
347   if (isPIC)
348     BuildMI(MBB, MBBI, TII.get(Mips::CPLOAD)).addReg(Mips::T9);
349
350   // Adjust stack : addi sp, sp, (-imm)
351   BuildMI(MBB, MBBI, TII.get(Mips::ADDiu), Mips::SP)
352       .addReg(Mips::SP).addImm(-NumBytes);
353
354   // Save the return address only if the function isnt a leaf one.
355   // sw  $ra, stack_loc($sp)
356   if (MFI->hasCalls()) { 
357     BuildMI(MBB, MBBI, TII.get(Mips::SW))
358         .addReg(Mips::RA).addImm(RAOffset).addReg(Mips::SP);
359   }
360
361   // if framepointer enabled, save it and set it
362   // to point to the stack pointer
363   if (hasFP(MF)) {
364     // sw  $fp,stack_loc($sp)
365     BuildMI(MBB, MBBI, TII.get(Mips::SW))
366       .addReg(Mips::FP).addImm(FPOffset).addReg(Mips::SP);
367
368     // move $fp, $sp
369     BuildMI(MBB, MBBI, TII.get(Mips::ADDu), Mips::FP)
370       .addReg(Mips::SP).addReg(Mips::ZERO);
371   }
372
373   // PIC speficic function prologue
374   if ((isPIC) && (MFI->hasCalls()))
375     BuildMI(MBB, MBBI, TII.get(Mips::CPRESTORE))
376       .addImm(MipsFI->getGPStackOffset());
377 }
378
379 void MipsRegisterInfo::
380 emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const 
381 {
382   MachineBasicBlock::iterator MBBI = prior(MBB.end());
383   MachineFrameInfo *MFI            = MF.getFrameInfo();
384   MipsFunctionInfo *MipsFI         = MF.getInfo<MipsFunctionInfo>();
385
386   // Get the number of bytes from FrameInfo
387   int NumBytes = (int) MFI->getStackSize();
388
389   // Get the FI's where RA and FP are saved.
390   int FPOffset = MipsFI->getFPStackOffset();
391   int RAOffset = MipsFI->getRAStackOffset();
392
393   // if framepointer enabled, restore it and restore the
394   // stack pointer
395   if (hasFP(MF)) {
396     // move $sp, $fp
397     BuildMI(MBB, MBBI, TII.get(Mips::ADDu), Mips::SP)
398       .addReg(Mips::FP).addReg(Mips::ZERO);
399
400     // lw  $fp,stack_loc($sp)
401     BuildMI(MBB, MBBI, TII.get(Mips::LW))
402       .addReg(Mips::FP).addImm(FPOffset).addReg(Mips::SP);
403   }
404
405   // Restore the return address only if the function isnt a leaf one.
406   // lw  $ra, stack_loc($sp)
407   if (MFI->hasCalls()) { 
408     BuildMI(MBB, MBBI, TII.get(Mips::LW))
409         .addReg(Mips::RA).addImm(RAOffset).addReg(Mips::SP);
410   }
411
412   // adjust stack  : insert addi sp, sp, (imm)
413   if (NumBytes) {
414     BuildMI(MBB, MBBI, TII.get(Mips::ADDiu), Mips::SP)
415       .addReg(Mips::SP).addImm(NumBytes);
416   }
417 }
418
419 void MipsRegisterInfo::
420 processFunctionBeforeFrameFinalized(MachineFunction &MF) const {
421   // Set the SPOffset on the FI where GP must be saved/loaded.
422   MachineFrameInfo *MFI = MF.getFrameInfo();
423   if (MFI->hasCalls()) { 
424     MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
425     #ifndef NDEBUG
426     DOUT << "processFunctionBeforeFrameFinalized\n";
427     DOUT << "GPOffset :" << MipsFI->getGPStackOffset() << "\n";
428     DOUT << "FI :" << MipsFI->getGPFI() << "\n";
429     #endif
430     MFI->setObjectOffset(MipsFI->getGPFI(), MipsFI->getGPStackOffset());
431   }    
432 }
433
434 unsigned MipsRegisterInfo::
435 getRARegister() const {
436   return Mips::RA;
437 }
438
439 unsigned MipsRegisterInfo::
440 getFrameRegister(MachineFunction &MF) const {
441   return hasFP(MF) ? Mips::FP : Mips::SP;
442 }
443
444 unsigned MipsRegisterInfo::
445 getEHExceptionRegister() const {
446   assert(0 && "What is the exception register");
447   return 0;
448 }
449
450 unsigned MipsRegisterInfo::
451 getEHHandlerRegister() const {
452   assert(0 && "What is the exception handler register");
453   return 0;
454 }
455
456 int MipsRegisterInfo::
457 getDwarfRegNum(unsigned RegNum, bool isEH) const {
458   assert(0 && "What is the dwarf register number");
459   return -1;
460 }
461
462 #include "MipsGenRegisterInfo.inc"
463