dcf33636b9f557cd01dd7a5c8f7c872e51f69886
[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 "MCTargetDesc/MipsBaseInfo.h"
16 #include "Mips16InstrInfo.h"
17 #include "MipsInstrInfo.h"
18 #include "MipsRegisterInfo.h"
19 #include "MipsSubtarget.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineModuleInfo.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Target/TargetOptions.h"
29
30 using namespace llvm;
31
32 Mips16FrameLowering::Mips16FrameLowering(const MipsSubtarget &STI)
33     : MipsFrameLowering(STI, STI.stackAlignment()) {}
34
35 void Mips16FrameLowering::emitPrologue(MachineFunction &MF) const {
36   MachineBasicBlock &MBB = MF.front();
37   MachineFrameInfo *MFI = MF.getFrameInfo();
38   const Mips16InstrInfo &TII =
39       *static_cast<const Mips16InstrInfo *>(
40           MF.getTarget().getSubtargetImpl()->getInstrInfo());
41   MachineBasicBlock::iterator MBBI = MBB.begin();
42   DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
43   uint64_t StackSize = MFI->getStackSize();
44
45   // No need to allocate space on the stack.
46   if (StackSize == 0 && !MFI->adjustsStack()) return;
47
48   MachineModuleInfo &MMI = MF.getMMI();
49   const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
50   MachineLocation DstML, SrcML;
51
52   // Adjust stack.
53   TII.makeFrame(Mips::SP, StackSize, MBB, MBBI);
54
55   // emit ".cfi_def_cfa_offset StackSize"
56   unsigned CFIIndex = MMI.addFrameInst(
57       MCCFIInstruction::createDefCfaOffset(nullptr, -StackSize));
58   BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
59       .addCFIIndex(CFIIndex);
60
61   const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
62
63   if (CSI.size()) {
64     const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
65
66     for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
67          E = CSI.end(); I != E; ++I) {
68       int64_t Offset = MFI->getObjectOffset(I->getFrameIdx());
69       unsigned Reg = I->getReg();
70       unsigned DReg = MRI->getDwarfRegNum(Reg, true);
71       unsigned CFIIndex = MMI.addFrameInst(
72           MCCFIInstruction::createOffset(nullptr, DReg, Offset));
73       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
74           .addCFIIndex(CFIIndex);
75     }
76   }
77   if (hasFP(MF))
78     BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0)
79       .addReg(Mips::SP).setMIFlag(MachineInstr::FrameSetup);
80
81 }
82
83 void Mips16FrameLowering::emitEpilogue(MachineFunction &MF,
84                                  MachineBasicBlock &MBB) const {
85   MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
86   MachineFrameInfo *MFI = MF.getFrameInfo();
87   const Mips16InstrInfo &TII =
88       *static_cast<const Mips16InstrInfo *>(
89           MF.getTarget().getSubtargetImpl()->getInstrInfo());
90   DebugLoc dl = MBBI->getDebugLoc();
91   uint64_t StackSize = MFI->getStackSize();
92
93   if (!StackSize)
94     return;
95
96   if (hasFP(MF))
97     BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP)
98       .addReg(Mips::S0);
99
100   // Adjust stack.
101   // assumes stacksize multiple of 8
102   TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI);
103 }
104
105 bool Mips16FrameLowering::
106 spillCalleeSavedRegisters(MachineBasicBlock &MBB,
107                           MachineBasicBlock::iterator MI,
108                           const std::vector<CalleeSavedInfo> &CSI,
109                           const TargetRegisterInfo *TRI) const {
110   MachineFunction *MF = MBB.getParent();
111   MachineBasicBlock *EntryBlock = MF->begin();
112
113   //
114   // Registers RA, S0,S1 are the callee saved registers and they
115   // will be saved with the "save" instruction
116   // during emitPrologue
117   //
118   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
119     // Add the callee-saved register as live-in. Do not add if the register is
120     // RA and return address is taken, because it has already been added in
121     // method MipsTargetLowering::LowerRETURNADDR.
122     // It's killed at the spill, unless the register is RA and return address
123     // is taken.
124     unsigned Reg = CSI[i].getReg();
125     bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA)
126       && MF->getFrameInfo()->isReturnAddressTaken();
127     if (!IsRAAndRetAddrIsTaken)
128       EntryBlock->addLiveIn(Reg);
129   }
130
131   return true;
132 }
133
134 bool Mips16FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
135                                           MachineBasicBlock::iterator MI,
136                                        const std::vector<CalleeSavedInfo> &CSI,
137                                        const TargetRegisterInfo *TRI) const {
138   //
139   // Registers RA,S0,S1 are the callee saved registers and they will be restored
140   // with the restore instruction during emitEpilogue.
141   // We need to override this virtual function, otherwise llvm will try and
142   // restore the registers on it's on from the stack.
143   //
144
145   return true;
146 }
147
148 // Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions
149 void Mips16FrameLowering::
150 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
151                               MachineBasicBlock::iterator I) const {
152   if (!hasReservedCallFrame(MF)) {
153     int64_t Amount = I->getOperand(0).getImm();
154
155     if (I->getOpcode() == Mips::ADJCALLSTACKDOWN)
156       Amount = -Amount;
157
158     const Mips16InstrInfo &TII =
159         *static_cast<const Mips16InstrInfo *>(
160             MF.getTarget().getSubtargetImpl()->getInstrInfo());
161
162     TII.adjustStackPtr(Mips::SP, Amount, MBB, I);
163   }
164
165   MBB.erase(I);
166 }
167
168 bool
169 Mips16FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
170   const MachineFrameInfo *MFI = MF.getFrameInfo();
171   // Reserve call frame if the size of the maximum call frame fits into 15-bit
172   // immediate field and there are no variable sized objects on the stack.
173   return isInt<15>(MFI->getMaxCallFrameSize()) && !MFI->hasVarSizedObjects();
174 }
175
176 void Mips16FrameLowering::
177 processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
178                                      RegScavenger *RS) const {
179   const Mips16InstrInfo &TII =
180       *static_cast<const Mips16InstrInfo *>(
181           MF.getTarget().getSubtargetImpl()->getInstrInfo());
182   const MipsRegisterInfo &RI = TII.getRegisterInfo();
183   const BitVector Reserved = RI.getReservedRegs(MF);
184   bool SaveS2 = Reserved[Mips::S2];
185   if (SaveS2)
186     MF.getRegInfo().setPhysRegUsed(Mips::S2);
187   if (hasFP(MF))
188     MF.getRegInfo().setPhysRegUsed(Mips::S0);
189 }
190
191 const MipsFrameLowering *
192 llvm::createMips16FrameLowering(const MipsSubtarget &ST) {
193   return new Mips16FrameLowering(ST);
194 }