1 //===-- PPCFrameLowering.cpp - PPC Frame Information ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the PPC implementation of TargetFrameLowering class.
12 //===----------------------------------------------------------------------===//
14 #include "PPCFrameLowering.h"
15 #include "PPCInstrBuilder.h"
16 #include "PPCInstrInfo.h"
17 #include "PPCMachineFunctionInfo.h"
18 #include "PPCSubtarget.h"
19 #include "PPCTargetMachine.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineModuleInfo.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/RegisterScavenging.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/Target/TargetOptions.h"
31 /// VRRegNo - Map from a numbered VR register to its enum value.
33 static const MCPhysReg VRRegNo[] = {
34 PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
35 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15,
36 PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23,
37 PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31
40 static unsigned computeReturnSaveOffset(const PPCSubtarget &STI) {
41 if (STI.isDarwinABI())
42 return STI.isPPC64() ? 16 : 8;
44 return STI.isPPC64() ? 16 : 4;
47 static unsigned computeTOCSaveOffset(const PPCSubtarget &STI) {
48 return STI.isELFv2ABI() ? 24 : 40;
51 static unsigned computeFramePointerSaveOffset(const PPCSubtarget &STI) {
52 // For the Darwin ABI:
53 // We cannot use the TOC save slot (offset +20) in the PowerPC linkage area
54 // for saving the frame pointer (if needed.) While the published ABI has
55 // not used this slot since at least MacOSX 10.2, there is older code
56 // around that does use it, and that needs to continue to work.
57 if (STI.isDarwinABI())
58 return STI.isPPC64() ? -8U : -4U;
60 // SVR4 ABI: First slot in the general register save area.
61 return STI.isPPC64() ? -8U : -4U;
64 static unsigned computeLinkageSize(const PPCSubtarget &STI) {
65 if (STI.isDarwinABI() || STI.isPPC64())
66 return (STI.isELFv2ABI() ? 4 : 6) * (STI.isPPC64() ? 8 : 4);
72 static unsigned computeBasePointerSaveOffset(const PPCSubtarget &STI) {
73 if (STI.isDarwinABI())
74 return STI.isPPC64() ? -16U : -8U;
76 // SVR4 ABI: First slot in the general register save area.
79 : STI.getTargetMachine().isPositionIndependent() ? -12U : -8U;
82 PPCFrameLowering::PPCFrameLowering(const PPCSubtarget &STI)
83 : TargetFrameLowering(TargetFrameLowering::StackGrowsDown,
84 STI.getPlatformStackAlignment(), 0),
85 Subtarget(STI), ReturnSaveOffset(computeReturnSaveOffset(Subtarget)),
86 TOCSaveOffset(computeTOCSaveOffset(Subtarget)),
87 FramePointerSaveOffset(computeFramePointerSaveOffset(Subtarget)),
88 LinkageSize(computeLinkageSize(Subtarget)),
89 BasePointerSaveOffset(computeBasePointerSaveOffset(STI)) {}
91 // With the SVR4 ABI, callee-saved registers have fixed offsets on the stack.
92 const PPCFrameLowering::SpillSlot *PPCFrameLowering::getCalleeSavedSpillSlots(
93 unsigned &NumEntries) const {
94 if (Subtarget.isDarwinABI()) {
96 if (Subtarget.isPPC64()) {
97 static const SpillSlot darwin64Offsets = {PPC::X31, -8};
98 return &darwin64Offsets;
100 static const SpillSlot darwinOffsets = {PPC::R31, -4};
101 return &darwinOffsets;
105 // Early exit if not using the SVR4 ABI.
106 if (!Subtarget.isSVR4ABI()) {
111 // Note that the offsets here overlap, but this is fixed up in
112 // processFunctionBeforeFrameFinalized.
114 static const SpillSlot Offsets[] = {
115 // Floating-point register save area offsets.
135 // General register save area offsets.
155 // CR save area offset. We map each of the nonvolatile CR fields
156 // to the slot for CR2, which is the first of the nonvolatile CR
157 // fields to be assigned, so that we only allocate one save slot.
158 // See PPCRegisterInfo::hasReservedSpillSlot() for more information.
161 // VRSAVE save area offset.
164 // Vector register save area
178 static const SpillSlot Offsets64[] = {
179 // Floating-point register save area offsets.
199 // General register save area offsets.
219 // VRSAVE save area offset.
222 // Vector register save area
236 if (Subtarget.isPPC64()) {
237 NumEntries = array_lengthof(Offsets64);
241 NumEntries = array_lengthof(Offsets);
247 /// RemoveVRSaveCode - We have found that this function does not need any code
248 /// to manipulate the VRSAVE register, even though it uses vector registers.
249 /// This can happen when the only registers used are known to be live in or out
250 /// of the function. Remove all of the VRSAVE related code from the function.
251 /// FIXME: The removal of the code results in a compile failure at -O0 when the
252 /// function contains a function call, as the GPR containing original VRSAVE
253 /// contents is spilled and reloaded around the call. Without the prolog code,
254 /// the spill instruction refers to an undefined register. This code needs
255 /// to account for all uses of that GPR.
256 static void RemoveVRSaveCode(MachineInstr &MI) {
257 MachineBasicBlock *Entry = MI.getParent();
258 MachineFunction *MF = Entry->getParent();
260 // We know that the MTVRSAVE instruction immediately follows MI. Remove it.
261 MachineBasicBlock::iterator MBBI = MI;
263 assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE);
264 MBBI->eraseFromParent();
266 bool RemovedAllMTVRSAVEs = true;
267 // See if we can find and remove the MTVRSAVE instruction from all of the
269 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) {
270 // If last instruction is a return instruction, add an epilogue
271 if (I->isReturnBlock()) {
272 bool FoundIt = false;
273 for (MBBI = I->end(); MBBI != I->begin(); ) {
275 if (MBBI->getOpcode() == PPC::MTVRSAVE) {
276 MBBI->eraseFromParent(); // remove it.
281 RemovedAllMTVRSAVEs &= FoundIt;
285 // If we found and removed all MTVRSAVE instructions, remove the read of
287 if (RemovedAllMTVRSAVEs) {
289 assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?");
291 assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?");
292 MBBI->eraseFromParent();
295 // Finally, nuke the UPDATE_VRSAVE.
296 MI.eraseFromParent();
299 // HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the
300 // instruction selector. Based on the vector registers that have been used,
301 // transform this into the appropriate ORI instruction.
302 static void HandleVRSaveUpdate(MachineInstr &MI, const TargetInstrInfo &TII) {
303 MachineFunction *MF = MI.getParent()->getParent();
304 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
305 DebugLoc dl = MI.getDebugLoc();
307 const MachineRegisterInfo &MRI = MF->getRegInfo();
308 unsigned UsedRegMask = 0;
309 for (unsigned i = 0; i != 32; ++i)
310 if (MRI.isPhysRegModified(VRRegNo[i]))
311 UsedRegMask |= 1 << (31-i);
313 // Live in and live out values already must be in the mask, so don't bother
315 for (MachineRegisterInfo::livein_iterator
316 I = MF->getRegInfo().livein_begin(),
317 E = MF->getRegInfo().livein_end(); I != E; ++I) {
318 unsigned RegNo = TRI->getEncodingValue(I->first);
319 if (VRRegNo[RegNo] == I->first) // If this really is a vector reg.
320 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
323 // Live out registers appear as use operands on return instructions.
324 for (MachineFunction::const_iterator BI = MF->begin(), BE = MF->end();
325 UsedRegMask != 0 && BI != BE; ++BI) {
326 const MachineBasicBlock &MBB = *BI;
327 if (!MBB.isReturnBlock())
329 const MachineInstr &Ret = MBB.back();
330 for (unsigned I = 0, E = Ret.getNumOperands(); I != E; ++I) {
331 const MachineOperand &MO = Ret.getOperand(I);
332 if (!MO.isReg() || !PPC::VRRCRegClass.contains(MO.getReg()))
334 unsigned RegNo = TRI->getEncodingValue(MO.getReg());
335 UsedRegMask &= ~(1 << (31-RegNo));
339 // If no registers are used, turn this into a copy.
340 if (UsedRegMask == 0) {
341 // Remove all VRSAVE code.
342 RemoveVRSaveCode(MI);
346 unsigned SrcReg = MI.getOperand(1).getReg();
347 unsigned DstReg = MI.getOperand(0).getReg();
349 if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
350 if (DstReg != SrcReg)
351 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
353 .addImm(UsedRegMask);
355 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
356 .addReg(SrcReg, RegState::Kill)
357 .addImm(UsedRegMask);
358 } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
359 if (DstReg != SrcReg)
360 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
362 .addImm(UsedRegMask >> 16);
364 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
365 .addReg(SrcReg, RegState::Kill)
366 .addImm(UsedRegMask >> 16);
368 if (DstReg != SrcReg)
369 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
371 .addImm(UsedRegMask >> 16);
373 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
374 .addReg(SrcReg, RegState::Kill)
375 .addImm(UsedRegMask >> 16);
377 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
378 .addReg(DstReg, RegState::Kill)
379 .addImm(UsedRegMask & 0xFFFF);
382 // Remove the old UPDATE_VRSAVE instruction.
383 MI.eraseFromParent();
386 static bool spillsCR(const MachineFunction &MF) {
387 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
388 return FuncInfo->isCRSpilled();
391 static bool spillsVRSAVE(const MachineFunction &MF) {
392 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
393 return FuncInfo->isVRSAVESpilled();
396 static bool hasSpills(const MachineFunction &MF) {
397 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
398 return FuncInfo->hasSpills();
401 static bool hasNonRISpills(const MachineFunction &MF) {
402 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
403 return FuncInfo->hasNonRISpills();
406 /// MustSaveLR - Return true if this function requires that we save the LR
407 /// register onto the stack in the prolog and restore it in the epilog of the
409 static bool MustSaveLR(const MachineFunction &MF, unsigned LR) {
410 const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>();
412 // We need a save/restore of LR if there is any def of LR (which is
413 // defined by calls, including the PIC setup sequence), or if there is
414 // some use of the LR stack slot (e.g. for builtin_return_address).
415 // (LR comes in 32 and 64 bit versions.)
416 MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR);
417 return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired();
420 /// determineFrameLayout - Determine the size of the frame and maximum call
422 unsigned PPCFrameLowering::determineFrameLayout(MachineFunction &MF,
424 bool UseEstimate) const {
425 MachineFrameInfo &MFI = MF.getFrameInfo();
427 // Get the number of bytes to allocate from the FrameInfo
429 UseEstimate ? MFI.estimateStackSize(MF) : MFI.getStackSize();
431 // Get stack alignments. The frame must be aligned to the greatest of these:
432 unsigned TargetAlign = getStackAlignment(); // alignment required per the ABI
433 unsigned MaxAlign = MFI.getMaxAlignment(); // algmt required by data in frame
434 unsigned AlignMask = std::max(MaxAlign, TargetAlign) - 1;
436 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
438 unsigned LR = RegInfo->getRARegister();
439 bool DisableRedZone = MF.getFunction()->hasFnAttribute(Attribute::NoRedZone);
440 bool CanUseRedZone = !MFI.hasVarSizedObjects() && // No dynamic alloca.
441 !MFI.adjustsStack() && // No calls.
442 !MustSaveLR(MF, LR) && // No need to save LR.
443 !RegInfo->hasBasePointer(MF); // No special alignment.
445 // Note: for PPC32 SVR4ABI (Non-DarwinABI), we can still generate stackless
446 // code if all local vars are reg-allocated.
447 bool FitsInRedZone = FrameSize <= Subtarget.getRedZoneSize();
449 // Check whether we can skip adjusting the stack pointer (by using red zone)
450 if (!DisableRedZone && CanUseRedZone && FitsInRedZone) {
457 // Get the maximum call frame size of all the calls.
458 unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
460 // Maximum call frame needs to be at least big enough for linkage area.
461 unsigned minCallFrameSize = getLinkageSize();
462 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
464 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
465 // that allocations will be aligned.
466 if (MFI.hasVarSizedObjects())
467 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
469 // Update maximum call frame size.
471 MFI.setMaxCallFrameSize(maxCallFrameSize);
473 // Include call frame size in total.
474 FrameSize += maxCallFrameSize;
476 // Make sure the frame is aligned.
477 FrameSize = (FrameSize + AlignMask) & ~AlignMask;
479 // Update frame info.
481 MFI.setStackSize(FrameSize);
486 // hasFP - Return true if the specified function actually has a dedicated frame
488 bool PPCFrameLowering::hasFP(const MachineFunction &MF) const {
489 const MachineFrameInfo &MFI = MF.getFrameInfo();
490 // FIXME: This is pretty much broken by design: hasFP() might be called really
491 // early, before the stack layout was calculated and thus hasFP() might return
492 // true or false here depending on the time of call.
493 return (MFI.getStackSize()) && needsFP(MF);
496 // needsFP - Return true if the specified function should have a dedicated frame
497 // pointer register. This is true if the function has variable sized allocas or
498 // if frame pointer elimination is disabled.
499 bool PPCFrameLowering::needsFP(const MachineFunction &MF) const {
500 const MachineFrameInfo &MFI = MF.getFrameInfo();
502 // Naked functions have no stack frame pushed, so we don't have a frame
504 if (MF.getFunction()->hasFnAttribute(Attribute::Naked))
507 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
508 MFI.hasVarSizedObjects() || MFI.hasStackMap() || MFI.hasPatchPoint() ||
509 (MF.getTarget().Options.GuaranteedTailCallOpt &&
510 MF.getInfo<PPCFunctionInfo>()->hasFastCall());
513 void PPCFrameLowering::replaceFPWithRealFP(MachineFunction &MF) const {
514 bool is31 = needsFP(MF);
515 unsigned FPReg = is31 ? PPC::R31 : PPC::R1;
516 unsigned FP8Reg = is31 ? PPC::X31 : PPC::X1;
518 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
519 bool HasBP = RegInfo->hasBasePointer(MF);
520 unsigned BPReg = HasBP ? (unsigned) RegInfo->getBaseRegister(MF) : FPReg;
521 unsigned BP8Reg = HasBP ? (unsigned) PPC::X30 : FP8Reg;
523 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
525 for (MachineBasicBlock::iterator MBBI = BI->end(); MBBI != BI->begin(); ) {
527 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
528 MachineOperand &MO = MBBI->getOperand(I);
532 switch (MO.getReg()) {
551 /* This function will do the following:
552 - If MBB is an entry or exit block, set SR1 and SR2 to R0 and R12
553 respectively (defaults recommended by the ABI) and return true
554 - If MBB is not an entry block, initialize the register scavenger and look
555 for available registers.
556 - If the defaults (R0/R12) are available, return true
557 - If TwoUniqueRegsRequired is set to true, it looks for two unique
558 registers. Otherwise, look for a single available register.
559 - If the required registers are found, set SR1 and SR2 and return true.
560 - If the required registers are not found, set SR2 or both SR1 and SR2 to
561 PPC::NoRegister and return false.
563 Note that if both SR1 and SR2 are valid parameters and TwoUniqueRegsRequired
564 is not set, this function will attempt to find two different registers, but
565 still return true if only one register is available (and set SR1 == SR2).
568 PPCFrameLowering::findScratchRegister(MachineBasicBlock *MBB,
570 bool TwoUniqueRegsRequired,
572 unsigned *SR2) const {
574 unsigned R0 = Subtarget.isPPC64() ? PPC::X0 : PPC::R0;
575 unsigned R12 = Subtarget.isPPC64() ? PPC::X12 : PPC::R12;
577 // Set the defaults for the two scratch registers.
582 assert (SR1 && "Asking for the second scratch register but not the first?");
586 // If MBB is an entry or exit block, use R0 and R12 as the scratch registers.
587 if ((UseAtEnd && MBB->isReturnBlock()) ||
588 (!UseAtEnd && (&MBB->getParent()->front() == MBB)))
591 RS.enterBasicBlock(*MBB);
593 if (UseAtEnd && !MBB->empty()) {
594 // The scratch register will be used at the end of the block, so must
595 // consider all registers used within the block
597 MachineBasicBlock::iterator MBBI = MBB->getFirstTerminator();
598 // If no terminator, back iterator up to previous instruction.
599 if (MBBI == MBB->end())
600 MBBI = std::prev(MBBI);
602 if (MBBI != MBB->begin())
606 // If the two registers are available, we're all good.
607 // Note that we only return here if both R0 and R12 are available because
608 // although the function may not require two unique registers, it may benefit
609 // from having two so we should try to provide them.
610 if (!RS.isRegUsed(R0) && !RS.isRegUsed(R12))
613 // Get the list of callee-saved registers for the target.
614 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
615 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(MBB->getParent());
617 // Get all the available registers in the block.
618 BitVector BV = RS.getRegsAvailable(Subtarget.isPPC64() ? &PPC::G8RCRegClass :
621 // We shouldn't use callee-saved registers as scratch registers as they may be
622 // available when looking for a candidate block for shrink wrapping but not
623 // available when the actual prologue/epilogue is being emitted because they
624 // were added as live-in to the prologue block by PrologueEpilogueInserter.
625 for (int i = 0; CSRegs[i]; ++i)
628 // Set the first scratch register to the first available one.
630 int FirstScratchReg = BV.find_first();
631 *SR1 = FirstScratchReg == -1 ? (unsigned)PPC::NoRegister : FirstScratchReg;
634 // If there is another one available, set the second scratch register to that.
635 // Otherwise, set it to either PPC::NoRegister if this function requires two
636 // or to whatever SR1 is set to if this function doesn't require two.
638 int SecondScratchReg = BV.find_next(*SR1);
639 if (SecondScratchReg != -1)
640 *SR2 = SecondScratchReg;
642 *SR2 = TwoUniqueRegsRequired ? (unsigned)PPC::NoRegister : *SR1;
645 // Now that we've done our best to provide both registers, double check
646 // whether we were unable to provide enough.
647 if (BV.count() < (TwoUniqueRegsRequired ? 2U : 1U))
653 // We need a scratch register for spilling LR and for spilling CR. By default,
654 // we use two scratch registers to hide latency. However, if only one scratch
655 // register is available, we can adjust for that by not overlapping the spill
656 // code. However, if we need to realign the stack (i.e. have a base pointer)
657 // and the stack frame is large, we need two scratch registers.
659 PPCFrameLowering::twoUniqueScratchRegsRequired(MachineBasicBlock *MBB) const {
660 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
661 MachineFunction &MF = *(MBB->getParent());
662 bool HasBP = RegInfo->hasBasePointer(MF);
663 unsigned FrameSize = determineFrameLayout(MF, false);
664 int NegFrameSize = -FrameSize;
665 bool IsLargeFrame = !isInt<16>(NegFrameSize);
666 MachineFrameInfo &MFI = MF.getFrameInfo();
667 unsigned MaxAlign = MFI.getMaxAlignment();
668 bool HasRedZone = Subtarget.isPPC64() || !Subtarget.isSVR4ABI();
670 return (IsLargeFrame || !HasRedZone) && HasBP && MaxAlign > 1;
673 bool PPCFrameLowering::canUseAsPrologue(const MachineBasicBlock &MBB) const {
674 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
676 return findScratchRegister(TmpMBB, false,
677 twoUniqueScratchRegsRequired(TmpMBB));
680 bool PPCFrameLowering::canUseAsEpilogue(const MachineBasicBlock &MBB) const {
681 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
683 return findScratchRegister(TmpMBB, true);
686 void PPCFrameLowering::emitPrologue(MachineFunction &MF,
687 MachineBasicBlock &MBB) const {
688 MachineBasicBlock::iterator MBBI = MBB.begin();
689 MachineFrameInfo &MFI = MF.getFrameInfo();
690 const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
691 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
693 MachineModuleInfo &MMI = MF.getMMI();
694 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
696 bool needsCFI = MMI.hasDebugInfo() ||
697 MF.getFunction()->needsUnwindTableEntry();
699 // Get processor type.
700 bool isPPC64 = Subtarget.isPPC64();
702 bool isSVR4ABI = Subtarget.isSVR4ABI();
703 bool isELFv2ABI = Subtarget.isELFv2ABI();
704 assert((Subtarget.isDarwinABI() || isSVR4ABI) &&
705 "Currently only Darwin and SVR4 ABIs are supported for PowerPC.");
707 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it,
710 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
711 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
712 HandleVRSaveUpdate(*MBBI, TII);
717 // Move MBBI back to the beginning of the prologue block.
720 // Work out frame sizes.
721 unsigned FrameSize = determineFrameLayout(MF);
722 int NegFrameSize = -FrameSize;
723 if (!isInt<32>(NegFrameSize))
724 llvm_unreachable("Unhandled stack size!");
726 if (MFI.isFrameAddressTaken())
727 replaceFPWithRealFP(MF);
729 // Check if the link register (LR) must be saved.
730 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
731 bool MustSaveLR = FI->mustSaveLR();
732 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
733 bool MustSaveCR = !MustSaveCRs.empty();
734 // Do we have a frame pointer and/or base pointer for this function?
735 bool HasFP = hasFP(MF);
736 bool HasBP = RegInfo->hasBasePointer(MF);
737 bool HasRedZone = isPPC64 || !isSVR4ABI;
739 unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1;
740 unsigned BPReg = RegInfo->getBaseRegister(MF);
741 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
742 unsigned LRReg = isPPC64 ? PPC::LR8 : PPC::LR;
743 unsigned ScratchReg = 0;
744 unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
745 // ...(R12/X12 is volatile in both Darwin & SVR4, & can't be a function arg.)
746 const MCInstrDesc& MFLRInst = TII.get(isPPC64 ? PPC::MFLR8
748 const MCInstrDesc& StoreInst = TII.get(isPPC64 ? PPC::STD
750 const MCInstrDesc& StoreUpdtInst = TII.get(isPPC64 ? PPC::STDU
752 const MCInstrDesc& StoreUpdtIdxInst = TII.get(isPPC64 ? PPC::STDUX
754 const MCInstrDesc& LoadImmShiftedInst = TII.get(isPPC64 ? PPC::LIS8
756 const MCInstrDesc& OrImmInst = TII.get(isPPC64 ? PPC::ORI8
758 const MCInstrDesc& OrInst = TII.get(isPPC64 ? PPC::OR8
760 const MCInstrDesc& SubtractCarryingInst = TII.get(isPPC64 ? PPC::SUBFC8
762 const MCInstrDesc& SubtractImmCarryingInst = TII.get(isPPC64 ? PPC::SUBFIC8
765 // Regarding this assert: Even though LR is saved in the caller's frame (i.e.,
766 // LROffset is positive), that slot is callee-owned. Because PPC32 SVR4 has no
767 // Red Zone, an asynchronous event (a form of "callee") could claim a frame &
768 // overwrite it, so PPC32 SVR4 must claim at least a minimal frame to save LR.
769 assert((isPPC64 || !isSVR4ABI || !(!FrameSize && (MustSaveLR || HasFP))) &&
770 "FrameSize must be >0 to save/restore the FP or LR for 32-bit SVR4.");
772 // Using the same bool variable as below to suppress compiler warnings.
773 bool SingleScratchReg =
774 findScratchRegister(&MBB, false, twoUniqueScratchRegsRequired(&MBB),
775 &ScratchReg, &TempReg);
776 assert(SingleScratchReg &&
777 "Required number of registers not available in this block");
779 SingleScratchReg = ScratchReg == TempReg;
781 int LROffset = getReturnSaveOffset();
786 MachineFrameInfo &MFI = MF.getFrameInfo();
787 int FPIndex = FI->getFramePointerSaveIndex();
788 assert(FPIndex && "No Frame Pointer Save Slot!");
789 FPOffset = MFI.getObjectOffset(FPIndex);
791 FPOffset = getFramePointerSaveOffset();
798 MachineFrameInfo &MFI = MF.getFrameInfo();
799 int BPIndex = FI->getBasePointerSaveIndex();
800 assert(BPIndex && "No Base Pointer Save Slot!");
801 BPOffset = MFI.getObjectOffset(BPIndex);
803 BPOffset = getBasePointerSaveOffset();
808 if (FI->usesPICBase()) {
809 MachineFrameInfo &MFI = MF.getFrameInfo();
810 int PBPIndex = FI->getPICBasePointerSaveIndex();
811 assert(PBPIndex && "No PIC Base Pointer Save Slot!");
812 PBPOffset = MFI.getObjectOffset(PBPIndex);
815 // Get stack alignments.
816 unsigned MaxAlign = MFI.getMaxAlignment();
817 if (HasBP && MaxAlign > 1)
818 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
819 "Invalid alignment!");
821 // Frames of 32KB & larger require special handling because they cannot be
822 // indexed into with a simple STDU/STWU/STD/STW immediate offset operand.
823 bool isLargeFrame = !isInt<16>(NegFrameSize);
825 assert((isPPC64 || !MustSaveCR) &&
826 "Prologue CR saving supported only in 64-bit mode");
828 // If we need to spill the CR and the LR but we don't have two separate
829 // registers available, we must spill them one at a time
830 if (MustSaveCR && SingleScratchReg && MustSaveLR) {
831 // In the ELFv2 ABI, we are not required to save all CR fields.
832 // If only one or two CR fields are clobbered, it is more efficient to use
833 // mfocrf to selectively save just those fields, because mfocrf has short
834 // latency compares to mfcr.
835 unsigned MfcrOpcode = PPC::MFCR8;
836 unsigned CrState = RegState::ImplicitKill;
837 if (isELFv2ABI && MustSaveCRs.size() == 1) {
838 MfcrOpcode = PPC::MFOCRF8;
839 CrState = RegState::Kill;
841 MachineInstrBuilder MIB =
842 BuildMI(MBB, MBBI, dl, TII.get(MfcrOpcode), TempReg);
843 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
844 MIB.addReg(MustSaveCRs[i], CrState);
845 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
846 .addReg(TempReg, getKillRegState(true))
852 BuildMI(MBB, MBBI, dl, MFLRInst, ScratchReg);
855 !(SingleScratchReg && MustSaveLR)) { // will only occur for PPC64
856 // In the ELFv2 ABI, we are not required to save all CR fields.
857 // If only one or two CR fields are clobbered, it is more efficient to use
858 // mfocrf to selectively save just those fields, because mfocrf has short
859 // latency compares to mfcr.
860 unsigned MfcrOpcode = PPC::MFCR8;
861 unsigned CrState = RegState::ImplicitKill;
862 if (isELFv2ABI && MustSaveCRs.size() == 1) {
863 MfcrOpcode = PPC::MFOCRF8;
864 CrState = RegState::Kill;
866 MachineInstrBuilder MIB =
867 BuildMI(MBB, MBBI, dl, TII.get(MfcrOpcode), TempReg);
868 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
869 MIB.addReg(MustSaveCRs[i], CrState);
874 BuildMI(MBB, MBBI, dl, StoreInst)
878 if (FI->usesPICBase())
879 BuildMI(MBB, MBBI, dl, StoreInst)
884 BuildMI(MBB, MBBI, dl, StoreInst)
891 BuildMI(MBB, MBBI, dl, StoreInst)
892 .addReg(ScratchReg, getKillRegState(true))
897 !(SingleScratchReg && MustSaveLR)) { // will only occur for PPC64
898 assert(HasRedZone && "A red zone is always available on PPC64");
899 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
900 .addReg(TempReg, getKillRegState(true))
905 // Skip the rest if this is a leaf function & all spills fit in the Red Zone.
909 // Adjust stack pointer: r1 += NegFrameSize.
910 // If there is a preferred stack alignment, align R1 now
912 if (HasBP && HasRedZone) {
913 // Save a copy of r1 as the base pointer.
914 BuildMI(MBB, MBBI, dl, OrInst, BPReg)
919 // Have we generated a STUX instruction to claim stack frame? If so,
920 // the negated frame size will be placed in ScratchReg.
921 bool HasSTUX = false;
923 // This condition must be kept in sync with canUseAsPrologue.
924 if (HasBP && MaxAlign > 1) {
926 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), ScratchReg)
929 .addImm(64 - Log2_32(MaxAlign));
931 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), ScratchReg)
934 .addImm(32 - Log2_32(MaxAlign))
937 BuildMI(MBB, MBBI, dl, SubtractImmCarryingInst, ScratchReg)
938 .addReg(ScratchReg, RegState::Kill)
939 .addImm(NegFrameSize);
941 assert(!SingleScratchReg && "Only a single scratch reg available");
942 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, TempReg)
943 .addImm(NegFrameSize >> 16);
944 BuildMI(MBB, MBBI, dl, OrImmInst, TempReg)
945 .addReg(TempReg, RegState::Kill)
946 .addImm(NegFrameSize & 0xFFFF);
947 BuildMI(MBB, MBBI, dl, SubtractCarryingInst, ScratchReg)
948 .addReg(ScratchReg, RegState::Kill)
949 .addReg(TempReg, RegState::Kill);
952 BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
953 .addReg(SPReg, RegState::Kill)
958 } else if (!isLargeFrame) {
959 BuildMI(MBB, MBBI, dl, StoreUpdtInst, SPReg)
961 .addImm(NegFrameSize)
965 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
966 .addImm(NegFrameSize >> 16);
967 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
968 .addReg(ScratchReg, RegState::Kill)
969 .addImm(NegFrameSize & 0xFFFF);
970 BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
971 .addReg(SPReg, RegState::Kill)
978 assert(!isPPC64 && "A red zone is always available on PPC64");
980 // The negated frame size is in ScratchReg, and the SPReg has been
981 // decremented by the frame size: SPReg = old SPReg + ScratchReg.
982 // Since FPOffset, PBPOffset, etc. are relative to the beginning of
983 // the stack frame (i.e. the old SP), ideally, we would put the old
984 // SP into a register and use it as the base for the stores. The
985 // problem is that the only available register may be ScratchReg,
986 // which could be R0, and R0 cannot be used as a base address.
988 // First, set ScratchReg to the old SP. This may need to be modified
990 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBF), ScratchReg)
991 .addReg(ScratchReg, RegState::Kill)
994 if (ScratchReg == PPC::R0) {
995 // R0 cannot be used as a base register, but it can be used as an
996 // index in a store-indexed.
999 // R0 += (FPOffset-LastOffset).
1000 // Need addic, since addi treats R0 as 0.
1001 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), ScratchReg)
1003 .addImm(FPOffset-LastOffset);
1004 LastOffset = FPOffset;
1005 // Store FP into *R0.
1006 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWX))
1007 .addReg(FPReg, RegState::Kill) // Save FP.
1009 .addReg(ScratchReg); // This will be the index (R0 is ok here).
1011 if (FI->usesPICBase()) {
1012 // R0 += (PBPOffset-LastOffset).
1013 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), ScratchReg)
1015 .addImm(PBPOffset-LastOffset);
1016 LastOffset = PBPOffset;
1017 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWX))
1018 .addReg(PPC::R30, RegState::Kill) // Save PIC base pointer.
1020 .addReg(ScratchReg); // This will be the index (R0 is ok here).
1023 // R0 += (BPOffset-LastOffset).
1024 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), ScratchReg)
1026 .addImm(BPOffset-LastOffset);
1027 LastOffset = BPOffset;
1028 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWX))
1029 .addReg(BPReg, RegState::Kill) // Save BP.
1031 .addReg(ScratchReg); // This will be the index (R0 is ok here).
1032 // BP = R0-LastOffset
1033 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), BPReg)
1034 .addReg(ScratchReg, RegState::Kill)
1035 .addImm(-LastOffset);
1038 // ScratchReg is not R0, so use it as the base register. It is
1039 // already set to the old SP, so we can use the offsets directly.
1041 // Now that the stack frame has been allocated, save all the necessary
1042 // registers using ScratchReg as the base address.
1044 BuildMI(MBB, MBBI, dl, StoreInst)
1047 .addReg(ScratchReg);
1048 if (FI->usesPICBase())
1049 BuildMI(MBB, MBBI, dl, StoreInst)
1052 .addReg(ScratchReg);
1054 BuildMI(MBB, MBBI, dl, StoreInst)
1057 .addReg(ScratchReg);
1058 BuildMI(MBB, MBBI, dl, OrInst, BPReg)
1059 .addReg(ScratchReg, RegState::Kill)
1060 .addReg(ScratchReg);
1064 // The frame size is a known 16-bit constant (fitting in the immediate
1065 // field of STWU). To be here we have to be compiling for PPC32.
1066 // Since the SPReg has been decreased by FrameSize, add it back to each
1069 BuildMI(MBB, MBBI, dl, StoreInst)
1071 .addImm(FrameSize + FPOffset)
1073 if (FI->usesPICBase())
1074 BuildMI(MBB, MBBI, dl, StoreInst)
1076 .addImm(FrameSize + PBPOffset)
1079 BuildMI(MBB, MBBI, dl, StoreInst)
1081 .addImm(FrameSize + BPOffset)
1083 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), BPReg)
1090 // Add Call Frame Information for the instructions we generated above.
1095 // Define CFA in terms of BP. Do this in preference to using FP/SP,
1096 // because if the stack needed aligning then CFA won't be at a fixed
1097 // offset from FP/SP.
1098 unsigned Reg = MRI->getDwarfRegNum(BPReg, true);
1099 CFIIndex = MF.addFrameInst(
1100 MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1102 // Adjust the definition of CFA to account for the change in SP.
1103 assert(NegFrameSize);
1104 CFIIndex = MF.addFrameInst(
1105 MCCFIInstruction::createDefCfaOffset(nullptr, NegFrameSize));
1107 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1108 .addCFIIndex(CFIIndex);
1111 // Describe where FP was saved, at a fixed offset from CFA.
1112 unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
1113 CFIIndex = MF.addFrameInst(
1114 MCCFIInstruction::createOffset(nullptr, Reg, FPOffset));
1115 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1116 .addCFIIndex(CFIIndex);
1119 if (FI->usesPICBase()) {
1120 // Describe where FP was saved, at a fixed offset from CFA.
1121 unsigned Reg = MRI->getDwarfRegNum(PPC::R30, true);
1122 CFIIndex = MF.addFrameInst(
1123 MCCFIInstruction::createOffset(nullptr, Reg, PBPOffset));
1124 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1125 .addCFIIndex(CFIIndex);
1129 // Describe where BP was saved, at a fixed offset from CFA.
1130 unsigned Reg = MRI->getDwarfRegNum(BPReg, true);
1131 CFIIndex = MF.addFrameInst(
1132 MCCFIInstruction::createOffset(nullptr, Reg, BPOffset));
1133 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1134 .addCFIIndex(CFIIndex);
1138 // Describe where LR was saved, at a fixed offset from CFA.
1139 unsigned Reg = MRI->getDwarfRegNum(LRReg, true);
1140 CFIIndex = MF.addFrameInst(
1141 MCCFIInstruction::createOffset(nullptr, Reg, LROffset));
1142 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1143 .addCFIIndex(CFIIndex);
1147 // If there is a frame pointer, copy R1 into R31
1149 BuildMI(MBB, MBBI, dl, OrInst, FPReg)
1153 if (!HasBP && needsCFI) {
1154 // Change the definition of CFA from SP+offset to FP+offset, because SP
1155 // will change at every alloca.
1156 unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
1157 unsigned CFIIndex = MF.addFrameInst(
1158 MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1160 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1161 .addCFIIndex(CFIIndex);
1166 // Describe where callee saved registers were saved, at fixed offsets from
1168 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
1169 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
1170 unsigned Reg = CSI[I].getReg();
1171 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
1173 // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just
1174 // subregisters of CR2. We just need to emit a move of CR2.
1175 if (PPC::CRBITRCRegClass.contains(Reg))
1178 // For SVR4, don't emit a move for the CR spill slot if we haven't
1180 if (isSVR4ABI && (PPC::CR2 <= Reg && Reg <= PPC::CR4)
1184 // For 64-bit SVR4 when we have spilled CRs, the spill location
1185 // is SP+8, not a frame-relative slot.
1186 if (isSVR4ABI && isPPC64 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
1187 // In the ELFv1 ABI, only CR2 is noted in CFI and stands in for
1188 // the whole CR word. In the ELFv2 ABI, every CR that was
1189 // actually saved gets its own CFI record.
1190 unsigned CRReg = isELFv2ABI? Reg : (unsigned) PPC::CR2;
1191 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
1192 nullptr, MRI->getDwarfRegNum(CRReg, true), 8));
1193 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1194 .addCFIIndex(CFIIndex);
1198 int Offset = MFI.getObjectOffset(CSI[I].getFrameIdx());
1199 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
1200 nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
1201 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1202 .addCFIIndex(CFIIndex);
1207 void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
1208 MachineBasicBlock &MBB) const {
1209 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
1212 if (MBBI != MBB.end())
1213 dl = MBBI->getDebugLoc();
1215 const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
1216 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
1218 // Get alignment info so we know how to restore the SP.
1219 const MachineFrameInfo &MFI = MF.getFrameInfo();
1221 // Get the number of bytes allocated from the FrameInfo.
1222 int FrameSize = MFI.getStackSize();
1224 // Get processor type.
1225 bool isPPC64 = Subtarget.isPPC64();
1227 bool isSVR4ABI = Subtarget.isSVR4ABI();
1229 // Check if the link register (LR) has been saved.
1230 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1231 bool MustSaveLR = FI->mustSaveLR();
1232 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
1233 bool MustSaveCR = !MustSaveCRs.empty();
1234 // Do we have a frame pointer and/or base pointer for this function?
1235 bool HasFP = hasFP(MF);
1236 bool HasBP = RegInfo->hasBasePointer(MF);
1237 bool HasRedZone = Subtarget.isPPC64() || !Subtarget.isSVR4ABI();
1239 unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1;
1240 unsigned BPReg = RegInfo->getBaseRegister(MF);
1241 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
1242 unsigned ScratchReg = 0;
1243 unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
1244 const MCInstrDesc& MTLRInst = TII.get( isPPC64 ? PPC::MTLR8
1246 const MCInstrDesc& LoadInst = TII.get( isPPC64 ? PPC::LD
1248 const MCInstrDesc& LoadImmShiftedInst = TII.get( isPPC64 ? PPC::LIS8
1250 const MCInstrDesc& OrInst = TII.get(isPPC64 ? PPC::OR8
1252 const MCInstrDesc& OrImmInst = TII.get( isPPC64 ? PPC::ORI8
1254 const MCInstrDesc& AddImmInst = TII.get( isPPC64 ? PPC::ADDI8
1256 const MCInstrDesc& AddInst = TII.get( isPPC64 ? PPC::ADD8
1259 int LROffset = getReturnSaveOffset();
1263 // Using the same bool variable as below to suppress compiler warnings.
1264 bool SingleScratchReg = findScratchRegister(&MBB, true, false, &ScratchReg,
1266 assert(SingleScratchReg &&
1267 "Could not find an available scratch register");
1269 SingleScratchReg = ScratchReg == TempReg;
1273 int FPIndex = FI->getFramePointerSaveIndex();
1274 assert(FPIndex && "No Frame Pointer Save Slot!");
1275 FPOffset = MFI.getObjectOffset(FPIndex);
1277 FPOffset = getFramePointerSaveOffset();
1284 int BPIndex = FI->getBasePointerSaveIndex();
1285 assert(BPIndex && "No Base Pointer Save Slot!");
1286 BPOffset = MFI.getObjectOffset(BPIndex);
1288 BPOffset = getBasePointerSaveOffset();
1293 if (FI->usesPICBase()) {
1294 int PBPIndex = FI->getPICBasePointerSaveIndex();
1295 assert(PBPIndex && "No PIC Base Pointer Save Slot!");
1296 PBPOffset = MFI.getObjectOffset(PBPIndex);
1299 bool IsReturnBlock = (MBBI != MBB.end() && MBBI->isReturn());
1301 if (IsReturnBlock) {
1302 unsigned RetOpcode = MBBI->getOpcode();
1303 bool UsesTCRet = RetOpcode == PPC::TCRETURNri ||
1304 RetOpcode == PPC::TCRETURNdi ||
1305 RetOpcode == PPC::TCRETURNai ||
1306 RetOpcode == PPC::TCRETURNri8 ||
1307 RetOpcode == PPC::TCRETURNdi8 ||
1308 RetOpcode == PPC::TCRETURNai8;
1311 int MaxTCRetDelta = FI->getTailCallSPDelta();
1312 MachineOperand &StackAdjust = MBBI->getOperand(1);
1313 assert(StackAdjust.isImm() && "Expecting immediate value.");
1314 // Adjust stack pointer.
1315 int StackAdj = StackAdjust.getImm();
1316 int Delta = StackAdj - MaxTCRetDelta;
1317 assert((Delta >= 0) && "Delta must be positive");
1318 if (MaxTCRetDelta>0)
1319 FrameSize += (StackAdj +Delta);
1321 FrameSize += StackAdj;
1325 // Frames of 32KB & larger require special handling because they cannot be
1326 // indexed into with a simple LD/LWZ immediate offset operand.
1327 bool isLargeFrame = !isInt<16>(FrameSize);
1329 // On targets without red zone, the SP needs to be restored last, so that
1330 // all live contents of the stack frame are upwards of the SP. This means
1331 // that we cannot restore SP just now, since there may be more registers
1332 // to restore from the stack frame (e.g. R31). If the frame size is not
1333 // a simple immediate value, we will need a spare register to hold the
1334 // restored SP. If the frame size is known and small, we can simply adjust
1335 // the offsets of the registers to be restored, and still use SP to restore
1336 // them. In such case, the final update of SP will be to add the frame
1338 // To simplify the code, set RBReg to the base register used to restore
1339 // values from the stack, and set SPAdd to the value that needs to be added
1340 // to the SP at the end. The default values are as if red zone was present.
1341 unsigned RBReg = SPReg;
1345 // In the prologue, the loaded (or persistent) stack pointer value is
1346 // offset by the STDU/STDUX/STWU/STWUX instruction. For targets with red
1347 // zone add this offset back now.
1349 // If this function contained a fastcc call and GuaranteedTailCallOpt is
1350 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
1351 // call which invalidates the stack pointer value in SP(0). So we use the
1352 // value of R31 in this case.
1353 if (FI->hasFastCall()) {
1354 assert(HasFP && "Expecting a valid frame pointer.");
1357 if (!isLargeFrame) {
1358 BuildMI(MBB, MBBI, dl, AddImmInst, RBReg)
1359 .addReg(FPReg).addImm(FrameSize);
1361 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
1362 .addImm(FrameSize >> 16);
1363 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
1364 .addReg(ScratchReg, RegState::Kill)
1365 .addImm(FrameSize & 0xFFFF);
1366 BuildMI(MBB, MBBI, dl, AddInst)
1369 .addReg(ScratchReg);
1371 } else if (!isLargeFrame && !HasBP && !MFI.hasVarSizedObjects()) {
1373 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1377 // Make sure that adding FrameSize will not overflow the max offset
1379 assert(FPOffset <= 0 && BPOffset <= 0 && PBPOffset <= 0 &&
1380 "Local offsets should be negative");
1382 FPOffset += FrameSize;
1383 BPOffset += FrameSize;
1384 PBPOffset += FrameSize;
1387 // We don't want to use ScratchReg as a base register, because it
1388 // could happen to be R0. Use FP instead, but make sure to preserve it.
1390 // If FP is not saved, copy it to ScratchReg.
1392 BuildMI(MBB, MBBI, dl, OrInst, ScratchReg)
1397 BuildMI(MBB, MBBI, dl, LoadInst, RBReg)
1402 assert(RBReg != ScratchReg && "Should have avoided ScratchReg");
1403 // If there is no red zone, ScratchReg may be needed for holding a useful
1404 // value (although not the base register). Make sure it is not overwritten
1407 assert((isPPC64 || !MustSaveCR) &&
1408 "Epilogue CR restoring supported only in 64-bit mode");
1410 // If we need to restore both the LR and the CR and we only have one
1411 // available scratch register, we must do them one at a time.
1412 if (MustSaveCR && SingleScratchReg && MustSaveLR) {
1413 // Here TempReg == ScratchReg, and in the absence of red zone ScratchReg
1415 assert(HasRedZone && "Expecting red zone");
1416 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg)
1419 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
1420 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i])
1421 .addReg(TempReg, getKillRegState(i == e-1));
1424 // Delay restoring of the LR if ScratchReg is needed. This is ok, since
1425 // LR is stored in the caller's stack frame. ScratchReg will be needed
1426 // if RBReg is anything other than SP. We shouldn't use ScratchReg as
1427 // a base register anyway, because it may happen to be R0.
1428 bool LoadedLR = false;
1429 if (MustSaveLR && RBReg == SPReg && isInt<16>(LROffset+SPAdd)) {
1430 BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg)
1431 .addImm(LROffset+SPAdd)
1436 if (MustSaveCR && !(SingleScratchReg && MustSaveLR)) {
1437 // This will only occur for PPC64.
1438 assert(isPPC64 && "Expecting 64-bit mode");
1439 assert(RBReg == SPReg && "Should be using SP as a base register");
1440 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg)
1446 // If there is red zone, restore FP directly, since SP has already been
1447 // restored. Otherwise, restore the value of FP into ScratchReg.
1448 if (HasRedZone || RBReg == SPReg)
1449 BuildMI(MBB, MBBI, dl, LoadInst, FPReg)
1453 BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg)
1458 if (FI->usesPICBase())
1459 BuildMI(MBB, MBBI, dl, LoadInst, PPC::R30)
1464 BuildMI(MBB, MBBI, dl, LoadInst, BPReg)
1468 // There is nothing more to be loaded from the stack, so now we can
1469 // restore SP: SP = RBReg + SPAdd.
1470 if (RBReg != SPReg || SPAdd != 0) {
1471 assert(!HasRedZone && "This should not happen with red zone");
1472 // If SPAdd is 0, generate a copy.
1474 BuildMI(MBB, MBBI, dl, OrInst, SPReg)
1478 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1482 assert(RBReg != ScratchReg && "Should be using FP or SP as base register");
1484 BuildMI(MBB, MBBI, dl, OrInst, FPReg)
1486 .addReg(ScratchReg);
1488 // Now load the LR from the caller's stack frame.
1489 if (MustSaveLR && !LoadedLR)
1490 BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg)
1496 !(SingleScratchReg && MustSaveLR)) // will only occur for PPC64
1497 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
1498 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i])
1499 .addReg(TempReg, getKillRegState(i == e-1));
1502 BuildMI(MBB, MBBI, dl, MTLRInst).addReg(ScratchReg);
1504 // Callee pop calling convention. Pop parameter/linkage area. Used for tail
1505 // call optimization
1506 if (IsReturnBlock) {
1507 unsigned RetOpcode = MBBI->getOpcode();
1508 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1509 (RetOpcode == PPC::BLR || RetOpcode == PPC::BLR8) &&
1510 MF.getFunction()->getCallingConv() == CallingConv::Fast) {
1511 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1512 unsigned CallerAllocatedAmt = FI->getMinReservedArea();
1514 if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) {
1515 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1516 .addReg(SPReg).addImm(CallerAllocatedAmt);
1518 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
1519 .addImm(CallerAllocatedAmt >> 16);
1520 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
1521 .addReg(ScratchReg, RegState::Kill)
1522 .addImm(CallerAllocatedAmt & 0xFFFF);
1523 BuildMI(MBB, MBBI, dl, AddInst)
1526 .addReg(ScratchReg);
1529 createTailCallBranchInstr(MBB);
1534 void PPCFrameLowering::createTailCallBranchInstr(MachineBasicBlock &MBB) const {
1535 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
1538 if (MBBI != MBB.end())
1539 dl = MBBI->getDebugLoc();
1541 const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
1543 // Create branch instruction for pseudo tail call return instruction
1544 unsigned RetOpcode = MBBI->getOpcode();
1545 if (RetOpcode == PPC::TCRETURNdi) {
1546 MBBI = MBB.getLastNonDebugInstr();
1547 MachineOperand &JumpTarget = MBBI->getOperand(0);
1548 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)).
1549 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1550 } else if (RetOpcode == PPC::TCRETURNri) {
1551 MBBI = MBB.getLastNonDebugInstr();
1552 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1553 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
1554 } else if (RetOpcode == PPC::TCRETURNai) {
1555 MBBI = MBB.getLastNonDebugInstr();
1556 MachineOperand &JumpTarget = MBBI->getOperand(0);
1557 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
1558 } else if (RetOpcode == PPC::TCRETURNdi8) {
1559 MBBI = MBB.getLastNonDebugInstr();
1560 MachineOperand &JumpTarget = MBBI->getOperand(0);
1561 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)).
1562 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1563 } else if (RetOpcode == PPC::TCRETURNri8) {
1564 MBBI = MBB.getLastNonDebugInstr();
1565 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1566 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
1567 } else if (RetOpcode == PPC::TCRETURNai8) {
1568 MBBI = MBB.getLastNonDebugInstr();
1569 MachineOperand &JumpTarget = MBBI->getOperand(0);
1570 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
1574 void PPCFrameLowering::determineCalleeSaves(MachineFunction &MF,
1575 BitVector &SavedRegs,
1576 RegScavenger *RS) const {
1577 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
1579 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
1581 // Save and clear the LR state.
1582 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1583 unsigned LR = RegInfo->getRARegister();
1584 FI->setMustSaveLR(MustSaveLR(MF, LR));
1585 SavedRegs.reset(LR);
1587 // Save R31 if necessary
1588 int FPSI = FI->getFramePointerSaveIndex();
1589 bool isPPC64 = Subtarget.isPPC64();
1590 bool isDarwinABI = Subtarget.isDarwinABI();
1591 MachineFrameInfo &MFI = MF.getFrameInfo();
1593 // If the frame pointer save index hasn't been defined yet.
1594 if (!FPSI && needsFP(MF)) {
1595 // Find out what the fix offset of the frame pointer save area.
1596 int FPOffset = getFramePointerSaveOffset();
1597 // Allocate the frame index for frame pointer save area.
1598 FPSI = MFI.CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
1600 FI->setFramePointerSaveIndex(FPSI);
1603 int BPSI = FI->getBasePointerSaveIndex();
1604 if (!BPSI && RegInfo->hasBasePointer(MF)) {
1605 int BPOffset = getBasePointerSaveOffset();
1606 // Allocate the frame index for the base pointer save area.
1607 BPSI = MFI.CreateFixedObject(isPPC64? 8 : 4, BPOffset, true);
1609 FI->setBasePointerSaveIndex(BPSI);
1612 // Reserve stack space for the PIC Base register (R30).
1613 // Only used in SVR4 32-bit.
1614 if (FI->usesPICBase()) {
1615 int PBPSI = MFI.CreateFixedObject(4, -8, true);
1616 FI->setPICBasePointerSaveIndex(PBPSI);
1619 // Make sure we don't explicitly spill r31, because, for example, we have
1620 // some inline asm which explicity clobbers it, when we otherwise have a
1621 // frame pointer and are using r31's spill slot for the prologue/epilogue
1622 // code. Same goes for the base pointer and the PIC base register.
1624 SavedRegs.reset(isPPC64 ? PPC::X31 : PPC::R31);
1625 if (RegInfo->hasBasePointer(MF))
1626 SavedRegs.reset(RegInfo->getBaseRegister(MF));
1627 if (FI->usesPICBase())
1628 SavedRegs.reset(PPC::R30);
1630 // Reserve stack space to move the linkage area to in case of a tail call.
1632 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1633 (TCSPDelta = FI->getTailCallSPDelta()) < 0) {
1634 MFI.CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true);
1637 // For 32-bit SVR4, allocate the nonvolatile CR spill slot iff the
1638 // function uses CR 2, 3, or 4.
1639 if (!isPPC64 && !isDarwinABI &&
1640 (SavedRegs.test(PPC::CR2) ||
1641 SavedRegs.test(PPC::CR3) ||
1642 SavedRegs.test(PPC::CR4))) {
1643 int FrameIdx = MFI.CreateFixedObject((uint64_t)4, (int64_t)-4, true);
1644 FI->setCRSpillFrameIndex(FrameIdx);
1648 void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
1649 RegScavenger *RS) const {
1650 // Early exit if not using the SVR4 ABI.
1651 if (!Subtarget.isSVR4ABI()) {
1652 addScavengingSpillSlot(MF, RS);
1656 // Get callee saved register information.
1657 MachineFrameInfo &MFI = MF.getFrameInfo();
1658 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
1660 // If the function is shrink-wrapped, and if the function has a tail call, the
1661 // tail call might not be in the new RestoreBlock, so real branch instruction
1662 // won't be generated by emitEpilogue(), because shrink-wrap has chosen new
1663 // RestoreBlock. So we handle this case here.
1664 if (MFI.getSavePoint() && MFI.hasTailCall()) {
1665 MachineBasicBlock *RestoreBlock = MFI.getRestorePoint();
1666 for (MachineBasicBlock &MBB : MF) {
1667 if (MBB.isReturnBlock() && (&MBB) != RestoreBlock)
1668 createTailCallBranchInstr(MBB);
1672 // Early exit if no callee saved registers are modified!
1673 if (CSI.empty() && !needsFP(MF)) {
1674 addScavengingSpillSlot(MF, RS);
1678 unsigned MinGPR = PPC::R31;
1679 unsigned MinG8R = PPC::X31;
1680 unsigned MinFPR = PPC::F31;
1681 unsigned MinVR = PPC::V31;
1683 bool HasGPSaveArea = false;
1684 bool HasG8SaveArea = false;
1685 bool HasFPSaveArea = false;
1686 bool HasVRSAVESaveArea = false;
1687 bool HasVRSaveArea = false;
1689 SmallVector<CalleeSavedInfo, 18> GPRegs;
1690 SmallVector<CalleeSavedInfo, 18> G8Regs;
1691 SmallVector<CalleeSavedInfo, 18> FPRegs;
1692 SmallVector<CalleeSavedInfo, 18> VRegs;
1694 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1695 unsigned Reg = CSI[i].getReg();
1696 if (PPC::GPRCRegClass.contains(Reg)) {
1697 HasGPSaveArea = true;
1699 GPRegs.push_back(CSI[i]);
1704 } else if (PPC::G8RCRegClass.contains(Reg)) {
1705 HasG8SaveArea = true;
1707 G8Regs.push_back(CSI[i]);
1712 } else if (PPC::F8RCRegClass.contains(Reg)) {
1713 HasFPSaveArea = true;
1715 FPRegs.push_back(CSI[i]);
1720 } else if (PPC::CRBITRCRegClass.contains(Reg) ||
1721 PPC::CRRCRegClass.contains(Reg)) {
1722 ; // do nothing, as we already know whether CRs are spilled
1723 } else if (PPC::VRSAVERCRegClass.contains(Reg)) {
1724 HasVRSAVESaveArea = true;
1725 } else if (PPC::VRRCRegClass.contains(Reg)) {
1726 HasVRSaveArea = true;
1728 VRegs.push_back(CSI[i]);
1734 llvm_unreachable("Unknown RegisterClass!");
1738 PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>();
1739 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
1741 int64_t LowerBound = 0;
1743 // Take into account stack space reserved for tail calls.
1745 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1746 (TCSPDelta = PFI->getTailCallSPDelta()) < 0) {
1747 LowerBound = TCSPDelta;
1750 // The Floating-point register save area is right below the back chain word
1751 // of the previous stack frame.
1752 if (HasFPSaveArea) {
1753 for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) {
1754 int FI = FPRegs[i].getFrameIdx();
1756 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
1759 LowerBound -= (31 - TRI->getEncodingValue(MinFPR) + 1) * 8;
1762 // Check whether the frame pointer register is allocated. If so, make sure it
1763 // is spilled to the correct offset.
1765 int FI = PFI->getFramePointerSaveIndex();
1766 assert(FI && "No Frame Pointer Save Slot!");
1767 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
1768 // FP is R31/X31, so no need to update MinGPR/MinG8R.
1769 HasGPSaveArea = true;
1772 if (PFI->usesPICBase()) {
1773 int FI = PFI->getPICBasePointerSaveIndex();
1774 assert(FI && "No PIC Base Pointer Save Slot!");
1775 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
1777 MinGPR = std::min<unsigned>(MinGPR, PPC::R30);
1778 HasGPSaveArea = true;
1781 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
1782 if (RegInfo->hasBasePointer(MF)) {
1783 int FI = PFI->getBasePointerSaveIndex();
1784 assert(FI && "No Base Pointer Save Slot!");
1785 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
1787 unsigned BP = RegInfo->getBaseRegister(MF);
1788 if (PPC::G8RCRegClass.contains(BP)) {
1789 MinG8R = std::min<unsigned>(MinG8R, BP);
1790 HasG8SaveArea = true;
1791 } else if (PPC::GPRCRegClass.contains(BP)) {
1792 MinGPR = std::min<unsigned>(MinGPR, BP);
1793 HasGPSaveArea = true;
1797 // General register save area starts right below the Floating-point
1798 // register save area.
1799 if (HasGPSaveArea || HasG8SaveArea) {
1800 // Move general register save area spill slots down, taking into account
1801 // the size of the Floating-point register save area.
1802 for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) {
1803 int FI = GPRegs[i].getFrameIdx();
1805 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
1808 // Move general register save area spill slots down, taking into account
1809 // the size of the Floating-point register save area.
1810 for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) {
1811 int FI = G8Regs[i].getFrameIdx();
1813 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
1817 std::min<unsigned>(TRI->getEncodingValue(MinGPR),
1818 TRI->getEncodingValue(MinG8R));
1820 if (Subtarget.isPPC64()) {
1821 LowerBound -= (31 - MinReg + 1) * 8;
1823 LowerBound -= (31 - MinReg + 1) * 4;
1827 // For 32-bit only, the CR save area is below the general register
1828 // save area. For 64-bit SVR4, the CR save area is addressed relative
1829 // to the stack pointer and hence does not need an adjustment here.
1830 // Only CR2 (the first nonvolatile spilled) has an associated frame
1831 // index so that we have a single uniform save area.
1832 if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) {
1833 // Adjust the frame index of the CR spill slot.
1834 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1835 unsigned Reg = CSI[i].getReg();
1837 if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2)
1838 // Leave Darwin logic as-is.
1839 || (!Subtarget.isSVR4ABI() &&
1840 (PPC::CRBITRCRegClass.contains(Reg) ||
1841 PPC::CRRCRegClass.contains(Reg)))) {
1842 int FI = CSI[i].getFrameIdx();
1844 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
1848 LowerBound -= 4; // The CR save area is always 4 bytes long.
1851 if (HasVRSAVESaveArea) {
1852 // FIXME SVR4: Is it actually possible to have multiple elements in CSI
1853 // which have the VRSAVE register class?
1854 // Adjust the frame index of the VRSAVE spill slot.
1855 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1856 unsigned Reg = CSI[i].getReg();
1858 if (PPC::VRSAVERCRegClass.contains(Reg)) {
1859 int FI = CSI[i].getFrameIdx();
1861 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
1865 LowerBound -= 4; // The VRSAVE save area is always 4 bytes long.
1868 if (HasVRSaveArea) {
1869 // Insert alignment padding, we need 16-byte alignment. Note: for postive
1870 // number the alignment formula is : y = (x + (n-1)) & (~(n-1)). But since
1871 // we are using negative number here (the stack grows downward). We should
1872 // use formula : y = x & (~(n-1)). Where x is the size before aligning, n
1873 // is the alignment size ( n = 16 here) and y is the size after aligning.
1874 assert(LowerBound <= 0 && "Expect LowerBound have a non-positive value!");
1875 LowerBound &= ~(15);
1877 for (unsigned i = 0, e = VRegs.size(); i != e; ++i) {
1878 int FI = VRegs[i].getFrameIdx();
1880 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
1884 addScavengingSpillSlot(MF, RS);
1888 PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF,
1889 RegScavenger *RS) const {
1890 // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
1891 // a large stack, which will require scavenging a register to materialize a
1894 // We need to have a scavenger spill slot for spills if the frame size is
1895 // large. In case there is no free register for large-offset addressing,
1896 // this slot is used for the necessary emergency spill. Also, we need the
1897 // slot for dynamic stack allocations.
1899 // The scavenger might be invoked if the frame offset does not fit into
1900 // the 16-bit immediate. We don't know the complete frame size here
1901 // because we've not yet computed callee-saved register spills or the
1902 // needed alignment padding.
1903 unsigned StackSize = determineFrameLayout(MF, false, true);
1904 MachineFrameInfo &MFI = MF.getFrameInfo();
1905 if (MFI.hasVarSizedObjects() || spillsCR(MF) || spillsVRSAVE(MF) ||
1906 hasNonRISpills(MF) || (hasSpills(MF) && !isInt<16>(StackSize))) {
1907 const TargetRegisterClass &GPRC = PPC::GPRCRegClass;
1908 const TargetRegisterClass &G8RC = PPC::G8RCRegClass;
1909 const TargetRegisterClass &RC = Subtarget.isPPC64() ? G8RC : GPRC;
1910 const TargetRegisterInfo &TRI = *Subtarget.getRegisterInfo();
1911 unsigned Size = TRI.getSpillSize(RC);
1912 unsigned Align = TRI.getSpillAlignment(RC);
1913 RS->addScavengingFrameIndex(MFI.CreateStackObject(Size, Align, false));
1915 // Might we have over-aligned allocas?
1916 bool HasAlVars = MFI.hasVarSizedObjects() &&
1917 MFI.getMaxAlignment() > getStackAlignment();
1919 // These kinds of spills might need two registers.
1920 if (spillsCR(MF) || spillsVRSAVE(MF) || HasAlVars)
1921 RS->addScavengingFrameIndex(MFI.CreateStackObject(Size, Align, false));
1927 PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
1928 MachineBasicBlock::iterator MI,
1929 const std::vector<CalleeSavedInfo> &CSI,
1930 const TargetRegisterInfo *TRI) const {
1932 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1933 // Return false otherwise to maintain pre-existing behavior.
1934 if (!Subtarget.isSVR4ABI())
1937 MachineFunction *MF = MBB.getParent();
1938 const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
1940 bool CRSpilled = false;
1941 MachineInstrBuilder CRMIB;
1943 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1944 unsigned Reg = CSI[i].getReg();
1945 // Only Darwin actually uses the VRSAVE register, but it can still appear
1946 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
1947 // Darwin, ignore it.
1948 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1951 // CR2 through CR4 are the nonvolatile CR fields.
1952 bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4;
1954 // Add the callee-saved register as live-in; it's killed at the spill.
1957 if (CRSpilled && IsCRField) {
1958 CRMIB.addReg(Reg, RegState::ImplicitKill);
1962 // Insert the spill to the stack frame.
1964 PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
1965 if (Subtarget.isPPC64()) {
1966 // The actual spill will happen at the start of the prologue.
1967 FuncInfo->addMustSaveCR(Reg);
1970 FuncInfo->setSpillsCR();
1972 // 32-bit: FP-relative. Note that we made sure CR2-CR4 all have
1973 // the same frame index in PPCRegisterInfo::hasReservedSpillSlot.
1974 CRMIB = BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12)
1975 .addReg(Reg, RegState::ImplicitKill);
1977 MBB.insert(MI, CRMIB);
1978 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW))
1980 getKillRegState(true)),
1981 CSI[i].getFrameIdx()));
1984 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1985 TII.storeRegToStackSlot(MBB, MI, Reg, true,
1986 CSI[i].getFrameIdx(), RC, TRI);
1993 restoreCRs(bool isPPC64, bool is31,
1994 bool CR2Spilled, bool CR3Spilled, bool CR4Spilled,
1995 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1996 const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) {
1998 MachineFunction *MF = MBB.getParent();
1999 const PPCInstrInfo &TII = *MF->getSubtarget<PPCSubtarget>().getInstrInfo();
2001 unsigned RestoreOp, MoveReg;
2004 // This is handled during epilogue generation.
2007 // 32-bit: FP-relative
2008 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ),
2010 CSI[CSIIndex].getFrameIdx()));
2011 RestoreOp = PPC::MTOCRF;
2016 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2)
2017 .addReg(MoveReg, getKillRegState(!CR3Spilled && !CR4Spilled)));
2020 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3)
2021 .addReg(MoveReg, getKillRegState(!CR4Spilled)));
2024 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4)
2025 .addReg(MoveReg, getKillRegState(true)));
2028 MachineBasicBlock::iterator PPCFrameLowering::
2029 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
2030 MachineBasicBlock::iterator I) const {
2031 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
2032 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
2033 I->getOpcode() == PPC::ADJCALLSTACKUP) {
2034 // Add (actually subtract) back the amount the callee popped on return.
2035 if (int CalleeAmt = I->getOperand(1).getImm()) {
2036 bool is64Bit = Subtarget.isPPC64();
2038 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
2039 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
2040 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
2041 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
2042 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
2043 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
2044 const DebugLoc &dl = I->getDebugLoc();
2046 if (isInt<16>(CalleeAmt)) {
2047 BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg)
2048 .addReg(StackReg, RegState::Kill)
2051 MachineBasicBlock::iterator MBBI = I;
2052 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
2053 .addImm(CalleeAmt >> 16);
2054 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
2055 .addReg(TmpReg, RegState::Kill)
2056 .addImm(CalleeAmt & 0xFFFF);
2057 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg)
2058 .addReg(StackReg, RegState::Kill)
2063 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
2064 return MBB.erase(I);
2068 PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
2069 MachineBasicBlock::iterator MI,
2070 const std::vector<CalleeSavedInfo> &CSI,
2071 const TargetRegisterInfo *TRI) const {
2073 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
2074 // Return false otherwise to maintain pre-existing behavior.
2075 if (!Subtarget.isSVR4ABI())
2078 MachineFunction *MF = MBB.getParent();
2079 const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
2080 bool CR2Spilled = false;
2081 bool CR3Spilled = false;
2082 bool CR4Spilled = false;
2083 unsigned CSIIndex = 0;
2085 // Initialize insertion-point logic; we will be restoring in reverse
2087 MachineBasicBlock::iterator I = MI, BeforeI = I;
2088 bool AtStart = I == MBB.begin();
2093 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
2094 unsigned Reg = CSI[i].getReg();
2096 // Only Darwin actually uses the VRSAVE register, but it can still appear
2097 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
2098 // Darwin, ignore it.
2099 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
2102 if (Reg == PPC::CR2) {
2104 // The spill slot is associated only with CR2, which is the
2105 // first nonvolatile spilled. Save it here.
2108 } else if (Reg == PPC::CR3) {
2111 } else if (Reg == PPC::CR4) {
2115 // When we first encounter a non-CR register after seeing at
2116 // least one CR register, restore all spilled CRs together.
2117 if ((CR2Spilled || CR3Spilled || CR4Spilled)
2118 && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
2119 bool is31 = needsFP(*MF);
2120 restoreCRs(Subtarget.isPPC64(), is31,
2121 CR2Spilled, CR3Spilled, CR4Spilled,
2122 MBB, I, CSI, CSIIndex);
2123 CR2Spilled = CR3Spilled = CR4Spilled = false;
2126 // Default behavior for non-CR saves.
2127 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
2128 TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(),
2130 assert(I != MBB.begin() &&
2131 "loadRegFromStackSlot didn't insert any code!");
2134 // Insert in reverse order.
2143 // If we haven't yet spilled the CRs, do so now.
2144 if (CR2Spilled || CR3Spilled || CR4Spilled) {
2145 bool is31 = needsFP(*MF);
2146 restoreCRs(Subtarget.isPPC64(), is31, CR2Spilled, CR3Spilled, CR4Spilled,
2147 MBB, I, CSI, CSIIndex);
2153 bool PPCFrameLowering::enableShrinkWrapping(const MachineFunction &MF) const {
2154 return (MF.getSubtarget<PPCSubtarget>().isSVR4ABI() &&
2155 MF.getSubtarget<PPCSubtarget>().isPPC64());