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, 51f1bd22dfSEric Christopher -SystemZMC::CallFrameSize, 8) { 525f613dfdSUlrich Weigand // Create a mapping from register number to save slot offset. 535f613dfdSUlrich Weigand RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS); 545f613dfdSUlrich Weigand for (unsigned I = 0, E = array_lengthof(SpillOffsetTable); I != E; ++I) 55db39b4a2SRichard Sandiford RegSpillOffsets[SpillOffsetTable[I].Reg] = SpillOffsetTable[I].Offset; 56db39b4a2SRichard Sandiford } 57db39b4a2SRichard Sandiford 58db39b4a2SRichard Sandiford const TargetFrameLowering::SpillSlot * 59db39b4a2SRichard Sandiford SystemZFrameLowering::getCalleeSavedSpillSlots(unsigned &NumEntries) const { 60db39b4a2SRichard Sandiford NumEntries = array_lengthof(SpillOffsetTable); 61db39b4a2SRichard Sandiford return SpillOffsetTable; 625f613dfdSUlrich Weigand } 635f613dfdSUlrich Weigand 645f613dfdSUlrich Weigand void SystemZFrameLowering:: 655f613dfdSUlrich Weigand processFunctionBeforeCalleeSavedScan(MachineFunction &MF, 665f613dfdSUlrich Weigand RegScavenger *RS) const { 675f613dfdSUlrich Weigand MachineFrameInfo *MFFrame = MF.getFrameInfo(); 685f613dfdSUlrich Weigand MachineRegisterInfo &MRI = MF.getRegInfo(); 69fc6de428SEric Christopher const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 705f613dfdSUlrich Weigand bool HasFP = hasFP(MF); 715f613dfdSUlrich Weigand SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>(); 725f613dfdSUlrich Weigand bool IsVarArg = MF.getFunction()->isVarArg(); 735f613dfdSUlrich Weigand 745f613dfdSUlrich Weigand // va_start stores incoming FPR varargs in the normal way, but delegates 755f613dfdSUlrich Weigand // the saving of incoming GPR varargs to spillCalleeSavedRegisters(). 765f613dfdSUlrich Weigand // Record these pending uses, which typically include the call-saved 775f613dfdSUlrich Weigand // argument register R6D. 785f613dfdSUlrich Weigand if (IsVarArg) 795f613dfdSUlrich Weigand for (unsigned I = MFI->getVarArgsFirstGPR(); I < SystemZ::NumArgGPRs; ++I) 805f613dfdSUlrich Weigand MRI.setPhysRegUsed(SystemZ::ArgGPRs[I]); 815f613dfdSUlrich Weigand 825f613dfdSUlrich Weigand // If the function requires a frame pointer, record that the hard 835f613dfdSUlrich Weigand // frame pointer will be clobbered. 845f613dfdSUlrich Weigand if (HasFP) 855f613dfdSUlrich Weigand MRI.setPhysRegUsed(SystemZ::R11D); 865f613dfdSUlrich Weigand 875f613dfdSUlrich Weigand // If the function calls other functions, record that the return 885f613dfdSUlrich Weigand // address register will be clobbered. 895f613dfdSUlrich Weigand if (MFFrame->hasCalls()) 905f613dfdSUlrich Weigand MRI.setPhysRegUsed(SystemZ::R14D); 915f613dfdSUlrich Weigand 925f613dfdSUlrich Weigand // If we are saving GPRs other than the stack pointer, we might as well 935f613dfdSUlrich Weigand // save and restore the stack pointer at the same time, via STMG and LMG. 945f613dfdSUlrich Weigand // This allows the deallocation to be done by the LMG, rather than needing 955f613dfdSUlrich Weigand // a separate %r15 addition. 96840beec2SCraig Topper const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF); 975f613dfdSUlrich Weigand for (unsigned I = 0; CSRegs[I]; ++I) { 985f613dfdSUlrich Weigand unsigned Reg = CSRegs[I]; 995f613dfdSUlrich Weigand if (SystemZ::GR64BitRegClass.contains(Reg) && MRI.isPhysRegUsed(Reg)) { 1005f613dfdSUlrich Weigand MRI.setPhysRegUsed(SystemZ::R15D); 1015f613dfdSUlrich Weigand break; 1025f613dfdSUlrich Weigand } 1035f613dfdSUlrich Weigand } 1045f613dfdSUlrich Weigand } 1055f613dfdSUlrich Weigand 1065f613dfdSUlrich Weigand // Add GPR64 to the save instruction being built by MIB, which is in basic 1075f613dfdSUlrich Weigand // block MBB. IsImplicit says whether this is an explicit operand to the 1085f613dfdSUlrich Weigand // instruction, or an implicit one that comes between the explicit start 1095f613dfdSUlrich Weigand // and end registers. 1105f613dfdSUlrich Weigand static void addSavedGPR(MachineBasicBlock &MBB, MachineInstrBuilder &MIB, 1115f613dfdSUlrich Weigand unsigned GPR64, bool IsImplicit) { 112d913448bSEric Christopher const TargetRegisterInfo *RI = 113fc6de428SEric Christopher MBB.getParent()->getSubtarget().getRegisterInfo(); 11487a44364SRichard Sandiford unsigned GPR32 = RI->getSubReg(GPR64, SystemZ::subreg_l32); 1155f613dfdSUlrich Weigand bool IsLive = MBB.isLiveIn(GPR64) || MBB.isLiveIn(GPR32); 1165f613dfdSUlrich Weigand if (!IsLive || !IsImplicit) { 1175f613dfdSUlrich Weigand MIB.addReg(GPR64, getImplRegState(IsImplicit) | getKillRegState(!IsLive)); 1185f613dfdSUlrich Weigand if (!IsLive) 1195f613dfdSUlrich Weigand MBB.addLiveIn(GPR64); 1205f613dfdSUlrich Weigand } 1215f613dfdSUlrich Weigand } 1225f613dfdSUlrich Weigand 1235f613dfdSUlrich Weigand bool SystemZFrameLowering:: 1245f613dfdSUlrich Weigand spillCalleeSavedRegisters(MachineBasicBlock &MBB, 1255f613dfdSUlrich Weigand MachineBasicBlock::iterator MBBI, 1265f613dfdSUlrich Weigand const std::vector<CalleeSavedInfo> &CSI, 1275f613dfdSUlrich Weigand const TargetRegisterInfo *TRI) const { 1285f613dfdSUlrich Weigand if (CSI.empty()) 1295f613dfdSUlrich Weigand return false; 1305f613dfdSUlrich Weigand 1315f613dfdSUlrich Weigand MachineFunction &MF = *MBB.getParent(); 132fc6de428SEric Christopher const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); 1335f613dfdSUlrich Weigand SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); 1345f613dfdSUlrich Weigand bool IsVarArg = MF.getFunction()->isVarArg(); 1355f613dfdSUlrich Weigand DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 1365f613dfdSUlrich Weigand 1375f613dfdSUlrich Weigand // Scan the call-saved GPRs and find the bounds of the register spill area. 1385f613dfdSUlrich Weigand unsigned LowGPR = 0; 1395f613dfdSUlrich Weigand unsigned HighGPR = SystemZ::R15D; 1405f613dfdSUlrich Weigand unsigned StartOffset = -1U; 1415f613dfdSUlrich Weigand for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 1425f613dfdSUlrich Weigand unsigned Reg = CSI[I].getReg(); 1435f613dfdSUlrich Weigand if (SystemZ::GR64BitRegClass.contains(Reg)) { 1445f613dfdSUlrich Weigand unsigned Offset = RegSpillOffsets[Reg]; 1455f613dfdSUlrich Weigand assert(Offset && "Unexpected GPR save"); 1465f613dfdSUlrich Weigand if (StartOffset > Offset) { 1475f613dfdSUlrich Weigand LowGPR = Reg; 1485f613dfdSUlrich Weigand StartOffset = Offset; 1495f613dfdSUlrich Weigand } 1505f613dfdSUlrich Weigand } 1515f613dfdSUlrich Weigand } 1525f613dfdSUlrich Weigand 153db39b4a2SRichard Sandiford // Save the range of call-saved registers, for use by the epilogue inserter. 1545f613dfdSUlrich Weigand ZFI->setLowSavedGPR(LowGPR); 1555f613dfdSUlrich Weigand ZFI->setHighSavedGPR(HighGPR); 1565f613dfdSUlrich Weigand 1575f613dfdSUlrich Weigand // Include the GPR varargs, if any. R6D is call-saved, so would 1585f613dfdSUlrich Weigand // be included by the loop above, but we also need to handle the 1595f613dfdSUlrich Weigand // call-clobbered argument registers. 1605f613dfdSUlrich Weigand if (IsVarArg) { 1615f613dfdSUlrich Weigand unsigned FirstGPR = ZFI->getVarArgsFirstGPR(); 1625f613dfdSUlrich Weigand if (FirstGPR < SystemZ::NumArgGPRs) { 1635f613dfdSUlrich Weigand unsigned Reg = SystemZ::ArgGPRs[FirstGPR]; 1645f613dfdSUlrich Weigand unsigned Offset = RegSpillOffsets[Reg]; 1655f613dfdSUlrich Weigand if (StartOffset > Offset) { 1665f613dfdSUlrich Weigand LowGPR = Reg; StartOffset = Offset; 1675f613dfdSUlrich Weigand } 1685f613dfdSUlrich Weigand } 1695f613dfdSUlrich Weigand } 1705f613dfdSUlrich Weigand 1715f613dfdSUlrich Weigand // Save GPRs 1725f613dfdSUlrich Weigand if (LowGPR) { 1735f613dfdSUlrich Weigand assert(LowGPR != HighGPR && "Should be saving %r15 and something else"); 1745f613dfdSUlrich Weigand 1755f613dfdSUlrich Weigand // Build an STMG instruction. 1765f613dfdSUlrich Weigand MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STMG)); 1775f613dfdSUlrich Weigand 1785f613dfdSUlrich Weigand // Add the explicit register operands. 179f1bd22dfSEric Christopher addSavedGPR(MBB, MIB, LowGPR, false); 180f1bd22dfSEric Christopher addSavedGPR(MBB, MIB, HighGPR, false); 1815f613dfdSUlrich Weigand 1825f613dfdSUlrich Weigand // Add the address. 1835f613dfdSUlrich Weigand MIB.addReg(SystemZ::R15D).addImm(StartOffset); 1845f613dfdSUlrich Weigand 1855f613dfdSUlrich Weigand // Make sure all call-saved GPRs are included as operands and are 1865f613dfdSUlrich Weigand // marked as live on entry. 1875f613dfdSUlrich Weigand for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 1885f613dfdSUlrich Weigand unsigned Reg = CSI[I].getReg(); 1895f613dfdSUlrich Weigand if (SystemZ::GR64BitRegClass.contains(Reg)) 190f1bd22dfSEric Christopher addSavedGPR(MBB, MIB, Reg, true); 1915f613dfdSUlrich Weigand } 1925f613dfdSUlrich Weigand 1935f613dfdSUlrich Weigand // ...likewise GPR varargs. 1945f613dfdSUlrich Weigand if (IsVarArg) 1955f613dfdSUlrich Weigand for (unsigned I = ZFI->getVarArgsFirstGPR(); I < SystemZ::NumArgGPRs; ++I) 196f1bd22dfSEric Christopher addSavedGPR(MBB, MIB, SystemZ::ArgGPRs[I], true); 1975f613dfdSUlrich Weigand } 1985f613dfdSUlrich Weigand 1995f613dfdSUlrich Weigand // Save FPRs in the normal TargetInstrInfo way. 2005f613dfdSUlrich Weigand for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 2015f613dfdSUlrich Weigand unsigned Reg = CSI[I].getReg(); 2025f613dfdSUlrich Weigand if (SystemZ::FP64BitRegClass.contains(Reg)) { 2035f613dfdSUlrich Weigand MBB.addLiveIn(Reg); 2045f613dfdSUlrich Weigand TII->storeRegToStackSlot(MBB, MBBI, Reg, true, CSI[I].getFrameIdx(), 2055f613dfdSUlrich Weigand &SystemZ::FP64BitRegClass, TRI); 2065f613dfdSUlrich Weigand } 2075f613dfdSUlrich Weigand } 2085f613dfdSUlrich Weigand 2095f613dfdSUlrich Weigand return true; 2105f613dfdSUlrich Weigand } 2115f613dfdSUlrich Weigand 2125f613dfdSUlrich Weigand bool SystemZFrameLowering:: 2135f613dfdSUlrich Weigand restoreCalleeSavedRegisters(MachineBasicBlock &MBB, 2145f613dfdSUlrich Weigand MachineBasicBlock::iterator MBBI, 2155f613dfdSUlrich Weigand const std::vector<CalleeSavedInfo> &CSI, 2165f613dfdSUlrich Weigand const TargetRegisterInfo *TRI) const { 2175f613dfdSUlrich Weigand if (CSI.empty()) 2185f613dfdSUlrich Weigand return false; 2195f613dfdSUlrich Weigand 2205f613dfdSUlrich Weigand MachineFunction &MF = *MBB.getParent(); 221fc6de428SEric Christopher const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); 2225f613dfdSUlrich Weigand SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); 2235f613dfdSUlrich Weigand bool HasFP = hasFP(MF); 2245f613dfdSUlrich Weigand DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 2255f613dfdSUlrich Weigand 2265f613dfdSUlrich Weigand // Restore FPRs in the normal TargetInstrInfo way. 2275f613dfdSUlrich Weigand for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 2285f613dfdSUlrich Weigand unsigned Reg = CSI[I].getReg(); 2295f613dfdSUlrich Weigand if (SystemZ::FP64BitRegClass.contains(Reg)) 2305f613dfdSUlrich Weigand TII->loadRegFromStackSlot(MBB, MBBI, Reg, CSI[I].getFrameIdx(), 2315f613dfdSUlrich Weigand &SystemZ::FP64BitRegClass, TRI); 2325f613dfdSUlrich Weigand } 2335f613dfdSUlrich Weigand 2345f613dfdSUlrich Weigand // Restore call-saved GPRs (but not call-clobbered varargs, which at 2355f613dfdSUlrich Weigand // this point might hold return values). 2365f613dfdSUlrich Weigand unsigned LowGPR = ZFI->getLowSavedGPR(); 2375f613dfdSUlrich Weigand unsigned HighGPR = ZFI->getHighSavedGPR(); 2385f613dfdSUlrich Weigand unsigned StartOffset = RegSpillOffsets[LowGPR]; 2395f613dfdSUlrich Weigand if (LowGPR) { 2405f613dfdSUlrich Weigand // If we saved any of %r2-%r5 as varargs, we should also be saving 2415f613dfdSUlrich Weigand // and restoring %r6. If we're saving %r6 or above, we should be 2425f613dfdSUlrich Weigand // restoring it too. 2435f613dfdSUlrich Weigand assert(LowGPR != HighGPR && "Should be loading %r15 and something else"); 2445f613dfdSUlrich Weigand 2455f613dfdSUlrich Weigand // Build an LMG instruction. 2465f613dfdSUlrich Weigand MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LMG)); 2475f613dfdSUlrich Weigand 2485f613dfdSUlrich Weigand // Add the explicit register operands. 2495f613dfdSUlrich Weigand MIB.addReg(LowGPR, RegState::Define); 2505f613dfdSUlrich Weigand MIB.addReg(HighGPR, RegState::Define); 2515f613dfdSUlrich Weigand 2525f613dfdSUlrich Weigand // Add the address. 2535f613dfdSUlrich Weigand MIB.addReg(HasFP ? SystemZ::R11D : SystemZ::R15D); 2545f613dfdSUlrich Weigand MIB.addImm(StartOffset); 2555f613dfdSUlrich Weigand 2565f613dfdSUlrich Weigand // Do a second scan adding regs as being defined by instruction 2575f613dfdSUlrich Weigand for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 2585f613dfdSUlrich Weigand unsigned Reg = CSI[I].getReg(); 2595f613dfdSUlrich Weigand if (Reg != LowGPR && Reg != HighGPR) 2605f613dfdSUlrich Weigand MIB.addReg(Reg, RegState::ImplicitDefine); 2615f613dfdSUlrich Weigand } 2625f613dfdSUlrich Weigand } 2635f613dfdSUlrich Weigand 2645f613dfdSUlrich Weigand return true; 2655f613dfdSUlrich Weigand } 2665f613dfdSUlrich Weigand 2675dd52f8cSRichard Sandiford void SystemZFrameLowering:: 2685dd52f8cSRichard Sandiford processFunctionBeforeFrameFinalized(MachineFunction &MF, 2695dd52f8cSRichard Sandiford RegScavenger *RS) const { 2705dd52f8cSRichard Sandiford MachineFrameInfo *MFFrame = MF.getFrameInfo(); 2715dd52f8cSRichard Sandiford uint64_t MaxReach = (MFFrame->estimateStackSize(MF) + 2725dd52f8cSRichard Sandiford SystemZMC::CallFrameSize * 2); 27323943229SRichard Sandiford if (!isUInt<12>(MaxReach)) { 27423943229SRichard Sandiford // We may need register scavenging slots if some parts of the frame 2755dd52f8cSRichard Sandiford // are outside the reach of an unsigned 12-bit displacement. 27623943229SRichard Sandiford // Create 2 for the case where both addresses in an MVC are 27723943229SRichard Sandiford // out of range. 2785dd52f8cSRichard Sandiford RS->addScavengingFrameIndex(MFFrame->CreateStackObject(8, 8, false)); 27923943229SRichard Sandiford RS->addScavengingFrameIndex(MFFrame->CreateStackObject(8, 8, false)); 28023943229SRichard Sandiford } 2815dd52f8cSRichard Sandiford } 2825dd52f8cSRichard Sandiford 2835f613dfdSUlrich Weigand // Emit instructions before MBBI (in MBB) to add NumBytes to Reg. 2845f613dfdSUlrich Weigand static void emitIncrement(MachineBasicBlock &MBB, 2855f613dfdSUlrich Weigand MachineBasicBlock::iterator &MBBI, 2865f613dfdSUlrich Weigand const DebugLoc &DL, 2875f613dfdSUlrich Weigand unsigned Reg, int64_t NumBytes, 2885f613dfdSUlrich Weigand const TargetInstrInfo *TII) { 2895f613dfdSUlrich Weigand while (NumBytes) { 2905f613dfdSUlrich Weigand unsigned Opcode; 2915f613dfdSUlrich Weigand int64_t ThisVal = NumBytes; 2925f613dfdSUlrich Weigand if (isInt<16>(NumBytes)) 2935f613dfdSUlrich Weigand Opcode = SystemZ::AGHI; 2945f613dfdSUlrich Weigand else { 2955f613dfdSUlrich Weigand Opcode = SystemZ::AGFI; 2965f613dfdSUlrich Weigand // Make sure we maintain 8-byte stack alignment. 297*fffd56ecSAlexey Samsonov int64_t MinVal = -uint64_t(1) << 31; 2985f613dfdSUlrich Weigand int64_t MaxVal = (int64_t(1) << 31) - 8; 2995f613dfdSUlrich Weigand if (ThisVal < MinVal) 3005f613dfdSUlrich Weigand ThisVal = MinVal; 3015f613dfdSUlrich Weigand else if (ThisVal > MaxVal) 3025f613dfdSUlrich Weigand ThisVal = MaxVal; 3035f613dfdSUlrich Weigand } 3045f613dfdSUlrich Weigand MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII->get(Opcode), Reg) 3055f613dfdSUlrich Weigand .addReg(Reg).addImm(ThisVal); 30614a44495SRichard Sandiford // The CC implicit def is dead. 3075f613dfdSUlrich Weigand MI->getOperand(3).setIsDead(); 3085f613dfdSUlrich Weigand NumBytes -= ThisVal; 3095f613dfdSUlrich Weigand } 3105f613dfdSUlrich Weigand } 3115f613dfdSUlrich Weigand 3125f613dfdSUlrich Weigand void SystemZFrameLowering::emitPrologue(MachineFunction &MF) const { 3135f613dfdSUlrich Weigand MachineBasicBlock &MBB = MF.front(); 3145f613dfdSUlrich Weigand MachineFrameInfo *MFFrame = MF.getFrameInfo(); 315fc6de428SEric Christopher auto *ZII = 316fc6de428SEric Christopher static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo()); 3175f613dfdSUlrich Weigand SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); 3185f613dfdSUlrich Weigand MachineBasicBlock::iterator MBBI = MBB.begin(); 3195f613dfdSUlrich Weigand MachineModuleInfo &MMI = MF.getMMI(); 320bc07a890SBill Wendling const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo(); 3215f613dfdSUlrich Weigand const std::vector<CalleeSavedInfo> &CSI = MFFrame->getCalleeSavedInfo(); 3225f613dfdSUlrich Weigand bool HasFP = hasFP(MF); 3235f613dfdSUlrich Weigand DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 3245f613dfdSUlrich Weigand 3255f613dfdSUlrich Weigand // The current offset of the stack pointer from the CFA. 3265f613dfdSUlrich Weigand int64_t SPOffsetFromCFA = -SystemZMC::CFAOffsetFromInitialSP; 3275f613dfdSUlrich Weigand 3285f613dfdSUlrich Weigand if (ZFI->getLowSavedGPR()) { 3295f613dfdSUlrich Weigand // Skip over the GPR saves. 3305f613dfdSUlrich Weigand if (MBBI != MBB.end() && MBBI->getOpcode() == SystemZ::STMG) 3315f613dfdSUlrich Weigand ++MBBI; 3325f613dfdSUlrich Weigand else 3335f613dfdSUlrich Weigand llvm_unreachable("Couldn't skip over GPR saves"); 3345f613dfdSUlrich Weigand 3355f613dfdSUlrich Weigand // Add CFI for the GPR saves. 33628c111ecSRichard Sandiford for (auto &Save : CSI) { 33728c111ecSRichard Sandiford unsigned Reg = Save.getReg(); 3385f613dfdSUlrich Weigand if (SystemZ::GR64BitRegClass.contains(Reg)) { 3395f613dfdSUlrich Weigand int64_t Offset = SPOffsetFromCFA + RegSpillOffsets[Reg]; 340b1f25f1bSRafael Espindola unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset( 341b1f25f1bSRafael Espindola nullptr, MRI->getDwarfRegNum(Reg, true), Offset)); 342b1f25f1bSRafael Espindola BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) 343b1f25f1bSRafael Espindola .addCFIIndex(CFIIndex); 3445f613dfdSUlrich Weigand } 3455f613dfdSUlrich Weigand } 3465f613dfdSUlrich Weigand } 3475f613dfdSUlrich Weigand 3485f613dfdSUlrich Weigand uint64_t StackSize = getAllocatedStackSize(MF); 3495f613dfdSUlrich Weigand if (StackSize) { 3505f613dfdSUlrich Weigand // Allocate StackSize bytes. 3515f613dfdSUlrich Weigand int64_t Delta = -int64_t(StackSize); 3525f613dfdSUlrich Weigand emitIncrement(MBB, MBBI, DL, SystemZ::R15D, Delta, ZII); 3535f613dfdSUlrich Weigand 3545f613dfdSUlrich Weigand // Add CFI for the allocation. 355b1f25f1bSRafael Espindola unsigned CFIIndex = MMI.addFrameInst( 356b1f25f1bSRafael Espindola MCCFIInstruction::createDefCfaOffset(nullptr, SPOffsetFromCFA + Delta)); 357b1f25f1bSRafael Espindola BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) 358b1f25f1bSRafael Espindola .addCFIIndex(CFIIndex); 3595f613dfdSUlrich Weigand SPOffsetFromCFA += Delta; 3605f613dfdSUlrich Weigand } 3615f613dfdSUlrich Weigand 3625f613dfdSUlrich Weigand if (HasFP) { 3635f613dfdSUlrich Weigand // Copy the base of the frame to R11. 3645f613dfdSUlrich Weigand BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR), SystemZ::R11D) 3655f613dfdSUlrich Weigand .addReg(SystemZ::R15D); 3665f613dfdSUlrich Weigand 3675f613dfdSUlrich Weigand // Add CFI for the new frame location. 368bc07a890SBill Wendling unsigned HardFP = MRI->getDwarfRegNum(SystemZ::R11D, true); 369b1f25f1bSRafael Espindola unsigned CFIIndex = MMI.addFrameInst( 370b1f25f1bSRafael Espindola MCCFIInstruction::createDefCfaRegister(nullptr, HardFP)); 371b1f25f1bSRafael Espindola BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) 372b1f25f1bSRafael Espindola .addCFIIndex(CFIIndex); 3735f613dfdSUlrich Weigand 3745f613dfdSUlrich Weigand // Mark the FramePtr as live at the beginning of every block except 3755f613dfdSUlrich Weigand // the entry block. (We'll have marked R11 as live on entry when 3765f613dfdSUlrich Weigand // saving the GPRs.) 37728c111ecSRichard Sandiford for (auto I = std::next(MF.begin()), E = MF.end(); I != E; ++I) 3785f613dfdSUlrich Weigand I->addLiveIn(SystemZ::R11D); 3795f613dfdSUlrich Weigand } 3805f613dfdSUlrich Weigand 3815f613dfdSUlrich Weigand // Skip over the FPR saves. 382b1f25f1bSRafael Espindola SmallVector<unsigned, 8> CFIIndexes; 38328c111ecSRichard Sandiford for (auto &Save : CSI) { 38428c111ecSRichard Sandiford unsigned Reg = Save.getReg(); 3855f613dfdSUlrich Weigand if (SystemZ::FP64BitRegClass.contains(Reg)) { 3865f613dfdSUlrich Weigand if (MBBI != MBB.end() && 3875f613dfdSUlrich Weigand (MBBI->getOpcode() == SystemZ::STD || 3885f613dfdSUlrich Weigand MBBI->getOpcode() == SystemZ::STDY)) 3895f613dfdSUlrich Weigand ++MBBI; 3905f613dfdSUlrich Weigand else 3915f613dfdSUlrich Weigand llvm_unreachable("Couldn't skip over FPR save"); 3925f613dfdSUlrich Weigand 3935f613dfdSUlrich Weigand // Add CFI for the this save. 39428c111ecSRichard Sandiford unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true); 39528c111ecSRichard Sandiford int64_t Offset = getFrameIndexOffset(MF, Save.getFrameIdx()); 396b1f25f1bSRafael Espindola unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset( 397b1f25f1bSRafael Espindola nullptr, DwarfReg, SPOffsetFromCFA + Offset)); 398b1f25f1bSRafael Espindola CFIIndexes.push_back(CFIIndex); 3995f613dfdSUlrich Weigand } 4005f613dfdSUlrich Weigand } 4015f613dfdSUlrich Weigand // Complete the CFI for the FPR saves, modelling them as taking effect 4025f613dfdSUlrich Weigand // after the last save. 403b1f25f1bSRafael Espindola for (auto CFIIndex : CFIIndexes) { 404b1f25f1bSRafael Espindola BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) 405b1f25f1bSRafael Espindola .addCFIIndex(CFIIndex); 406b1f25f1bSRafael Espindola } 4075f613dfdSUlrich Weigand } 4085f613dfdSUlrich Weigand 4095f613dfdSUlrich Weigand void SystemZFrameLowering::emitEpilogue(MachineFunction &MF, 4105f613dfdSUlrich Weigand MachineBasicBlock &MBB) const { 4115f613dfdSUlrich Weigand MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 412fc6de428SEric Christopher auto *ZII = 413fc6de428SEric Christopher static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo()); 4145f613dfdSUlrich Weigand SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); 4155f613dfdSUlrich Weigand 4165f613dfdSUlrich Weigand // Skip the return instruction. 417709bda66SRichard Sandiford assert(MBBI->isReturn() && "Can only insert epilogue into returning blocks"); 4185f613dfdSUlrich Weigand 4195f613dfdSUlrich Weigand uint64_t StackSize = getAllocatedStackSize(MF); 4205f613dfdSUlrich Weigand if (ZFI->getLowSavedGPR()) { 4215f613dfdSUlrich Weigand --MBBI; 4225f613dfdSUlrich Weigand unsigned Opcode = MBBI->getOpcode(); 4235f613dfdSUlrich Weigand if (Opcode != SystemZ::LMG) 4245f613dfdSUlrich Weigand llvm_unreachable("Expected to see callee-save register restore code"); 4255f613dfdSUlrich Weigand 4265f613dfdSUlrich Weigand unsigned AddrOpNo = 2; 4275f613dfdSUlrich Weigand DebugLoc DL = MBBI->getDebugLoc(); 4285f613dfdSUlrich Weigand uint64_t Offset = StackSize + MBBI->getOperand(AddrOpNo + 1).getImm(); 4295f613dfdSUlrich Weigand unsigned NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset); 4305f613dfdSUlrich Weigand 4315f613dfdSUlrich Weigand // If the offset is too large, use the largest stack-aligned offset 4325f613dfdSUlrich Weigand // and add the rest to the base register (the stack or frame pointer). 4335f613dfdSUlrich Weigand if (!NewOpcode) { 4345f613dfdSUlrich Weigand uint64_t NumBytes = Offset - 0x7fff8; 4355f613dfdSUlrich Weigand emitIncrement(MBB, MBBI, DL, MBBI->getOperand(AddrOpNo).getReg(), 4365f613dfdSUlrich Weigand NumBytes, ZII); 4375f613dfdSUlrich Weigand Offset -= NumBytes; 4385f613dfdSUlrich Weigand NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset); 4395f613dfdSUlrich Weigand assert(NewOpcode && "No restore instruction available"); 4405f613dfdSUlrich Weigand } 4415f613dfdSUlrich Weigand 4425f613dfdSUlrich Weigand MBBI->setDesc(ZII->get(NewOpcode)); 4435f613dfdSUlrich Weigand MBBI->getOperand(AddrOpNo + 1).ChangeToImmediate(Offset); 4445f613dfdSUlrich Weigand } else if (StackSize) { 4455f613dfdSUlrich Weigand DebugLoc DL = MBBI->getDebugLoc(); 4465f613dfdSUlrich Weigand emitIncrement(MBB, MBBI, DL, SystemZ::R15D, StackSize, ZII); 4475f613dfdSUlrich Weigand } 4485f613dfdSUlrich Weigand } 4495f613dfdSUlrich Weigand 4505f613dfdSUlrich Weigand bool SystemZFrameLowering::hasFP(const MachineFunction &MF) const { 4515f613dfdSUlrich Weigand return (MF.getTarget().Options.DisableFramePointerElim(MF) || 4525f613dfdSUlrich Weigand MF.getFrameInfo()->hasVarSizedObjects() || 4535f613dfdSUlrich Weigand MF.getInfo<SystemZMachineFunctionInfo>()->getManipulatesSP()); 4545f613dfdSUlrich Weigand } 4555f613dfdSUlrich Weigand 4565f613dfdSUlrich Weigand int SystemZFrameLowering::getFrameIndexOffset(const MachineFunction &MF, 4575f613dfdSUlrich Weigand int FI) const { 4585f613dfdSUlrich Weigand const MachineFrameInfo *MFFrame = MF.getFrameInfo(); 4595f613dfdSUlrich Weigand 4605f613dfdSUlrich Weigand // Start with the offset of FI from the top of the caller-allocated frame 4615f613dfdSUlrich Weigand // (i.e. the top of the 160 bytes allocated by the caller). This initial 4625f613dfdSUlrich Weigand // offset is therefore negative. 4635f613dfdSUlrich Weigand int64_t Offset = (MFFrame->getObjectOffset(FI) + 4645f613dfdSUlrich Weigand MFFrame->getOffsetAdjustment()); 4655f613dfdSUlrich Weigand 4665f613dfdSUlrich Weigand // Make the offset relative to the incoming stack pointer. 4675f613dfdSUlrich Weigand Offset -= getOffsetOfLocalArea(); 4685f613dfdSUlrich Weigand 4695f613dfdSUlrich Weigand // Make the offset relative to the bottom of the frame. 4705f613dfdSUlrich Weigand Offset += getAllocatedStackSize(MF); 4715f613dfdSUlrich Weigand 4725f613dfdSUlrich Weigand return Offset; 4735f613dfdSUlrich Weigand } 4745f613dfdSUlrich Weigand 4755f613dfdSUlrich Weigand uint64_t SystemZFrameLowering:: 4765f613dfdSUlrich Weigand getAllocatedStackSize(const MachineFunction &MF) const { 4775f613dfdSUlrich Weigand const MachineFrameInfo *MFFrame = MF.getFrameInfo(); 4785f613dfdSUlrich Weigand 4795f613dfdSUlrich Weigand // Start with the size of the local variables and spill slots. 4805f613dfdSUlrich Weigand uint64_t StackSize = MFFrame->getStackSize(); 4815f613dfdSUlrich Weigand 4825f613dfdSUlrich Weigand // We need to allocate the ABI-defined 160-byte base area whenever 4835f613dfdSUlrich Weigand // we allocate stack space for our own use and whenever we call another 4845f613dfdSUlrich Weigand // function. 4855f613dfdSUlrich Weigand if (StackSize || MFFrame->hasVarSizedObjects() || MFFrame->hasCalls()) 4865f613dfdSUlrich Weigand StackSize += SystemZMC::CallFrameSize; 4875f613dfdSUlrich Weigand 4885f613dfdSUlrich Weigand return StackSize; 4895f613dfdSUlrich Weigand } 4905f613dfdSUlrich Weigand 4915f613dfdSUlrich Weigand bool 4925f613dfdSUlrich Weigand SystemZFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { 4935f613dfdSUlrich Weigand // The ABI requires us to allocate 160 bytes of stack space for the callee, 4945f613dfdSUlrich Weigand // with any outgoing stack arguments being placed above that. It seems 4955f613dfdSUlrich Weigand // better to make that area a permanent feature of the frame even if 4965f613dfdSUlrich Weigand // we're using a frame pointer. 4975f613dfdSUlrich Weigand return true; 4985f613dfdSUlrich Weigand } 4995f613dfdSUlrich Weigand 5005f613dfdSUlrich Weigand void SystemZFrameLowering:: 5015f613dfdSUlrich Weigand eliminateCallFramePseudoInstr(MachineFunction &MF, 5025f613dfdSUlrich Weigand MachineBasicBlock &MBB, 5035f613dfdSUlrich Weigand MachineBasicBlock::iterator MI) const { 5045f613dfdSUlrich Weigand switch (MI->getOpcode()) { 5055f613dfdSUlrich Weigand case SystemZ::ADJCALLSTACKDOWN: 5065f613dfdSUlrich Weigand case SystemZ::ADJCALLSTACKUP: 5075f613dfdSUlrich Weigand assert(hasReservedCallFrame(MF) && 5085f613dfdSUlrich Weigand "ADJSTACKDOWN and ADJSTACKUP should be no-ops"); 5095f613dfdSUlrich Weigand MBB.erase(MI); 5105f613dfdSUlrich Weigand break; 5115f613dfdSUlrich Weigand 5125f613dfdSUlrich Weigand default: 5135f613dfdSUlrich Weigand llvm_unreachable("Unexpected call frame instruction"); 5145f613dfdSUlrich Weigand } 5155f613dfdSUlrich Weigand } 516