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