xref: /aosp_15_r20/external/llvm/lib/Target/Mips/Mips16FrameLowering.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- Mips16FrameLowering.cpp - Mips16 Frame Information ----------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file contains the Mips16 implementation of TargetFrameLowering class.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker 
14*9880d681SAndroid Build Coastguard Worker #include "Mips16FrameLowering.h"
15*9880d681SAndroid Build Coastguard Worker #include "MCTargetDesc/MipsBaseInfo.h"
16*9880d681SAndroid Build Coastguard Worker #include "Mips16InstrInfo.h"
17*9880d681SAndroid Build Coastguard Worker #include "MipsInstrInfo.h"
18*9880d681SAndroid Build Coastguard Worker #include "MipsRegisterInfo.h"
19*9880d681SAndroid Build Coastguard Worker #include "MipsSubtarget.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFrameInfo.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstrBuilder.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineModuleInfo.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DataLayout.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetOptions.h"
28*9880d681SAndroid Build Coastguard Worker 
29*9880d681SAndroid Build Coastguard Worker using namespace llvm;
30*9880d681SAndroid Build Coastguard Worker 
Mips16FrameLowering(const MipsSubtarget & STI)31*9880d681SAndroid Build Coastguard Worker Mips16FrameLowering::Mips16FrameLowering(const MipsSubtarget &STI)
32*9880d681SAndroid Build Coastguard Worker     : MipsFrameLowering(STI, STI.stackAlignment()) {}
33*9880d681SAndroid Build Coastguard Worker 
emitPrologue(MachineFunction & MF,MachineBasicBlock & MBB) const34*9880d681SAndroid Build Coastguard Worker void Mips16FrameLowering::emitPrologue(MachineFunction &MF,
35*9880d681SAndroid Build Coastguard Worker                                        MachineBasicBlock &MBB) const {
36*9880d681SAndroid Build Coastguard Worker   assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
37*9880d681SAndroid Build Coastguard Worker   MachineFrameInfo *MFI = MF.getFrameInfo();
38*9880d681SAndroid Build Coastguard Worker   const Mips16InstrInfo &TII =
39*9880d681SAndroid Build Coastguard Worker       *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
40*9880d681SAndroid Build Coastguard Worker   MachineBasicBlock::iterator MBBI = MBB.begin();
41*9880d681SAndroid Build Coastguard Worker 
42*9880d681SAndroid Build Coastguard Worker   // Debug location must be unknown since the first debug location is used
43*9880d681SAndroid Build Coastguard Worker   // to determine the end of the prologue.
44*9880d681SAndroid Build Coastguard Worker   DebugLoc dl;
45*9880d681SAndroid Build Coastguard Worker 
46*9880d681SAndroid Build Coastguard Worker   uint64_t StackSize = MFI->getStackSize();
47*9880d681SAndroid Build Coastguard Worker 
48*9880d681SAndroid Build Coastguard Worker   // No need to allocate space on the stack.
49*9880d681SAndroid Build Coastguard Worker   if (StackSize == 0 && !MFI->adjustsStack()) return;
50*9880d681SAndroid Build Coastguard Worker 
51*9880d681SAndroid Build Coastguard Worker   MachineModuleInfo &MMI = MF.getMMI();
52*9880d681SAndroid Build Coastguard Worker   const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
53*9880d681SAndroid Build Coastguard Worker   MachineLocation DstML, SrcML;
54*9880d681SAndroid Build Coastguard Worker 
55*9880d681SAndroid Build Coastguard Worker   // Adjust stack.
56*9880d681SAndroid Build Coastguard Worker   TII.makeFrame(Mips::SP, StackSize, MBB, MBBI);
57*9880d681SAndroid Build Coastguard Worker 
58*9880d681SAndroid Build Coastguard Worker   // emit ".cfi_def_cfa_offset StackSize"
59*9880d681SAndroid Build Coastguard Worker   unsigned CFIIndex = MMI.addFrameInst(
60*9880d681SAndroid Build Coastguard Worker       MCCFIInstruction::createDefCfaOffset(nullptr, -StackSize));
61*9880d681SAndroid Build Coastguard Worker   BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
62*9880d681SAndroid Build Coastguard Worker       .addCFIIndex(CFIIndex);
63*9880d681SAndroid Build Coastguard Worker 
64*9880d681SAndroid Build Coastguard Worker   const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
65*9880d681SAndroid Build Coastguard Worker 
66*9880d681SAndroid Build Coastguard Worker   if (CSI.size()) {
67*9880d681SAndroid Build Coastguard Worker     const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
68*9880d681SAndroid Build Coastguard Worker 
69*9880d681SAndroid Build Coastguard Worker     for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
70*9880d681SAndroid Build Coastguard Worker          E = CSI.end(); I != E; ++I) {
71*9880d681SAndroid Build Coastguard Worker       int64_t Offset = MFI->getObjectOffset(I->getFrameIdx());
72*9880d681SAndroid Build Coastguard Worker       unsigned Reg = I->getReg();
73*9880d681SAndroid Build Coastguard Worker       unsigned DReg = MRI->getDwarfRegNum(Reg, true);
74*9880d681SAndroid Build Coastguard Worker       unsigned CFIIndex = MMI.addFrameInst(
75*9880d681SAndroid Build Coastguard Worker           MCCFIInstruction::createOffset(nullptr, DReg, Offset));
76*9880d681SAndroid Build Coastguard Worker       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
77*9880d681SAndroid Build Coastguard Worker           .addCFIIndex(CFIIndex);
78*9880d681SAndroid Build Coastguard Worker     }
79*9880d681SAndroid Build Coastguard Worker   }
80*9880d681SAndroid Build Coastguard Worker   if (hasFP(MF))
81*9880d681SAndroid Build Coastguard Worker     BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0)
82*9880d681SAndroid Build Coastguard Worker       .addReg(Mips::SP).setMIFlag(MachineInstr::FrameSetup);
83*9880d681SAndroid Build Coastguard Worker 
84*9880d681SAndroid Build Coastguard Worker }
85*9880d681SAndroid Build Coastguard Worker 
emitEpilogue(MachineFunction & MF,MachineBasicBlock & MBB) const86*9880d681SAndroid Build Coastguard Worker void Mips16FrameLowering::emitEpilogue(MachineFunction &MF,
87*9880d681SAndroid Build Coastguard Worker                                  MachineBasicBlock &MBB) const {
88*9880d681SAndroid Build Coastguard Worker   MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
89*9880d681SAndroid Build Coastguard Worker   MachineFrameInfo *MFI = MF.getFrameInfo();
90*9880d681SAndroid Build Coastguard Worker   const Mips16InstrInfo &TII =
91*9880d681SAndroid Build Coastguard Worker       *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
92*9880d681SAndroid Build Coastguard Worker   DebugLoc dl = MBBI->getDebugLoc();
93*9880d681SAndroid Build Coastguard Worker   uint64_t StackSize = MFI->getStackSize();
94*9880d681SAndroid Build Coastguard Worker 
95*9880d681SAndroid Build Coastguard Worker   if (!StackSize)
96*9880d681SAndroid Build Coastguard Worker     return;
97*9880d681SAndroid Build Coastguard Worker 
98*9880d681SAndroid Build Coastguard Worker   if (hasFP(MF))
99*9880d681SAndroid Build Coastguard Worker     BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP)
100*9880d681SAndroid Build Coastguard Worker       .addReg(Mips::S0);
101*9880d681SAndroid Build Coastguard Worker 
102*9880d681SAndroid Build Coastguard Worker   // Adjust stack.
103*9880d681SAndroid Build Coastguard Worker   // assumes stacksize multiple of 8
104*9880d681SAndroid Build Coastguard Worker   TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI);
105*9880d681SAndroid Build Coastguard Worker }
106*9880d681SAndroid Build Coastguard Worker 
107*9880d681SAndroid Build Coastguard Worker bool Mips16FrameLowering::
spillCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,const std::vector<CalleeSavedInfo> & CSI,const TargetRegisterInfo * TRI) const108*9880d681SAndroid Build Coastguard Worker spillCalleeSavedRegisters(MachineBasicBlock &MBB,
109*9880d681SAndroid Build Coastguard Worker                           MachineBasicBlock::iterator MI,
110*9880d681SAndroid Build Coastguard Worker                           const std::vector<CalleeSavedInfo> &CSI,
111*9880d681SAndroid Build Coastguard Worker                           const TargetRegisterInfo *TRI) const {
112*9880d681SAndroid Build Coastguard Worker   MachineFunction *MF = MBB.getParent();
113*9880d681SAndroid Build Coastguard Worker   MachineBasicBlock *EntryBlock = &MF->front();
114*9880d681SAndroid Build Coastguard Worker 
115*9880d681SAndroid Build Coastguard Worker   //
116*9880d681SAndroid Build Coastguard Worker   // Registers RA, S0,S1 are the callee saved registers and they
117*9880d681SAndroid Build Coastguard Worker   // will be saved with the "save" instruction
118*9880d681SAndroid Build Coastguard Worker   // during emitPrologue
119*9880d681SAndroid Build Coastguard Worker   //
120*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
121*9880d681SAndroid Build Coastguard Worker     // Add the callee-saved register as live-in. Do not add if the register is
122*9880d681SAndroid Build Coastguard Worker     // RA and return address is taken, because it has already been added in
123*9880d681SAndroid Build Coastguard Worker     // method MipsTargetLowering::LowerRETURNADDR.
124*9880d681SAndroid Build Coastguard Worker     // It's killed at the spill, unless the register is RA and return address
125*9880d681SAndroid Build Coastguard Worker     // is taken.
126*9880d681SAndroid Build Coastguard Worker     unsigned Reg = CSI[i].getReg();
127*9880d681SAndroid Build Coastguard Worker     bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA)
128*9880d681SAndroid Build Coastguard Worker       && MF->getFrameInfo()->isReturnAddressTaken();
129*9880d681SAndroid Build Coastguard Worker     if (!IsRAAndRetAddrIsTaken)
130*9880d681SAndroid Build Coastguard Worker       EntryBlock->addLiveIn(Reg);
131*9880d681SAndroid Build Coastguard Worker   }
132*9880d681SAndroid Build Coastguard Worker 
133*9880d681SAndroid Build Coastguard Worker   return true;
134*9880d681SAndroid Build Coastguard Worker }
135*9880d681SAndroid Build Coastguard Worker 
restoreCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,const std::vector<CalleeSavedInfo> & CSI,const TargetRegisterInfo * TRI) const136*9880d681SAndroid Build Coastguard Worker bool Mips16FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
137*9880d681SAndroid Build Coastguard Worker                                           MachineBasicBlock::iterator MI,
138*9880d681SAndroid Build Coastguard Worker                                        const std::vector<CalleeSavedInfo> &CSI,
139*9880d681SAndroid Build Coastguard Worker                                        const TargetRegisterInfo *TRI) const {
140*9880d681SAndroid Build Coastguard Worker   //
141*9880d681SAndroid Build Coastguard Worker   // Registers RA,S0,S1 are the callee saved registers and they will be restored
142*9880d681SAndroid Build Coastguard Worker   // with the restore instruction during emitEpilogue.
143*9880d681SAndroid Build Coastguard Worker   // We need to override this virtual function, otherwise llvm will try and
144*9880d681SAndroid Build Coastguard Worker   // restore the registers on it's on from the stack.
145*9880d681SAndroid Build Coastguard Worker   //
146*9880d681SAndroid Build Coastguard Worker 
147*9880d681SAndroid Build Coastguard Worker   return true;
148*9880d681SAndroid Build Coastguard Worker }
149*9880d681SAndroid Build Coastguard Worker 
150*9880d681SAndroid Build Coastguard Worker bool
hasReservedCallFrame(const MachineFunction & MF) const151*9880d681SAndroid Build Coastguard Worker Mips16FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
152*9880d681SAndroid Build Coastguard Worker   const MachineFrameInfo *MFI = MF.getFrameInfo();
153*9880d681SAndroid Build Coastguard Worker   // Reserve call frame if the size of the maximum call frame fits into 15-bit
154*9880d681SAndroid Build Coastguard Worker   // immediate field and there are no variable sized objects on the stack.
155*9880d681SAndroid Build Coastguard Worker   return isInt<15>(MFI->getMaxCallFrameSize()) && !MFI->hasVarSizedObjects();
156*9880d681SAndroid Build Coastguard Worker }
157*9880d681SAndroid Build Coastguard Worker 
determineCalleeSaves(MachineFunction & MF,BitVector & SavedRegs,RegScavenger * RS) const158*9880d681SAndroid Build Coastguard Worker void Mips16FrameLowering::determineCalleeSaves(MachineFunction &MF,
159*9880d681SAndroid Build Coastguard Worker                                                BitVector &SavedRegs,
160*9880d681SAndroid Build Coastguard Worker                                                RegScavenger *RS) const {
161*9880d681SAndroid Build Coastguard Worker   TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
162*9880d681SAndroid Build Coastguard Worker   const Mips16InstrInfo &TII =
163*9880d681SAndroid Build Coastguard Worker       *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
164*9880d681SAndroid Build Coastguard Worker   const MipsRegisterInfo &RI = TII.getRegisterInfo();
165*9880d681SAndroid Build Coastguard Worker   const BitVector Reserved = RI.getReservedRegs(MF);
166*9880d681SAndroid Build Coastguard Worker   bool SaveS2 = Reserved[Mips::S2];
167*9880d681SAndroid Build Coastguard Worker   if (SaveS2)
168*9880d681SAndroid Build Coastguard Worker     SavedRegs.set(Mips::S2);
169*9880d681SAndroid Build Coastguard Worker   if (hasFP(MF))
170*9880d681SAndroid Build Coastguard Worker     SavedRegs.set(Mips::S0);
171*9880d681SAndroid Build Coastguard Worker }
172*9880d681SAndroid Build Coastguard Worker 
173*9880d681SAndroid Build Coastguard Worker const MipsFrameLowering *
createMips16FrameLowering(const MipsSubtarget & ST)174*9880d681SAndroid Build Coastguard Worker llvm::createMips16FrameLowering(const MipsSubtarget &ST) {
175*9880d681SAndroid Build Coastguard Worker   return new Mips16FrameLowering(ST);
176*9880d681SAndroid Build Coastguard Worker }
177