xref: /freebsd/contrib/llvm-project/llvm/lib/Target/Mips/MipsSEFrameLowering.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- MipsSEFrameLowering.cpp - Mips32/64 Frame Information --------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains the Mips32/64 implementation of TargetFrameLowering class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "MipsSEFrameLowering.h"
140b57cec5SDimitry Andric #include "MCTargetDesc/MipsABIInfo.h"
150b57cec5SDimitry Andric #include "MipsMachineFunction.h"
160b57cec5SDimitry Andric #include "MipsRegisterInfo.h"
170b57cec5SDimitry Andric #include "MipsSEInstrInfo.h"
180b57cec5SDimitry Andric #include "MipsSubtarget.h"
190b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h"
200b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
210b57cec5SDimitry Andric #include "llvm/ADT/StringSwitch.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
340b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h"
350b57cec5SDimitry Andric #include "llvm/IR/Function.h"
360b57cec5SDimitry Andric #include "llvm/MC/MCDwarf.h"
370b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
380b57cec5SDimitry Andric #include "llvm/MC/MachineLocation.h"
390b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h"
400b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
410b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
420b57cec5SDimitry Andric #include <cassert>
430b57cec5SDimitry Andric #include <cstdint>
440b57cec5SDimitry Andric #include <utility>
450b57cec5SDimitry Andric #include <vector>
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric using namespace llvm;
480b57cec5SDimitry Andric 
getMFHiLoOpc(unsigned Src)490b57cec5SDimitry Andric static std::pair<unsigned, unsigned> getMFHiLoOpc(unsigned Src) {
500b57cec5SDimitry Andric   if (Mips::ACC64RegClass.contains(Src))
510b57cec5SDimitry Andric     return std::make_pair((unsigned)Mips::PseudoMFHI,
520b57cec5SDimitry Andric                           (unsigned)Mips::PseudoMFLO);
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric   if (Mips::ACC64DSPRegClass.contains(Src))
550b57cec5SDimitry Andric     return std::make_pair((unsigned)Mips::MFHI_DSP, (unsigned)Mips::MFLO_DSP);
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric   if (Mips::ACC128RegClass.contains(Src))
580b57cec5SDimitry Andric     return std::make_pair((unsigned)Mips::PseudoMFHI64,
590b57cec5SDimitry Andric                           (unsigned)Mips::PseudoMFLO64);
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric   return std::make_pair(0, 0);
620b57cec5SDimitry Andric }
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric namespace {
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric /// Helper class to expand pseudos.
670b57cec5SDimitry Andric class ExpandPseudo {
680b57cec5SDimitry Andric public:
690b57cec5SDimitry Andric   ExpandPseudo(MachineFunction &MF);
700b57cec5SDimitry Andric   bool expand();
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric private:
730b57cec5SDimitry Andric   using Iter = MachineBasicBlock::iterator;
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric   bool expandInstr(MachineBasicBlock &MBB, Iter I);
760b57cec5SDimitry Andric   void expandLoadCCond(MachineBasicBlock &MBB, Iter I);
770b57cec5SDimitry Andric   void expandStoreCCond(MachineBasicBlock &MBB, Iter I);
780b57cec5SDimitry Andric   void expandLoadACC(MachineBasicBlock &MBB, Iter I, unsigned RegSize);
790b57cec5SDimitry Andric   void expandStoreACC(MachineBasicBlock &MBB, Iter I, unsigned MFHiOpc,
800b57cec5SDimitry Andric                       unsigned MFLoOpc, unsigned RegSize);
810b57cec5SDimitry Andric   bool expandCopy(MachineBasicBlock &MBB, Iter I);
820b57cec5SDimitry Andric   bool expandCopyACC(MachineBasicBlock &MBB, Iter I, unsigned MFHiOpc,
830b57cec5SDimitry Andric                      unsigned MFLoOpc);
840b57cec5SDimitry Andric   bool expandBuildPairF64(MachineBasicBlock &MBB,
850b57cec5SDimitry Andric                           MachineBasicBlock::iterator I, bool FP64) const;
860b57cec5SDimitry Andric   bool expandExtractElementF64(MachineBasicBlock &MBB,
870b57cec5SDimitry Andric                                MachineBasicBlock::iterator I, bool FP64) const;
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric   MachineFunction &MF;
900b57cec5SDimitry Andric   MachineRegisterInfo &MRI;
910b57cec5SDimitry Andric   const MipsSubtarget &Subtarget;
920b57cec5SDimitry Andric   const MipsSEInstrInfo &TII;
930b57cec5SDimitry Andric   const MipsRegisterInfo &RegInfo;
940b57cec5SDimitry Andric };
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric } // end anonymous namespace
970b57cec5SDimitry Andric 
ExpandPseudo(MachineFunction & MF_)980b57cec5SDimitry Andric ExpandPseudo::ExpandPseudo(MachineFunction &MF_)
990b57cec5SDimitry Andric     : MF(MF_), MRI(MF.getRegInfo()),
10081ad6265SDimitry Andric       Subtarget(MF.getSubtarget<MipsSubtarget>()),
1010b57cec5SDimitry Andric       TII(*static_cast<const MipsSEInstrInfo *>(Subtarget.getInstrInfo())),
1020b57cec5SDimitry Andric       RegInfo(*Subtarget.getRegisterInfo()) {}
1030b57cec5SDimitry Andric 
expand()1040b57cec5SDimitry Andric bool ExpandPseudo::expand() {
1050b57cec5SDimitry Andric   bool Expanded = false;
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric   for (auto &MBB : MF) {
1080b57cec5SDimitry Andric     for (Iter I = MBB.begin(), End = MBB.end(); I != End;)
1090b57cec5SDimitry Andric       Expanded |= expandInstr(MBB, I++);
1100b57cec5SDimitry Andric   }
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric   return Expanded;
1130b57cec5SDimitry Andric }
1140b57cec5SDimitry Andric 
expandInstr(MachineBasicBlock & MBB,Iter I)1150b57cec5SDimitry Andric bool ExpandPseudo::expandInstr(MachineBasicBlock &MBB, Iter I) {
1160b57cec5SDimitry Andric   switch(I->getOpcode()) {
1170b57cec5SDimitry Andric   case Mips::LOAD_CCOND_DSP:
1180b57cec5SDimitry Andric     expandLoadCCond(MBB, I);
1190b57cec5SDimitry Andric     break;
1200b57cec5SDimitry Andric   case Mips::STORE_CCOND_DSP:
1210b57cec5SDimitry Andric     expandStoreCCond(MBB, I);
1220b57cec5SDimitry Andric     break;
1230b57cec5SDimitry Andric   case Mips::LOAD_ACC64:
1240b57cec5SDimitry Andric   case Mips::LOAD_ACC64DSP:
1250b57cec5SDimitry Andric     expandLoadACC(MBB, I, 4);
1260b57cec5SDimitry Andric     break;
1270b57cec5SDimitry Andric   case Mips::LOAD_ACC128:
1280b57cec5SDimitry Andric     expandLoadACC(MBB, I, 8);
1290b57cec5SDimitry Andric     break;
1300b57cec5SDimitry Andric   case Mips::STORE_ACC64:
1310b57cec5SDimitry Andric     expandStoreACC(MBB, I, Mips::PseudoMFHI, Mips::PseudoMFLO, 4);
1320b57cec5SDimitry Andric     break;
1330b57cec5SDimitry Andric   case Mips::STORE_ACC64DSP:
1340b57cec5SDimitry Andric     expandStoreACC(MBB, I, Mips::MFHI_DSP, Mips::MFLO_DSP, 4);
1350b57cec5SDimitry Andric     break;
1360b57cec5SDimitry Andric   case Mips::STORE_ACC128:
1370b57cec5SDimitry Andric     expandStoreACC(MBB, I, Mips::PseudoMFHI64, Mips::PseudoMFLO64, 8);
1380b57cec5SDimitry Andric     break;
1390b57cec5SDimitry Andric   case Mips::BuildPairF64:
1400b57cec5SDimitry Andric     if (expandBuildPairF64(MBB, I, false))
1410b57cec5SDimitry Andric       MBB.erase(I);
1420b57cec5SDimitry Andric     return false;
1430b57cec5SDimitry Andric   case Mips::BuildPairF64_64:
1440b57cec5SDimitry Andric     if (expandBuildPairF64(MBB, I, true))
1450b57cec5SDimitry Andric       MBB.erase(I);
1460b57cec5SDimitry Andric     return false;
1470b57cec5SDimitry Andric   case Mips::ExtractElementF64:
1480b57cec5SDimitry Andric     if (expandExtractElementF64(MBB, I, false))
1490b57cec5SDimitry Andric       MBB.erase(I);
1500b57cec5SDimitry Andric     return false;
1510b57cec5SDimitry Andric   case Mips::ExtractElementF64_64:
1520b57cec5SDimitry Andric     if (expandExtractElementF64(MBB, I, true))
1530b57cec5SDimitry Andric       MBB.erase(I);
1540b57cec5SDimitry Andric     return false;
1550b57cec5SDimitry Andric   case TargetOpcode::COPY:
1560b57cec5SDimitry Andric     if (!expandCopy(MBB, I))
1570b57cec5SDimitry Andric       return false;
1580b57cec5SDimitry Andric     break;
1590b57cec5SDimitry Andric   default:
1600b57cec5SDimitry Andric     return false;
1610b57cec5SDimitry Andric   }
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric   MBB.erase(I);
1640b57cec5SDimitry Andric   return true;
1650b57cec5SDimitry Andric }
1660b57cec5SDimitry Andric 
expandLoadCCond(MachineBasicBlock & MBB,Iter I)1670b57cec5SDimitry Andric void ExpandPseudo::expandLoadCCond(MachineBasicBlock &MBB, Iter I) {
1680b57cec5SDimitry Andric   //  load $vr, FI
1690b57cec5SDimitry Andric   //  copy ccond, $vr
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric   assert(I->getOperand(0).isReg() && I->getOperand(1).isFI());
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric   const TargetRegisterClass *RC = RegInfo.intRegClass(4);
1748bcb0991SDimitry Andric   Register VR = MRI.createVirtualRegister(RC);
1758bcb0991SDimitry Andric   Register Dst = I->getOperand(0).getReg(), FI = I->getOperand(1).getIndex();
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric   TII.loadRegFromStack(MBB, I, VR, FI, RC, &RegInfo, 0);
1780b57cec5SDimitry Andric   BuildMI(MBB, I, I->getDebugLoc(), TII.get(TargetOpcode::COPY), Dst)
1790b57cec5SDimitry Andric     .addReg(VR, RegState::Kill);
1800b57cec5SDimitry Andric }
1810b57cec5SDimitry Andric 
expandStoreCCond(MachineBasicBlock & MBB,Iter I)1820b57cec5SDimitry Andric void ExpandPseudo::expandStoreCCond(MachineBasicBlock &MBB, Iter I) {
1830b57cec5SDimitry Andric   //  copy $vr, ccond
1840b57cec5SDimitry Andric   //  store $vr, FI
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric   assert(I->getOperand(0).isReg() && I->getOperand(1).isFI());
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric   const TargetRegisterClass *RC = RegInfo.intRegClass(4);
1898bcb0991SDimitry Andric   Register VR = MRI.createVirtualRegister(RC);
1908bcb0991SDimitry Andric   Register Src = I->getOperand(0).getReg(), FI = I->getOperand(1).getIndex();
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric   BuildMI(MBB, I, I->getDebugLoc(), TII.get(TargetOpcode::COPY), VR)
1930b57cec5SDimitry Andric     .addReg(Src, getKillRegState(I->getOperand(0).isKill()));
1940b57cec5SDimitry Andric   TII.storeRegToStack(MBB, I, VR, true, FI, RC, &RegInfo, 0);
1950b57cec5SDimitry Andric }
1960b57cec5SDimitry Andric 
expandLoadACC(MachineBasicBlock & MBB,Iter I,unsigned RegSize)1970b57cec5SDimitry Andric void ExpandPseudo::expandLoadACC(MachineBasicBlock &MBB, Iter I,
1980b57cec5SDimitry Andric                                  unsigned RegSize) {
1990b57cec5SDimitry Andric   //  load $vr0, FI
2000b57cec5SDimitry Andric   //  copy lo, $vr0
2010b57cec5SDimitry Andric   //  load $vr1, FI + 4
2020b57cec5SDimitry Andric   //  copy hi, $vr1
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric   assert(I->getOperand(0).isReg() && I->getOperand(1).isFI());
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric   const TargetRegisterClass *RC = RegInfo.intRegClass(RegSize);
2078bcb0991SDimitry Andric   Register VR0 = MRI.createVirtualRegister(RC);
2088bcb0991SDimitry Andric   Register VR1 = MRI.createVirtualRegister(RC);
2098bcb0991SDimitry Andric   Register Dst = I->getOperand(0).getReg(), FI = I->getOperand(1).getIndex();
2108bcb0991SDimitry Andric   Register Lo = RegInfo.getSubReg(Dst, Mips::sub_lo);
2118bcb0991SDimitry Andric   Register Hi = RegInfo.getSubReg(Dst, Mips::sub_hi);
2120b57cec5SDimitry Andric   DebugLoc DL = I->getDebugLoc();
2130b57cec5SDimitry Andric   const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY);
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric   TII.loadRegFromStack(MBB, I, VR0, FI, RC, &RegInfo, 0);
2160b57cec5SDimitry Andric   BuildMI(MBB, I, DL, Desc, Lo).addReg(VR0, RegState::Kill);
2170b57cec5SDimitry Andric   TII.loadRegFromStack(MBB, I, VR1, FI, RC, &RegInfo, RegSize);
2180b57cec5SDimitry Andric   BuildMI(MBB, I, DL, Desc, Hi).addReg(VR1, RegState::Kill);
2190b57cec5SDimitry Andric }
2200b57cec5SDimitry Andric 
expandStoreACC(MachineBasicBlock & MBB,Iter I,unsigned MFHiOpc,unsigned MFLoOpc,unsigned RegSize)2210b57cec5SDimitry Andric void ExpandPseudo::expandStoreACC(MachineBasicBlock &MBB, Iter I,
2220b57cec5SDimitry Andric                                   unsigned MFHiOpc, unsigned MFLoOpc,
2230b57cec5SDimitry Andric                                   unsigned RegSize) {
2240b57cec5SDimitry Andric   //  mflo $vr0, src
2250b57cec5SDimitry Andric   //  store $vr0, FI
2260b57cec5SDimitry Andric   //  mfhi $vr1, src
2270b57cec5SDimitry Andric   //  store $vr1, FI + 4
2280b57cec5SDimitry Andric 
2290b57cec5SDimitry Andric   assert(I->getOperand(0).isReg() && I->getOperand(1).isFI());
2300b57cec5SDimitry Andric 
2310b57cec5SDimitry Andric   const TargetRegisterClass *RC = RegInfo.intRegClass(RegSize);
2328bcb0991SDimitry Andric   Register VR0 = MRI.createVirtualRegister(RC);
2338bcb0991SDimitry Andric   Register VR1 = MRI.createVirtualRegister(RC);
2348bcb0991SDimitry Andric   Register Src = I->getOperand(0).getReg(), FI = I->getOperand(1).getIndex();
2350b57cec5SDimitry Andric   unsigned SrcKill = getKillRegState(I->getOperand(0).isKill());
2360b57cec5SDimitry Andric   DebugLoc DL = I->getDebugLoc();
2370b57cec5SDimitry Andric 
2380b57cec5SDimitry Andric   BuildMI(MBB, I, DL, TII.get(MFLoOpc), VR0).addReg(Src);
2390b57cec5SDimitry Andric   TII.storeRegToStack(MBB, I, VR0, true, FI, RC, &RegInfo, 0);
2400b57cec5SDimitry Andric   BuildMI(MBB, I, DL, TII.get(MFHiOpc), VR1).addReg(Src, SrcKill);
2410b57cec5SDimitry Andric   TII.storeRegToStack(MBB, I, VR1, true, FI, RC, &RegInfo, RegSize);
2420b57cec5SDimitry Andric }
2430b57cec5SDimitry Andric 
expandCopy(MachineBasicBlock & MBB,Iter I)2440b57cec5SDimitry Andric bool ExpandPseudo::expandCopy(MachineBasicBlock &MBB, Iter I) {
2458bcb0991SDimitry Andric   Register Src = I->getOperand(1).getReg();
2460b57cec5SDimitry Andric   std::pair<unsigned, unsigned> Opcodes = getMFHiLoOpc(Src);
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric   if (!Opcodes.first)
2490b57cec5SDimitry Andric     return false;
2500b57cec5SDimitry Andric 
2510b57cec5SDimitry Andric   return expandCopyACC(MBB, I, Opcodes.first, Opcodes.second);
2520b57cec5SDimitry Andric }
2530b57cec5SDimitry Andric 
expandCopyACC(MachineBasicBlock & MBB,Iter I,unsigned MFHiOpc,unsigned MFLoOpc)2540b57cec5SDimitry Andric bool ExpandPseudo::expandCopyACC(MachineBasicBlock &MBB, Iter I,
2550b57cec5SDimitry Andric                                  unsigned MFHiOpc, unsigned MFLoOpc) {
2560b57cec5SDimitry Andric   //  mflo $vr0, src
2570b57cec5SDimitry Andric   //  copy dst_lo, $vr0
2580b57cec5SDimitry Andric   //  mfhi $vr1, src
2590b57cec5SDimitry Andric   //  copy dst_hi, $vr1
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric   unsigned Dst = I->getOperand(0).getReg(), Src = I->getOperand(1).getReg();
2620b57cec5SDimitry Andric   const TargetRegisterClass *DstRC = RegInfo.getMinimalPhysRegClass(Dst);
2630b57cec5SDimitry Andric   unsigned VRegSize = RegInfo.getRegSizeInBits(*DstRC) / 16;
2640b57cec5SDimitry Andric   const TargetRegisterClass *RC = RegInfo.intRegClass(VRegSize);
2658bcb0991SDimitry Andric   Register VR0 = MRI.createVirtualRegister(RC);
2668bcb0991SDimitry Andric   Register VR1 = MRI.createVirtualRegister(RC);
2670b57cec5SDimitry Andric   unsigned SrcKill = getKillRegState(I->getOperand(1).isKill());
2688bcb0991SDimitry Andric   Register DstLo = RegInfo.getSubReg(Dst, Mips::sub_lo);
2698bcb0991SDimitry Andric   Register DstHi = RegInfo.getSubReg(Dst, Mips::sub_hi);
2700b57cec5SDimitry Andric   DebugLoc DL = I->getDebugLoc();
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric   BuildMI(MBB, I, DL, TII.get(MFLoOpc), VR0).addReg(Src);
2730b57cec5SDimitry Andric   BuildMI(MBB, I, DL, TII.get(TargetOpcode::COPY), DstLo)
2740b57cec5SDimitry Andric     .addReg(VR0, RegState::Kill);
2750b57cec5SDimitry Andric   BuildMI(MBB, I, DL, TII.get(MFHiOpc), VR1).addReg(Src, SrcKill);
2760b57cec5SDimitry Andric   BuildMI(MBB, I, DL, TII.get(TargetOpcode::COPY), DstHi)
2770b57cec5SDimitry Andric     .addReg(VR1, RegState::Kill);
2780b57cec5SDimitry Andric   return true;
2790b57cec5SDimitry Andric }
2800b57cec5SDimitry Andric 
2810b57cec5SDimitry Andric /// This method expands the same instruction that MipsSEInstrInfo::
2820b57cec5SDimitry Andric /// expandBuildPairF64 does, for the case when ABI is fpxx and mthc1 is not
2830b57cec5SDimitry Andric /// available and the case where the ABI is FP64A. It is implemented here
2840b57cec5SDimitry Andric /// because frame indexes are eliminated before MipsSEInstrInfo::
2850b57cec5SDimitry Andric /// expandBuildPairF64 is called.
expandBuildPairF64(MachineBasicBlock & MBB,MachineBasicBlock::iterator I,bool FP64) const2860b57cec5SDimitry Andric bool ExpandPseudo::expandBuildPairF64(MachineBasicBlock &MBB,
2870b57cec5SDimitry Andric                                       MachineBasicBlock::iterator I,
2880b57cec5SDimitry Andric                                       bool FP64) const {
2890b57cec5SDimitry Andric   // For fpxx and when mthc1 is not available, use:
2900b57cec5SDimitry Andric   //   spill + reload via ldc1
2910b57cec5SDimitry Andric   //
2920b57cec5SDimitry Andric   // The case where dmtc1 is available doesn't need to be handled here
2930b57cec5SDimitry Andric   // because it never creates a BuildPairF64 node.
2940b57cec5SDimitry Andric   //
2950b57cec5SDimitry Andric   // The FP64A ABI (fp64 with nooddspreg) must also use a spill/reload sequence
2960b57cec5SDimitry Andric   // for odd-numbered double precision values (because the lower 32-bits is
2970b57cec5SDimitry Andric   // transferred with mtc1 which is redirected to the upper half of the even
2980b57cec5SDimitry Andric   // register). Unfortunately, we have to make this decision before register
2990b57cec5SDimitry Andric   // allocation so for now we use a spill/reload sequence for all
3000b57cec5SDimitry Andric   // double-precision values in regardless of being an odd/even register.
3010b57cec5SDimitry Andric   //
3020b57cec5SDimitry Andric   // For the cases that should be covered here MipsSEISelDAGToDAG adds $sp as
3030b57cec5SDimitry Andric   // implicit operand, so other passes (like ShrinkWrapping) are aware that
3040b57cec5SDimitry Andric   // stack is used.
3050b57cec5SDimitry Andric   if (I->getNumOperands() == 4 && I->getOperand(3).isReg()
3060b57cec5SDimitry Andric       && I->getOperand(3).getReg() == Mips::SP) {
3078bcb0991SDimitry Andric     Register DstReg = I->getOperand(0).getReg();
3088bcb0991SDimitry Andric     Register LoReg = I->getOperand(1).getReg();
3098bcb0991SDimitry Andric     Register HiReg = I->getOperand(2).getReg();
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric     // It should be impossible to have FGR64 on MIPS-II or MIPS32r1 (which are
3120b57cec5SDimitry Andric     // the cases where mthc1 is not available). 64-bit architectures and
3130b57cec5SDimitry Andric     // MIPS32r2 or later can use FGR64 though.
3140b57cec5SDimitry Andric     assert(Subtarget.isGP64bit() || Subtarget.hasMTHC1() ||
3150b57cec5SDimitry Andric            !Subtarget.isFP64bit());
3160b57cec5SDimitry Andric 
3170b57cec5SDimitry Andric     const TargetRegisterClass *RC = &Mips::GPR32RegClass;
3180b57cec5SDimitry Andric     const TargetRegisterClass *RC2 =
3190b57cec5SDimitry Andric         FP64 ? &Mips::FGR64RegClass : &Mips::AFGR64RegClass;
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric     // We re-use the same spill slot each time so that the stack frame doesn't
3220b57cec5SDimitry Andric     // grow too much in functions with a large number of moves.
3235ffd83dbSDimitry Andric     int FI = MF.getInfo<MipsFunctionInfo>()->getMoveF64ViaSpillFI(MF, RC2);
3240b57cec5SDimitry Andric     if (!Subtarget.isLittle())
3250b57cec5SDimitry Andric       std::swap(LoReg, HiReg);
3260b57cec5SDimitry Andric     TII.storeRegToStack(MBB, I, LoReg, I->getOperand(1).isKill(), FI, RC,
3270b57cec5SDimitry Andric                         &RegInfo, 0);
3280b57cec5SDimitry Andric     TII.storeRegToStack(MBB, I, HiReg, I->getOperand(2).isKill(), FI, RC,
3290b57cec5SDimitry Andric                         &RegInfo, 4);
3300b57cec5SDimitry Andric     TII.loadRegFromStack(MBB, I, DstReg, FI, RC2, &RegInfo, 0);
3310b57cec5SDimitry Andric     return true;
3320b57cec5SDimitry Andric   }
3330b57cec5SDimitry Andric 
3340b57cec5SDimitry Andric   return false;
3350b57cec5SDimitry Andric }
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric /// This method expands the same instruction that MipsSEInstrInfo::
3380b57cec5SDimitry Andric /// expandExtractElementF64 does, for the case when ABI is fpxx and mfhc1 is not
3390b57cec5SDimitry Andric /// available and the case where the ABI is FP64A. It is implemented here
3400b57cec5SDimitry Andric /// because frame indexes are eliminated before MipsSEInstrInfo::
3410b57cec5SDimitry Andric /// expandExtractElementF64 is called.
expandExtractElementF64(MachineBasicBlock & MBB,MachineBasicBlock::iterator I,bool FP64) const3420b57cec5SDimitry Andric bool ExpandPseudo::expandExtractElementF64(MachineBasicBlock &MBB,
3430b57cec5SDimitry Andric                                            MachineBasicBlock::iterator I,
3440b57cec5SDimitry Andric                                            bool FP64) const {
3450b57cec5SDimitry Andric   const MachineOperand &Op1 = I->getOperand(1);
3460b57cec5SDimitry Andric   const MachineOperand &Op2 = I->getOperand(2);
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric   if ((Op1.isReg() && Op1.isUndef()) || (Op2.isReg() && Op2.isUndef())) {
3498bcb0991SDimitry Andric     Register DstReg = I->getOperand(0).getReg();
3500b57cec5SDimitry Andric     BuildMI(MBB, I, I->getDebugLoc(), TII.get(Mips::IMPLICIT_DEF), DstReg);
3510b57cec5SDimitry Andric     return true;
3520b57cec5SDimitry Andric   }
3530b57cec5SDimitry Andric 
3540b57cec5SDimitry Andric   // For fpxx and when mfhc1 is not available, use:
3550b57cec5SDimitry Andric   //   spill + reload via ldc1
3560b57cec5SDimitry Andric   //
3570b57cec5SDimitry Andric   // The case where dmfc1 is available doesn't need to be handled here
3580b57cec5SDimitry Andric   // because it never creates a ExtractElementF64 node.
3590b57cec5SDimitry Andric   //
3600b57cec5SDimitry Andric   // The FP64A ABI (fp64 with nooddspreg) must also use a spill/reload sequence
3610b57cec5SDimitry Andric   // for odd-numbered double precision values (because the lower 32-bits is
3620b57cec5SDimitry Andric   // transferred with mfc1 which is redirected to the upper half of the even
3630b57cec5SDimitry Andric   // register). Unfortunately, we have to make this decision before register
3640b57cec5SDimitry Andric   // allocation so for now we use a spill/reload sequence for all
3650b57cec5SDimitry Andric   // double-precision values in regardless of being an odd/even register.
3660b57cec5SDimitry Andric   //
3670b57cec5SDimitry Andric   // For the cases that should be covered here MipsSEISelDAGToDAG adds $sp as
3680b57cec5SDimitry Andric   // implicit operand, so other passes (like ShrinkWrapping) are aware that
3690b57cec5SDimitry Andric   // stack is used.
3700b57cec5SDimitry Andric   if (I->getNumOperands() == 4 && I->getOperand(3).isReg()
3710b57cec5SDimitry Andric       && I->getOperand(3).getReg() == Mips::SP) {
3728bcb0991SDimitry Andric     Register DstReg = I->getOperand(0).getReg();
3738bcb0991SDimitry Andric     Register SrcReg = Op1.getReg();
3740b57cec5SDimitry Andric     unsigned N = Op2.getImm();
3750b57cec5SDimitry Andric     int64_t Offset = 4 * (Subtarget.isLittle() ? N : (1 - N));
3760b57cec5SDimitry Andric 
3770b57cec5SDimitry Andric     // It should be impossible to have FGR64 on MIPS-II or MIPS32r1 (which are
3780b57cec5SDimitry Andric     // the cases where mfhc1 is not available). 64-bit architectures and
3790b57cec5SDimitry Andric     // MIPS32r2 or later can use FGR64 though.
3800b57cec5SDimitry Andric     assert(Subtarget.isGP64bit() || Subtarget.hasMTHC1() ||
3810b57cec5SDimitry Andric            !Subtarget.isFP64bit());
3820b57cec5SDimitry Andric 
3830b57cec5SDimitry Andric     const TargetRegisterClass *RC =
3840b57cec5SDimitry Andric         FP64 ? &Mips::FGR64RegClass : &Mips::AFGR64RegClass;
3850b57cec5SDimitry Andric     const TargetRegisterClass *RC2 = &Mips::GPR32RegClass;
3860b57cec5SDimitry Andric 
3870b57cec5SDimitry Andric     // We re-use the same spill slot each time so that the stack frame doesn't
3880b57cec5SDimitry Andric     // grow too much in functions with a large number of moves.
3895ffd83dbSDimitry Andric     int FI = MF.getInfo<MipsFunctionInfo>()->getMoveF64ViaSpillFI(MF, RC);
3900b57cec5SDimitry Andric     TII.storeRegToStack(MBB, I, SrcReg, Op1.isKill(), FI, RC, &RegInfo, 0);
3910b57cec5SDimitry Andric     TII.loadRegFromStack(MBB, I, DstReg, FI, RC2, &RegInfo, Offset);
3920b57cec5SDimitry Andric     return true;
3930b57cec5SDimitry Andric   }
3940b57cec5SDimitry Andric 
3950b57cec5SDimitry Andric   return false;
3960b57cec5SDimitry Andric }
3970b57cec5SDimitry Andric 
MipsSEFrameLowering(const MipsSubtarget & STI)3980b57cec5SDimitry Andric MipsSEFrameLowering::MipsSEFrameLowering(const MipsSubtarget &STI)
3990b57cec5SDimitry Andric     : MipsFrameLowering(STI, STI.getStackAlignment()) {}
4000b57cec5SDimitry Andric 
emitPrologue(MachineFunction & MF,MachineBasicBlock & MBB) const4010b57cec5SDimitry Andric void MipsSEFrameLowering::emitPrologue(MachineFunction &MF,
4020b57cec5SDimitry Andric                                        MachineBasicBlock &MBB) const {
4030b57cec5SDimitry Andric   MachineFrameInfo &MFI    = MF.getFrameInfo();
4040b57cec5SDimitry Andric   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
4050b57cec5SDimitry Andric 
4060b57cec5SDimitry Andric   const MipsSEInstrInfo &TII =
4070b57cec5SDimitry Andric       *static_cast<const MipsSEInstrInfo *>(STI.getInstrInfo());
4080b57cec5SDimitry Andric   const MipsRegisterInfo &RegInfo =
4090b57cec5SDimitry Andric       *static_cast<const MipsRegisterInfo *>(STI.getRegisterInfo());
4100b57cec5SDimitry Andric 
4110b57cec5SDimitry Andric   MachineBasicBlock::iterator MBBI = MBB.begin();
4120b57cec5SDimitry Andric   DebugLoc dl;
4130b57cec5SDimitry Andric   MipsABIInfo ABI = STI.getABI();
4140b57cec5SDimitry Andric   unsigned SP = ABI.GetStackPtr();
4150b57cec5SDimitry Andric   unsigned FP = ABI.GetFramePtr();
4160b57cec5SDimitry Andric   unsigned ZERO = ABI.GetNullPtr();
4170b57cec5SDimitry Andric   unsigned MOVE = ABI.GetGPRMoveOp();
4180b57cec5SDimitry Andric   unsigned ADDiu = ABI.GetPtrAddiuOp();
4190b57cec5SDimitry Andric   unsigned AND = ABI.IsN64() ? Mips::AND64 : Mips::AND;
4200b57cec5SDimitry Andric 
4210b57cec5SDimitry Andric   const TargetRegisterClass *RC = ABI.ArePtrs64bit() ?
4220b57cec5SDimitry Andric         &Mips::GPR64RegClass : &Mips::GPR32RegClass;
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric   // First, compute final stack size.
4250b57cec5SDimitry Andric   uint64_t StackSize = MFI.getStackSize();
4260b57cec5SDimitry Andric 
4270b57cec5SDimitry Andric   // No need to allocate space on the stack.
4280b57cec5SDimitry Andric   if (StackSize == 0 && !MFI.adjustsStack()) return;
4290b57cec5SDimitry Andric 
430*0fca6ea1SDimitry Andric   const MCRegisterInfo *MRI = MF.getContext().getRegisterInfo();
4310b57cec5SDimitry Andric 
4320b57cec5SDimitry Andric   // Adjust stack.
4330b57cec5SDimitry Andric   TII.adjustStackPtr(SP, -StackSize, MBB, MBBI);
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric   // emit ".cfi_def_cfa_offset StackSize"
4365ffd83dbSDimitry Andric   unsigned CFIIndex =
4375ffd83dbSDimitry Andric       MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, StackSize));
4380b57cec5SDimitry Andric   BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
4390b57cec5SDimitry Andric       .addCFIIndex(CFIIndex);
4400b57cec5SDimitry Andric 
4410b57cec5SDimitry Andric   if (MF.getFunction().hasFnAttribute("interrupt"))
4420b57cec5SDimitry Andric     emitInterruptPrologueStub(MF, MBB);
4430b57cec5SDimitry Andric 
4440b57cec5SDimitry Andric   const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
4450b57cec5SDimitry Andric 
4460b57cec5SDimitry Andric   if (!CSI.empty()) {
4470b57cec5SDimitry Andric     // Find the instruction past the last instruction that saves a callee-saved
4480b57cec5SDimitry Andric     // register to the stack.
4490b57cec5SDimitry Andric     for (unsigned i = 0; i < CSI.size(); ++i)
4500b57cec5SDimitry Andric       ++MBBI;
4510b57cec5SDimitry Andric 
4520b57cec5SDimitry Andric     // Iterate over list of callee-saved registers and emit .cfi_offset
4530b57cec5SDimitry Andric     // directives.
4544824e7fdSDimitry Andric     for (const CalleeSavedInfo &I : CSI) {
4554824e7fdSDimitry Andric       int64_t Offset = MFI.getObjectOffset(I.getFrameIdx());
45604eeddc0SDimitry Andric       Register Reg = I.getReg();
4570b57cec5SDimitry Andric 
4580b57cec5SDimitry Andric       // If Reg is a double precision register, emit two cfa_offsets,
4590b57cec5SDimitry Andric       // one for each of the paired single precision registers.
4600b57cec5SDimitry Andric       if (Mips::AFGR64RegClass.contains(Reg)) {
4610b57cec5SDimitry Andric         unsigned Reg0 =
4620b57cec5SDimitry Andric             MRI->getDwarfRegNum(RegInfo.getSubReg(Reg, Mips::sub_lo), true);
4630b57cec5SDimitry Andric         unsigned Reg1 =
4640b57cec5SDimitry Andric             MRI->getDwarfRegNum(RegInfo.getSubReg(Reg, Mips::sub_hi), true);
4650b57cec5SDimitry Andric 
4660b57cec5SDimitry Andric         if (!STI.isLittle())
4670b57cec5SDimitry Andric           std::swap(Reg0, Reg1);
4680b57cec5SDimitry Andric 
4690b57cec5SDimitry Andric         unsigned CFIIndex = MF.addFrameInst(
4700b57cec5SDimitry Andric             MCCFIInstruction::createOffset(nullptr, Reg0, Offset));
4710b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
4720b57cec5SDimitry Andric             .addCFIIndex(CFIIndex);
4730b57cec5SDimitry Andric 
4740b57cec5SDimitry Andric         CFIIndex = MF.addFrameInst(
4750b57cec5SDimitry Andric             MCCFIInstruction::createOffset(nullptr, Reg1, Offset + 4));
4760b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
4770b57cec5SDimitry Andric             .addCFIIndex(CFIIndex);
4780b57cec5SDimitry Andric       } else if (Mips::FGR64RegClass.contains(Reg)) {
4790b57cec5SDimitry Andric         unsigned Reg0 = MRI->getDwarfRegNum(Reg, true);
4800b57cec5SDimitry Andric         unsigned Reg1 = MRI->getDwarfRegNum(Reg, true) + 1;
4810b57cec5SDimitry Andric 
4820b57cec5SDimitry Andric         if (!STI.isLittle())
4830b57cec5SDimitry Andric           std::swap(Reg0, Reg1);
4840b57cec5SDimitry Andric 
4850b57cec5SDimitry Andric         unsigned CFIIndex = MF.addFrameInst(
4860b57cec5SDimitry Andric           MCCFIInstruction::createOffset(nullptr, Reg0, Offset));
4870b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
4880b57cec5SDimitry Andric             .addCFIIndex(CFIIndex);
4890b57cec5SDimitry Andric 
4900b57cec5SDimitry Andric         CFIIndex = MF.addFrameInst(
4910b57cec5SDimitry Andric           MCCFIInstruction::createOffset(nullptr, Reg1, Offset + 4));
4920b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
4930b57cec5SDimitry Andric             .addCFIIndex(CFIIndex);
4940b57cec5SDimitry Andric       } else {
4950b57cec5SDimitry Andric         // Reg is either in GPR32 or FGR32.
4960b57cec5SDimitry Andric         unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
4970b57cec5SDimitry Andric             nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
4980b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
4990b57cec5SDimitry Andric             .addCFIIndex(CFIIndex);
5000b57cec5SDimitry Andric       }
5010b57cec5SDimitry Andric     }
5020b57cec5SDimitry Andric   }
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric   if (MipsFI->callsEhReturn()) {
5050b57cec5SDimitry Andric     // Insert instructions that spill eh data registers.
5060b57cec5SDimitry Andric     for (int I = 0; I < 4; ++I) {
5070b57cec5SDimitry Andric       if (!MBB.isLiveIn(ABI.GetEhDataReg(I)))
5080b57cec5SDimitry Andric         MBB.addLiveIn(ABI.GetEhDataReg(I));
5090b57cec5SDimitry Andric       TII.storeRegToStackSlot(MBB, MBBI, ABI.GetEhDataReg(I), false,
510bdd1243dSDimitry Andric                               MipsFI->getEhDataRegFI(I), RC, &RegInfo,
511bdd1243dSDimitry Andric                               Register());
5120b57cec5SDimitry Andric     }
5130b57cec5SDimitry Andric 
5140b57cec5SDimitry Andric     // Emit .cfi_offset directives for eh data registers.
5150b57cec5SDimitry Andric     for (int I = 0; I < 4; ++I) {
5160b57cec5SDimitry Andric       int64_t Offset = MFI.getObjectOffset(MipsFI->getEhDataRegFI(I));
5170b57cec5SDimitry Andric       unsigned Reg = MRI->getDwarfRegNum(ABI.GetEhDataReg(I), true);
5180b57cec5SDimitry Andric       unsigned CFIIndex = MF.addFrameInst(
5190b57cec5SDimitry Andric           MCCFIInstruction::createOffset(nullptr, Reg, Offset));
5200b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
5210b57cec5SDimitry Andric           .addCFIIndex(CFIIndex);
5220b57cec5SDimitry Andric     }
5230b57cec5SDimitry Andric   }
5240b57cec5SDimitry Andric 
5250b57cec5SDimitry Andric   // if framepointer enabled, set it to point to the stack pointer.
5260b57cec5SDimitry Andric   if (hasFP(MF)) {
5270b57cec5SDimitry Andric     // Insert instruction "move $fp, $sp" at this location.
5280b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(MOVE), FP).addReg(SP).addReg(ZERO)
5290b57cec5SDimitry Andric       .setMIFlag(MachineInstr::FrameSetup);
5300b57cec5SDimitry Andric 
5310b57cec5SDimitry Andric     // emit ".cfi_def_cfa_register $fp"
5320b57cec5SDimitry Andric     unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(
5330b57cec5SDimitry Andric         nullptr, MRI->getDwarfRegNum(FP, true)));
5340b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
5350b57cec5SDimitry Andric         .addCFIIndex(CFIIndex);
5360b57cec5SDimitry Andric 
537fe6060f1SDimitry Andric     if (RegInfo.hasStackRealignment(MF)) {
5380b57cec5SDimitry Andric       // addiu $Reg, $zero, -MaxAlignment
5390b57cec5SDimitry Andric       // andi $sp, $sp, $Reg
5408bcb0991SDimitry Andric       Register VR = MF.getRegInfo().createVirtualRegister(RC);
5415ffd83dbSDimitry Andric       assert((Log2(MFI.getMaxAlign()) < 16) &&
5420b57cec5SDimitry Andric              "Function's alignment size requirement is not supported.");
5435ffd83dbSDimitry Andric       int64_t MaxAlign = -(int64_t)MFI.getMaxAlign().value();
5440b57cec5SDimitry Andric 
5450b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(ADDiu), VR).addReg(ZERO).addImm(MaxAlign);
5460b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(AND), SP).addReg(SP).addReg(VR);
5470b57cec5SDimitry Andric 
5480b57cec5SDimitry Andric       if (hasBP(MF)) {
5490b57cec5SDimitry Andric         // move $s7, $sp
5500b57cec5SDimitry Andric         unsigned BP = STI.isABI_N64() ? Mips::S7_64 : Mips::S7;
5510b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, TII.get(MOVE), BP)
5520b57cec5SDimitry Andric           .addReg(SP)
5530b57cec5SDimitry Andric           .addReg(ZERO);
5540b57cec5SDimitry Andric       }
5550b57cec5SDimitry Andric     }
5560b57cec5SDimitry Andric   }
5570b57cec5SDimitry Andric }
5580b57cec5SDimitry Andric 
emitInterruptPrologueStub(MachineFunction & MF,MachineBasicBlock & MBB) const5590b57cec5SDimitry Andric void MipsSEFrameLowering::emitInterruptPrologueStub(
5600b57cec5SDimitry Andric     MachineFunction &MF, MachineBasicBlock &MBB) const {
5610b57cec5SDimitry Andric   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
5620b57cec5SDimitry Andric   MachineBasicBlock::iterator MBBI = MBB.begin();
5630b57cec5SDimitry Andric   DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
5640b57cec5SDimitry Andric 
5650b57cec5SDimitry Andric   // Report an error the target doesn't support Mips32r2 or later.
5660b57cec5SDimitry Andric   // The epilogue relies on the use of the "ehb" to clear execution
5670b57cec5SDimitry Andric   // hazards. Pre R2 Mips relies on an implementation defined number
5680b57cec5SDimitry Andric   // of "ssnop"s to clear the execution hazard. Support for ssnop hazard
5690b57cec5SDimitry Andric   // clearing is not provided so reject that configuration.
5700b57cec5SDimitry Andric   if (!STI.hasMips32r2())
5710b57cec5SDimitry Andric     report_fatal_error(
5720b57cec5SDimitry Andric         "\"interrupt\" attribute is not supported on pre-MIPS32R2 or "
5730b57cec5SDimitry Andric         "MIPS16 targets.");
5740b57cec5SDimitry Andric 
5750b57cec5SDimitry Andric   // The GP register contains the "user" value, so we cannot perform
5760b57cec5SDimitry Andric   // any gp relative loads until we restore the "kernel" or "system" gp
5770b57cec5SDimitry Andric   // value. Until support is written we shall only accept the static
5780b57cec5SDimitry Andric   // relocation model.
5790b57cec5SDimitry Andric   if ((STI.getRelocationModel() != Reloc::Static))
5800b57cec5SDimitry Andric     report_fatal_error("\"interrupt\" attribute is only supported for the "
5810b57cec5SDimitry Andric                        "static relocation model on MIPS at the present time.");
5820b57cec5SDimitry Andric 
5830b57cec5SDimitry Andric   if (!STI.isABI_O32() || STI.hasMips64())
5840b57cec5SDimitry Andric     report_fatal_error("\"interrupt\" attribute is only supported for the "
5850b57cec5SDimitry Andric                        "O32 ABI on MIPS32R2+ at the present time.");
5860b57cec5SDimitry Andric 
5870b57cec5SDimitry Andric   // Perform ISR handling like GCC
5880b57cec5SDimitry Andric   StringRef IntKind =
5890b57cec5SDimitry Andric       MF.getFunction().getFnAttribute("interrupt").getValueAsString();
5900b57cec5SDimitry Andric   const TargetRegisterClass *PtrRC = &Mips::GPR32RegClass;
5910b57cec5SDimitry Andric 
5920b57cec5SDimitry Andric   // EIC interrupt handling needs to read the Cause register to disable
5930b57cec5SDimitry Andric   // interrupts.
5940b57cec5SDimitry Andric   if (IntKind == "eic") {
5950b57cec5SDimitry Andric     // Coprocessor registers are always live per se.
5960b57cec5SDimitry Andric     MBB.addLiveIn(Mips::COP013);
5970b57cec5SDimitry Andric     BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::MFC0), Mips::K0)
5980b57cec5SDimitry Andric         .addReg(Mips::COP013)
5990b57cec5SDimitry Andric         .addImm(0)
6000b57cec5SDimitry Andric         .setMIFlag(MachineInstr::FrameSetup);
6010b57cec5SDimitry Andric 
6020b57cec5SDimitry Andric     BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::EXT), Mips::K0)
6030b57cec5SDimitry Andric         .addReg(Mips::K0)
6040b57cec5SDimitry Andric         .addImm(10)
6050b57cec5SDimitry Andric         .addImm(6)
6060b57cec5SDimitry Andric         .setMIFlag(MachineInstr::FrameSetup);
6070b57cec5SDimitry Andric   }
6080b57cec5SDimitry Andric 
6090b57cec5SDimitry Andric   // Fetch and spill EPC
6100b57cec5SDimitry Andric   MBB.addLiveIn(Mips::COP014);
6110b57cec5SDimitry Andric   BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::MFC0), Mips::K1)
6120b57cec5SDimitry Andric       .addReg(Mips::COP014)
6130b57cec5SDimitry Andric       .addImm(0)
6140b57cec5SDimitry Andric       .setMIFlag(MachineInstr::FrameSetup);
6150b57cec5SDimitry Andric 
6160b57cec5SDimitry Andric   STI.getInstrInfo()->storeRegToStack(MBB, MBBI, Mips::K1, false,
6170b57cec5SDimitry Andric                                       MipsFI->getISRRegFI(0), PtrRC,
6180b57cec5SDimitry Andric                                       STI.getRegisterInfo(), 0);
6190b57cec5SDimitry Andric 
6200b57cec5SDimitry Andric   // Fetch and Spill Status
6210b57cec5SDimitry Andric   MBB.addLiveIn(Mips::COP012);
6220b57cec5SDimitry Andric   BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::MFC0), Mips::K1)
6230b57cec5SDimitry Andric       .addReg(Mips::COP012)
6240b57cec5SDimitry Andric       .addImm(0)
6250b57cec5SDimitry Andric       .setMIFlag(MachineInstr::FrameSetup);
6260b57cec5SDimitry Andric 
6270b57cec5SDimitry Andric   STI.getInstrInfo()->storeRegToStack(MBB, MBBI, Mips::K1, false,
6280b57cec5SDimitry Andric                                       MipsFI->getISRRegFI(1), PtrRC,
6290b57cec5SDimitry Andric                                       STI.getRegisterInfo(), 0);
6300b57cec5SDimitry Andric 
6310b57cec5SDimitry Andric   // Build the configuration for disabling lower priority interrupts. Non EIC
6320b57cec5SDimitry Andric   // interrupts need to be masked off with zero, EIC from the Cause register.
6330b57cec5SDimitry Andric   unsigned InsPosition = 8;
6340b57cec5SDimitry Andric   unsigned InsSize = 0;
6350b57cec5SDimitry Andric   unsigned SrcReg = Mips::ZERO;
6360b57cec5SDimitry Andric 
6370b57cec5SDimitry Andric   // If the interrupt we're tied to is the EIC, switch the source for the
6380b57cec5SDimitry Andric   // masking off interrupts to the cause register.
6390b57cec5SDimitry Andric   if (IntKind == "eic") {
6400b57cec5SDimitry Andric     SrcReg = Mips::K0;
6410b57cec5SDimitry Andric     InsPosition = 10;
6420b57cec5SDimitry Andric     InsSize = 6;
6430b57cec5SDimitry Andric   } else
6440b57cec5SDimitry Andric     InsSize = StringSwitch<unsigned>(IntKind)
6450b57cec5SDimitry Andric                   .Case("sw0", 1)
6460b57cec5SDimitry Andric                   .Case("sw1", 2)
6470b57cec5SDimitry Andric                   .Case("hw0", 3)
6480b57cec5SDimitry Andric                   .Case("hw1", 4)
6490b57cec5SDimitry Andric                   .Case("hw2", 5)
6500b57cec5SDimitry Andric                   .Case("hw3", 6)
6510b57cec5SDimitry Andric                   .Case("hw4", 7)
6520b57cec5SDimitry Andric                   .Case("hw5", 8)
6530b57cec5SDimitry Andric                   .Default(0);
6540b57cec5SDimitry Andric   assert(InsSize != 0 && "Unknown interrupt type!");
6550b57cec5SDimitry Andric 
6560b57cec5SDimitry Andric   BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::INS), Mips::K1)
6570b57cec5SDimitry Andric       .addReg(SrcReg)
6580b57cec5SDimitry Andric       .addImm(InsPosition)
6590b57cec5SDimitry Andric       .addImm(InsSize)
6600b57cec5SDimitry Andric       .addReg(Mips::K1)
6610b57cec5SDimitry Andric       .setMIFlag(MachineInstr::FrameSetup);
6620b57cec5SDimitry Andric 
6630b57cec5SDimitry Andric   // Mask off KSU, ERL, EXL
6640b57cec5SDimitry Andric   BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::INS), Mips::K1)
6650b57cec5SDimitry Andric       .addReg(Mips::ZERO)
6660b57cec5SDimitry Andric       .addImm(1)
6670b57cec5SDimitry Andric       .addImm(4)
6680b57cec5SDimitry Andric       .addReg(Mips::K1)
6690b57cec5SDimitry Andric       .setMIFlag(MachineInstr::FrameSetup);
6700b57cec5SDimitry Andric 
6710b57cec5SDimitry Andric   // Disable the FPU as we are not spilling those register sets.
6720b57cec5SDimitry Andric   if (!STI.useSoftFloat())
6730b57cec5SDimitry Andric     BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::INS), Mips::K1)
6740b57cec5SDimitry Andric         .addReg(Mips::ZERO)
6750b57cec5SDimitry Andric         .addImm(29)
6760b57cec5SDimitry Andric         .addImm(1)
6770b57cec5SDimitry Andric         .addReg(Mips::K1)
6780b57cec5SDimitry Andric         .setMIFlag(MachineInstr::FrameSetup);
6790b57cec5SDimitry Andric 
6800b57cec5SDimitry Andric   // Set the new status
6810b57cec5SDimitry Andric   BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::MTC0), Mips::COP012)
6820b57cec5SDimitry Andric       .addReg(Mips::K1)
6830b57cec5SDimitry Andric       .addImm(0)
6840b57cec5SDimitry Andric       .setMIFlag(MachineInstr::FrameSetup);
6850b57cec5SDimitry Andric }
6860b57cec5SDimitry Andric 
emitEpilogue(MachineFunction & MF,MachineBasicBlock & MBB) const6870b57cec5SDimitry Andric void MipsSEFrameLowering::emitEpilogue(MachineFunction &MF,
6880b57cec5SDimitry Andric                                        MachineBasicBlock &MBB) const {
6890b57cec5SDimitry Andric   MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
6900b57cec5SDimitry Andric   MachineFrameInfo &MFI            = MF.getFrameInfo();
6910b57cec5SDimitry Andric   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
6920b57cec5SDimitry Andric 
6930b57cec5SDimitry Andric   const MipsSEInstrInfo &TII =
6940b57cec5SDimitry Andric       *static_cast<const MipsSEInstrInfo *>(STI.getInstrInfo());
6950b57cec5SDimitry Andric   const MipsRegisterInfo &RegInfo =
6960b57cec5SDimitry Andric       *static_cast<const MipsRegisterInfo *>(STI.getRegisterInfo());
6970b57cec5SDimitry Andric 
6980b57cec5SDimitry Andric   DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
6990b57cec5SDimitry Andric   MipsABIInfo ABI = STI.getABI();
7000b57cec5SDimitry Andric   unsigned SP = ABI.GetStackPtr();
7010b57cec5SDimitry Andric   unsigned FP = ABI.GetFramePtr();
7020b57cec5SDimitry Andric   unsigned ZERO = ABI.GetNullPtr();
7030b57cec5SDimitry Andric   unsigned MOVE = ABI.GetGPRMoveOp();
7040b57cec5SDimitry Andric 
7050b57cec5SDimitry Andric   // if framepointer enabled, restore the stack pointer.
7060b57cec5SDimitry Andric   if (hasFP(MF)) {
7070b57cec5SDimitry Andric     // Find the first instruction that restores a callee-saved register.
7080b57cec5SDimitry Andric     MachineBasicBlock::iterator I = MBBI;
7090b57cec5SDimitry Andric 
7100b57cec5SDimitry Andric     for (unsigned i = 0; i < MFI.getCalleeSavedInfo().size(); ++i)
7110b57cec5SDimitry Andric       --I;
7120b57cec5SDimitry Andric 
7130b57cec5SDimitry Andric     // Insert instruction "move $sp, $fp" at this location.
7140b57cec5SDimitry Andric     BuildMI(MBB, I, DL, TII.get(MOVE), SP).addReg(FP).addReg(ZERO);
7150b57cec5SDimitry Andric   }
7160b57cec5SDimitry Andric 
7170b57cec5SDimitry Andric   if (MipsFI->callsEhReturn()) {
7180b57cec5SDimitry Andric     const TargetRegisterClass *RC =
7190b57cec5SDimitry Andric         ABI.ArePtrs64bit() ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
7200b57cec5SDimitry Andric 
7210b57cec5SDimitry Andric     // Find first instruction that restores a callee-saved register.
7220b57cec5SDimitry Andric     MachineBasicBlock::iterator I = MBBI;
7230b57cec5SDimitry Andric     for (unsigned i = 0; i < MFI.getCalleeSavedInfo().size(); ++i)
7240b57cec5SDimitry Andric       --I;
7250b57cec5SDimitry Andric 
7260b57cec5SDimitry Andric     // Insert instructions that restore eh data registers.
7270b57cec5SDimitry Andric     for (int J = 0; J < 4; ++J) {
7280b57cec5SDimitry Andric       TII.loadRegFromStackSlot(MBB, I, ABI.GetEhDataReg(J),
729bdd1243dSDimitry Andric                                MipsFI->getEhDataRegFI(J), RC, &RegInfo,
730bdd1243dSDimitry Andric                                Register());
7310b57cec5SDimitry Andric     }
7320b57cec5SDimitry Andric   }
7330b57cec5SDimitry Andric 
7340b57cec5SDimitry Andric   if (MF.getFunction().hasFnAttribute("interrupt"))
7350b57cec5SDimitry Andric     emitInterruptEpilogueStub(MF, MBB);
7360b57cec5SDimitry Andric 
7370b57cec5SDimitry Andric   // Get the number of bytes from FrameInfo
7380b57cec5SDimitry Andric   uint64_t StackSize = MFI.getStackSize();
7390b57cec5SDimitry Andric 
7400b57cec5SDimitry Andric   if (!StackSize)
7410b57cec5SDimitry Andric     return;
7420b57cec5SDimitry Andric 
7430b57cec5SDimitry Andric   // Adjust stack.
7440b57cec5SDimitry Andric   TII.adjustStackPtr(SP, StackSize, MBB, MBBI);
7450b57cec5SDimitry Andric }
7460b57cec5SDimitry Andric 
emitInterruptEpilogueStub(MachineFunction & MF,MachineBasicBlock & MBB) const7470b57cec5SDimitry Andric void MipsSEFrameLowering::emitInterruptEpilogueStub(
7480b57cec5SDimitry Andric     MachineFunction &MF, MachineBasicBlock &MBB) const {
7490b57cec5SDimitry Andric   MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
7500b57cec5SDimitry Andric   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
7510b57cec5SDimitry Andric   DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
7520b57cec5SDimitry Andric 
7530b57cec5SDimitry Andric   // Perform ISR handling like GCC
7540b57cec5SDimitry Andric   const TargetRegisterClass *PtrRC = &Mips::GPR32RegClass;
7550b57cec5SDimitry Andric 
7560b57cec5SDimitry Andric   // Disable Interrupts.
7570b57cec5SDimitry Andric   BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::DI), Mips::ZERO);
7580b57cec5SDimitry Andric   BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::EHB));
7590b57cec5SDimitry Andric 
7600b57cec5SDimitry Andric   // Restore EPC
7610b57cec5SDimitry Andric   STI.getInstrInfo()->loadRegFromStackSlot(MBB, MBBI, Mips::K1,
7620b57cec5SDimitry Andric                                            MipsFI->getISRRegFI(0), PtrRC,
763bdd1243dSDimitry Andric                                            STI.getRegisterInfo(), Register());
7640b57cec5SDimitry Andric   BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::MTC0), Mips::COP014)
7650b57cec5SDimitry Andric       .addReg(Mips::K1)
7660b57cec5SDimitry Andric       .addImm(0);
7670b57cec5SDimitry Andric 
7680b57cec5SDimitry Andric   // Restore Status
7690b57cec5SDimitry Andric   STI.getInstrInfo()->loadRegFromStackSlot(MBB, MBBI, Mips::K1,
7700b57cec5SDimitry Andric                                            MipsFI->getISRRegFI(1), PtrRC,
771bdd1243dSDimitry Andric                                            STI.getRegisterInfo(), Register());
7720b57cec5SDimitry Andric   BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::MTC0), Mips::COP012)
7730b57cec5SDimitry Andric       .addReg(Mips::K1)
7740b57cec5SDimitry Andric       .addImm(0);
7750b57cec5SDimitry Andric }
7760b57cec5SDimitry Andric 
777e8d8bef9SDimitry Andric StackOffset
getFrameIndexReference(const MachineFunction & MF,int FI,Register & FrameReg) const778e8d8bef9SDimitry Andric MipsSEFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
7795ffd83dbSDimitry Andric                                             Register &FrameReg) const {
7800b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
7810b57cec5SDimitry Andric   MipsABIInfo ABI = STI.getABI();
7820b57cec5SDimitry Andric 
7830b57cec5SDimitry Andric   if (MFI.isFixedObjectIndex(FI))
7840b57cec5SDimitry Andric     FrameReg = hasFP(MF) ? ABI.GetFramePtr() : ABI.GetStackPtr();
7850b57cec5SDimitry Andric   else
7860b57cec5SDimitry Andric     FrameReg = hasBP(MF) ? ABI.GetBasePtr() : ABI.GetStackPtr();
7870b57cec5SDimitry Andric 
788e8d8bef9SDimitry Andric   return StackOffset::getFixed(MFI.getObjectOffset(FI) + MFI.getStackSize() -
789e8d8bef9SDimitry Andric                                getOffsetOfLocalArea() +
790e8d8bef9SDimitry Andric                                MFI.getOffsetAdjustment());
7910b57cec5SDimitry Andric }
7920b57cec5SDimitry Andric 
spillCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,ArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI) const7935ffd83dbSDimitry Andric bool MipsSEFrameLowering::spillCalleeSavedRegisters(
7945ffd83dbSDimitry Andric     MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
7955ffd83dbSDimitry Andric     ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
7960b57cec5SDimitry Andric   MachineFunction *MF = MBB.getParent();
7970b57cec5SDimitry Andric   const TargetInstrInfo &TII = *STI.getInstrInfo();
7980b57cec5SDimitry Andric 
7994824e7fdSDimitry Andric   for (const CalleeSavedInfo &I : CSI) {
8000b57cec5SDimitry Andric     // Add the callee-saved register as live-in. Do not add if the register is
8010b57cec5SDimitry Andric     // RA and return address is taken, because it has already been added in
8020b57cec5SDimitry Andric     // method MipsTargetLowering::lowerRETURNADDR.
8030b57cec5SDimitry Andric     // It's killed at the spill, unless the register is RA and return address
8040b57cec5SDimitry Andric     // is taken.
80504eeddc0SDimitry Andric     Register Reg = I.getReg();
8060b57cec5SDimitry Andric     bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA || Reg == Mips::RA_64)
8070b57cec5SDimitry Andric         && MF->getFrameInfo().isReturnAddressTaken();
8080b57cec5SDimitry Andric     if (!IsRAAndRetAddrIsTaken)
8090b57cec5SDimitry Andric       MBB.addLiveIn(Reg);
8100b57cec5SDimitry Andric 
8110b57cec5SDimitry Andric     // ISRs require HI/LO to be spilled into kernel registers to be then
8120b57cec5SDimitry Andric     // spilled to the stack frame.
8130b57cec5SDimitry Andric     bool IsLOHI = (Reg == Mips::LO0 || Reg == Mips::LO0_64 ||
8140b57cec5SDimitry Andric                    Reg == Mips::HI0 || Reg == Mips::HI0_64);
8150b57cec5SDimitry Andric     const Function &Func = MBB.getParent()->getFunction();
8160b57cec5SDimitry Andric     if (IsLOHI && Func.hasFnAttribute("interrupt")) {
8170b57cec5SDimitry Andric       DebugLoc DL = MI->getDebugLoc();
8180b57cec5SDimitry Andric 
8190b57cec5SDimitry Andric       unsigned Op = 0;
8200b57cec5SDimitry Andric       if (!STI.getABI().ArePtrs64bit()) {
8210b57cec5SDimitry Andric         Op = (Reg == Mips::HI0) ? Mips::MFHI : Mips::MFLO;
8220b57cec5SDimitry Andric         Reg = Mips::K0;
8230b57cec5SDimitry Andric       } else {
8240b57cec5SDimitry Andric         Op = (Reg == Mips::HI0) ? Mips::MFHI64 : Mips::MFLO64;
8250b57cec5SDimitry Andric         Reg = Mips::K0_64;
8260b57cec5SDimitry Andric       }
8270b57cec5SDimitry Andric       BuildMI(MBB, MI, DL, TII.get(Op), Mips::K0)
8280b57cec5SDimitry Andric           .setMIFlag(MachineInstr::FrameSetup);
8290b57cec5SDimitry Andric     }
8300b57cec5SDimitry Andric 
8310b57cec5SDimitry Andric     // Insert the spill to the stack frame.
8320b57cec5SDimitry Andric     bool IsKill = !IsRAAndRetAddrIsTaken;
8330b57cec5SDimitry Andric     const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
834bdd1243dSDimitry Andric     TII.storeRegToStackSlot(MBB, MI, Reg, IsKill, I.getFrameIdx(), RC, TRI,
835bdd1243dSDimitry Andric                             Register());
8360b57cec5SDimitry Andric   }
8370b57cec5SDimitry Andric 
8380b57cec5SDimitry Andric   return true;
8390b57cec5SDimitry Andric }
8400b57cec5SDimitry Andric 
8410b57cec5SDimitry Andric bool
hasReservedCallFrame(const MachineFunction & MF) const8420b57cec5SDimitry Andric MipsSEFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
8430b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
8440b57cec5SDimitry Andric   // Reserve call frame if the size of the maximum call frame fits into 16-bit
8450b57cec5SDimitry Andric   // immediate field and there are no variable sized objects on the stack.
8460b57cec5SDimitry Andric   // Make sure the second register scavenger spill slot can be accessed with one
8470b57cec5SDimitry Andric   // instruction.
8480b57cec5SDimitry Andric   return isInt<16>(MFI.getMaxCallFrameSize() + getStackAlignment()) &&
8490b57cec5SDimitry Andric     !MFI.hasVarSizedObjects();
8500b57cec5SDimitry Andric }
8510b57cec5SDimitry Andric 
8520b57cec5SDimitry Andric /// Mark \p Reg and all registers aliasing it in the bitset.
setAliasRegs(MachineFunction & MF,BitVector & SavedRegs,unsigned Reg)8530b57cec5SDimitry Andric static void setAliasRegs(MachineFunction &MF, BitVector &SavedRegs,
8540b57cec5SDimitry Andric                          unsigned Reg) {
8550b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
8560b57cec5SDimitry Andric   for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
8570b57cec5SDimitry Andric     SavedRegs.set(*AI);
8580b57cec5SDimitry Andric }
8590b57cec5SDimitry Andric 
determineCalleeSaves(MachineFunction & MF,BitVector & SavedRegs,RegScavenger * RS) const8600b57cec5SDimitry Andric void MipsSEFrameLowering::determineCalleeSaves(MachineFunction &MF,
8610b57cec5SDimitry Andric                                                BitVector &SavedRegs,
8620b57cec5SDimitry Andric                                                RegScavenger *RS) const {
8630b57cec5SDimitry Andric   TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
8640b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
8650b57cec5SDimitry Andric   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
8660b57cec5SDimitry Andric   MipsABIInfo ABI = STI.getABI();
8678bcb0991SDimitry Andric   unsigned RA = ABI.IsN64() ? Mips::RA_64 : Mips::RA;
8680b57cec5SDimitry Andric   unsigned FP = ABI.GetFramePtr();
8690b57cec5SDimitry Andric   unsigned BP = ABI.IsN64() ? Mips::S7_64 : Mips::S7;
8700b57cec5SDimitry Andric 
8718bcb0991SDimitry Andric   // Mark $ra and $fp as used if function has dedicated frame pointer.
8728bcb0991SDimitry Andric   if (hasFP(MF)) {
8738bcb0991SDimitry Andric     setAliasRegs(MF, SavedRegs, RA);
8740b57cec5SDimitry Andric     setAliasRegs(MF, SavedRegs, FP);
8758bcb0991SDimitry Andric   }
8760b57cec5SDimitry Andric   // Mark $s7 as used if function has dedicated base pointer.
8770b57cec5SDimitry Andric   if (hasBP(MF))
8780b57cec5SDimitry Andric     setAliasRegs(MF, SavedRegs, BP);
8790b57cec5SDimitry Andric 
8800b57cec5SDimitry Andric   // Create spill slots for eh data registers if function calls eh_return.
8810b57cec5SDimitry Andric   if (MipsFI->callsEhReturn())
8825ffd83dbSDimitry Andric     MipsFI->createEhDataRegsFI(MF);
8830b57cec5SDimitry Andric 
8840b57cec5SDimitry Andric   // Create spill slots for Coprocessor 0 registers if function is an ISR.
8850b57cec5SDimitry Andric   if (MipsFI->isISR())
8865ffd83dbSDimitry Andric     MipsFI->createISRRegFI(MF);
8870b57cec5SDimitry Andric 
8880b57cec5SDimitry Andric   // Expand pseudo instructions which load, store or copy accumulators.
8890b57cec5SDimitry Andric   // Add an emergency spill slot if a pseudo was expanded.
8900b57cec5SDimitry Andric   if (ExpandPseudo(MF).expand()) {
8910b57cec5SDimitry Andric     // The spill slot should be half the size of the accumulator. If target have
8920b57cec5SDimitry Andric     // general-purpose registers 64 bits wide, it should be 64-bit, otherwise
8930b57cec5SDimitry Andric     // it should be 32-bit.
8940b57cec5SDimitry Andric     const TargetRegisterClass &RC = STI.isGP64bit() ?
8950b57cec5SDimitry Andric       Mips::GPR64RegClass : Mips::GPR32RegClass;
8960b57cec5SDimitry Andric     int FI = MF.getFrameInfo().CreateStackObject(TRI->getSpillSize(RC),
8975ffd83dbSDimitry Andric                                                  TRI->getSpillAlign(RC), false);
8980b57cec5SDimitry Andric     RS->addScavengingFrameIndex(FI);
8990b57cec5SDimitry Andric   }
9000b57cec5SDimitry Andric 
9010b57cec5SDimitry Andric   // Set scavenging frame index if necessary.
9020b57cec5SDimitry Andric   uint64_t MaxSPOffset = estimateStackSize(MF);
9030b57cec5SDimitry Andric 
9040b57cec5SDimitry Andric   // MSA has a minimum offset of 10 bits signed. If there is a variable
9050b57cec5SDimitry Andric   // sized object on the stack, the estimation cannot account for it.
9060b57cec5SDimitry Andric   if (isIntN(STI.hasMSA() ? 10 : 16, MaxSPOffset) &&
9070b57cec5SDimitry Andric       !MF.getFrameInfo().hasVarSizedObjects())
9080b57cec5SDimitry Andric     return;
9090b57cec5SDimitry Andric 
9100b57cec5SDimitry Andric   const TargetRegisterClass &RC =
9110b57cec5SDimitry Andric       ABI.ArePtrs64bit() ? Mips::GPR64RegClass : Mips::GPR32RegClass;
9120b57cec5SDimitry Andric   int FI = MF.getFrameInfo().CreateStackObject(TRI->getSpillSize(RC),
9135ffd83dbSDimitry Andric                                                TRI->getSpillAlign(RC), false);
9140b57cec5SDimitry Andric   RS->addScavengingFrameIndex(FI);
9150b57cec5SDimitry Andric }
9160b57cec5SDimitry Andric 
9170b57cec5SDimitry Andric const MipsFrameLowering *
createMipsSEFrameLowering(const MipsSubtarget & ST)9180b57cec5SDimitry Andric llvm::createMipsSEFrameLowering(const MipsSubtarget &ST) {
9190b57cec5SDimitry Andric   return new MipsSEFrameLowering(ST);
9200b57cec5SDimitry Andric }
921