10b57cec5SDimitry Andric //===-- SystemZFrameLowering.cpp - Frame lowering for SystemZ -------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "SystemZFrameLowering.h" 100b57cec5SDimitry Andric #include "SystemZCallingConv.h" 110b57cec5SDimitry Andric #include "SystemZInstrBuilder.h" 120b57cec5SDimitry Andric #include "SystemZInstrInfo.h" 130b57cec5SDimitry Andric #include "SystemZMachineFunctionInfo.h" 140b57cec5SDimitry Andric #include "SystemZRegisterInfo.h" 150b57cec5SDimitry Andric #include "SystemZSubtarget.h" 160b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h" 170b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 180b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h" 190b57cec5SDimitry Andric #include "llvm/IR/Function.h" 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric using namespace llvm; 220b57cec5SDimitry Andric 230b57cec5SDimitry Andric namespace { 240b57cec5SDimitry Andric // The ABI-defined register save slots, relative to the incoming stack 250b57cec5SDimitry Andric // pointer. 260b57cec5SDimitry Andric static const TargetFrameLowering::SpillSlot SpillOffsetTable[] = { 270b57cec5SDimitry Andric { SystemZ::R2D, 0x10 }, 280b57cec5SDimitry Andric { SystemZ::R3D, 0x18 }, 290b57cec5SDimitry Andric { SystemZ::R4D, 0x20 }, 300b57cec5SDimitry Andric { SystemZ::R5D, 0x28 }, 310b57cec5SDimitry Andric { SystemZ::R6D, 0x30 }, 320b57cec5SDimitry Andric { SystemZ::R7D, 0x38 }, 330b57cec5SDimitry Andric { SystemZ::R8D, 0x40 }, 340b57cec5SDimitry Andric { SystemZ::R9D, 0x48 }, 350b57cec5SDimitry Andric { SystemZ::R10D, 0x50 }, 360b57cec5SDimitry Andric { SystemZ::R11D, 0x58 }, 370b57cec5SDimitry Andric { SystemZ::R12D, 0x60 }, 380b57cec5SDimitry Andric { SystemZ::R13D, 0x68 }, 390b57cec5SDimitry Andric { SystemZ::R14D, 0x70 }, 400b57cec5SDimitry Andric { SystemZ::R15D, 0x78 }, 410b57cec5SDimitry Andric { SystemZ::F0D, 0x80 }, 420b57cec5SDimitry Andric { SystemZ::F2D, 0x88 }, 430b57cec5SDimitry Andric { SystemZ::F4D, 0x90 }, 440b57cec5SDimitry Andric { SystemZ::F6D, 0x98 } 450b57cec5SDimitry Andric }; 460b57cec5SDimitry Andric } // end anonymous namespace 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric SystemZFrameLowering::SystemZFrameLowering() 49*8bcb0991SDimitry Andric : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, Align(8), 50*8bcb0991SDimitry Andric -SystemZMC::CallFrameSize, Align(8), 510b57cec5SDimitry Andric false /* StackRealignable */) { 520b57cec5SDimitry Andric // Create a mapping from register number to save slot offset. 530b57cec5SDimitry Andric RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS); 540b57cec5SDimitry Andric for (unsigned I = 0, E = array_lengthof(SpillOffsetTable); I != E; ++I) 550b57cec5SDimitry Andric RegSpillOffsets[SpillOffsetTable[I].Reg] = SpillOffsetTable[I].Offset; 560b57cec5SDimitry Andric } 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric const TargetFrameLowering::SpillSlot * 590b57cec5SDimitry Andric SystemZFrameLowering::getCalleeSavedSpillSlots(unsigned &NumEntries) const { 600b57cec5SDimitry Andric NumEntries = array_lengthof(SpillOffsetTable); 610b57cec5SDimitry Andric return SpillOffsetTable; 620b57cec5SDimitry Andric } 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric void SystemZFrameLowering::determineCalleeSaves(MachineFunction &MF, 650b57cec5SDimitry Andric BitVector &SavedRegs, 660b57cec5SDimitry Andric RegScavenger *RS) const { 670b57cec5SDimitry Andric TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS); 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo(); 700b57cec5SDimitry Andric const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 710b57cec5SDimitry Andric bool HasFP = hasFP(MF); 720b57cec5SDimitry Andric SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>(); 730b57cec5SDimitry Andric bool IsVarArg = MF.getFunction().isVarArg(); 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric // va_start stores incoming FPR varargs in the normal way, but delegates 760b57cec5SDimitry Andric // the saving of incoming GPR varargs to spillCalleeSavedRegisters(). 770b57cec5SDimitry Andric // Record these pending uses, which typically include the call-saved 780b57cec5SDimitry Andric // argument register R6D. 790b57cec5SDimitry Andric if (IsVarArg) 800b57cec5SDimitry Andric for (unsigned I = MFI->getVarArgsFirstGPR(); I < SystemZ::NumArgGPRs; ++I) 810b57cec5SDimitry Andric SavedRegs.set(SystemZ::ArgGPRs[I]); 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric // If there are any landing pads, entering them will modify r6/r7. 840b57cec5SDimitry Andric if (!MF.getLandingPads().empty()) { 850b57cec5SDimitry Andric SavedRegs.set(SystemZ::R6D); 860b57cec5SDimitry Andric SavedRegs.set(SystemZ::R7D); 870b57cec5SDimitry Andric } 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric // If the function requires a frame pointer, record that the hard 900b57cec5SDimitry Andric // frame pointer will be clobbered. 910b57cec5SDimitry Andric if (HasFP) 920b57cec5SDimitry Andric SavedRegs.set(SystemZ::R11D); 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric // If the function calls other functions, record that the return 950b57cec5SDimitry Andric // address register will be clobbered. 960b57cec5SDimitry Andric if (MFFrame.hasCalls()) 970b57cec5SDimitry Andric SavedRegs.set(SystemZ::R14D); 980b57cec5SDimitry Andric 990b57cec5SDimitry Andric // If we are saving GPRs other than the stack pointer, we might as well 1000b57cec5SDimitry Andric // save and restore the stack pointer at the same time, via STMG and LMG. 1010b57cec5SDimitry Andric // This allows the deallocation to be done by the LMG, rather than needing 1020b57cec5SDimitry Andric // a separate %r15 addition. 1030b57cec5SDimitry Andric const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF); 1040b57cec5SDimitry Andric for (unsigned I = 0; CSRegs[I]; ++I) { 1050b57cec5SDimitry Andric unsigned Reg = CSRegs[I]; 1060b57cec5SDimitry Andric if (SystemZ::GR64BitRegClass.contains(Reg) && SavedRegs.test(Reg)) { 1070b57cec5SDimitry Andric SavedRegs.set(SystemZ::R15D); 1080b57cec5SDimitry Andric break; 1090b57cec5SDimitry Andric } 1100b57cec5SDimitry Andric } 1110b57cec5SDimitry Andric } 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric // Add GPR64 to the save instruction being built by MIB, which is in basic 1140b57cec5SDimitry Andric // block MBB. IsImplicit says whether this is an explicit operand to the 1150b57cec5SDimitry Andric // instruction, or an implicit one that comes between the explicit start 1160b57cec5SDimitry Andric // and end registers. 1170b57cec5SDimitry Andric static void addSavedGPR(MachineBasicBlock &MBB, MachineInstrBuilder &MIB, 1180b57cec5SDimitry Andric unsigned GPR64, bool IsImplicit) { 1190b57cec5SDimitry Andric const TargetRegisterInfo *RI = 1200b57cec5SDimitry Andric MBB.getParent()->getSubtarget().getRegisterInfo(); 121*8bcb0991SDimitry Andric Register GPR32 = RI->getSubReg(GPR64, SystemZ::subreg_l32); 1220b57cec5SDimitry Andric bool IsLive = MBB.isLiveIn(GPR64) || MBB.isLiveIn(GPR32); 1230b57cec5SDimitry Andric if (!IsLive || !IsImplicit) { 1240b57cec5SDimitry Andric MIB.addReg(GPR64, getImplRegState(IsImplicit) | getKillRegState(!IsLive)); 1250b57cec5SDimitry Andric if (!IsLive) 1260b57cec5SDimitry Andric MBB.addLiveIn(GPR64); 1270b57cec5SDimitry Andric } 1280b57cec5SDimitry Andric } 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric bool SystemZFrameLowering:: 1310b57cec5SDimitry Andric spillCalleeSavedRegisters(MachineBasicBlock &MBB, 1320b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI, 1330b57cec5SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 1340b57cec5SDimitry Andric const TargetRegisterInfo *TRI) const { 1350b57cec5SDimitry Andric if (CSI.empty()) 1360b57cec5SDimitry Andric return false; 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 1390b57cec5SDimitry Andric const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); 1400b57cec5SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); 1410b57cec5SDimitry Andric bool IsVarArg = MF.getFunction().isVarArg(); 1420b57cec5SDimitry Andric DebugLoc DL; 1430b57cec5SDimitry Andric 1440b57cec5SDimitry Andric // Scan the call-saved GPRs and find the bounds of the register spill area. 1450b57cec5SDimitry Andric unsigned LowGPR = 0; 1460b57cec5SDimitry Andric unsigned HighGPR = SystemZ::R15D; 1470b57cec5SDimitry Andric unsigned StartOffset = -1U; 1480b57cec5SDimitry Andric for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 1490b57cec5SDimitry Andric unsigned Reg = CSI[I].getReg(); 1500b57cec5SDimitry Andric if (SystemZ::GR64BitRegClass.contains(Reg)) { 1510b57cec5SDimitry Andric unsigned Offset = RegSpillOffsets[Reg]; 1520b57cec5SDimitry Andric assert(Offset && "Unexpected GPR save"); 1530b57cec5SDimitry Andric if (StartOffset > Offset) { 1540b57cec5SDimitry Andric LowGPR = Reg; 1550b57cec5SDimitry Andric StartOffset = Offset; 1560b57cec5SDimitry Andric } 1570b57cec5SDimitry Andric } 1580b57cec5SDimitry Andric } 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric // Save the range of call-saved registers, for use by the epilogue inserter. 1610b57cec5SDimitry Andric ZFI->setLowSavedGPR(LowGPR); 1620b57cec5SDimitry Andric ZFI->setHighSavedGPR(HighGPR); 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric // Include the GPR varargs, if any. R6D is call-saved, so would 1650b57cec5SDimitry Andric // be included by the loop above, but we also need to handle the 1660b57cec5SDimitry Andric // call-clobbered argument registers. 1670b57cec5SDimitry Andric if (IsVarArg) { 1680b57cec5SDimitry Andric unsigned FirstGPR = ZFI->getVarArgsFirstGPR(); 1690b57cec5SDimitry Andric if (FirstGPR < SystemZ::NumArgGPRs) { 1700b57cec5SDimitry Andric unsigned Reg = SystemZ::ArgGPRs[FirstGPR]; 1710b57cec5SDimitry Andric unsigned Offset = RegSpillOffsets[Reg]; 1720b57cec5SDimitry Andric if (StartOffset > Offset) { 1730b57cec5SDimitry Andric LowGPR = Reg; StartOffset = Offset; 1740b57cec5SDimitry Andric } 1750b57cec5SDimitry Andric } 1760b57cec5SDimitry Andric } 1770b57cec5SDimitry Andric 1780b57cec5SDimitry Andric // Save GPRs 1790b57cec5SDimitry Andric if (LowGPR) { 1800b57cec5SDimitry Andric assert(LowGPR != HighGPR && "Should be saving %r15 and something else"); 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andric // Build an STMG instruction. 1830b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STMG)); 1840b57cec5SDimitry Andric 1850b57cec5SDimitry Andric // Add the explicit register operands. 1860b57cec5SDimitry Andric addSavedGPR(MBB, MIB, LowGPR, false); 1870b57cec5SDimitry Andric addSavedGPR(MBB, MIB, HighGPR, false); 1880b57cec5SDimitry Andric 1890b57cec5SDimitry Andric // Add the address. 1900b57cec5SDimitry Andric MIB.addReg(SystemZ::R15D).addImm(StartOffset); 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric // Make sure all call-saved GPRs are included as operands and are 1930b57cec5SDimitry Andric // marked as live on entry. 1940b57cec5SDimitry Andric for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 1950b57cec5SDimitry Andric unsigned Reg = CSI[I].getReg(); 1960b57cec5SDimitry Andric if (SystemZ::GR64BitRegClass.contains(Reg)) 1970b57cec5SDimitry Andric addSavedGPR(MBB, MIB, Reg, true); 1980b57cec5SDimitry Andric } 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric // ...likewise GPR varargs. 2010b57cec5SDimitry Andric if (IsVarArg) 2020b57cec5SDimitry Andric for (unsigned I = ZFI->getVarArgsFirstGPR(); I < SystemZ::NumArgGPRs; ++I) 2030b57cec5SDimitry Andric addSavedGPR(MBB, MIB, SystemZ::ArgGPRs[I], true); 2040b57cec5SDimitry Andric } 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric // Save FPRs/VRs in the normal TargetInstrInfo way. 2070b57cec5SDimitry Andric for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 2080b57cec5SDimitry Andric unsigned Reg = CSI[I].getReg(); 2090b57cec5SDimitry Andric if (SystemZ::FP64BitRegClass.contains(Reg)) { 2100b57cec5SDimitry Andric MBB.addLiveIn(Reg); 2110b57cec5SDimitry Andric TII->storeRegToStackSlot(MBB, MBBI, Reg, true, CSI[I].getFrameIdx(), 2120b57cec5SDimitry Andric &SystemZ::FP64BitRegClass, TRI); 2130b57cec5SDimitry Andric } 2140b57cec5SDimitry Andric if (SystemZ::VR128BitRegClass.contains(Reg)) { 2150b57cec5SDimitry Andric MBB.addLiveIn(Reg); 2160b57cec5SDimitry Andric TII->storeRegToStackSlot(MBB, MBBI, Reg, true, CSI[I].getFrameIdx(), 2170b57cec5SDimitry Andric &SystemZ::VR128BitRegClass, TRI); 2180b57cec5SDimitry Andric } 2190b57cec5SDimitry Andric } 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric return true; 2220b57cec5SDimitry Andric } 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric bool SystemZFrameLowering:: 2250b57cec5SDimitry Andric restoreCalleeSavedRegisters(MachineBasicBlock &MBB, 2260b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI, 2270b57cec5SDimitry Andric std::vector<CalleeSavedInfo> &CSI, 2280b57cec5SDimitry Andric const TargetRegisterInfo *TRI) const { 2290b57cec5SDimitry Andric if (CSI.empty()) 2300b57cec5SDimitry Andric return false; 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 2330b57cec5SDimitry Andric const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); 2340b57cec5SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); 2350b57cec5SDimitry Andric bool HasFP = hasFP(MF); 2360b57cec5SDimitry Andric DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 2370b57cec5SDimitry Andric 2380b57cec5SDimitry Andric // Restore FPRs/VRs in the normal TargetInstrInfo way. 2390b57cec5SDimitry Andric for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 2400b57cec5SDimitry Andric unsigned Reg = CSI[I].getReg(); 2410b57cec5SDimitry Andric if (SystemZ::FP64BitRegClass.contains(Reg)) 2420b57cec5SDimitry Andric TII->loadRegFromStackSlot(MBB, MBBI, Reg, CSI[I].getFrameIdx(), 2430b57cec5SDimitry Andric &SystemZ::FP64BitRegClass, TRI); 2440b57cec5SDimitry Andric if (SystemZ::VR128BitRegClass.contains(Reg)) 2450b57cec5SDimitry Andric TII->loadRegFromStackSlot(MBB, MBBI, Reg, CSI[I].getFrameIdx(), 2460b57cec5SDimitry Andric &SystemZ::VR128BitRegClass, TRI); 2470b57cec5SDimitry Andric } 2480b57cec5SDimitry Andric 2490b57cec5SDimitry Andric // Restore call-saved GPRs (but not call-clobbered varargs, which at 2500b57cec5SDimitry Andric // this point might hold return values). 2510b57cec5SDimitry Andric unsigned LowGPR = ZFI->getLowSavedGPR(); 2520b57cec5SDimitry Andric unsigned HighGPR = ZFI->getHighSavedGPR(); 2530b57cec5SDimitry Andric unsigned StartOffset = RegSpillOffsets[LowGPR]; 2540b57cec5SDimitry Andric if (LowGPR) { 2550b57cec5SDimitry Andric // If we saved any of %r2-%r5 as varargs, we should also be saving 2560b57cec5SDimitry Andric // and restoring %r6. If we're saving %r6 or above, we should be 2570b57cec5SDimitry Andric // restoring it too. 2580b57cec5SDimitry Andric assert(LowGPR != HighGPR && "Should be loading %r15 and something else"); 2590b57cec5SDimitry Andric 2600b57cec5SDimitry Andric // Build an LMG instruction. 2610b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LMG)); 2620b57cec5SDimitry Andric 2630b57cec5SDimitry Andric // Add the explicit register operands. 2640b57cec5SDimitry Andric MIB.addReg(LowGPR, RegState::Define); 2650b57cec5SDimitry Andric MIB.addReg(HighGPR, RegState::Define); 2660b57cec5SDimitry Andric 2670b57cec5SDimitry Andric // Add the address. 2680b57cec5SDimitry Andric MIB.addReg(HasFP ? SystemZ::R11D : SystemZ::R15D); 2690b57cec5SDimitry Andric MIB.addImm(StartOffset); 2700b57cec5SDimitry Andric 2710b57cec5SDimitry Andric // Do a second scan adding regs as being defined by instruction 2720b57cec5SDimitry Andric for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 2730b57cec5SDimitry Andric unsigned Reg = CSI[I].getReg(); 2740b57cec5SDimitry Andric if (Reg != LowGPR && Reg != HighGPR && 2750b57cec5SDimitry Andric SystemZ::GR64BitRegClass.contains(Reg)) 2760b57cec5SDimitry Andric MIB.addReg(Reg, RegState::ImplicitDefine); 2770b57cec5SDimitry Andric } 2780b57cec5SDimitry Andric } 2790b57cec5SDimitry Andric 2800b57cec5SDimitry Andric return true; 2810b57cec5SDimitry Andric } 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andric void SystemZFrameLowering:: 2840b57cec5SDimitry Andric processFunctionBeforeFrameFinalized(MachineFunction &MF, 2850b57cec5SDimitry Andric RegScavenger *RS) const { 2860b57cec5SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo(); 2870b57cec5SDimitry Andric // Get the size of our stack frame to be allocated ... 2880b57cec5SDimitry Andric uint64_t StackSize = (MFFrame.estimateStackSize(MF) + 2890b57cec5SDimitry Andric SystemZMC::CallFrameSize); 2900b57cec5SDimitry Andric // ... and the maximum offset we may need to reach into the 2910b57cec5SDimitry Andric // caller's frame to access the save area or stack arguments. 2920b57cec5SDimitry Andric int64_t MaxArgOffset = SystemZMC::CallFrameSize; 2930b57cec5SDimitry Andric for (int I = MFFrame.getObjectIndexBegin(); I != 0; ++I) 2940b57cec5SDimitry Andric if (MFFrame.getObjectOffset(I) >= 0) { 2950b57cec5SDimitry Andric int64_t ArgOffset = SystemZMC::CallFrameSize + 2960b57cec5SDimitry Andric MFFrame.getObjectOffset(I) + 2970b57cec5SDimitry Andric MFFrame.getObjectSize(I); 2980b57cec5SDimitry Andric MaxArgOffset = std::max(MaxArgOffset, ArgOffset); 2990b57cec5SDimitry Andric } 3000b57cec5SDimitry Andric 3010b57cec5SDimitry Andric uint64_t MaxReach = StackSize + MaxArgOffset; 3020b57cec5SDimitry Andric if (!isUInt<12>(MaxReach)) { 3030b57cec5SDimitry Andric // We may need register scavenging slots if some parts of the frame 3040b57cec5SDimitry Andric // are outside the reach of an unsigned 12-bit displacement. 3050b57cec5SDimitry Andric // Create 2 for the case where both addresses in an MVC are 3060b57cec5SDimitry Andric // out of range. 3070b57cec5SDimitry Andric RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, 8, false)); 3080b57cec5SDimitry Andric RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, 8, false)); 3090b57cec5SDimitry Andric } 3100b57cec5SDimitry Andric } 3110b57cec5SDimitry Andric 3120b57cec5SDimitry Andric // Emit instructions before MBBI (in MBB) to add NumBytes to Reg. 3130b57cec5SDimitry Andric static void emitIncrement(MachineBasicBlock &MBB, 3140b57cec5SDimitry Andric MachineBasicBlock::iterator &MBBI, 3150b57cec5SDimitry Andric const DebugLoc &DL, 3160b57cec5SDimitry Andric unsigned Reg, int64_t NumBytes, 3170b57cec5SDimitry Andric const TargetInstrInfo *TII) { 3180b57cec5SDimitry Andric while (NumBytes) { 3190b57cec5SDimitry Andric unsigned Opcode; 3200b57cec5SDimitry Andric int64_t ThisVal = NumBytes; 3210b57cec5SDimitry Andric if (isInt<16>(NumBytes)) 3220b57cec5SDimitry Andric Opcode = SystemZ::AGHI; 3230b57cec5SDimitry Andric else { 3240b57cec5SDimitry Andric Opcode = SystemZ::AGFI; 3250b57cec5SDimitry Andric // Make sure we maintain 8-byte stack alignment. 3260b57cec5SDimitry Andric int64_t MinVal = -uint64_t(1) << 31; 3270b57cec5SDimitry Andric int64_t MaxVal = (int64_t(1) << 31) - 8; 3280b57cec5SDimitry Andric if (ThisVal < MinVal) 3290b57cec5SDimitry Andric ThisVal = MinVal; 3300b57cec5SDimitry Andric else if (ThisVal > MaxVal) 3310b57cec5SDimitry Andric ThisVal = MaxVal; 3320b57cec5SDimitry Andric } 3330b57cec5SDimitry Andric MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII->get(Opcode), Reg) 3340b57cec5SDimitry Andric .addReg(Reg).addImm(ThisVal); 3350b57cec5SDimitry Andric // The CC implicit def is dead. 3360b57cec5SDimitry Andric MI->getOperand(3).setIsDead(); 3370b57cec5SDimitry Andric NumBytes -= ThisVal; 3380b57cec5SDimitry Andric } 3390b57cec5SDimitry Andric } 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andric void SystemZFrameLowering::emitPrologue(MachineFunction &MF, 3420b57cec5SDimitry Andric MachineBasicBlock &MBB) const { 3430b57cec5SDimitry Andric assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported"); 3440b57cec5SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo(); 3450b57cec5SDimitry Andric auto *ZII = 3460b57cec5SDimitry Andric static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo()); 3470b57cec5SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); 3480b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.begin(); 3490b57cec5SDimitry Andric MachineModuleInfo &MMI = MF.getMMI(); 3500b57cec5SDimitry Andric const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo(); 3510b57cec5SDimitry Andric const std::vector<CalleeSavedInfo> &CSI = MFFrame.getCalleeSavedInfo(); 3520b57cec5SDimitry Andric bool HasFP = hasFP(MF); 3530b57cec5SDimitry Andric 3540b57cec5SDimitry Andric // Debug location must be unknown since the first debug location is used 3550b57cec5SDimitry Andric // to determine the end of the prologue. 3560b57cec5SDimitry Andric DebugLoc DL; 3570b57cec5SDimitry Andric 3580b57cec5SDimitry Andric // The current offset of the stack pointer from the CFA. 3590b57cec5SDimitry Andric int64_t SPOffsetFromCFA = -SystemZMC::CFAOffsetFromInitialSP; 3600b57cec5SDimitry Andric 3610b57cec5SDimitry Andric if (ZFI->getLowSavedGPR()) { 3620b57cec5SDimitry Andric // Skip over the GPR saves. 3630b57cec5SDimitry Andric if (MBBI != MBB.end() && MBBI->getOpcode() == SystemZ::STMG) 3640b57cec5SDimitry Andric ++MBBI; 3650b57cec5SDimitry Andric else 3660b57cec5SDimitry Andric llvm_unreachable("Couldn't skip over GPR saves"); 3670b57cec5SDimitry Andric 3680b57cec5SDimitry Andric // Add CFI for the GPR saves. 3690b57cec5SDimitry Andric for (auto &Save : CSI) { 3700b57cec5SDimitry Andric unsigned Reg = Save.getReg(); 3710b57cec5SDimitry Andric if (SystemZ::GR64BitRegClass.contains(Reg)) { 3720b57cec5SDimitry Andric int64_t Offset = SPOffsetFromCFA + RegSpillOffsets[Reg]; 3730b57cec5SDimitry Andric unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset( 3740b57cec5SDimitry Andric nullptr, MRI->getDwarfRegNum(Reg, true), Offset)); 3750b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) 3760b57cec5SDimitry Andric .addCFIIndex(CFIIndex); 3770b57cec5SDimitry Andric } 3780b57cec5SDimitry Andric } 3790b57cec5SDimitry Andric } 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric uint64_t StackSize = MFFrame.getStackSize(); 3820b57cec5SDimitry Andric // We need to allocate the ABI-defined 160-byte base area whenever 3830b57cec5SDimitry Andric // we allocate stack space for our own use and whenever we call another 3840b57cec5SDimitry Andric // function. 3850b57cec5SDimitry Andric if (StackSize || MFFrame.hasVarSizedObjects() || MFFrame.hasCalls()) { 3860b57cec5SDimitry Andric StackSize += SystemZMC::CallFrameSize; 3870b57cec5SDimitry Andric MFFrame.setStackSize(StackSize); 3880b57cec5SDimitry Andric } 3890b57cec5SDimitry Andric 3900b57cec5SDimitry Andric if (StackSize) { 3910b57cec5SDimitry Andric // Determine if we want to store a backchain. 3920b57cec5SDimitry Andric bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain"); 3930b57cec5SDimitry Andric 3940b57cec5SDimitry Andric // If we need backchain, save current stack pointer. R1 is free at this 3950b57cec5SDimitry Andric // point. 3960b57cec5SDimitry Andric if (StoreBackchain) 3970b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR)) 3980b57cec5SDimitry Andric .addReg(SystemZ::R1D, RegState::Define).addReg(SystemZ::R15D); 3990b57cec5SDimitry Andric 4000b57cec5SDimitry Andric // Allocate StackSize bytes. 4010b57cec5SDimitry Andric int64_t Delta = -int64_t(StackSize); 4020b57cec5SDimitry Andric emitIncrement(MBB, MBBI, DL, SystemZ::R15D, Delta, ZII); 4030b57cec5SDimitry Andric 4040b57cec5SDimitry Andric // Add CFI for the allocation. 4050b57cec5SDimitry Andric unsigned CFIIndex = MF.addFrameInst( 4060b57cec5SDimitry Andric MCCFIInstruction::createDefCfaOffset(nullptr, SPOffsetFromCFA + Delta)); 4070b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) 4080b57cec5SDimitry Andric .addCFIIndex(CFIIndex); 4090b57cec5SDimitry Andric SPOffsetFromCFA += Delta; 4100b57cec5SDimitry Andric 4110b57cec5SDimitry Andric if (StoreBackchain) 4120b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::STG)) 4130b57cec5SDimitry Andric .addReg(SystemZ::R1D, RegState::Kill).addReg(SystemZ::R15D).addImm(0).addReg(0); 4140b57cec5SDimitry Andric } 4150b57cec5SDimitry Andric 4160b57cec5SDimitry Andric if (HasFP) { 4170b57cec5SDimitry Andric // Copy the base of the frame to R11. 4180b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR), SystemZ::R11D) 4190b57cec5SDimitry Andric .addReg(SystemZ::R15D); 4200b57cec5SDimitry Andric 4210b57cec5SDimitry Andric // Add CFI for the new frame location. 4220b57cec5SDimitry Andric unsigned HardFP = MRI->getDwarfRegNum(SystemZ::R11D, true); 4230b57cec5SDimitry Andric unsigned CFIIndex = MF.addFrameInst( 4240b57cec5SDimitry Andric MCCFIInstruction::createDefCfaRegister(nullptr, HardFP)); 4250b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) 4260b57cec5SDimitry Andric .addCFIIndex(CFIIndex); 4270b57cec5SDimitry Andric 4280b57cec5SDimitry Andric // Mark the FramePtr as live at the beginning of every block except 4290b57cec5SDimitry Andric // the entry block. (We'll have marked R11 as live on entry when 4300b57cec5SDimitry Andric // saving the GPRs.) 4310b57cec5SDimitry Andric for (auto I = std::next(MF.begin()), E = MF.end(); I != E; ++I) 4320b57cec5SDimitry Andric I->addLiveIn(SystemZ::R11D); 4330b57cec5SDimitry Andric } 4340b57cec5SDimitry Andric 4350b57cec5SDimitry Andric // Skip over the FPR/VR saves. 4360b57cec5SDimitry Andric SmallVector<unsigned, 8> CFIIndexes; 4370b57cec5SDimitry Andric for (auto &Save : CSI) { 4380b57cec5SDimitry Andric unsigned Reg = Save.getReg(); 4390b57cec5SDimitry Andric if (SystemZ::FP64BitRegClass.contains(Reg)) { 4400b57cec5SDimitry Andric if (MBBI != MBB.end() && 4410b57cec5SDimitry Andric (MBBI->getOpcode() == SystemZ::STD || 4420b57cec5SDimitry Andric MBBI->getOpcode() == SystemZ::STDY)) 4430b57cec5SDimitry Andric ++MBBI; 4440b57cec5SDimitry Andric else 4450b57cec5SDimitry Andric llvm_unreachable("Couldn't skip over FPR save"); 4460b57cec5SDimitry Andric } else if (SystemZ::VR128BitRegClass.contains(Reg)) { 4470b57cec5SDimitry Andric if (MBBI != MBB.end() && 4480b57cec5SDimitry Andric MBBI->getOpcode() == SystemZ::VST) 4490b57cec5SDimitry Andric ++MBBI; 4500b57cec5SDimitry Andric else 4510b57cec5SDimitry Andric llvm_unreachable("Couldn't skip over VR save"); 4520b57cec5SDimitry Andric } else 4530b57cec5SDimitry Andric continue; 4540b57cec5SDimitry Andric 4550b57cec5SDimitry Andric // Add CFI for the this save. 4560b57cec5SDimitry Andric unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true); 4570b57cec5SDimitry Andric unsigned IgnoredFrameReg; 4580b57cec5SDimitry Andric int64_t Offset = 4590b57cec5SDimitry Andric getFrameIndexReference(MF, Save.getFrameIdx(), IgnoredFrameReg); 4600b57cec5SDimitry Andric 4610b57cec5SDimitry Andric unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset( 4620b57cec5SDimitry Andric nullptr, DwarfReg, SPOffsetFromCFA + Offset)); 4630b57cec5SDimitry Andric CFIIndexes.push_back(CFIIndex); 4640b57cec5SDimitry Andric } 4650b57cec5SDimitry Andric // Complete the CFI for the FPR/VR saves, modelling them as taking effect 4660b57cec5SDimitry Andric // after the last save. 4670b57cec5SDimitry Andric for (auto CFIIndex : CFIIndexes) { 4680b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) 4690b57cec5SDimitry Andric .addCFIIndex(CFIIndex); 4700b57cec5SDimitry Andric } 4710b57cec5SDimitry Andric } 4720b57cec5SDimitry Andric 4730b57cec5SDimitry Andric void SystemZFrameLowering::emitEpilogue(MachineFunction &MF, 4740b57cec5SDimitry Andric MachineBasicBlock &MBB) const { 4750b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 4760b57cec5SDimitry Andric auto *ZII = 4770b57cec5SDimitry Andric static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo()); 4780b57cec5SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); 4790b57cec5SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo(); 4800b57cec5SDimitry Andric 4810b57cec5SDimitry Andric // Skip the return instruction. 4820b57cec5SDimitry Andric assert(MBBI->isReturn() && "Can only insert epilogue into returning blocks"); 4830b57cec5SDimitry Andric 4840b57cec5SDimitry Andric uint64_t StackSize = MFFrame.getStackSize(); 4850b57cec5SDimitry Andric if (ZFI->getLowSavedGPR()) { 4860b57cec5SDimitry Andric --MBBI; 4870b57cec5SDimitry Andric unsigned Opcode = MBBI->getOpcode(); 4880b57cec5SDimitry Andric if (Opcode != SystemZ::LMG) 4890b57cec5SDimitry Andric llvm_unreachable("Expected to see callee-save register restore code"); 4900b57cec5SDimitry Andric 4910b57cec5SDimitry Andric unsigned AddrOpNo = 2; 4920b57cec5SDimitry Andric DebugLoc DL = MBBI->getDebugLoc(); 4930b57cec5SDimitry Andric uint64_t Offset = StackSize + MBBI->getOperand(AddrOpNo + 1).getImm(); 4940b57cec5SDimitry Andric unsigned NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset); 4950b57cec5SDimitry Andric 4960b57cec5SDimitry Andric // If the offset is too large, use the largest stack-aligned offset 4970b57cec5SDimitry Andric // and add the rest to the base register (the stack or frame pointer). 4980b57cec5SDimitry Andric if (!NewOpcode) { 4990b57cec5SDimitry Andric uint64_t NumBytes = Offset - 0x7fff8; 5000b57cec5SDimitry Andric emitIncrement(MBB, MBBI, DL, MBBI->getOperand(AddrOpNo).getReg(), 5010b57cec5SDimitry Andric NumBytes, ZII); 5020b57cec5SDimitry Andric Offset -= NumBytes; 5030b57cec5SDimitry Andric NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset); 5040b57cec5SDimitry Andric assert(NewOpcode && "No restore instruction available"); 5050b57cec5SDimitry Andric } 5060b57cec5SDimitry Andric 5070b57cec5SDimitry Andric MBBI->setDesc(ZII->get(NewOpcode)); 5080b57cec5SDimitry Andric MBBI->getOperand(AddrOpNo + 1).ChangeToImmediate(Offset); 5090b57cec5SDimitry Andric } else if (StackSize) { 5100b57cec5SDimitry Andric DebugLoc DL = MBBI->getDebugLoc(); 5110b57cec5SDimitry Andric emitIncrement(MBB, MBBI, DL, SystemZ::R15D, StackSize, ZII); 5120b57cec5SDimitry Andric } 5130b57cec5SDimitry Andric } 5140b57cec5SDimitry Andric 5150b57cec5SDimitry Andric bool SystemZFrameLowering::hasFP(const MachineFunction &MF) const { 5160b57cec5SDimitry Andric return (MF.getTarget().Options.DisableFramePointerElim(MF) || 5170b57cec5SDimitry Andric MF.getFrameInfo().hasVarSizedObjects() || 5180b57cec5SDimitry Andric MF.getInfo<SystemZMachineFunctionInfo>()->getManipulatesSP()); 5190b57cec5SDimitry Andric } 5200b57cec5SDimitry Andric 5210b57cec5SDimitry Andric bool 5220b57cec5SDimitry Andric SystemZFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { 5230b57cec5SDimitry Andric // The ABI requires us to allocate 160 bytes of stack space for the callee, 5240b57cec5SDimitry Andric // with any outgoing stack arguments being placed above that. It seems 5250b57cec5SDimitry Andric // better to make that area a permanent feature of the frame even if 5260b57cec5SDimitry Andric // we're using a frame pointer. 5270b57cec5SDimitry Andric return true; 5280b57cec5SDimitry Andric } 5290b57cec5SDimitry Andric 5300b57cec5SDimitry Andric MachineBasicBlock::iterator SystemZFrameLowering:: 5310b57cec5SDimitry Andric eliminateCallFramePseudoInstr(MachineFunction &MF, 5320b57cec5SDimitry Andric MachineBasicBlock &MBB, 5330b57cec5SDimitry Andric MachineBasicBlock::iterator MI) const { 5340b57cec5SDimitry Andric switch (MI->getOpcode()) { 5350b57cec5SDimitry Andric case SystemZ::ADJCALLSTACKDOWN: 5360b57cec5SDimitry Andric case SystemZ::ADJCALLSTACKUP: 5370b57cec5SDimitry Andric assert(hasReservedCallFrame(MF) && 5380b57cec5SDimitry Andric "ADJSTACKDOWN and ADJSTACKUP should be no-ops"); 5390b57cec5SDimitry Andric return MBB.erase(MI); 5400b57cec5SDimitry Andric break; 5410b57cec5SDimitry Andric 5420b57cec5SDimitry Andric default: 5430b57cec5SDimitry Andric llvm_unreachable("Unexpected call frame instruction"); 5440b57cec5SDimitry Andric } 5450b57cec5SDimitry Andric } 546