Mips16FrameLowering.cpp revision 243830
168673Sobrien//===-- Mips16FrameLowering.cpp - Mips16 Frame Information ----------------===//
2130561Sobrien//
368673Sobrien//                     The LLVM Compiler Infrastructure
468673Sobrien//
568673Sobrien// This file is distributed under the University of Illinois Open Source
668673Sobrien// License. See LICENSE.TXT for details.
768673Sobrien//
868673Sobrien//===----------------------------------------------------------------------===//
968673Sobrien//
1068673Sobrien// This file contains the Mips16 implementation of TargetFrameLowering class.
1168673Sobrien//
1268673Sobrien//===----------------------------------------------------------------------===//
1368673Sobrien
1468673Sobrien#include "Mips16FrameLowering.h"
1568673Sobrien#include "MipsInstrInfo.h"
1668673Sobrien#include "MCTargetDesc/MipsBaseInfo.h"
1768673Sobrien#include "llvm/Function.h"
1868673Sobrien#include "llvm/CodeGen/MachineFrameInfo.h"
1968673Sobrien#include "llvm/CodeGen/MachineFunction.h"
2068673Sobrien#include "llvm/CodeGen/MachineInstrBuilder.h"
2168673Sobrien#include "llvm/CodeGen/MachineModuleInfo.h"
2268673Sobrien#include "llvm/CodeGen/MachineRegisterInfo.h"
2368673Sobrien#include "llvm/DataLayout.h"
2468673Sobrien#include "llvm/Target/TargetOptions.h"
2568673Sobrien#include "llvm/Support/CommandLine.h"
2668673Sobrien
2768673Sobrienusing namespace llvm;
2868673Sobrien
2968673Sobrienvoid Mips16FrameLowering::emitPrologue(MachineFunction &MF) const {
3068673Sobrien  MachineBasicBlock &MBB = MF.front();
3168673Sobrien  MachineFrameInfo *MFI = MF.getFrameInfo();
3268673Sobrien  const MipsInstrInfo &TII =
3368673Sobrien    *static_cast<const MipsInstrInfo*>(MF.getTarget().getInstrInfo());
3468673Sobrien  MachineBasicBlock::iterator MBBI = MBB.begin();
3568673Sobrien  DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
3668673Sobrien  uint64_t StackSize = MFI->getStackSize();
3768673Sobrien
3868673Sobrien  // No need to allocate space on the stack.
39130561Sobrien  if (StackSize == 0 && !MFI->adjustsStack()) return;
40130561Sobrien
41130561Sobrien  // Adjust stack.
42130561Sobrien  if (isInt<16>(-StackSize))
43130561Sobrien    BuildMI(MBB, MBBI, dl, TII.get(Mips::SaveRaF16)).addImm(StackSize);
44130561Sobrien
45130561Sobrien  if (hasFP(MF))
46130561Sobrien    BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0)
47130561Sobrien      .addReg(Mips::SP);
48130561Sobrien
49130561Sobrien}
50130561Sobrien
51130561Sobrienvoid Mips16FrameLowering::emitEpilogue(MachineFunction &MF,
52130561Sobrien                                 MachineBasicBlock &MBB) const {
5377298Sobrien  MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
5468673Sobrien  MachineFrameInfo *MFI = MF.getFrameInfo();
5568673Sobrien  const MipsInstrInfo &TII =
5668673Sobrien    *static_cast<const MipsInstrInfo*>(MF.getTarget().getInstrInfo());
5768673Sobrien  DebugLoc dl = MBBI->getDebugLoc();
5868673Sobrien  uint64_t StackSize = MFI->getStackSize();
5968673Sobrien
6068673Sobrien  if (!StackSize)
6168673Sobrien    return;
6268673Sobrien
6368673Sobrien  if (hasFP(MF))
6468673Sobrien    BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP)
6568673Sobrien      .addReg(Mips::S0);
6668673Sobrien
67130561Sobrien  // Adjust stack.
6868673Sobrien  if (isInt<16>(StackSize))
69    // assumes stacksize multiple of 8
70    BuildMI(MBB, MBBI, dl, TII.get(Mips::RestoreRaF16)).addImm(StackSize);
71}
72
73bool Mips16FrameLowering::
74spillCalleeSavedRegisters(MachineBasicBlock &MBB,
75                          MachineBasicBlock::iterator MI,
76                          const std::vector<CalleeSavedInfo> &CSI,
77                          const TargetRegisterInfo *TRI) const {
78  MachineFunction *MF = MBB.getParent();
79  MachineBasicBlock *EntryBlock = MF->begin();
80
81  //
82  // Registers RA, S0,S1 are the callee saved registers and they
83  // will be saved with the "save" instruction
84  // during emitPrologue
85  //
86  for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
87    // Add the callee-saved register as live-in. Do not add if the register is
88    // RA and return address is taken, because it has already been added in
89    // method MipsTargetLowering::LowerRETURNADDR.
90    // It's killed at the spill, unless the register is RA and return address
91    // is taken.
92    unsigned Reg = CSI[i].getReg();
93    bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA)
94      && MF->getFrameInfo()->isReturnAddressTaken();
95    if (!IsRAAndRetAddrIsTaken)
96      EntryBlock->addLiveIn(Reg);
97  }
98
99  return true;
100}
101
102bool Mips16FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
103                                          MachineBasicBlock::iterator MI,
104                                       const std::vector<CalleeSavedInfo> &CSI,
105                                       const TargetRegisterInfo *TRI) const {
106  //
107  // Registers RA,S0,S1 are the callee saved registers and they will be restored
108  // with the restore instruction during emitEpilogue.
109  // We need to override this virtual function, otherwise llvm will try and
110  // restore the registers on it's on from the stack.
111  //
112
113  return true;
114}
115
116bool
117Mips16FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
118  const MachineFrameInfo *MFI = MF.getFrameInfo();
119  // Reserve call frame if the size of the maximum call frame fits into 15-bit
120  // immediate field and there are no variable sized objects on the stack.
121  return isInt<15>(MFI->getMaxCallFrameSize()) && !MFI->hasVarSizedObjects();
122}
123
124void Mips16FrameLowering::
125processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
126                                     RegScavenger *RS) const {
127  MF.getRegInfo().setPhysRegUsed(Mips::RA);
128  MF.getRegInfo().setPhysRegUsed(Mips::S0);
129  MF.getRegInfo().setPhysRegUsed(Mips::S1);
130}
131
132const MipsFrameLowering *
133llvm::createMips16FrameLowering(const MipsSubtarget &ST) {
134  return new Mips16FrameLowering(ST);
135}
136