Fix "Control reaches the end of non-void function" warnings,
[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 TargetRegisterInfo 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   return 0; // Not reached
85 }
86
87 void MipsRegisterInfo::reMaterialize(MachineBasicBlock &MBB, 
88                                       MachineBasicBlock::iterator I,
89                                       unsigned DestReg, 
90                                       const MachineInstr *Orig) const 
91 {
92     MachineInstr *MI = Orig->clone();
93     MI->getOperand(0).setReg(DestReg);
94     MBB.insert(I, MI);
95 }
96
97 //===----------------------------------------------------------------------===//
98 //
99 // Callee Saved Registers methods 
100 //
101 //===----------------------------------------------------------------------===//
102
103 /// Mips Callee Saved Registers
104 const unsigned* MipsRegisterInfo::
105 getCalleeSavedRegs(const MachineFunction *MF) const 
106 {
107   // Mips calle-save register range is $16-$26(s0-s7)
108   static const unsigned CalleeSavedRegs[] = {  
109     Mips::S0, Mips::S1, Mips::S2, Mips::S3, 
110     Mips::S4, Mips::S5, Mips::S6, Mips::S7, 0
111   };
112   return CalleeSavedRegs;
113 }
114
115 /// Mips Callee Saved Register Classes
116 const TargetRegisterClass* const* 
117 MipsRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const 
118 {
119   static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
120     &Mips::CPURegsRegClass, &Mips::CPURegsRegClass,
121     &Mips::CPURegsRegClass, &Mips::CPURegsRegClass,
122     &Mips::CPURegsRegClass, &Mips::CPURegsRegClass,
123     &Mips::CPURegsRegClass, &Mips::CPURegsRegClass, 0 
124   };
125   return CalleeSavedRegClasses;
126 }
127
128 BitVector MipsRegisterInfo::
129 getReservedRegs(const MachineFunction &MF) const
130 {
131   BitVector Reserved(getNumRegs());
132   Reserved.set(Mips::ZERO);
133   Reserved.set(Mips::AT);
134   Reserved.set(Mips::K0);
135   Reserved.set(Mips::K1);
136   Reserved.set(Mips::GP);
137   Reserved.set(Mips::SP);
138   Reserved.set(Mips::FP);
139   Reserved.set(Mips::RA);
140   return Reserved;
141 }
142
143 //===----------------------------------------------------------------------===//
144 //
145 // Stack Frame Processing methods
146 // +----------------------------+
147 //
148 // The stack is allocated decrementing the stack pointer on
149 // the first instruction of a function prologue. Once decremented,
150 // all stack referencesare are done thought a positive offset
151 // from the stack/frame pointer, so the stack is considering
152 // to grow up! Otherwise terrible hacks would have to be made
153 // to get this stack ABI compliant :)
154 //
155 //  The stack frame required by the ABI:
156 //  Offset
157 //
158 //  0                 ----------
159 //  4                 Args to pass
160 //  .                 saved $GP  (used in PIC - not supported yet)
161 //  .                 Local Area
162 //  .                 saved "Callee Saved" Registers
163 //  .                 saved FP
164 //  .                 saved RA
165 //  StackSize         -----------
166 //
167 // Offset - offset from sp after stack allocation on function prologue
168 //
169 // The sp is the stack pointer subtracted/added from the stack size
170 // at the Prologue/Epilogue
171 //
172 // References to the previous stack (to obtain arguments) are done
173 // with offsets that exceeds the stack size: (stacksize+(4*(num_arg-1))
174 //
175 // Examples:
176 // - reference to the actual stack frame
177 //   for any local area var there is smt like : FI >= 0, StackOffset: 4
178 //     sw REGX, 4(SP)
179 //
180 // - reference to previous stack frame
181 //   suppose there's a load to the 5th arguments : FI < 0, StackOffset: 16.
182 //   The emitted instruction will be something like:
183 //     lw REGX, 16+StackSize(SP)
184 //
185 // Since the total stack size is unknown on LowerFORMAL_ARGUMENTS, all
186 // stack references (ObjectOffset) created to reference the function 
187 // arguments, are negative numbers. This way, on eliminateFrameIndex it's
188 // possible to detect those references and the offsets are adjusted to
189 // their real location.
190 //
191 //
192 //
193 //===----------------------------------------------------------------------===//
194
195 // hasFP - Return true if the specified function should have a dedicated frame
196 // pointer register.  This is true if the function has variable sized allocas or
197 // if frame pointer elimination is disabled.
198 bool MipsRegisterInfo::
199 hasFP(const MachineFunction &MF) const {
200   return (NoFramePointerElim || MF.getFrameInfo()->hasVarSizedObjects());
201 }
202
203 // This function eliminate ADJCALLSTACKDOWN, 
204 // ADJCALLSTACKUP pseudo instructions
205 void MipsRegisterInfo::
206 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
207                               MachineBasicBlock::iterator I) const {
208   // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
209   MBB.erase(I);
210 }
211
212 // FrameIndex represent objects inside a abstract stack.
213 // We must replace FrameIndex with an stack/frame pointer
214 // direct reference.
215 void MipsRegisterInfo::
216 eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj, 
217                     RegScavenger *RS) const 
218 {
219   MachineInstr &MI = *II;
220   MachineFunction &MF = *MI.getParent()->getParent();
221
222   unsigned i = 0;
223   while (!MI.getOperand(i).isFrameIndex()) {
224     ++i;
225     assert(i < MI.getNumOperands() && 
226            "Instr doesn't have FrameIndex operand!");
227   }
228
229   int FrameIndex = MI.getOperand(i).getIndex();
230   int stackSize  = MF.getFrameInfo()->getStackSize();
231   int spOffset   = MF.getFrameInfo()->getObjectOffset(FrameIndex);
232
233   #ifndef NDEBUG
234   DOUT << "\nFunction : " << MF.getFunction()->getName() << "\n";
235   DOUT << "<--------->\n";
236   MI.print(DOUT);
237   DOUT << "FrameIndex : " << FrameIndex << "\n";
238   DOUT << "spOffset   : " << spOffset << "\n";
239   DOUT << "stackSize  : " << stackSize << "\n";
240   #endif
241
242   // as explained on LowerFORMAL_ARGUMENTS, detect negative offsets 
243   // and adjust SPOffsets considering the final stack size.
244   int Offset = ((spOffset < 0) ? (stackSize + (-(spOffset+4))) : (spOffset));
245   Offset    += MI.getOperand(i-1).getImm();
246
247   #ifndef NDEBUG
248   DOUT << "Offset     : " << Offset << "\n";
249   DOUT << "<--------->\n";
250   #endif
251
252   MI.getOperand(i-1).ChangeToImmediate(Offset);
253   MI.getOperand(i).ChangeToRegister(getFrameRegister(MF), false);
254 }
255
256 void MipsRegisterInfo::
257 emitPrologue(MachineFunction &MF) const 
258 {
259   MachineBasicBlock &MBB   = MF.front();
260   MachineFrameInfo *MFI    = MF.getFrameInfo();
261   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
262   MachineBasicBlock::iterator MBBI = MBB.begin();
263   bool isPIC = (MF.getTarget().getRelocationModel() == Reloc::PIC_);
264
265   // Replace the dummy '0' SPOffset by the negative 
266   // offsets, as explained on LowerFORMAL_ARGUMENTS
267   MipsFI->adjustLoadArgsFI(MFI);
268   MipsFI->adjustStoreVarArgsFI(MFI); 
269
270   // Get the number of bytes to allocate from the FrameInfo.
271   int NumBytes = (int) MFI->getStackSize();
272
273   #ifndef NDEBUG
274   DOUT << "\n<--- EMIT PROLOGUE --->\n";
275   DOUT << "Actual Stack size :" << NumBytes << "\n";
276   #endif
277
278   // No need to allocate space on the stack.
279   if (NumBytes == 0) return;
280
281   int FPOffset, RAOffset;
282   
283   // Allocate space for saved RA and FP when needed 
284   if ((hasFP(MF)) && (MFI->hasCalls())) {
285     FPOffset = NumBytes;
286     RAOffset = (NumBytes+4);
287     NumBytes += 8;
288   } else if ((!hasFP(MF)) && (MFI->hasCalls())) {
289     FPOffset = 0;
290     RAOffset = NumBytes;
291     NumBytes += 4;
292   } else if ((hasFP(MF)) && (!MFI->hasCalls())) {
293     FPOffset = NumBytes;
294     RAOffset = 0;
295     NumBytes += 4;
296   } else {
297     // No calls and no fp.
298     RAOffset = FPOffset = 0;
299   }
300
301   MFI->setObjectOffset(MFI->CreateStackObject(4,4), FPOffset);
302   MFI->setObjectOffset(MFI->CreateStackObject(4,4), RAOffset);
303   MipsFI->setFPStackOffset(FPOffset);
304   MipsFI->setRAStackOffset(RAOffset);
305
306   // Align stack. 
307   unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
308   NumBytes = ((NumBytes+Align-1)/Align*Align);
309
310   #ifndef NDEBUG
311   DOUT << "FPOffset :" << FPOffset << "\n";
312   DOUT << "RAOffset :" << RAOffset << "\n";
313   DOUT << "New stack size :" << NumBytes << "\n\n";
314   #endif
315
316   // Update frame info
317   MFI->setStackSize(NumBytes);
318
319   // PIC speficic function prologue
320   if (isPIC)
321     BuildMI(MBB, MBBI, TII.get(Mips::CPLOAD)).addReg(Mips::T9);
322
323   // Adjust stack : addi sp, sp, (-imm)
324   BuildMI(MBB, MBBI, TII.get(Mips::ADDiu), Mips::SP)
325       .addReg(Mips::SP).addImm(-NumBytes);
326
327   // Save the return address only if the function isnt a leaf one.
328   // sw  $ra, stack_loc($sp)
329   if (MFI->hasCalls()) { 
330     BuildMI(MBB, MBBI, TII.get(Mips::SW))
331         .addReg(Mips::RA).addImm(RAOffset).addReg(Mips::SP);
332   }
333
334   // if framepointer enabled, save it and set it
335   // to point to the stack pointer
336   if (hasFP(MF)) {
337     // sw  $fp,stack_loc($sp)
338     BuildMI(MBB, MBBI, TII.get(Mips::SW))
339       .addReg(Mips::FP).addImm(FPOffset).addReg(Mips::SP);
340
341     // move $fp, $sp
342     BuildMI(MBB, MBBI, TII.get(Mips::ADDu), Mips::FP)
343       .addReg(Mips::SP).addReg(Mips::ZERO);
344   }
345
346   // PIC speficic function prologue
347   if ((isPIC) && (MFI->hasCalls()))
348     BuildMI(MBB, MBBI, TII.get(Mips::CPRESTORE))
349       .addImm(MipsFI->getGPStackOffset());
350 }
351
352 void MipsRegisterInfo::
353 emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const 
354 {
355   MachineBasicBlock::iterator MBBI = prior(MBB.end());
356   MachineFrameInfo *MFI            = MF.getFrameInfo();
357   MipsFunctionInfo *MipsFI         = MF.getInfo<MipsFunctionInfo>();
358
359   // Get the number of bytes from FrameInfo
360   int NumBytes = (int) MFI->getStackSize();
361
362   // Get the FI's where RA and FP are saved.
363   int FPOffset = MipsFI->getFPStackOffset();
364   int RAOffset = MipsFI->getRAStackOffset();
365
366   // if framepointer enabled, restore it and restore the
367   // stack pointer
368   if (hasFP(MF)) {
369     // move $sp, $fp
370     BuildMI(MBB, MBBI, TII.get(Mips::ADDu), Mips::SP)
371       .addReg(Mips::FP).addReg(Mips::ZERO);
372
373     // lw  $fp,stack_loc($sp)
374     BuildMI(MBB, MBBI, TII.get(Mips::LW))
375       .addReg(Mips::FP).addImm(FPOffset).addReg(Mips::SP);
376   }
377
378   // Restore the return address only if the function isnt a leaf one.
379   // lw  $ra, stack_loc($sp)
380   if (MFI->hasCalls()) { 
381     BuildMI(MBB, MBBI, TII.get(Mips::LW))
382         .addReg(Mips::RA).addImm(RAOffset).addReg(Mips::SP);
383   }
384
385   // adjust stack  : insert addi sp, sp, (imm)
386   if (NumBytes) {
387     BuildMI(MBB, MBBI, TII.get(Mips::ADDiu), Mips::SP)
388       .addReg(Mips::SP).addImm(NumBytes);
389   }
390 }
391
392 void MipsRegisterInfo::
393 processFunctionBeforeFrameFinalized(MachineFunction &MF) const {
394   // Set the SPOffset on the FI where GP must be saved/loaded.
395   MachineFrameInfo *MFI = MF.getFrameInfo();
396   if (MFI->hasCalls()) { 
397     MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
398     #ifndef NDEBUG
399     DOUT << "processFunctionBeforeFrameFinalized\n";
400     DOUT << "GPOffset :" << MipsFI->getGPStackOffset() << "\n";
401     DOUT << "FI :" << MipsFI->getGPFI() << "\n";
402     #endif
403     MFI->setObjectOffset(MipsFI->getGPFI(), MipsFI->getGPStackOffset());
404   }    
405 }
406
407 unsigned MipsRegisterInfo::
408 getRARegister() const {
409   return Mips::RA;
410 }
411
412 unsigned MipsRegisterInfo::
413 getFrameRegister(MachineFunction &MF) const {
414   return hasFP(MF) ? Mips::FP : Mips::SP;
415 }
416
417 unsigned MipsRegisterInfo::
418 getEHExceptionRegister() const {
419   assert(0 && "What is the exception register");
420   return 0;
421 }
422
423 unsigned MipsRegisterInfo::
424 getEHHandlerRegister() const {
425   assert(0 && "What is the exception handler register");
426   return 0;
427 }
428
429 int MipsRegisterInfo::
430 getDwarfRegNum(unsigned RegNum, bool isEH) const {
431   assert(0 && "What is the dwarf register number");
432   return -1;
433 }
434
435 #include "MipsGenRegisterInfo.inc"
436