Properly save and restore RA and Mips16 callee save registers S0,S1
[oota-llvm.git] / lib / Target / Mips / Mips16FrameLowering.cpp
1 //===-- Mips16FrameLowering.cpp - Mips16 Frame Information ----------------===//
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 Mips16 implementation of TargetFrameLowering class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "Mips16FrameLowering.h"
15 #include "MipsInstrInfo.h"
16 #include "MCTargetDesc/MipsBaseInfo.h"
17 #include "llvm/Function.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineModuleInfo.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/Target/TargetData.h"
24 #include "llvm/Target/TargetOptions.h"
25 #include "llvm/Support/CommandLine.h"
26
27 using namespace llvm;
28
29 void Mips16FrameLowering::emitPrologue(MachineFunction &MF) const {
30   MachineBasicBlock &MBB = MF.front();
31   MachineFrameInfo *MFI = MF.getFrameInfo();
32   const MipsInstrInfo &TII =
33     *static_cast<const MipsInstrInfo*>(MF.getTarget().getInstrInfo());
34   MachineBasicBlock::iterator MBBI = MBB.begin();
35   DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
36   uint64_t StackSize = MFI->getStackSize();
37
38   // No need to allocate space on the stack.
39   if (StackSize == 0 && !MFI->adjustsStack()) return;
40
41   // Adjust stack.
42   if (isInt<16>(-StackSize))
43     BuildMI(MBB, MBBI, dl, TII.get(Mips::SaveRaF16)).addImm(StackSize);
44 }
45
46 void Mips16FrameLowering::emitEpilogue(MachineFunction &MF,
47                                  MachineBasicBlock &MBB) const {
48   MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
49   MachineFrameInfo *MFI = MF.getFrameInfo();
50   const MipsInstrInfo &TII =
51     *static_cast<const MipsInstrInfo*>(MF.getTarget().getInstrInfo());
52   DebugLoc dl = MBBI->getDebugLoc();
53   uint64_t StackSize = MFI->getStackSize();
54
55   if (!StackSize)
56     return;
57
58   // Adjust stack.
59   if (isInt<16>(StackSize))
60     // assumes stacksize multiple of 8
61     BuildMI(MBB, MBBI, dl, TII.get(Mips::RestoreRaF16)).addImm(StackSize);
62 }
63
64 bool Mips16FrameLowering::
65 spillCalleeSavedRegisters(MachineBasicBlock &MBB,
66                           MachineBasicBlock::iterator MI,
67                           const std::vector<CalleeSavedInfo> &CSI,
68                           const TargetRegisterInfo *TRI) const {
69   MachineFunction *MF = MBB.getParent();
70   MachineBasicBlock *EntryBlock = MF->begin();
71   const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
72
73   //
74   // Registers RA, S0,S1 are the callee saved registers and they
75   // will be saved with the "save" instruction
76   // during emitPrologue
77   //
78   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
79     // Add the callee-saved register as live-in. Do not add if the register is
80     // RA and return address is taken, because it has already been added in
81     // method MipsTargetLowering::LowerRETURNADDR.
82     // It's killed at the spill, unless the register is RA and return address
83     // is taken.
84     unsigned Reg = CSI[i].getReg();
85     bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA)
86       && MF->getFrameInfo()->isReturnAddressTaken();
87     if (!IsRAAndRetAddrIsTaken)
88       EntryBlock->addLiveIn(Reg);
89   }
90
91   return true;
92 }
93
94 bool Mips16FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
95                                           MachineBasicBlock::iterator MI,
96                                        const std::vector<CalleeSavedInfo> &CSI,
97                                        const TargetRegisterInfo *TRI) const {
98   //
99   // Registers RA,S0,S1 are the callee saved registers and they will be restored
100   // with the restore instruction during emitEpilogue.
101   // We need to override this virtual function, otherwise llvm will try and
102   // restore the registers on it's on from the stack.
103   //
104
105   return true;
106 }
107
108 bool
109 Mips16FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
110   // FIXME: implement.
111   return true;
112 }
113
114 void Mips16FrameLowering::
115 processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
116                                      RegScavenger *RS) const {
117   MF.getRegInfo().setPhysRegUsed(Mips::RA);
118   MF.getRegInfo().setPhysRegUsed(Mips::S0);
119   MF.getRegInfo().setPhysRegUsed(Mips::S1);
120 }
121
122 const MipsFrameLowering *
123 llvm::createMips16FrameLowering(const MipsSubtarget &ST) {
124   return new Mips16FrameLowering(ST);
125 }