PPCFrameLowering.cpp revision 218885
1218885Sdim//=====- PPCFrameLowering.cpp - PPC Frame Information -----------*- C++ -*-===// 2218885Sdim// 3218885Sdim// The LLVM Compiler Infrastructure 4218885Sdim// 5218885Sdim// This file is distributed under the University of Illinois Open Source 6218885Sdim// License. See LICENSE.TXT for details. 7218885Sdim// 8218885Sdim//===----------------------------------------------------------------------===// 9218885Sdim// 10218885Sdim// This file contains the PPC implementation of TargetFrameLowering class. 11218885Sdim// 12218885Sdim//===----------------------------------------------------------------------===// 13218885Sdim 14218885Sdim#include "PPCFrameLowering.h" 15218885Sdim#include "PPCInstrInfo.h" 16218885Sdim#include "PPCMachineFunctionInfo.h" 17218885Sdim#include "llvm/Function.h" 18218885Sdim#include "llvm/CodeGen/MachineFrameInfo.h" 19218885Sdim#include "llvm/CodeGen/MachineFunction.h" 20218885Sdim#include "llvm/CodeGen/MachineInstrBuilder.h" 21218885Sdim#include "llvm/CodeGen/MachineModuleInfo.h" 22218885Sdim#include "llvm/CodeGen/MachineRegisterInfo.h" 23218885Sdim#include "llvm/CodeGen/RegisterScavenging.h" 24218885Sdim#include "llvm/Target/TargetOptions.h" 25218885Sdim 26218885Sdimusing namespace llvm; 27218885Sdim 28218885Sdim// FIXME This disables some code that aligns the stack to a boundary bigger than 29218885Sdim// the default (16 bytes on Darwin) when there is a stack local of greater 30218885Sdim// alignment. This does not currently work, because the delta between old and 31218885Sdim// new stack pointers is added to offsets that reference incoming parameters 32218885Sdim// after the prolog is generated, and the code that does that doesn't handle a 33218885Sdim// variable delta. You don't want to do that anyway; a better approach is to 34218885Sdim// reserve another register that retains to the incoming stack pointer, and 35218885Sdim// reference parameters relative to that. 36218885Sdim#define ALIGN_STACK 0 37218885Sdim 38218885Sdim 39218885Sdim/// VRRegNo - Map from a numbered VR register to its enum value. 40218885Sdim/// 41218885Sdimstatic const unsigned short VRRegNo[] = { 42218885Sdim PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 , 43218885Sdim PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15, 44218885Sdim PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23, 45218885Sdim PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31 46218885Sdim}; 47218885Sdim 48218885Sdim/// RemoveVRSaveCode - We have found that this function does not need any code 49218885Sdim/// to manipulate the VRSAVE register, even though it uses vector registers. 50218885Sdim/// This can happen when the only registers used are known to be live in or out 51218885Sdim/// of the function. Remove all of the VRSAVE related code from the function. 52218885Sdimstatic void RemoveVRSaveCode(MachineInstr *MI) { 53218885Sdim MachineBasicBlock *Entry = MI->getParent(); 54218885Sdim MachineFunction *MF = Entry->getParent(); 55218885Sdim 56218885Sdim // We know that the MTVRSAVE instruction immediately follows MI. Remove it. 57218885Sdim MachineBasicBlock::iterator MBBI = MI; 58218885Sdim ++MBBI; 59218885Sdim assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE); 60218885Sdim MBBI->eraseFromParent(); 61218885Sdim 62218885Sdim bool RemovedAllMTVRSAVEs = true; 63218885Sdim // See if we can find and remove the MTVRSAVE instruction from all of the 64218885Sdim // epilog blocks. 65218885Sdim for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) { 66218885Sdim // If last instruction is a return instruction, add an epilogue 67218885Sdim if (!I->empty() && I->back().getDesc().isReturn()) { 68218885Sdim bool FoundIt = false; 69218885Sdim for (MBBI = I->end(); MBBI != I->begin(); ) { 70218885Sdim --MBBI; 71218885Sdim if (MBBI->getOpcode() == PPC::MTVRSAVE) { 72218885Sdim MBBI->eraseFromParent(); // remove it. 73218885Sdim FoundIt = true; 74218885Sdim break; 75218885Sdim } 76218885Sdim } 77218885Sdim RemovedAllMTVRSAVEs &= FoundIt; 78218885Sdim } 79218885Sdim } 80218885Sdim 81218885Sdim // If we found and removed all MTVRSAVE instructions, remove the read of 82218885Sdim // VRSAVE as well. 83218885Sdim if (RemovedAllMTVRSAVEs) { 84218885Sdim MBBI = MI; 85218885Sdim assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?"); 86218885Sdim --MBBI; 87218885Sdim assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?"); 88218885Sdim MBBI->eraseFromParent(); 89218885Sdim } 90218885Sdim 91218885Sdim // Finally, nuke the UPDATE_VRSAVE. 92218885Sdim MI->eraseFromParent(); 93218885Sdim} 94218885Sdim 95218885Sdim// HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the 96218885Sdim// instruction selector. Based on the vector registers that have been used, 97218885Sdim// transform this into the appropriate ORI instruction. 98218885Sdimstatic void HandleVRSaveUpdate(MachineInstr *MI, const TargetInstrInfo &TII) { 99218885Sdim MachineFunction *MF = MI->getParent()->getParent(); 100218885Sdim DebugLoc dl = MI->getDebugLoc(); 101218885Sdim 102218885Sdim unsigned UsedRegMask = 0; 103218885Sdim for (unsigned i = 0; i != 32; ++i) 104218885Sdim if (MF->getRegInfo().isPhysRegUsed(VRRegNo[i])) 105218885Sdim UsedRegMask |= 1 << (31-i); 106218885Sdim 107218885Sdim // Live in and live out values already must be in the mask, so don't bother 108218885Sdim // marking them. 109218885Sdim for (MachineRegisterInfo::livein_iterator 110218885Sdim I = MF->getRegInfo().livein_begin(), 111218885Sdim E = MF->getRegInfo().livein_end(); I != E; ++I) { 112218885Sdim unsigned RegNo = PPCRegisterInfo::getRegisterNumbering(I->first); 113218885Sdim if (VRRegNo[RegNo] == I->first) // If this really is a vector reg. 114218885Sdim UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked. 115218885Sdim } 116218885Sdim for (MachineRegisterInfo::liveout_iterator 117218885Sdim I = MF->getRegInfo().liveout_begin(), 118218885Sdim E = MF->getRegInfo().liveout_end(); I != E; ++I) { 119218885Sdim unsigned RegNo = PPCRegisterInfo::getRegisterNumbering(*I); 120218885Sdim if (VRRegNo[RegNo] == *I) // If this really is a vector reg. 121218885Sdim UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked. 122218885Sdim } 123218885Sdim 124218885Sdim // If no registers are used, turn this into a copy. 125218885Sdim if (UsedRegMask == 0) { 126218885Sdim // Remove all VRSAVE code. 127218885Sdim RemoveVRSaveCode(MI); 128218885Sdim return; 129218885Sdim } 130218885Sdim 131218885Sdim unsigned SrcReg = MI->getOperand(1).getReg(); 132218885Sdim unsigned DstReg = MI->getOperand(0).getReg(); 133218885Sdim 134218885Sdim if ((UsedRegMask & 0xFFFF) == UsedRegMask) { 135218885Sdim if (DstReg != SrcReg) 136218885Sdim BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg) 137218885Sdim .addReg(SrcReg) 138218885Sdim .addImm(UsedRegMask); 139218885Sdim else 140218885Sdim BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg) 141218885Sdim .addReg(SrcReg, RegState::Kill) 142218885Sdim .addImm(UsedRegMask); 143218885Sdim } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) { 144218885Sdim if (DstReg != SrcReg) 145218885Sdim BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) 146218885Sdim .addReg(SrcReg) 147218885Sdim .addImm(UsedRegMask >> 16); 148218885Sdim else 149218885Sdim BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) 150218885Sdim .addReg(SrcReg, RegState::Kill) 151218885Sdim .addImm(UsedRegMask >> 16); 152218885Sdim } else { 153218885Sdim if (DstReg != SrcReg) 154218885Sdim BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) 155218885Sdim .addReg(SrcReg) 156218885Sdim .addImm(UsedRegMask >> 16); 157218885Sdim else 158218885Sdim BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) 159218885Sdim .addReg(SrcReg, RegState::Kill) 160218885Sdim .addImm(UsedRegMask >> 16); 161218885Sdim 162218885Sdim BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg) 163218885Sdim .addReg(DstReg, RegState::Kill) 164218885Sdim .addImm(UsedRegMask & 0xFFFF); 165218885Sdim } 166218885Sdim 167218885Sdim // Remove the old UPDATE_VRSAVE instruction. 168218885Sdim MI->eraseFromParent(); 169218885Sdim} 170218885Sdim 171218885Sdim/// determineFrameLayout - Determine the size of the frame and maximum call 172218885Sdim/// frame size. 173218885Sdimvoid PPCFrameLowering::determineFrameLayout(MachineFunction &MF) const { 174218885Sdim MachineFrameInfo *MFI = MF.getFrameInfo(); 175218885Sdim 176218885Sdim // Get the number of bytes to allocate from the FrameInfo 177218885Sdim unsigned FrameSize = MFI->getStackSize(); 178218885Sdim 179218885Sdim // Get the alignments provided by the target, and the maximum alignment 180218885Sdim // (if any) of the fixed frame objects. 181218885Sdim unsigned MaxAlign = MFI->getMaxAlignment(); 182218885Sdim unsigned TargetAlign = getStackAlignment(); 183218885Sdim unsigned AlignMask = TargetAlign - 1; // 184218885Sdim 185218885Sdim // If we are a leaf function, and use up to 224 bytes of stack space, 186218885Sdim // don't have a frame pointer, calls, or dynamic alloca then we do not need 187218885Sdim // to adjust the stack pointer (we fit in the Red Zone). 188218885Sdim bool DisableRedZone = MF.getFunction()->hasFnAttr(Attribute::NoRedZone); 189218885Sdim // FIXME SVR4 The 32-bit SVR4 ABI has no red zone. 190218885Sdim if (!DisableRedZone && 191218885Sdim FrameSize <= 224 && // Fits in red zone. 192218885Sdim !MFI->hasVarSizedObjects() && // No dynamic alloca. 193218885Sdim !MFI->adjustsStack() && // No calls. 194218885Sdim (!ALIGN_STACK || MaxAlign <= TargetAlign)) { // No special alignment. 195218885Sdim // No need for frame 196218885Sdim MFI->setStackSize(0); 197218885Sdim return; 198218885Sdim } 199218885Sdim 200218885Sdim // Get the maximum call frame size of all the calls. 201218885Sdim unsigned maxCallFrameSize = MFI->getMaxCallFrameSize(); 202218885Sdim 203218885Sdim // Maximum call frame needs to be at least big enough for linkage and 8 args. 204218885Sdim unsigned minCallFrameSize = getMinCallFrameSize(Subtarget.isPPC64(), 205218885Sdim Subtarget.isDarwinABI()); 206218885Sdim maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize); 207218885Sdim 208218885Sdim // If we have dynamic alloca then maxCallFrameSize needs to be aligned so 209218885Sdim // that allocations will be aligned. 210218885Sdim if (MFI->hasVarSizedObjects()) 211218885Sdim maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask; 212218885Sdim 213218885Sdim // Update maximum call frame size. 214218885Sdim MFI->setMaxCallFrameSize(maxCallFrameSize); 215218885Sdim 216218885Sdim // Include call frame size in total. 217218885Sdim FrameSize += maxCallFrameSize; 218218885Sdim 219218885Sdim // Make sure the frame is aligned. 220218885Sdim FrameSize = (FrameSize + AlignMask) & ~AlignMask; 221218885Sdim 222218885Sdim // Update frame info. 223218885Sdim MFI->setStackSize(FrameSize); 224218885Sdim} 225218885Sdim 226218885Sdim// hasFP - Return true if the specified function actually has a dedicated frame 227218885Sdim// pointer register. 228218885Sdimbool PPCFrameLowering::hasFP(const MachineFunction &MF) const { 229218885Sdim const MachineFrameInfo *MFI = MF.getFrameInfo(); 230218885Sdim // FIXME: This is pretty much broken by design: hasFP() might be called really 231218885Sdim // early, before the stack layout was calculated and thus hasFP() might return 232218885Sdim // true or false here depending on the time of call. 233218885Sdim return (MFI->getStackSize()) && needsFP(MF); 234218885Sdim} 235218885Sdim 236218885Sdim// needsFP - Return true if the specified function should have a dedicated frame 237218885Sdim// pointer register. This is true if the function has variable sized allocas or 238218885Sdim// if frame pointer elimination is disabled. 239218885Sdimbool PPCFrameLowering::needsFP(const MachineFunction &MF) const { 240218885Sdim const MachineFrameInfo *MFI = MF.getFrameInfo(); 241218885Sdim 242218885Sdim // Naked functions have no stack frame pushed, so we don't have a frame 243218885Sdim // pointer. 244218885Sdim if (MF.getFunction()->hasFnAttr(Attribute::Naked)) 245218885Sdim return false; 246218885Sdim 247218885Sdim return DisableFramePointerElim(MF) || MFI->hasVarSizedObjects() || 248218885Sdim (GuaranteedTailCallOpt && MF.getInfo<PPCFunctionInfo>()->hasFastCall()); 249218885Sdim} 250218885Sdim 251218885Sdim 252218885Sdimvoid PPCFrameLowering::emitPrologue(MachineFunction &MF) const { 253218885Sdim MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB 254218885Sdim MachineBasicBlock::iterator MBBI = MBB.begin(); 255218885Sdim MachineFrameInfo *MFI = MF.getFrameInfo(); 256218885Sdim const PPCInstrInfo &TII = 257218885Sdim *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo()); 258218885Sdim 259218885Sdim MachineModuleInfo &MMI = MF.getMMI(); 260218885Sdim DebugLoc dl; 261218885Sdim bool needsFrameMoves = MMI.hasDebugInfo() || 262218885Sdim !MF.getFunction()->doesNotThrow() || 263218885Sdim UnwindTablesMandatory; 264218885Sdim 265218885Sdim // Prepare for frame info. 266218885Sdim MCSymbol *FrameLabel = 0; 267218885Sdim 268218885Sdim // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it, 269218885Sdim // process it. 270218885Sdim for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) { 271218885Sdim if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) { 272218885Sdim HandleVRSaveUpdate(MBBI, TII); 273218885Sdim break; 274218885Sdim } 275218885Sdim } 276218885Sdim 277218885Sdim // Move MBBI back to the beginning of the function. 278218885Sdim MBBI = MBB.begin(); 279218885Sdim 280218885Sdim // Work out frame sizes. 281218885Sdim // FIXME: determineFrameLayout() may change the frame size. This should be 282218885Sdim // moved upper, to some hook. 283218885Sdim determineFrameLayout(MF); 284218885Sdim unsigned FrameSize = MFI->getStackSize(); 285218885Sdim 286218885Sdim int NegFrameSize = -FrameSize; 287218885Sdim 288218885Sdim // Get processor type. 289218885Sdim bool isPPC64 = Subtarget.isPPC64(); 290218885Sdim // Get operating system 291218885Sdim bool isDarwinABI = Subtarget.isDarwinABI(); 292218885Sdim // Check if the link register (LR) must be saved. 293218885Sdim PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 294218885Sdim bool MustSaveLR = FI->mustSaveLR(); 295218885Sdim // Do we have a frame pointer for this function? 296218885Sdim bool HasFP = hasFP(MF); 297218885Sdim 298218885Sdim int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI); 299218885Sdim 300218885Sdim int FPOffset = 0; 301218885Sdim if (HasFP) { 302218885Sdim if (Subtarget.isSVR4ABI()) { 303218885Sdim MachineFrameInfo *FFI = MF.getFrameInfo(); 304218885Sdim int FPIndex = FI->getFramePointerSaveIndex(); 305218885Sdim assert(FPIndex && "No Frame Pointer Save Slot!"); 306218885Sdim FPOffset = FFI->getObjectOffset(FPIndex); 307218885Sdim } else { 308218885Sdim FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI); 309218885Sdim } 310218885Sdim } 311218885Sdim 312218885Sdim if (isPPC64) { 313218885Sdim if (MustSaveLR) 314218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR8), PPC::X0); 315218885Sdim 316218885Sdim if (HasFP) 317218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::STD)) 318218885Sdim .addReg(PPC::X31) 319218885Sdim .addImm(FPOffset/4) 320218885Sdim .addReg(PPC::X1); 321218885Sdim 322218885Sdim if (MustSaveLR) 323218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::STD)) 324218885Sdim .addReg(PPC::X0) 325218885Sdim .addImm(LROffset / 4) 326218885Sdim .addReg(PPC::X1); 327218885Sdim } else { 328218885Sdim if (MustSaveLR) 329218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR), PPC::R0); 330218885Sdim 331218885Sdim if (HasFP) 332218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::STW)) 333218885Sdim .addReg(PPC::R31) 334218885Sdim .addImm(FPOffset) 335218885Sdim .addReg(PPC::R1); 336218885Sdim 337218885Sdim if (MustSaveLR) 338218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::STW)) 339218885Sdim .addReg(PPC::R0) 340218885Sdim .addImm(LROffset) 341218885Sdim .addReg(PPC::R1); 342218885Sdim } 343218885Sdim 344218885Sdim // Skip if a leaf routine. 345218885Sdim if (!FrameSize) return; 346218885Sdim 347218885Sdim // Get stack alignments. 348218885Sdim unsigned TargetAlign = getStackAlignment(); 349218885Sdim unsigned MaxAlign = MFI->getMaxAlignment(); 350218885Sdim 351218885Sdim // Adjust stack pointer: r1 += NegFrameSize. 352218885Sdim // If there is a preferred stack alignment, align R1 now 353218885Sdim if (!isPPC64) { 354218885Sdim // PPC32. 355218885Sdim if (ALIGN_STACK && MaxAlign > TargetAlign) { 356218885Sdim assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) && 357218885Sdim "Invalid alignment!"); 358218885Sdim assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!"); 359218885Sdim 360218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), PPC::R0) 361218885Sdim .addReg(PPC::R1) 362218885Sdim .addImm(0) 363218885Sdim .addImm(32 - Log2_32(MaxAlign)) 364218885Sdim .addImm(31); 365218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC) ,PPC::R0) 366218885Sdim .addReg(PPC::R0, RegState::Kill) 367218885Sdim .addImm(NegFrameSize); 368218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX)) 369218885Sdim .addReg(PPC::R1) 370218885Sdim .addReg(PPC::R1) 371218885Sdim .addReg(PPC::R0); 372218885Sdim } else if (isInt<16>(NegFrameSize)) { 373218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::STWU), PPC::R1) 374218885Sdim .addReg(PPC::R1) 375218885Sdim .addImm(NegFrameSize) 376218885Sdim .addReg(PPC::R1); 377218885Sdim } else { 378218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0) 379218885Sdim .addImm(NegFrameSize >> 16); 380218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0) 381218885Sdim .addReg(PPC::R0, RegState::Kill) 382218885Sdim .addImm(NegFrameSize & 0xFFFF); 383218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX)) 384218885Sdim .addReg(PPC::R1) 385218885Sdim .addReg(PPC::R1) 386218885Sdim .addReg(PPC::R0); 387218885Sdim } 388218885Sdim } else { // PPC64. 389218885Sdim if (ALIGN_STACK && MaxAlign > TargetAlign) { 390218885Sdim assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) && 391218885Sdim "Invalid alignment!"); 392218885Sdim assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!"); 393218885Sdim 394218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), PPC::X0) 395218885Sdim .addReg(PPC::X1) 396218885Sdim .addImm(0) 397218885Sdim .addImm(64 - Log2_32(MaxAlign)); 398218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC8), PPC::X0) 399218885Sdim .addReg(PPC::X0) 400218885Sdim .addImm(NegFrameSize); 401218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX)) 402218885Sdim .addReg(PPC::X1) 403218885Sdim .addReg(PPC::X1) 404218885Sdim .addReg(PPC::X0); 405218885Sdim } else if (isInt<16>(NegFrameSize)) { 406218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::STDU), PPC::X1) 407218885Sdim .addReg(PPC::X1) 408218885Sdim .addImm(NegFrameSize / 4) 409218885Sdim .addReg(PPC::X1); 410218885Sdim } else { 411218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0) 412218885Sdim .addImm(NegFrameSize >> 16); 413218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0) 414218885Sdim .addReg(PPC::X0, RegState::Kill) 415218885Sdim .addImm(NegFrameSize & 0xFFFF); 416218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX)) 417218885Sdim .addReg(PPC::X1) 418218885Sdim .addReg(PPC::X1) 419218885Sdim .addReg(PPC::X0); 420218885Sdim } 421218885Sdim } 422218885Sdim 423218885Sdim std::vector<MachineMove> &Moves = MMI.getFrameMoves(); 424218885Sdim 425218885Sdim // Add the "machine moves" for the instructions we generated above, but in 426218885Sdim // reverse order. 427218885Sdim if (needsFrameMoves) { 428218885Sdim // Mark effective beginning of when frame pointer becomes valid. 429218885Sdim FrameLabel = MMI.getContext().CreateTempSymbol(); 430218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(FrameLabel); 431218885Sdim 432218885Sdim // Show update of SP. 433218885Sdim if (NegFrameSize) { 434218885Sdim MachineLocation SPDst(MachineLocation::VirtualFP); 435218885Sdim MachineLocation SPSrc(MachineLocation::VirtualFP, NegFrameSize); 436218885Sdim Moves.push_back(MachineMove(FrameLabel, SPDst, SPSrc)); 437218885Sdim } else { 438218885Sdim MachineLocation SP(isPPC64 ? PPC::X31 : PPC::R31); 439218885Sdim Moves.push_back(MachineMove(FrameLabel, SP, SP)); 440218885Sdim } 441218885Sdim 442218885Sdim if (HasFP) { 443218885Sdim MachineLocation FPDst(MachineLocation::VirtualFP, FPOffset); 444218885Sdim MachineLocation FPSrc(isPPC64 ? PPC::X31 : PPC::R31); 445218885Sdim Moves.push_back(MachineMove(FrameLabel, FPDst, FPSrc)); 446218885Sdim } 447218885Sdim 448218885Sdim if (MustSaveLR) { 449218885Sdim MachineLocation LRDst(MachineLocation::VirtualFP, LROffset); 450218885Sdim MachineLocation LRSrc(isPPC64 ? PPC::LR8 : PPC::LR); 451218885Sdim Moves.push_back(MachineMove(FrameLabel, LRDst, LRSrc)); 452218885Sdim } 453218885Sdim } 454218885Sdim 455218885Sdim MCSymbol *ReadyLabel = 0; 456218885Sdim 457218885Sdim // If there is a frame pointer, copy R1 into R31 458218885Sdim if (HasFP) { 459218885Sdim if (!isPPC64) { 460218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::OR), PPC::R31) 461218885Sdim .addReg(PPC::R1) 462218885Sdim .addReg(PPC::R1); 463218885Sdim } else { 464218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::OR8), PPC::X31) 465218885Sdim .addReg(PPC::X1) 466218885Sdim .addReg(PPC::X1); 467218885Sdim } 468218885Sdim 469218885Sdim if (needsFrameMoves) { 470218885Sdim ReadyLabel = MMI.getContext().CreateTempSymbol(); 471218885Sdim 472218885Sdim // Mark effective beginning of when frame pointer is ready. 473218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(ReadyLabel); 474218885Sdim 475218885Sdim MachineLocation FPDst(HasFP ? (isPPC64 ? PPC::X31 : PPC::R31) : 476218885Sdim (isPPC64 ? PPC::X1 : PPC::R1)); 477218885Sdim MachineLocation FPSrc(MachineLocation::VirtualFP); 478218885Sdim Moves.push_back(MachineMove(ReadyLabel, FPDst, FPSrc)); 479218885Sdim } 480218885Sdim } 481218885Sdim 482218885Sdim if (needsFrameMoves) { 483218885Sdim MCSymbol *Label = HasFP ? ReadyLabel : FrameLabel; 484218885Sdim 485218885Sdim // Add callee saved registers to move list. 486218885Sdim const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); 487218885Sdim for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 488218885Sdim int Offset = MFI->getObjectOffset(CSI[I].getFrameIdx()); 489218885Sdim unsigned Reg = CSI[I].getReg(); 490218885Sdim if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue; 491218885Sdim MachineLocation CSDst(MachineLocation::VirtualFP, Offset); 492218885Sdim MachineLocation CSSrc(Reg); 493218885Sdim Moves.push_back(MachineMove(Label, CSDst, CSSrc)); 494218885Sdim } 495218885Sdim } 496218885Sdim} 497218885Sdim 498218885Sdimvoid PPCFrameLowering::emitEpilogue(MachineFunction &MF, 499218885Sdim MachineBasicBlock &MBB) const { 500218885Sdim MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 501218885Sdim assert(MBBI != MBB.end() && "Returning block has no terminator"); 502218885Sdim const PPCInstrInfo &TII = 503218885Sdim *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo()); 504218885Sdim 505218885Sdim unsigned RetOpcode = MBBI->getOpcode(); 506218885Sdim DebugLoc dl; 507218885Sdim 508218885Sdim assert((RetOpcode == PPC::BLR || 509218885Sdim RetOpcode == PPC::TCRETURNri || 510218885Sdim RetOpcode == PPC::TCRETURNdi || 511218885Sdim RetOpcode == PPC::TCRETURNai || 512218885Sdim RetOpcode == PPC::TCRETURNri8 || 513218885Sdim RetOpcode == PPC::TCRETURNdi8 || 514218885Sdim RetOpcode == PPC::TCRETURNai8) && 515218885Sdim "Can only insert epilog into returning blocks"); 516218885Sdim 517218885Sdim // Get alignment info so we know how to restore r1 518218885Sdim const MachineFrameInfo *MFI = MF.getFrameInfo(); 519218885Sdim unsigned TargetAlign = getStackAlignment(); 520218885Sdim unsigned MaxAlign = MFI->getMaxAlignment(); 521218885Sdim 522218885Sdim // Get the number of bytes allocated from the FrameInfo. 523218885Sdim int FrameSize = MFI->getStackSize(); 524218885Sdim 525218885Sdim // Get processor type. 526218885Sdim bool isPPC64 = Subtarget.isPPC64(); 527218885Sdim // Get operating system 528218885Sdim bool isDarwinABI = Subtarget.isDarwinABI(); 529218885Sdim // Check if the link register (LR) has been saved. 530218885Sdim PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 531218885Sdim bool MustSaveLR = FI->mustSaveLR(); 532218885Sdim // Do we have a frame pointer for this function? 533218885Sdim bool HasFP = hasFP(MF); 534218885Sdim 535218885Sdim int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI); 536218885Sdim 537218885Sdim int FPOffset = 0; 538218885Sdim if (HasFP) { 539218885Sdim if (Subtarget.isSVR4ABI()) { 540218885Sdim MachineFrameInfo *FFI = MF.getFrameInfo(); 541218885Sdim int FPIndex = FI->getFramePointerSaveIndex(); 542218885Sdim assert(FPIndex && "No Frame Pointer Save Slot!"); 543218885Sdim FPOffset = FFI->getObjectOffset(FPIndex); 544218885Sdim } else { 545218885Sdim FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI); 546218885Sdim } 547218885Sdim } 548218885Sdim 549218885Sdim bool UsesTCRet = RetOpcode == PPC::TCRETURNri || 550218885Sdim RetOpcode == PPC::TCRETURNdi || 551218885Sdim RetOpcode == PPC::TCRETURNai || 552218885Sdim RetOpcode == PPC::TCRETURNri8 || 553218885Sdim RetOpcode == PPC::TCRETURNdi8 || 554218885Sdim RetOpcode == PPC::TCRETURNai8; 555218885Sdim 556218885Sdim if (UsesTCRet) { 557218885Sdim int MaxTCRetDelta = FI->getTailCallSPDelta(); 558218885Sdim MachineOperand &StackAdjust = MBBI->getOperand(1); 559218885Sdim assert(StackAdjust.isImm() && "Expecting immediate value."); 560218885Sdim // Adjust stack pointer. 561218885Sdim int StackAdj = StackAdjust.getImm(); 562218885Sdim int Delta = StackAdj - MaxTCRetDelta; 563218885Sdim assert((Delta >= 0) && "Delta must be positive"); 564218885Sdim if (MaxTCRetDelta>0) 565218885Sdim FrameSize += (StackAdj +Delta); 566218885Sdim else 567218885Sdim FrameSize += StackAdj; 568218885Sdim } 569218885Sdim 570218885Sdim if (FrameSize) { 571218885Sdim // The loaded (or persistent) stack pointer value is offset by the 'stwu' 572218885Sdim // on entry to the function. Add this offset back now. 573218885Sdim if (!isPPC64) { 574218885Sdim // If this function contained a fastcc call and GuaranteedTailCallOpt is 575218885Sdim // enabled (=> hasFastCall()==true) the fastcc call might contain a tail 576218885Sdim // call which invalidates the stack pointer value in SP(0). So we use the 577218885Sdim // value of R31 in this case. 578218885Sdim if (FI->hasFastCall() && isInt<16>(FrameSize)) { 579218885Sdim assert(hasFP(MF) && "Expecting a valid the frame pointer."); 580218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1) 581218885Sdim .addReg(PPC::R31).addImm(FrameSize); 582218885Sdim } else if(FI->hasFastCall()) { 583218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0) 584218885Sdim .addImm(FrameSize >> 16); 585218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0) 586218885Sdim .addReg(PPC::R0, RegState::Kill) 587218885Sdim .addImm(FrameSize & 0xFFFF); 588218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD4)) 589218885Sdim .addReg(PPC::R1) 590218885Sdim .addReg(PPC::R31) 591218885Sdim .addReg(PPC::R0); 592218885Sdim } else if (isInt<16>(FrameSize) && 593218885Sdim (!ALIGN_STACK || TargetAlign >= MaxAlign) && 594218885Sdim !MFI->hasVarSizedObjects()) { 595218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1) 596218885Sdim .addReg(PPC::R1).addImm(FrameSize); 597218885Sdim } else { 598218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ),PPC::R1) 599218885Sdim .addImm(0).addReg(PPC::R1); 600218885Sdim } 601218885Sdim } else { 602218885Sdim if (FI->hasFastCall() && isInt<16>(FrameSize)) { 603218885Sdim assert(hasFP(MF) && "Expecting a valid the frame pointer."); 604218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1) 605218885Sdim .addReg(PPC::X31).addImm(FrameSize); 606218885Sdim } else if(FI->hasFastCall()) { 607218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0) 608218885Sdim .addImm(FrameSize >> 16); 609218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0) 610218885Sdim .addReg(PPC::X0, RegState::Kill) 611218885Sdim .addImm(FrameSize & 0xFFFF); 612218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD8)) 613218885Sdim .addReg(PPC::X1) 614218885Sdim .addReg(PPC::X31) 615218885Sdim .addReg(PPC::X0); 616218885Sdim } else if (isInt<16>(FrameSize) && TargetAlign >= MaxAlign && 617218885Sdim !MFI->hasVarSizedObjects()) { 618218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1) 619218885Sdim .addReg(PPC::X1).addImm(FrameSize); 620218885Sdim } else { 621218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X1) 622218885Sdim .addImm(0).addReg(PPC::X1); 623218885Sdim } 624218885Sdim } 625218885Sdim } 626218885Sdim 627218885Sdim if (isPPC64) { 628218885Sdim if (MustSaveLR) 629218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X0) 630218885Sdim .addImm(LROffset/4).addReg(PPC::X1); 631218885Sdim 632218885Sdim if (HasFP) 633218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X31) 634218885Sdim .addImm(FPOffset/4).addReg(PPC::X1); 635218885Sdim 636218885Sdim if (MustSaveLR) 637218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR8)).addReg(PPC::X0); 638218885Sdim } else { 639218885Sdim if (MustSaveLR) 640218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R0) 641218885Sdim .addImm(LROffset).addReg(PPC::R1); 642218885Sdim 643218885Sdim if (HasFP) 644218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R31) 645218885Sdim .addImm(FPOffset).addReg(PPC::R1); 646218885Sdim 647218885Sdim if (MustSaveLR) 648218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR)).addReg(PPC::R0); 649218885Sdim } 650218885Sdim 651218885Sdim // Callee pop calling convention. Pop parameter/linkage area. Used for tail 652218885Sdim // call optimization 653218885Sdim if (GuaranteedTailCallOpt && RetOpcode == PPC::BLR && 654218885Sdim MF.getFunction()->getCallingConv() == CallingConv::Fast) { 655218885Sdim PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 656218885Sdim unsigned CallerAllocatedAmt = FI->getMinReservedArea(); 657218885Sdim unsigned StackReg = isPPC64 ? PPC::X1 : PPC::R1; 658218885Sdim unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31; 659218885Sdim unsigned TmpReg = isPPC64 ? PPC::X0 : PPC::R0; 660218885Sdim unsigned ADDIInstr = isPPC64 ? PPC::ADDI8 : PPC::ADDI; 661218885Sdim unsigned ADDInstr = isPPC64 ? PPC::ADD8 : PPC::ADD4; 662218885Sdim unsigned LISInstr = isPPC64 ? PPC::LIS8 : PPC::LIS; 663218885Sdim unsigned ORIInstr = isPPC64 ? PPC::ORI8 : PPC::ORI; 664218885Sdim 665218885Sdim if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) { 666218885Sdim BuildMI(MBB, MBBI, dl, TII.get(ADDIInstr), StackReg) 667218885Sdim .addReg(StackReg).addImm(CallerAllocatedAmt); 668218885Sdim } else { 669218885Sdim BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg) 670218885Sdim .addImm(CallerAllocatedAmt >> 16); 671218885Sdim BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg) 672218885Sdim .addReg(TmpReg, RegState::Kill) 673218885Sdim .addImm(CallerAllocatedAmt & 0xFFFF); 674218885Sdim BuildMI(MBB, MBBI, dl, TII.get(ADDInstr)) 675218885Sdim .addReg(StackReg) 676218885Sdim .addReg(FPReg) 677218885Sdim .addReg(TmpReg); 678218885Sdim } 679218885Sdim } else if (RetOpcode == PPC::TCRETURNdi) { 680218885Sdim MBBI = MBB.getLastNonDebugInstr(); 681218885Sdim MachineOperand &JumpTarget = MBBI->getOperand(0); 682218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)). 683218885Sdim addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset()); 684218885Sdim } else if (RetOpcode == PPC::TCRETURNri) { 685218885Sdim MBBI = MBB.getLastNonDebugInstr(); 686218885Sdim assert(MBBI->getOperand(0).isReg() && "Expecting register operand."); 687218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR)); 688218885Sdim } else if (RetOpcode == PPC::TCRETURNai) { 689218885Sdim MBBI = MBB.getLastNonDebugInstr(); 690218885Sdim MachineOperand &JumpTarget = MBBI->getOperand(0); 691218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm()); 692218885Sdim } else if (RetOpcode == PPC::TCRETURNdi8) { 693218885Sdim MBBI = MBB.getLastNonDebugInstr(); 694218885Sdim MachineOperand &JumpTarget = MBBI->getOperand(0); 695218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)). 696218885Sdim addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset()); 697218885Sdim } else if (RetOpcode == PPC::TCRETURNri8) { 698218885Sdim MBBI = MBB.getLastNonDebugInstr(); 699218885Sdim assert(MBBI->getOperand(0).isReg() && "Expecting register operand."); 700218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8)); 701218885Sdim } else if (RetOpcode == PPC::TCRETURNai8) { 702218885Sdim MBBI = MBB.getLastNonDebugInstr(); 703218885Sdim MachineOperand &JumpTarget = MBBI->getOperand(0); 704218885Sdim BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm()); 705218885Sdim } 706218885Sdim} 707218885Sdim 708218885Sdimvoid PPCFrameLowering::getInitialFrameState(std::vector<MachineMove> &Moves) const { 709218885Sdim // Initial state of the frame pointer is R1. 710218885Sdim MachineLocation Dst(MachineLocation::VirtualFP); 711218885Sdim MachineLocation Src(PPC::R1, 0); 712218885Sdim Moves.push_back(MachineMove(0, Dst, Src)); 713218885Sdim} 714218885Sdim 715218885Sdimstatic bool spillsCR(const MachineFunction &MF) { 716218885Sdim const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 717218885Sdim return FuncInfo->isCRSpilled(); 718218885Sdim} 719218885Sdim 720218885Sdim/// MustSaveLR - Return true if this function requires that we save the LR 721218885Sdim/// register onto the stack in the prolog and restore it in the epilog of the 722218885Sdim/// function. 723218885Sdimstatic bool MustSaveLR(const MachineFunction &MF, unsigned LR) { 724218885Sdim const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>(); 725218885Sdim 726218885Sdim // We need a save/restore of LR if there is any def of LR (which is 727218885Sdim // defined by calls, including the PIC setup sequence), or if there is 728218885Sdim // some use of the LR stack slot (e.g. for builtin_return_address). 729218885Sdim // (LR comes in 32 and 64 bit versions.) 730218885Sdim MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR); 731218885Sdim return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired(); 732218885Sdim} 733218885Sdim 734218885Sdimvoid 735218885SdimPPCFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF, 736218885Sdim RegScavenger *RS) const { 737218885Sdim const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo(); 738218885Sdim 739218885Sdim // Save and clear the LR state. 740218885Sdim PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 741218885Sdim unsigned LR = RegInfo->getRARegister(); 742218885Sdim FI->setMustSaveLR(MustSaveLR(MF, LR)); 743218885Sdim MF.getRegInfo().setPhysRegUnused(LR); 744218885Sdim 745218885Sdim // Save R31 if necessary 746218885Sdim int FPSI = FI->getFramePointerSaveIndex(); 747218885Sdim bool isPPC64 = Subtarget.isPPC64(); 748218885Sdim bool isDarwinABI = Subtarget.isDarwinABI(); 749218885Sdim MachineFrameInfo *MFI = MF.getFrameInfo(); 750218885Sdim 751218885Sdim // If the frame pointer save index hasn't been defined yet. 752218885Sdim if (!FPSI && needsFP(MF)) { 753218885Sdim // Find out what the fix offset of the frame pointer save area. 754218885Sdim int FPOffset = getFramePointerSaveOffset(isPPC64, isDarwinABI); 755218885Sdim // Allocate the frame index for frame pointer save area. 756218885Sdim FPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true); 757218885Sdim // Save the result. 758218885Sdim FI->setFramePointerSaveIndex(FPSI); 759218885Sdim } 760218885Sdim 761218885Sdim // Reserve stack space to move the linkage area to in case of a tail call. 762218885Sdim int TCSPDelta = 0; 763218885Sdim if (GuaranteedTailCallOpt && (TCSPDelta = FI->getTailCallSPDelta()) < 0) { 764218885Sdim MFI->CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true); 765218885Sdim } 766218885Sdim 767218885Sdim // Reserve a slot closest to SP or frame pointer if we have a dynalloc or 768218885Sdim // a large stack, which will require scavenging a register to materialize a 769218885Sdim // large offset. 770218885Sdim // FIXME: this doesn't actually check stack size, so is a bit pessimistic 771218885Sdim // FIXME: doesn't detect whether or not we need to spill vXX, which requires 772218885Sdim // r0 for now. 773218885Sdim 774218885Sdim if (RegInfo->requiresRegisterScavenging(MF)) // FIXME (64-bit): Enable. 775218885Sdim if (needsFP(MF) || spillsCR(MF)) { 776218885Sdim const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 777218885Sdim const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 778218885Sdim const TargetRegisterClass *RC = isPPC64 ? G8RC : GPRC; 779218885Sdim RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(), 780218885Sdim RC->getAlignment(), 781218885Sdim false)); 782218885Sdim } 783218885Sdim} 784218885Sdim 785218885Sdimvoid PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF) 786218885Sdim const { 787218885Sdim // Early exit if not using the SVR4 ABI. 788218885Sdim if (!Subtarget.isSVR4ABI()) 789218885Sdim return; 790218885Sdim 791218885Sdim // Get callee saved register information. 792218885Sdim MachineFrameInfo *FFI = MF.getFrameInfo(); 793218885Sdim const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo(); 794218885Sdim 795218885Sdim // Early exit if no callee saved registers are modified! 796218885Sdim if (CSI.empty() && !needsFP(MF)) { 797218885Sdim return; 798218885Sdim } 799218885Sdim 800218885Sdim unsigned MinGPR = PPC::R31; 801218885Sdim unsigned MinG8R = PPC::X31; 802218885Sdim unsigned MinFPR = PPC::F31; 803218885Sdim unsigned MinVR = PPC::V31; 804218885Sdim 805218885Sdim bool HasGPSaveArea = false; 806218885Sdim bool HasG8SaveArea = false; 807218885Sdim bool HasFPSaveArea = false; 808218885Sdim bool HasCRSaveArea = false; 809218885Sdim bool HasVRSAVESaveArea = false; 810218885Sdim bool HasVRSaveArea = false; 811218885Sdim 812218885Sdim SmallVector<CalleeSavedInfo, 18> GPRegs; 813218885Sdim SmallVector<CalleeSavedInfo, 18> G8Regs; 814218885Sdim SmallVector<CalleeSavedInfo, 18> FPRegs; 815218885Sdim SmallVector<CalleeSavedInfo, 18> VRegs; 816218885Sdim 817218885Sdim for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 818218885Sdim unsigned Reg = CSI[i].getReg(); 819218885Sdim if (PPC::GPRCRegisterClass->contains(Reg)) { 820218885Sdim HasGPSaveArea = true; 821218885Sdim 822218885Sdim GPRegs.push_back(CSI[i]); 823218885Sdim 824218885Sdim if (Reg < MinGPR) { 825218885Sdim MinGPR = Reg; 826218885Sdim } 827218885Sdim } else if (PPC::G8RCRegisterClass->contains(Reg)) { 828218885Sdim HasG8SaveArea = true; 829218885Sdim 830218885Sdim G8Regs.push_back(CSI[i]); 831218885Sdim 832218885Sdim if (Reg < MinG8R) { 833218885Sdim MinG8R = Reg; 834218885Sdim } 835218885Sdim } else if (PPC::F8RCRegisterClass->contains(Reg)) { 836218885Sdim HasFPSaveArea = true; 837218885Sdim 838218885Sdim FPRegs.push_back(CSI[i]); 839218885Sdim 840218885Sdim if (Reg < MinFPR) { 841218885Sdim MinFPR = Reg; 842218885Sdim } 843218885Sdim// FIXME SVR4: Disable CR save area for now. 844218885Sdim } else if (PPC::CRBITRCRegisterClass->contains(Reg) 845218885Sdim || PPC::CRRCRegisterClass->contains(Reg)) { 846218885Sdim// HasCRSaveArea = true; 847218885Sdim } else if (PPC::VRSAVERCRegisterClass->contains(Reg)) { 848218885Sdim HasVRSAVESaveArea = true; 849218885Sdim } else if (PPC::VRRCRegisterClass->contains(Reg)) { 850218885Sdim HasVRSaveArea = true; 851218885Sdim 852218885Sdim VRegs.push_back(CSI[i]); 853218885Sdim 854218885Sdim if (Reg < MinVR) { 855218885Sdim MinVR = Reg; 856218885Sdim } 857218885Sdim } else { 858218885Sdim llvm_unreachable("Unknown RegisterClass!"); 859218885Sdim } 860218885Sdim } 861218885Sdim 862218885Sdim PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>(); 863218885Sdim 864218885Sdim int64_t LowerBound = 0; 865218885Sdim 866218885Sdim // Take into account stack space reserved for tail calls. 867218885Sdim int TCSPDelta = 0; 868218885Sdim if (GuaranteedTailCallOpt && (TCSPDelta = PFI->getTailCallSPDelta()) < 0) { 869218885Sdim LowerBound = TCSPDelta; 870218885Sdim } 871218885Sdim 872218885Sdim // The Floating-point register save area is right below the back chain word 873218885Sdim // of the previous stack frame. 874218885Sdim if (HasFPSaveArea) { 875218885Sdim for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) { 876218885Sdim int FI = FPRegs[i].getFrameIdx(); 877218885Sdim 878218885Sdim FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); 879218885Sdim } 880218885Sdim 881218885Sdim LowerBound -= (31 - PPCRegisterInfo::getRegisterNumbering(MinFPR) + 1) * 8; 882218885Sdim } 883218885Sdim 884218885Sdim // Check whether the frame pointer register is allocated. If so, make sure it 885218885Sdim // is spilled to the correct offset. 886218885Sdim if (needsFP(MF)) { 887218885Sdim HasGPSaveArea = true; 888218885Sdim 889218885Sdim int FI = PFI->getFramePointerSaveIndex(); 890218885Sdim assert(FI && "No Frame Pointer Save Slot!"); 891218885Sdim 892218885Sdim FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); 893218885Sdim } 894218885Sdim 895218885Sdim // General register save area starts right below the Floating-point 896218885Sdim // register save area. 897218885Sdim if (HasGPSaveArea || HasG8SaveArea) { 898218885Sdim // Move general register save area spill slots down, taking into account 899218885Sdim // the size of the Floating-point register save area. 900218885Sdim for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) { 901218885Sdim int FI = GPRegs[i].getFrameIdx(); 902218885Sdim 903218885Sdim FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); 904218885Sdim } 905218885Sdim 906218885Sdim // Move general register save area spill slots down, taking into account 907218885Sdim // the size of the Floating-point register save area. 908218885Sdim for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) { 909218885Sdim int FI = G8Regs[i].getFrameIdx(); 910218885Sdim 911218885Sdim FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); 912218885Sdim } 913218885Sdim 914218885Sdim unsigned MinReg = 915218885Sdim std::min<unsigned>(PPCRegisterInfo::getRegisterNumbering(MinGPR), 916218885Sdim PPCRegisterInfo::getRegisterNumbering(MinG8R)); 917218885Sdim 918218885Sdim if (Subtarget.isPPC64()) { 919218885Sdim LowerBound -= (31 - MinReg + 1) * 8; 920218885Sdim } else { 921218885Sdim LowerBound -= (31 - MinReg + 1) * 4; 922218885Sdim } 923218885Sdim } 924218885Sdim 925218885Sdim // The CR save area is below the general register save area. 926218885Sdim if (HasCRSaveArea) { 927218885Sdim // FIXME SVR4: Is it actually possible to have multiple elements in CSI 928218885Sdim // which have the CR/CRBIT register class? 929218885Sdim // Adjust the frame index of the CR spill slot. 930218885Sdim for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 931218885Sdim unsigned Reg = CSI[i].getReg(); 932218885Sdim 933218885Sdim if (PPC::CRBITRCRegisterClass->contains(Reg) || 934218885Sdim PPC::CRRCRegisterClass->contains(Reg)) { 935218885Sdim int FI = CSI[i].getFrameIdx(); 936218885Sdim 937218885Sdim FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); 938218885Sdim } 939218885Sdim } 940218885Sdim 941218885Sdim LowerBound -= 4; // The CR save area is always 4 bytes long. 942218885Sdim } 943218885Sdim 944218885Sdim if (HasVRSAVESaveArea) { 945218885Sdim // FIXME SVR4: Is it actually possible to have multiple elements in CSI 946218885Sdim // which have the VRSAVE register class? 947218885Sdim // Adjust the frame index of the VRSAVE spill slot. 948218885Sdim for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 949218885Sdim unsigned Reg = CSI[i].getReg(); 950218885Sdim 951218885Sdim if (PPC::VRSAVERCRegisterClass->contains(Reg)) { 952218885Sdim int FI = CSI[i].getFrameIdx(); 953218885Sdim 954218885Sdim FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); 955218885Sdim } 956218885Sdim } 957218885Sdim 958218885Sdim LowerBound -= 4; // The VRSAVE save area is always 4 bytes long. 959218885Sdim } 960218885Sdim 961218885Sdim if (HasVRSaveArea) { 962218885Sdim // Insert alignment padding, we need 16-byte alignment. 963218885Sdim LowerBound = (LowerBound - 15) & ~(15); 964218885Sdim 965218885Sdim for (unsigned i = 0, e = VRegs.size(); i != e; ++i) { 966218885Sdim int FI = VRegs[i].getFrameIdx(); 967218885Sdim 968218885Sdim FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); 969218885Sdim } 970218885Sdim } 971218885Sdim} 972