159d6cff9SDimitry Andric //===-- SystemZFrameLowering.cpp - Frame lowering for SystemZ -------------===//
259d6cff9SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
659d6cff9SDimitry Andric //
759d6cff9SDimitry Andric //===----------------------------------------------------------------------===//
859d6cff9SDimitry Andric
959d6cff9SDimitry Andric #include "SystemZFrameLowering.h"
1059d6cff9SDimitry Andric #include "SystemZCallingConv.h"
1159d6cff9SDimitry Andric #include "SystemZInstrBuilder.h"
125ca98fd9SDimitry Andric #include "SystemZInstrInfo.h"
1359d6cff9SDimitry Andric #include "SystemZMachineFunctionInfo.h"
145ca98fd9SDimitry Andric #include "SystemZRegisterInfo.h"
1567c32a98SDimitry Andric #include "SystemZSubtarget.h"
16145449b1SDimitry Andric #include "llvm/CodeGen/LivePhysRegs.h"
1759d6cff9SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
1859d6cff9SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
19f8af5cf6SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h"
2099aabd70SDimitry Andric #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
2159d6cff9SDimitry Andric #include "llvm/IR/Function.h"
22cfca06d7SDimitry Andric #include "llvm/Target/TargetMachine.h"
2359d6cff9SDimitry Andric
2459d6cff9SDimitry Andric using namespace llvm;
2559d6cff9SDimitry Andric
26f8af5cf6SDimitry Andric namespace {
27706b4fc4SDimitry Andric // The ABI-defined register save slots, relative to the CFA (i.e.
28344a3780SDimitry Andric // incoming stack pointer + SystemZMC::ELFCallFrameSize).
29c0981da4SDimitry Andric static const TargetFrameLowering::SpillSlot ELFSpillOffsetTable[] = {
3059d6cff9SDimitry Andric { SystemZ::R2D, 0x10 },
3159d6cff9SDimitry Andric { SystemZ::R3D, 0x18 },
3259d6cff9SDimitry Andric { SystemZ::R4D, 0x20 },
3359d6cff9SDimitry Andric { SystemZ::R5D, 0x28 },
3459d6cff9SDimitry Andric { SystemZ::R6D, 0x30 },
3559d6cff9SDimitry Andric { SystemZ::R7D, 0x38 },
3659d6cff9SDimitry Andric { SystemZ::R8D, 0x40 },
3759d6cff9SDimitry Andric { SystemZ::R9D, 0x48 },
3859d6cff9SDimitry Andric { SystemZ::R10D, 0x50 },
3959d6cff9SDimitry Andric { SystemZ::R11D, 0x58 },
4059d6cff9SDimitry Andric { SystemZ::R12D, 0x60 },
4159d6cff9SDimitry Andric { SystemZ::R13D, 0x68 },
4259d6cff9SDimitry Andric { SystemZ::R14D, 0x70 },
4359d6cff9SDimitry Andric { SystemZ::R15D, 0x78 },
4459d6cff9SDimitry Andric { SystemZ::F0D, 0x80 },
4559d6cff9SDimitry Andric { SystemZ::F2D, 0x88 },
4659d6cff9SDimitry Andric { SystemZ::F4D, 0x90 },
4759d6cff9SDimitry Andric { SystemZ::F6D, 0x98 }
4859d6cff9SDimitry Andric };
49c0981da4SDimitry Andric
50c0981da4SDimitry Andric static const TargetFrameLowering::SpillSlot XPLINKSpillOffsetTable[] = {
51c0981da4SDimitry Andric {SystemZ::R4D, 0x00}, {SystemZ::R5D, 0x08}, {SystemZ::R6D, 0x10},
52c0981da4SDimitry Andric {SystemZ::R7D, 0x18}, {SystemZ::R8D, 0x20}, {SystemZ::R9D, 0x28},
53c0981da4SDimitry Andric {SystemZ::R10D, 0x30}, {SystemZ::R11D, 0x38}, {SystemZ::R12D, 0x40},
54c0981da4SDimitry Andric {SystemZ::R13D, 0x48}, {SystemZ::R14D, 0x50}, {SystemZ::R15D, 0x58}};
555ca98fd9SDimitry Andric } // end anonymous namespace
5659d6cff9SDimitry Andric
SystemZFrameLowering(StackDirection D,Align StackAl,int LAO,Align TransAl,bool StackReal,unsigned PointerSize)57c0981da4SDimitry Andric SystemZFrameLowering::SystemZFrameLowering(StackDirection D, Align StackAl,
58c0981da4SDimitry Andric int LAO, Align TransAl,
59ac9a064cSDimitry Andric bool StackReal, unsigned PointerSize)
60ac9a064cSDimitry Andric : TargetFrameLowering(D, StackAl, LAO, TransAl, StackReal),
61ac9a064cSDimitry Andric PointerSize(PointerSize) {}
62706b4fc4SDimitry Andric
63c0981da4SDimitry Andric std::unique_ptr<SystemZFrameLowering>
create(const SystemZSubtarget & STI)64c0981da4SDimitry Andric SystemZFrameLowering::create(const SystemZSubtarget &STI) {
65ac9a064cSDimitry Andric unsigned PtrSz =
66ac9a064cSDimitry Andric STI.getTargetLowering()->getTargetMachine().getPointerSize(0);
67c0981da4SDimitry Andric if (STI.isTargetXPLINK64())
68ac9a064cSDimitry Andric return std::make_unique<SystemZXPLINKFrameLowering>(PtrSz);
69ac9a064cSDimitry Andric return std::make_unique<SystemZELFFrameLowering>(PtrSz);
70c0981da4SDimitry Andric }
71c0981da4SDimitry Andric
72ecbca9f5SDimitry Andric namespace {
73ecbca9f5SDimitry Andric struct SZFrameSortingObj {
74ecbca9f5SDimitry Andric bool IsValid = false; // True if we care about this Object.
75ecbca9f5SDimitry Andric uint32_t ObjectIndex = 0; // Index of Object into MFI list.
76ecbca9f5SDimitry Andric uint64_t ObjectSize = 0; // Size of Object in bytes.
77ecbca9f5SDimitry Andric uint32_t D12Count = 0; // 12-bit displacement only.
78ecbca9f5SDimitry Andric uint32_t DPairCount = 0; // 12 or 20 bit displacement.
79ecbca9f5SDimitry Andric };
80ecbca9f5SDimitry Andric typedef std::vector<SZFrameSortingObj> SZFrameObjVec;
81ecbca9f5SDimitry Andric } // namespace
82ecbca9f5SDimitry Andric
83ecbca9f5SDimitry Andric // TODO: Move to base class.
orderFrameObjects(const MachineFunction & MF,SmallVectorImpl<int> & ObjectsToAllocate) const84ecbca9f5SDimitry Andric void SystemZELFFrameLowering::orderFrameObjects(
85ecbca9f5SDimitry Andric const MachineFunction &MF, SmallVectorImpl<int> &ObjectsToAllocate) const {
86ecbca9f5SDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo();
87145449b1SDimitry Andric auto *TII = MF.getSubtarget<SystemZSubtarget>().getInstrInfo();
88ecbca9f5SDimitry Andric
89ecbca9f5SDimitry Andric // Make a vector of sorting objects to track all MFI objects and mark those
90ecbca9f5SDimitry Andric // to be sorted as valid.
91ecbca9f5SDimitry Andric if (ObjectsToAllocate.size() <= 1)
92ecbca9f5SDimitry Andric return;
93ecbca9f5SDimitry Andric SZFrameObjVec SortingObjects(MFI.getObjectIndexEnd());
94ecbca9f5SDimitry Andric for (auto &Obj : ObjectsToAllocate) {
95ecbca9f5SDimitry Andric SortingObjects[Obj].IsValid = true;
96ecbca9f5SDimitry Andric SortingObjects[Obj].ObjectIndex = Obj;
97ecbca9f5SDimitry Andric SortingObjects[Obj].ObjectSize = MFI.getObjectSize(Obj);
98ecbca9f5SDimitry Andric }
99ecbca9f5SDimitry Andric
100ecbca9f5SDimitry Andric // Examine uses for each object and record short (12-bit) and "pair"
101ecbca9f5SDimitry Andric // displacement types.
102ecbca9f5SDimitry Andric for (auto &MBB : MF)
103ecbca9f5SDimitry Andric for (auto &MI : MBB) {
104ecbca9f5SDimitry Andric if (MI.isDebugInstr())
105ecbca9f5SDimitry Andric continue;
106ecbca9f5SDimitry Andric for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
107ecbca9f5SDimitry Andric const MachineOperand &MO = MI.getOperand(I);
108ecbca9f5SDimitry Andric if (!MO.isFI())
109ecbca9f5SDimitry Andric continue;
110ecbca9f5SDimitry Andric int Index = MO.getIndex();
111ecbca9f5SDimitry Andric if (Index >= 0 && Index < MFI.getObjectIndexEnd() &&
112ecbca9f5SDimitry Andric SortingObjects[Index].IsValid) {
113ecbca9f5SDimitry Andric if (TII->hasDisplacementPairInsn(MI.getOpcode()))
114ecbca9f5SDimitry Andric SortingObjects[Index].DPairCount++;
115ecbca9f5SDimitry Andric else if (!(MI.getDesc().TSFlags & SystemZII::Has20BitOffset))
116ecbca9f5SDimitry Andric SortingObjects[Index].D12Count++;
117ecbca9f5SDimitry Andric }
118ecbca9f5SDimitry Andric }
119ecbca9f5SDimitry Andric }
120ecbca9f5SDimitry Andric
121ecbca9f5SDimitry Andric // Sort all objects for short/paired displacements, which should be
122ecbca9f5SDimitry Andric // sufficient as it seems like all frame objects typically are within the
123ecbca9f5SDimitry Andric // long displacement range. Sorting works by computing the "density" as
124ecbca9f5SDimitry Andric // Count / ObjectSize. The comparisons of two such fractions are refactored
125ecbca9f5SDimitry Andric // by multiplying both sides with A.ObjectSize * B.ObjectSize, in order to
126ecbca9f5SDimitry Andric // eliminate the (fp) divisions. A higher density object needs to go after
127ecbca9f5SDimitry Andric // in the list in order for it to end up lower on the stack.
128ecbca9f5SDimitry Andric auto CmpD12 = [](const SZFrameSortingObj &A, const SZFrameSortingObj &B) {
129ecbca9f5SDimitry Andric // Put all invalid and variable sized objects at the end.
130ecbca9f5SDimitry Andric if (!A.IsValid || !B.IsValid)
131ecbca9f5SDimitry Andric return A.IsValid;
132ecbca9f5SDimitry Andric if (!A.ObjectSize || !B.ObjectSize)
133ecbca9f5SDimitry Andric return A.ObjectSize > 0;
134ecbca9f5SDimitry Andric uint64_t ADensityCmp = A.D12Count * B.ObjectSize;
135ecbca9f5SDimitry Andric uint64_t BDensityCmp = B.D12Count * A.ObjectSize;
136ecbca9f5SDimitry Andric if (ADensityCmp != BDensityCmp)
137ecbca9f5SDimitry Andric return ADensityCmp < BDensityCmp;
138ecbca9f5SDimitry Andric return A.DPairCount * B.ObjectSize < B.DPairCount * A.ObjectSize;
139ecbca9f5SDimitry Andric };
140ecbca9f5SDimitry Andric std::stable_sort(SortingObjects.begin(), SortingObjects.end(), CmpD12);
141ecbca9f5SDimitry Andric
142ecbca9f5SDimitry Andric // Now modify the original list to represent the final order that
143ecbca9f5SDimitry Andric // we want.
144ecbca9f5SDimitry Andric unsigned Idx = 0;
145ecbca9f5SDimitry Andric for (auto &Obj : SortingObjects) {
146ecbca9f5SDimitry Andric // All invalid items are sorted at the end, so it's safe to stop.
147ecbca9f5SDimitry Andric if (!Obj.IsValid)
148ecbca9f5SDimitry Andric break;
149ecbca9f5SDimitry Andric ObjectsToAllocate[Idx++] = Obj.ObjectIndex;
150ecbca9f5SDimitry Andric }
151ecbca9f5SDimitry Andric }
152ecbca9f5SDimitry Andric
hasReservedCallFrame(const MachineFunction & MF) const153c0981da4SDimitry Andric bool SystemZFrameLowering::hasReservedCallFrame(
154c0981da4SDimitry Andric const MachineFunction &MF) const {
155c0981da4SDimitry Andric // The ELF ABI requires us to allocate 160 bytes of stack space for the
156c0981da4SDimitry Andric // callee, with any outgoing stack arguments being placed above that. It
157c0981da4SDimitry Andric // seems better to make that area a permanent feature of the frame even if
158c0981da4SDimitry Andric // we're using a frame pointer. Similarly, 64-bit XPLINK requires 96 bytes
159c0981da4SDimitry Andric // of stack space for the register save area.
160c0981da4SDimitry Andric return true;
161c0981da4SDimitry Andric }
162c0981da4SDimitry Andric
assignCalleeSavedSpillSlots(MachineFunction & MF,const TargetRegisterInfo * TRI,std::vector<CalleeSavedInfo> & CSI) const163c0981da4SDimitry Andric bool SystemZELFFrameLowering::assignCalleeSavedSpillSlots(
164c0981da4SDimitry Andric MachineFunction &MF, const TargetRegisterInfo *TRI,
165706b4fc4SDimitry Andric std::vector<CalleeSavedInfo> &CSI) const {
166706b4fc4SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
167706b4fc4SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
168706b4fc4SDimitry Andric bool IsVarArg = MF.getFunction().isVarArg();
169706b4fc4SDimitry Andric if (CSI.empty())
170706b4fc4SDimitry Andric return true; // Early exit if no callee saved registers are modified!
171706b4fc4SDimitry Andric
172706b4fc4SDimitry Andric unsigned LowGPR = 0;
173706b4fc4SDimitry Andric unsigned HighGPR = SystemZ::R15D;
174344a3780SDimitry Andric int StartSPOffset = SystemZMC::ELFCallFrameSize;
175706b4fc4SDimitry Andric for (auto &CS : CSI) {
1766f8fc217SDimitry Andric Register Reg = CS.getReg();
177cfca06d7SDimitry Andric int Offset = getRegSpillOffset(MF, Reg);
178706b4fc4SDimitry Andric if (Offset) {
179706b4fc4SDimitry Andric if (SystemZ::GR64BitRegClass.contains(Reg) && StartSPOffset > Offset) {
180706b4fc4SDimitry Andric LowGPR = Reg;
181706b4fc4SDimitry Andric StartSPOffset = Offset;
182706b4fc4SDimitry Andric }
183344a3780SDimitry Andric Offset -= SystemZMC::ELFCallFrameSize;
184ac9a064cSDimitry Andric int FrameIdx =
185ac9a064cSDimitry Andric MFFrame.CreateFixedSpillStackObject(getPointerSize(), Offset);
186706b4fc4SDimitry Andric CS.setFrameIdx(FrameIdx);
187706b4fc4SDimitry Andric } else
188706b4fc4SDimitry Andric CS.setFrameIdx(INT32_MAX);
189706b4fc4SDimitry Andric }
190706b4fc4SDimitry Andric
191706b4fc4SDimitry Andric // Save the range of call-saved registers, for use by the
192706b4fc4SDimitry Andric // prologue/epilogue inserters.
193706b4fc4SDimitry Andric ZFI->setRestoreGPRRegs(LowGPR, HighGPR, StartSPOffset);
194706b4fc4SDimitry Andric if (IsVarArg) {
195706b4fc4SDimitry Andric // Also save the GPR varargs, if any. R6D is call-saved, so would
196706b4fc4SDimitry Andric // already be included, but we also need to handle the call-clobbered
197706b4fc4SDimitry Andric // argument registers.
1986f8fc217SDimitry Andric Register FirstGPR = ZFI->getVarArgsFirstGPR();
199344a3780SDimitry Andric if (FirstGPR < SystemZ::ELFNumArgGPRs) {
200344a3780SDimitry Andric unsigned Reg = SystemZ::ELFArgGPRs[FirstGPR];
201cfca06d7SDimitry Andric int Offset = getRegSpillOffset(MF, Reg);
202706b4fc4SDimitry Andric if (StartSPOffset > Offset) {
203706b4fc4SDimitry Andric LowGPR = Reg; StartSPOffset = Offset;
204706b4fc4SDimitry Andric }
205706b4fc4SDimitry Andric }
206706b4fc4SDimitry Andric }
207706b4fc4SDimitry Andric ZFI->setSpillGPRRegs(LowGPR, HighGPR, StartSPOffset);
208706b4fc4SDimitry Andric
209706b4fc4SDimitry Andric // Create fixed stack objects for the remaining registers.
210344a3780SDimitry Andric int CurrOffset = -SystemZMC::ELFCallFrameSize;
211cfca06d7SDimitry Andric if (usePackedStack(MF))
212cfca06d7SDimitry Andric CurrOffset += StartSPOffset;
213cfca06d7SDimitry Andric
214706b4fc4SDimitry Andric for (auto &CS : CSI) {
215706b4fc4SDimitry Andric if (CS.getFrameIdx() != INT32_MAX)
216706b4fc4SDimitry Andric continue;
2176f8fc217SDimitry Andric Register Reg = CS.getReg();
218706b4fc4SDimitry Andric const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
219706b4fc4SDimitry Andric unsigned Size = TRI->getSpillSize(*RC);
220706b4fc4SDimitry Andric CurrOffset -= Size;
221706b4fc4SDimitry Andric assert(CurrOffset % 8 == 0 &&
222706b4fc4SDimitry Andric "8-byte alignment required for for all register save slots");
223706b4fc4SDimitry Andric int FrameIdx = MFFrame.CreateFixedSpillStackObject(Size, CurrOffset);
224706b4fc4SDimitry Andric CS.setFrameIdx(FrameIdx);
225706b4fc4SDimitry Andric }
226706b4fc4SDimitry Andric
227706b4fc4SDimitry Andric return true;
22859d6cff9SDimitry Andric }
22959d6cff9SDimitry Andric
determineCalleeSaves(MachineFunction & MF,BitVector & SavedRegs,RegScavenger * RS) const230c0981da4SDimitry Andric void SystemZELFFrameLowering::determineCalleeSaves(MachineFunction &MF,
231ee8648bdSDimitry Andric BitVector &SavedRegs,
23259d6cff9SDimitry Andric RegScavenger *RS) const {
233ee8648bdSDimitry Andric TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
234ee8648bdSDimitry Andric
235b915e9e0SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
23667c32a98SDimitry Andric const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
23759d6cff9SDimitry Andric bool HasFP = hasFP(MF);
23859d6cff9SDimitry Andric SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>();
239044eb2f6SDimitry Andric bool IsVarArg = MF.getFunction().isVarArg();
24059d6cff9SDimitry Andric
24159d6cff9SDimitry Andric // va_start stores incoming FPR varargs in the normal way, but delegates
24259d6cff9SDimitry Andric // the saving of incoming GPR varargs to spillCalleeSavedRegisters().
24359d6cff9SDimitry Andric // Record these pending uses, which typically include the call-saved
24459d6cff9SDimitry Andric // argument register R6D.
24559d6cff9SDimitry Andric if (IsVarArg)
246344a3780SDimitry Andric for (unsigned I = MFI->getVarArgsFirstGPR(); I < SystemZ::ELFNumArgGPRs; ++I)
247344a3780SDimitry Andric SavedRegs.set(SystemZ::ELFArgGPRs[I]);
24859d6cff9SDimitry Andric
24901095a5dSDimitry Andric // If there are any landing pads, entering them will modify r6/r7.
250b915e9e0SDimitry Andric if (!MF.getLandingPads().empty()) {
25101095a5dSDimitry Andric SavedRegs.set(SystemZ::R6D);
25201095a5dSDimitry Andric SavedRegs.set(SystemZ::R7D);
25301095a5dSDimitry Andric }
25401095a5dSDimitry Andric
25559d6cff9SDimitry Andric // If the function requires a frame pointer, record that the hard
25659d6cff9SDimitry Andric // frame pointer will be clobbered.
25759d6cff9SDimitry Andric if (HasFP)
258ee8648bdSDimitry Andric SavedRegs.set(SystemZ::R11D);
25959d6cff9SDimitry Andric
26059d6cff9SDimitry Andric // If the function calls other functions, record that the return
26159d6cff9SDimitry Andric // address register will be clobbered.
262b915e9e0SDimitry Andric if (MFFrame.hasCalls())
263ee8648bdSDimitry Andric SavedRegs.set(SystemZ::R14D);
26459d6cff9SDimitry Andric
26559d6cff9SDimitry Andric // If we are saving GPRs other than the stack pointer, we might as well
26659d6cff9SDimitry Andric // save and restore the stack pointer at the same time, via STMG and LMG.
26759d6cff9SDimitry Andric // This allows the deallocation to be done by the LMG, rather than needing
26859d6cff9SDimitry Andric // a separate %r15 addition.
2695ca98fd9SDimitry Andric const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
27059d6cff9SDimitry Andric for (unsigned I = 0; CSRegs[I]; ++I) {
27159d6cff9SDimitry Andric unsigned Reg = CSRegs[I];
272ee8648bdSDimitry Andric if (SystemZ::GR64BitRegClass.contains(Reg) && SavedRegs.test(Reg)) {
273ee8648bdSDimitry Andric SavedRegs.set(SystemZ::R15D);
27459d6cff9SDimitry Andric break;
27559d6cff9SDimitry Andric }
27659d6cff9SDimitry Andric }
27759d6cff9SDimitry Andric }
27859d6cff9SDimitry Andric
SystemZELFFrameLowering(unsigned PointerSize)279ac9a064cSDimitry Andric SystemZELFFrameLowering::SystemZELFFrameLowering(unsigned PointerSize)
280c0981da4SDimitry Andric : SystemZFrameLowering(TargetFrameLowering::StackGrowsDown, Align(8), 0,
281ac9a064cSDimitry Andric Align(8), /* StackRealignable */ false, PointerSize),
282c0981da4SDimitry Andric RegSpillOffsets(0) {
283c0981da4SDimitry Andric
284c0981da4SDimitry Andric // Due to the SystemZ ABI, the DWARF CFA (Canonical Frame Address) is not
285c0981da4SDimitry Andric // equal to the incoming stack pointer, but to incoming stack pointer plus
286c0981da4SDimitry Andric // 160. Instead of using a Local Area Offset, the Register save area will
287c0981da4SDimitry Andric // be occupied by fixed frame objects, and all offsets are actually
288c0981da4SDimitry Andric // relative to CFA.
289c0981da4SDimitry Andric
290c0981da4SDimitry Andric // Create a mapping from register number to save slot offset.
291c0981da4SDimitry Andric // These offsets are relative to the start of the register save area.
292c0981da4SDimitry Andric RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS);
293e3b55780SDimitry Andric for (const auto &Entry : ELFSpillOffsetTable)
294e3b55780SDimitry Andric RegSpillOffsets[Entry.Reg] = Entry.Offset;
295c0981da4SDimitry Andric }
296c0981da4SDimitry Andric
29759d6cff9SDimitry Andric // Add GPR64 to the save instruction being built by MIB, which is in basic
29859d6cff9SDimitry Andric // block MBB. IsImplicit says whether this is an explicit operand to the
29959d6cff9SDimitry Andric // instruction, or an implicit one that comes between the explicit start
30059d6cff9SDimitry Andric // and end registers.
addSavedGPR(MachineBasicBlock & MBB,MachineInstrBuilder & MIB,unsigned GPR64,bool IsImplicit)30159d6cff9SDimitry Andric static void addSavedGPR(MachineBasicBlock &MBB, MachineInstrBuilder &MIB,
30259d6cff9SDimitry Andric unsigned GPR64, bool IsImplicit) {
30367c32a98SDimitry Andric const TargetRegisterInfo *RI =
30467c32a98SDimitry Andric MBB.getParent()->getSubtarget().getRegisterInfo();
3051d5ae102SDimitry Andric Register GPR32 = RI->getSubReg(GPR64, SystemZ::subreg_l32);
30659d6cff9SDimitry Andric bool IsLive = MBB.isLiveIn(GPR64) || MBB.isLiveIn(GPR32);
30759d6cff9SDimitry Andric if (!IsLive || !IsImplicit) {
30859d6cff9SDimitry Andric MIB.addReg(GPR64, getImplRegState(IsImplicit) | getKillRegState(!IsLive));
30959d6cff9SDimitry Andric if (!IsLive)
31059d6cff9SDimitry Andric MBB.addLiveIn(GPR64);
31159d6cff9SDimitry Andric }
31259d6cff9SDimitry Andric }
31359d6cff9SDimitry Andric
spillCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,ArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI) const314c0981da4SDimitry Andric bool SystemZELFFrameLowering::spillCalleeSavedRegisters(
315cfca06d7SDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
316cfca06d7SDimitry Andric ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
31759d6cff9SDimitry Andric if (CSI.empty())
31859d6cff9SDimitry Andric return false;
31959d6cff9SDimitry Andric
32059d6cff9SDimitry Andric MachineFunction &MF = *MBB.getParent();
32167c32a98SDimitry Andric const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
32259d6cff9SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
323044eb2f6SDimitry Andric bool IsVarArg = MF.getFunction().isVarArg();
324dd58ef01SDimitry Andric DebugLoc DL;
32559d6cff9SDimitry Andric
32659d6cff9SDimitry Andric // Save GPRs
327706b4fc4SDimitry Andric SystemZ::GPRRegs SpillGPRs = ZFI->getSpillGPRRegs();
328706b4fc4SDimitry Andric if (SpillGPRs.LowGPR) {
329706b4fc4SDimitry Andric assert(SpillGPRs.LowGPR != SpillGPRs.HighGPR &&
330706b4fc4SDimitry Andric "Should be saving %r15 and something else");
33159d6cff9SDimitry Andric
33259d6cff9SDimitry Andric // Build an STMG instruction.
33359d6cff9SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STMG));
33459d6cff9SDimitry Andric
33559d6cff9SDimitry Andric // Add the explicit register operands.
336706b4fc4SDimitry Andric addSavedGPR(MBB, MIB, SpillGPRs.LowGPR, false);
337706b4fc4SDimitry Andric addSavedGPR(MBB, MIB, SpillGPRs.HighGPR, false);
33859d6cff9SDimitry Andric
33959d6cff9SDimitry Andric // Add the address.
340706b4fc4SDimitry Andric MIB.addReg(SystemZ::R15D).addImm(SpillGPRs.GPROffset);
34159d6cff9SDimitry Andric
34259d6cff9SDimitry Andric // Make sure all call-saved GPRs are included as operands and are
34359d6cff9SDimitry Andric // marked as live on entry.
344f65dcba8SDimitry Andric for (const CalleeSavedInfo &I : CSI) {
3456f8fc217SDimitry Andric Register Reg = I.getReg();
34659d6cff9SDimitry Andric if (SystemZ::GR64BitRegClass.contains(Reg))
3475ca98fd9SDimitry Andric addSavedGPR(MBB, MIB, Reg, true);
34859d6cff9SDimitry Andric }
34959d6cff9SDimitry Andric
35059d6cff9SDimitry Andric // ...likewise GPR varargs.
35159d6cff9SDimitry Andric if (IsVarArg)
352344a3780SDimitry Andric for (unsigned I = ZFI->getVarArgsFirstGPR(); I < SystemZ::ELFNumArgGPRs; ++I)
353344a3780SDimitry Andric addSavedGPR(MBB, MIB, SystemZ::ELFArgGPRs[I], true);
35459d6cff9SDimitry Andric }
35559d6cff9SDimitry Andric
356eb11fae6SDimitry Andric // Save FPRs/VRs in the normal TargetInstrInfo way.
357f65dcba8SDimitry Andric for (const CalleeSavedInfo &I : CSI) {
3586f8fc217SDimitry Andric Register Reg = I.getReg();
35959d6cff9SDimitry Andric if (SystemZ::FP64BitRegClass.contains(Reg)) {
36059d6cff9SDimitry Andric MBB.addLiveIn(Reg);
361f65dcba8SDimitry Andric TII->storeRegToStackSlot(MBB, MBBI, Reg, true, I.getFrameIdx(),
362e3b55780SDimitry Andric &SystemZ::FP64BitRegClass, TRI, Register());
36359d6cff9SDimitry Andric }
364eb11fae6SDimitry Andric if (SystemZ::VR128BitRegClass.contains(Reg)) {
365eb11fae6SDimitry Andric MBB.addLiveIn(Reg);
366f65dcba8SDimitry Andric TII->storeRegToStackSlot(MBB, MBBI, Reg, true, I.getFrameIdx(),
367e3b55780SDimitry Andric &SystemZ::VR128BitRegClass, TRI, Register());
368eb11fae6SDimitry Andric }
36959d6cff9SDimitry Andric }
37059d6cff9SDimitry Andric
37159d6cff9SDimitry Andric return true;
37259d6cff9SDimitry Andric }
37359d6cff9SDimitry Andric
restoreCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,MutableArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI) const374c0981da4SDimitry Andric bool SystemZELFFrameLowering::restoreCalleeSavedRegisters(
375cfca06d7SDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
376cfca06d7SDimitry Andric MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
37759d6cff9SDimitry Andric if (CSI.empty())
37859d6cff9SDimitry Andric return false;
37959d6cff9SDimitry Andric
38059d6cff9SDimitry Andric MachineFunction &MF = *MBB.getParent();
38167c32a98SDimitry Andric const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
38259d6cff9SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
38359d6cff9SDimitry Andric bool HasFP = hasFP(MF);
38459d6cff9SDimitry Andric DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
38559d6cff9SDimitry Andric
386eb11fae6SDimitry Andric // Restore FPRs/VRs in the normal TargetInstrInfo way.
387f65dcba8SDimitry Andric for (const CalleeSavedInfo &I : CSI) {
3886f8fc217SDimitry Andric Register Reg = I.getReg();
38959d6cff9SDimitry Andric if (SystemZ::FP64BitRegClass.contains(Reg))
390f65dcba8SDimitry Andric TII->loadRegFromStackSlot(MBB, MBBI, Reg, I.getFrameIdx(),
391e3b55780SDimitry Andric &SystemZ::FP64BitRegClass, TRI, Register());
392eb11fae6SDimitry Andric if (SystemZ::VR128BitRegClass.contains(Reg))
393f65dcba8SDimitry Andric TII->loadRegFromStackSlot(MBB, MBBI, Reg, I.getFrameIdx(),
394e3b55780SDimitry Andric &SystemZ::VR128BitRegClass, TRI, Register());
39559d6cff9SDimitry Andric }
39659d6cff9SDimitry Andric
39759d6cff9SDimitry Andric // Restore call-saved GPRs (but not call-clobbered varargs, which at
39859d6cff9SDimitry Andric // this point might hold return values).
399706b4fc4SDimitry Andric SystemZ::GPRRegs RestoreGPRs = ZFI->getRestoreGPRRegs();
400706b4fc4SDimitry Andric if (RestoreGPRs.LowGPR) {
40159d6cff9SDimitry Andric // If we saved any of %r2-%r5 as varargs, we should also be saving
40259d6cff9SDimitry Andric // and restoring %r6. If we're saving %r6 or above, we should be
40359d6cff9SDimitry Andric // restoring it too.
404706b4fc4SDimitry Andric assert(RestoreGPRs.LowGPR != RestoreGPRs.HighGPR &&
405706b4fc4SDimitry Andric "Should be loading %r15 and something else");
40659d6cff9SDimitry Andric
40759d6cff9SDimitry Andric // Build an LMG instruction.
40859d6cff9SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LMG));
40959d6cff9SDimitry Andric
41059d6cff9SDimitry Andric // Add the explicit register operands.
411706b4fc4SDimitry Andric MIB.addReg(RestoreGPRs.LowGPR, RegState::Define);
412706b4fc4SDimitry Andric MIB.addReg(RestoreGPRs.HighGPR, RegState::Define);
41359d6cff9SDimitry Andric
41459d6cff9SDimitry Andric // Add the address.
41559d6cff9SDimitry Andric MIB.addReg(HasFP ? SystemZ::R11D : SystemZ::R15D);
416706b4fc4SDimitry Andric MIB.addImm(RestoreGPRs.GPROffset);
41759d6cff9SDimitry Andric
41859d6cff9SDimitry Andric // Do a second scan adding regs as being defined by instruction
419f65dcba8SDimitry Andric for (const CalleeSavedInfo &I : CSI) {
4206f8fc217SDimitry Andric Register Reg = I.getReg();
421706b4fc4SDimitry Andric if (Reg != RestoreGPRs.LowGPR && Reg != RestoreGPRs.HighGPR &&
42201095a5dSDimitry Andric SystemZ::GR64BitRegClass.contains(Reg))
42359d6cff9SDimitry Andric MIB.addReg(Reg, RegState::ImplicitDefine);
42459d6cff9SDimitry Andric }
42559d6cff9SDimitry Andric }
42659d6cff9SDimitry Andric
42759d6cff9SDimitry Andric return true;
42859d6cff9SDimitry Andric }
42959d6cff9SDimitry Andric
processFunctionBeforeFrameFinalized(MachineFunction & MF,RegScavenger * RS) const430c0981da4SDimitry Andric void SystemZELFFrameLowering::processFunctionBeforeFrameFinalized(
431c0981da4SDimitry Andric MachineFunction &MF, RegScavenger *RS) const {
432b915e9e0SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
433b60736ecSDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
434b60736ecSDimitry Andric MachineRegisterInfo *MRI = &MF.getRegInfo();
435b1c73532SDimitry Andric bool BackChain = MF.getSubtarget<SystemZSubtarget>().hasBackChain();
436706b4fc4SDimitry Andric
437cfca06d7SDimitry Andric if (!usePackedStack(MF) || BackChain)
438cfca06d7SDimitry Andric // Create the incoming register save area.
439706b4fc4SDimitry Andric getOrCreateFramePointerSaveIndex(MF);
440706b4fc4SDimitry Andric
44108bbd35aSDimitry Andric // Get the size of our stack frame to be allocated ...
44208bbd35aSDimitry Andric uint64_t StackSize = (MFFrame.estimateStackSize(MF) +
443344a3780SDimitry Andric SystemZMC::ELFCallFrameSize);
44408bbd35aSDimitry Andric // ... and the maximum offset we may need to reach into the
44508bbd35aSDimitry Andric // caller's frame to access the save area or stack arguments.
446706b4fc4SDimitry Andric int64_t MaxArgOffset = 0;
44708bbd35aSDimitry Andric for (int I = MFFrame.getObjectIndexBegin(); I != 0; ++I)
44808bbd35aSDimitry Andric if (MFFrame.getObjectOffset(I) >= 0) {
449706b4fc4SDimitry Andric int64_t ArgOffset = MFFrame.getObjectOffset(I) +
45008bbd35aSDimitry Andric MFFrame.getObjectSize(I);
45108bbd35aSDimitry Andric MaxArgOffset = std::max(MaxArgOffset, ArgOffset);
45208bbd35aSDimitry Andric }
45308bbd35aSDimitry Andric
45408bbd35aSDimitry Andric uint64_t MaxReach = StackSize + MaxArgOffset;
455f8af5cf6SDimitry Andric if (!isUInt<12>(MaxReach)) {
456f8af5cf6SDimitry Andric // We may need register scavenging slots if some parts of the frame
457f8af5cf6SDimitry Andric // are outside the reach of an unsigned 12-bit displacement.
458f8af5cf6SDimitry Andric // Create 2 for the case where both addresses in an MVC are
459f8af5cf6SDimitry Andric // out of range.
460ac9a064cSDimitry Andric RS->addScavengingFrameIndex(
461ac9a064cSDimitry Andric MFFrame.CreateStackObject(getPointerSize(), Align(8), false));
462ac9a064cSDimitry Andric RS->addScavengingFrameIndex(
463ac9a064cSDimitry Andric MFFrame.CreateStackObject(getPointerSize(), Align(8), false));
464f8af5cf6SDimitry Andric }
465b60736ecSDimitry Andric
466b60736ecSDimitry Andric // If R6 is used as an argument register it is still callee saved. If it in
467b60736ecSDimitry Andric // this case is not clobbered (and restored) it should never be marked as
468b60736ecSDimitry Andric // killed.
469b60736ecSDimitry Andric if (MF.front().isLiveIn(SystemZ::R6D) &&
470b60736ecSDimitry Andric ZFI->getRestoreGPRRegs().LowGPR != SystemZ::R6D)
471b60736ecSDimitry Andric for (auto &MO : MRI->use_nodbg_operands(SystemZ::R6D))
472b60736ecSDimitry Andric MO.setIsKill(false);
473f8af5cf6SDimitry Andric }
474f8af5cf6SDimitry Andric
47559d6cff9SDimitry Andric // Emit instructions before MBBI (in MBB) to add NumBytes to Reg.
emitIncrement(MachineBasicBlock & MBB,MachineBasicBlock::iterator & MBBI,const DebugLoc & DL,Register Reg,int64_t NumBytes,const TargetInstrInfo * TII)47659d6cff9SDimitry Andric static void emitIncrement(MachineBasicBlock &MBB,
477cfca06d7SDimitry Andric MachineBasicBlock::iterator &MBBI, const DebugLoc &DL,
478cfca06d7SDimitry Andric Register Reg, int64_t NumBytes,
47959d6cff9SDimitry Andric const TargetInstrInfo *TII) {
48059d6cff9SDimitry Andric while (NumBytes) {
48159d6cff9SDimitry Andric unsigned Opcode;
48259d6cff9SDimitry Andric int64_t ThisVal = NumBytes;
48359d6cff9SDimitry Andric if (isInt<16>(NumBytes))
48459d6cff9SDimitry Andric Opcode = SystemZ::AGHI;
48559d6cff9SDimitry Andric else {
48659d6cff9SDimitry Andric Opcode = SystemZ::AGFI;
48759d6cff9SDimitry Andric // Make sure we maintain 8-byte stack alignment.
48867c32a98SDimitry Andric int64_t MinVal = -uint64_t(1) << 31;
48959d6cff9SDimitry Andric int64_t MaxVal = (int64_t(1) << 31) - 8;
49059d6cff9SDimitry Andric if (ThisVal < MinVal)
49159d6cff9SDimitry Andric ThisVal = MinVal;
49259d6cff9SDimitry Andric else if (ThisVal > MaxVal)
49359d6cff9SDimitry Andric ThisVal = MaxVal;
49459d6cff9SDimitry Andric }
49559d6cff9SDimitry Andric MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII->get(Opcode), Reg)
49659d6cff9SDimitry Andric .addReg(Reg).addImm(ThisVal);
497f8af5cf6SDimitry Andric // The CC implicit def is dead.
49859d6cff9SDimitry Andric MI->getOperand(3).setIsDead();
49959d6cff9SDimitry Andric NumBytes -= ThisVal;
50059d6cff9SDimitry Andric }
50159d6cff9SDimitry Andric }
50259d6cff9SDimitry Andric
503cfca06d7SDimitry Andric // Add CFI for the new CFA offset.
buildCFAOffs(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,const DebugLoc & DL,int Offset,const SystemZInstrInfo * ZII)504cfca06d7SDimitry Andric static void buildCFAOffs(MachineBasicBlock &MBB,
505cfca06d7SDimitry Andric MachineBasicBlock::iterator MBBI,
506cfca06d7SDimitry Andric const DebugLoc &DL, int Offset,
507cfca06d7SDimitry Andric const SystemZInstrInfo *ZII) {
508cfca06d7SDimitry Andric unsigned CFIIndex = MBB.getParent()->addFrameInst(
509cfca06d7SDimitry Andric MCCFIInstruction::cfiDefCfaOffset(nullptr, -Offset));
510cfca06d7SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
511cfca06d7SDimitry Andric .addCFIIndex(CFIIndex);
512cfca06d7SDimitry Andric }
513cfca06d7SDimitry Andric
514cfca06d7SDimitry Andric // Add CFI for the new frame location.
buildDefCFAReg(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,const DebugLoc & DL,unsigned Reg,const SystemZInstrInfo * ZII)515cfca06d7SDimitry Andric static void buildDefCFAReg(MachineBasicBlock &MBB,
516cfca06d7SDimitry Andric MachineBasicBlock::iterator MBBI,
517cfca06d7SDimitry Andric const DebugLoc &DL, unsigned Reg,
518cfca06d7SDimitry Andric const SystemZInstrInfo *ZII) {
519cfca06d7SDimitry Andric MachineFunction &MF = *MBB.getParent();
520ac9a064cSDimitry Andric const MCRegisterInfo *MRI = MF.getContext().getRegisterInfo();
521cfca06d7SDimitry Andric unsigned RegNum = MRI->getDwarfRegNum(Reg, true);
522cfca06d7SDimitry Andric unsigned CFIIndex = MF.addFrameInst(
523cfca06d7SDimitry Andric MCCFIInstruction::createDefCfaRegister(nullptr, RegNum));
524cfca06d7SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
525cfca06d7SDimitry Andric .addCFIIndex(CFIIndex);
526cfca06d7SDimitry Andric }
527cfca06d7SDimitry Andric
emitPrologue(MachineFunction & MF,MachineBasicBlock & MBB) const528c0981da4SDimitry Andric void SystemZELFFrameLowering::emitPrologue(MachineFunction &MF,
5295a5ac124SDimitry Andric MachineBasicBlock &MBB) const {
5305a5ac124SDimitry Andric assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
531cfca06d7SDimitry Andric const SystemZSubtarget &STI = MF.getSubtarget<SystemZSubtarget>();
532cfca06d7SDimitry Andric const SystemZTargetLowering &TLI = *STI.getTargetLowering();
533b915e9e0SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
534cfca06d7SDimitry Andric auto *ZII = static_cast<const SystemZInstrInfo *>(STI.getInstrInfo());
53559d6cff9SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
53659d6cff9SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.begin();
537ac9a064cSDimitry Andric const MCRegisterInfo *MRI = MF.getContext().getRegisterInfo();
538b915e9e0SDimitry Andric const std::vector<CalleeSavedInfo> &CSI = MFFrame.getCalleeSavedInfo();
53959d6cff9SDimitry Andric bool HasFP = hasFP(MF);
540dd58ef01SDimitry Andric
541706b4fc4SDimitry Andric // In GHC calling convention C stack space, including the ABI-defined
542706b4fc4SDimitry Andric // 160-byte base area, is (de)allocated by GHC itself. This stack space may
543706b4fc4SDimitry Andric // be used by LLVM as spill slots for the tail recursive GHC functions. Thus
544706b4fc4SDimitry Andric // do not allocate stack space here, too.
545706b4fc4SDimitry Andric if (MF.getFunction().getCallingConv() == CallingConv::GHC) {
546706b4fc4SDimitry Andric if (MFFrame.getStackSize() > 2048 * sizeof(long)) {
547706b4fc4SDimitry Andric report_fatal_error(
548706b4fc4SDimitry Andric "Pre allocated stack space for GHC function is too small");
549706b4fc4SDimitry Andric }
550706b4fc4SDimitry Andric if (HasFP) {
551706b4fc4SDimitry Andric report_fatal_error(
552706b4fc4SDimitry Andric "In GHC calling convention a frame pointer is not supported");
553706b4fc4SDimitry Andric }
554344a3780SDimitry Andric MFFrame.setStackSize(MFFrame.getStackSize() + SystemZMC::ELFCallFrameSize);
555706b4fc4SDimitry Andric return;
556706b4fc4SDimitry Andric }
557706b4fc4SDimitry Andric
558dd58ef01SDimitry Andric // Debug location must be unknown since the first debug location is used
559dd58ef01SDimitry Andric // to determine the end of the prologue.
560dd58ef01SDimitry Andric DebugLoc DL;
56159d6cff9SDimitry Andric
56259d6cff9SDimitry Andric // The current offset of the stack pointer from the CFA.
563344a3780SDimitry Andric int64_t SPOffsetFromCFA = -SystemZMC::ELFCFAOffsetFromInitialSP;
56459d6cff9SDimitry Andric
565706b4fc4SDimitry Andric if (ZFI->getSpillGPRRegs().LowGPR) {
56659d6cff9SDimitry Andric // Skip over the GPR saves.
56759d6cff9SDimitry Andric if (MBBI != MBB.end() && MBBI->getOpcode() == SystemZ::STMG)
56859d6cff9SDimitry Andric ++MBBI;
56959d6cff9SDimitry Andric else
57059d6cff9SDimitry Andric llvm_unreachable("Couldn't skip over GPR saves");
57159d6cff9SDimitry Andric
57259d6cff9SDimitry Andric // Add CFI for the GPR saves.
5735ca98fd9SDimitry Andric for (auto &Save : CSI) {
5746f8fc217SDimitry Andric Register Reg = Save.getReg();
57559d6cff9SDimitry Andric if (SystemZ::GR64BitRegClass.contains(Reg)) {
576706b4fc4SDimitry Andric int FI = Save.getFrameIdx();
577706b4fc4SDimitry Andric int64_t Offset = MFFrame.getObjectOffset(FI);
578b915e9e0SDimitry Andric unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
5795ca98fd9SDimitry Andric nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
5805ca98fd9SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
5815ca98fd9SDimitry Andric .addCFIIndex(CFIIndex);
58259d6cff9SDimitry Andric }
58359d6cff9SDimitry Andric }
58459d6cff9SDimitry Andric }
58559d6cff9SDimitry Andric
586eb11fae6SDimitry Andric uint64_t StackSize = MFFrame.getStackSize();
587eb11fae6SDimitry Andric // We need to allocate the ABI-defined 160-byte base area whenever
588eb11fae6SDimitry Andric // we allocate stack space for our own use and whenever we call another
589eb11fae6SDimitry Andric // function.
590706b4fc4SDimitry Andric bool HasStackObject = false;
591706b4fc4SDimitry Andric for (unsigned i = 0, e = MFFrame.getObjectIndexEnd(); i != e; ++i)
592706b4fc4SDimitry Andric if (!MFFrame.isDeadObjectIndex(i)) {
593706b4fc4SDimitry Andric HasStackObject = true;
594706b4fc4SDimitry Andric break;
595eb11fae6SDimitry Andric }
596706b4fc4SDimitry Andric if (HasStackObject || MFFrame.hasCalls())
597344a3780SDimitry Andric StackSize += SystemZMC::ELFCallFrameSize;
598706b4fc4SDimitry Andric // Don't allocate the incoming reg save area.
599344a3780SDimitry Andric StackSize = StackSize > SystemZMC::ELFCallFrameSize
600344a3780SDimitry Andric ? StackSize - SystemZMC::ELFCallFrameSize
601706b4fc4SDimitry Andric : 0;
602706b4fc4SDimitry Andric MFFrame.setStackSize(StackSize);
603eb11fae6SDimitry Andric
60459d6cff9SDimitry Andric if (StackSize) {
60559d6cff9SDimitry Andric // Allocate StackSize bytes.
60659d6cff9SDimitry Andric int64_t Delta = -int64_t(StackSize);
607cfca06d7SDimitry Andric const unsigned ProbeSize = TLI.getStackProbeSize(MF);
608cfca06d7SDimitry Andric bool FreeProbe = (ZFI->getSpillGPRRegs().GPROffset &&
609cfca06d7SDimitry Andric (ZFI->getSpillGPRRegs().GPROffset + StackSize) < ProbeSize);
610cfca06d7SDimitry Andric if (!FreeProbe &&
611cfca06d7SDimitry Andric MF.getSubtarget().getTargetLowering()->hasInlineStackProbe(MF)) {
612cfca06d7SDimitry Andric // Stack probing may involve looping, but splitting the prologue block
613cfca06d7SDimitry Andric // is not possible at this point since it would invalidate the
614cfca06d7SDimitry Andric // SaveBlocks / RestoreBlocks sets of PEI in the single block function
615cfca06d7SDimitry Andric // case. Build a pseudo to be handled later by inlineStackProbe().
616cfca06d7SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::PROBED_STACKALLOC))
617cfca06d7SDimitry Andric .addImm(StackSize);
618cfca06d7SDimitry Andric }
619cfca06d7SDimitry Andric else {
620b1c73532SDimitry Andric bool StoreBackchain = MF.getSubtarget<SystemZSubtarget>().hasBackChain();
621b60736ecSDimitry Andric // If we need backchain, save current stack pointer. R1 is free at
622b60736ecSDimitry Andric // this point.
623b60736ecSDimitry Andric if (StoreBackchain)
624b60736ecSDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR))
625b60736ecSDimitry Andric .addReg(SystemZ::R1D, RegState::Define).addReg(SystemZ::R15D);
62659d6cff9SDimitry Andric emitIncrement(MBB, MBBI, DL, SystemZ::R15D, Delta, ZII);
627cfca06d7SDimitry Andric buildCFAOffs(MBB, MBBI, DL, SPOffsetFromCFA + Delta, ZII);
628b60736ecSDimitry Andric if (StoreBackchain)
62901095a5dSDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::STG))
630cfca06d7SDimitry Andric .addReg(SystemZ::R1D, RegState::Kill).addReg(SystemZ::R15D)
631b60736ecSDimitry Andric .addImm(getBackchainOffset(MF)).addReg(0);
632cfca06d7SDimitry Andric }
633b60736ecSDimitry Andric SPOffsetFromCFA += Delta;
63459d6cff9SDimitry Andric }
63559d6cff9SDimitry Andric
63659d6cff9SDimitry Andric if (HasFP) {
63759d6cff9SDimitry Andric // Copy the base of the frame to R11.
63859d6cff9SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR), SystemZ::R11D)
63959d6cff9SDimitry Andric .addReg(SystemZ::R15D);
64059d6cff9SDimitry Andric
64159d6cff9SDimitry Andric // Add CFI for the new frame location.
642cfca06d7SDimitry Andric buildDefCFAReg(MBB, MBBI, DL, SystemZ::R11D, ZII);
64359d6cff9SDimitry Andric
64459d6cff9SDimitry Andric // Mark the FramePtr as live at the beginning of every block except
64559d6cff9SDimitry Andric // the entry block. (We'll have marked R11 as live on entry when
64659d6cff9SDimitry Andric // saving the GPRs.)
647c0981da4SDimitry Andric for (MachineBasicBlock &MBBJ : llvm::drop_begin(MF))
648c0981da4SDimitry Andric MBBJ.addLiveIn(SystemZ::R11D);
64959d6cff9SDimitry Andric }
65059d6cff9SDimitry Andric
651eb11fae6SDimitry Andric // Skip over the FPR/VR saves.
6525ca98fd9SDimitry Andric SmallVector<unsigned, 8> CFIIndexes;
6535ca98fd9SDimitry Andric for (auto &Save : CSI) {
6546f8fc217SDimitry Andric Register Reg = Save.getReg();
65559d6cff9SDimitry Andric if (SystemZ::FP64BitRegClass.contains(Reg)) {
65659d6cff9SDimitry Andric if (MBBI != MBB.end() &&
65759d6cff9SDimitry Andric (MBBI->getOpcode() == SystemZ::STD ||
65859d6cff9SDimitry Andric MBBI->getOpcode() == SystemZ::STDY))
65959d6cff9SDimitry Andric ++MBBI;
66059d6cff9SDimitry Andric else
66159d6cff9SDimitry Andric llvm_unreachable("Couldn't skip over FPR save");
662eb11fae6SDimitry Andric } else if (SystemZ::VR128BitRegClass.contains(Reg)) {
663eb11fae6SDimitry Andric if (MBBI != MBB.end() &&
664eb11fae6SDimitry Andric MBBI->getOpcode() == SystemZ::VST)
665eb11fae6SDimitry Andric ++MBBI;
666eb11fae6SDimitry Andric else
667eb11fae6SDimitry Andric llvm_unreachable("Couldn't skip over VR save");
668eb11fae6SDimitry Andric } else
669eb11fae6SDimitry Andric continue;
67059d6cff9SDimitry Andric
67159d6cff9SDimitry Andric // Add CFI for the this save.
6725ca98fd9SDimitry Andric unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
673cfca06d7SDimitry Andric Register IgnoredFrameReg;
674dd58ef01SDimitry Andric int64_t Offset =
675b60736ecSDimitry Andric getFrameIndexReference(MF, Save.getFrameIdx(), IgnoredFrameReg)
676b60736ecSDimitry Andric .getFixed();
677dd58ef01SDimitry Andric
678b915e9e0SDimitry Andric unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
6795ca98fd9SDimitry Andric nullptr, DwarfReg, SPOffsetFromCFA + Offset));
6805ca98fd9SDimitry Andric CFIIndexes.push_back(CFIIndex);
68159d6cff9SDimitry Andric }
682eb11fae6SDimitry Andric // Complete the CFI for the FPR/VR saves, modelling them as taking effect
68359d6cff9SDimitry Andric // after the last save.
6845ca98fd9SDimitry Andric for (auto CFIIndex : CFIIndexes) {
6855ca98fd9SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
6865ca98fd9SDimitry Andric .addCFIIndex(CFIIndex);
6875ca98fd9SDimitry Andric }
68859d6cff9SDimitry Andric }
68959d6cff9SDimitry Andric
emitEpilogue(MachineFunction & MF,MachineBasicBlock & MBB) const690c0981da4SDimitry Andric void SystemZELFFrameLowering::emitEpilogue(MachineFunction &MF,
69159d6cff9SDimitry Andric MachineBasicBlock &MBB) const {
69259d6cff9SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
6935ca98fd9SDimitry Andric auto *ZII =
69467c32a98SDimitry Andric static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
69559d6cff9SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
696eb11fae6SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
69759d6cff9SDimitry Andric
698c0981da4SDimitry Andric // See SystemZELFFrameLowering::emitPrologue
699706b4fc4SDimitry Andric if (MF.getFunction().getCallingConv() == CallingConv::GHC)
700706b4fc4SDimitry Andric return;
701706b4fc4SDimitry Andric
70259d6cff9SDimitry Andric // Skip the return instruction.
703f8af5cf6SDimitry Andric assert(MBBI->isReturn() && "Can only insert epilogue into returning blocks");
70459d6cff9SDimitry Andric
705eb11fae6SDimitry Andric uint64_t StackSize = MFFrame.getStackSize();
706706b4fc4SDimitry Andric if (ZFI->getRestoreGPRRegs().LowGPR) {
70759d6cff9SDimitry Andric --MBBI;
70859d6cff9SDimitry Andric unsigned Opcode = MBBI->getOpcode();
70959d6cff9SDimitry Andric if (Opcode != SystemZ::LMG)
71059d6cff9SDimitry Andric llvm_unreachable("Expected to see callee-save register restore code");
71159d6cff9SDimitry Andric
71259d6cff9SDimitry Andric unsigned AddrOpNo = 2;
71359d6cff9SDimitry Andric DebugLoc DL = MBBI->getDebugLoc();
71459d6cff9SDimitry Andric uint64_t Offset = StackSize + MBBI->getOperand(AddrOpNo + 1).getImm();
71559d6cff9SDimitry Andric unsigned NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset);
71659d6cff9SDimitry Andric
71759d6cff9SDimitry Andric // If the offset is too large, use the largest stack-aligned offset
71859d6cff9SDimitry Andric // and add the rest to the base register (the stack or frame pointer).
71959d6cff9SDimitry Andric if (!NewOpcode) {
72059d6cff9SDimitry Andric uint64_t NumBytes = Offset - 0x7fff8;
72159d6cff9SDimitry Andric emitIncrement(MBB, MBBI, DL, MBBI->getOperand(AddrOpNo).getReg(),
72259d6cff9SDimitry Andric NumBytes, ZII);
72359d6cff9SDimitry Andric Offset -= NumBytes;
72459d6cff9SDimitry Andric NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset);
72559d6cff9SDimitry Andric assert(NewOpcode && "No restore instruction available");
72659d6cff9SDimitry Andric }
72759d6cff9SDimitry Andric
72859d6cff9SDimitry Andric MBBI->setDesc(ZII->get(NewOpcode));
72959d6cff9SDimitry Andric MBBI->getOperand(AddrOpNo + 1).ChangeToImmediate(Offset);
73059d6cff9SDimitry Andric } else if (StackSize) {
73159d6cff9SDimitry Andric DebugLoc DL = MBBI->getDebugLoc();
73259d6cff9SDimitry Andric emitIncrement(MBB, MBBI, DL, SystemZ::R15D, StackSize, ZII);
73359d6cff9SDimitry Andric }
73459d6cff9SDimitry Andric }
73559d6cff9SDimitry Andric
inlineStackProbe(MachineFunction & MF,MachineBasicBlock & PrologMBB) const736c0981da4SDimitry Andric void SystemZELFFrameLowering::inlineStackProbe(
737c0981da4SDimitry Andric MachineFunction &MF, MachineBasicBlock &PrologMBB) const {
738cfca06d7SDimitry Andric auto *ZII =
739cfca06d7SDimitry Andric static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
740cfca06d7SDimitry Andric const SystemZSubtarget &STI = MF.getSubtarget<SystemZSubtarget>();
741cfca06d7SDimitry Andric const SystemZTargetLowering &TLI = *STI.getTargetLowering();
742cfca06d7SDimitry Andric
743cfca06d7SDimitry Andric MachineInstr *StackAllocMI = nullptr;
744cfca06d7SDimitry Andric for (MachineInstr &MI : PrologMBB)
745cfca06d7SDimitry Andric if (MI.getOpcode() == SystemZ::PROBED_STACKALLOC) {
746cfca06d7SDimitry Andric StackAllocMI = &MI;
747cfca06d7SDimitry Andric break;
748cfca06d7SDimitry Andric }
749cfca06d7SDimitry Andric if (StackAllocMI == nullptr)
750cfca06d7SDimitry Andric return;
751cfca06d7SDimitry Andric uint64_t StackSize = StackAllocMI->getOperand(0).getImm();
752cfca06d7SDimitry Andric const unsigned ProbeSize = TLI.getStackProbeSize(MF);
753cfca06d7SDimitry Andric uint64_t NumFullBlocks = StackSize / ProbeSize;
754cfca06d7SDimitry Andric uint64_t Residual = StackSize % ProbeSize;
755344a3780SDimitry Andric int64_t SPOffsetFromCFA = -SystemZMC::ELFCFAOffsetFromInitialSP;
756cfca06d7SDimitry Andric MachineBasicBlock *MBB = &PrologMBB;
757cfca06d7SDimitry Andric MachineBasicBlock::iterator MBBI = StackAllocMI;
758cfca06d7SDimitry Andric const DebugLoc DL = StackAllocMI->getDebugLoc();
759cfca06d7SDimitry Andric
760cfca06d7SDimitry Andric // Allocate a block of Size bytes on the stack and probe it.
761cfca06d7SDimitry Andric auto allocateAndProbe = [&](MachineBasicBlock &InsMBB,
762cfca06d7SDimitry Andric MachineBasicBlock::iterator InsPt, unsigned Size,
763cfca06d7SDimitry Andric bool EmitCFI) -> void {
764cfca06d7SDimitry Andric emitIncrement(InsMBB, InsPt, DL, SystemZ::R15D, -int64_t(Size), ZII);
765cfca06d7SDimitry Andric if (EmitCFI) {
766cfca06d7SDimitry Andric SPOffsetFromCFA -= Size;
767cfca06d7SDimitry Andric buildCFAOffs(InsMBB, InsPt, DL, SPOffsetFromCFA, ZII);
768cfca06d7SDimitry Andric }
769cfca06d7SDimitry Andric // Probe by means of a volatile compare.
770cfca06d7SDimitry Andric MachineMemOperand *MMO = MF.getMachineMemOperand(MachinePointerInfo(),
771cfca06d7SDimitry Andric MachineMemOperand::MOVolatile | MachineMemOperand::MOLoad, 8, Align(1));
772cfca06d7SDimitry Andric BuildMI(InsMBB, InsPt, DL, ZII->get(SystemZ::CG))
773cfca06d7SDimitry Andric .addReg(SystemZ::R0D, RegState::Undef)
774cfca06d7SDimitry Andric .addReg(SystemZ::R15D).addImm(Size - 8).addReg(0)
775cfca06d7SDimitry Andric .addMemOperand(MMO);
776cfca06d7SDimitry Andric };
777cfca06d7SDimitry Andric
778b1c73532SDimitry Andric bool StoreBackchain = MF.getSubtarget<SystemZSubtarget>().hasBackChain();
779b60736ecSDimitry Andric if (StoreBackchain)
780b60736ecSDimitry Andric BuildMI(*MBB, MBBI, DL, ZII->get(SystemZ::LGR))
781b60736ecSDimitry Andric .addReg(SystemZ::R1D, RegState::Define).addReg(SystemZ::R15D);
782b60736ecSDimitry Andric
783b60736ecSDimitry Andric MachineBasicBlock *DoneMBB = nullptr;
784b60736ecSDimitry Andric MachineBasicBlock *LoopMBB = nullptr;
785cfca06d7SDimitry Andric if (NumFullBlocks < 3) {
786cfca06d7SDimitry Andric // Emit unrolled probe statements.
787cfca06d7SDimitry Andric for (unsigned int i = 0; i < NumFullBlocks; i++)
788cfca06d7SDimitry Andric allocateAndProbe(*MBB, MBBI, ProbeSize, true/*EmitCFI*/);
789cfca06d7SDimitry Andric } else {
790cfca06d7SDimitry Andric // Emit a loop probing the pages.
791cfca06d7SDimitry Andric uint64_t LoopAlloc = ProbeSize * NumFullBlocks;
792cfca06d7SDimitry Andric SPOffsetFromCFA -= LoopAlloc;
793cfca06d7SDimitry Andric
794b60736ecSDimitry Andric // Use R0D to hold the exit value.
795b60736ecSDimitry Andric BuildMI(*MBB, MBBI, DL, ZII->get(SystemZ::LGR), SystemZ::R0D)
796cfca06d7SDimitry Andric .addReg(SystemZ::R15D);
797b60736ecSDimitry Andric buildDefCFAReg(*MBB, MBBI, DL, SystemZ::R0D, ZII);
798b60736ecSDimitry Andric emitIncrement(*MBB, MBBI, DL, SystemZ::R0D, -int64_t(LoopAlloc), ZII);
799344a3780SDimitry Andric buildCFAOffs(*MBB, MBBI, DL, -int64_t(SystemZMC::ELFCallFrameSize + LoopAlloc),
800cfca06d7SDimitry Andric ZII);
801cfca06d7SDimitry Andric
802b60736ecSDimitry Andric DoneMBB = SystemZ::splitBlockBefore(MBBI, MBB);
803b60736ecSDimitry Andric LoopMBB = SystemZ::emitBlockAfter(MBB);
804cfca06d7SDimitry Andric MBB->addSuccessor(LoopMBB);
805cfca06d7SDimitry Andric LoopMBB->addSuccessor(LoopMBB);
806cfca06d7SDimitry Andric LoopMBB->addSuccessor(DoneMBB);
807cfca06d7SDimitry Andric
808cfca06d7SDimitry Andric MBB = LoopMBB;
809cfca06d7SDimitry Andric allocateAndProbe(*MBB, MBB->end(), ProbeSize, false/*EmitCFI*/);
810cfca06d7SDimitry Andric BuildMI(*MBB, MBB->end(), DL, ZII->get(SystemZ::CLGR))
811b60736ecSDimitry Andric .addReg(SystemZ::R15D).addReg(SystemZ::R0D);
812cfca06d7SDimitry Andric BuildMI(*MBB, MBB->end(), DL, ZII->get(SystemZ::BRC))
813cfca06d7SDimitry Andric .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_GT).addMBB(MBB);
814cfca06d7SDimitry Andric
815cfca06d7SDimitry Andric MBB = DoneMBB;
816cfca06d7SDimitry Andric MBBI = DoneMBB->begin();
817cfca06d7SDimitry Andric buildDefCFAReg(*MBB, MBBI, DL, SystemZ::R15D, ZII);
818cfca06d7SDimitry Andric }
819cfca06d7SDimitry Andric
820cfca06d7SDimitry Andric if (Residual)
821cfca06d7SDimitry Andric allocateAndProbe(*MBB, MBBI, Residual, true/*EmitCFI*/);
822cfca06d7SDimitry Andric
823b60736ecSDimitry Andric if (StoreBackchain)
824b60736ecSDimitry Andric BuildMI(*MBB, MBBI, DL, ZII->get(SystemZ::STG))
825b60736ecSDimitry Andric .addReg(SystemZ::R1D, RegState::Kill).addReg(SystemZ::R15D)
826b60736ecSDimitry Andric .addImm(getBackchainOffset(MF)).addReg(0);
827b60736ecSDimitry Andric
828cfca06d7SDimitry Andric StackAllocMI->eraseFromParent();
829b60736ecSDimitry Andric if (DoneMBB != nullptr) {
830b60736ecSDimitry Andric // Compute the live-in lists for the new blocks.
831ac9a064cSDimitry Andric fullyRecomputeLiveIns({DoneMBB, LoopMBB});
832b60736ecSDimitry Andric }
833cfca06d7SDimitry Andric }
834cfca06d7SDimitry Andric
hasFP(const MachineFunction & MF) const835c0981da4SDimitry Andric bool SystemZELFFrameLowering::hasFP(const MachineFunction &MF) const {
83659d6cff9SDimitry Andric return (MF.getTarget().Options.DisableFramePointerElim(MF) ||
8376f8fc217SDimitry Andric MF.getFrameInfo().hasVarSizedObjects());
83859d6cff9SDimitry Andric }
83959d6cff9SDimitry Andric
getFrameIndexReference(const MachineFunction & MF,int FI,Register & FrameReg) const840c0981da4SDimitry Andric StackOffset SystemZELFFrameLowering::getFrameIndexReference(
841c0981da4SDimitry Andric const MachineFunction &MF, int FI, Register &FrameReg) const {
842344a3780SDimitry Andric // Our incoming SP is actually SystemZMC::ELFCallFrameSize below the CFA, so
843706b4fc4SDimitry Andric // add that difference here.
844b60736ecSDimitry Andric StackOffset Offset =
845706b4fc4SDimitry Andric TargetFrameLowering::getFrameIndexReference(MF, FI, FrameReg);
846344a3780SDimitry Andric return Offset + StackOffset::getFixed(SystemZMC::ELFCallFrameSize);
847706b4fc4SDimitry Andric }
848706b4fc4SDimitry Andric
getRegSpillOffset(MachineFunction & MF,Register Reg) const849c0981da4SDimitry Andric unsigned SystemZELFFrameLowering::getRegSpillOffset(MachineFunction &MF,
850cfca06d7SDimitry Andric Register Reg) const {
851cfca06d7SDimitry Andric bool IsVarArg = MF.getFunction().isVarArg();
852b1c73532SDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
853b1c73532SDimitry Andric bool BackChain = Subtarget.hasBackChain();
854b1c73532SDimitry Andric bool SoftFloat = Subtarget.hasSoftFloat();
855cfca06d7SDimitry Andric unsigned Offset = RegSpillOffsets[Reg];
856cfca06d7SDimitry Andric if (usePackedStack(MF) && !(IsVarArg && !SoftFloat)) {
857cfca06d7SDimitry Andric if (SystemZ::GR64BitRegClass.contains(Reg))
858cfca06d7SDimitry Andric // Put all GPRs at the top of the Register save area with packed
859cfca06d7SDimitry Andric // stack. Make room for the backchain if needed.
860cfca06d7SDimitry Andric Offset += BackChain ? 24 : 32;
861cfca06d7SDimitry Andric else
862cfca06d7SDimitry Andric Offset = 0;
863cfca06d7SDimitry Andric }
864cfca06d7SDimitry Andric return Offset;
865cfca06d7SDimitry Andric }
866cfca06d7SDimitry Andric
getOrCreateFramePointerSaveIndex(MachineFunction & MF) const867c0981da4SDimitry Andric int SystemZELFFrameLowering::getOrCreateFramePointerSaveIndex(
868c0981da4SDimitry Andric MachineFunction &MF) const {
869706b4fc4SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
870706b4fc4SDimitry Andric int FI = ZFI->getFramePointerSaveIndex();
871706b4fc4SDimitry Andric if (!FI) {
872706b4fc4SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
873344a3780SDimitry Andric int Offset = getBackchainOffset(MF) - SystemZMC::ELFCallFrameSize;
874ac9a064cSDimitry Andric FI = MFFrame.CreateFixedObject(getPointerSize(), Offset, false);
875706b4fc4SDimitry Andric ZFI->setFramePointerSaveIndex(FI);
876706b4fc4SDimitry Andric }
877706b4fc4SDimitry Andric return FI;
878706b4fc4SDimitry Andric }
879cfca06d7SDimitry Andric
usePackedStack(MachineFunction & MF) const880c0981da4SDimitry Andric bool SystemZELFFrameLowering::usePackedStack(MachineFunction &MF) const {
881cfca06d7SDimitry Andric bool HasPackedStackAttr = MF.getFunction().hasFnAttribute("packed-stack");
882b1c73532SDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
883b1c73532SDimitry Andric bool BackChain = Subtarget.hasBackChain();
884b1c73532SDimitry Andric bool SoftFloat = Subtarget.hasSoftFloat();
885cfca06d7SDimitry Andric if (HasPackedStackAttr && BackChain && !SoftFloat)
886cfca06d7SDimitry Andric report_fatal_error("packed-stack + backchain + hard-float is unsupported.");
887cfca06d7SDimitry Andric bool CallConv = MF.getFunction().getCallingConv() != CallingConv::GHC;
888cfca06d7SDimitry Andric return HasPackedStackAttr && CallConv;
889cfca06d7SDimitry Andric }
890c0981da4SDimitry Andric
SystemZXPLINKFrameLowering(unsigned PointerSize)891ac9a064cSDimitry Andric SystemZXPLINKFrameLowering::SystemZXPLINKFrameLowering(unsigned PointerSize)
89277fc4c14SDimitry Andric : SystemZFrameLowering(TargetFrameLowering::StackGrowsDown, Align(32), 0,
893ac9a064cSDimitry Andric Align(32), /* StackRealignable */ false,
894ac9a064cSDimitry Andric PointerSize),
895c0981da4SDimitry Andric RegSpillOffsets(-1) {
896c0981da4SDimitry Andric
897c0981da4SDimitry Andric // Create a mapping from register number to save slot offset.
898c0981da4SDimitry Andric // These offsets are relative to the start of the local are area.
899c0981da4SDimitry Andric RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS);
900e3b55780SDimitry Andric for (const auto &Entry : XPLINKSpillOffsetTable)
901e3b55780SDimitry Andric RegSpillOffsets[Entry.Reg] = Entry.Offset;
902c0981da4SDimitry Andric }
903c0981da4SDimitry Andric
getOrCreateFramePointerSaveIndex(MachineFunction & MF) const904ac9a064cSDimitry Andric int SystemZXPLINKFrameLowering::getOrCreateFramePointerSaveIndex(
905ac9a064cSDimitry Andric MachineFunction &MF) const {
906ac9a064cSDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
907ac9a064cSDimitry Andric int FI = ZFI->getFramePointerSaveIndex();
908ac9a064cSDimitry Andric if (!FI) {
909ac9a064cSDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
910ac9a064cSDimitry Andric FI = MFFrame.CreateFixedObject(getPointerSize(), 0, false);
911ac9a064cSDimitry Andric MFFrame.setStackID(FI, TargetStackID::NoAlloc);
912ac9a064cSDimitry Andric ZFI->setFramePointerSaveIndex(FI);
913ac9a064cSDimitry Andric }
914ac9a064cSDimitry Andric return FI;
915ac9a064cSDimitry Andric }
916ac9a064cSDimitry Andric
9174b4fe385SDimitry Andric // Checks if the function is a potential candidate for being a XPLeaf routine.
isXPLeafCandidate(const MachineFunction & MF)9184b4fe385SDimitry Andric static bool isXPLeafCandidate(const MachineFunction &MF) {
9194b4fe385SDimitry Andric const MachineFrameInfo &MFFrame = MF.getFrameInfo();
9204b4fe385SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo();
9214b4fe385SDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
9224b4fe385SDimitry Andric auto *Regs =
9234b4fe385SDimitry Andric static_cast<SystemZXPLINK64Registers *>(Subtarget.getSpecialRegisters());
9244b4fe385SDimitry Andric
9254b4fe385SDimitry Andric // If function calls other functions including alloca, then it is not a XPLeaf
9264b4fe385SDimitry Andric // routine.
9274b4fe385SDimitry Andric if (MFFrame.hasCalls())
9284b4fe385SDimitry Andric return false;
9294b4fe385SDimitry Andric
9304b4fe385SDimitry Andric // If the function has var Sized Objects, then it is not a XPLeaf routine.
9314b4fe385SDimitry Andric if (MFFrame.hasVarSizedObjects())
9324b4fe385SDimitry Andric return false;
9334b4fe385SDimitry Andric
9344b4fe385SDimitry Andric // If the function adjusts the stack, then it is not a XPLeaf routine.
9354b4fe385SDimitry Andric if (MFFrame.adjustsStack())
9364b4fe385SDimitry Andric return false;
9374b4fe385SDimitry Andric
9384b4fe385SDimitry Andric // If function modifies the stack pointer register, then it is not a XPLeaf
9394b4fe385SDimitry Andric // routine.
9404b4fe385SDimitry Andric if (MRI.isPhysRegModified(Regs->getStackPointerRegister()))
9414b4fe385SDimitry Andric return false;
9424b4fe385SDimitry Andric
9434b4fe385SDimitry Andric // If function modifies the ADA register, then it is not a XPLeaf routine.
9444b4fe385SDimitry Andric if (MRI.isPhysRegModified(Regs->getAddressOfCalleeRegister()))
9454b4fe385SDimitry Andric return false;
9464b4fe385SDimitry Andric
9474b4fe385SDimitry Andric // If function modifies the return address register, then it is not a XPLeaf
9484b4fe385SDimitry Andric // routine.
9494b4fe385SDimitry Andric if (MRI.isPhysRegModified(Regs->getReturnFunctionAddressRegister()))
9504b4fe385SDimitry Andric return false;
9514b4fe385SDimitry Andric
9524b4fe385SDimitry Andric // If the backchain pointer should be stored, then it is not a XPLeaf routine.
953b1c73532SDimitry Andric if (MF.getSubtarget<SystemZSubtarget>().hasBackChain())
9544b4fe385SDimitry Andric return false;
9554b4fe385SDimitry Andric
9564b4fe385SDimitry Andric // If function acquires its own stack frame, then it is not a XPLeaf routine.
9574b4fe385SDimitry Andric // At the time this function is called, only slots for local variables are
9584b4fe385SDimitry Andric // allocated, so this is a very rough estimate.
9594b4fe385SDimitry Andric if (MFFrame.estimateStackSize(MF) > 0)
9604b4fe385SDimitry Andric return false;
9614b4fe385SDimitry Andric
9624b4fe385SDimitry Andric return true;
9634b4fe385SDimitry Andric }
9644b4fe385SDimitry Andric
assignCalleeSavedSpillSlots(MachineFunction & MF,const TargetRegisterInfo * TRI,std::vector<CalleeSavedInfo> & CSI) const965c0981da4SDimitry Andric bool SystemZXPLINKFrameLowering::assignCalleeSavedSpillSlots(
966c0981da4SDimitry Andric MachineFunction &MF, const TargetRegisterInfo *TRI,
967c0981da4SDimitry Andric std::vector<CalleeSavedInfo> &CSI) const {
968c0981da4SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
969c0981da4SDimitry Andric SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>();
970c0981da4SDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
971c0981da4SDimitry Andric auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
9721f917f69SDimitry Andric auto &GRRegClass = SystemZ::GR64BitRegClass;
9731f917f69SDimitry Andric
9744b4fe385SDimitry Andric // At this point, the result of isXPLeafCandidate() is not accurate because
9754b4fe385SDimitry Andric // the size of the save area has not yet been determined. If
9764b4fe385SDimitry Andric // isXPLeafCandidate() indicates a potential leaf function, and there are no
9774b4fe385SDimitry Andric // callee-save registers, then it is indeed a leaf function, and we can early
9784b4fe385SDimitry Andric // exit.
9794b4fe385SDimitry Andric // TODO: It is possible for leaf functions to use callee-saved registers.
9804b4fe385SDimitry Andric // It can use the 0-2k range between R4 and the caller's stack frame without
9814b4fe385SDimitry Andric // acquiring its own stack frame.
9824b4fe385SDimitry Andric bool IsLeaf = CSI.empty() && isXPLeafCandidate(MF);
9834b4fe385SDimitry Andric if (IsLeaf)
9844b4fe385SDimitry Andric return true;
9854b4fe385SDimitry Andric
9861f917f69SDimitry Andric // For non-leaf functions:
9871f917f69SDimitry Andric // - the address of callee (entry point) register R6 must be saved
9881f917f69SDimitry Andric CSI.push_back(CalleeSavedInfo(Regs.getAddressOfCalleeRegister()));
9891f917f69SDimitry Andric CSI.back().setRestored(false);
9901f917f69SDimitry Andric
9911f917f69SDimitry Andric // The return address register R7 must be saved and restored.
9921f917f69SDimitry Andric CSI.push_back(CalleeSavedInfo(Regs.getReturnFunctionAddressRegister()));
9931f917f69SDimitry Andric
9941f917f69SDimitry Andric // If the function needs a frame pointer, or if the backchain pointer should
9951f917f69SDimitry Andric // be stored, then save the stack pointer register R4.
996b1c73532SDimitry Andric if (hasFP(MF) || Subtarget.hasBackChain())
9971f917f69SDimitry Andric CSI.push_back(CalleeSavedInfo(Regs.getStackPointerRegister()));
998c0981da4SDimitry Andric
99999aabd70SDimitry Andric // If this function has an associated personality function then the
100099aabd70SDimitry Andric // environment register R5 must be saved in the DSA.
100199aabd70SDimitry Andric if (!MF.getLandingPads().empty())
100299aabd70SDimitry Andric CSI.push_back(CalleeSavedInfo(Regs.getADARegister()));
100399aabd70SDimitry Andric
1004c0981da4SDimitry Andric // Scan the call-saved GPRs and find the bounds of the register spill area.
10051f917f69SDimitry Andric Register LowRestoreGPR = 0;
10061f917f69SDimitry Andric int LowRestoreOffset = INT32_MAX;
10071f917f69SDimitry Andric Register LowSpillGPR = 0;
10081f917f69SDimitry Andric int LowSpillOffset = INT32_MAX;
10091f917f69SDimitry Andric Register HighGPR = 0;
1010c0981da4SDimitry Andric int HighOffset = -1;
1011c0981da4SDimitry Andric
1012ac9a064cSDimitry Andric // Query index of the saved frame pointer.
1013ac9a064cSDimitry Andric int FPSI = MFI->getFramePointerSaveIndex();
1014ac9a064cSDimitry Andric
10151f917f69SDimitry Andric for (auto &CS : CSI) {
10166f8fc217SDimitry Andric Register Reg = CS.getReg();
1017c0981da4SDimitry Andric int Offset = RegSpillOffsets[Reg];
1018c0981da4SDimitry Andric if (Offset >= 0) {
1019c0981da4SDimitry Andric if (GRRegClass.contains(Reg)) {
10201f917f69SDimitry Andric if (LowSpillOffset > Offset) {
10211f917f69SDimitry Andric LowSpillOffset = Offset;
10221f917f69SDimitry Andric LowSpillGPR = Reg;
10231f917f69SDimitry Andric }
10241f917f69SDimitry Andric if (CS.isRestored() && LowRestoreOffset > Offset) {
10251f917f69SDimitry Andric LowRestoreOffset = Offset;
10261f917f69SDimitry Andric LowRestoreGPR = Reg;
1027c0981da4SDimitry Andric }
1028c0981da4SDimitry Andric
1029c0981da4SDimitry Andric if (Offset > HighOffset) {
1030c0981da4SDimitry Andric HighOffset = Offset;
1031c0981da4SDimitry Andric HighGPR = Reg;
1032c0981da4SDimitry Andric }
10331f917f69SDimitry Andric // Non-volatile GPRs are saved in the dedicated register save area at
10341f917f69SDimitry Andric // the bottom of the stack and are not truly part of the "normal" stack
10351f917f69SDimitry Andric // frame. Mark the frame index as NoAlloc to indicate it as such.
1036ac9a064cSDimitry Andric unsigned RegSize = getPointerSize();
1037ac9a064cSDimitry Andric int FrameIdx =
1038ac9a064cSDimitry Andric (FPSI && Offset == 0)
1039ac9a064cSDimitry Andric ? FPSI
1040ac9a064cSDimitry Andric : MFFrame.CreateFixedSpillStackObject(RegSize, Offset);
1041c0981da4SDimitry Andric CS.setFrameIdx(FrameIdx);
10421f917f69SDimitry Andric MFFrame.setStackID(FrameIdx, TargetStackID::NoAlloc);
1043c0981da4SDimitry Andric }
10441f917f69SDimitry Andric } else {
10456f8fc217SDimitry Andric Register Reg = CS.getReg();
1046c0981da4SDimitry Andric const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1047c0981da4SDimitry Andric Align Alignment = TRI->getSpillAlign(*RC);
1048c0981da4SDimitry Andric unsigned Size = TRI->getSpillSize(*RC);
1049c0981da4SDimitry Andric Alignment = std::min(Alignment, getStackAlign());
1050c0981da4SDimitry Andric int FrameIdx = MFFrame.CreateStackObject(Size, Alignment, true);
1051c0981da4SDimitry Andric CS.setFrameIdx(FrameIdx);
1052c0981da4SDimitry Andric }
10531f917f69SDimitry Andric }
10541f917f69SDimitry Andric
10551f917f69SDimitry Andric // Save the range of call-saved registers, for use by the
10561f917f69SDimitry Andric // prologue/epilogue inserters.
10571f917f69SDimitry Andric if (LowRestoreGPR)
10581f917f69SDimitry Andric MFI->setRestoreGPRRegs(LowRestoreGPR, HighGPR, LowRestoreOffset);
10591f917f69SDimitry Andric
10601f917f69SDimitry Andric // Save the range of call-saved registers, for use by the epilogue inserter.
10611f917f69SDimitry Andric assert(LowSpillGPR && "Expected registers to spill");
10621f917f69SDimitry Andric MFI->setSpillGPRRegs(LowSpillGPR, HighGPR, LowSpillOffset);
1063c0981da4SDimitry Andric
1064c0981da4SDimitry Andric return true;
1065c0981da4SDimitry Andric }
1066c0981da4SDimitry Andric
determineCalleeSaves(MachineFunction & MF,BitVector & SavedRegs,RegScavenger * RS) const1067c0981da4SDimitry Andric void SystemZXPLINKFrameLowering::determineCalleeSaves(MachineFunction &MF,
1068c0981da4SDimitry Andric BitVector &SavedRegs,
1069c0981da4SDimitry Andric RegScavenger *RS) const {
1070c0981da4SDimitry Andric TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
1071c0981da4SDimitry Andric
1072c0981da4SDimitry Andric bool HasFP = hasFP(MF);
1073c0981da4SDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
1074c0981da4SDimitry Andric auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
1075c0981da4SDimitry Andric
1076c0981da4SDimitry Andric // If the function requires a frame pointer, record that the hard
1077c0981da4SDimitry Andric // frame pointer will be clobbered.
1078c0981da4SDimitry Andric if (HasFP)
1079c0981da4SDimitry Andric SavedRegs.set(Regs.getFramePointerRegister());
1080c0981da4SDimitry Andric }
1081c0981da4SDimitry Andric
spillCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,ArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI) const1082c0981da4SDimitry Andric bool SystemZXPLINKFrameLowering::spillCalleeSavedRegisters(
1083c0981da4SDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
1084c0981da4SDimitry Andric ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
1085c0981da4SDimitry Andric if (CSI.empty())
1086c0981da4SDimitry Andric return true;
1087c0981da4SDimitry Andric
1088c0981da4SDimitry Andric MachineFunction &MF = *MBB.getParent();
1089c0981da4SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
1090c0981da4SDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
1091c0981da4SDimitry Andric const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1092c0981da4SDimitry Andric auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
1093c0981da4SDimitry Andric SystemZ::GPRRegs SpillGPRs = ZFI->getSpillGPRRegs();
1094c0981da4SDimitry Andric DebugLoc DL;
1095c0981da4SDimitry Andric
1096c0981da4SDimitry Andric // Save GPRs
1097c0981da4SDimitry Andric if (SpillGPRs.LowGPR) {
1098c0981da4SDimitry Andric assert(SpillGPRs.LowGPR != SpillGPRs.HighGPR &&
1099c0981da4SDimitry Andric "Should be saving multiple registers");
1100c0981da4SDimitry Andric
1101c0981da4SDimitry Andric // Build an STM/STMG instruction.
1102c0981da4SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STMG));
1103c0981da4SDimitry Andric
1104c0981da4SDimitry Andric // Add the explicit register operands.
1105c0981da4SDimitry Andric addSavedGPR(MBB, MIB, SpillGPRs.LowGPR, false);
1106c0981da4SDimitry Andric addSavedGPR(MBB, MIB, SpillGPRs.HighGPR, false);
1107c0981da4SDimitry Andric
1108c0981da4SDimitry Andric // Add the address r4
1109c0981da4SDimitry Andric MIB.addReg(Regs.getStackPointerRegister());
1110c0981da4SDimitry Andric
1111c0981da4SDimitry Andric // Add the partial offset
1112c0981da4SDimitry Andric // We cannot add the actual offset as, at the stack is not finalized
1113c0981da4SDimitry Andric MIB.addImm(SpillGPRs.GPROffset);
1114c0981da4SDimitry Andric
1115c0981da4SDimitry Andric // Make sure all call-saved GPRs are included as operands and are
1116c0981da4SDimitry Andric // marked as live on entry.
1117c0981da4SDimitry Andric auto &GRRegClass = SystemZ::GR64BitRegClass;
1118f65dcba8SDimitry Andric for (const CalleeSavedInfo &I : CSI) {
11196f8fc217SDimitry Andric Register Reg = I.getReg();
1120c0981da4SDimitry Andric if (GRRegClass.contains(Reg))
1121c0981da4SDimitry Andric addSavedGPR(MBB, MIB, Reg, true);
1122c0981da4SDimitry Andric }
1123c0981da4SDimitry Andric }
1124c0981da4SDimitry Andric
1125c0981da4SDimitry Andric // Spill FPRs to the stack in the normal TargetInstrInfo way
1126f65dcba8SDimitry Andric for (const CalleeSavedInfo &I : CSI) {
11276f8fc217SDimitry Andric Register Reg = I.getReg();
1128c0981da4SDimitry Andric if (SystemZ::FP64BitRegClass.contains(Reg)) {
1129c0981da4SDimitry Andric MBB.addLiveIn(Reg);
1130f65dcba8SDimitry Andric TII->storeRegToStackSlot(MBB, MBBI, Reg, true, I.getFrameIdx(),
1131e3b55780SDimitry Andric &SystemZ::FP64BitRegClass, TRI, Register());
1132c0981da4SDimitry Andric }
1133c0981da4SDimitry Andric if (SystemZ::VR128BitRegClass.contains(Reg)) {
1134c0981da4SDimitry Andric MBB.addLiveIn(Reg);
1135f65dcba8SDimitry Andric TII->storeRegToStackSlot(MBB, MBBI, Reg, true, I.getFrameIdx(),
1136e3b55780SDimitry Andric &SystemZ::VR128BitRegClass, TRI, Register());
1137c0981da4SDimitry Andric }
1138c0981da4SDimitry Andric }
1139c0981da4SDimitry Andric
1140c0981da4SDimitry Andric return true;
1141c0981da4SDimitry Andric }
1142c0981da4SDimitry Andric
restoreCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,MutableArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI) const114377fc4c14SDimitry Andric bool SystemZXPLINKFrameLowering::restoreCalleeSavedRegisters(
114477fc4c14SDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
114577fc4c14SDimitry Andric MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
114677fc4c14SDimitry Andric
114777fc4c14SDimitry Andric if (CSI.empty())
114877fc4c14SDimitry Andric return false;
114977fc4c14SDimitry Andric
115077fc4c14SDimitry Andric MachineFunction &MF = *MBB.getParent();
115177fc4c14SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
115277fc4c14SDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
115377fc4c14SDimitry Andric const TargetInstrInfo *TII = Subtarget.getInstrInfo();
115477fc4c14SDimitry Andric auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
115577fc4c14SDimitry Andric
115677fc4c14SDimitry Andric DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
115777fc4c14SDimitry Andric
115877fc4c14SDimitry Andric // Restore FPRs in the normal TargetInstrInfo way.
1159e3b55780SDimitry Andric for (const CalleeSavedInfo &I : CSI) {
1160e3b55780SDimitry Andric Register Reg = I.getReg();
116177fc4c14SDimitry Andric if (SystemZ::FP64BitRegClass.contains(Reg))
1162e3b55780SDimitry Andric TII->loadRegFromStackSlot(MBB, MBBI, Reg, I.getFrameIdx(),
1163e3b55780SDimitry Andric &SystemZ::FP64BitRegClass, TRI, Register());
116477fc4c14SDimitry Andric if (SystemZ::VR128BitRegClass.contains(Reg))
1165e3b55780SDimitry Andric TII->loadRegFromStackSlot(MBB, MBBI, Reg, I.getFrameIdx(),
1166e3b55780SDimitry Andric &SystemZ::VR128BitRegClass, TRI, Register());
116777fc4c14SDimitry Andric }
116877fc4c14SDimitry Andric
116977fc4c14SDimitry Andric // Restore call-saved GPRs (but not call-clobbered varargs, which at
117077fc4c14SDimitry Andric // this point might hold return values).
117177fc4c14SDimitry Andric SystemZ::GPRRegs RestoreGPRs = ZFI->getRestoreGPRRegs();
117277fc4c14SDimitry Andric if (RestoreGPRs.LowGPR) {
117377fc4c14SDimitry Andric assert(isInt<20>(Regs.getStackPointerBias() + RestoreGPRs.GPROffset));
117477fc4c14SDimitry Andric if (RestoreGPRs.LowGPR == RestoreGPRs.HighGPR)
117577fc4c14SDimitry Andric // Build an LG/L instruction.
117677fc4c14SDimitry Andric BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LG), RestoreGPRs.LowGPR)
117777fc4c14SDimitry Andric .addReg(Regs.getStackPointerRegister())
117877fc4c14SDimitry Andric .addImm(Regs.getStackPointerBias() + RestoreGPRs.GPROffset)
117977fc4c14SDimitry Andric .addReg(0);
118077fc4c14SDimitry Andric else {
118177fc4c14SDimitry Andric // Build an LMG/LM instruction.
118277fc4c14SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LMG));
118377fc4c14SDimitry Andric
118477fc4c14SDimitry Andric // Add the explicit register operands.
118577fc4c14SDimitry Andric MIB.addReg(RestoreGPRs.LowGPR, RegState::Define);
118677fc4c14SDimitry Andric MIB.addReg(RestoreGPRs.HighGPR, RegState::Define);
118777fc4c14SDimitry Andric
118877fc4c14SDimitry Andric // Add the address.
118977fc4c14SDimitry Andric MIB.addReg(Regs.getStackPointerRegister());
119077fc4c14SDimitry Andric MIB.addImm(Regs.getStackPointerBias() + RestoreGPRs.GPROffset);
119177fc4c14SDimitry Andric
119277fc4c14SDimitry Andric // Do a second scan adding regs as being defined by instruction
1193e3b55780SDimitry Andric for (const CalleeSavedInfo &I : CSI) {
1194e3b55780SDimitry Andric Register Reg = I.getReg();
119577fc4c14SDimitry Andric if (Reg > RestoreGPRs.LowGPR && Reg < RestoreGPRs.HighGPR)
119677fc4c14SDimitry Andric MIB.addReg(Reg, RegState::ImplicitDefine);
119777fc4c14SDimitry Andric }
119877fc4c14SDimitry Andric }
119977fc4c14SDimitry Andric }
120077fc4c14SDimitry Andric
120177fc4c14SDimitry Andric return true;
120277fc4c14SDimitry Andric }
120377fc4c14SDimitry Andric
emitPrologue(MachineFunction & MF,MachineBasicBlock & MBB) const1204c0981da4SDimitry Andric void SystemZXPLINKFrameLowering::emitPrologue(MachineFunction &MF,
120577fc4c14SDimitry Andric MachineBasicBlock &MBB) const {
120677fc4c14SDimitry Andric assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
120777fc4c14SDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
120877fc4c14SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
120977fc4c14SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.begin();
121077fc4c14SDimitry Andric auto *ZII = static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
121177fc4c14SDimitry Andric auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
121277fc4c14SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
121377fc4c14SDimitry Andric MachineInstr *StoreInstr = nullptr;
12144b4fe385SDimitry Andric
12154b4fe385SDimitry Andric determineFrameLayout(MF);
12164b4fe385SDimitry Andric
121777fc4c14SDimitry Andric bool HasFP = hasFP(MF);
121877fc4c14SDimitry Andric // Debug location must be unknown since the first debug location is used
121977fc4c14SDimitry Andric // to determine the end of the prologue.
122077fc4c14SDimitry Andric DebugLoc DL;
122177fc4c14SDimitry Andric uint64_t Offset = 0;
122277fc4c14SDimitry Andric
12234b4fe385SDimitry Andric const uint64_t StackSize = MFFrame.getStackSize();
122477fc4c14SDimitry Andric
122577fc4c14SDimitry Andric if (ZFI->getSpillGPRRegs().LowGPR) {
122677fc4c14SDimitry Andric // Skip over the GPR saves.
122777fc4c14SDimitry Andric if ((MBBI != MBB.end()) && ((MBBI->getOpcode() == SystemZ::STMG))) {
122877fc4c14SDimitry Andric const int Operand = 3;
122977fc4c14SDimitry Andric // Now we can set the offset for the operation, since now the Stack
123077fc4c14SDimitry Andric // has been finalized.
123177fc4c14SDimitry Andric Offset = Regs.getStackPointerBias() + MBBI->getOperand(Operand).getImm();
123277fc4c14SDimitry Andric // Maximum displacement for STMG instruction.
123377fc4c14SDimitry Andric if (isInt<20>(Offset - StackSize))
123477fc4c14SDimitry Andric Offset -= StackSize;
123577fc4c14SDimitry Andric else
123677fc4c14SDimitry Andric StoreInstr = &*MBBI;
123777fc4c14SDimitry Andric MBBI->getOperand(Operand).setImm(Offset);
123877fc4c14SDimitry Andric ++MBBI;
123977fc4c14SDimitry Andric } else
124077fc4c14SDimitry Andric llvm_unreachable("Couldn't skip over GPR saves");
124177fc4c14SDimitry Andric }
124277fc4c14SDimitry Andric
124377fc4c14SDimitry Andric if (StackSize) {
124477fc4c14SDimitry Andric MachineBasicBlock::iterator InsertPt = StoreInstr ? StoreInstr : MBBI;
124577fc4c14SDimitry Andric // Allocate StackSize bytes.
124677fc4c14SDimitry Andric int64_t Delta = -int64_t(StackSize);
124777fc4c14SDimitry Andric
124877fc4c14SDimitry Andric // In case the STM(G) instruction also stores SP (R4), but the displacement
124977fc4c14SDimitry Andric // is too large, the SP register is manipulated first before storing,
125077fc4c14SDimitry Andric // resulting in the wrong value stored and retrieved later. In this case, we
125177fc4c14SDimitry Andric // need to temporarily save the value of SP, and store it later to memory.
125277fc4c14SDimitry Andric if (StoreInstr && HasFP) {
125377fc4c14SDimitry Andric // Insert LR r0,r4 before STMG instruction.
125477fc4c14SDimitry Andric BuildMI(MBB, InsertPt, DL, ZII->get(SystemZ::LGR))
125577fc4c14SDimitry Andric .addReg(SystemZ::R0D, RegState::Define)
125677fc4c14SDimitry Andric .addReg(SystemZ::R4D);
125777fc4c14SDimitry Andric // Insert ST r0,xxx(,r4) after STMG instruction.
125877fc4c14SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::STG))
125977fc4c14SDimitry Andric .addReg(SystemZ::R0D, RegState::Kill)
126077fc4c14SDimitry Andric .addReg(SystemZ::R4D)
126177fc4c14SDimitry Andric .addImm(Offset)
126277fc4c14SDimitry Andric .addReg(0);
126377fc4c14SDimitry Andric }
126477fc4c14SDimitry Andric
126577fc4c14SDimitry Andric emitIncrement(MBB, InsertPt, DL, Regs.getStackPointerRegister(), Delta,
126677fc4c14SDimitry Andric ZII);
1267145449b1SDimitry Andric
1268145449b1SDimitry Andric // If the requested stack size is larger than the guard page, then we need
1269145449b1SDimitry Andric // to check if we need to call the stack extender. This requires adding a
1270145449b1SDimitry Andric // conditional branch, but splitting the prologue block is not possible at
1271145449b1SDimitry Andric // this point since it would invalidate the SaveBlocks / RestoreBlocks sets
1272145449b1SDimitry Andric // of PEI in the single block function case. Build a pseudo to be handled
1273145449b1SDimitry Andric // later by inlineStackProbe().
1274145449b1SDimitry Andric const uint64_t GuardPageSize = 1024 * 1024;
1275145449b1SDimitry Andric if (StackSize > GuardPageSize) {
1276145449b1SDimitry Andric assert(StoreInstr && "Wrong insertion point");
1277145449b1SDimitry Andric BuildMI(MBB, InsertPt, DL, ZII->get(SystemZ::XPLINK_STACKALLOC));
1278145449b1SDimitry Andric }
127977fc4c14SDimitry Andric }
128077fc4c14SDimitry Andric
128177fc4c14SDimitry Andric if (HasFP) {
128277fc4c14SDimitry Andric // Copy the base of the frame to Frame Pointer Register.
128377fc4c14SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR),
128477fc4c14SDimitry Andric Regs.getFramePointerRegister())
128577fc4c14SDimitry Andric .addReg(Regs.getStackPointerRegister());
128677fc4c14SDimitry Andric
128777fc4c14SDimitry Andric // Mark the FramePtr as live at the beginning of every block except
128877fc4c14SDimitry Andric // the entry block. (We'll have marked R8 as live on entry when
128977fc4c14SDimitry Andric // saving the GPRs.)
12904b4fe385SDimitry Andric for (MachineBasicBlock &B : llvm::drop_begin(MF))
12914b4fe385SDimitry Andric B.addLiveIn(Regs.getFramePointerRegister());
129277fc4c14SDimitry Andric }
1293b1c73532SDimitry Andric
1294b1c73532SDimitry Andric // Save GPRs used for varargs, if any.
1295b1c73532SDimitry Andric const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1296b1c73532SDimitry Andric bool IsVarArg = MF.getFunction().isVarArg();
1297b1c73532SDimitry Andric
1298b1c73532SDimitry Andric if (IsVarArg) {
1299b1c73532SDimitry Andric // FixedRegs is the number of used registers, accounting for shadow
1300b1c73532SDimitry Andric // registers.
1301b1c73532SDimitry Andric unsigned FixedRegs = ZFI->getVarArgsFirstGPR() + ZFI->getVarArgsFirstFPR();
1302b1c73532SDimitry Andric auto &GPRs = SystemZ::XPLINK64ArgGPRs;
1303b1c73532SDimitry Andric for (unsigned I = FixedRegs; I < SystemZ::XPLINK64NumArgGPRs; I++) {
1304b1c73532SDimitry Andric uint64_t StartOffset = MFFrame.getOffsetAdjustment() +
1305b1c73532SDimitry Andric MFFrame.getStackSize() + Regs.getCallFrameSize() +
1306ac9a064cSDimitry Andric getOffsetOfLocalArea() + I * getPointerSize();
1307b1c73532SDimitry Andric unsigned Reg = GPRs[I];
1308b1c73532SDimitry Andric BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STG))
1309b1c73532SDimitry Andric .addReg(Reg)
1310b1c73532SDimitry Andric .addReg(Regs.getStackPointerRegister())
1311b1c73532SDimitry Andric .addImm(StartOffset)
1312b1c73532SDimitry Andric .addReg(0);
1313b1c73532SDimitry Andric if (!MBB.isLiveIn(Reg))
1314b1c73532SDimitry Andric MBB.addLiveIn(Reg);
1315b1c73532SDimitry Andric }
1316b1c73532SDimitry Andric }
131777fc4c14SDimitry Andric }
1318c0981da4SDimitry Andric
emitEpilogue(MachineFunction & MF,MachineBasicBlock & MBB) const1319c0981da4SDimitry Andric void SystemZXPLINKFrameLowering::emitEpilogue(MachineFunction &MF,
132077fc4c14SDimitry Andric MachineBasicBlock &MBB) const {
132177fc4c14SDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
132277fc4c14SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
132377fc4c14SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
132477fc4c14SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
132577fc4c14SDimitry Andric auto *ZII = static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
132677fc4c14SDimitry Andric auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
132777fc4c14SDimitry Andric
132877fc4c14SDimitry Andric // Skip the return instruction.
132977fc4c14SDimitry Andric assert(MBBI->isReturn() && "Can only insert epilogue into returning blocks");
133077fc4c14SDimitry Andric
133177fc4c14SDimitry Andric uint64_t StackSize = MFFrame.getStackSize();
133277fc4c14SDimitry Andric if (StackSize) {
133377fc4c14SDimitry Andric unsigned SPReg = Regs.getStackPointerRegister();
133477fc4c14SDimitry Andric if (ZFI->getRestoreGPRRegs().LowGPR != SPReg) {
133577fc4c14SDimitry Andric DebugLoc DL = MBBI->getDebugLoc();
133677fc4c14SDimitry Andric emitIncrement(MBB, MBBI, DL, SPReg, StackSize, ZII);
133777fc4c14SDimitry Andric }
133877fc4c14SDimitry Andric }
133977fc4c14SDimitry Andric }
1340c0981da4SDimitry Andric
1341145449b1SDimitry Andric // Emit a compare of the stack pointer against the stack floor, and a call to
1342145449b1SDimitry Andric // the LE stack extender if needed.
inlineStackProbe(MachineFunction & MF,MachineBasicBlock & PrologMBB) const1343145449b1SDimitry Andric void SystemZXPLINKFrameLowering::inlineStackProbe(
1344145449b1SDimitry Andric MachineFunction &MF, MachineBasicBlock &PrologMBB) const {
1345145449b1SDimitry Andric auto *ZII =
1346145449b1SDimitry Andric static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
1347145449b1SDimitry Andric
1348145449b1SDimitry Andric MachineInstr *StackAllocMI = nullptr;
1349145449b1SDimitry Andric for (MachineInstr &MI : PrologMBB)
1350145449b1SDimitry Andric if (MI.getOpcode() == SystemZ::XPLINK_STACKALLOC) {
1351145449b1SDimitry Andric StackAllocMI = &MI;
1352145449b1SDimitry Andric break;
1353145449b1SDimitry Andric }
1354145449b1SDimitry Andric if (StackAllocMI == nullptr)
1355145449b1SDimitry Andric return;
1356145449b1SDimitry Andric
13577fa27ce4SDimitry Andric bool NeedSaveSP = hasFP(MF);
13587fa27ce4SDimitry Andric bool NeedSaveArg = PrologMBB.isLiveIn(SystemZ::R3D);
13597fa27ce4SDimitry Andric const int64_t SaveSlotR3 = 2192;
13607fa27ce4SDimitry Andric
1361145449b1SDimitry Andric MachineBasicBlock &MBB = PrologMBB;
1362145449b1SDimitry Andric const DebugLoc DL = StackAllocMI->getDebugLoc();
1363145449b1SDimitry Andric
1364145449b1SDimitry Andric // The 2nd half of block MBB after split.
1365145449b1SDimitry Andric MachineBasicBlock *NextMBB;
1366145449b1SDimitry Andric
1367145449b1SDimitry Andric // Add new basic block for the call to the stack overflow function.
1368145449b1SDimitry Andric MachineBasicBlock *StackExtMBB =
1369145449b1SDimitry Andric MF.CreateMachineBasicBlock(MBB.getBasicBlock());
1370145449b1SDimitry Andric MF.push_back(StackExtMBB);
1371145449b1SDimitry Andric
1372145449b1SDimitry Andric // LG r3,72(,r3)
1373145449b1SDimitry Andric BuildMI(StackExtMBB, DL, ZII->get(SystemZ::LG), SystemZ::R3D)
1374145449b1SDimitry Andric .addReg(SystemZ::R3D)
1375145449b1SDimitry Andric .addImm(72)
1376145449b1SDimitry Andric .addReg(0);
1377145449b1SDimitry Andric // BASR r3,r3
1378145449b1SDimitry Andric BuildMI(StackExtMBB, DL, ZII->get(SystemZ::CallBASR_STACKEXT))
1379145449b1SDimitry Andric .addReg(SystemZ::R3D);
13807fa27ce4SDimitry Andric if (NeedSaveArg) {
13817fa27ce4SDimitry Andric if (!NeedSaveSP) {
13827fa27ce4SDimitry Andric // LGR r0,r3
13837fa27ce4SDimitry Andric BuildMI(MBB, StackAllocMI, DL, ZII->get(SystemZ::LGR))
13847fa27ce4SDimitry Andric .addReg(SystemZ::R0D, RegState::Define)
13857fa27ce4SDimitry Andric .addReg(SystemZ::R3D);
13867fa27ce4SDimitry Andric } else {
13877fa27ce4SDimitry Andric // In this case, the incoming value of r4 is saved in r0 so the
13887fa27ce4SDimitry Andric // latter register is unavailable. Store r3 in its corresponding
13897fa27ce4SDimitry Andric // slot in the parameter list instead. Do this at the start of
13907fa27ce4SDimitry Andric // the prolog before r4 is manipulated by anything else.
13917fa27ce4SDimitry Andric // STG r3, 2192(r4)
13927fa27ce4SDimitry Andric BuildMI(MBB, MBB.begin(), DL, ZII->get(SystemZ::STG))
13937fa27ce4SDimitry Andric .addReg(SystemZ::R3D)
13947fa27ce4SDimitry Andric .addReg(SystemZ::R4D)
13957fa27ce4SDimitry Andric .addImm(SaveSlotR3)
13967fa27ce4SDimitry Andric .addReg(0);
13977fa27ce4SDimitry Andric }
13987fa27ce4SDimitry Andric }
1399145449b1SDimitry Andric // LLGT r3,1208
1400145449b1SDimitry Andric BuildMI(MBB, StackAllocMI, DL, ZII->get(SystemZ::LLGT), SystemZ::R3D)
1401145449b1SDimitry Andric .addReg(0)
1402145449b1SDimitry Andric .addImm(1208)
1403145449b1SDimitry Andric .addReg(0);
1404145449b1SDimitry Andric // CG r4,64(,r3)
1405145449b1SDimitry Andric BuildMI(MBB, StackAllocMI, DL, ZII->get(SystemZ::CG))
1406145449b1SDimitry Andric .addReg(SystemZ::R4D)
1407145449b1SDimitry Andric .addReg(SystemZ::R3D)
1408145449b1SDimitry Andric .addImm(64)
1409145449b1SDimitry Andric .addReg(0);
1410145449b1SDimitry Andric // JLL b'0100',F'37'
1411145449b1SDimitry Andric BuildMI(MBB, StackAllocMI, DL, ZII->get(SystemZ::BRC))
1412145449b1SDimitry Andric .addImm(SystemZ::CCMASK_ICMP)
1413145449b1SDimitry Andric .addImm(SystemZ::CCMASK_CMP_LT)
1414145449b1SDimitry Andric .addMBB(StackExtMBB);
1415145449b1SDimitry Andric
1416145449b1SDimitry Andric NextMBB = SystemZ::splitBlockBefore(StackAllocMI, &MBB);
1417145449b1SDimitry Andric MBB.addSuccessor(NextMBB);
1418145449b1SDimitry Andric MBB.addSuccessor(StackExtMBB);
14197fa27ce4SDimitry Andric if (NeedSaveArg) {
14207fa27ce4SDimitry Andric if (!NeedSaveSP) {
14217fa27ce4SDimitry Andric // LGR r3, r0
14227fa27ce4SDimitry Andric BuildMI(*NextMBB, StackAllocMI, DL, ZII->get(SystemZ::LGR))
14237fa27ce4SDimitry Andric .addReg(SystemZ::R3D, RegState::Define)
14247fa27ce4SDimitry Andric .addReg(SystemZ::R0D, RegState::Kill);
14257fa27ce4SDimitry Andric } else {
14267fa27ce4SDimitry Andric // In this case, the incoming value of r4 is saved in r0 so the
14277fa27ce4SDimitry Andric // latter register is unavailable. We stored r3 in its corresponding
14287fa27ce4SDimitry Andric // slot in the parameter list instead and we now restore it from there.
14297fa27ce4SDimitry Andric // LGR r3, r0
14307fa27ce4SDimitry Andric BuildMI(*NextMBB, StackAllocMI, DL, ZII->get(SystemZ::LGR))
14317fa27ce4SDimitry Andric .addReg(SystemZ::R3D, RegState::Define)
14327fa27ce4SDimitry Andric .addReg(SystemZ::R0D);
14337fa27ce4SDimitry Andric // LG r3, 2192(r3)
14347fa27ce4SDimitry Andric BuildMI(*NextMBB, StackAllocMI, DL, ZII->get(SystemZ::LG))
14357fa27ce4SDimitry Andric .addReg(SystemZ::R3D, RegState::Define)
14367fa27ce4SDimitry Andric .addReg(SystemZ::R3D)
14377fa27ce4SDimitry Andric .addImm(SaveSlotR3)
14387fa27ce4SDimitry Andric .addReg(0);
14397fa27ce4SDimitry Andric }
14407fa27ce4SDimitry Andric }
1441145449b1SDimitry Andric
1442145449b1SDimitry Andric // Add jump back from stack extension BB.
1443145449b1SDimitry Andric BuildMI(StackExtMBB, DL, ZII->get(SystemZ::J)).addMBB(NextMBB);
1444145449b1SDimitry Andric StackExtMBB->addSuccessor(NextMBB);
1445145449b1SDimitry Andric
1446145449b1SDimitry Andric StackAllocMI->eraseFromParent();
1447145449b1SDimitry Andric
1448145449b1SDimitry Andric // Compute the live-in lists for the new blocks.
1449ac9a064cSDimitry Andric fullyRecomputeLiveIns({StackExtMBB, NextMBB});
1450145449b1SDimitry Andric }
1451145449b1SDimitry Andric
hasFP(const MachineFunction & MF) const1452c0981da4SDimitry Andric bool SystemZXPLINKFrameLowering::hasFP(const MachineFunction &MF) const {
145377fc4c14SDimitry Andric return (MF.getFrameInfo().hasVarSizedObjects());
145477fc4c14SDimitry Andric }
145577fc4c14SDimitry Andric
processFunctionBeforeFrameFinalized(MachineFunction & MF,RegScavenger * RS) const145677fc4c14SDimitry Andric void SystemZXPLINKFrameLowering::processFunctionBeforeFrameFinalized(
145777fc4c14SDimitry Andric MachineFunction &MF, RegScavenger *RS) const {
145877fc4c14SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
145977fc4c14SDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
146077fc4c14SDimitry Andric auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
146177fc4c14SDimitry Andric
146277fc4c14SDimitry Andric // Setup stack frame offset
146377fc4c14SDimitry Andric MFFrame.setOffsetAdjustment(Regs.getStackPointerBias());
1464b1c73532SDimitry Andric
1465b1c73532SDimitry Andric // Nothing to do for leaf functions.
1466b1c73532SDimitry Andric uint64_t StackSize = MFFrame.estimateStackSize(MF);
1467b1c73532SDimitry Andric if (StackSize == 0 && MFFrame.getCalleeSavedInfo().empty())
1468b1c73532SDimitry Andric return;
1469b1c73532SDimitry Andric
1470b1c73532SDimitry Andric // Although the XPLINK specifications for AMODE64 state that minimum size
1471b1c73532SDimitry Andric // of the param area is minimum 32 bytes and no rounding is otherwise
1472b1c73532SDimitry Andric // specified, we round this area in 64 bytes increments to be compatible
1473b1c73532SDimitry Andric // with existing compilers.
1474b1c73532SDimitry Andric MFFrame.setMaxCallFrameSize(
1475b1c73532SDimitry Andric std::max(64U, (unsigned)alignTo(MFFrame.getMaxCallFrameSize(), 64)));
1476ac9a064cSDimitry Andric
1477ac9a064cSDimitry Andric // Add frame values with positive object offsets. Since the displacement from
1478ac9a064cSDimitry Andric // the SP/FP is calculated by ObjectOffset + StackSize + Bias, object offsets
1479ac9a064cSDimitry Andric // with positive values are in the caller's stack frame. We need to include
1480ac9a064cSDimitry Andric // that since it is accessed by displacement to SP/FP.
1481ac9a064cSDimitry Andric int64_t LargestArgOffset = 0;
1482ac9a064cSDimitry Andric for (int I = MFFrame.getObjectIndexBegin(); I != 0; ++I) {
1483ac9a064cSDimitry Andric if (MFFrame.getObjectOffset(I) >= 0) {
1484ac9a064cSDimitry Andric int64_t ObjOffset = MFFrame.getObjectOffset(I) + MFFrame.getObjectSize(I);
1485ac9a064cSDimitry Andric LargestArgOffset = std::max(ObjOffset, LargestArgOffset);
1486ac9a064cSDimitry Andric }
1487ac9a064cSDimitry Andric }
1488ac9a064cSDimitry Andric
1489ac9a064cSDimitry Andric uint64_t MaxReach = (StackSize + Regs.getCallFrameSize() +
1490ac9a064cSDimitry Andric Regs.getStackPointerBias() + LargestArgOffset);
1491ac9a064cSDimitry Andric
1492ac9a064cSDimitry Andric if (!isUInt<12>(MaxReach)) {
1493ac9a064cSDimitry Andric // We may need register scavenging slots if some parts of the frame
1494ac9a064cSDimitry Andric // are outside the reach of an unsigned 12-bit displacement.
1495ac9a064cSDimitry Andric RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, Align(8), false));
1496ac9a064cSDimitry Andric RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, Align(8), false));
1497ac9a064cSDimitry Andric }
1498c0981da4SDimitry Andric }
14994b4fe385SDimitry Andric
15004b4fe385SDimitry Andric // Determines the size of the frame, and creates the deferred spill objects.
determineFrameLayout(MachineFunction & MF) const15014b4fe385SDimitry Andric void SystemZXPLINKFrameLowering::determineFrameLayout(
15024b4fe385SDimitry Andric MachineFunction &MF) const {
15034b4fe385SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
15044b4fe385SDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
15054b4fe385SDimitry Andric auto *Regs =
15064b4fe385SDimitry Andric static_cast<SystemZXPLINK64Registers *>(Subtarget.getSpecialRegisters());
15074b4fe385SDimitry Andric
15084b4fe385SDimitry Andric uint64_t StackSize = MFFrame.getStackSize();
15094b4fe385SDimitry Andric if (StackSize == 0)
15104b4fe385SDimitry Andric return;
15114b4fe385SDimitry Andric
15124b4fe385SDimitry Andric // Add the size of the register save area and the reserved area to the size.
15134b4fe385SDimitry Andric StackSize += Regs->getCallFrameSize();
15144b4fe385SDimitry Andric MFFrame.setStackSize(StackSize);
15154b4fe385SDimitry Andric
1516ac9a064cSDimitry Andric // We now know the stack size. Update the stack objects for the register save
1517ac9a064cSDimitry Andric // area now. This has no impact on the stack frame layout, as this is already
1518ac9a064cSDimitry Andric // computed. However, it makes sure that all callee saved registers have a
1519ac9a064cSDimitry Andric // valid offset assigned.
1520ac9a064cSDimitry Andric for (int FrameIdx = MFFrame.getObjectIndexBegin(); FrameIdx != 0;
1521ac9a064cSDimitry Andric ++FrameIdx) {
1522ac9a064cSDimitry Andric if (MFFrame.getStackID(FrameIdx) == TargetStackID::NoAlloc) {
1523ac9a064cSDimitry Andric int64_t SPOffset = MFFrame.getObjectOffset(FrameIdx);
1524ac9a064cSDimitry Andric SPOffset -= StackSize;
1525ac9a064cSDimitry Andric MFFrame.setObjectOffset(FrameIdx, SPOffset);
1526ac9a064cSDimitry Andric }
15274b4fe385SDimitry Andric }
15284b4fe385SDimitry Andric }
1529