LLVM API Documentation

Mips16FrameLowering.cpp
Go to the documentation of this file.
00001 //===-- Mips16FrameLowering.cpp - Mips16 Frame Information ----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the Mips16 implementation of TargetFrameLowering class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "Mips16FrameLowering.h"
00015 #include "MCTargetDesc/MipsBaseInfo.h"
00016 #include "Mips16InstrInfo.h"
00017 #include "MipsInstrInfo.h"
00018 #include "MipsRegisterInfo.h"
00019 #include "MipsSubtarget.h"
00020 #include "llvm/CodeGen/MachineFrameInfo.h"
00021 #include "llvm/CodeGen/MachineFunction.h"
00022 #include "llvm/CodeGen/MachineInstrBuilder.h"
00023 #include "llvm/CodeGen/MachineModuleInfo.h"
00024 #include "llvm/CodeGen/MachineRegisterInfo.h"
00025 #include "llvm/IR/DataLayout.h"
00026 #include "llvm/IR/Function.h"
00027 #include "llvm/Support/CommandLine.h"
00028 #include "llvm/Target/TargetOptions.h"
00029 
00030 using namespace llvm;
00031 
00032 Mips16FrameLowering::Mips16FrameLowering(const MipsSubtarget &STI)
00033     : MipsFrameLowering(STI, STI.stackAlignment()) {}
00034 
00035 void Mips16FrameLowering::emitPrologue(MachineFunction &MF) const {
00036   MachineBasicBlock &MBB = MF.front();
00037   MachineFrameInfo *MFI = MF.getFrameInfo();
00038   const Mips16InstrInfo &TII =
00039       *static_cast<const Mips16InstrInfo *>(MF.getSubtarget().getInstrInfo());
00040   MachineBasicBlock::iterator MBBI = MBB.begin();
00041   DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
00042   uint64_t StackSize = MFI->getStackSize();
00043 
00044   // No need to allocate space on the stack.
00045   if (StackSize == 0 && !MFI->adjustsStack()) return;
00046 
00047   MachineModuleInfo &MMI = MF.getMMI();
00048   const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
00049   MachineLocation DstML, SrcML;
00050 
00051   // Adjust stack.
00052   TII.makeFrame(Mips::SP, StackSize, MBB, MBBI);
00053 
00054   // emit ".cfi_def_cfa_offset StackSize"
00055   unsigned CFIIndex = MMI.addFrameInst(
00056       MCCFIInstruction::createDefCfaOffset(nullptr, -StackSize));
00057   BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
00058       .addCFIIndex(CFIIndex);
00059 
00060   const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
00061 
00062   if (CSI.size()) {
00063     const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
00064 
00065     for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
00066          E = CSI.end(); I != E; ++I) {
00067       int64_t Offset = MFI->getObjectOffset(I->getFrameIdx());
00068       unsigned Reg = I->getReg();
00069       unsigned DReg = MRI->getDwarfRegNum(Reg, true);
00070       unsigned CFIIndex = MMI.addFrameInst(
00071           MCCFIInstruction::createOffset(nullptr, DReg, Offset));
00072       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
00073           .addCFIIndex(CFIIndex);
00074     }
00075   }
00076   if (hasFP(MF))
00077     BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0)
00078       .addReg(Mips::SP).setMIFlag(MachineInstr::FrameSetup);
00079 
00080 }
00081 
00082 void Mips16FrameLowering::emitEpilogue(MachineFunction &MF,
00083                                  MachineBasicBlock &MBB) const {
00084   MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
00085   MachineFrameInfo *MFI = MF.getFrameInfo();
00086   const Mips16InstrInfo &TII =
00087       *static_cast<const Mips16InstrInfo *>(MF.getSubtarget().getInstrInfo());
00088   DebugLoc dl = MBBI->getDebugLoc();
00089   uint64_t StackSize = MFI->getStackSize();
00090 
00091   if (!StackSize)
00092     return;
00093 
00094   if (hasFP(MF))
00095     BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP)
00096       .addReg(Mips::S0);
00097 
00098   // Adjust stack.
00099   // assumes stacksize multiple of 8
00100   TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI);
00101 }
00102 
00103 bool Mips16FrameLowering::
00104 spillCalleeSavedRegisters(MachineBasicBlock &MBB,
00105                           MachineBasicBlock::iterator MI,
00106                           const std::vector<CalleeSavedInfo> &CSI,
00107                           const TargetRegisterInfo *TRI) const {
00108   MachineFunction *MF = MBB.getParent();
00109   MachineBasicBlock *EntryBlock = MF->begin();
00110 
00111   //
00112   // Registers RA, S0,S1 are the callee saved registers and they
00113   // will be saved with the "save" instruction
00114   // during emitPrologue
00115   //
00116   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
00117     // Add the callee-saved register as live-in. Do not add if the register is
00118     // RA and return address is taken, because it has already been added in
00119     // method MipsTargetLowering::LowerRETURNADDR.
00120     // It's killed at the spill, unless the register is RA and return address
00121     // is taken.
00122     unsigned Reg = CSI[i].getReg();
00123     bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA)
00124       && MF->getFrameInfo()->isReturnAddressTaken();
00125     if (!IsRAAndRetAddrIsTaken)
00126       EntryBlock->addLiveIn(Reg);
00127   }
00128 
00129   return true;
00130 }
00131 
00132 bool Mips16FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
00133                                           MachineBasicBlock::iterator MI,
00134                                        const std::vector<CalleeSavedInfo> &CSI,
00135                                        const TargetRegisterInfo *TRI) const {
00136   //
00137   // Registers RA,S0,S1 are the callee saved registers and they will be restored
00138   // with the restore instruction during emitEpilogue.
00139   // We need to override this virtual function, otherwise llvm will try and
00140   // restore the registers on it's on from the stack.
00141   //
00142 
00143   return true;
00144 }
00145 
00146 // Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions
00147 void Mips16FrameLowering::
00148 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
00149                               MachineBasicBlock::iterator I) const {
00150   if (!hasReservedCallFrame(MF)) {
00151     int64_t Amount = I->getOperand(0).getImm();
00152 
00153     if (I->getOpcode() == Mips::ADJCALLSTACKDOWN)
00154       Amount = -Amount;
00155 
00156     const Mips16InstrInfo &TII =
00157         *static_cast<const Mips16InstrInfo *>(MF.getSubtarget().getInstrInfo());
00158 
00159     TII.adjustStackPtr(Mips::SP, Amount, MBB, I);
00160   }
00161 
00162   MBB.erase(I);
00163 }
00164 
00165 bool
00166 Mips16FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
00167   const MachineFrameInfo *MFI = MF.getFrameInfo();
00168   // Reserve call frame if the size of the maximum call frame fits into 15-bit
00169   // immediate field and there are no variable sized objects on the stack.
00170   return isInt<15>(MFI->getMaxCallFrameSize()) && !MFI->hasVarSizedObjects();
00171 }
00172 
00173 void Mips16FrameLowering::
00174 processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
00175                                      RegScavenger *RS) const {
00176   const Mips16InstrInfo &TII =
00177       *static_cast<const Mips16InstrInfo *>(MF.getSubtarget().getInstrInfo());
00178   const MipsRegisterInfo &RI = TII.getRegisterInfo();
00179   const BitVector Reserved = RI.getReservedRegs(MF);
00180   bool SaveS2 = Reserved[Mips::S2];
00181   if (SaveS2)
00182     MF.getRegInfo().setPhysRegUsed(Mips::S2);
00183   if (hasFP(MF))
00184     MF.getRegInfo().setPhysRegUsed(Mips::S0);
00185 }
00186 
00187 const MipsFrameLowering *
00188 llvm::createMips16FrameLowering(const MipsSubtarget &ST) {
00189   return new Mips16FrameLowering(ST);
00190 }