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>(); 745f613dfdSUlrich Weigand 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. 85*d0ee66c2SMatthias 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>(); 1425f613dfdSUlrich Weigand 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, 2235f613dfdSUlrich Weigand const 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(); 280941a705bSMatthias Braun uint64_t MaxReach = (MFFrame.estimateStackSize(MF) + 2815dd52f8cSRichard Sandiford SystemZMC::CallFrameSize * 2); 28223943229SRichard Sandiford if (!isUInt<12>(MaxReach)) { 28323943229SRichard Sandiford // We may need register scavenging slots if some parts of the frame 2845dd52f8cSRichard Sandiford // are outside the reach of an unsigned 12-bit displacement. 28523943229SRichard Sandiford // Create 2 for the case where both addresses in an MVC are 28623943229SRichard Sandiford // out of range. 287941a705bSMatthias Braun RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, 8, false)); 288941a705bSMatthias Braun RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, 8, false)); 28923943229SRichard Sandiford } 2905dd52f8cSRichard Sandiford } 2915dd52f8cSRichard Sandiford 2925f613dfdSUlrich Weigand // Emit instructions before MBBI (in MBB) to add NumBytes to Reg. 2935f613dfdSUlrich Weigand static void emitIncrement(MachineBasicBlock &MBB, 2945f613dfdSUlrich Weigand MachineBasicBlock::iterator &MBBI, 2955f613dfdSUlrich Weigand const DebugLoc &DL, 2965f613dfdSUlrich Weigand unsigned Reg, int64_t NumBytes, 2975f613dfdSUlrich Weigand const TargetInstrInfo *TII) { 2985f613dfdSUlrich Weigand while (NumBytes) { 2995f613dfdSUlrich Weigand unsigned Opcode; 3005f613dfdSUlrich Weigand int64_t ThisVal = NumBytes; 3015f613dfdSUlrich Weigand if (isInt<16>(NumBytes)) 3025f613dfdSUlrich Weigand Opcode = SystemZ::AGHI; 3035f613dfdSUlrich Weigand else { 3045f613dfdSUlrich Weigand Opcode = SystemZ::AGFI; 3055f613dfdSUlrich Weigand // Make sure we maintain 8-byte stack alignment. 306fffd56ecSAlexey Samsonov int64_t MinVal = -uint64_t(1) << 31; 3075f613dfdSUlrich Weigand int64_t MaxVal = (int64_t(1) << 31) - 8; 3085f613dfdSUlrich Weigand if (ThisVal < MinVal) 3095f613dfdSUlrich Weigand ThisVal = MinVal; 3105f613dfdSUlrich Weigand else if (ThisVal > MaxVal) 3115f613dfdSUlrich Weigand ThisVal = MaxVal; 3125f613dfdSUlrich Weigand } 3135f613dfdSUlrich Weigand MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII->get(Opcode), Reg) 3145f613dfdSUlrich Weigand .addReg(Reg).addImm(ThisVal); 31514a44495SRichard Sandiford // The CC implicit def is dead. 3165f613dfdSUlrich Weigand MI->getOperand(3).setIsDead(); 3175f613dfdSUlrich Weigand NumBytes -= ThisVal; 3185f613dfdSUlrich Weigand } 3195f613dfdSUlrich Weigand } 3205f613dfdSUlrich Weigand 32161b305edSQuentin Colombet void SystemZFrameLowering::emitPrologue(MachineFunction &MF, 32261b305edSQuentin Colombet MachineBasicBlock &MBB) const { 32361b305edSQuentin Colombet assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported"); 324941a705bSMatthias Braun MachineFrameInfo &MFFrame = MF.getFrameInfo(); 325fc6de428SEric Christopher auto *ZII = 326fc6de428SEric Christopher static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo()); 3275f613dfdSUlrich Weigand SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); 3285f613dfdSUlrich Weigand MachineBasicBlock::iterator MBBI = MBB.begin(); 3295f613dfdSUlrich Weigand MachineModuleInfo &MMI = MF.getMMI(); 330bc07a890SBill Wendling const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo(); 331941a705bSMatthias Braun const std::vector<CalleeSavedInfo> &CSI = MFFrame.getCalleeSavedInfo(); 3325f613dfdSUlrich Weigand bool HasFP = hasFP(MF); 333057c5a6bSOleg Ranevskyy 334057c5a6bSOleg Ranevskyy // Debug location must be unknown since the first debug location is used 335057c5a6bSOleg Ranevskyy // to determine the end of the prologue. 336057c5a6bSOleg Ranevskyy DebugLoc DL; 3375f613dfdSUlrich Weigand 3385f613dfdSUlrich Weigand // The current offset of the stack pointer from the CFA. 3395f613dfdSUlrich Weigand int64_t SPOffsetFromCFA = -SystemZMC::CFAOffsetFromInitialSP; 3405f613dfdSUlrich Weigand 3415f613dfdSUlrich Weigand if (ZFI->getLowSavedGPR()) { 3425f613dfdSUlrich Weigand // Skip over the GPR saves. 3435f613dfdSUlrich Weigand if (MBBI != MBB.end() && MBBI->getOpcode() == SystemZ::STMG) 3445f613dfdSUlrich Weigand ++MBBI; 3455f613dfdSUlrich Weigand else 3465f613dfdSUlrich Weigand llvm_unreachable("Couldn't skip over GPR saves"); 3475f613dfdSUlrich Weigand 3485f613dfdSUlrich Weigand // Add CFI for the GPR saves. 34928c111ecSRichard Sandiford for (auto &Save : CSI) { 35028c111ecSRichard Sandiford unsigned Reg = Save.getReg(); 3515f613dfdSUlrich Weigand if (SystemZ::GR64BitRegClass.contains(Reg)) { 3525f613dfdSUlrich Weigand int64_t Offset = SPOffsetFromCFA + RegSpillOffsets[Reg]; 353f23ef437SMatthias Braun unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset( 354b1f25f1bSRafael Espindola nullptr, MRI->getDwarfRegNum(Reg, true), Offset)); 355b1f25f1bSRafael Espindola BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) 356b1f25f1bSRafael Espindola .addCFIIndex(CFIIndex); 3575f613dfdSUlrich Weigand } 3585f613dfdSUlrich Weigand } 3595f613dfdSUlrich Weigand } 3605f613dfdSUlrich Weigand 3615f613dfdSUlrich Weigand uint64_t StackSize = getAllocatedStackSize(MF); 3625f613dfdSUlrich Weigand if (StackSize) { 363ad1482c6SMarcin Koscielnicki // Determine if we want to store a backchain. 364ad1482c6SMarcin Koscielnicki bool StoreBackchain = MF.getFunction()->hasFnAttribute("backchain"); 365ad1482c6SMarcin Koscielnicki 366ad1482c6SMarcin Koscielnicki // If we need backchain, save current stack pointer. R1 is free at this 367ad1482c6SMarcin Koscielnicki // point. 368ad1482c6SMarcin Koscielnicki if (StoreBackchain) 369ad1482c6SMarcin Koscielnicki BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR)) 370ad1482c6SMarcin Koscielnicki .addReg(SystemZ::R1D, RegState::Define).addReg(SystemZ::R15D); 371ad1482c6SMarcin Koscielnicki 3725f613dfdSUlrich Weigand // Allocate StackSize bytes. 3735f613dfdSUlrich Weigand int64_t Delta = -int64_t(StackSize); 3745f613dfdSUlrich Weigand emitIncrement(MBB, MBBI, DL, SystemZ::R15D, Delta, ZII); 3755f613dfdSUlrich Weigand 3765f613dfdSUlrich Weigand // Add CFI for the allocation. 377f23ef437SMatthias Braun unsigned CFIIndex = MF.addFrameInst( 378b1f25f1bSRafael Espindola MCCFIInstruction::createDefCfaOffset(nullptr, SPOffsetFromCFA + Delta)); 379b1f25f1bSRafael Espindola BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) 380b1f25f1bSRafael Espindola .addCFIIndex(CFIIndex); 3815f613dfdSUlrich Weigand SPOffsetFromCFA += Delta; 382ad1482c6SMarcin Koscielnicki 383ad1482c6SMarcin Koscielnicki if (StoreBackchain) 384ad1482c6SMarcin Koscielnicki BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::STG)) 385ad1482c6SMarcin Koscielnicki .addReg(SystemZ::R1D, RegState::Kill).addReg(SystemZ::R15D).addImm(0).addReg(0); 3865f613dfdSUlrich Weigand } 3875f613dfdSUlrich Weigand 3885f613dfdSUlrich Weigand if (HasFP) { 3895f613dfdSUlrich Weigand // Copy the base of the frame to R11. 3905f613dfdSUlrich Weigand BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR), SystemZ::R11D) 3915f613dfdSUlrich Weigand .addReg(SystemZ::R15D); 3925f613dfdSUlrich Weigand 3935f613dfdSUlrich Weigand // Add CFI for the new frame location. 394bc07a890SBill Wendling unsigned HardFP = MRI->getDwarfRegNum(SystemZ::R11D, true); 395f23ef437SMatthias Braun unsigned CFIIndex = MF.addFrameInst( 396b1f25f1bSRafael Espindola MCCFIInstruction::createDefCfaRegister(nullptr, HardFP)); 397b1f25f1bSRafael Espindola BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) 398b1f25f1bSRafael Espindola .addCFIIndex(CFIIndex); 3995f613dfdSUlrich Weigand 4005f613dfdSUlrich Weigand // Mark the FramePtr as live at the beginning of every block except 4015f613dfdSUlrich Weigand // the entry block. (We'll have marked R11 as live on entry when 4025f613dfdSUlrich Weigand // saving the GPRs.) 40328c111ecSRichard Sandiford for (auto I = std::next(MF.begin()), E = MF.end(); I != E; ++I) 4045f613dfdSUlrich Weigand I->addLiveIn(SystemZ::R11D); 4055f613dfdSUlrich Weigand } 4065f613dfdSUlrich Weigand 4075f613dfdSUlrich Weigand // Skip over the FPR saves. 408b1f25f1bSRafael Espindola SmallVector<unsigned, 8> CFIIndexes; 40928c111ecSRichard Sandiford for (auto &Save : CSI) { 41028c111ecSRichard Sandiford unsigned Reg = Save.getReg(); 4115f613dfdSUlrich Weigand if (SystemZ::FP64BitRegClass.contains(Reg)) { 4125f613dfdSUlrich Weigand if (MBBI != MBB.end() && 4135f613dfdSUlrich Weigand (MBBI->getOpcode() == SystemZ::STD || 4145f613dfdSUlrich Weigand MBBI->getOpcode() == SystemZ::STDY)) 4155f613dfdSUlrich Weigand ++MBBI; 4165f613dfdSUlrich Weigand else 4175f613dfdSUlrich Weigand llvm_unreachable("Couldn't skip over FPR save"); 4185f613dfdSUlrich Weigand 4195f613dfdSUlrich Weigand // Add CFI for the this save. 42028c111ecSRichard Sandiford unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true); 4215567bafeSJames Y Knight unsigned IgnoredFrameReg; 4225567bafeSJames Y Knight int64_t Offset = 4235567bafeSJames Y Knight getFrameIndexReference(MF, Save.getFrameIdx(), IgnoredFrameReg); 4245567bafeSJames Y Knight 425f23ef437SMatthias Braun unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset( 426b1f25f1bSRafael Espindola nullptr, DwarfReg, SPOffsetFromCFA + Offset)); 427b1f25f1bSRafael Espindola CFIIndexes.push_back(CFIIndex); 4285f613dfdSUlrich Weigand } 4295f613dfdSUlrich Weigand } 4305f613dfdSUlrich Weigand // Complete the CFI for the FPR saves, modelling them as taking effect 4315f613dfdSUlrich Weigand // after the last save. 432b1f25f1bSRafael Espindola for (auto CFIIndex : CFIIndexes) { 433b1f25f1bSRafael Espindola BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) 434b1f25f1bSRafael Espindola .addCFIIndex(CFIIndex); 435b1f25f1bSRafael Espindola } 4365f613dfdSUlrich Weigand } 4375f613dfdSUlrich Weigand 4385f613dfdSUlrich Weigand void SystemZFrameLowering::emitEpilogue(MachineFunction &MF, 4395f613dfdSUlrich Weigand MachineBasicBlock &MBB) const { 4405f613dfdSUlrich Weigand MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 441fc6de428SEric Christopher auto *ZII = 442fc6de428SEric Christopher static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo()); 4435f613dfdSUlrich Weigand SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); 4445f613dfdSUlrich Weigand 4455f613dfdSUlrich Weigand // Skip the return instruction. 446709bda66SRichard Sandiford assert(MBBI->isReturn() && "Can only insert epilogue into returning blocks"); 4475f613dfdSUlrich Weigand 4485f613dfdSUlrich Weigand uint64_t StackSize = getAllocatedStackSize(MF); 4495f613dfdSUlrich Weigand if (ZFI->getLowSavedGPR()) { 4505f613dfdSUlrich Weigand --MBBI; 4515f613dfdSUlrich Weigand unsigned Opcode = MBBI->getOpcode(); 4525f613dfdSUlrich Weigand if (Opcode != SystemZ::LMG) 4535f613dfdSUlrich Weigand llvm_unreachable("Expected to see callee-save register restore code"); 4545f613dfdSUlrich Weigand 4555f613dfdSUlrich Weigand unsigned AddrOpNo = 2; 4565f613dfdSUlrich Weigand DebugLoc DL = MBBI->getDebugLoc(); 4575f613dfdSUlrich Weigand uint64_t Offset = StackSize + MBBI->getOperand(AddrOpNo + 1).getImm(); 4585f613dfdSUlrich Weigand unsigned NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset); 4595f613dfdSUlrich Weigand 4605f613dfdSUlrich Weigand // If the offset is too large, use the largest stack-aligned offset 4615f613dfdSUlrich Weigand // and add the rest to the base register (the stack or frame pointer). 4625f613dfdSUlrich Weigand if (!NewOpcode) { 4635f613dfdSUlrich Weigand uint64_t NumBytes = Offset - 0x7fff8; 4645f613dfdSUlrich Weigand emitIncrement(MBB, MBBI, DL, MBBI->getOperand(AddrOpNo).getReg(), 4655f613dfdSUlrich Weigand NumBytes, ZII); 4665f613dfdSUlrich Weigand Offset -= NumBytes; 4675f613dfdSUlrich Weigand NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset); 4685f613dfdSUlrich Weigand assert(NewOpcode && "No restore instruction available"); 4695f613dfdSUlrich Weigand } 4705f613dfdSUlrich Weigand 4715f613dfdSUlrich Weigand MBBI->setDesc(ZII->get(NewOpcode)); 4725f613dfdSUlrich Weigand MBBI->getOperand(AddrOpNo + 1).ChangeToImmediate(Offset); 4735f613dfdSUlrich Weigand } else if (StackSize) { 4745f613dfdSUlrich Weigand DebugLoc DL = MBBI->getDebugLoc(); 4755f613dfdSUlrich Weigand emitIncrement(MBB, MBBI, DL, SystemZ::R15D, StackSize, ZII); 4765f613dfdSUlrich Weigand } 4775f613dfdSUlrich Weigand } 4785f613dfdSUlrich Weigand 4795f613dfdSUlrich Weigand bool SystemZFrameLowering::hasFP(const MachineFunction &MF) const { 4805f613dfdSUlrich Weigand return (MF.getTarget().Options.DisableFramePointerElim(MF) || 481941a705bSMatthias Braun MF.getFrameInfo().hasVarSizedObjects() || 4825f613dfdSUlrich Weigand MF.getInfo<SystemZMachineFunctionInfo>()->getManipulatesSP()); 4835f613dfdSUlrich Weigand } 4845f613dfdSUlrich Weigand 4855567bafeSJames Y Knight int SystemZFrameLowering::getFrameIndexReference(const MachineFunction &MF, 4865567bafeSJames Y Knight int FI, 4875567bafeSJames Y Knight unsigned &FrameReg) const { 488941a705bSMatthias Braun const MachineFrameInfo &MFFrame = MF.getFrameInfo(); 4895567bafeSJames Y Knight const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo(); 4905567bafeSJames Y Knight 4915567bafeSJames Y Knight // Fill in FrameReg output argument. 4925567bafeSJames Y Knight FrameReg = RI->getFrameRegister(MF); 4935f613dfdSUlrich Weigand 4945f613dfdSUlrich Weigand // Start with the offset of FI from the top of the caller-allocated frame 4955f613dfdSUlrich Weigand // (i.e. the top of the 160 bytes allocated by the caller). This initial 4965f613dfdSUlrich Weigand // offset is therefore negative. 497941a705bSMatthias Braun int64_t Offset = (MFFrame.getObjectOffset(FI) + 498941a705bSMatthias Braun MFFrame.getOffsetAdjustment()); 4995f613dfdSUlrich Weigand 5005f613dfdSUlrich Weigand // Make the offset relative to the incoming stack pointer. 5015f613dfdSUlrich Weigand Offset -= getOffsetOfLocalArea(); 5025f613dfdSUlrich Weigand 5035f613dfdSUlrich Weigand // Make the offset relative to the bottom of the frame. 5045f613dfdSUlrich Weigand Offset += getAllocatedStackSize(MF); 5055f613dfdSUlrich Weigand 5065f613dfdSUlrich Weigand return Offset; 5075f613dfdSUlrich Weigand } 5085f613dfdSUlrich Weigand 5095f613dfdSUlrich Weigand uint64_t SystemZFrameLowering:: 5105f613dfdSUlrich Weigand getAllocatedStackSize(const MachineFunction &MF) const { 511941a705bSMatthias Braun const MachineFrameInfo &MFFrame = MF.getFrameInfo(); 5125f613dfdSUlrich Weigand 5135f613dfdSUlrich Weigand // Start with the size of the local variables and spill slots. 514941a705bSMatthias Braun uint64_t StackSize = MFFrame.getStackSize(); 5155f613dfdSUlrich Weigand 5165f613dfdSUlrich Weigand // We need to allocate the ABI-defined 160-byte base area whenever 5175f613dfdSUlrich Weigand // we allocate stack space for our own use and whenever we call another 5185f613dfdSUlrich Weigand // function. 519941a705bSMatthias Braun if (StackSize || MFFrame.hasVarSizedObjects() || MFFrame.hasCalls()) 5205f613dfdSUlrich Weigand StackSize += SystemZMC::CallFrameSize; 5215f613dfdSUlrich Weigand 5225f613dfdSUlrich Weigand return StackSize; 5235f613dfdSUlrich Weigand } 5245f613dfdSUlrich Weigand 5255f613dfdSUlrich Weigand bool 5265f613dfdSUlrich Weigand SystemZFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { 5275f613dfdSUlrich Weigand // The ABI requires us to allocate 160 bytes of stack space for the callee, 5285f613dfdSUlrich Weigand // with any outgoing stack arguments being placed above that. It seems 5295f613dfdSUlrich Weigand // better to make that area a permanent feature of the frame even if 5305f613dfdSUlrich Weigand // we're using a frame pointer. 5315f613dfdSUlrich Weigand return true; 5325f613dfdSUlrich Weigand } 5335f613dfdSUlrich Weigand 534e1a2e90fSHans Wennborg MachineBasicBlock::iterator SystemZFrameLowering:: 5355f613dfdSUlrich Weigand eliminateCallFramePseudoInstr(MachineFunction &MF, 5365f613dfdSUlrich Weigand MachineBasicBlock &MBB, 5375f613dfdSUlrich Weigand MachineBasicBlock::iterator MI) const { 5385f613dfdSUlrich Weigand switch (MI->getOpcode()) { 5395f613dfdSUlrich Weigand case SystemZ::ADJCALLSTACKDOWN: 5405f613dfdSUlrich Weigand case SystemZ::ADJCALLSTACKUP: 5415f613dfdSUlrich Weigand assert(hasReservedCallFrame(MF) && 5425f613dfdSUlrich Weigand "ADJSTACKDOWN and ADJSTACKUP should be no-ops"); 543e1a2e90fSHans Wennborg return MBB.erase(MI); 5445f613dfdSUlrich Weigand break; 5455f613dfdSUlrich Weigand 5465f613dfdSUlrich Weigand default: 5475f613dfdSUlrich Weigand llvm_unreachable("Unexpected call frame instruction"); 5485f613dfdSUlrich Weigand } 5495f613dfdSUlrich Weigand } 550