17330f729Sjoerg //===-- AVRFrameLowering.cpp - AVR Frame Information ----------------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file contains the AVR implementation of TargetFrameLowering class.
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
127330f729Sjoerg
137330f729Sjoerg #include "AVRFrameLowering.h"
147330f729Sjoerg
157330f729Sjoerg #include "AVR.h"
167330f729Sjoerg #include "AVRInstrInfo.h"
177330f729Sjoerg #include "AVRMachineFunctionInfo.h"
187330f729Sjoerg #include "AVRTargetMachine.h"
197330f729Sjoerg #include "MCTargetDesc/AVRMCTargetDesc.h"
207330f729Sjoerg
217330f729Sjoerg #include "llvm/CodeGen/MachineFrameInfo.h"
227330f729Sjoerg #include "llvm/CodeGen/MachineFunction.h"
237330f729Sjoerg #include "llvm/CodeGen/MachineFunctionPass.h"
247330f729Sjoerg #include "llvm/CodeGen/MachineInstrBuilder.h"
257330f729Sjoerg #include "llvm/CodeGen/MachineRegisterInfo.h"
267330f729Sjoerg #include "llvm/IR/Function.h"
277330f729Sjoerg
287330f729Sjoerg #include <vector>
297330f729Sjoerg
307330f729Sjoerg namespace llvm {
317330f729Sjoerg
AVRFrameLowering()327330f729Sjoerg AVRFrameLowering::AVRFrameLowering()
33*82d56013Sjoerg : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, Align(1), -2) {}
347330f729Sjoerg
canSimplifyCallFramePseudos(const MachineFunction & MF) const357330f729Sjoerg bool AVRFrameLowering::canSimplifyCallFramePseudos(
367330f729Sjoerg const MachineFunction &MF) const {
377330f729Sjoerg // Always simplify call frame pseudo instructions, even when
387330f729Sjoerg // hasReservedCallFrame is false.
397330f729Sjoerg return true;
407330f729Sjoerg }
417330f729Sjoerg
hasReservedCallFrame(const MachineFunction & MF) const427330f729Sjoerg bool AVRFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
437330f729Sjoerg // Reserve call frame memory in function prologue under the following
447330f729Sjoerg // conditions:
457330f729Sjoerg // - Y pointer is reserved to be the frame pointer.
467330f729Sjoerg // - The function does not contain variable sized objects.
477330f729Sjoerg
487330f729Sjoerg const MachineFrameInfo &MFI = MF.getFrameInfo();
497330f729Sjoerg return hasFP(MF) && !MFI.hasVarSizedObjects();
507330f729Sjoerg }
517330f729Sjoerg
emitPrologue(MachineFunction & MF,MachineBasicBlock & MBB) const527330f729Sjoerg void AVRFrameLowering::emitPrologue(MachineFunction &MF,
537330f729Sjoerg MachineBasicBlock &MBB) const {
547330f729Sjoerg MachineBasicBlock::iterator MBBI = MBB.begin();
557330f729Sjoerg DebugLoc DL = (MBBI != MBB.end()) ? MBBI->getDebugLoc() : DebugLoc();
567330f729Sjoerg const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();
577330f729Sjoerg const AVRInstrInfo &TII = *STI.getInstrInfo();
58*82d56013Sjoerg const AVRMachineFunctionInfo *AFI = MF.getInfo<AVRMachineFunctionInfo>();
597330f729Sjoerg bool HasFP = hasFP(MF);
607330f729Sjoerg
617330f729Sjoerg // Interrupt handlers re-enable interrupts in function entry.
62*82d56013Sjoerg if (AFI->isInterruptHandler()) {
637330f729Sjoerg BuildMI(MBB, MBBI, DL, TII.get(AVR::BSETs))
647330f729Sjoerg .addImm(0x07)
657330f729Sjoerg .setMIFlag(MachineInstr::FrameSetup);
667330f729Sjoerg }
677330f729Sjoerg
687330f729Sjoerg // Emit special prologue code to save R1, R0 and SREG in interrupt/signal
697330f729Sjoerg // handlers before saving any other registers.
70*82d56013Sjoerg if (AFI->isInterruptOrSignalHandler()) {
717330f729Sjoerg BuildMI(MBB, MBBI, DL, TII.get(AVR::PUSHWRr))
727330f729Sjoerg .addReg(AVR::R1R0, RegState::Kill)
737330f729Sjoerg .setMIFlag(MachineInstr::FrameSetup);
747330f729Sjoerg
757330f729Sjoerg BuildMI(MBB, MBBI, DL, TII.get(AVR::INRdA), AVR::R0)
767330f729Sjoerg .addImm(0x3f)
777330f729Sjoerg .setMIFlag(MachineInstr::FrameSetup);
787330f729Sjoerg BuildMI(MBB, MBBI, DL, TII.get(AVR::PUSHRr))
797330f729Sjoerg .addReg(AVR::R0, RegState::Kill)
807330f729Sjoerg .setMIFlag(MachineInstr::FrameSetup);
817330f729Sjoerg BuildMI(MBB, MBBI, DL, TII.get(AVR::EORRdRr))
827330f729Sjoerg .addReg(AVR::R0, RegState::Define)
837330f729Sjoerg .addReg(AVR::R0, RegState::Kill)
847330f729Sjoerg .addReg(AVR::R0, RegState::Kill)
857330f729Sjoerg .setMIFlag(MachineInstr::FrameSetup);
867330f729Sjoerg }
877330f729Sjoerg
887330f729Sjoerg // Early exit if the frame pointer is not needed in this function.
897330f729Sjoerg if (!HasFP) {
907330f729Sjoerg return;
917330f729Sjoerg }
927330f729Sjoerg
937330f729Sjoerg const MachineFrameInfo &MFI = MF.getFrameInfo();
947330f729Sjoerg unsigned FrameSize = MFI.getStackSize() - AFI->getCalleeSavedFrameSize();
957330f729Sjoerg
967330f729Sjoerg // Skip the callee-saved push instructions.
977330f729Sjoerg while (
987330f729Sjoerg (MBBI != MBB.end()) && MBBI->getFlag(MachineInstr::FrameSetup) &&
997330f729Sjoerg (MBBI->getOpcode() == AVR::PUSHRr || MBBI->getOpcode() == AVR::PUSHWRr)) {
1007330f729Sjoerg ++MBBI;
1017330f729Sjoerg }
1027330f729Sjoerg
1037330f729Sjoerg // Update Y with the new base value.
1047330f729Sjoerg BuildMI(MBB, MBBI, DL, TII.get(AVR::SPREAD), AVR::R29R28)
1057330f729Sjoerg .addReg(AVR::SP)
1067330f729Sjoerg .setMIFlag(MachineInstr::FrameSetup);
1077330f729Sjoerg
1087330f729Sjoerg // Mark the FramePtr as live-in in every block except the entry.
1097330f729Sjoerg for (MachineFunction::iterator I = std::next(MF.begin()), E = MF.end();
1107330f729Sjoerg I != E; ++I) {
1117330f729Sjoerg I->addLiveIn(AVR::R29R28);
1127330f729Sjoerg }
1137330f729Sjoerg
1147330f729Sjoerg if (!FrameSize) {
1157330f729Sjoerg return;
1167330f729Sjoerg }
1177330f729Sjoerg
1187330f729Sjoerg // Reserve the necessary frame memory by doing FP -= <size>.
1197330f729Sjoerg unsigned Opcode = (isUInt<6>(FrameSize)) ? AVR::SBIWRdK : AVR::SUBIWRdK;
1207330f729Sjoerg
1217330f729Sjoerg MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(Opcode), AVR::R29R28)
1227330f729Sjoerg .addReg(AVR::R29R28, RegState::Kill)
1237330f729Sjoerg .addImm(FrameSize)
1247330f729Sjoerg .setMIFlag(MachineInstr::FrameSetup);
1257330f729Sjoerg // The SREG implicit def is dead.
1267330f729Sjoerg MI->getOperand(3).setIsDead();
1277330f729Sjoerg
1287330f729Sjoerg // Write back R29R28 to SP and temporarily disable interrupts.
1297330f729Sjoerg BuildMI(MBB, MBBI, DL, TII.get(AVR::SPWRITE), AVR::SP)
1307330f729Sjoerg .addReg(AVR::R29R28)
1317330f729Sjoerg .setMIFlag(MachineInstr::FrameSetup);
1327330f729Sjoerg }
1337330f729Sjoerg
restoreStatusRegister(MachineFunction & MF,MachineBasicBlock & MBB)134*82d56013Sjoerg static void restoreStatusRegister(MachineFunction &MF, MachineBasicBlock &MBB) {
135*82d56013Sjoerg const AVRMachineFunctionInfo *AFI = MF.getInfo<AVRMachineFunctionInfo>();
136*82d56013Sjoerg
137*82d56013Sjoerg MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
138*82d56013Sjoerg
139*82d56013Sjoerg DebugLoc DL = MBBI->getDebugLoc();
140*82d56013Sjoerg const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();
141*82d56013Sjoerg const AVRInstrInfo &TII = *STI.getInstrInfo();
142*82d56013Sjoerg
143*82d56013Sjoerg // Emit special epilogue code to restore R1, R0 and SREG in interrupt/signal
144*82d56013Sjoerg // handlers at the very end of the function, just before reti.
145*82d56013Sjoerg if (AFI->isInterruptOrSignalHandler()) {
146*82d56013Sjoerg BuildMI(MBB, MBBI, DL, TII.get(AVR::POPRd), AVR::R0);
147*82d56013Sjoerg BuildMI(MBB, MBBI, DL, TII.get(AVR::OUTARr))
148*82d56013Sjoerg .addImm(0x3f)
149*82d56013Sjoerg .addReg(AVR::R0, RegState::Kill);
150*82d56013Sjoerg BuildMI(MBB, MBBI, DL, TII.get(AVR::POPWRd), AVR::R1R0);
151*82d56013Sjoerg }
152*82d56013Sjoerg }
153*82d56013Sjoerg
emitEpilogue(MachineFunction & MF,MachineBasicBlock & MBB) const1547330f729Sjoerg void AVRFrameLowering::emitEpilogue(MachineFunction &MF,
1557330f729Sjoerg MachineBasicBlock &MBB) const {
156*82d56013Sjoerg const AVRMachineFunctionInfo *AFI = MF.getInfo<AVRMachineFunctionInfo>();
1577330f729Sjoerg
1587330f729Sjoerg // Early exit if the frame pointer is not needed in this function except for
1597330f729Sjoerg // signal/interrupt handlers where special code generation is required.
160*82d56013Sjoerg if (!hasFP(MF) && !AFI->isInterruptOrSignalHandler()) {
1617330f729Sjoerg return;
1627330f729Sjoerg }
1637330f729Sjoerg
1647330f729Sjoerg MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
1657330f729Sjoerg assert(MBBI->getDesc().isReturn() &&
1667330f729Sjoerg "Can only insert epilog into returning blocks");
1677330f729Sjoerg
1687330f729Sjoerg DebugLoc DL = MBBI->getDebugLoc();
1697330f729Sjoerg const MachineFrameInfo &MFI = MF.getFrameInfo();
1707330f729Sjoerg unsigned FrameSize = MFI.getStackSize() - AFI->getCalleeSavedFrameSize();
1717330f729Sjoerg const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();
1727330f729Sjoerg const AVRInstrInfo &TII = *STI.getInstrInfo();
1737330f729Sjoerg
1747330f729Sjoerg // Early exit if there is no need to restore the frame pointer.
1757330f729Sjoerg if (!FrameSize) {
176*82d56013Sjoerg restoreStatusRegister(MF, MBB);
1777330f729Sjoerg return;
1787330f729Sjoerg }
1797330f729Sjoerg
1807330f729Sjoerg // Skip the callee-saved pop instructions.
1817330f729Sjoerg while (MBBI != MBB.begin()) {
1827330f729Sjoerg MachineBasicBlock::iterator PI = std::prev(MBBI);
1837330f729Sjoerg int Opc = PI->getOpcode();
1847330f729Sjoerg
1857330f729Sjoerg if (Opc != AVR::POPRd && Opc != AVR::POPWRd && !PI->isTerminator()) {
1867330f729Sjoerg break;
1877330f729Sjoerg }
1887330f729Sjoerg
1897330f729Sjoerg --MBBI;
1907330f729Sjoerg }
1917330f729Sjoerg
1927330f729Sjoerg unsigned Opcode;
1937330f729Sjoerg
1947330f729Sjoerg // Select the optimal opcode depending on how big it is.
1957330f729Sjoerg if (isUInt<6>(FrameSize)) {
1967330f729Sjoerg Opcode = AVR::ADIWRdK;
1977330f729Sjoerg } else {
1987330f729Sjoerg Opcode = AVR::SUBIWRdK;
1997330f729Sjoerg FrameSize = -FrameSize;
2007330f729Sjoerg }
2017330f729Sjoerg
2027330f729Sjoerg // Restore the frame pointer by doing FP += <size>.
2037330f729Sjoerg MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(Opcode), AVR::R29R28)
2047330f729Sjoerg .addReg(AVR::R29R28, RegState::Kill)
2057330f729Sjoerg .addImm(FrameSize);
2067330f729Sjoerg // The SREG implicit def is dead.
2077330f729Sjoerg MI->getOperand(3).setIsDead();
2087330f729Sjoerg
2097330f729Sjoerg // Write back R29R28 to SP and temporarily disable interrupts.
2107330f729Sjoerg BuildMI(MBB, MBBI, DL, TII.get(AVR::SPWRITE), AVR::SP)
2117330f729Sjoerg .addReg(AVR::R29R28, RegState::Kill);
212*82d56013Sjoerg
213*82d56013Sjoerg restoreStatusRegister(MF, MBB);
2147330f729Sjoerg }
2157330f729Sjoerg
2167330f729Sjoerg // Return true if the specified function should have a dedicated frame
2177330f729Sjoerg // pointer register. This is true if the function meets any of the following
2187330f729Sjoerg // conditions:
2197330f729Sjoerg // - a register has been spilled
2207330f729Sjoerg // - has allocas
2217330f729Sjoerg // - input arguments are passed using the stack
2227330f729Sjoerg //
2237330f729Sjoerg // Notice that strictly this is not a frame pointer because it contains SP after
2247330f729Sjoerg // frame allocation instead of having the original SP in function entry.
hasFP(const MachineFunction & MF) const2257330f729Sjoerg bool AVRFrameLowering::hasFP(const MachineFunction &MF) const {
2267330f729Sjoerg const AVRMachineFunctionInfo *FuncInfo = MF.getInfo<AVRMachineFunctionInfo>();
2277330f729Sjoerg
2287330f729Sjoerg return (FuncInfo->getHasSpills() || FuncInfo->getHasAllocas() ||
2297330f729Sjoerg FuncInfo->getHasStackArgs());
2307330f729Sjoerg }
2317330f729Sjoerg
spillCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,ArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI) const2327330f729Sjoerg bool AVRFrameLowering::spillCalleeSavedRegisters(
2337330f729Sjoerg MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
234*82d56013Sjoerg ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
2357330f729Sjoerg if (CSI.empty()) {
2367330f729Sjoerg return false;
2377330f729Sjoerg }
2387330f729Sjoerg
2397330f729Sjoerg unsigned CalleeFrameSize = 0;
2407330f729Sjoerg DebugLoc DL = MBB.findDebugLoc(MI);
2417330f729Sjoerg MachineFunction &MF = *MBB.getParent();
2427330f729Sjoerg const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();
2437330f729Sjoerg const TargetInstrInfo &TII = *STI.getInstrInfo();
2447330f729Sjoerg AVRMachineFunctionInfo *AVRFI = MF.getInfo<AVRMachineFunctionInfo>();
2457330f729Sjoerg
2467330f729Sjoerg for (unsigned i = CSI.size(); i != 0; --i) {
2477330f729Sjoerg unsigned Reg = CSI[i - 1].getReg();
2487330f729Sjoerg bool IsNotLiveIn = !MBB.isLiveIn(Reg);
2497330f729Sjoerg
2507330f729Sjoerg assert(TRI->getRegSizeInBits(*TRI->getMinimalPhysRegClass(Reg)) == 8 &&
2517330f729Sjoerg "Invalid register size");
2527330f729Sjoerg
2537330f729Sjoerg // Add the callee-saved register as live-in only if it is not already a
2547330f729Sjoerg // live-in register, this usually happens with arguments that are passed
2557330f729Sjoerg // through callee-saved registers.
2567330f729Sjoerg if (IsNotLiveIn) {
2577330f729Sjoerg MBB.addLiveIn(Reg);
2587330f729Sjoerg }
2597330f729Sjoerg
2607330f729Sjoerg // Do not kill the register when it is an input argument.
2617330f729Sjoerg BuildMI(MBB, MI, DL, TII.get(AVR::PUSHRr))
2627330f729Sjoerg .addReg(Reg, getKillRegState(IsNotLiveIn))
2637330f729Sjoerg .setMIFlag(MachineInstr::FrameSetup);
2647330f729Sjoerg ++CalleeFrameSize;
2657330f729Sjoerg }
2667330f729Sjoerg
2677330f729Sjoerg AVRFI->setCalleeSavedFrameSize(CalleeFrameSize);
2687330f729Sjoerg
2697330f729Sjoerg return true;
2707330f729Sjoerg }
2717330f729Sjoerg
restoreCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,MutableArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI) const2727330f729Sjoerg bool AVRFrameLowering::restoreCalleeSavedRegisters(
2737330f729Sjoerg MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
274*82d56013Sjoerg MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
2757330f729Sjoerg if (CSI.empty()) {
2767330f729Sjoerg return false;
2777330f729Sjoerg }
2787330f729Sjoerg
2797330f729Sjoerg DebugLoc DL = MBB.findDebugLoc(MI);
2807330f729Sjoerg const MachineFunction &MF = *MBB.getParent();
2817330f729Sjoerg const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();
2827330f729Sjoerg const TargetInstrInfo &TII = *STI.getInstrInfo();
2837330f729Sjoerg
2847330f729Sjoerg for (const CalleeSavedInfo &CCSI : CSI) {
2857330f729Sjoerg unsigned Reg = CCSI.getReg();
2867330f729Sjoerg
2877330f729Sjoerg assert(TRI->getRegSizeInBits(*TRI->getMinimalPhysRegClass(Reg)) == 8 &&
2887330f729Sjoerg "Invalid register size");
2897330f729Sjoerg
2907330f729Sjoerg BuildMI(MBB, MI, DL, TII.get(AVR::POPRd), Reg);
2917330f729Sjoerg }
2927330f729Sjoerg
2937330f729Sjoerg return true;
2947330f729Sjoerg }
2957330f729Sjoerg
2967330f729Sjoerg /// Replace pseudo store instructions that pass arguments through the stack with
297*82d56013Sjoerg /// real instructions.
fixStackStores(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,const TargetInstrInfo & TII,Register FP)2987330f729Sjoerg static void fixStackStores(MachineBasicBlock &MBB,
2997330f729Sjoerg MachineBasicBlock::iterator MI,
300*82d56013Sjoerg const TargetInstrInfo &TII, Register FP) {
3017330f729Sjoerg // Iterate through the BB until we hit a call instruction or we reach the end.
3027330f729Sjoerg for (auto I = MI, E = MBB.end(); I != E && !I->isCall();) {
3037330f729Sjoerg MachineBasicBlock::iterator NextMI = std::next(I);
3047330f729Sjoerg MachineInstr &MI = *I;
3057330f729Sjoerg unsigned Opcode = I->getOpcode();
3067330f729Sjoerg
3077330f729Sjoerg // Only care of pseudo store instructions where SP is the base pointer.
3087330f729Sjoerg if (Opcode != AVR::STDSPQRr && Opcode != AVR::STDWSPQRr) {
3097330f729Sjoerg I = NextMI;
3107330f729Sjoerg continue;
3117330f729Sjoerg }
3127330f729Sjoerg
3137330f729Sjoerg assert(MI.getOperand(0).getReg() == AVR::SP &&
3147330f729Sjoerg "Invalid register, should be SP!");
3157330f729Sjoerg
3167330f729Sjoerg // Replace this instruction with a regular store. Use Y as the base
3177330f729Sjoerg // pointer since it is guaranteed to contain a copy of SP.
3187330f729Sjoerg unsigned STOpc =
3197330f729Sjoerg (Opcode == AVR::STDWSPQRr) ? AVR::STDWPtrQRr : AVR::STDPtrQRr;
3207330f729Sjoerg
3217330f729Sjoerg MI.setDesc(TII.get(STOpc));
322*82d56013Sjoerg MI.getOperand(0).setReg(FP);
3237330f729Sjoerg
3247330f729Sjoerg I = NextMI;
3257330f729Sjoerg }
3267330f729Sjoerg }
3277330f729Sjoerg
eliminateCallFramePseudoInstr(MachineFunction & MF,MachineBasicBlock & MBB,MachineBasicBlock::iterator MI) const3287330f729Sjoerg MachineBasicBlock::iterator AVRFrameLowering::eliminateCallFramePseudoInstr(
3297330f729Sjoerg MachineFunction &MF, MachineBasicBlock &MBB,
3307330f729Sjoerg MachineBasicBlock::iterator MI) const {
3317330f729Sjoerg const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();
3327330f729Sjoerg const AVRInstrInfo &TII = *STI.getInstrInfo();
3337330f729Sjoerg
3347330f729Sjoerg // There is nothing to insert when the call frame memory is allocated during
3357330f729Sjoerg // function entry. Delete the call frame pseudo and replace all pseudo stores
3367330f729Sjoerg // with real store instructions.
3377330f729Sjoerg if (hasReservedCallFrame(MF)) {
338*82d56013Sjoerg fixStackStores(MBB, MI, TII, AVR::R29R28);
3397330f729Sjoerg return MBB.erase(MI);
3407330f729Sjoerg }
3417330f729Sjoerg
3427330f729Sjoerg DebugLoc DL = MI->getDebugLoc();
3437330f729Sjoerg unsigned int Opcode = MI->getOpcode();
3447330f729Sjoerg int Amount = TII.getFrameSize(*MI);
3457330f729Sjoerg
346*82d56013Sjoerg // ADJCALLSTACKUP and ADJCALLSTACKDOWN are converted to adiw/subi
347*82d56013Sjoerg // instructions to read and write the stack pointer in I/O space.
3487330f729Sjoerg if (Amount != 0) {
349*82d56013Sjoerg assert(getStackAlign() == Align(1) && "Unsupported stack alignment");
3507330f729Sjoerg
3517330f729Sjoerg if (Opcode == TII.getCallFrameSetupOpcode()) {
352*82d56013Sjoerg // Update the stack pointer.
353*82d56013Sjoerg // In many cases this can be done far more efficiently by pushing the
354*82d56013Sjoerg // relevant values directly to the stack. However, doing that correctly
355*82d56013Sjoerg // (in the right order, possibly skipping some empty space for undef
356*82d56013Sjoerg // values, etc) is tricky and thus left to be optimized in the future.
357*82d56013Sjoerg BuildMI(MBB, MI, DL, TII.get(AVR::SPREAD), AVR::R31R30).addReg(AVR::SP);
358*82d56013Sjoerg
359*82d56013Sjoerg MachineInstr *New = BuildMI(MBB, MI, DL, TII.get(AVR::SUBIWRdK), AVR::R31R30)
360*82d56013Sjoerg .addReg(AVR::R31R30, RegState::Kill)
361*82d56013Sjoerg .addImm(Amount);
362*82d56013Sjoerg New->getOperand(3).setIsDead();
363*82d56013Sjoerg
364*82d56013Sjoerg BuildMI(MBB, MI, DL, TII.get(AVR::SPWRITE), AVR::SP)
365*82d56013Sjoerg .addReg(AVR::R31R30);
366*82d56013Sjoerg
367*82d56013Sjoerg // Make sure the remaining stack stores are converted to real store
368*82d56013Sjoerg // instructions.
369*82d56013Sjoerg fixStackStores(MBB, MI, TII, AVR::R31R30);
3707330f729Sjoerg } else {
3717330f729Sjoerg assert(Opcode == TII.getCallFrameDestroyOpcode());
3727330f729Sjoerg
373*82d56013Sjoerg // Note that small stack changes could be implemented more efficiently
374*82d56013Sjoerg // with a few pop instructions instead of the 8-9 instructions now
375*82d56013Sjoerg // required.
376*82d56013Sjoerg
3777330f729Sjoerg // Select the best opcode to adjust SP based on the offset size.
3787330f729Sjoerg unsigned addOpcode;
3797330f729Sjoerg if (isUInt<6>(Amount)) {
3807330f729Sjoerg addOpcode = AVR::ADIWRdK;
3817330f729Sjoerg } else {
3827330f729Sjoerg addOpcode = AVR::SUBIWRdK;
3837330f729Sjoerg Amount = -Amount;
3847330f729Sjoerg }
3857330f729Sjoerg
3867330f729Sjoerg // Build the instruction sequence.
3877330f729Sjoerg BuildMI(MBB, MI, DL, TII.get(AVR::SPREAD), AVR::R31R30).addReg(AVR::SP);
3887330f729Sjoerg
3897330f729Sjoerg MachineInstr *New = BuildMI(MBB, MI, DL, TII.get(addOpcode), AVR::R31R30)
3907330f729Sjoerg .addReg(AVR::R31R30, RegState::Kill)
3917330f729Sjoerg .addImm(Amount);
3927330f729Sjoerg New->getOperand(3).setIsDead();
3937330f729Sjoerg
3947330f729Sjoerg BuildMI(MBB, MI, DL, TII.get(AVR::SPWRITE), AVR::SP)
3957330f729Sjoerg .addReg(AVR::R31R30, RegState::Kill);
3967330f729Sjoerg }
3977330f729Sjoerg }
3987330f729Sjoerg
3997330f729Sjoerg return MBB.erase(MI);
4007330f729Sjoerg }
4017330f729Sjoerg
determineCalleeSaves(MachineFunction & MF,BitVector & SavedRegs,RegScavenger * RS) const4027330f729Sjoerg void AVRFrameLowering::determineCalleeSaves(MachineFunction &MF,
4037330f729Sjoerg BitVector &SavedRegs,
4047330f729Sjoerg RegScavenger *RS) const {
4057330f729Sjoerg TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
4067330f729Sjoerg
4077330f729Sjoerg // If we have a frame pointer, the Y register needs to be saved as well.
408*82d56013Sjoerg if (hasFP(MF)) {
409*82d56013Sjoerg SavedRegs.set(AVR::R29);
410*82d56013Sjoerg SavedRegs.set(AVR::R28);
411*82d56013Sjoerg }
4127330f729Sjoerg }
4137330f729Sjoerg /// The frame analyzer pass.
4147330f729Sjoerg ///
4157330f729Sjoerg /// Scans the function for allocas and used arguments
4167330f729Sjoerg /// that are passed through the stack.
4177330f729Sjoerg struct AVRFrameAnalyzer : public MachineFunctionPass {
4187330f729Sjoerg static char ID;
AVRFrameAnalyzerllvm::AVRFrameAnalyzer4197330f729Sjoerg AVRFrameAnalyzer() : MachineFunctionPass(ID) {}
4207330f729Sjoerg
runOnMachineFunctionllvm::AVRFrameAnalyzer421*82d56013Sjoerg bool runOnMachineFunction(MachineFunction &MF) override {
4227330f729Sjoerg const MachineFrameInfo &MFI = MF.getFrameInfo();
4237330f729Sjoerg AVRMachineFunctionInfo *FuncInfo = MF.getInfo<AVRMachineFunctionInfo>();
4247330f729Sjoerg
4257330f729Sjoerg // If there are no fixed frame indexes during this stage it means there
4267330f729Sjoerg // are allocas present in the function.
4277330f729Sjoerg if (MFI.getNumObjects() != MFI.getNumFixedObjects()) {
4287330f729Sjoerg // Check for the type of allocas present in the function. We only care
4297330f729Sjoerg // about fixed size allocas so do not give false positives if only
4307330f729Sjoerg // variable sized allocas are present.
4317330f729Sjoerg for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
4327330f729Sjoerg // Variable sized objects have size 0.
4337330f729Sjoerg if (MFI.getObjectSize(i)) {
4347330f729Sjoerg FuncInfo->setHasAllocas(true);
4357330f729Sjoerg break;
4367330f729Sjoerg }
4377330f729Sjoerg }
4387330f729Sjoerg }
4397330f729Sjoerg
4407330f729Sjoerg // If there are fixed frame indexes present, scan the function to see if
4417330f729Sjoerg // they are really being used.
4427330f729Sjoerg if (MFI.getNumFixedObjects() == 0) {
4437330f729Sjoerg return false;
4447330f729Sjoerg }
4457330f729Sjoerg
4467330f729Sjoerg // Ok fixed frame indexes present, now scan the function to see if they
4477330f729Sjoerg // are really being used, otherwise we can ignore them.
4487330f729Sjoerg for (const MachineBasicBlock &BB : MF) {
4497330f729Sjoerg for (const MachineInstr &MI : BB) {
4507330f729Sjoerg int Opcode = MI.getOpcode();
4517330f729Sjoerg
4527330f729Sjoerg if ((Opcode != AVR::LDDRdPtrQ) && (Opcode != AVR::LDDWRdPtrQ) &&
4537330f729Sjoerg (Opcode != AVR::STDPtrQRr) && (Opcode != AVR::STDWPtrQRr)) {
4547330f729Sjoerg continue;
4557330f729Sjoerg }
4567330f729Sjoerg
4577330f729Sjoerg for (const MachineOperand &MO : MI.operands()) {
4587330f729Sjoerg if (!MO.isFI()) {
4597330f729Sjoerg continue;
4607330f729Sjoerg }
4617330f729Sjoerg
4627330f729Sjoerg if (MFI.isFixedObjectIndex(MO.getIndex())) {
4637330f729Sjoerg FuncInfo->setHasStackArgs(true);
4647330f729Sjoerg return false;
4657330f729Sjoerg }
4667330f729Sjoerg }
4677330f729Sjoerg }
4687330f729Sjoerg }
4697330f729Sjoerg
4707330f729Sjoerg return false;
4717330f729Sjoerg }
4727330f729Sjoerg
getPassNamellvm::AVRFrameAnalyzer473*82d56013Sjoerg StringRef getPassName() const override { return "AVR Frame Analyzer"; }
4747330f729Sjoerg };
4757330f729Sjoerg
4767330f729Sjoerg char AVRFrameAnalyzer::ID = 0;
4777330f729Sjoerg
4787330f729Sjoerg /// Creates instance of the frame analyzer pass.
createAVRFrameAnalyzerPass()4797330f729Sjoerg FunctionPass *createAVRFrameAnalyzerPass() { return new AVRFrameAnalyzer(); }
4807330f729Sjoerg
4817330f729Sjoerg /// Create the Dynalloca Stack Pointer Save/Restore pass.
4827330f729Sjoerg /// Insert a copy of SP before allocating the dynamic stack memory and restore
4837330f729Sjoerg /// it in function exit to restore the original SP state. This avoids the need
4847330f729Sjoerg /// of reserving a register pair for a frame pointer.
4857330f729Sjoerg struct AVRDynAllocaSR : public MachineFunctionPass {
4867330f729Sjoerg static char ID;
AVRDynAllocaSRllvm::AVRDynAllocaSR4877330f729Sjoerg AVRDynAllocaSR() : MachineFunctionPass(ID) {}
4887330f729Sjoerg
runOnMachineFunctionllvm::AVRDynAllocaSR489*82d56013Sjoerg bool runOnMachineFunction(MachineFunction &MF) override {
4907330f729Sjoerg // Early exit when there are no variable sized objects in the function.
4917330f729Sjoerg if (!MF.getFrameInfo().hasVarSizedObjects()) {
4927330f729Sjoerg return false;
4937330f729Sjoerg }
4947330f729Sjoerg
4957330f729Sjoerg const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();
4967330f729Sjoerg const TargetInstrInfo &TII = *STI.getInstrInfo();
4977330f729Sjoerg MachineBasicBlock &EntryMBB = MF.front();
4987330f729Sjoerg MachineBasicBlock::iterator MBBI = EntryMBB.begin();
4997330f729Sjoerg DebugLoc DL = EntryMBB.findDebugLoc(MBBI);
5007330f729Sjoerg
501*82d56013Sjoerg Register SPCopy =
5027330f729Sjoerg MF.getRegInfo().createVirtualRegister(&AVR::DREGSRegClass);
5037330f729Sjoerg
5047330f729Sjoerg // Create a copy of SP in function entry before any dynallocas are
5057330f729Sjoerg // inserted.
5067330f729Sjoerg BuildMI(EntryMBB, MBBI, DL, TII.get(AVR::COPY), SPCopy).addReg(AVR::SP);
5077330f729Sjoerg
5087330f729Sjoerg // Restore SP in all exit basic blocks.
5097330f729Sjoerg for (MachineBasicBlock &MBB : MF) {
5107330f729Sjoerg // If last instruction is a return instruction, add a restore copy.
5117330f729Sjoerg if (!MBB.empty() && MBB.back().isReturn()) {
5127330f729Sjoerg MBBI = MBB.getLastNonDebugInstr();
5137330f729Sjoerg DL = MBBI->getDebugLoc();
5147330f729Sjoerg BuildMI(MBB, MBBI, DL, TII.get(AVR::COPY), AVR::SP)
5157330f729Sjoerg .addReg(SPCopy, RegState::Kill);
5167330f729Sjoerg }
5177330f729Sjoerg }
5187330f729Sjoerg
5197330f729Sjoerg return true;
5207330f729Sjoerg }
5217330f729Sjoerg
getPassNamellvm::AVRDynAllocaSR522*82d56013Sjoerg StringRef getPassName() const override {
5237330f729Sjoerg return "AVR dynalloca stack pointer save/restore";
5247330f729Sjoerg }
5257330f729Sjoerg };
5267330f729Sjoerg
5277330f729Sjoerg char AVRDynAllocaSR::ID = 0;
5287330f729Sjoerg
5297330f729Sjoerg /// createAVRDynAllocaSRPass - returns an instance of the dynalloca stack
5307330f729Sjoerg /// pointer save/restore pass.
createAVRDynAllocaSRPass()5317330f729Sjoerg FunctionPass *createAVRDynAllocaSRPass() { return new AVRDynAllocaSR(); }
5327330f729Sjoerg
5337330f729Sjoerg } // end of namespace llvm
5347330f729Sjoerg
535