Mips16FrameLowering.cpp revision 296417
1239310Sdim//===-- Mips16FrameLowering.cpp - Mips16 Frame Information ----------------===// 2239310Sdim// 3239310Sdim// The LLVM Compiler Infrastructure 4239310Sdim// 5239310Sdim// This file is distributed under the University of Illinois Open Source 6239310Sdim// License. See LICENSE.TXT for details. 7239310Sdim// 8239310Sdim//===----------------------------------------------------------------------===// 9239310Sdim// 10239310Sdim// This file contains the Mips16 implementation of TargetFrameLowering class. 11239310Sdim// 12239310Sdim//===----------------------------------------------------------------------===// 13239310Sdim 14239310Sdim#include "Mips16FrameLowering.h" 15249423Sdim#include "MCTargetDesc/MipsBaseInfo.h" 16249423Sdim#include "Mips16InstrInfo.h" 17239310Sdim#include "MipsInstrInfo.h" 18276479Sdim#include "MipsRegisterInfo.h" 19276479Sdim#include "MipsSubtarget.h" 20239310Sdim#include "llvm/CodeGen/MachineFrameInfo.h" 21239310Sdim#include "llvm/CodeGen/MachineFunction.h" 22239310Sdim#include "llvm/CodeGen/MachineInstrBuilder.h" 23239310Sdim#include "llvm/CodeGen/MachineModuleInfo.h" 24239310Sdim#include "llvm/CodeGen/MachineRegisterInfo.h" 25249423Sdim#include "llvm/IR/DataLayout.h" 26249423Sdim#include "llvm/IR/Function.h" 27249423Sdim#include "llvm/Support/CommandLine.h" 28239310Sdim#include "llvm/Target/TargetOptions.h" 29239310Sdim 30239310Sdimusing namespace llvm; 31239310Sdim 32276479SdimMips16FrameLowering::Mips16FrameLowering(const MipsSubtarget &STI) 33276479Sdim : MipsFrameLowering(STI, STI.stackAlignment()) {} 34276479Sdim 35288943Sdimvoid Mips16FrameLowering::emitPrologue(MachineFunction &MF, 36288943Sdim MachineBasicBlock &MBB) const { 37288943Sdim assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported"); 38239310Sdim MachineFrameInfo *MFI = MF.getFrameInfo(); 39249423Sdim const Mips16InstrInfo &TII = 40288943Sdim *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo()); 41239310Sdim MachineBasicBlock::iterator MBBI = MBB.begin(); 42296417Sdim 43296417Sdim // Debug location must be unknown since the first debug location is used 44296417Sdim // to determine the end of the prologue. 45296417Sdim DebugLoc dl; 46296417Sdim 47239310Sdim uint64_t StackSize = MFI->getStackSize(); 48239310Sdim 49239310Sdim // No need to allocate space on the stack. 50239310Sdim if (StackSize == 0 && !MFI->adjustsStack()) return; 51239310Sdim 52249423Sdim MachineModuleInfo &MMI = MF.getMMI(); 53261991Sdim const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo(); 54249423Sdim MachineLocation DstML, SrcML; 55249423Sdim 56239310Sdim // Adjust stack. 57249423Sdim TII.makeFrame(Mips::SP, StackSize, MBB, MBBI); 58243830Sdim 59249423Sdim // emit ".cfi_def_cfa_offset StackSize" 60276479Sdim unsigned CFIIndex = MMI.addFrameInst( 61276479Sdim MCCFIInstruction::createDefCfaOffset(nullptr, -StackSize)); 62276479Sdim BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 63276479Sdim .addCFIIndex(CFIIndex); 64249423Sdim 65276479Sdim const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); 66249423Sdim 67276479Sdim if (CSI.size()) { 68276479Sdim const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); 69249423Sdim 70276479Sdim for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(), 71276479Sdim E = CSI.end(); I != E; ++I) { 72276479Sdim int64_t Offset = MFI->getObjectOffset(I->getFrameIdx()); 73276479Sdim unsigned Reg = I->getReg(); 74276479Sdim unsigned DReg = MRI->getDwarfRegNum(Reg, true); 75276479Sdim unsigned CFIIndex = MMI.addFrameInst( 76276479Sdim MCCFIInstruction::createOffset(nullptr, DReg, Offset)); 77276479Sdim BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 78276479Sdim .addCFIIndex(CFIIndex); 79276479Sdim } 80276479Sdim } 81243830Sdim if (hasFP(MF)) 82243830Sdim BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0) 83276479Sdim .addReg(Mips::SP).setMIFlag(MachineInstr::FrameSetup); 84243830Sdim 85239310Sdim} 86239310Sdim 87239310Sdimvoid Mips16FrameLowering::emitEpilogue(MachineFunction &MF, 88239310Sdim MachineBasicBlock &MBB) const { 89239310Sdim MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 90239310Sdim MachineFrameInfo *MFI = MF.getFrameInfo(); 91249423Sdim const Mips16InstrInfo &TII = 92288943Sdim *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo()); 93239310Sdim DebugLoc dl = MBBI->getDebugLoc(); 94239310Sdim uint64_t StackSize = MFI->getStackSize(); 95239310Sdim 96239310Sdim if (!StackSize) 97239310Sdim return; 98239310Sdim 99243830Sdim if (hasFP(MF)) 100243830Sdim BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP) 101243830Sdim .addReg(Mips::S0); 102243830Sdim 103239310Sdim // Adjust stack. 104249423Sdim // assumes stacksize multiple of 8 105249423Sdim TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI); 106239310Sdim} 107239310Sdim 108239310Sdimbool Mips16FrameLowering:: 109239310SdimspillCalleeSavedRegisters(MachineBasicBlock &MBB, 110239310Sdim MachineBasicBlock::iterator MI, 111239310Sdim const std::vector<CalleeSavedInfo> &CSI, 112239310Sdim const TargetRegisterInfo *TRI) const { 113243830Sdim MachineFunction *MF = MBB.getParent(); 114296417Sdim MachineBasicBlock *EntryBlock = &MF->front(); 115243830Sdim 116243830Sdim // 117243830Sdim // Registers RA, S0,S1 are the callee saved registers and they 118243830Sdim // will be saved with the "save" instruction 119243830Sdim // during emitPrologue 120243830Sdim // 121243830Sdim for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 122243830Sdim // Add the callee-saved register as live-in. Do not add if the register is 123243830Sdim // RA and return address is taken, because it has already been added in 124243830Sdim // method MipsTargetLowering::LowerRETURNADDR. 125243830Sdim // It's killed at the spill, unless the register is RA and return address 126243830Sdim // is taken. 127243830Sdim unsigned Reg = CSI[i].getReg(); 128243830Sdim bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA) 129243830Sdim && MF->getFrameInfo()->isReturnAddressTaken(); 130243830Sdim if (!IsRAAndRetAddrIsTaken) 131243830Sdim EntryBlock->addLiveIn(Reg); 132243830Sdim } 133243830Sdim 134239310Sdim return true; 135239310Sdim} 136239310Sdim 137243830Sdimbool Mips16FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, 138243830Sdim MachineBasicBlock::iterator MI, 139243830Sdim const std::vector<CalleeSavedInfo> &CSI, 140243830Sdim const TargetRegisterInfo *TRI) const { 141243830Sdim // 142243830Sdim // Registers RA,S0,S1 are the callee saved registers and they will be restored 143243830Sdim // with the restore instruction during emitEpilogue. 144243830Sdim // We need to override this virtual function, otherwise llvm will try and 145243830Sdim // restore the registers on it's on from the stack. 146243830Sdim // 147243830Sdim 148243830Sdim return true; 149243830Sdim} 150243830Sdim 151239310Sdimbool 152239310SdimMips16FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { 153243830Sdim const MachineFrameInfo *MFI = MF.getFrameInfo(); 154243830Sdim // Reserve call frame if the size of the maximum call frame fits into 15-bit 155243830Sdim // immediate field and there are no variable sized objects on the stack. 156243830Sdim return isInt<15>(MFI->getMaxCallFrameSize()) && !MFI->hasVarSizedObjects(); 157239310Sdim} 158239310Sdim 159288943Sdimvoid Mips16FrameLowering::determineCalleeSaves(MachineFunction &MF, 160288943Sdim BitVector &SavedRegs, 161288943Sdim RegScavenger *RS) const { 162288943Sdim TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS); 163276479Sdim const Mips16InstrInfo &TII = 164288943Sdim *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo()); 165276479Sdim const MipsRegisterInfo &RI = TII.getRegisterInfo(); 166276479Sdim const BitVector Reserved = RI.getReservedRegs(MF); 167276479Sdim bool SaveS2 = Reserved[Mips::S2]; 168276479Sdim if (SaveS2) 169288943Sdim SavedRegs.set(Mips::S2); 170276479Sdim if (hasFP(MF)) 171288943Sdim SavedRegs.set(Mips::S0); 172239310Sdim} 173239310Sdim 174239310Sdimconst MipsFrameLowering * 175239310Sdimllvm::createMips16FrameLowering(const MipsSubtarget &ST) { 176239310Sdim return new Mips16FrameLowering(ST); 177239310Sdim} 178