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