Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1 | //===-- SystemZFrameLowering.cpp - Frame lowering for SystemZ -------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "SystemZFrameLowering.h" |
| 10 | #include "SystemZCallingConv.h" |
| 11 | #include "SystemZInstrBuilder.h" |
Eric Christopher | f1bd22d | 2014-07-01 20:18:59 +0000 | [diff] [blame] | 12 | #include "SystemZInstrInfo.h" |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 13 | #include "SystemZMachineFunctionInfo.h" |
Eric Christopher | f1bd22d | 2014-07-01 20:18:59 +0000 | [diff] [blame] | 14 | #include "SystemZRegisterInfo.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 15 | #include "SystemZSubtarget.h" |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 17 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Richard Sandiford | 5dd52f8 | 2013-07-05 12:55:00 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/RegisterScavenging.h" |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Function.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
Richard Sandiford | db39b4a | 2013-07-03 09:11:00 +0000 | [diff] [blame] | 23 | namespace { |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 24 | // The ABI-defined register save slots, relative to the incoming stack |
| 25 | // pointer. |
| 26 | static const TargetFrameLowering::SpillSlot SpillOffsetTable[] = { |
| 27 | { SystemZ::R2D, 0x10 }, |
| 28 | { SystemZ::R3D, 0x18 }, |
| 29 | { SystemZ::R4D, 0x20 }, |
| 30 | { SystemZ::R5D, 0x28 }, |
| 31 | { SystemZ::R6D, 0x30 }, |
| 32 | { SystemZ::R7D, 0x38 }, |
| 33 | { SystemZ::R8D, 0x40 }, |
| 34 | { SystemZ::R9D, 0x48 }, |
| 35 | { SystemZ::R10D, 0x50 }, |
| 36 | { SystemZ::R11D, 0x58 }, |
| 37 | { SystemZ::R12D, 0x60 }, |
| 38 | { SystemZ::R13D, 0x68 }, |
| 39 | { SystemZ::R14D, 0x70 }, |
| 40 | { SystemZ::R15D, 0x78 }, |
| 41 | { SystemZ::F0D, 0x80 }, |
| 42 | { SystemZ::F2D, 0x88 }, |
| 43 | { SystemZ::F4D, 0x90 }, |
| 44 | { SystemZ::F6D, 0x98 } |
| 45 | }; |
| 46 | } // end anonymous namespace |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 47 | |
Eric Christopher | f1bd22d | 2014-07-01 20:18:59 +0000 | [diff] [blame] | 48 | SystemZFrameLowering::SystemZFrameLowering() |
Guillaume Chatelet | 882c43d | 2019-10-17 07:49:39 +0000 | [diff] [blame] | 49 | : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, Align(8), |
Jonas Paulsson | f12b925 | 2015-11-28 11:02:32 +0000 | [diff] [blame] | 50 | -SystemZMC::CallFrameSize, 8, |
| 51 | false /* StackRealignable */) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 52 | // Create a mapping from register number to save slot offset. |
| 53 | RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS); |
| 54 | for (unsigned I = 0, E = array_lengthof(SpillOffsetTable); I != E; ++I) |
Richard Sandiford | db39b4a | 2013-07-03 09:11:00 +0000 | [diff] [blame] | 55 | RegSpillOffsets[SpillOffsetTable[I].Reg] = SpillOffsetTable[I].Offset; |
| 56 | } |
| 57 | |
| 58 | const TargetFrameLowering::SpillSlot * |
| 59 | SystemZFrameLowering::getCalleeSavedSpillSlots(unsigned &NumEntries) const { |
| 60 | NumEntries = array_lengthof(SpillOffsetTable); |
| 61 | return SpillOffsetTable; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 64 | void SystemZFrameLowering::determineCalleeSaves(MachineFunction &MF, |
| 65 | BitVector &SavedRegs, |
| 66 | RegScavenger *RS) const { |
| 67 | TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS); |
| 68 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 69 | MachineFrameInfo &MFFrame = MF.getFrameInfo(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 70 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 71 | bool HasFP = hasFP(MF); |
| 72 | SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>(); |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 73 | bool IsVarArg = MF.getFunction().isVarArg(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 74 | |
| 75 | // va_start stores incoming FPR varargs in the normal way, but delegates |
| 76 | // the saving of incoming GPR varargs to spillCalleeSavedRegisters(). |
| 77 | // Record these pending uses, which typically include the call-saved |
| 78 | // argument register R6D. |
| 79 | if (IsVarArg) |
| 80 | for (unsigned I = MFI->getVarArgsFirstGPR(); I < SystemZ::NumArgGPRs; ++I) |
Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 81 | SavedRegs.set(SystemZ::ArgGPRs[I]); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 82 | |
Marcin Koscielnicki | 234e5a8 | 2016-06-28 14:13:11 +0000 | [diff] [blame] | 83 | // If there are any landing pads, entering them will modify r6/r7. |
Matthias Braun | d0ee66c | 2016-12-01 19:32:15 +0000 | [diff] [blame] | 84 | if (!MF.getLandingPads().empty()) { |
Marcin Koscielnicki | 234e5a8 | 2016-06-28 14:13:11 +0000 | [diff] [blame] | 85 | SavedRegs.set(SystemZ::R6D); |
| 86 | SavedRegs.set(SystemZ::R7D); |
| 87 | } |
| 88 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 89 | // If the function requires a frame pointer, record that the hard |
| 90 | // frame pointer will be clobbered. |
| 91 | if (HasFP) |
Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 92 | SavedRegs.set(SystemZ::R11D); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 93 | |
| 94 | // If the function calls other functions, record that the return |
| 95 | // address register will be clobbered. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 96 | if (MFFrame.hasCalls()) |
Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 97 | SavedRegs.set(SystemZ::R14D); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 98 | |
| 99 | // If we are saving GPRs other than the stack pointer, we might as well |
| 100 | // save and restore the stack pointer at the same time, via STMG and LMG. |
| 101 | // This allows the deallocation to be done by the LMG, rather than needing |
| 102 | // a separate %r15 addition. |
Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 103 | const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 104 | for (unsigned I = 0; CSRegs[I]; ++I) { |
| 105 | unsigned Reg = CSRegs[I]; |
Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 106 | if (SystemZ::GR64BitRegClass.contains(Reg) && SavedRegs.test(Reg)) { |
| 107 | SavedRegs.set(SystemZ::R15D); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 108 | break; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Add GPR64 to the save instruction being built by MIB, which is in basic |
| 114 | // block MBB. IsImplicit says whether this is an explicit operand to the |
| 115 | // instruction, or an implicit one that comes between the explicit start |
| 116 | // and end registers. |
| 117 | static void addSavedGPR(MachineBasicBlock &MBB, MachineInstrBuilder &MIB, |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 118 | unsigned GPR64, bool IsImplicit) { |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 119 | const TargetRegisterInfo *RI = |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 120 | MBB.getParent()->getSubtarget().getRegisterInfo(); |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 121 | Register GPR32 = RI->getSubReg(GPR64, SystemZ::subreg_l32); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 122 | bool IsLive = MBB.isLiveIn(GPR64) || MBB.isLiveIn(GPR32); |
| 123 | if (!IsLive || !IsImplicit) { |
| 124 | MIB.addReg(GPR64, getImplRegState(IsImplicit) | getKillRegState(!IsLive)); |
| 125 | if (!IsLive) |
| 126 | MBB.addLiveIn(GPR64); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | bool SystemZFrameLowering:: |
| 131 | spillCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 132 | MachineBasicBlock::iterator MBBI, |
| 133 | const std::vector<CalleeSavedInfo> &CSI, |
| 134 | const TargetRegisterInfo *TRI) const { |
| 135 | if (CSI.empty()) |
| 136 | return false; |
| 137 | |
| 138 | MachineFunction &MF = *MBB.getParent(); |
Tim Northover | 775aaeb | 2015-11-05 21:54:58 +0000 | [diff] [blame] | 139 | const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); |
| 140 | SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 141 | bool IsVarArg = MF.getFunction().isVarArg(); |
Tim Northover | 775aaeb | 2015-11-05 21:54:58 +0000 | [diff] [blame] | 142 | DebugLoc DL; |
| 143 | |
| 144 | // Scan the call-saved GPRs and find the bounds of the register spill area. |
| 145 | unsigned LowGPR = 0; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 146 | unsigned HighGPR = SystemZ::R15D; |
| 147 | unsigned StartOffset = -1U; |
| 148 | for (unsigned I = 0, E = CSI.size(); I != E; ++I) { |
| 149 | unsigned Reg = CSI[I].getReg(); |
| 150 | if (SystemZ::GR64BitRegClass.contains(Reg)) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 151 | unsigned Offset = RegSpillOffsets[Reg]; |
| 152 | assert(Offset && "Unexpected GPR save"); |
| 153 | if (StartOffset > Offset) { |
| 154 | LowGPR = Reg; |
| 155 | StartOffset = Offset; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
Richard Sandiford | db39b4a | 2013-07-03 09:11:00 +0000 | [diff] [blame] | 160 | // Save the range of call-saved registers, for use by the epilogue inserter. |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 161 | ZFI->setLowSavedGPR(LowGPR); |
| 162 | ZFI->setHighSavedGPR(HighGPR); |
| 163 | |
| 164 | // Include the GPR varargs, if any. R6D is call-saved, so would |
| 165 | // be included by the loop above, but we also need to handle the |
| 166 | // call-clobbered argument registers. |
| 167 | if (IsVarArg) { |
| 168 | unsigned FirstGPR = ZFI->getVarArgsFirstGPR(); |
| 169 | if (FirstGPR < SystemZ::NumArgGPRs) { |
| 170 | unsigned Reg = SystemZ::ArgGPRs[FirstGPR]; |
| 171 | unsigned Offset = RegSpillOffsets[Reg]; |
| 172 | if (StartOffset > Offset) { |
| 173 | LowGPR = Reg; StartOffset = Offset; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Save GPRs |
| 179 | if (LowGPR) { |
| 180 | assert(LowGPR != HighGPR && "Should be saving %r15 and something else"); |
| 181 | |
| 182 | // Build an STMG instruction. |
| 183 | MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STMG)); |
| 184 | |
| 185 | // Add the explicit register operands. |
Eric Christopher | f1bd22d | 2014-07-01 20:18:59 +0000 | [diff] [blame] | 186 | addSavedGPR(MBB, MIB, LowGPR, false); |
| 187 | addSavedGPR(MBB, MIB, HighGPR, false); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 188 | |
| 189 | // Add the address. |
| 190 | MIB.addReg(SystemZ::R15D).addImm(StartOffset); |
| 191 | |
| 192 | // Make sure all call-saved GPRs are included as operands and are |
| 193 | // marked as live on entry. |
| 194 | for (unsigned I = 0, E = CSI.size(); I != E; ++I) { |
| 195 | unsigned Reg = CSI[I].getReg(); |
| 196 | if (SystemZ::GR64BitRegClass.contains(Reg)) |
Eric Christopher | f1bd22d | 2014-07-01 20:18:59 +0000 | [diff] [blame] | 197 | addSavedGPR(MBB, MIB, Reg, true); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // ...likewise GPR varargs. |
| 201 | if (IsVarArg) |
| 202 | for (unsigned I = ZFI->getVarArgsFirstGPR(); I < SystemZ::NumArgGPRs; ++I) |
Eric Christopher | f1bd22d | 2014-07-01 20:18:59 +0000 | [diff] [blame] | 203 | addSavedGPR(MBB, MIB, SystemZ::ArgGPRs[I], true); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Ulrich Weigand | 8b19be4 | 2018-03-02 20:40:11 +0000 | [diff] [blame] | 206 | // Save FPRs/VRs in the normal TargetInstrInfo way. |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 207 | for (unsigned I = 0, E = CSI.size(); I != E; ++I) { |
| 208 | unsigned Reg = CSI[I].getReg(); |
| 209 | if (SystemZ::FP64BitRegClass.contains(Reg)) { |
| 210 | MBB.addLiveIn(Reg); |
| 211 | TII->storeRegToStackSlot(MBB, MBBI, Reg, true, CSI[I].getFrameIdx(), |
| 212 | &SystemZ::FP64BitRegClass, TRI); |
| 213 | } |
Ulrich Weigand | 8b19be4 | 2018-03-02 20:40:11 +0000 | [diff] [blame] | 214 | if (SystemZ::VR128BitRegClass.contains(Reg)) { |
| 215 | MBB.addLiveIn(Reg); |
| 216 | TII->storeRegToStackSlot(MBB, MBBI, Reg, true, CSI[I].getFrameIdx(), |
| 217 | &SystemZ::VR128BitRegClass, TRI); |
| 218 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | bool SystemZFrameLowering:: |
| 225 | restoreCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 226 | MachineBasicBlock::iterator MBBI, |
Krzysztof Parzyszek | bea30c6 | 2017-08-10 16:17:32 +0000 | [diff] [blame] | 227 | std::vector<CalleeSavedInfo> &CSI, |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 228 | const TargetRegisterInfo *TRI) const { |
| 229 | if (CSI.empty()) |
| 230 | return false; |
| 231 | |
| 232 | MachineFunction &MF = *MBB.getParent(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 233 | const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 234 | SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); |
| 235 | bool HasFP = hasFP(MF); |
| 236 | DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); |
| 237 | |
Ulrich Weigand | 8b19be4 | 2018-03-02 20:40:11 +0000 | [diff] [blame] | 238 | // Restore FPRs/VRs in the normal TargetInstrInfo way. |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 239 | for (unsigned I = 0, E = CSI.size(); I != E; ++I) { |
| 240 | unsigned Reg = CSI[I].getReg(); |
| 241 | if (SystemZ::FP64BitRegClass.contains(Reg)) |
| 242 | TII->loadRegFromStackSlot(MBB, MBBI, Reg, CSI[I].getFrameIdx(), |
| 243 | &SystemZ::FP64BitRegClass, TRI); |
Ulrich Weigand | 8b19be4 | 2018-03-02 20:40:11 +0000 | [diff] [blame] | 244 | if (SystemZ::VR128BitRegClass.contains(Reg)) |
| 245 | TII->loadRegFromStackSlot(MBB, MBBI, Reg, CSI[I].getFrameIdx(), |
| 246 | &SystemZ::VR128BitRegClass, TRI); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | // Restore call-saved GPRs (but not call-clobbered varargs, which at |
| 250 | // this point might hold return values). |
| 251 | unsigned LowGPR = ZFI->getLowSavedGPR(); |
| 252 | unsigned HighGPR = ZFI->getHighSavedGPR(); |
| 253 | unsigned StartOffset = RegSpillOffsets[LowGPR]; |
| 254 | if (LowGPR) { |
| 255 | // If we saved any of %r2-%r5 as varargs, we should also be saving |
| 256 | // and restoring %r6. If we're saving %r6 or above, we should be |
| 257 | // restoring it too. |
| 258 | assert(LowGPR != HighGPR && "Should be loading %r15 and something else"); |
| 259 | |
| 260 | // Build an LMG instruction. |
| 261 | MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LMG)); |
| 262 | |
| 263 | // Add the explicit register operands. |
| 264 | MIB.addReg(LowGPR, RegState::Define); |
| 265 | MIB.addReg(HighGPR, RegState::Define); |
| 266 | |
| 267 | // Add the address. |
| 268 | MIB.addReg(HasFP ? SystemZ::R11D : SystemZ::R15D); |
| 269 | MIB.addImm(StartOffset); |
| 270 | |
| 271 | // Do a second scan adding regs as being defined by instruction |
| 272 | for (unsigned I = 0, E = CSI.size(); I != E; ++I) { |
| 273 | unsigned Reg = CSI[I].getReg(); |
Jonas Paulsson | f034482 | 2016-05-02 09:37:44 +0000 | [diff] [blame] | 274 | if (Reg != LowGPR && Reg != HighGPR && |
| 275 | SystemZ::GR64BitRegClass.contains(Reg)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 276 | MIB.addReg(Reg, RegState::ImplicitDefine); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | return true; |
| 281 | } |
| 282 | |
Richard Sandiford | 5dd52f8 | 2013-07-05 12:55:00 +0000 | [diff] [blame] | 283 | void SystemZFrameLowering:: |
| 284 | processFunctionBeforeFrameFinalized(MachineFunction &MF, |
| 285 | RegScavenger *RS) const { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 286 | MachineFrameInfo &MFFrame = MF.getFrameInfo(); |
Ulrich Weigand | af98b74 | 2017-06-26 16:50:32 +0000 | [diff] [blame] | 287 | // Get the size of our stack frame to be allocated ... |
| 288 | uint64_t StackSize = (MFFrame.estimateStackSize(MF) + |
| 289 | SystemZMC::CallFrameSize); |
| 290 | // ... and the maximum offset we may need to reach into the |
| 291 | // caller's frame to access the save area or stack arguments. |
| 292 | int64_t MaxArgOffset = SystemZMC::CallFrameSize; |
| 293 | for (int I = MFFrame.getObjectIndexBegin(); I != 0; ++I) |
| 294 | if (MFFrame.getObjectOffset(I) >= 0) { |
| 295 | int64_t ArgOffset = SystemZMC::CallFrameSize + |
| 296 | MFFrame.getObjectOffset(I) + |
| 297 | MFFrame.getObjectSize(I); |
| 298 | MaxArgOffset = std::max(MaxArgOffset, ArgOffset); |
| 299 | } |
| 300 | |
| 301 | uint64_t MaxReach = StackSize + MaxArgOffset; |
Richard Sandiford | 2394322 | 2013-07-05 13:11:52 +0000 | [diff] [blame] | 302 | if (!isUInt<12>(MaxReach)) { |
| 303 | // We may need register scavenging slots if some parts of the frame |
Richard Sandiford | 5dd52f8 | 2013-07-05 12:55:00 +0000 | [diff] [blame] | 304 | // are outside the reach of an unsigned 12-bit displacement. |
Richard Sandiford | 2394322 | 2013-07-05 13:11:52 +0000 | [diff] [blame] | 305 | // Create 2 for the case where both addresses in an MVC are |
| 306 | // out of range. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 307 | RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, 8, false)); |
| 308 | RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, 8, false)); |
Richard Sandiford | 2394322 | 2013-07-05 13:11:52 +0000 | [diff] [blame] | 309 | } |
Richard Sandiford | 5dd52f8 | 2013-07-05 12:55:00 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 312 | // Emit instructions before MBBI (in MBB) to add NumBytes to Reg. |
| 313 | static void emitIncrement(MachineBasicBlock &MBB, |
| 314 | MachineBasicBlock::iterator &MBBI, |
| 315 | const DebugLoc &DL, |
| 316 | unsigned Reg, int64_t NumBytes, |
| 317 | const TargetInstrInfo *TII) { |
| 318 | while (NumBytes) { |
| 319 | unsigned Opcode; |
| 320 | int64_t ThisVal = NumBytes; |
| 321 | if (isInt<16>(NumBytes)) |
| 322 | Opcode = SystemZ::AGHI; |
| 323 | else { |
| 324 | Opcode = SystemZ::AGFI; |
| 325 | // Make sure we maintain 8-byte stack alignment. |
Alexey Samsonov | fffd56ec | 2014-08-20 21:56:43 +0000 | [diff] [blame] | 326 | int64_t MinVal = -uint64_t(1) << 31; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 327 | int64_t MaxVal = (int64_t(1) << 31) - 8; |
| 328 | if (ThisVal < MinVal) |
| 329 | ThisVal = MinVal; |
| 330 | else if (ThisVal > MaxVal) |
| 331 | ThisVal = MaxVal; |
| 332 | } |
| 333 | MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII->get(Opcode), Reg) |
| 334 | .addReg(Reg).addImm(ThisVal); |
Richard Sandiford | 14a4449 | 2013-05-22 13:38:45 +0000 | [diff] [blame] | 335 | // The CC implicit def is dead. |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 336 | MI->getOperand(3).setIsDead(); |
| 337 | NumBytes -= ThisVal; |
| 338 | } |
| 339 | } |
| 340 | |
Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 341 | void SystemZFrameLowering::emitPrologue(MachineFunction &MF, |
| 342 | MachineBasicBlock &MBB) const { |
| 343 | assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported"); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 344 | MachineFrameInfo &MFFrame = MF.getFrameInfo(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 345 | auto *ZII = |
| 346 | static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 347 | SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); |
| 348 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| 349 | MachineModuleInfo &MMI = MF.getMMI(); |
Tim Northover | 775aaeb | 2015-11-05 21:54:58 +0000 | [diff] [blame] | 350 | const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo(); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 351 | const std::vector<CalleeSavedInfo> &CSI = MFFrame.getCalleeSavedInfo(); |
Tim Northover | 775aaeb | 2015-11-05 21:54:58 +0000 | [diff] [blame] | 352 | bool HasFP = hasFP(MF); |
| 353 | |
| 354 | // Debug location must be unknown since the first debug location is used |
| 355 | // to determine the end of the prologue. |
| 356 | DebugLoc DL; |
| 357 | |
| 358 | // The current offset of the stack pointer from the CFA. |
| 359 | int64_t SPOffsetFromCFA = -SystemZMC::CFAOffsetFromInitialSP; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 360 | |
| 361 | if (ZFI->getLowSavedGPR()) { |
| 362 | // Skip over the GPR saves. |
| 363 | if (MBBI != MBB.end() && MBBI->getOpcode() == SystemZ::STMG) |
| 364 | ++MBBI; |
| 365 | else |
| 366 | llvm_unreachable("Couldn't skip over GPR saves"); |
| 367 | |
| 368 | // Add CFI for the GPR saves. |
Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 369 | for (auto &Save : CSI) { |
| 370 | unsigned Reg = Save.getReg(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 371 | if (SystemZ::GR64BitRegClass.contains(Reg)) { |
| 372 | int64_t Offset = SPOffsetFromCFA + RegSpillOffsets[Reg]; |
Matthias Braun | f23ef43 | 2016-11-30 23:48:42 +0000 | [diff] [blame] | 373 | unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset( |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 374 | nullptr, MRI->getDwarfRegNum(Reg, true), Offset)); |
| 375 | BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) |
| 376 | .addCFIIndex(CFIIndex); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
Ulrich Weigand | 3206388 | 2018-03-02 20:38:41 +0000 | [diff] [blame] | 381 | uint64_t StackSize = MFFrame.getStackSize(); |
| 382 | // We need to allocate the ABI-defined 160-byte base area whenever |
| 383 | // we allocate stack space for our own use and whenever we call another |
| 384 | // function. |
| 385 | if (StackSize || MFFrame.hasVarSizedObjects() || MFFrame.hasCalls()) { |
| 386 | StackSize += SystemZMC::CallFrameSize; |
| 387 | MFFrame.setStackSize(StackSize); |
| 388 | } |
| 389 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 390 | if (StackSize) { |
Marcin Koscielnicki | ad1482c | 2016-05-05 00:37:30 +0000 | [diff] [blame] | 391 | // Determine if we want to store a backchain. |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 392 | bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain"); |
Marcin Koscielnicki | ad1482c | 2016-05-05 00:37:30 +0000 | [diff] [blame] | 393 | |
| 394 | // If we need backchain, save current stack pointer. R1 is free at this |
| 395 | // point. |
| 396 | if (StoreBackchain) |
| 397 | BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR)) |
| 398 | .addReg(SystemZ::R1D, RegState::Define).addReg(SystemZ::R15D); |
| 399 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 400 | // Allocate StackSize bytes. |
| 401 | int64_t Delta = -int64_t(StackSize); |
| 402 | emitIncrement(MBB, MBBI, DL, SystemZ::R15D, Delta, ZII); |
| 403 | |
| 404 | // Add CFI for the allocation. |
Matthias Braun | f23ef43 | 2016-11-30 23:48:42 +0000 | [diff] [blame] | 405 | unsigned CFIIndex = MF.addFrameInst( |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 406 | MCCFIInstruction::createDefCfaOffset(nullptr, SPOffsetFromCFA + Delta)); |
| 407 | BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) |
| 408 | .addCFIIndex(CFIIndex); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 409 | SPOffsetFromCFA += Delta; |
Marcin Koscielnicki | ad1482c | 2016-05-05 00:37:30 +0000 | [diff] [blame] | 410 | |
| 411 | if (StoreBackchain) |
| 412 | BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::STG)) |
| 413 | .addReg(SystemZ::R1D, RegState::Kill).addReg(SystemZ::R15D).addImm(0).addReg(0); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | if (HasFP) { |
| 417 | // Copy the base of the frame to R11. |
| 418 | BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR), SystemZ::R11D) |
| 419 | .addReg(SystemZ::R15D); |
| 420 | |
| 421 | // Add CFI for the new frame location. |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 422 | unsigned HardFP = MRI->getDwarfRegNum(SystemZ::R11D, true); |
Matthias Braun | f23ef43 | 2016-11-30 23:48:42 +0000 | [diff] [blame] | 423 | unsigned CFIIndex = MF.addFrameInst( |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 424 | MCCFIInstruction::createDefCfaRegister(nullptr, HardFP)); |
| 425 | BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) |
| 426 | .addCFIIndex(CFIIndex); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 427 | |
| 428 | // Mark the FramePtr as live at the beginning of every block except |
| 429 | // the entry block. (We'll have marked R11 as live on entry when |
| 430 | // saving the GPRs.) |
Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 431 | for (auto I = std::next(MF.begin()), E = MF.end(); I != E; ++I) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 432 | I->addLiveIn(SystemZ::R11D); |
| 433 | } |
| 434 | |
Ulrich Weigand | 8b19be4 | 2018-03-02 20:40:11 +0000 | [diff] [blame] | 435 | // Skip over the FPR/VR saves. |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 436 | SmallVector<unsigned, 8> CFIIndexes; |
Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 437 | for (auto &Save : CSI) { |
| 438 | unsigned Reg = Save.getReg(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 439 | if (SystemZ::FP64BitRegClass.contains(Reg)) { |
| 440 | if (MBBI != MBB.end() && |
| 441 | (MBBI->getOpcode() == SystemZ::STD || |
| 442 | MBBI->getOpcode() == SystemZ::STDY)) |
| 443 | ++MBBI; |
| 444 | else |
| 445 | llvm_unreachable("Couldn't skip over FPR save"); |
Ulrich Weigand | 8b19be4 | 2018-03-02 20:40:11 +0000 | [diff] [blame] | 446 | } else if (SystemZ::VR128BitRegClass.contains(Reg)) { |
| 447 | if (MBBI != MBB.end() && |
| 448 | MBBI->getOpcode() == SystemZ::VST) |
| 449 | ++MBBI; |
| 450 | else |
| 451 | llvm_unreachable("Couldn't skip over VR save"); |
| 452 | } else |
| 453 | continue; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 454 | |
Ulrich Weigand | 8b19be4 | 2018-03-02 20:40:11 +0000 | [diff] [blame] | 455 | // Add CFI for the this save. |
| 456 | unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true); |
| 457 | unsigned IgnoredFrameReg; |
| 458 | int64_t Offset = |
| 459 | getFrameIndexReference(MF, Save.getFrameIdx(), IgnoredFrameReg); |
James Y Knight | 5567baf | 2015-08-15 02:32:35 +0000 | [diff] [blame] | 460 | |
Ulrich Weigand | 8b19be4 | 2018-03-02 20:40:11 +0000 | [diff] [blame] | 461 | unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset( |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 462 | nullptr, DwarfReg, SPOffsetFromCFA + Offset)); |
Ulrich Weigand | 8b19be4 | 2018-03-02 20:40:11 +0000 | [diff] [blame] | 463 | CFIIndexes.push_back(CFIIndex); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 464 | } |
Ulrich Weigand | 8b19be4 | 2018-03-02 20:40:11 +0000 | [diff] [blame] | 465 | // Complete the CFI for the FPR/VR saves, modelling them as taking effect |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 466 | // after the last save. |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 467 | for (auto CFIIndex : CFIIndexes) { |
| 468 | BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) |
| 469 | .addCFIIndex(CFIIndex); |
| 470 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | void SystemZFrameLowering::emitEpilogue(MachineFunction &MF, |
| 474 | MachineBasicBlock &MBB) const { |
| 475 | MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 476 | auto *ZII = |
| 477 | static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 478 | SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); |
Ulrich Weigand | 3206388 | 2018-03-02 20:38:41 +0000 | [diff] [blame] | 479 | MachineFrameInfo &MFFrame = MF.getFrameInfo(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 480 | |
| 481 | // Skip the return instruction. |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 482 | assert(MBBI->isReturn() && "Can only insert epilogue into returning blocks"); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 483 | |
Ulrich Weigand | 3206388 | 2018-03-02 20:38:41 +0000 | [diff] [blame] | 484 | uint64_t StackSize = MFFrame.getStackSize(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 485 | if (ZFI->getLowSavedGPR()) { |
| 486 | --MBBI; |
| 487 | unsigned Opcode = MBBI->getOpcode(); |
| 488 | if (Opcode != SystemZ::LMG) |
| 489 | llvm_unreachable("Expected to see callee-save register restore code"); |
| 490 | |
| 491 | unsigned AddrOpNo = 2; |
| 492 | DebugLoc DL = MBBI->getDebugLoc(); |
| 493 | uint64_t Offset = StackSize + MBBI->getOperand(AddrOpNo + 1).getImm(); |
| 494 | unsigned NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset); |
| 495 | |
| 496 | // If the offset is too large, use the largest stack-aligned offset |
| 497 | // and add the rest to the base register (the stack or frame pointer). |
| 498 | if (!NewOpcode) { |
| 499 | uint64_t NumBytes = Offset - 0x7fff8; |
| 500 | emitIncrement(MBB, MBBI, DL, MBBI->getOperand(AddrOpNo).getReg(), |
| 501 | NumBytes, ZII); |
| 502 | Offset -= NumBytes; |
| 503 | NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset); |
| 504 | assert(NewOpcode && "No restore instruction available"); |
| 505 | } |
| 506 | |
| 507 | MBBI->setDesc(ZII->get(NewOpcode)); |
| 508 | MBBI->getOperand(AddrOpNo + 1).ChangeToImmediate(Offset); |
| 509 | } else if (StackSize) { |
| 510 | DebugLoc DL = MBBI->getDebugLoc(); |
| 511 | emitIncrement(MBB, MBBI, DL, SystemZ::R15D, StackSize, ZII); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | bool SystemZFrameLowering::hasFP(const MachineFunction &MF) const { |
| 516 | return (MF.getTarget().Options.DisableFramePointerElim(MF) || |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 517 | MF.getFrameInfo().hasVarSizedObjects() || |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 518 | MF.getInfo<SystemZMachineFunctionInfo>()->getManipulatesSP()); |
| 519 | } |
| 520 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 521 | bool |
| 522 | SystemZFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { |
| 523 | // The ABI requires us to allocate 160 bytes of stack space for the callee, |
| 524 | // with any outgoing stack arguments being placed above that. It seems |
| 525 | // better to make that area a permanent feature of the frame even if |
| 526 | // we're using a frame pointer. |
| 527 | return true; |
| 528 | } |
| 529 | |
Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame] | 530 | MachineBasicBlock::iterator SystemZFrameLowering:: |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 531 | eliminateCallFramePseudoInstr(MachineFunction &MF, |
| 532 | MachineBasicBlock &MBB, |
| 533 | MachineBasicBlock::iterator MI) const { |
| 534 | switch (MI->getOpcode()) { |
| 535 | case SystemZ::ADJCALLSTACKDOWN: |
| 536 | case SystemZ::ADJCALLSTACKUP: |
| 537 | assert(hasReservedCallFrame(MF) && |
| 538 | "ADJSTACKDOWN and ADJSTACKUP should be no-ops"); |
Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame] | 539 | return MBB.erase(MI); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 540 | break; |
| 541 | |
| 542 | default: |
| 543 | llvm_unreachable("Unexpected call frame instruction"); |
| 544 | } |
| 545 | } |