1 //===-- SystemZFrameLowering.cpp - Frame lowering for SystemZ -------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "SystemZFrameLowering.h" 10 #include "SystemZCallingConv.h" 11 #include "SystemZInstrBuilder.h" 12 #include "SystemZInstrInfo.h" 13 #include "SystemZMachineFunctionInfo.h" 14 #include "SystemZRegisterInfo.h" 15 #include "SystemZSubtarget.h" 16 #include "llvm/CodeGen/MachineModuleInfo.h" 17 #include "llvm/CodeGen/MachineRegisterInfo.h" 18 #include "llvm/CodeGen/RegisterScavenging.h" 19 #include "llvm/IR/Function.h" 20 21 using namespace llvm; 22 23 namespace { 24 // The ABI-defined register save slots, relative to the CFA (i.e. 25 // incoming stack pointer + SystemZMC::CallFrameSize). 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 47 48 SystemZFrameLowering::SystemZFrameLowering() 49 : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, Align(8), 50 0, Align(8), false /* StackRealignable */), 51 RegSpillOffsets(0) { 52 // Due to the SystemZ ABI, the DWARF CFA (Canonical Frame Address) is not 53 // equal to the incoming stack pointer, but to incoming stack pointer plus 54 // 160. Instead of using a Local Area Offset, the Register save area will 55 // be occupied by fixed frame objects, and all offsets are actually 56 // relative to CFA. 57 58 // Create a mapping from register number to save slot offset. 59 // These offsets are relative to the start of the register save area. 60 RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS); 61 for (unsigned I = 0, E = array_lengthof(SpillOffsetTable); I != E; ++I) 62 RegSpillOffsets[SpillOffsetTable[I].Reg] = SpillOffsetTable[I].Offset; 63 } 64 65 static bool usePackedStack(MachineFunction &MF) { 66 bool HasPackedStackAttr = MF.getFunction().hasFnAttribute("packed-stack"); 67 bool IsVarArg = MF.getFunction().isVarArg(); 68 bool CallConv = MF.getFunction().getCallingConv() != CallingConv::GHC; 69 bool BackChain = MF.getFunction().hasFnAttribute("backchain"); 70 bool FramAddressTaken = MF.getFrameInfo().isFrameAddressTaken(); 71 if (HasPackedStackAttr && BackChain) 72 report_fatal_error("packed-stack with backchain is currently unsupported."); 73 return HasPackedStackAttr && !IsVarArg && CallConv && !BackChain && 74 !FramAddressTaken; 75 } 76 77 bool SystemZFrameLowering:: 78 assignCalleeSavedSpillSlots(MachineFunction &MF, 79 const TargetRegisterInfo *TRI, 80 std::vector<CalleeSavedInfo> &CSI) const { 81 SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); 82 MachineFrameInfo &MFFrame = MF.getFrameInfo(); 83 bool IsVarArg = MF.getFunction().isVarArg(); 84 if (CSI.empty()) 85 return true; // Early exit if no callee saved registers are modified! 86 87 unsigned LowGPR = 0; 88 unsigned HighGPR = SystemZ::R15D; 89 int StartSPOffset = SystemZMC::CallFrameSize; 90 int CurrOffset; 91 if (!usePackedStack(MF)) { 92 for (auto &CS : CSI) { 93 unsigned Reg = CS.getReg(); 94 int Offset = RegSpillOffsets[Reg]; 95 if (Offset) { 96 if (SystemZ::GR64BitRegClass.contains(Reg) && StartSPOffset > Offset) { 97 LowGPR = Reg; 98 StartSPOffset = Offset; 99 } 100 Offset -= SystemZMC::CallFrameSize; 101 int FrameIdx = MFFrame.CreateFixedSpillStackObject(8, Offset); 102 CS.setFrameIdx(FrameIdx); 103 } else 104 CS.setFrameIdx(INT32_MAX); 105 } 106 107 // Save the range of call-saved registers, for use by the 108 // prologue/epilogue inserters. 109 ZFI->setRestoreGPRRegs(LowGPR, HighGPR, StartSPOffset); 110 if (IsVarArg) { 111 // Also save the GPR varargs, if any. R6D is call-saved, so would 112 // already be included, but we also need to handle the call-clobbered 113 // argument registers. 114 unsigned FirstGPR = ZFI->getVarArgsFirstGPR(); 115 if (FirstGPR < SystemZ::NumArgGPRs) { 116 unsigned Reg = SystemZ::ArgGPRs[FirstGPR]; 117 int Offset = RegSpillOffsets[Reg]; 118 if (StartSPOffset > Offset) { 119 LowGPR = Reg; StartSPOffset = Offset; 120 } 121 } 122 } 123 ZFI->setSpillGPRRegs(LowGPR, HighGPR, StartSPOffset); 124 125 CurrOffset = -SystemZMC::CallFrameSize; 126 } else { 127 // Packed stack: put all the GPRs at the top of the Register save area. 128 uint32_t LowGR64Num = UINT32_MAX; 129 for (auto &CS : CSI) { 130 unsigned Reg = CS.getReg(); 131 if (SystemZ::GR64BitRegClass.contains(Reg)) { 132 unsigned GR64Num = SystemZMC::getFirstReg(Reg); 133 int Offset = -8 * (15 - GR64Num + 1); 134 if (LowGR64Num > GR64Num) { 135 LowGR64Num = GR64Num; 136 StartSPOffset = SystemZMC::CallFrameSize + Offset; 137 } 138 int FrameIdx = MFFrame.CreateFixedSpillStackObject(8, Offset); 139 CS.setFrameIdx(FrameIdx); 140 } else 141 CS.setFrameIdx(INT32_MAX); 142 } 143 if (LowGR64Num < UINT32_MAX) 144 LowGPR = SystemZMC::GR64Regs[LowGR64Num]; 145 146 // Save the range of call-saved registers, for use by the 147 // prologue/epilogue inserters. 148 ZFI->setRestoreGPRRegs(LowGPR, HighGPR, StartSPOffset); 149 ZFI->setSpillGPRRegs(LowGPR, HighGPR, StartSPOffset); 150 151 CurrOffset = LowGPR ? -(SystemZMC::CallFrameSize - StartSPOffset) : 0; 152 } 153 154 // Create fixed stack objects for the remaining registers. 155 for (auto &CS : CSI) { 156 if (CS.getFrameIdx() != INT32_MAX) 157 continue; 158 unsigned Reg = CS.getReg(); 159 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); 160 unsigned Size = TRI->getSpillSize(*RC); 161 CurrOffset -= Size; 162 assert(CurrOffset % 8 == 0 && 163 "8-byte alignment required for for all register save slots"); 164 int FrameIdx = MFFrame.CreateFixedSpillStackObject(Size, CurrOffset); 165 CS.setFrameIdx(FrameIdx); 166 } 167 168 return true; 169 } 170 171 void SystemZFrameLowering::determineCalleeSaves(MachineFunction &MF, 172 BitVector &SavedRegs, 173 RegScavenger *RS) const { 174 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS); 175 176 MachineFrameInfo &MFFrame = MF.getFrameInfo(); 177 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 178 bool HasFP = hasFP(MF); 179 SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>(); 180 bool IsVarArg = MF.getFunction().isVarArg(); 181 182 // va_start stores incoming FPR varargs in the normal way, but delegates 183 // the saving of incoming GPR varargs to spillCalleeSavedRegisters(). 184 // Record these pending uses, which typically include the call-saved 185 // argument register R6D. 186 if (IsVarArg) 187 for (unsigned I = MFI->getVarArgsFirstGPR(); I < SystemZ::NumArgGPRs; ++I) 188 SavedRegs.set(SystemZ::ArgGPRs[I]); 189 190 // If there are any landing pads, entering them will modify r6/r7. 191 if (!MF.getLandingPads().empty()) { 192 SavedRegs.set(SystemZ::R6D); 193 SavedRegs.set(SystemZ::R7D); 194 } 195 196 // If the function requires a frame pointer, record that the hard 197 // frame pointer will be clobbered. 198 if (HasFP) 199 SavedRegs.set(SystemZ::R11D); 200 201 // If the function calls other functions, record that the return 202 // address register will be clobbered. 203 if (MFFrame.hasCalls()) 204 SavedRegs.set(SystemZ::R14D); 205 206 // If we are saving GPRs other than the stack pointer, we might as well 207 // save and restore the stack pointer at the same time, via STMG and LMG. 208 // This allows the deallocation to be done by the LMG, rather than needing 209 // a separate %r15 addition. 210 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF); 211 for (unsigned I = 0; CSRegs[I]; ++I) { 212 unsigned Reg = CSRegs[I]; 213 if (SystemZ::GR64BitRegClass.contains(Reg) && SavedRegs.test(Reg)) { 214 SavedRegs.set(SystemZ::R15D); 215 break; 216 } 217 } 218 } 219 220 // Add GPR64 to the save instruction being built by MIB, which is in basic 221 // block MBB. IsImplicit says whether this is an explicit operand to the 222 // instruction, or an implicit one that comes between the explicit start 223 // and end registers. 224 static void addSavedGPR(MachineBasicBlock &MBB, MachineInstrBuilder &MIB, 225 unsigned GPR64, bool IsImplicit) { 226 const TargetRegisterInfo *RI = 227 MBB.getParent()->getSubtarget().getRegisterInfo(); 228 Register GPR32 = RI->getSubReg(GPR64, SystemZ::subreg_l32); 229 bool IsLive = MBB.isLiveIn(GPR64) || MBB.isLiveIn(GPR32); 230 if (!IsLive || !IsImplicit) { 231 MIB.addReg(GPR64, getImplRegState(IsImplicit) | getKillRegState(!IsLive)); 232 if (!IsLive) 233 MBB.addLiveIn(GPR64); 234 } 235 } 236 237 bool SystemZFrameLowering::spillCalleeSavedRegisters( 238 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, 239 ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const { 240 if (CSI.empty()) 241 return false; 242 243 MachineFunction &MF = *MBB.getParent(); 244 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); 245 SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); 246 bool IsVarArg = MF.getFunction().isVarArg(); 247 DebugLoc DL; 248 249 // Save GPRs 250 SystemZ::GPRRegs SpillGPRs = ZFI->getSpillGPRRegs(); 251 if (SpillGPRs.LowGPR) { 252 assert(SpillGPRs.LowGPR != SpillGPRs.HighGPR && 253 "Should be saving %r15 and something else"); 254 255 // Build an STMG instruction. 256 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STMG)); 257 258 // Add the explicit register operands. 259 addSavedGPR(MBB, MIB, SpillGPRs.LowGPR, false); 260 addSavedGPR(MBB, MIB, SpillGPRs.HighGPR, false); 261 262 // Add the address. 263 MIB.addReg(SystemZ::R15D).addImm(SpillGPRs.GPROffset); 264 265 // Make sure all call-saved GPRs are included as operands and are 266 // marked as live on entry. 267 for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 268 unsigned Reg = CSI[I].getReg(); 269 if (SystemZ::GR64BitRegClass.contains(Reg)) 270 addSavedGPR(MBB, MIB, Reg, true); 271 } 272 273 // ...likewise GPR varargs. 274 if (IsVarArg) 275 for (unsigned I = ZFI->getVarArgsFirstGPR(); I < SystemZ::NumArgGPRs; ++I) 276 addSavedGPR(MBB, MIB, SystemZ::ArgGPRs[I], true); 277 } 278 279 // Save FPRs/VRs in the normal TargetInstrInfo way. 280 for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 281 unsigned Reg = CSI[I].getReg(); 282 if (SystemZ::FP64BitRegClass.contains(Reg)) { 283 MBB.addLiveIn(Reg); 284 TII->storeRegToStackSlot(MBB, MBBI, Reg, true, CSI[I].getFrameIdx(), 285 &SystemZ::FP64BitRegClass, TRI); 286 } 287 if (SystemZ::VR128BitRegClass.contains(Reg)) { 288 MBB.addLiveIn(Reg); 289 TII->storeRegToStackSlot(MBB, MBBI, Reg, true, CSI[I].getFrameIdx(), 290 &SystemZ::VR128BitRegClass, TRI); 291 } 292 } 293 294 return true; 295 } 296 297 bool SystemZFrameLowering:: 298 restoreCalleeSavedRegisters(MachineBasicBlock &MBB, 299 MachineBasicBlock::iterator MBBI, 300 std::vector<CalleeSavedInfo> &CSI, 301 const TargetRegisterInfo *TRI) const { 302 if (CSI.empty()) 303 return false; 304 305 MachineFunction &MF = *MBB.getParent(); 306 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); 307 SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); 308 bool HasFP = hasFP(MF); 309 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 310 311 // Restore FPRs/VRs in the normal TargetInstrInfo way. 312 for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 313 unsigned Reg = CSI[I].getReg(); 314 if (SystemZ::FP64BitRegClass.contains(Reg)) 315 TII->loadRegFromStackSlot(MBB, MBBI, Reg, CSI[I].getFrameIdx(), 316 &SystemZ::FP64BitRegClass, TRI); 317 if (SystemZ::VR128BitRegClass.contains(Reg)) 318 TII->loadRegFromStackSlot(MBB, MBBI, Reg, CSI[I].getFrameIdx(), 319 &SystemZ::VR128BitRegClass, TRI); 320 } 321 322 // Restore call-saved GPRs (but not call-clobbered varargs, which at 323 // this point might hold return values). 324 SystemZ::GPRRegs RestoreGPRs = ZFI->getRestoreGPRRegs(); 325 if (RestoreGPRs.LowGPR) { 326 // If we saved any of %r2-%r5 as varargs, we should also be saving 327 // and restoring %r6. If we're saving %r6 or above, we should be 328 // restoring it too. 329 assert(RestoreGPRs.LowGPR != RestoreGPRs.HighGPR && 330 "Should be loading %r15 and something else"); 331 332 // Build an LMG instruction. 333 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LMG)); 334 335 // Add the explicit register operands. 336 MIB.addReg(RestoreGPRs.LowGPR, RegState::Define); 337 MIB.addReg(RestoreGPRs.HighGPR, RegState::Define); 338 339 // Add the address. 340 MIB.addReg(HasFP ? SystemZ::R11D : SystemZ::R15D); 341 MIB.addImm(RestoreGPRs.GPROffset); 342 343 // Do a second scan adding regs as being defined by instruction 344 for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 345 unsigned Reg = CSI[I].getReg(); 346 if (Reg != RestoreGPRs.LowGPR && Reg != RestoreGPRs.HighGPR && 347 SystemZ::GR64BitRegClass.contains(Reg)) 348 MIB.addReg(Reg, RegState::ImplicitDefine); 349 } 350 } 351 352 return true; 353 } 354 355 void SystemZFrameLowering:: 356 processFunctionBeforeFrameFinalized(MachineFunction &MF, 357 RegScavenger *RS) const { 358 MachineFrameInfo &MFFrame = MF.getFrameInfo(); 359 360 if (!usePackedStack(MF)) 361 // Always create the full incoming register save area. 362 getOrCreateFramePointerSaveIndex(MF); 363 364 // Get the size of our stack frame to be allocated ... 365 uint64_t StackSize = (MFFrame.estimateStackSize(MF) + 366 SystemZMC::CallFrameSize); 367 // ... and the maximum offset we may need to reach into the 368 // caller's frame to access the save area or stack arguments. 369 int64_t MaxArgOffset = 0; 370 for (int I = MFFrame.getObjectIndexBegin(); I != 0; ++I) 371 if (MFFrame.getObjectOffset(I) >= 0) { 372 int64_t ArgOffset = MFFrame.getObjectOffset(I) + 373 MFFrame.getObjectSize(I); 374 MaxArgOffset = std::max(MaxArgOffset, ArgOffset); 375 } 376 377 uint64_t MaxReach = StackSize + MaxArgOffset; 378 if (!isUInt<12>(MaxReach)) { 379 // We may need register scavenging slots if some parts of the frame 380 // are outside the reach of an unsigned 12-bit displacement. 381 // Create 2 for the case where both addresses in an MVC are 382 // out of range. 383 RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, 8, false)); 384 RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, 8, false)); 385 } 386 } 387 388 // Emit instructions before MBBI (in MBB) to add NumBytes to Reg. 389 static void emitIncrement(MachineBasicBlock &MBB, 390 MachineBasicBlock::iterator &MBBI, 391 const DebugLoc &DL, 392 unsigned Reg, int64_t NumBytes, 393 const TargetInstrInfo *TII) { 394 while (NumBytes) { 395 unsigned Opcode; 396 int64_t ThisVal = NumBytes; 397 if (isInt<16>(NumBytes)) 398 Opcode = SystemZ::AGHI; 399 else { 400 Opcode = SystemZ::AGFI; 401 // Make sure we maintain 8-byte stack alignment. 402 int64_t MinVal = -uint64_t(1) << 31; 403 int64_t MaxVal = (int64_t(1) << 31) - 8; 404 if (ThisVal < MinVal) 405 ThisVal = MinVal; 406 else if (ThisVal > MaxVal) 407 ThisVal = MaxVal; 408 } 409 MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII->get(Opcode), Reg) 410 .addReg(Reg).addImm(ThisVal); 411 // The CC implicit def is dead. 412 MI->getOperand(3).setIsDead(); 413 NumBytes -= ThisVal; 414 } 415 } 416 417 void SystemZFrameLowering::emitPrologue(MachineFunction &MF, 418 MachineBasicBlock &MBB) const { 419 assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported"); 420 MachineFrameInfo &MFFrame = MF.getFrameInfo(); 421 auto *ZII = 422 static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo()); 423 SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); 424 MachineBasicBlock::iterator MBBI = MBB.begin(); 425 MachineModuleInfo &MMI = MF.getMMI(); 426 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo(); 427 const std::vector<CalleeSavedInfo> &CSI = MFFrame.getCalleeSavedInfo(); 428 bool HasFP = hasFP(MF); 429 430 // In GHC calling convention C stack space, including the ABI-defined 431 // 160-byte base area, is (de)allocated by GHC itself. This stack space may 432 // be used by LLVM as spill slots for the tail recursive GHC functions. Thus 433 // do not allocate stack space here, too. 434 if (MF.getFunction().getCallingConv() == CallingConv::GHC) { 435 if (MFFrame.getStackSize() > 2048 * sizeof(long)) { 436 report_fatal_error( 437 "Pre allocated stack space for GHC function is too small"); 438 } 439 if (HasFP) { 440 report_fatal_error( 441 "In GHC calling convention a frame pointer is not supported"); 442 } 443 MFFrame.setStackSize(MFFrame.getStackSize() + SystemZMC::CallFrameSize); 444 return; 445 } 446 447 // Debug location must be unknown since the first debug location is used 448 // to determine the end of the prologue. 449 DebugLoc DL; 450 451 // The current offset of the stack pointer from the CFA. 452 int64_t SPOffsetFromCFA = -SystemZMC::CFAOffsetFromInitialSP; 453 454 if (ZFI->getSpillGPRRegs().LowGPR) { 455 // Skip over the GPR saves. 456 if (MBBI != MBB.end() && MBBI->getOpcode() == SystemZ::STMG) 457 ++MBBI; 458 else 459 llvm_unreachable("Couldn't skip over GPR saves"); 460 461 // Add CFI for the GPR saves. 462 for (auto &Save : CSI) { 463 unsigned Reg = Save.getReg(); 464 if (SystemZ::GR64BitRegClass.contains(Reg)) { 465 int FI = Save.getFrameIdx(); 466 int64_t Offset = MFFrame.getObjectOffset(FI); 467 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset( 468 nullptr, MRI->getDwarfRegNum(Reg, true), Offset)); 469 BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) 470 .addCFIIndex(CFIIndex); 471 } 472 } 473 } 474 475 uint64_t StackSize = MFFrame.getStackSize(); 476 // We need to allocate the ABI-defined 160-byte base area whenever 477 // we allocate stack space for our own use and whenever we call another 478 // function. 479 bool HasStackObject = false; 480 for (unsigned i = 0, e = MFFrame.getObjectIndexEnd(); i != e; ++i) 481 if (!MFFrame.isDeadObjectIndex(i)) { 482 HasStackObject = true; 483 break; 484 } 485 if (HasStackObject || MFFrame.hasCalls()) 486 StackSize += SystemZMC::CallFrameSize; 487 // Don't allocate the incoming reg save area. 488 StackSize = StackSize > SystemZMC::CallFrameSize 489 ? StackSize - SystemZMC::CallFrameSize 490 : 0; 491 MFFrame.setStackSize(StackSize); 492 493 if (StackSize) { 494 // Determine if we want to store a backchain. 495 bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain"); 496 497 // If we need backchain, save current stack pointer. R1 is free at this 498 // point. 499 if (StoreBackchain) 500 BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR)) 501 .addReg(SystemZ::R1D, RegState::Define).addReg(SystemZ::R15D); 502 503 // Allocate StackSize bytes. 504 int64_t Delta = -int64_t(StackSize); 505 emitIncrement(MBB, MBBI, DL, SystemZ::R15D, Delta, ZII); 506 507 // Add CFI for the allocation. 508 unsigned CFIIndex = MF.addFrameInst( 509 MCCFIInstruction::createDefCfaOffset(nullptr, SPOffsetFromCFA + Delta)); 510 BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) 511 .addCFIIndex(CFIIndex); 512 SPOffsetFromCFA += Delta; 513 514 if (StoreBackchain) 515 BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::STG)) 516 .addReg(SystemZ::R1D, RegState::Kill).addReg(SystemZ::R15D).addImm(0) 517 .addReg(0); 518 } 519 520 if (HasFP) { 521 // Copy the base of the frame to R11. 522 BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR), SystemZ::R11D) 523 .addReg(SystemZ::R15D); 524 525 // Add CFI for the new frame location. 526 unsigned HardFP = MRI->getDwarfRegNum(SystemZ::R11D, true); 527 unsigned CFIIndex = MF.addFrameInst( 528 MCCFIInstruction::createDefCfaRegister(nullptr, HardFP)); 529 BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) 530 .addCFIIndex(CFIIndex); 531 532 // Mark the FramePtr as live at the beginning of every block except 533 // the entry block. (We'll have marked R11 as live on entry when 534 // saving the GPRs.) 535 for (auto I = std::next(MF.begin()), E = MF.end(); I != E; ++I) 536 I->addLiveIn(SystemZ::R11D); 537 } 538 539 // Skip over the FPR/VR saves. 540 SmallVector<unsigned, 8> CFIIndexes; 541 for (auto &Save : CSI) { 542 unsigned Reg = Save.getReg(); 543 if (SystemZ::FP64BitRegClass.contains(Reg)) { 544 if (MBBI != MBB.end() && 545 (MBBI->getOpcode() == SystemZ::STD || 546 MBBI->getOpcode() == SystemZ::STDY)) 547 ++MBBI; 548 else 549 llvm_unreachable("Couldn't skip over FPR save"); 550 } else if (SystemZ::VR128BitRegClass.contains(Reg)) { 551 if (MBBI != MBB.end() && 552 MBBI->getOpcode() == SystemZ::VST) 553 ++MBBI; 554 else 555 llvm_unreachable("Couldn't skip over VR save"); 556 } else 557 continue; 558 559 // Add CFI for the this save. 560 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true); 561 unsigned IgnoredFrameReg; 562 int64_t Offset = 563 getFrameIndexReference(MF, Save.getFrameIdx(), IgnoredFrameReg); 564 565 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset( 566 nullptr, DwarfReg, SPOffsetFromCFA + Offset)); 567 CFIIndexes.push_back(CFIIndex); 568 } 569 // Complete the CFI for the FPR/VR saves, modelling them as taking effect 570 // after the last save. 571 for (auto CFIIndex : CFIIndexes) { 572 BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION)) 573 .addCFIIndex(CFIIndex); 574 } 575 } 576 577 void SystemZFrameLowering::emitEpilogue(MachineFunction &MF, 578 MachineBasicBlock &MBB) const { 579 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 580 auto *ZII = 581 static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo()); 582 SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); 583 MachineFrameInfo &MFFrame = MF.getFrameInfo(); 584 585 // See SystemZFrameLowering::emitPrologue 586 if (MF.getFunction().getCallingConv() == CallingConv::GHC) 587 return; 588 589 // Skip the return instruction. 590 assert(MBBI->isReturn() && "Can only insert epilogue into returning blocks"); 591 592 uint64_t StackSize = MFFrame.getStackSize(); 593 if (ZFI->getRestoreGPRRegs().LowGPR) { 594 --MBBI; 595 unsigned Opcode = MBBI->getOpcode(); 596 if (Opcode != SystemZ::LMG) 597 llvm_unreachable("Expected to see callee-save register restore code"); 598 599 unsigned AddrOpNo = 2; 600 DebugLoc DL = MBBI->getDebugLoc(); 601 uint64_t Offset = StackSize + MBBI->getOperand(AddrOpNo + 1).getImm(); 602 unsigned NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset); 603 604 // If the offset is too large, use the largest stack-aligned offset 605 // and add the rest to the base register (the stack or frame pointer). 606 if (!NewOpcode) { 607 uint64_t NumBytes = Offset - 0x7fff8; 608 emitIncrement(MBB, MBBI, DL, MBBI->getOperand(AddrOpNo).getReg(), 609 NumBytes, ZII); 610 Offset -= NumBytes; 611 NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset); 612 assert(NewOpcode && "No restore instruction available"); 613 } 614 615 MBBI->setDesc(ZII->get(NewOpcode)); 616 MBBI->getOperand(AddrOpNo + 1).ChangeToImmediate(Offset); 617 } else if (StackSize) { 618 DebugLoc DL = MBBI->getDebugLoc(); 619 emitIncrement(MBB, MBBI, DL, SystemZ::R15D, StackSize, ZII); 620 } 621 } 622 623 bool SystemZFrameLowering::hasFP(const MachineFunction &MF) const { 624 return (MF.getTarget().Options.DisableFramePointerElim(MF) || 625 MF.getFrameInfo().hasVarSizedObjects() || 626 MF.getInfo<SystemZMachineFunctionInfo>()->getManipulatesSP()); 627 } 628 629 bool 630 SystemZFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { 631 // The ABI requires us to allocate 160 bytes of stack space for the callee, 632 // with any outgoing stack arguments being placed above that. It seems 633 // better to make that area a permanent feature of the frame even if 634 // we're using a frame pointer. 635 return true; 636 } 637 638 int SystemZFrameLowering::getFrameIndexReference(const MachineFunction &MF, 639 int FI, 640 unsigned &FrameReg) const { 641 // Our incoming SP is actually SystemZMC::CallFrameSize below the CFA, so 642 // add that difference here. 643 int64_t Offset = 644 TargetFrameLowering::getFrameIndexReference(MF, FI, FrameReg); 645 return Offset + SystemZMC::CallFrameSize; 646 } 647 648 MachineBasicBlock::iterator SystemZFrameLowering:: 649 eliminateCallFramePseudoInstr(MachineFunction &MF, 650 MachineBasicBlock &MBB, 651 MachineBasicBlock::iterator MI) const { 652 switch (MI->getOpcode()) { 653 case SystemZ::ADJCALLSTACKDOWN: 654 case SystemZ::ADJCALLSTACKUP: 655 assert(hasReservedCallFrame(MF) && 656 "ADJSTACKDOWN and ADJSTACKUP should be no-ops"); 657 return MBB.erase(MI); 658 break; 659 660 default: 661 llvm_unreachable("Unexpected call frame instruction"); 662 } 663 } 664 665 int SystemZFrameLowering:: 666 getOrCreateFramePointerSaveIndex(MachineFunction &MF) const { 667 SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>(); 668 int FI = ZFI->getFramePointerSaveIndex(); 669 if (!FI) { 670 MachineFrameInfo &MFFrame = MF.getFrameInfo(); 671 FI = MFFrame.CreateFixedObject(8, -SystemZMC::CallFrameSize, false); 672 ZFI->setFramePointerSaveIndex(FI); 673 } 674 return FI; 675 } 676