Path: blob/main/contrib/llvm-project/llvm/lib/Target/Mips/Mips16FrameLowering.cpp
35269 views
//===- Mips16FrameLowering.cpp - Mips16 Frame Information -----------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains the Mips16 implementation of TargetFrameLowering class.9//10//===----------------------------------------------------------------------===//1112#include "Mips16FrameLowering.h"13#include "MCTargetDesc/MipsBaseInfo.h"14#include "Mips16InstrInfo.h"15#include "MipsInstrInfo.h"16#include "MipsRegisterInfo.h"17#include "MipsSubtarget.h"18#include "llvm/ADT/BitVector.h"19#include "llvm/CodeGen/MachineBasicBlock.h"20#include "llvm/CodeGen/MachineFrameInfo.h"21#include "llvm/CodeGen/MachineFunction.h"22#include "llvm/CodeGen/MachineInstr.h"23#include "llvm/CodeGen/MachineInstrBuilder.h"24#include "llvm/CodeGen/MachineModuleInfo.h"25#include "llvm/IR/DebugLoc.h"26#include "llvm/MC/MCContext.h"27#include "llvm/MC/MCDwarf.h"28#include "llvm/MC/MCRegisterInfo.h"29#include "llvm/MC/MachineLocation.h"30#include "llvm/Support/MathExtras.h"31#include "llvm/CodeGen/TargetFrameLowering.h"32#include <cstdint>33#include <vector>3435using namespace llvm;3637Mips16FrameLowering::Mips16FrameLowering(const MipsSubtarget &STI)38: MipsFrameLowering(STI, STI.getStackAlignment()) {}3940void Mips16FrameLowering::emitPrologue(MachineFunction &MF,41MachineBasicBlock &MBB) const {42MachineFrameInfo &MFI = MF.getFrameInfo();43const Mips16InstrInfo &TII =44*static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());45MachineBasicBlock::iterator MBBI = MBB.begin();4647// Debug location must be unknown since the first debug location is used48// to determine the end of the prologue.49DebugLoc dl;5051uint64_t StackSize = MFI.getStackSize();5253// No need to allocate space on the stack.54if (StackSize == 0 && !MFI.adjustsStack()) return;5556const MCRegisterInfo *MRI = MF.getContext().getRegisterInfo();5758// Adjust stack.59TII.makeFrame(Mips::SP, StackSize, MBB, MBBI);6061// emit ".cfi_def_cfa_offset StackSize"62unsigned CFIIndex =63MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, StackSize));64BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))65.addCFIIndex(CFIIndex);6667const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();6869if (!CSI.empty()) {70const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();7172for (const CalleeSavedInfo &I : CSI) {73int64_t Offset = MFI.getObjectOffset(I.getFrameIdx());74Register Reg = I.getReg();75unsigned DReg = MRI->getDwarfRegNum(Reg, true);76unsigned CFIIndex = MF.addFrameInst(77MCCFIInstruction::createOffset(nullptr, DReg, Offset));78BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))79.addCFIIndex(CFIIndex);80}81}82if (hasFP(MF))83BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0)84.addReg(Mips::SP).setMIFlag(MachineInstr::FrameSetup);85}8687void Mips16FrameLowering::emitEpilogue(MachineFunction &MF,88MachineBasicBlock &MBB) const {89MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();90MachineFrameInfo &MFI = MF.getFrameInfo();91const Mips16InstrInfo &TII =92*static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());93DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();94uint64_t StackSize = MFI.getStackSize();9596if (!StackSize)97return;9899if (hasFP(MF))100BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP)101.addReg(Mips::S0);102103// Adjust stack.104// assumes stacksize multiple of 8105TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI);106}107108bool Mips16FrameLowering::spillCalleeSavedRegisters(109MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,110ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {111MachineFunction *MF = MBB.getParent();112113//114// Registers RA, S0,S1 are the callee saved registers and they115// will be saved with the "save" instruction116// during emitPrologue117//118for (const CalleeSavedInfo &I : CSI) {119// Add the callee-saved register as live-in. Do not add if the register is120// RA and return address is taken, because it has already been added in121// method MipsTargetLowering::lowerRETURNADDR.122// It's killed at the spill, unless the register is RA and return address123// is taken.124Register Reg = I.getReg();125bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA)126&& MF->getFrameInfo().isReturnAddressTaken();127if (!IsRAAndRetAddrIsTaken)128MBB.addLiveIn(Reg);129}130131return true;132}133134bool Mips16FrameLowering::restoreCalleeSavedRegisters(135MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,136MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {137//138// Registers RA,S0,S1 are the callee saved registers and they will be restored139// with the restore instruction during emitEpilogue.140// We need to override this virtual function, otherwise llvm will try and141// restore the registers on it's on from the stack.142//143144return true;145}146147bool148Mips16FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {149const MachineFrameInfo &MFI = MF.getFrameInfo();150// Reserve call frame if the size of the maximum call frame fits into 15-bit151// immediate field and there are no variable sized objects on the stack.152return isInt<15>(MFI.getMaxCallFrameSize()) && !MFI.hasVarSizedObjects();153}154155void Mips16FrameLowering::determineCalleeSaves(MachineFunction &MF,156BitVector &SavedRegs,157RegScavenger *RS) const {158TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);159const Mips16InstrInfo &TII =160*static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());161const MipsRegisterInfo &RI = TII.getRegisterInfo();162const BitVector Reserved = RI.getReservedRegs(MF);163bool SaveS2 = Reserved[Mips::S2];164if (SaveS2)165SavedRegs.set(Mips::S2);166if (hasFP(MF))167SavedRegs.set(Mips::S0);168}169170const MipsFrameLowering *171llvm::createMips16FrameLowering(const MipsSubtarget &ST) {172return new Mips16FrameLowering(ST);173}174175176