xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/Lanai/LanaiFrameLowering.cpp (revision 4824e7fd18a1223177218d4aec1b3c6c5c4a444e)
10b57cec5SDimitry Andric //===-- LanaiFrameLowering.cpp - Lanai 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 Lanai implementation of TargetFrameLowering class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "LanaiFrameLowering.h"
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "LanaiAluCode.h"
160b57cec5SDimitry Andric #include "LanaiInstrInfo.h"
170b57cec5SDimitry Andric #include "LanaiSubtarget.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
220b57cec5SDimitry Andric #include "llvm/IR/Function.h"
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric using namespace llvm;
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric // Determines the size of the frame and maximum call frame size.
determineFrameLayout(MachineFunction & MF) const270b57cec5SDimitry Andric void LanaiFrameLowering::determineFrameLayout(MachineFunction &MF) const {
280b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
290b57cec5SDimitry Andric   const LanaiRegisterInfo *LRI = STI.getRegisterInfo();
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric   // Get the number of bytes to allocate from the FrameInfo.
320b57cec5SDimitry Andric   unsigned FrameSize = MFI.getStackSize();
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric   // Get the alignment.
355ffd83dbSDimitry Andric   Align StackAlign =
36fe6060f1SDimitry Andric       LRI->hasStackRealignment(MF) ? MFI.getMaxAlign() : getStackAlign();
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric   // Get the maximum call frame size of all the calls.
390b57cec5SDimitry Andric   unsigned MaxCallFrameSize = MFI.getMaxCallFrameSize();
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric   // If we have dynamic alloca then MaxCallFrameSize needs to be aligned so
420b57cec5SDimitry Andric   // that allocations will be aligned.
430b57cec5SDimitry Andric   if (MFI.hasVarSizedObjects())
440b57cec5SDimitry Andric     MaxCallFrameSize = alignTo(MaxCallFrameSize, StackAlign);
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric   // Update maximum call frame size.
470b57cec5SDimitry Andric   MFI.setMaxCallFrameSize(MaxCallFrameSize);
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric   // Include call frame size in total.
500b57cec5SDimitry Andric   if (!(hasReservedCallFrame(MF) && MFI.adjustsStack()))
510b57cec5SDimitry Andric     FrameSize += MaxCallFrameSize;
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric   // Make sure the frame is aligned.
540b57cec5SDimitry Andric   FrameSize = alignTo(FrameSize, StackAlign);
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric   // Update frame info.
570b57cec5SDimitry Andric   MFI.setStackSize(FrameSize);
580b57cec5SDimitry Andric }
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric // Iterates through each basic block in a machine function and replaces
610b57cec5SDimitry Andric // ADJDYNALLOC pseudo instructions with a Lanai:ADDI with the
620b57cec5SDimitry Andric // maximum call frame size as the immediate.
replaceAdjDynAllocPseudo(MachineFunction & MF) const630b57cec5SDimitry Andric void LanaiFrameLowering::replaceAdjDynAllocPseudo(MachineFunction &MF) const {
640b57cec5SDimitry Andric   const LanaiInstrInfo &LII =
650b57cec5SDimitry Andric       *static_cast<const LanaiInstrInfo *>(STI.getInstrInfo());
660b57cec5SDimitry Andric   unsigned MaxCallFrameSize = MF.getFrameInfo().getMaxCallFrameSize();
670b57cec5SDimitry Andric 
68*4824e7fdSDimitry Andric   for (MachineBasicBlock &MBB : MF) {
69*4824e7fdSDimitry Andric     for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
700b57cec5SDimitry Andric       if (MI.getOpcode() == Lanai::ADJDYNALLOC) {
710b57cec5SDimitry Andric         DebugLoc DL = MI.getDebugLoc();
728bcb0991SDimitry Andric         Register Dst = MI.getOperand(0).getReg();
738bcb0991SDimitry Andric         Register Src = MI.getOperand(1).getReg();
740b57cec5SDimitry Andric 
75*4824e7fdSDimitry Andric         BuildMI(MBB, MI, DL, LII.get(Lanai::ADD_I_LO), Dst)
760b57cec5SDimitry Andric             .addReg(Src)
770b57cec5SDimitry Andric             .addImm(MaxCallFrameSize);
780b57cec5SDimitry Andric         MI.eraseFromParent();
790b57cec5SDimitry Andric       }
800b57cec5SDimitry Andric     }
810b57cec5SDimitry Andric   }
820b57cec5SDimitry Andric }
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric // Generates the following sequence for function entry:
850b57cec5SDimitry Andric //   st %fp,-4[*%sp]        !push old FP
860b57cec5SDimitry Andric //   add %sp,8,%fp          !generate new FP
870b57cec5SDimitry Andric //   sub %sp,0x4,%sp        !allocate stack space (as needed)
emitPrologue(MachineFunction & MF,MachineBasicBlock & MBB) const880b57cec5SDimitry Andric void LanaiFrameLowering::emitPrologue(MachineFunction &MF,
890b57cec5SDimitry Andric                                       MachineBasicBlock &MBB) const {
900b57cec5SDimitry Andric   assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
930b57cec5SDimitry Andric   const LanaiInstrInfo &LII =
940b57cec5SDimitry Andric       *static_cast<const LanaiInstrInfo *>(STI.getInstrInfo());
950b57cec5SDimitry Andric   MachineBasicBlock::iterator MBBI = MBB.begin();
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric   // Debug location must be unknown since the first debug location is used
980b57cec5SDimitry Andric   // to determine the end of the prologue.
990b57cec5SDimitry Andric   DebugLoc DL;
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric   // Determine the correct frame layout
1020b57cec5SDimitry Andric   determineFrameLayout(MF);
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric   // FIXME: This appears to be overallocating.  Needs investigation.
1050b57cec5SDimitry Andric   // Get the number of bytes to allocate from the FrameInfo.
1060b57cec5SDimitry Andric   unsigned StackSize = MFI.getStackSize();
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   // Push old FP
1090b57cec5SDimitry Andric   // st %fp,-4[*%sp]
1100b57cec5SDimitry Andric   BuildMI(MBB, MBBI, DL, LII.get(Lanai::SW_RI))
1110b57cec5SDimitry Andric       .addReg(Lanai::FP)
1120b57cec5SDimitry Andric       .addReg(Lanai::SP)
1130b57cec5SDimitry Andric       .addImm(-4)
1140b57cec5SDimitry Andric       .addImm(LPAC::makePreOp(LPAC::ADD))
1150b57cec5SDimitry Andric       .setMIFlag(MachineInstr::FrameSetup);
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   // Generate new FP
1180b57cec5SDimitry Andric   // add %sp,8,%fp
1190b57cec5SDimitry Andric   BuildMI(MBB, MBBI, DL, LII.get(Lanai::ADD_I_LO), Lanai::FP)
1200b57cec5SDimitry Andric       .addReg(Lanai::SP)
1210b57cec5SDimitry Andric       .addImm(8)
1220b57cec5SDimitry Andric       .setMIFlag(MachineInstr::FrameSetup);
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric   // Allocate space on the stack if needed
1250b57cec5SDimitry Andric   // sub %sp,StackSize,%sp
1260b57cec5SDimitry Andric   if (StackSize != 0) {
1270b57cec5SDimitry Andric     BuildMI(MBB, MBBI, DL, LII.get(Lanai::SUB_I_LO), Lanai::SP)
1280b57cec5SDimitry Andric         .addReg(Lanai::SP)
1290b57cec5SDimitry Andric         .addImm(StackSize)
1300b57cec5SDimitry Andric         .setMIFlag(MachineInstr::FrameSetup);
1310b57cec5SDimitry Andric   }
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric   // Replace ADJDYNANALLOC
1340b57cec5SDimitry Andric   if (MFI.hasVarSizedObjects())
1350b57cec5SDimitry Andric     replaceAdjDynAllocPseudo(MF);
1360b57cec5SDimitry Andric }
1370b57cec5SDimitry Andric 
eliminateCallFramePseudoInstr(MachineFunction &,MachineBasicBlock & MBB,MachineBasicBlock::iterator I) const1380b57cec5SDimitry Andric MachineBasicBlock::iterator LanaiFrameLowering::eliminateCallFramePseudoInstr(
1390b57cec5SDimitry Andric     MachineFunction & /*MF*/, MachineBasicBlock &MBB,
1400b57cec5SDimitry Andric     MachineBasicBlock::iterator I) const {
1410b57cec5SDimitry Andric   // Discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
1420b57cec5SDimitry Andric   return MBB.erase(I);
1430b57cec5SDimitry Andric }
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric // The function epilogue should not depend on the current stack pointer!
1460b57cec5SDimitry Andric // It should use the frame pointer only.  This is mandatory because
1470b57cec5SDimitry Andric // of alloca; we also take advantage of it to omit stack adjustments
1480b57cec5SDimitry Andric // before returning.
1490b57cec5SDimitry Andric //
1500b57cec5SDimitry Andric // Note that when we go to restore the preserved register values we must
1510b57cec5SDimitry Andric // not try to address their slots by using offsets from the stack pointer.
1520b57cec5SDimitry Andric // That's because the stack pointer may have been moved during the function
1530b57cec5SDimitry Andric // execution due to a call to alloca().  Rather, we must restore all
1540b57cec5SDimitry Andric // preserved registers via offsets from the frame pointer value.
1550b57cec5SDimitry Andric //
1560b57cec5SDimitry Andric // Note also that when the current frame is being "popped" (by adjusting
1570b57cec5SDimitry Andric // the value of the stack pointer) on function exit, we must (for the
1580b57cec5SDimitry Andric // sake of alloca) set the new value of the stack pointer based upon
1590b57cec5SDimitry Andric // the current value of the frame pointer.  We can't just add what we
1600b57cec5SDimitry Andric // believe to be the (static) frame size to the stack pointer because
1610b57cec5SDimitry Andric // if we did that, and alloca() had been called during this function,
1620b57cec5SDimitry Andric // we would end up returning *without* having fully deallocated all of
1630b57cec5SDimitry Andric // the space grabbed by alloca.  If that happened, and a function
1640b57cec5SDimitry Andric // containing one or more alloca() calls was called over and over again,
1650b57cec5SDimitry Andric // then the stack would grow without limit!
1660b57cec5SDimitry Andric //
1670b57cec5SDimitry Andric // RET is lowered to
1680b57cec5SDimitry Andric //      ld -4[%fp],%pc  # modify %pc (two delay slots)
1690b57cec5SDimitry Andric // as the return address is in the stack frame and mov to pc is allowed.
1700b57cec5SDimitry Andric // emitEpilogue emits
1710b57cec5SDimitry Andric //      mov %fp,%sp     # restore the stack pointer
1720b57cec5SDimitry Andric //      ld -8[%fp],%fp  # restore the caller's frame pointer
1730b57cec5SDimitry Andric // before RET and the delay slot filler will move RET such that these
1740b57cec5SDimitry Andric // instructions execute in the delay slots of the load to PC.
emitEpilogue(MachineFunction &,MachineBasicBlock & MBB) const1750b57cec5SDimitry Andric void LanaiFrameLowering::emitEpilogue(MachineFunction & /*MF*/,
1760b57cec5SDimitry Andric                                       MachineBasicBlock &MBB) const {
1770b57cec5SDimitry Andric   MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
1780b57cec5SDimitry Andric   const LanaiInstrInfo &LII =
1790b57cec5SDimitry Andric       *static_cast<const LanaiInstrInfo *>(STI.getInstrInfo());
1800b57cec5SDimitry Andric   DebugLoc DL = MBBI->getDebugLoc();
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric   // Restore the stack pointer using the callee's frame pointer value.
1830b57cec5SDimitry Andric   BuildMI(MBB, MBBI, DL, LII.get(Lanai::ADD_I_LO), Lanai::SP)
1840b57cec5SDimitry Andric       .addReg(Lanai::FP)
1850b57cec5SDimitry Andric       .addImm(0);
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric   // Restore the frame pointer from the stack.
1880b57cec5SDimitry Andric   BuildMI(MBB, MBBI, DL, LII.get(Lanai::LDW_RI), Lanai::FP)
1890b57cec5SDimitry Andric       .addReg(Lanai::FP)
1900b57cec5SDimitry Andric       .addImm(-8)
1910b57cec5SDimitry Andric       .addImm(LPAC::ADD);
1920b57cec5SDimitry Andric }
1930b57cec5SDimitry Andric 
determineCalleeSaves(MachineFunction & MF,BitVector & SavedRegs,RegScavenger * RS) const1940b57cec5SDimitry Andric void LanaiFrameLowering::determineCalleeSaves(MachineFunction &MF,
1950b57cec5SDimitry Andric                                               BitVector &SavedRegs,
1960b57cec5SDimitry Andric                                               RegScavenger *RS) const {
1970b57cec5SDimitry Andric   TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
2000b57cec5SDimitry Andric   const LanaiRegisterInfo *LRI =
2010b57cec5SDimitry Andric       static_cast<const LanaiRegisterInfo *>(STI.getRegisterInfo());
2020b57cec5SDimitry Andric   int Offset = -4;
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric   // Reserve 4 bytes for the saved RCA
2050b57cec5SDimitry Andric   MFI.CreateFixedObject(4, Offset, true);
2060b57cec5SDimitry Andric   Offset -= 4;
2070b57cec5SDimitry Andric 
2080b57cec5SDimitry Andric   // Reserve 4 bytes for the saved FP
2090b57cec5SDimitry Andric   MFI.CreateFixedObject(4, Offset, true);
2100b57cec5SDimitry Andric   Offset -= 4;
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric   if (LRI->hasBasePointer(MF)) {
2130b57cec5SDimitry Andric     MFI.CreateFixedObject(4, Offset, true);
2140b57cec5SDimitry Andric     SavedRegs.reset(LRI->getBaseRegister());
2150b57cec5SDimitry Andric   }
2160b57cec5SDimitry Andric }
217