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