1b22310fdSJia Liu //===-- XCoreFrameLowering.cpp - Frame info for XCore Target --------------===//
22f931281SAnton Korobeynikov //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62f931281SAnton Korobeynikov //
72f931281SAnton Korobeynikov //===----------------------------------------------------------------------===//
82f931281SAnton Korobeynikov //
92f931281SAnton Korobeynikov // This file contains XCore frame information that doesn't fit anywhere else
102f931281SAnton Korobeynikov // cleanly...
112f931281SAnton Korobeynikov //
122f931281SAnton Korobeynikov //===----------------------------------------------------------------------===//
132f931281SAnton Korobeynikov 
142f931281SAnton Korobeynikov #include "XCoreFrameLowering.h"
15b25fda95SCraig Topper #include "XCore.h"
162f931281SAnton Korobeynikov #include "XCoreInstrInfo.h"
172f931281SAnton Korobeynikov #include "XCoreMachineFunctionInfo.h"
18d913448bSEric Christopher #include "XCoreSubtarget.h"
192f931281SAnton Korobeynikov #include "llvm/CodeGen/MachineFrameInfo.h"
202f931281SAnton Korobeynikov #include "llvm/CodeGen/MachineFunction.h"
212f931281SAnton Korobeynikov #include "llvm/CodeGen/MachineInstrBuilder.h"
222f931281SAnton Korobeynikov #include "llvm/CodeGen/MachineModuleInfo.h"
232f931281SAnton Korobeynikov #include "llvm/CodeGen/MachineRegisterInfo.h"
242f931281SAnton Korobeynikov #include "llvm/CodeGen/RegisterScavenging.h"
25b3bde2eaSDavid Blaikie #include "llvm/CodeGen/TargetLowering.h"
269fb823bbSChandler Carruth #include "llvm/IR/DataLayout.h"
279fb823bbSChandler Carruth #include "llvm/IR/Function.h"
282f931281SAnton Korobeynikov #include "llvm/Support/ErrorHandling.h"
29ed0881b2SChandler Carruth #include "llvm/Target/TargetOptions.h"
30*aba43035SDmitri Gribenko #include <algorithm>
31af6c256cSRobert Lytton 
322f931281SAnton Korobeynikov using namespace llvm;
332f931281SAnton Korobeynikov 
34a9f984fbSRobert Lytton static const unsigned FramePtr = XCore::R10;
35a9f984fbSRobert Lytton static const int MaxImmU16 = (1<<16) - 1;
36a9f984fbSRobert Lytton 
372f931281SAnton Korobeynikov // helper functions. FIXME: Eliminate.
isImmU6(unsigned val)382f931281SAnton Korobeynikov static inline bool isImmU6(unsigned val) {
392f931281SAnton Korobeynikov   return val < (1 << 6);
402f931281SAnton Korobeynikov }
412f931281SAnton Korobeynikov 
isImmU16(unsigned val)422f931281SAnton Korobeynikov static inline bool isImmU16(unsigned val) {
432f931281SAnton Korobeynikov   return val < (1 << 16);
442f931281SAnton Korobeynikov }
452f931281SAnton Korobeynikov 
4619ed0d05SRobert Lytton // Helper structure with compare function for handling stack slots.
4719ed0d05SRobert Lytton namespace {
4819ed0d05SRobert Lytton struct StackSlotInfo {
4919ed0d05SRobert Lytton   int FI;
5019ed0d05SRobert Lytton   int Offset;
5119ed0d05SRobert Lytton   unsigned Reg;
StackSlotInfo__anon274fd6790111::StackSlotInfo5219ed0d05SRobert Lytton   StackSlotInfo(int f, int o, int r) : FI(f), Offset(o), Reg(r){};
5319ed0d05SRobert Lytton };
5419ed0d05SRobert Lytton }  // end anonymous namespace
5519ed0d05SRobert Lytton 
CompareSSIOffset(const StackSlotInfo & a,const StackSlotInfo & b)5619ed0d05SRobert Lytton static bool CompareSSIOffset(const StackSlotInfo& a, const StackSlotInfo& b) {
5719ed0d05SRobert Lytton   return a.Offset < b.Offset;
5819ed0d05SRobert Lytton }
5919ed0d05SRobert Lytton 
EmitDefCfaRegister(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,const DebugLoc & dl,const TargetInstrInfo & TII,MachineFunction & MF,unsigned DRegNum)60a9f984fbSRobert Lytton static void EmitDefCfaRegister(MachineBasicBlock &MBB,
61bdc4956bSBenjamin Kramer                                MachineBasicBlock::iterator MBBI,
62bdc4956bSBenjamin Kramer                                const DebugLoc &dl, const TargetInstrInfo &TII,
63f23ef437SMatthias Braun                                MachineFunction &MF, unsigned DRegNum) {
64f23ef437SMatthias Braun   unsigned CFIIndex = MF.addFrameInst(
65b1f25f1bSRafael Espindola       MCCFIInstruction::createDefCfaRegister(nullptr, DRegNum));
66612bb69bSEric Christopher   BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
67612bb69bSEric Christopher       .addCFIIndex(CFIIndex);
682f931281SAnton Korobeynikov }
692f931281SAnton Korobeynikov 
EmitDefCfaOffset(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,const DebugLoc & dl,const TargetInstrInfo & TII,int Offset)70a9f984fbSRobert Lytton static void EmitDefCfaOffset(MachineBasicBlock &MBB,
71bdc4956bSBenjamin Kramer                              MachineBasicBlock::iterator MBBI,
72bdc4956bSBenjamin Kramer                              const DebugLoc &dl, const TargetInstrInfo &TII,
73f23ef437SMatthias Braun                              int Offset) {
74f23ef437SMatthias Braun   MachineFunction &MF = *MBB.getParent();
75b1f25f1bSRafael Espindola   unsigned CFIIndex =
760840d725SFangrui Song       MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, Offset));
77612bb69bSEric Christopher   BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
78612bb69bSEric Christopher       .addCFIIndex(CFIIndex);
79a9f984fbSRobert Lytton }
802f931281SAnton Korobeynikov 
EmitCfiOffset(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,const DebugLoc & dl,const TargetInstrInfo & TII,unsigned DRegNum,int Offset)81a9f984fbSRobert Lytton static void EmitCfiOffset(MachineBasicBlock &MBB,
82bdc4956bSBenjamin Kramer                           MachineBasicBlock::iterator MBBI, const DebugLoc &dl,
83f23ef437SMatthias Braun                           const TargetInstrInfo &TII, unsigned DRegNum,
84f23ef437SMatthias Braun                           int Offset) {
85f23ef437SMatthias Braun   MachineFunction &MF = *MBB.getParent();
86f23ef437SMatthias Braun   unsigned CFIIndex = MF.addFrameInst(
87b1f25f1bSRafael Espindola       MCCFIInstruction::createOffset(nullptr, DRegNum, Offset));
88612bb69bSEric Christopher   BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
89612bb69bSEric Christopher       .addCFIIndex(CFIIndex);
90a9f984fbSRobert Lytton }
91a9f984fbSRobert Lytton 
92a9f984fbSRobert Lytton /// The SP register is moved in steps of 'MaxImmU16' towards the bottom of the
93a9f984fbSRobert Lytton /// frame. During these steps, it may be necessary to spill registers.
94a9f984fbSRobert Lytton /// IfNeededExtSP emits the necessary EXTSP instructions to move the SP only
95a9f984fbSRobert Lytton /// as far as to make 'OffsetFromBottom' reachable using an STWSP_lru6.
96a9f984fbSRobert Lytton /// \param OffsetFromTop the spill offset from the top of the frame.
97e4b8a2e5SNAKAMURA Takumi /// \param [in,out] Adjusted the current SP offset from the top of the frame.
IfNeededExtSP(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,const DebugLoc & dl,const TargetInstrInfo & TII,int OffsetFromTop,int & Adjusted,int FrameSize,bool emitFrameMoves)98a9f984fbSRobert Lytton static void IfNeededExtSP(MachineBasicBlock &MBB,
99bdc4956bSBenjamin Kramer                           MachineBasicBlock::iterator MBBI, const DebugLoc &dl,
100f23ef437SMatthias Braun                           const TargetInstrInfo &TII, int OffsetFromTop,
101f23ef437SMatthias Braun                           int &Adjusted, int FrameSize, bool emitFrameMoves) {
102a9f984fbSRobert Lytton   while (OffsetFromTop > Adjusted) {
103a9f984fbSRobert Lytton     assert(Adjusted < FrameSize && "OffsetFromTop is beyond FrameSize");
104a9f984fbSRobert Lytton     int remaining = FrameSize - Adjusted;
105a9f984fbSRobert Lytton     int OpImm = (remaining > MaxImmU16) ? MaxImmU16 : remaining;
106a9f984fbSRobert Lytton     int Opcode = isImmU6(OpImm) ? XCore::EXTSP_u6 : XCore::EXTSP_lu6;
107a9f984fbSRobert Lytton     BuildMI(MBB, MBBI, dl, TII.get(Opcode)).addImm(OpImm);
108a9f984fbSRobert Lytton     Adjusted += OpImm;
109a9f984fbSRobert Lytton     if (emitFrameMoves)
110f23ef437SMatthias Braun       EmitDefCfaOffset(MBB, MBBI, dl, TII, Adjusted*4);
111a9f984fbSRobert Lytton   }
112a9f984fbSRobert Lytton }
113a9f984fbSRobert Lytton 
114a9f984fbSRobert Lytton /// The SP register is moved in steps of 'MaxImmU16' towards the top of the
115a9f984fbSRobert Lytton /// frame. During these steps, it may be necessary to re-load registers.
116a9f984fbSRobert Lytton /// IfNeededLDAWSP emits the necessary LDAWSP instructions to move the SP only
117a9f984fbSRobert Lytton /// as far as to make 'OffsetFromTop' reachable using an LDAWSP_lru6.
118a9f984fbSRobert Lytton /// \param OffsetFromTop the spill offset from the top of the frame.
11940af4505SEric Christopher /// \param [in,out] RemainingAdj the current SP offset from the top of the
12040af4505SEric Christopher /// frame.
IfNeededLDAWSP(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,const DebugLoc & dl,const TargetInstrInfo & TII,int OffsetFromTop,int & RemainingAdj)121a9f984fbSRobert Lytton static void IfNeededLDAWSP(MachineBasicBlock &MBB,
122bdc4956bSBenjamin Kramer                            MachineBasicBlock::iterator MBBI, const DebugLoc &dl,
123a9f984fbSRobert Lytton                            const TargetInstrInfo &TII, int OffsetFromTop,
124a9f984fbSRobert Lytton                            int &RemainingAdj) {
125a9f984fbSRobert Lytton   while (OffsetFromTop < RemainingAdj - MaxImmU16) {
126a9f984fbSRobert Lytton     assert(RemainingAdj && "OffsetFromTop is beyond FrameSize");
127a9f984fbSRobert Lytton     int OpImm = (RemainingAdj > MaxImmU16) ? MaxImmU16 : RemainingAdj;
128a9f984fbSRobert Lytton     int Opcode = isImmU6(OpImm) ? XCore::LDAWSP_ru6 : XCore::LDAWSP_lru6;
129a9f984fbSRobert Lytton     BuildMI(MBB, MBBI, dl, TII.get(Opcode), XCore::SP).addImm(OpImm);
130a9f984fbSRobert Lytton     RemainingAdj -= OpImm;
131a9f984fbSRobert Lytton   }
132a9f984fbSRobert Lytton }
133a9f984fbSRobert Lytton 
134a9f984fbSRobert Lytton /// Creates an ordered list of registers that are spilled
135a9f984fbSRobert Lytton /// during the emitPrologue/emitEpilogue.
136a9f984fbSRobert Lytton /// Registers are ordered according to their frame offset.
137af6c256cSRobert Lytton /// As offsets are negative, the largest offsets will be first.
GetSpillList(SmallVectorImpl<StackSlotInfo> & SpillList,MachineFrameInfo & MFI,XCoreFunctionInfo * XFI,bool fetchLR,bool fetchFP)13819ed0d05SRobert Lytton static void GetSpillList(SmallVectorImpl<StackSlotInfo> &SpillList,
139941a705bSMatthias Braun                          MachineFrameInfo &MFI, XCoreFunctionInfo *XFI,
140a9f984fbSRobert Lytton                          bool fetchLR, bool fetchFP) {
141af6c256cSRobert Lytton   if (fetchLR) {
142941a705bSMatthias Braun     int Offset = MFI.getObjectOffset(XFI->getLRSpillSlot());
14319ed0d05SRobert Lytton     SpillList.push_back(StackSlotInfo(XFI->getLRSpillSlot(),
14419ed0d05SRobert Lytton                                       Offset,
14519ed0d05SRobert Lytton                                       XCore::LR));
146a9f984fbSRobert Lytton   }
147af6c256cSRobert Lytton   if (fetchFP) {
148941a705bSMatthias Braun     int Offset = MFI.getObjectOffset(XFI->getFPSpillSlot());
14919ed0d05SRobert Lytton     SpillList.push_back(StackSlotInfo(XFI->getFPSpillSlot(),
15019ed0d05SRobert Lytton                                       Offset,
15119ed0d05SRobert Lytton                                       FramePtr));
152af6c256cSRobert Lytton   }
1530cac726aSFangrui Song   llvm::sort(SpillList, CompareSSIOffset);
1542f931281SAnton Korobeynikov }
1552f931281SAnton Korobeynikov 
156af6c256cSRobert Lytton /// Creates an ordered list of EH info register 'spills'.
157af6c256cSRobert Lytton /// These slots are only used by the unwinder and calls to llvm.eh.return().
158af6c256cSRobert Lytton /// Registers are ordered according to their frame offset.
159af6c256cSRobert Lytton /// As offsets are negative, the largest offsets will be first.
GetEHSpillList(SmallVectorImpl<StackSlotInfo> & SpillList,MachineFrameInfo & MFI,XCoreFunctionInfo * XFI,const Constant * PersonalityFn,const TargetLowering * TL)16019ed0d05SRobert Lytton static void GetEHSpillList(SmallVectorImpl<StackSlotInfo> &SpillList,
161941a705bSMatthias Braun                            MachineFrameInfo &MFI, XCoreFunctionInfo *XFI,
162f748c893SJoseph Tremoulet                            const Constant *PersonalityFn,
163af6c256cSRobert Lytton                            const TargetLowering *TL) {
164af6c256cSRobert Lytton   assert(XFI->hasEHSpillSlot() && "There are no EH register spill slots");
165af6c256cSRobert Lytton   const int *EHSlot = XFI->getEHSpillSlot();
166f748c893SJoseph Tremoulet   SpillList.push_back(
167941a705bSMatthias Braun       StackSlotInfo(EHSlot[0], MFI.getObjectOffset(EHSlot[0]),
168f748c893SJoseph Tremoulet                     TL->getExceptionPointerRegister(PersonalityFn)));
169f748c893SJoseph Tremoulet   SpillList.push_back(
170941a705bSMatthias Braun       StackSlotInfo(EHSlot[0], MFI.getObjectOffset(EHSlot[1]),
171f748c893SJoseph Tremoulet                     TL->getExceptionSelectorRegister(PersonalityFn)));
1720cac726aSFangrui Song   llvm::sort(SpillList, CompareSSIOffset);
173af6c256cSRobert Lytton }
174af6c256cSRobert Lytton 
getFrameIndexMMO(MachineBasicBlock & MBB,int FrameIndex,MachineMemOperand::Flags flags)1750af80cd6SJustin Lebar static MachineMemOperand *getFrameIndexMMO(MachineBasicBlock &MBB,
1760af80cd6SJustin Lebar                                            int FrameIndex,
1770af80cd6SJustin Lebar                                            MachineMemOperand::Flags flags) {
17819ed0d05SRobert Lytton   MachineFunction *MF = MBB.getParent();
179941a705bSMatthias Braun   const MachineFrameInfo &MFI = MF->getFrameInfo();
180e40c8a2bSAlex Lorenz   MachineMemOperand *MMO = MF->getMachineMemOperand(
181e40c8a2bSAlex Lorenz       MachinePointerInfo::getFixedStack(*MF, FrameIndex), flags,
182bdf77209SGuillaume Chatelet       MFI.getObjectSize(FrameIndex), MFI.getObjectAlign(FrameIndex));
18319ed0d05SRobert Lytton   return MMO;
18419ed0d05SRobert Lytton }
18519ed0d05SRobert Lytton 
18619ed0d05SRobert Lytton 
187af6c256cSRobert Lytton /// Restore clobbered registers with their spill slot value.
188af6c256cSRobert Lytton /// The SP will be adjusted at the same time, thus the SpillList must be ordered
189af6c256cSRobert Lytton /// with the largest (negative) offsets first.
RestoreSpillList(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,const DebugLoc & dl,const TargetInstrInfo & TII,int & RemainingAdj,SmallVectorImpl<StackSlotInfo> & SpillList)190bdc4956bSBenjamin Kramer static void RestoreSpillList(MachineBasicBlock &MBB,
191bdc4956bSBenjamin Kramer                              MachineBasicBlock::iterator MBBI,
192bdc4956bSBenjamin Kramer                              const DebugLoc &dl, const TargetInstrInfo &TII,
193bdc4956bSBenjamin Kramer                              int &RemainingAdj,
19419ed0d05SRobert Lytton                              SmallVectorImpl<StackSlotInfo> &SpillList) {
195af6c256cSRobert Lytton   for (unsigned i = 0, e = SpillList.size(); i != e; ++i) {
19619ed0d05SRobert Lytton     assert(SpillList[i].Offset % 4 == 0 && "Misaligned stack offset");
19719ed0d05SRobert Lytton     assert(SpillList[i].Offset <= 0 && "Unexpected positive stack offset");
19819ed0d05SRobert Lytton     int OffsetFromTop = - SpillList[i].Offset/4;
199af6c256cSRobert Lytton     IfNeededLDAWSP(MBB, MBBI, dl, TII, OffsetFromTop, RemainingAdj);
200af6c256cSRobert Lytton     int Offset = RemainingAdj - OffsetFromTop;
201af6c256cSRobert Lytton     int Opcode = isImmU6(Offset) ? XCore::LDWSP_ru6 : XCore::LDWSP_lru6;
20219ed0d05SRobert Lytton     BuildMI(MBB, MBBI, dl, TII.get(Opcode), SpillList[i].Reg)
20319ed0d05SRobert Lytton       .addImm(Offset)
20419ed0d05SRobert Lytton       .addMemOperand(getFrameIndexMMO(MBB, SpillList[i].FI,
20519ed0d05SRobert Lytton                                       MachineMemOperand::MOLoad));
206af6c256cSRobert Lytton   }
207af6c256cSRobert Lytton }
2082f931281SAnton Korobeynikov 
2092f931281SAnton Korobeynikov //===----------------------------------------------------------------------===//
2102f931281SAnton Korobeynikov // XCoreFrameLowering:
2112f931281SAnton Korobeynikov //===----------------------------------------------------------------------===//
2122f931281SAnton Korobeynikov 
XCoreFrameLowering(const XCoreSubtarget & sti)2132f931281SAnton Korobeynikov XCoreFrameLowering::XCoreFrameLowering(const XCoreSubtarget &sti)
214882c43d7SGuillaume Chatelet     : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, Align(4), 0) {
2152f931281SAnton Korobeynikov   // Do nothing
2162f931281SAnton Korobeynikov }
2172f931281SAnton Korobeynikov 
hasFP(const MachineFunction & MF) const2182f931281SAnton Korobeynikov bool XCoreFrameLowering::hasFP(const MachineFunction &MF) const {
21950f02cb2SNick Lewycky   return MF.getTarget().Options.DisableFramePointerElim(MF) ||
220941a705bSMatthias Braun          MF.getFrameInfo().hasVarSizedObjects();
2212f931281SAnton Korobeynikov }
2222f931281SAnton Korobeynikov 
emitPrologue(MachineFunction & MF,MachineBasicBlock & MBB) const22361b305edSQuentin Colombet void XCoreFrameLowering::emitPrologue(MachineFunction &MF,
22461b305edSQuentin Colombet                                       MachineBasicBlock &MBB) const {
22561b305edSQuentin Colombet   assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
2262f931281SAnton Korobeynikov   MachineBasicBlock::iterator MBBI = MBB.begin();
227941a705bSMatthias Braun   MachineFrameInfo &MFI = MF.getFrameInfo();
2282f931281SAnton Korobeynikov   MachineModuleInfo *MMI = &MF.getMMI();
229a83c0482SRobert Lytton   const MCRegisterInfo *MRI = MMI->getContext().getRegisterInfo();
2301c504299SEric Christopher   const XCoreInstrInfo &TII = *MF.getSubtarget<XCoreSubtarget>().getInstrInfo();
2312f931281SAnton Korobeynikov   XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
23237d3fa7eSRobert Lytton   // Debug location must be unknown since the first debug location is used
23337d3fa7eSRobert Lytton   // to determine the end of the prologue.
23437d3fa7eSRobert Lytton   DebugLoc dl;
2352f931281SAnton Korobeynikov 
236c3df69faSGuillaume Chatelet   if (MFI.getMaxAlign() > getStackAlign())
237c3df69faSGuillaume Chatelet     report_fatal_error("emitPrologue unsupported alignment: " +
238c3df69faSGuillaume Chatelet                        Twine(MFI.getMaxAlign().value()));
239ed835b6fSRobert Lytton 
240f1caa283SMatthias Braun   const AttributeList &PAL = MF.getFunction().getAttributes();
2416e95ae80SBill Wendling   if (PAL.hasAttrSomewhere(Attribute::Nest))
242a9f984fbSRobert Lytton     BuildMI(MBB, MBBI, dl, TII.get(XCore::LDWSP_ru6), XCore::R11).addImm(0);
24319ed0d05SRobert Lytton     // FIX: Needs addMemOperand() but can't use getFixedStack() or getStack().
2442f931281SAnton Korobeynikov 
2452f931281SAnton Korobeynikov   // Work out frame sizes.
246a9f984fbSRobert Lytton   // We will adjust the SP in stages towards the final FrameSize.
247941a705bSMatthias Braun   assert(MFI.getStackSize()%4 == 0 && "Misaligned frame size");
248941a705bSMatthias Braun   const int FrameSize = MFI.getStackSize() / 4;
249a9f984fbSRobert Lytton   int Adjusted = 0;
2502f931281SAnton Korobeynikov 
251a53360a3SRobert Lytton   bool saveLR = XFI->hasLRSpillSlot();
252a9f984fbSRobert Lytton   bool UseENTSP = saveLR && FrameSize
253941a705bSMatthias Braun                   && (MFI.getObjectOffset(XFI->getLRSpillSlot()) == 0);
254a9f984fbSRobert Lytton   if (UseENTSP)
2552f931281SAnton Korobeynikov     saveLR = false;
256a9f984fbSRobert Lytton   bool FP = hasFP(MF);
257a9f984fbSRobert Lytton   bool emitFrameMoves = XCoreRegisterInfo::needsFrameMoves(MF);
2582f931281SAnton Korobeynikov 
259a9f984fbSRobert Lytton   if (UseENTSP) {
260a9f984fbSRobert Lytton     // Allocate space on the stack at the same time as saving LR.
261a9f984fbSRobert Lytton     Adjusted = (FrameSize > MaxImmU16) ? MaxImmU16 : FrameSize;
262a9f984fbSRobert Lytton     int Opcode = isImmU6(Adjusted) ? XCore::ENTSP_u6 : XCore::ENTSP_lu6;
2632f931281SAnton Korobeynikov     MBB.addLiveIn(XCore::LR);
264a53360a3SRobert Lytton     MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(Opcode));
265a53360a3SRobert Lytton     MIB.addImm(Adjusted);
266fc6de428SEric Christopher     MIB->addRegisterKilled(XCore::LR, MF.getSubtarget().getRegisterInfo(),
267fc6de428SEric Christopher                            true);
2682f931281SAnton Korobeynikov     if (emitFrameMoves) {
269f23ef437SMatthias Braun       EmitDefCfaOffset(MBB, MBBI, dl, TII, Adjusted*4);
270a9f984fbSRobert Lytton       unsigned DRegNum = MRI->getDwarfRegNum(XCore::LR, true);
271f23ef437SMatthias Braun       EmitCfiOffset(MBB, MBBI, dl, TII, DRegNum, 0);
2722f931281SAnton Korobeynikov     }
2732f931281SAnton Korobeynikov   }
2742f931281SAnton Korobeynikov 
275a9f984fbSRobert Lytton   // If necessary, save LR and FP to the stack, as we EXTSP.
27619ed0d05SRobert Lytton   SmallVector<StackSlotInfo,2> SpillList;
277a9f984fbSRobert Lytton   GetSpillList(SpillList, MFI, XFI, saveLR, FP);
278af6c256cSRobert Lytton   // We want the nearest (negative) offsets first, so reverse list.
279af6c256cSRobert Lytton   std::reverse(SpillList.begin(), SpillList.end());
280a9f984fbSRobert Lytton   for (unsigned i = 0, e = SpillList.size(); i != e; ++i) {
28119ed0d05SRobert Lytton     assert(SpillList[i].Offset % 4 == 0 && "Misaligned stack offset");
28219ed0d05SRobert Lytton     assert(SpillList[i].Offset <= 0 && "Unexpected positive stack offset");
28319ed0d05SRobert Lytton     int OffsetFromTop = - SpillList[i].Offset/4;
284f23ef437SMatthias Braun     IfNeededExtSP(MBB, MBBI, dl, TII, OffsetFromTop, Adjusted, FrameSize,
285a9f984fbSRobert Lytton                   emitFrameMoves);
286a9f984fbSRobert Lytton     int Offset = Adjusted - OffsetFromTop;
287a9f984fbSRobert Lytton     int Opcode = isImmU6(Offset) ? XCore::STWSP_ru6 : XCore::STWSP_lru6;
28819ed0d05SRobert Lytton     MBB.addLiveIn(SpillList[i].Reg);
28919ed0d05SRobert Lytton     BuildMI(MBB, MBBI, dl, TII.get(Opcode))
29019ed0d05SRobert Lytton       .addReg(SpillList[i].Reg, RegState::Kill)
29119ed0d05SRobert Lytton       .addImm(Offset)
29219ed0d05SRobert Lytton       .addMemOperand(getFrameIndexMMO(MBB, SpillList[i].FI,
29319ed0d05SRobert Lytton                                       MachineMemOperand::MOStore));
294a9f984fbSRobert Lytton     if (emitFrameMoves) {
29519ed0d05SRobert Lytton       unsigned DRegNum = MRI->getDwarfRegNum(SpillList[i].Reg, true);
296f23ef437SMatthias Braun       EmitCfiOffset(MBB, MBBI, dl, TII, DRegNum, SpillList[i].Offset);
297a9f984fbSRobert Lytton     }
298a9f984fbSRobert Lytton   }
299a9f984fbSRobert Lytton 
300a9f984fbSRobert Lytton   // Complete any remaining Stack adjustment.
301f23ef437SMatthias Braun   IfNeededExtSP(MBB, MBBI, dl, TII, FrameSize, Adjusted, FrameSize,
302a9f984fbSRobert Lytton                 emitFrameMoves);
303a9f984fbSRobert Lytton   assert(Adjusted==FrameSize && "IfNeededExtSP has not completed adjustment");
304a9f984fbSRobert Lytton 
3052f931281SAnton Korobeynikov   if (FP) {
3062f931281SAnton Korobeynikov     // Set the FP from the SP.
307a83c0482SRobert Lytton     BuildMI(MBB, MBBI, dl, TII.get(XCore::LDAWSP_ru6), FramePtr).addImm(0);
308a9f984fbSRobert Lytton     if (emitFrameMoves)
309f23ef437SMatthias Braun       EmitDefCfaRegister(MBB, MBBI, dl, TII, MF,
310a9f984fbSRobert Lytton                          MRI->getDwarfRegNum(FramePtr, true));
311a83c0482SRobert Lytton   }
312a83c0482SRobert Lytton 
313a83c0482SRobert Lytton   if (emitFrameMoves) {
314a83c0482SRobert Lytton     // Frame moves for callee saved.
315e12a6bacSBenjamin Kramer     for (const auto &SpillLabel : XFI->getSpillLabels()) {
316e12a6bacSBenjamin Kramer       MachineBasicBlock::iterator Pos = SpillLabel.first;
317b1f25f1bSRafael Espindola       ++Pos;
318e12a6bacSBenjamin Kramer       const CalleeSavedInfo &CSI = SpillLabel.second;
319941a705bSMatthias Braun       int Offset = MFI.getObjectOffset(CSI.getFrameIdx());
320a9f984fbSRobert Lytton       unsigned DRegNum = MRI->getDwarfRegNum(CSI.getReg(), true);
321f23ef437SMatthias Braun       EmitCfiOffset(MBB, Pos, dl, TII, DRegNum, Offset);
3222f931281SAnton Korobeynikov     }
323af6c256cSRobert Lytton     if (XFI->hasEHSpillSlot()) {
324af6c256cSRobert Lytton       // The unwinder requires stack slot & CFI offsets for the exception info.
325af6c256cSRobert Lytton       // We do not save/spill these registers.
326f1caa283SMatthias Braun       const Function *Fn = &MF.getFunction();
327f748c893SJoseph Tremoulet       const Constant *PersonalityFn =
328f748c893SJoseph Tremoulet           Fn->hasPersonalityFn() ? Fn->getPersonalityFn() : nullptr;
32919ed0d05SRobert Lytton       SmallVector<StackSlotInfo, 2> SpillList;
330f748c893SJoseph Tremoulet       GetEHSpillList(SpillList, MFI, XFI, PersonalityFn,
331fc6de428SEric Christopher                      MF.getSubtarget().getTargetLowering());
332af6c256cSRobert Lytton       assert(SpillList.size()==2 && "Unexpected SpillList size");
333f23ef437SMatthias Braun       EmitCfiOffset(MBB, MBBI, dl, TII,
33419ed0d05SRobert Lytton                     MRI->getDwarfRegNum(SpillList[0].Reg, true),
335b1f25f1bSRafael Espindola                     SpillList[0].Offset);
336f23ef437SMatthias Braun       EmitCfiOffset(MBB, MBBI, dl, TII,
33719ed0d05SRobert Lytton                     MRI->getDwarfRegNum(SpillList[1].Reg, true),
338b1f25f1bSRafael Espindola                     SpillList[1].Offset);
339af6c256cSRobert Lytton     }
3402f931281SAnton Korobeynikov   }
3412f931281SAnton Korobeynikov }
3422f931281SAnton Korobeynikov 
emitEpilogue(MachineFunction & MF,MachineBasicBlock & MBB) const3432f931281SAnton Korobeynikov void XCoreFrameLowering::emitEpilogue(MachineFunction &MF,
3442f931281SAnton Korobeynikov                                      MachineBasicBlock &MBB) const {
345941a705bSMatthias Braun   MachineFrameInfo &MFI = MF.getFrameInfo();
346bbb1a54bSJakob Stoklund Olesen   MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
3471c504299SEric Christopher   const XCoreInstrInfo &TII = *MF.getSubtarget<XCoreSubtarget>().getInstrInfo();
3481333fa3dSRichard Osborne   XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
3492f931281SAnton Korobeynikov   DebugLoc dl = MBBI->getDebugLoc();
350a53360a3SRobert Lytton   unsigned RetOpcode = MBBI->getOpcode();
3512f931281SAnton Korobeynikov 
352af6c256cSRobert Lytton   // Work out frame sizes.
353af6c256cSRobert Lytton   // We will adjust the SP in stages towards the final FrameSize.
354941a705bSMatthias Braun   int RemainingAdj = MFI.getStackSize();
355af6c256cSRobert Lytton   assert(RemainingAdj%4 == 0 && "Misaligned frame size");
356af6c256cSRobert Lytton   RemainingAdj /= 4;
357af6c256cSRobert Lytton 
358c8c4aa66SRobert Lytton   if (RetOpcode == XCore::EH_RETURN) {
35940af4505SEric Christopher     // 'Restore' the exception info the unwinder has placed into the stack
36040af4505SEric Christopher     // slots.
361f1caa283SMatthias Braun     const Function *Fn = &MF.getFunction();
362f748c893SJoseph Tremoulet     const Constant *PersonalityFn =
363f748c893SJoseph Tremoulet         Fn->hasPersonalityFn() ? Fn->getPersonalityFn() : nullptr;
36419ed0d05SRobert Lytton     SmallVector<StackSlotInfo, 2> SpillList;
365f748c893SJoseph Tremoulet     GetEHSpillList(SpillList, MFI, XFI, PersonalityFn,
366f748c893SJoseph Tremoulet                    MF.getSubtarget().getTargetLowering());
367af6c256cSRobert Lytton     RestoreSpillList(MBB, MBBI, dl, TII, RemainingAdj, SpillList);
368af6c256cSRobert Lytton 
369af6c256cSRobert Lytton     // Return to the landing pad.
3700c476111SDaniel Sanders     Register EhStackReg = MBBI->getOperand(0).getReg();
3710c476111SDaniel Sanders     Register EhHandlerReg = MBBI->getOperand(1).getReg();
372c8c4aa66SRobert Lytton     BuildMI(MBB, MBBI, dl, TII.get(XCore::SETSP_1r)).addReg(EhStackReg);
373c8c4aa66SRobert Lytton     BuildMI(MBB, MBBI, dl, TII.get(XCore::BAU_1r)).addReg(EhHandlerReg);
374c8c4aa66SRobert Lytton     MBB.erase(MBBI);  // Erase the previous return instruction.
375c8c4aa66SRobert Lytton     return;
376c8c4aa66SRobert Lytton   }
377c8c4aa66SRobert Lytton 
378a53360a3SRobert Lytton   bool restoreLR = XFI->hasLRSpillSlot();
379a9f984fbSRobert Lytton   bool UseRETSP = restoreLR && RemainingAdj
380941a705bSMatthias Braun                   && (MFI.getObjectOffset(XFI->getLRSpillSlot()) == 0);
381a9f984fbSRobert Lytton   if (UseRETSP)
3822f931281SAnton Korobeynikov     restoreLR = false;
383a9f984fbSRobert Lytton   bool FP = hasFP(MF);
384a9f984fbSRobert Lytton 
385a9f984fbSRobert Lytton   if (FP) // Restore the stack pointer.
386a9f984fbSRobert Lytton     BuildMI(MBB, MBBI, dl, TII.get(XCore::SETSP_1r)).addReg(FramePtr);
387a9f984fbSRobert Lytton 
388a9f984fbSRobert Lytton   // If necessary, restore LR and FP from the stack, as we EXTSP.
38919ed0d05SRobert Lytton   SmallVector<StackSlotInfo,2> SpillList;
390a9f984fbSRobert Lytton   GetSpillList(SpillList, MFI, XFI, restoreLR, FP);
391af6c256cSRobert Lytton   RestoreSpillList(MBB, MBBI, dl, TII, RemainingAdj, SpillList);
3921333fa3dSRichard Osborne 
393a9f984fbSRobert Lytton   if (RemainingAdj) {
394a9f984fbSRobert Lytton     // Complete all but one of the remaining Stack adjustments.
395a9f984fbSRobert Lytton     IfNeededLDAWSP(MBB, MBBI, dl, TII, 0, RemainingAdj);
396a9f984fbSRobert Lytton     if (UseRETSP) {
3972f931281SAnton Korobeynikov       // Fold prologue into return instruction
398a53360a3SRobert Lytton       assert(RetOpcode == XCore::RETSP_u6
399a53360a3SRobert Lytton              || RetOpcode == XCore::RETSP_lu6);
400a9f984fbSRobert Lytton       int Opcode = isImmU6(RemainingAdj) ? XCore::RETSP_u6 : XCore::RETSP_lu6;
401a9f984fbSRobert Lytton       MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(Opcode))
402a9f984fbSRobert Lytton                                   .addImm(RemainingAdj);
4039ff96e6fSRichard Osborne       for (unsigned i = 3, e = MBBI->getNumOperands(); i < e; ++i)
4049ff96e6fSRichard Osborne         MIB->addOperand(MBBI->getOperand(i)); // copy any variadic operands
405a9f984fbSRobert Lytton       MBB.erase(MBBI);  // Erase the previous return instruction.
4062f931281SAnton Korobeynikov     } else {
407a9f984fbSRobert Lytton       int Opcode = isImmU6(RemainingAdj) ? XCore::LDAWSP_ru6 :
408a9f984fbSRobert Lytton                                            XCore::LDAWSP_lru6;
409a9f984fbSRobert Lytton       BuildMI(MBB, MBBI, dl, TII.get(Opcode), XCore::SP).addImm(RemainingAdj);
410a9f984fbSRobert Lytton       // Don't erase the return instruction.
4112f931281SAnton Korobeynikov     }
412a9f984fbSRobert Lytton   } // else Don't erase the return instruction.
4132f931281SAnton Korobeynikov }
4142f931281SAnton Korobeynikov 
spillCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,ArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI) const415e4230a9fSBenjamin Kramer bool XCoreFrameLowering::spillCalleeSavedRegisters(
416e4230a9fSBenjamin Kramer     MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
417e4230a9fSBenjamin Kramer     ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
4182f931281SAnton Korobeynikov   if (CSI.empty())
4192f931281SAnton Korobeynikov     return true;
4202f931281SAnton Korobeynikov 
4212f931281SAnton Korobeynikov   MachineFunction *MF = MBB.getParent();
422fc6de428SEric Christopher   const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
4232f931281SAnton Korobeynikov   XCoreFunctionInfo *XFI = MF->getInfo<XCoreFunctionInfo>();
4242f931281SAnton Korobeynikov   bool emitFrameMoves = XCoreRegisterInfo::needsFrameMoves(*MF);
4252f931281SAnton Korobeynikov 
4262f931281SAnton Korobeynikov   DebugLoc DL;
427801bf7ebSShiva Chen   if (MI != MBB.end() && !MI->isDebugInstr())
428a9f984fbSRobert Lytton     DL = MI->getDebugLoc();
4292f931281SAnton Korobeynikov 
430ea5421bdSKazu Hirata   for (const CalleeSavedInfo &I : CSI) {
431d6b07348SJim Lin     Register Reg = I.getReg();
4329523aa41SRobert Lytton     assert(Reg != XCore::LR && !(Reg == XCore::R10 && hasFP(*MF)) &&
4339523aa41SRobert Lytton            "LR & FP are always handled in emitPrologue");
4349523aa41SRobert Lytton 
435a53360a3SRobert Lytton     // Add the callee-saved register as live-in. It's killed at the spill.
436a53360a3SRobert Lytton     MBB.addLiveIn(Reg);
4372f931281SAnton Korobeynikov     const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
438ea5421bdSKazu Hirata     TII.storeRegToStackSlot(MBB, MI, Reg, true, I.getFrameIdx(), RC, TRI);
4392f931281SAnton Korobeynikov     if (emitFrameMoves) {
440b1f25f1bSRafael Espindola       auto Store = MI;
441b1f25f1bSRafael Espindola       --Store;
442ea5421bdSKazu Hirata       XFI->getSpillLabels().push_back(std::make_pair(Store, I));
4432f931281SAnton Korobeynikov     }
4442f931281SAnton Korobeynikov   }
4452f931281SAnton Korobeynikov   return true;
4462f931281SAnton Korobeynikov }
4472f931281SAnton Korobeynikov 
restoreCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,MutableArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI) const448186dd631SBenjamin Kramer bool XCoreFrameLowering::restoreCalleeSavedRegisters(
449186dd631SBenjamin Kramer     MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
450186dd631SBenjamin Kramer     MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
4512f931281SAnton Korobeynikov   MachineFunction *MF = MBB.getParent();
452fc6de428SEric Christopher   const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
4532f931281SAnton Korobeynikov   bool AtStart = MI == MBB.begin();
4542f931281SAnton Korobeynikov   MachineBasicBlock::iterator BeforeI = MI;
4552f931281SAnton Korobeynikov   if (!AtStart)
4562f931281SAnton Korobeynikov     --BeforeI;
457186dd631SBenjamin Kramer   for (const CalleeSavedInfo &CSR : CSI) {
458d6b07348SJim Lin     Register Reg = CSR.getReg();
4599523aa41SRobert Lytton     assert(Reg != XCore::LR && !(Reg == XCore::R10 && hasFP(*MF)) &&
4609523aa41SRobert Lytton            "LR & FP are always handled in emitEpilogue");
4619523aa41SRobert Lytton 
4622f931281SAnton Korobeynikov     const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
463186dd631SBenjamin Kramer     TII.loadRegFromStackSlot(MBB, MI, Reg, CSR.getFrameIdx(), RC, TRI);
4642f931281SAnton Korobeynikov     assert(MI != MBB.begin() &&
4652f931281SAnton Korobeynikov            "loadRegFromStackSlot didn't insert any code!");
4662f931281SAnton Korobeynikov     // Insert in reverse order.  loadRegFromStackSlot can insert multiple
4672f931281SAnton Korobeynikov     // instructions.
4682f931281SAnton Korobeynikov     if (AtStart)
4692f931281SAnton Korobeynikov       MI = MBB.begin();
4702f931281SAnton Korobeynikov     else {
4712f931281SAnton Korobeynikov       MI = BeforeI;
4722f931281SAnton Korobeynikov       ++MI;
4732f931281SAnton Korobeynikov     }
4742f931281SAnton Korobeynikov   }
4752f931281SAnton Korobeynikov   return true;
4762f931281SAnton Korobeynikov }
4772f931281SAnton Korobeynikov 
4788da87163SEli Bendersky // This function eliminates ADJCALLSTACKDOWN,
4798da87163SEli Bendersky // ADJCALLSTACKUP pseudo instructions
eliminateCallFramePseudoInstr(MachineFunction & MF,MachineBasicBlock & MBB,MachineBasicBlock::iterator I) const480e1a2e90fSHans Wennborg MachineBasicBlock::iterator XCoreFrameLowering::eliminateCallFramePseudoInstr(
481e1a2e90fSHans Wennborg     MachineFunction &MF, MachineBasicBlock &MBB,
4828da87163SEli Bendersky     MachineBasicBlock::iterator I) const {
4831c504299SEric Christopher   const XCoreInstrInfo &TII = *MF.getSubtarget<XCoreSubtarget>().getInstrInfo();
4848da87163SEli Bendersky   if (!hasReservedCallFrame(MF)) {
4858da87163SEli Bendersky     // Turn the adjcallstackdown instruction into 'extsp <amt>' and the
4868da87163SEli Bendersky     // adjcallstackup instruction into 'ldaw sp, sp[<amt>]'
487e921088cSDuncan P. N. Exon Smith     MachineInstr &Old = *I;
488e921088cSDuncan P. N. Exon Smith     uint64_t Amount = Old.getOperand(0).getImm();
4898da87163SEli Bendersky     if (Amount != 0) {
4908da87163SEli Bendersky       // We need to keep the stack aligned properly.  To do this, we round the
4918da87163SEli Bendersky       // amount of space needed for the outgoing arguments up to the next
4928da87163SEli Bendersky       // alignment boundary.
493c3df69faSGuillaume Chatelet       Amount = alignTo(Amount, getStackAlign());
4948da87163SEli Bendersky 
4958da87163SEli Bendersky       assert(Amount%4 == 0);
4968da87163SEli Bendersky       Amount /= 4;
4978da87163SEli Bendersky 
4988da87163SEli Bendersky       bool isU6 = isImmU6(Amount);
4998da87163SEli Bendersky       if (!isU6 && !isImmU16(Amount)) {
5008da87163SEli Bendersky         // FIX could emit multiple instructions in this case.
5018da87163SEli Bendersky #ifndef NDEBUG
5028da87163SEli Bendersky         errs() << "eliminateCallFramePseudoInstr size too big: "
5038da87163SEli Bendersky                << Amount << "\n";
5048da87163SEli Bendersky #endif
505e73658ddSCraig Topper         llvm_unreachable(nullptr);
5068da87163SEli Bendersky       }
5078da87163SEli Bendersky 
5088da87163SEli Bendersky       MachineInstr *New;
509e921088cSDuncan P. N. Exon Smith       if (Old.getOpcode() == XCore::ADJCALLSTACKDOWN) {
5108da87163SEli Bendersky         int Opcode = isU6 ? XCore::EXTSP_u6 : XCore::EXTSP_lu6;
511e921088cSDuncan P. N. Exon Smith         New = BuildMI(MF, Old.getDebugLoc(), TII.get(Opcode)).addImm(Amount);
5128da87163SEli Bendersky       } else {
513e921088cSDuncan P. N. Exon Smith         assert(Old.getOpcode() == XCore::ADJCALLSTACKUP);
514f18d95f7SRichard Osborne         int Opcode = isU6 ? XCore::LDAWSP_ru6 : XCore::LDAWSP_lru6;
515e921088cSDuncan P. N. Exon Smith         New = BuildMI(MF, Old.getDebugLoc(), TII.get(Opcode), XCore::SP)
5168da87163SEli Bendersky                   .addImm(Amount);
5178da87163SEli Bendersky       }
5188da87163SEli Bendersky 
5198da87163SEli Bendersky       // Replace the pseudo instruction with a new instruction...
5208da87163SEli Bendersky       MBB.insert(I, New);
5218da87163SEli Bendersky     }
5228da87163SEli Bendersky   }
5238da87163SEli Bendersky 
524e1a2e90fSHans Wennborg   return MBB.erase(I);
5258da87163SEli Bendersky }
5268da87163SEli Bendersky 
determineCalleeSaves(MachineFunction & MF,BitVector & SavedRegs,RegScavenger * RS) const52702564865SMatthias Braun void XCoreFrameLowering::determineCalleeSaves(MachineFunction &MF,
52802564865SMatthias Braun                                               BitVector &SavedRegs,
5292f931281SAnton Korobeynikov                                               RegScavenger *RS) const {
53002564865SMatthias Braun   TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
53102564865SMatthias Braun 
5322f931281SAnton Korobeynikov   XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
533a53360a3SRobert Lytton 
53402564865SMatthias Braun   const MachineRegisterInfo &MRI = MF.getRegInfo();
53502564865SMatthias Braun   bool LRUsed = MRI.isPhysRegModified(XCore::LR);
536af6c256cSRobert Lytton 
537f1caa283SMatthias Braun   if (!LRUsed && !MF.getFunction().isVarArg() &&
538941a705bSMatthias Braun       MF.getFrameInfo().estimateStackSize(MF))
539af6c256cSRobert Lytton     // If we need to extend the stack it is more efficient to use entsp / retsp.
540af6c256cSRobert Lytton     // We force the LR to be saved so these instructions are used.
5419523aa41SRobert Lytton     LRUsed = true;
542cbb588a2SRobert Lytton 
543d0ee66c2SMatthias Braun   if (MF.callsUnwindInit() || MF.callsEHReturn()) {
544af6c256cSRobert Lytton     // The unwinder expects to find spill slots for the exception info regs R0
545af6c256cSRobert Lytton     // & R1. These are used during llvm.eh.return() to 'restore' the exception
546af6c256cSRobert Lytton     // info. N.B. we do not spill or restore R0, R1 during normal operation.
547af6c256cSRobert Lytton     XFI->createEHSpillSlot(MF);
548af6c256cSRobert Lytton     // As we will  have a stack, we force the LR to be saved.
549af6c256cSRobert Lytton     LRUsed = true;
550af6c256cSRobert Lytton   }
551af6c256cSRobert Lytton 
552cbb588a2SRobert Lytton   if (LRUsed) {
553af6c256cSRobert Lytton     // We will handle the LR in the prologue/epilogue
554af6c256cSRobert Lytton     // and allocate space on the stack ourselves.
55502564865SMatthias Braun     SavedRegs.reset(XCore::LR);
556a53360a3SRobert Lytton     XFI->createLRSpillSlot(MF);
5572f931281SAnton Korobeynikov   }
558cbb588a2SRobert Lytton 
559af6c256cSRobert Lytton   if (hasFP(MF))
5602f931281SAnton Korobeynikov     // A callee save register is used to hold the FP.
5612f931281SAnton Korobeynikov     // This needs saving / restoring in the epilogue / prologue.
562a53360a3SRobert Lytton     XFI->createFPSpillSlot(MF);
5632f931281SAnton Korobeynikov }
5647fbca3ccSRobert Lytton 
5657fbca3ccSRobert Lytton void XCoreFrameLowering::
processFunctionBeforeFrameFinalized(MachineFunction & MF,RegScavenger * RS) const5667fbca3ccSRobert Lytton processFunctionBeforeFrameFinalized(MachineFunction &MF,
5677fbca3ccSRobert Lytton                                     RegScavenger *RS) const {
5687fbca3ccSRobert Lytton   assert(RS && "requiresRegisterScavenging failed");
569941a705bSMatthias Braun   MachineFrameInfo &MFI = MF.getFrameInfo();
57044e25f37SKrzysztof Parzyszek   const TargetRegisterClass &RC = XCore::GRRegsRegClass;
57144e25f37SKrzysztof Parzyszek   const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
5727fbca3ccSRobert Lytton   XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
5737fbca3ccSRobert Lytton   // Reserve slots close to SP or frame pointer for Scavenging spills.
5747fbca3ccSRobert Lytton   // When using SP for small frames, we don't need any scratch registers.
5757fbca3ccSRobert Lytton   // When using SP for large frames, we may need 2 scratch registers.
5767fbca3ccSRobert Lytton   // When using FP, for large or small frames, we may need 1 scratch register.
57744e25f37SKrzysztof Parzyszek   unsigned Size = TRI.getSpillSize(RC);
578a976ea32SGuillaume Chatelet   Align Alignment = TRI.getSpillAlign(RC);
5797fbca3ccSRobert Lytton   if (XFI->isLargeFrame(MF) || hasFP(MF))
580a976ea32SGuillaume Chatelet     RS->addScavengingFrameIndex(MFI.CreateStackObject(Size, Alignment, false));
5817fbca3ccSRobert Lytton   if (XFI->isLargeFrame(MF) && !hasFP(MF))
582a976ea32SGuillaume Chatelet     RS->addScavengingFrameIndex(MFI.CreateStackObject(Size, Alignment, false));
5832f931281SAnton Korobeynikov }
584