14e9736b1SEugene Zelenko //===- Mips16FrameLowering.cpp - Mips16 Frame Information -----------------===//
2d1c43ceeSAkira Hatanaka //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6d1c43ceeSAkira Hatanaka //
7d1c43ceeSAkira Hatanaka //===----------------------------------------------------------------------===//
8d1c43ceeSAkira Hatanaka //
9d1c43ceeSAkira Hatanaka // This file contains the Mips16 implementation of TargetFrameLowering class.
10d1c43ceeSAkira Hatanaka //
11d1c43ceeSAkira Hatanaka //===----------------------------------------------------------------------===//
12d1c43ceeSAkira Hatanaka
134e9736b1SEugene Zelenko #include "Mips16FrameLowering.h"
146bda14b3SChandler Carruth #include "MCTargetDesc/MipsBaseInfo.h"
15be81023dSChandler Carruth #include "Mips16InstrInfo.h"
16ed0881b2SChandler Carruth #include "MipsInstrInfo.h"
170ff40017SReed Kotler #include "MipsRegisterInfo.h"
184cdb3f9bSEric Christopher #include "MipsSubtarget.h"
194e9736b1SEugene Zelenko #include "llvm/ADT/BitVector.h"
204e9736b1SEugene Zelenko #include "llvm/CodeGen/MachineBasicBlock.h"
21d1c43ceeSAkira Hatanaka #include "llvm/CodeGen/MachineFrameInfo.h"
22d1c43ceeSAkira Hatanaka #include "llvm/CodeGen/MachineFunction.h"
234e9736b1SEugene Zelenko #include "llvm/CodeGen/MachineInstr.h"
24d1c43ceeSAkira Hatanaka #include "llvm/CodeGen/MachineInstrBuilder.h"
25d1c43ceeSAkira Hatanaka #include "llvm/CodeGen/MachineModuleInfo.h"
264e9736b1SEugene Zelenko #include "llvm/IR/DebugLoc.h"
274e9736b1SEugene Zelenko #include "llvm/MC/MCContext.h"
284e9736b1SEugene Zelenko #include "llvm/MC/MCDwarf.h"
294e9736b1SEugene Zelenko #include "llvm/MC/MCRegisterInfo.h"
306bda14b3SChandler Carruth #include "llvm/MC/MachineLocation.h"
314e9736b1SEugene Zelenko #include "llvm/Support/MathExtras.h"
321be62f03SDavid Blaikie #include "llvm/CodeGen/TargetFrameLowering.h"
334e9736b1SEugene Zelenko #include <cassert>
344e9736b1SEugene Zelenko #include <cstdint>
354e9736b1SEugene Zelenko #include <vector>
36d1c43ceeSAkira Hatanaka
37d1c43ceeSAkira Hatanaka using namespace llvm;
38d1c43ceeSAkira Hatanaka
Mips16FrameLowering(const MipsSubtarget & STI)394cdb3f9bSEric Christopher Mips16FrameLowering::Mips16FrameLowering(const MipsSubtarget &STI)
401255b165SJohn Baldwin : MipsFrameLowering(STI, STI.getStackAlignment()) {}
414cdb3f9bSEric Christopher
emitPrologue(MachineFunction & MF,MachineBasicBlock & MBB) const4261b305edSQuentin Colombet void Mips16FrameLowering::emitPrologue(MachineFunction &MF,
4361b305edSQuentin Colombet MachineBasicBlock &MBB) const {
44941a705bSMatthias Braun MachineFrameInfo &MFI = MF.getFrameInfo();
45d019dbf7SReed Kotler const Mips16InstrInfo &TII =
4696e72c6aSEric Christopher *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
47d1c43ceeSAkira Hatanaka MachineBasicBlock::iterator MBBI = MBB.begin();
48057c5a6bSOleg Ranevskyy
49057c5a6bSOleg Ranevskyy // Debug location must be unknown since the first debug location is used
50057c5a6bSOleg Ranevskyy // to determine the end of the prologue.
51057c5a6bSOleg Ranevskyy DebugLoc dl;
52057c5a6bSOleg Ranevskyy
53941a705bSMatthias Braun uint64_t StackSize = MFI.getStackSize();
54d1c43ceeSAkira Hatanaka
55d1c43ceeSAkira Hatanaka // No need to allocate space on the stack.
56941a705bSMatthias Braun if (StackSize == 0 && !MFI.adjustsStack()) return;
57d1c43ceeSAkira Hatanaka
58d11acc7dSReed Kotler MachineModuleInfo &MMI = MF.getMMI();
59bc07a890SBill Wendling const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
60d11acc7dSReed Kotler
61d1c43ceeSAkira Hatanaka // Adjust stack.
62d019dbf7SReed Kotler TII.makeFrame(Mips::SP, StackSize, MBB, MBBI);
633589dd74SReed Kotler
64d11acc7dSReed Kotler // emit ".cfi_def_cfa_offset StackSize"
650840d725SFangrui Song unsigned CFIIndex =
660840d725SFangrui Song MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, StackSize));
67b1f25f1bSRafael Espindola BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
68b1f25f1bSRafael Espindola .addCFIIndex(CFIIndex);
69d11acc7dSReed Kotler
70941a705bSMatthias Braun const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
715c29d63aSReed Kotler
724e9736b1SEugene Zelenko if (!CSI.empty()) {
73941a705bSMatthias Braun const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
740ff40017SReed Kotler
75fc981cedSKazu Hirata for (const CalleeSavedInfo &I : CSI) {
76fc981cedSKazu Hirata int64_t Offset = MFI.getObjectOffset(I.getFrameIdx());
77*d6b07348SJim Lin Register Reg = I.getReg();
785c29d63aSReed Kotler unsigned DReg = MRI->getDwarfRegNum(Reg, true);
79f23ef437SMatthias Braun unsigned CFIIndex = MF.addFrameInst(
80b1f25f1bSRafael Espindola MCCFIInstruction::createOffset(nullptr, DReg, Offset));
81b1f25f1bSRafael Espindola BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
82b1f25f1bSRafael Espindola .addCFIIndex(CFIIndex);
830ff40017SReed Kotler }
845c29d63aSReed Kotler }
853589dd74SReed Kotler if (hasFP(MF))
863589dd74SReed Kotler BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0)
87b45b4814SEric Christopher .addReg(Mips::SP).setMIFlag(MachineInstr::FrameSetup);
88d1c43ceeSAkira Hatanaka }
89d1c43ceeSAkira Hatanaka
emitEpilogue(MachineFunction & MF,MachineBasicBlock & MBB) const90d1c43ceeSAkira Hatanaka void Mips16FrameLowering::emitEpilogue(MachineFunction &MF,
91d1c43ceeSAkira Hatanaka MachineBasicBlock &MBB) const {
92cccc236aSPetar Jovanovic MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
93941a705bSMatthias Braun MachineFrameInfo &MFI = MF.getFrameInfo();
94d019dbf7SReed Kotler const Mips16InstrInfo &TII =
9596e72c6aSEric Christopher *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
96cccc236aSPetar Jovanovic DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
97941a705bSMatthias Braun uint64_t StackSize = MFI.getStackSize();
98d1c43ceeSAkira Hatanaka
99d1c43ceeSAkira Hatanaka if (!StackSize)
100d1c43ceeSAkira Hatanaka return;
101d1c43ceeSAkira Hatanaka
1023589dd74SReed Kotler if (hasFP(MF))
1033589dd74SReed Kotler BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP)
1043589dd74SReed Kotler .addReg(Mips::S0);
1053589dd74SReed Kotler
106d1c43ceeSAkira Hatanaka // Adjust stack.
107d1c43ceeSAkira Hatanaka // assumes stacksize multiple of 8
108d019dbf7SReed Kotler TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI);
109d1c43ceeSAkira Hatanaka }
110d1c43ceeSAkira Hatanaka
spillCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,ArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI) const111e4230a9fSBenjamin Kramer bool Mips16FrameLowering::spillCalleeSavedRegisters(
112e4230a9fSBenjamin Kramer MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
113e4230a9fSBenjamin Kramer ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
114cd04e2b8SAkira Hatanaka MachineFunction *MF = MBB.getParent();
115cd04e2b8SAkira Hatanaka
116cd04e2b8SAkira Hatanaka //
117cd04e2b8SAkira Hatanaka // Registers RA, S0,S1 are the callee saved registers and they
118cd04e2b8SAkira Hatanaka // will be saved with the "save" instruction
119cd04e2b8SAkira Hatanaka // during emitPrologue
120cd04e2b8SAkira Hatanaka //
121fc981cedSKazu Hirata for (const CalleeSavedInfo &I : CSI) {
122cd04e2b8SAkira Hatanaka // Add the callee-saved register as live-in. Do not add if the register is
123cd04e2b8SAkira Hatanaka // RA and return address is taken, because it has already been added in
12494ed30a4SDaniel Sanders // method MipsTargetLowering::lowerRETURNADDR.
125cd04e2b8SAkira Hatanaka // It's killed at the spill, unless the register is RA and return address
126cd04e2b8SAkira Hatanaka // is taken.
127*d6b07348SJim Lin Register Reg = I.getReg();
128cd04e2b8SAkira Hatanaka bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA)
129941a705bSMatthias Braun && MF->getFrameInfo().isReturnAddressTaken();
130cd04e2b8SAkira Hatanaka if (!IsRAAndRetAddrIsTaken)
131cccc236aSPetar Jovanovic MBB.addLiveIn(Reg);
132cd04e2b8SAkira Hatanaka }
133cd04e2b8SAkira Hatanaka
134cd04e2b8SAkira Hatanaka return true;
135cd04e2b8SAkira Hatanaka }
136cd04e2b8SAkira Hatanaka
restoreCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,MutableArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI) const137186dd631SBenjamin Kramer bool Mips16FrameLowering::restoreCalleeSavedRegisters(
138186dd631SBenjamin Kramer MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
139186dd631SBenjamin Kramer MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
140cd04e2b8SAkira Hatanaka //
141cd04e2b8SAkira Hatanaka // Registers RA,S0,S1 are the callee saved registers and they will be restored
142cd04e2b8SAkira Hatanaka // with the restore instruction during emitEpilogue.
143cd04e2b8SAkira Hatanaka // We need to override this virtual function, otherwise llvm will try and
144cd04e2b8SAkira Hatanaka // restore the registers on it's on from the stack.
145cd04e2b8SAkira Hatanaka //
146cd04e2b8SAkira Hatanaka
147d1c43ceeSAkira Hatanaka return true;
148d1c43ceeSAkira Hatanaka }
149d1c43ceeSAkira Hatanaka
150d1c43ceeSAkira Hatanaka bool
hasReservedCallFrame(const MachineFunction & MF) const151d1c43ceeSAkira Hatanaka Mips16FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
152941a705bSMatthias Braun const MachineFrameInfo &MFI = MF.getFrameInfo();
15327a7229cSReed Kotler // Reserve call frame if the size of the maximum call frame fits into 15-bit
15427a7229cSReed Kotler // immediate field and there are no variable sized objects on the stack.
155941a705bSMatthias Braun return isInt<15>(MFI.getMaxCallFrameSize()) && !MFI.hasVarSizedObjects();
156d1c43ceeSAkira Hatanaka }
157d1c43ceeSAkira Hatanaka
determineCalleeSaves(MachineFunction & MF,BitVector & SavedRegs,RegScavenger * RS) const15802564865SMatthias Braun void Mips16FrameLowering::determineCalleeSaves(MachineFunction &MF,
15902564865SMatthias Braun BitVector &SavedRegs,
160d1c43ceeSAkira Hatanaka RegScavenger *RS) const {
16102564865SMatthias Braun TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
1625c29d63aSReed Kotler const Mips16InstrInfo &TII =
16396e72c6aSEric Christopher *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
1645c29d63aSReed Kotler const MipsRegisterInfo &RI = TII.getRegisterInfo();
1655c29d63aSReed Kotler const BitVector Reserved = RI.getReservedRegs(MF);
1665c29d63aSReed Kotler bool SaveS2 = Reserved[Mips::S2];
1675c29d63aSReed Kotler if (SaveS2)
16802564865SMatthias Braun SavedRegs.set(Mips::S2);
1695c29d63aSReed Kotler if (hasFP(MF))
17002564865SMatthias Braun SavedRegs.set(Mips::S0);
171d1c43ceeSAkira Hatanaka }
172fab89294SAkira Hatanaka
173fab89294SAkira Hatanaka const MipsFrameLowering *
createMips16FrameLowering(const MipsSubtarget & ST)174fab89294SAkira Hatanaka llvm::createMips16FrameLowering(const MipsSubtarget &ST) {
175fab89294SAkira Hatanaka return new Mips16FrameLowering(ST);
176fab89294SAkira Hatanaka }
177