15f613dfdSUlrich Weigand //===-- SystemZFrameLowering.cpp - Frame lowering for SystemZ -------------===// 25f613dfdSUlrich Weigand // 35f613dfdSUlrich Weigand // The LLVM Compiler Infrastructure 45f613dfdSUlrich Weigand // 55f613dfdSUlrich Weigand // This file is distributed under the University of Illinois Open Source 65f613dfdSUlrich Weigand // License. See LICENSE.TXT for details. 75f613dfdSUlrich Weigand // 85f613dfdSUlrich Weigand //===----------------------------------------------------------------------===// 95f613dfdSUlrich Weigand 105f613dfdSUlrich Weigand #include "SystemZFrameLowering.h" 115f613dfdSUlrich Weigand #include "SystemZCallingConv.h" 125f613dfdSUlrich Weigand #include "SystemZInstrBuilder.h" 13f1bd22dfSEric Christopher #include "SystemZInstrInfo.h" 145f613dfdSUlrich Weigand #include "SystemZMachineFunctionInfo.h" 15f1bd22dfSEric Christopher #include "SystemZRegisterInfo.h" 16d913448bSEric Christopher #include "SystemZSubtarget.h" 175f613dfdSUlrich Weigand #include "llvm/CodeGen/MachineModuleInfo.h" 185f613dfdSUlrich Weigand #include "llvm/CodeGen/MachineRegisterInfo.h" 195dd52f8cSRichard Sandiford #include "llvm/CodeGen/RegisterScavenging.h" 205f613dfdSUlrich Weigand #include "llvm/IR/Function.h" 215f613dfdSUlrich Weigand 225f613dfdSUlrich Weigand using namespace llvm; 235f613dfdSUlrich Weigand 24db39b4a2SRichard Sandiford namespace { 255f613dfdSUlrich Weigand // The ABI-defined register save slots, relative to the incoming stack 265f613dfdSUlrich Weigand // pointer. 27db39b4a2SRichard Sandiford static const TargetFrameLowering::SpillSlot SpillOffsetTable[] = { 285f613dfdSUlrich Weigand { SystemZ::R2D, 0x10 }, 295f613dfdSUlrich Weigand { SystemZ::R3D, 0x18 }, 305f613dfdSUlrich Weigand { SystemZ::R4D, 0x20 }, 315f613dfdSUlrich Weigand { SystemZ::R5D, 0x28 }, 325f613dfdSUlrich Weigand { SystemZ::R6D, 0x30 }, 335f613dfdSUlrich Weigand { SystemZ::R7D, 0x38 }, 345f613dfdSUlrich Weigand { SystemZ::R8D, 0x40 }, 355f613dfdSUlrich Weigand { SystemZ::R9D, 0x48 }, 365f613dfdSUlrich Weigand { SystemZ::R10D, 0x50 }, 375f613dfdSUlrich Weigand { SystemZ::R11D, 0x58 }, 385f613dfdSUlrich Weigand { SystemZ::R12D, 0x60 }, 395f613dfdSUlrich Weigand { SystemZ::R13D, 0x68 }, 405f613dfdSUlrich Weigand { SystemZ::R14D, 0x70 }, 415f613dfdSUlrich Weigand { SystemZ::R15D, 0x78 }, 425f613dfdSUlrich Weigand { SystemZ::F0D, 0x80 }, 435f613dfdSUlrich Weigand { SystemZ::F2D, 0x88 }, 445f613dfdSUlrich Weigand { SystemZ::F4D, 0x90 }, 455f613dfdSUlrich Weigand { SystemZ::F6D, 0x98 } 465f613dfdSUlrich Weigand }; 47c231269fSRichard Sandiford } // end anonymous namespace 485f613dfdSUlrich Weigand 49f1bd22dfSEric Christopher SystemZFrameLowering::SystemZFrameLowering() 50db39b4a2SRichard Sandiford : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, 8, 51f12b925bSJonas Paulsson -SystemZMC::CallFrameSize, 8, 52f12b925bSJonas Paulsson false /* StackRealignable */) { 535f613dfdSUlrich Weigand // Create a mapping from register number to save slot offset. 545f613dfdSUlrich Weigand RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS); 555f613dfdSUlrich Weigand for (unsigned I = 0, E = array_lengthof(SpillOffsetTable); I != E; ++I) 56db39b4a2SRichard Sandiford RegSpillOffsets[SpillOffsetTable[I].Reg] = SpillOffsetTable[I].Offset; 57db39b4a2SRichard Sandiford } 58db39b4a2SRichard Sandiford 59db39b4a2SRichard Sandiford const TargetFrameLowering::SpillSlot * 60db39b4a2SRichard Sandiford SystemZFrameLowering::getCalleeSavedSpillSlots(unsigned &NumEntries) const { 61db39b4a2SRichard Sandiford NumEntries = array_lengthof(SpillOffsetTable); 62db39b4a2SRichard Sandiford return SpillOffsetTable; 635f613dfdSUlrich Weigand } 645f613dfdSUlrich Weigand 6502564865SMatthias Braun void SystemZFrameLowering::determineCalleeSaves(MachineFunction &MF, 6602564865SMatthias Braun BitVector &SavedRegs, 675f613dfdSUlrich Weigand RegScavenger *RS) const { 6802564865SMatthias Braun TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS); 6902564865SMatthias Braun 70941a705bSMatthias Braun MachineFrameInfo &MFFrame = MF.getFrameInfo(); 71fc6de428SEric Christopher const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 725f613dfdSUlrich Weigand bool HasFP = hasFP(MF); 735f613dfdSUlrich Weigand SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>(); 74*f1caa283SMatthias Braun bool IsVarArg = MF.getFunction().isVarArg(); 755f613dfdSUlrich Weigand 765f613dfdSUlrich Weigand // va_start stores incoming FPR varargs in the normal way, but delegates 775f613dfdSUlrich Weigand // the saving of incoming GPR varargs to spillCalleeSavedRegisters(). 785f613dfdSUlrich Weigand // Record these pending uses, which typically include the call-saved 795f613dfdSUlrich Weigand // argument register R6D. 805f613dfdSUlrich Weigand if (IsVarArg) 815f613dfdSUlrich Weigand for (unsigned I = MFI->getVarArgsFirstGPR(); I < SystemZ::NumArgGPRs; ++I) 8202564865SMatthias Braun SavedRegs.set(SystemZ::ArgGPRs[I]); 835f613dfdSUlrich Weigand 84234e5a80SMarcin Koscielnicki // If there are any landing pads, entering them will modify r6/r7. 85d0ee66c2SMatthias Braun if (!MF.getLandingPads().empty()) { 86234e5a80SMarcin Koscielnicki SavedRegs.set(SystemZ::R6D); 87234e5a80SMarcin Koscielnicki SavedRegs.set(SystemZ::R7D); 88234e5a80SMarcin Koscielnicki } 89234e5a80SMarcin Koscielnicki 905f613dfdSUlrich Weigand // If the function requires a frame pointer, record that the hard 915f613dfdSUlrich Weigand // frame pointer will be clobbered. 925f613dfdSUlrich Weigand if (HasFP) 9302564865SMatthias Braun SavedRegs.set(SystemZ::R11D); 945f613dfdSUlrich Weigand 955f613dfdSUlrich Weigand // If the function calls other functions, record that the return 965f613dfdSUlrich Weigand // address register will be clobbered. 97941a705bSMatthias Braun if (MFFrame.hasCalls()) 9802564865SMatthias Braun SavedRegs.set(SystemZ::R14D); 995f613dfdSUlrich Weigand 1005f613dfdSUlrich Weigand // If we are saving GPRs other than the stack pointer, we might as well 1015f613dfdSUlrich Weigand // save and restore the stack pointer at the same time, via STMG and LMG. 1025f613dfdSUlrich Weigand // This allows the deallocation to be done by the LMG, rather than needing 1035f613dfdSUlrich Weigand // a separate %r15 addition. 104840beec2SCraig Topper const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF); 1055f613dfdSUlrich Weigand for (unsigned I = 0; CSRegs[I]; ++I) { 1065f613dfdSUlrich Weigand unsigned Reg = CSRegs[I]; 10702564865SMatthias Braun if (SystemZ::GR64BitRegClass.contains(Reg) && SavedRegs.test(Reg)) { 10802564865SMatthias Braun SavedRegs.set(SystemZ::R15D); 1095f613dfdSUlrich Weigand break; 1105f613dfdSUlrich Weigand } 1115f613dfdSUlrich Weigand } 1125f613dfdSUlrich Weigand } 1135f613dfdSUlrich Weigand 1145f613dfdSUlrich Weigand // Add GPR64 to the save instruction being built by MIB, which is in basic 1155f613dfdSUlrich Weigand // block MBB. IsImplicit says whether this is an explicit operand to the 1165f613dfdSUlrich Weigand // instruction, or an implicit one that comes between the explicit start 1175f613dfdSUlrich Weigand // and end registers. 1185f613dfdSUlrich Weigand static void addSavedGPR(MachineBasicBlock &MBB, MachineInstrBuilder &MIB, 1195f613dfdSUlrich Weigand unsigned GPR64, bool IsImplicit) { 120d913448bSEric Christopher const TargetRegisterInfo *RI = 121fc6de428SEric Christopher MBB.getParent()->getSubtarget().getRegisterInfo(); 12287a44364SRichard Sandiford unsigned GPR32 = RI->getSubReg(GPR64, SystemZ::subreg_l32); 1235f613dfdSUlrich Weigand bool IsLive = MBB.isLiveIn(GPR64) || MBB.isLiveIn(GPR32); 1245f613dfdSUlrich Weigand if (!IsLive || !IsImplicit) { 1255f613dfdSUlrich Weigand MIB.addReg(GPR64, getImplRegState(IsImplicit) | getKillRegState(!IsLive)); 1265f613dfdSUlrich Weigand if (!IsLive) 1275f613dfdSUlrich Weigand MBB.addLiveIn(GPR64); 1285f613dfdSUlrich Weigand } 1295f613dfdSUlrich Weigand } 1305f613dfdSUlrich Weigand 1315f613dfdSUlrich Weigand bool SystemZFrameLowering:: 1325f613dfdSUlrich Weigand spillCalleeSavedRegisters(MachineBasicBlock &MBB, 1335f613dfdSUlrich Weigand MachineBasicBlock::iterator MBBI, 1345f613dfdSUlrich Weigand const std::vector<CalleeSavedInfo> &CSI, 1355f613dfdSUlrich Weigand const TargetRegisterInfo *TRI) const { 1365f613dfdSUlrich Weigand if (CSI.empty()) 1375f613dfdSUlrich Weigand return false; 1385f613dfdSUlrich Weigand 1395f613dfdSUlrich Weigand MachineFunction &MF = *MBB.getParent(); 140fc6de428SEric Christopher const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); 1415f613dfdSUlrich Weigand SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); 142*f1caa283SMatthias Braun bool IsVarArg = MF.getFunction().isVarArg(); 143057c5a6bSOleg Ranevskyy DebugLoc DL; 1445f613dfdSUlrich Weigand 1455f613dfdSUlrich Weigand // Scan the call-saved GPRs and find the bounds of the register spill area. 1465f613dfdSUlrich Weigand unsigned LowGPR = 0; 1475f613dfdSUlrich Weigand unsigned HighGPR = SystemZ::R15D; 1485f613dfdSUlrich Weigand unsigned StartOffset = -1U; 1495f613dfdSUlrich Weigand for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 1505f613dfdSUlrich Weigand unsigned Reg = CSI[I].getReg(); 1515f613dfdSUlrich Weigand if (SystemZ::GR64BitRegClass.contains(Reg)) { 1525f613dfdSUlrich Weigand unsigned Offset = RegSpillOffsets[Reg]; 1535f613dfdSUlrich Weigand assert(Offset && "Unexpected GPR save"); 1545f613dfdSUlrich Weigand if (StartOffset > Offset) { 1555f613dfdSUlrich Weigand LowGPR = Reg; 1565f613dfdSUlrich Weigand StartOffset = Offset; 1575f613dfdSUlrich Weigand } 1585f613dfdSUlrich Weigand } 1595f613dfdSUlrich Weigand } 1605f613dfdSUlrich Weigand 161db39b4a2SRichard Sandiford // Save the range of call-saved registers, for use by the epilogue inserter. 1625f613dfdSUlrich Weigand ZFI->setLowSavedGPR(LowGPR); 1635f613dfdSUlrich Weigand ZFI->setHighSavedGPR(HighGPR); 1645f613dfdSUlrich Weigand 1655f613dfdSUlrich Weigand // Include the GPR varargs, if any. R6D is call-saved, so would 1665f613dfdSUlrich Weigand // be included by the loop above, but we also need to handle the 1675f613dfdSUlrich Weigand // call-clobbered argument registers. 1685f613dfdSUlrich Weigand if (IsVarArg) { 1695f613dfdSUlrich Weigand unsigned FirstGPR = ZFI->getVarArgsFirstGPR(); 1705f613dfdSUlrich Weigand if (FirstGPR < SystemZ::NumArgGPRs) { 1715f613dfdSUlrich Weigand unsigned Reg = SystemZ::ArgGPRs[FirstGPR]; 1725f613dfdSUlrich Weigand unsigned Offset = RegSpillOffsets[Reg]; 1735f613dfdSUlrich Weigand if (StartOffset > Offset) { 1745f613dfdSUlrich Weigand LowGPR = Reg; StartOffset = Offset; 1755f613dfdSUlrich Weigand } 1765f613dfdSUlrich Weigand } 1775f613dfdSUlrich Weigand } 1785f613dfdSUlrich Weigand 1795f613dfdSUlrich Weigand // Save GPRs 1805f613dfdSUlrich Weigand if (LowGPR) { 1815f613dfdSUlrich Weigand assert(LowGPR != HighGPR && "Should be saving %r15 and something else"); 1825f613dfdSUlrich Weigand 1835f613dfdSUlrich Weigand // Build an STMG instruction. 1845f613dfdSUlrich Weigand MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STMG)); 1855f613dfdSUlrich Weigand 1865f613dfdSUlrich Weigand // Add the explicit register operands. 187f1bd22dfSEric Christopher addSavedGPR(MBB, MIB, LowGPR, false); 188f1bd22dfSEric Christopher addSavedGPR(MBB, MIB, HighGPR, false); 1895f613dfdSUlrich Weigand 1905f613dfdSUlrich Weigand // Add the address. 1915f613dfdSUlrich Weigand MIB.addReg(SystemZ::R15D).addImm(StartOffset); 1925f613dfdSUlrich Weigand 1935f613dfdSUlrich Weigand // Make sure all call-saved GPRs are included as operands and are 1945f613dfdSUlrich Weigand // marked as live on entry. 1955f613dfdSUlrich Weigand for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 1965f613dfdSUlrich Weigand unsigned Reg = CSI[I].getReg(); 1975f613dfdSUlrich Weigand if (SystemZ::GR64BitRegClass.contains(Reg)) 198f1bd22dfSEric Christopher addSavedGPR(MBB, MIB, Reg, true); 1995f613dfdSUlrich Weigand } 2005f613dfdSUlrich Weigand 2015f613dfdSUlrich Weigand // ...likewise GPR varargs. 2025f613dfdSUlrich Weigand if (IsVarArg) 2035f613dfdSUlrich Weigand for (unsigned I = ZFI->getVarArgsFirstGPR(); I < SystemZ::NumArgGPRs; ++I) 204f1bd22dfSEric Christopher addSavedGPR(MBB, MIB, SystemZ::ArgGPRs[I], true); 2055f613dfdSUlrich Weigand } 2065f613dfdSUlrich Weigand 2075f613dfdSUlrich Weigand // Save FPRs in the normal TargetInstrInfo way. 2085f613dfdSUlrich Weigand for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 2095f613dfdSUlrich Weigand unsigned Reg = CSI[I].getReg(); 2105f613dfdSUlrich Weigand if (SystemZ::FP64BitRegClass.contains(Reg)) { 2115f613dfdSUlrich Weigand MBB.addLiveIn(Reg); 2125f613dfdSUlrich Weigand TII->storeRegToStackSlot(MBB, MBBI, Reg, true, CSI[I].getFrameIdx(), 2135f613dfdSUlrich Weigand &SystemZ::FP64BitRegClass, TRI); 2145f613dfdSUlrich Weigand } 2155f613dfdSUlrich Weigand } 2165f613dfdSUlrich Weigand 2175f613dfdSUlrich Weigand return true; 2185f613dfdSUlrich Weigand } 2195f613dfdSUlrich Weigand 2205f613dfdSUlrich Weigand bool SystemZFrameLowering:: 2215f613dfdSUlrich Weigand restoreCalleeSavedRegisters(MachineBasicBlock &MBB, 2225f613dfdSUlrich Weigand MachineBasicBlock::iterator MBBI, 223bea30c62SKrzysztof Parzyszek std::vector<CalleeSavedInfo> &CSI, 2245f613dfdSUlrich Weigand const TargetRegisterInfo *TRI) const { 2255f613dfdSUlrich Weigand if (CSI.empty()) 2265f613dfdSUlrich Weigand return false; 2275f613dfdSUlrich Weigand 2285f613dfdSUlrich Weigand MachineFunction &MF = *MBB.getParent(); 229fc6de428SEric Christopher const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); 2305f613dfdSUlrich Weigand SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); 2315f613dfdSUlrich Weigand bool HasFP = hasFP(MF); 2325f613dfdSUlrich Weigand DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 2335f613dfdSUlrich Weigand 2345f613dfdSUlrich Weigand // Restore FPRs in the normal TargetInstrInfo way. 2355f613dfdSUlrich Weigand for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 2365f613dfdSUlrich Weigand unsigned Reg = CSI[I].getReg(); 2375f613dfdSUlrich Weigand if (SystemZ::FP64BitRegClass.contains(Reg)) 2385f613dfdSUlrich Weigand TII->loadRegFromStackSlot(MBB, MBBI, Reg, CSI[I].getFrameIdx(), 2395f613dfdSUlrich Weigand &SystemZ::FP64BitRegClass, TRI); 2405f613dfdSUlrich Weigand } 2415f613dfdSUlrich Weigand 2425f613dfdSUlrich Weigand // Restore call-saved GPRs (but not call-clobbered varargs, which at 2435f613dfdSUlrich Weigand // this point might hold return values). 2445f613dfdSUlrich Weigand unsigned LowGPR = ZFI->getLowSavedGPR(); 2455f613dfdSUlrich Weigand unsigned HighGPR = ZFI->getHighSavedGPR(); 2465f613dfdSUlrich Weigand unsigned StartOffset = RegSpillOffsets[LowGPR]; 2475f613dfdSUlrich Weigand if (LowGPR) { 2485f613dfdSUlrich Weigand // If we saved any of %r2-%r5 as varargs, we should also be saving 2495f613dfdSUlrich Weigand // and restoring %r6. If we're saving %r6 or above, we should be 2505f613dfdSUlrich Weigand // restoring it too. 2515f613dfdSUlrich Weigand assert(LowGPR != HighGPR && "Should be loading %r15 and something else"); 2525f613dfdSUlrich Weigand 2535f613dfdSUlrich Weigand // Build an LMG instruction. 2545f613dfdSUlrich Weigand MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LMG)); 2555f613dfdSUlrich Weigand 2565f613dfdSUlrich Weigand // Add the explicit register operands. 2575f613dfdSUlrich Weigand MIB.addReg(LowGPR, RegState::Define); 2585f613dfdSUlrich Weigand MIB.addReg(HighGPR, RegState::Define); 2595f613dfdSUlrich Weigand 2605f613dfdSUlrich Weigand // Add the address. 2615f613dfdSUlrich Weigand MIB.addReg(HasFP ? SystemZ::R11D : SystemZ::R15D); 2625f613dfdSUlrich Weigand MIB.addImm(StartOffset); 2635f613dfdSUlrich Weigand 2645f613dfdSUlrich Weigand // Do a second scan adding regs as being defined by instruction 2655f613dfdSUlrich Weigand for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 2665f613dfdSUlrich Weigand unsigned Reg = CSI[I].getReg(); 267f0344826SJonas Paulsson if (Reg != LowGPR && Reg != HighGPR && 268f0344826SJonas Paulsson SystemZ::GR64BitRegClass.contains(Reg)) 2695f613dfdSUlrich Weigand MIB.addReg(Reg, RegState::ImplicitDefine); 2705f613dfdSUlrich Weigand } 2715f613dfdSUlrich Weigand } 2725f613dfdSUlrich Weigand 2735f613dfdSUlrich Weigand return true; 2745f613dfdSUlrich Weigand } 2755f613dfdSUlrich Weigand 2765dd52f8cSRichard Sandiford void SystemZFrameLowering:: 2775dd52f8cSRichard Sandiford processFunctionBeforeFrameFinalized(MachineFunction &MF, 2785dd52f8cSRichard Sandiford RegScavenger *RS) const { 279941a705bSMatthias Braun MachineFrameInfo &MFFrame = MF.getFrameInfo(); 280af98b748SUlrich Weigand // Get the size of our stack frame to be allocated ... 281af98b748SUlrich Weigand uint64_t StackSize = (MFFrame.estimateStackSize(MF) + 282af98b748SUlrich Weigand SystemZMC::CallFrameSize); 283af98b748SUlrich Weigand // ... and the maximum offset we may need to reach into the 284af98b748SUlrich Weigand // caller's frame to access the save area or stack arguments. 285af98b748SUlrich Weigand int64_t MaxArgOffset = SystemZMC::CallFrameSize; 286af98b748SUlrich Weigand for (int I = MFFrame.getObjectIndexBegin(); I != 0; ++I) 287af98b748SUlrich Weigand if (MFFrame.getObjectOffset(I) >= 0) { 288af98b748SUlrich Weigand int64_t ArgOffset = SystemZMC::CallFrameSize + 289af98b748SUlrich Weigand MFFrame.getObjectOffset(I) + 290af98b748SUlrich Weigand MFFrame.getObjectSize(I); 291af98b748SUlrich Weigand MaxArgOffset = std::max(MaxArgOffset, ArgOffset); 292af98b748SUlrich Weigand } 293af98b748SUlrich Weigand 294af98b748SUlrich Weigand uint64_t MaxReach = StackSize + MaxArgOffset; 29523943229SRichard Sandiford if (!isUInt<12>(MaxReach)) { 29623943229SRichard Sandiford // We may need register scavenging slots if some parts of the frame 2975dd52f8cSRichard Sandiford // are outside the reach of an unsigned 12-bit displacement. 29823943229SRichard Sandiford // Create 2 for the case where both addresses in an MVC are 29923943229SRichard Sandiford // out of range. 300941a705bSMatthias Braun RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, 8, false)); 301941a705bSMatthias Braun RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, 8, false)); 30223943229SRichard Sandiford } 3035dd52f8cSRichard Sandiford } 3045dd52f8cSRichard Sandiford 3055f613dfdSUlrich Weigand // Emit instructions before MBBI (in MBB) to add NumBytes to Reg. 3065f613dfdSUlrich Weigand static void emitIncrement(MachineBasicBlock &MBB, 3075f613dfdSUlrich Weigand MachineBasicBlock::iterator &MBBI, 3085f613dfdSUlrich Weigand const DebugLoc &DL, 3095f613dfdSUlrich Weigand unsigned Reg, int64_t NumBytes, 3105f613dfdSUlrich Weigand const TargetInstrInfo *TII) { 3115f613dfdSUlrich Weigand while (NumBytes) { 3125f613dfdSUlrich Weigand unsigned Opcode; 3135f613dfdSUlrich Weigand int64_t ThisVal = NumBytes; 3145f613dfdSUlrich Weigand if (isInt<16>(NumBytes)) 3155f613dfdSUlrich Weigand Opcode = SystemZ::AGHI; 3165f613dfdSUlrich Weigand else { 3175f613dfdSUlrich Weigand Opcode = SystemZ::AGFI; 3185f613dfdSUlrich Weigand // Make sure we maintain 8-byte stack alignment. 319fffd56ecSAlexey Samsonov int64_t MinVal = -uint64_t(1) << 31; 3205f613dfdSUlrich Weigand int64_t MaxVal = (int64_t(1) << 31) - 8; 3215f613dfdSUlrich Weigand if (ThisVal < MinVal) 3225f613dfdSUlrich Weigand ThisVal = MinVal; 3235f613dfdSUlrich Weigand else if (ThisVal > MaxVal) 3245f613dfdSUlrich Weigand ThisVal = MaxVal; 3255f613dfdSUlrich Weigand } 3265f613dfdSUlrich Weigand MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII->get(Opcode), Reg) 3275f613dfdSUlrich Weigand .addReg(Reg).addImm(ThisVal); 32814a44495SRichard Sandiford // The CC implicit def is dead. 3295f613dfdSUlrich Weigand MI->getOperand(3).setIsDead(); 3305f613dfdSUlrich Weigand NumBytes -= ThisVal; 3315f613dfdSUlrich Weigand } 3325f613dfdSUlrich Weigand } 3335f613dfdSUlrich Weigand 33461b305edSQuentin Colombet void SystemZFrameLowering::emitPrologue(MachineFunction &MF, 33561b305edSQuentin Colombet MachineBasicBlock &MBB) const { 33661b305edSQuentin Colombet assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported"); 337941a705bSMatthias Braun MachineFrameInfo &MFFrame = MF.getFrameInfo(); 338fc6de428SEric Christopher auto *ZII = 339fc6de428SEric Christopher static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo()); 3405f613dfdSUlrich Weigand SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); 3415f613dfdSUlrich Weigand MachineBasicBlock::iterator MBBI = MBB.begin(); 3425f613dfdSUlrich Weigand MachineModuleInfo &MMI = MF.getMMI(); 343bc07a890SBill Wendling const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo(); 344941a705bSMatthias Braun const std::vector<CalleeSavedInfo> &CSI = MFFrame.getCalleeSavedInfo(); 3455f613dfdSUlrich Weigand bool HasFP = hasFP(MF); 346057c5a6bSOleg Ranevskyy 347057c5a6bSOleg Ranevskyy // Debug location must be unknown since the first debug location is used 348057c5a6bSOleg Ranevskyy // to determine the end of the prologue. 349057c5a6bSOleg Ranevskyy DebugLoc DL; 3505f613dfdSUlrich Weigand 3515f613dfdSUlrich Weigand // The current offset of the stack pointer from the CFA. 3525f613dfdSUlrich Weigand int64_t SPOffsetFromCFA = -SystemZMC::CFAOffsetFromInitialSP; 3535f613dfdSUlrich Weigand 3545f613dfdSUlrich Weigand if (ZFI->getLowSavedGPR()) { 3555f613dfdSUlrich Weigand // Skip over the GPR saves. 3565f613dfdSUlrich Weigand if (MBBI != MBB.end() && MBBI->getOpcode() == SystemZ::STMG) 3575f613dfdSUlrich Weigand ++MBBI; 3585f613dfdSUlrich Weigand else 3595f613dfdSUlrich Weigand llvm_unreachable("Couldn't skip over GPR saves"); 3605f613dfdSUlrich Weigand 3615f613dfdSUlrich Weigand // Add CFI for the GPR saves. 36228c111ecSRichard Sandiford for (auto &Save : CSI) { 36328c111ecSRichard Sandiford unsigned Reg = Save.getReg(); 3645f613dfdSUlrich Weigand if (SystemZ::GR64BitRegClass.contains(Reg)) { 3655f613dfdSUlrich Weigand int64_t Offset = SPOffsetFromCFA + RegSpillOffsets[Reg]; 366f23ef437SMatthias Braun unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset( 367b1f25f1bSRafael Espindola nullptr, MRI->getDwarfRegNum(Reg, true), Offset)); 368b1f25f1bSRafael Espindola BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) 369b1f25f1bSRafael Espindola .addCFIIndex(CFIIndex); 3705f613dfdSUlrich Weigand } 3715f613dfdSUlrich Weigand } 3725f613dfdSUlrich Weigand } 3735f613dfdSUlrich Weigand 3745f613dfdSUlrich Weigand uint64_t StackSize = getAllocatedStackSize(MF); 3755f613dfdSUlrich Weigand if (StackSize) { 376ad1482c6SMarcin Koscielnicki // Determine if we want to store a backchain. 377*f1caa283SMatthias Braun bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain"); 378ad1482c6SMarcin Koscielnicki 379ad1482c6SMarcin Koscielnicki // If we need backchain, save current stack pointer. R1 is free at this 380ad1482c6SMarcin Koscielnicki // point. 381ad1482c6SMarcin Koscielnicki if (StoreBackchain) 382ad1482c6SMarcin Koscielnicki BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR)) 383ad1482c6SMarcin Koscielnicki .addReg(SystemZ::R1D, RegState::Define).addReg(SystemZ::R15D); 384ad1482c6SMarcin Koscielnicki 3855f613dfdSUlrich Weigand // Allocate StackSize bytes. 3865f613dfdSUlrich Weigand int64_t Delta = -int64_t(StackSize); 3875f613dfdSUlrich Weigand emitIncrement(MBB, MBBI, DL, SystemZ::R15D, Delta, ZII); 3885f613dfdSUlrich Weigand 3895f613dfdSUlrich Weigand // Add CFI for the allocation. 390f23ef437SMatthias Braun unsigned CFIIndex = MF.addFrameInst( 391b1f25f1bSRafael Espindola MCCFIInstruction::createDefCfaOffset(nullptr, SPOffsetFromCFA + Delta)); 392b1f25f1bSRafael Espindola BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) 393b1f25f1bSRafael Espindola .addCFIIndex(CFIIndex); 3945f613dfdSUlrich Weigand SPOffsetFromCFA += Delta; 395ad1482c6SMarcin Koscielnicki 396ad1482c6SMarcin Koscielnicki if (StoreBackchain) 397ad1482c6SMarcin Koscielnicki BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::STG)) 398ad1482c6SMarcin Koscielnicki .addReg(SystemZ::R1D, RegState::Kill).addReg(SystemZ::R15D).addImm(0).addReg(0); 3995f613dfdSUlrich Weigand } 4005f613dfdSUlrich Weigand 4015f613dfdSUlrich Weigand if (HasFP) { 4025f613dfdSUlrich Weigand // Copy the base of the frame to R11. 4035f613dfdSUlrich Weigand BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR), SystemZ::R11D) 4045f613dfdSUlrich Weigand .addReg(SystemZ::R15D); 4055f613dfdSUlrich Weigand 4065f613dfdSUlrich Weigand // Add CFI for the new frame location. 407bc07a890SBill Wendling unsigned HardFP = MRI->getDwarfRegNum(SystemZ::R11D, true); 408f23ef437SMatthias Braun unsigned CFIIndex = MF.addFrameInst( 409b1f25f1bSRafael Espindola MCCFIInstruction::createDefCfaRegister(nullptr, HardFP)); 410b1f25f1bSRafael Espindola BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) 411b1f25f1bSRafael Espindola .addCFIIndex(CFIIndex); 4125f613dfdSUlrich Weigand 4135f613dfdSUlrich Weigand // Mark the FramePtr as live at the beginning of every block except 4145f613dfdSUlrich Weigand // the entry block. (We'll have marked R11 as live on entry when 4155f613dfdSUlrich Weigand // saving the GPRs.) 41628c111ecSRichard Sandiford for (auto I = std::next(MF.begin()), E = MF.end(); I != E; ++I) 4175f613dfdSUlrich Weigand I->addLiveIn(SystemZ::R11D); 4185f613dfdSUlrich Weigand } 4195f613dfdSUlrich Weigand 4205f613dfdSUlrich Weigand // Skip over the FPR saves. 421b1f25f1bSRafael Espindola SmallVector<unsigned, 8> CFIIndexes; 42228c111ecSRichard Sandiford for (auto &Save : CSI) { 42328c111ecSRichard Sandiford unsigned Reg = Save.getReg(); 4245f613dfdSUlrich Weigand if (SystemZ::FP64BitRegClass.contains(Reg)) { 4255f613dfdSUlrich Weigand if (MBBI != MBB.end() && 4265f613dfdSUlrich Weigand (MBBI->getOpcode() == SystemZ::STD || 4275f613dfdSUlrich Weigand MBBI->getOpcode() == SystemZ::STDY)) 4285f613dfdSUlrich Weigand ++MBBI; 4295f613dfdSUlrich Weigand else 4305f613dfdSUlrich Weigand llvm_unreachable("Couldn't skip over FPR save"); 4315f613dfdSUlrich Weigand 4325f613dfdSUlrich Weigand // Add CFI for the this save. 43328c111ecSRichard Sandiford unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true); 4345567bafeSJames Y Knight unsigned IgnoredFrameReg; 4355567bafeSJames Y Knight int64_t Offset = 4365567bafeSJames Y Knight getFrameIndexReference(MF, Save.getFrameIdx(), IgnoredFrameReg); 4375567bafeSJames Y Knight 438f23ef437SMatthias Braun unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset( 439b1f25f1bSRafael Espindola nullptr, DwarfReg, SPOffsetFromCFA + Offset)); 440b1f25f1bSRafael Espindola CFIIndexes.push_back(CFIIndex); 4415f613dfdSUlrich Weigand } 4425f613dfdSUlrich Weigand } 4435f613dfdSUlrich Weigand // Complete the CFI for the FPR saves, modelling them as taking effect 4445f613dfdSUlrich Weigand // after the last save. 445b1f25f1bSRafael Espindola for (auto CFIIndex : CFIIndexes) { 446b1f25f1bSRafael Espindola BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) 447b1f25f1bSRafael Espindola .addCFIIndex(CFIIndex); 448b1f25f1bSRafael Espindola } 4495f613dfdSUlrich Weigand } 4505f613dfdSUlrich Weigand 4515f613dfdSUlrich Weigand void SystemZFrameLowering::emitEpilogue(MachineFunction &MF, 4525f613dfdSUlrich Weigand MachineBasicBlock &MBB) const { 4535f613dfdSUlrich Weigand MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 454fc6de428SEric Christopher auto *ZII = 455fc6de428SEric Christopher static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo()); 4565f613dfdSUlrich Weigand SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); 4575f613dfdSUlrich Weigand 4585f613dfdSUlrich Weigand // Skip the return instruction. 459709bda66SRichard Sandiford assert(MBBI->isReturn() && "Can only insert epilogue into returning blocks"); 4605f613dfdSUlrich Weigand 4615f613dfdSUlrich Weigand uint64_t StackSize = getAllocatedStackSize(MF); 4625f613dfdSUlrich Weigand if (ZFI->getLowSavedGPR()) { 4635f613dfdSUlrich Weigand --MBBI; 4645f613dfdSUlrich Weigand unsigned Opcode = MBBI->getOpcode(); 4655f613dfdSUlrich Weigand if (Opcode != SystemZ::LMG) 4665f613dfdSUlrich Weigand llvm_unreachable("Expected to see callee-save register restore code"); 4675f613dfdSUlrich Weigand 4685f613dfdSUlrich Weigand unsigned AddrOpNo = 2; 4695f613dfdSUlrich Weigand DebugLoc DL = MBBI->getDebugLoc(); 4705f613dfdSUlrich Weigand uint64_t Offset = StackSize + MBBI->getOperand(AddrOpNo + 1).getImm(); 4715f613dfdSUlrich Weigand unsigned NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset); 4725f613dfdSUlrich Weigand 4735f613dfdSUlrich Weigand // If the offset is too large, use the largest stack-aligned offset 4745f613dfdSUlrich Weigand // and add the rest to the base register (the stack or frame pointer). 4755f613dfdSUlrich Weigand if (!NewOpcode) { 4765f613dfdSUlrich Weigand uint64_t NumBytes = Offset - 0x7fff8; 4775f613dfdSUlrich Weigand emitIncrement(MBB, MBBI, DL, MBBI->getOperand(AddrOpNo).getReg(), 4785f613dfdSUlrich Weigand NumBytes, ZII); 4795f613dfdSUlrich Weigand Offset -= NumBytes; 4805f613dfdSUlrich Weigand NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset); 4815f613dfdSUlrich Weigand assert(NewOpcode && "No restore instruction available"); 4825f613dfdSUlrich Weigand } 4835f613dfdSUlrich Weigand 4845f613dfdSUlrich Weigand MBBI->setDesc(ZII->get(NewOpcode)); 4855f613dfdSUlrich Weigand MBBI->getOperand(AddrOpNo + 1).ChangeToImmediate(Offset); 4865f613dfdSUlrich Weigand } else if (StackSize) { 4875f613dfdSUlrich Weigand DebugLoc DL = MBBI->getDebugLoc(); 4885f613dfdSUlrich Weigand emitIncrement(MBB, MBBI, DL, SystemZ::R15D, StackSize, ZII); 4895f613dfdSUlrich Weigand } 4905f613dfdSUlrich Weigand } 4915f613dfdSUlrich Weigand 4925f613dfdSUlrich Weigand bool SystemZFrameLowering::hasFP(const MachineFunction &MF) const { 4935f613dfdSUlrich Weigand return (MF.getTarget().Options.DisableFramePointerElim(MF) || 494941a705bSMatthias Braun MF.getFrameInfo().hasVarSizedObjects() || 4955f613dfdSUlrich Weigand MF.getInfo<SystemZMachineFunctionInfo>()->getManipulatesSP()); 4965f613dfdSUlrich Weigand } 4975f613dfdSUlrich Weigand 4985567bafeSJames Y Knight int SystemZFrameLowering::getFrameIndexReference(const MachineFunction &MF, 4995567bafeSJames Y Knight int FI, 5005567bafeSJames Y Knight unsigned &FrameReg) const { 501941a705bSMatthias Braun const MachineFrameInfo &MFFrame = MF.getFrameInfo(); 5025567bafeSJames Y Knight const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo(); 5035567bafeSJames Y Knight 5045567bafeSJames Y Knight // Fill in FrameReg output argument. 5055567bafeSJames Y Knight FrameReg = RI->getFrameRegister(MF); 5065f613dfdSUlrich Weigand 5075f613dfdSUlrich Weigand // Start with the offset of FI from the top of the caller-allocated frame 5085f613dfdSUlrich Weigand // (i.e. the top of the 160 bytes allocated by the caller). This initial 5095f613dfdSUlrich Weigand // offset is therefore negative. 510941a705bSMatthias Braun int64_t Offset = (MFFrame.getObjectOffset(FI) + 511941a705bSMatthias Braun MFFrame.getOffsetAdjustment()); 5125f613dfdSUlrich Weigand 5135f613dfdSUlrich Weigand // Make the offset relative to the incoming stack pointer. 5145f613dfdSUlrich Weigand Offset -= getOffsetOfLocalArea(); 5155f613dfdSUlrich Weigand 5165f613dfdSUlrich Weigand // Make the offset relative to the bottom of the frame. 5175f613dfdSUlrich Weigand Offset += getAllocatedStackSize(MF); 5185f613dfdSUlrich Weigand 5195f613dfdSUlrich Weigand return Offset; 5205f613dfdSUlrich Weigand } 5215f613dfdSUlrich Weigand 5225f613dfdSUlrich Weigand uint64_t SystemZFrameLowering:: 5235f613dfdSUlrich Weigand getAllocatedStackSize(const MachineFunction &MF) const { 524941a705bSMatthias Braun const MachineFrameInfo &MFFrame = MF.getFrameInfo(); 5255f613dfdSUlrich Weigand 5265f613dfdSUlrich Weigand // Start with the size of the local variables and spill slots. 527941a705bSMatthias Braun uint64_t StackSize = MFFrame.getStackSize(); 5285f613dfdSUlrich Weigand 5295f613dfdSUlrich Weigand // We need to allocate the ABI-defined 160-byte base area whenever 5305f613dfdSUlrich Weigand // we allocate stack space for our own use and whenever we call another 5315f613dfdSUlrich Weigand // function. 532941a705bSMatthias Braun if (StackSize || MFFrame.hasVarSizedObjects() || MFFrame.hasCalls()) 5335f613dfdSUlrich Weigand StackSize += SystemZMC::CallFrameSize; 5345f613dfdSUlrich Weigand 5355f613dfdSUlrich Weigand return StackSize; 5365f613dfdSUlrich Weigand } 5375f613dfdSUlrich Weigand 5385f613dfdSUlrich Weigand bool 5395f613dfdSUlrich Weigand SystemZFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { 5405f613dfdSUlrich Weigand // The ABI requires us to allocate 160 bytes of stack space for the callee, 5415f613dfdSUlrich Weigand // with any outgoing stack arguments being placed above that. It seems 5425f613dfdSUlrich Weigand // better to make that area a permanent feature of the frame even if 5435f613dfdSUlrich Weigand // we're using a frame pointer. 5445f613dfdSUlrich Weigand return true; 5455f613dfdSUlrich Weigand } 5465f613dfdSUlrich Weigand 547e1a2e90fSHans Wennborg MachineBasicBlock::iterator SystemZFrameLowering:: 5485f613dfdSUlrich Weigand eliminateCallFramePseudoInstr(MachineFunction &MF, 5495f613dfdSUlrich Weigand MachineBasicBlock &MBB, 5505f613dfdSUlrich Weigand MachineBasicBlock::iterator MI) const { 5515f613dfdSUlrich Weigand switch (MI->getOpcode()) { 5525f613dfdSUlrich Weigand case SystemZ::ADJCALLSTACKDOWN: 5535f613dfdSUlrich Weigand case SystemZ::ADJCALLSTACKUP: 5545f613dfdSUlrich Weigand assert(hasReservedCallFrame(MF) && 5555f613dfdSUlrich Weigand "ADJSTACKDOWN and ADJSTACKUP should be no-ops"); 556e1a2e90fSHans Wennborg return MBB.erase(MI); 5575f613dfdSUlrich Weigand break; 5585f613dfdSUlrich Weigand 5595f613dfdSUlrich Weigand default: 5605f613dfdSUlrich Weigand llvm_unreachable("Unexpected call frame instruction"); 5615f613dfdSUlrich Weigand } 5625f613dfdSUlrich Weigand } 563