12754fe60SDimitry Andric //=======- ARMFrameLowering.cpp - ARM Frame Information --------*- C++ -*-====// 22754fe60SDimitry Andric // 32754fe60SDimitry Andric // The LLVM Compiler Infrastructure 42754fe60SDimitry Andric // 52754fe60SDimitry Andric // This file is distributed under the University of Illinois Open Source 62754fe60SDimitry Andric // License. See LICENSE.TXT for details. 72754fe60SDimitry Andric // 82754fe60SDimitry Andric //===----------------------------------------------------------------------===// 92754fe60SDimitry Andric // 102754fe60SDimitry Andric // This file contains the ARM implementation of TargetFrameLowering class. 112754fe60SDimitry Andric // 122754fe60SDimitry Andric //===----------------------------------------------------------------------===// 132754fe60SDimitry Andric 142754fe60SDimitry Andric #include "ARMFrameLowering.h" 152754fe60SDimitry Andric #include "ARMAddressingModes.h" 162754fe60SDimitry Andric #include "ARMBaseInstrInfo.h" 172754fe60SDimitry Andric #include "ARMBaseRegisterInfo.h" 182754fe60SDimitry Andric #include "ARMMachineFunctionInfo.h" 192754fe60SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 202754fe60SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 212754fe60SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 222754fe60SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 232754fe60SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h" 242754fe60SDimitry Andric #include "llvm/Target/TargetOptions.h" 252754fe60SDimitry Andric 262754fe60SDimitry Andric using namespace llvm; 272754fe60SDimitry Andric 282754fe60SDimitry Andric /// hasFP - Return true if the specified function should have a dedicated frame 292754fe60SDimitry Andric /// pointer register. This is true if the function has variable sized allocas 302754fe60SDimitry Andric /// or if frame pointer elimination is disabled. 312754fe60SDimitry Andric bool ARMFrameLowering::hasFP(const MachineFunction &MF) const { 322754fe60SDimitry Andric const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo(); 332754fe60SDimitry Andric 342754fe60SDimitry Andric // Mac OS X requires FP not to be clobbered for backtracing purpose. 352754fe60SDimitry Andric if (STI.isTargetDarwin()) 362754fe60SDimitry Andric return true; 372754fe60SDimitry Andric 382754fe60SDimitry Andric const MachineFrameInfo *MFI = MF.getFrameInfo(); 392754fe60SDimitry Andric // Always eliminate non-leaf frame pointers. 402754fe60SDimitry Andric return ((DisableFramePointerElim(MF) && MFI->hasCalls()) || 412754fe60SDimitry Andric RegInfo->needsStackRealignment(MF) || 422754fe60SDimitry Andric MFI->hasVarSizedObjects() || 432754fe60SDimitry Andric MFI->isFrameAddressTaken()); 442754fe60SDimitry Andric } 452754fe60SDimitry Andric 462754fe60SDimitry Andric /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is 472754fe60SDimitry Andric /// not required, we reserve argument space for call sites in the function 482754fe60SDimitry Andric /// immediately on entry to the current function. This eliminates the need for 492754fe60SDimitry Andric /// add/sub sp brackets around call sites. Returns true if the call frame is 502754fe60SDimitry Andric /// included as part of the stack frame. 512754fe60SDimitry Andric bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { 522754fe60SDimitry Andric const MachineFrameInfo *FFI = MF.getFrameInfo(); 532754fe60SDimitry Andric unsigned CFSize = FFI->getMaxCallFrameSize(); 542754fe60SDimitry Andric // It's not always a good idea to include the call frame as part of the 552754fe60SDimitry Andric // stack frame. ARM (especially Thumb) has small immediate offset to 562754fe60SDimitry Andric // address the stack frame. So a large call frame can cause poor codegen 572754fe60SDimitry Andric // and may even makes it impossible to scavenge a register. 582754fe60SDimitry Andric if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12 592754fe60SDimitry Andric return false; 602754fe60SDimitry Andric 612754fe60SDimitry Andric return !MF.getFrameInfo()->hasVarSizedObjects(); 622754fe60SDimitry Andric } 632754fe60SDimitry Andric 642754fe60SDimitry Andric /// canSimplifyCallFramePseudos - If there is a reserved call frame, the 652754fe60SDimitry Andric /// call frame pseudos can be simplified. Unlike most targets, having a FP 662754fe60SDimitry Andric /// is not sufficient here since we still may reference some objects via SP 672754fe60SDimitry Andric /// even when FP is available in Thumb2 mode. 682754fe60SDimitry Andric bool 692754fe60SDimitry Andric ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const { 702754fe60SDimitry Andric return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects(); 712754fe60SDimitry Andric } 722754fe60SDimitry Andric 732754fe60SDimitry Andric static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) { 742754fe60SDimitry Andric for (unsigned i = 0; CSRegs[i]; ++i) 752754fe60SDimitry Andric if (Reg == CSRegs[i]) 762754fe60SDimitry Andric return true; 772754fe60SDimitry Andric return false; 782754fe60SDimitry Andric } 792754fe60SDimitry Andric 802754fe60SDimitry Andric static bool isCSRestore(MachineInstr *MI, 812754fe60SDimitry Andric const ARMBaseInstrInfo &TII, 822754fe60SDimitry Andric const unsigned *CSRegs) { 832754fe60SDimitry Andric // Integer spill area is handled with "pop". 842754fe60SDimitry Andric if (MI->getOpcode() == ARM::LDMIA_RET || 852754fe60SDimitry Andric MI->getOpcode() == ARM::t2LDMIA_RET || 862754fe60SDimitry Andric MI->getOpcode() == ARM::LDMIA_UPD || 872754fe60SDimitry Andric MI->getOpcode() == ARM::t2LDMIA_UPD || 882754fe60SDimitry Andric MI->getOpcode() == ARM::VLDMDIA_UPD) { 892754fe60SDimitry Andric // The first two operands are predicates. The last two are 902754fe60SDimitry Andric // imp-def and imp-use of SP. Check everything in between. 912754fe60SDimitry Andric for (int i = 5, e = MI->getNumOperands(); i != e; ++i) 922754fe60SDimitry Andric if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs)) 932754fe60SDimitry Andric return false; 942754fe60SDimitry Andric return true; 952754fe60SDimitry Andric } 962754fe60SDimitry Andric if ((MI->getOpcode() == ARM::LDR_POST || 972754fe60SDimitry Andric MI->getOpcode() == ARM::t2LDR_POST) && 982754fe60SDimitry Andric isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs) && 992754fe60SDimitry Andric MI->getOperand(1).getReg() == ARM::SP) 1002754fe60SDimitry Andric return true; 1012754fe60SDimitry Andric 1022754fe60SDimitry Andric return false; 1032754fe60SDimitry Andric } 1042754fe60SDimitry Andric 1052754fe60SDimitry Andric static void 1062754fe60SDimitry Andric emitSPUpdate(bool isARM, 1072754fe60SDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, 1082754fe60SDimitry Andric DebugLoc dl, const ARMBaseInstrInfo &TII, 1093b0f4066SDimitry Andric int NumBytes, unsigned MIFlags = MachineInstr::NoFlags) { 1102754fe60SDimitry Andric if (isARM) 1112754fe60SDimitry Andric emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, 1123b0f4066SDimitry Andric ARMCC::AL, 0, TII, MIFlags); 1132754fe60SDimitry Andric else 1142754fe60SDimitry Andric emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, 1153b0f4066SDimitry Andric ARMCC::AL, 0, TII, MIFlags); 1162754fe60SDimitry Andric } 1172754fe60SDimitry Andric 1182754fe60SDimitry Andric void ARMFrameLowering::emitPrologue(MachineFunction &MF) const { 1192754fe60SDimitry Andric MachineBasicBlock &MBB = MF.front(); 1202754fe60SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.begin(); 1212754fe60SDimitry Andric MachineFrameInfo *MFI = MF.getFrameInfo(); 1222754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1232754fe60SDimitry Andric const ARMBaseRegisterInfo *RegInfo = 1242754fe60SDimitry Andric static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo()); 1252754fe60SDimitry Andric const ARMBaseInstrInfo &TII = 1262754fe60SDimitry Andric *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo()); 1272754fe60SDimitry Andric assert(!AFI->isThumb1OnlyFunction() && 1282754fe60SDimitry Andric "This emitPrologue does not support Thumb1!"); 1292754fe60SDimitry Andric bool isARM = !AFI->isThumbFunction(); 1302754fe60SDimitry Andric unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize(); 1312754fe60SDimitry Andric unsigned NumBytes = MFI->getStackSize(); 1322754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); 1332754fe60SDimitry Andric DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 1342754fe60SDimitry Andric unsigned FramePtr = RegInfo->getFrameRegister(MF); 1352754fe60SDimitry Andric 1362754fe60SDimitry Andric // Determine the sizes of each callee-save spill areas and record which frame 1372754fe60SDimitry Andric // belongs to which callee-save spill areas. 1382754fe60SDimitry Andric unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0; 1392754fe60SDimitry Andric int FramePtrSpillFI = 0; 1402754fe60SDimitry Andric 1412754fe60SDimitry Andric // Allocate the vararg register save area. This is not counted in NumBytes. 1422754fe60SDimitry Andric if (VARegSaveSize) 1433b0f4066SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, -VARegSaveSize, 1443b0f4066SDimitry Andric MachineInstr::FrameSetup); 1452754fe60SDimitry Andric 1462754fe60SDimitry Andric if (!AFI->hasStackFrame()) { 1472754fe60SDimitry Andric if (NumBytes != 0) 1483b0f4066SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes, 1493b0f4066SDimitry Andric MachineInstr::FrameSetup); 1502754fe60SDimitry Andric return; 1512754fe60SDimitry Andric } 1522754fe60SDimitry Andric 1532754fe60SDimitry Andric for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 1542754fe60SDimitry Andric unsigned Reg = CSI[i].getReg(); 1552754fe60SDimitry Andric int FI = CSI[i].getFrameIdx(); 1562754fe60SDimitry Andric switch (Reg) { 1572754fe60SDimitry Andric case ARM::R4: 1582754fe60SDimitry Andric case ARM::R5: 1592754fe60SDimitry Andric case ARM::R6: 1602754fe60SDimitry Andric case ARM::R7: 1612754fe60SDimitry Andric case ARM::LR: 1622754fe60SDimitry Andric if (Reg == FramePtr) 1632754fe60SDimitry Andric FramePtrSpillFI = FI; 1642754fe60SDimitry Andric AFI->addGPRCalleeSavedArea1Frame(FI); 1652754fe60SDimitry Andric GPRCS1Size += 4; 1662754fe60SDimitry Andric break; 1672754fe60SDimitry Andric case ARM::R8: 1682754fe60SDimitry Andric case ARM::R9: 1692754fe60SDimitry Andric case ARM::R10: 1702754fe60SDimitry Andric case ARM::R11: 1712754fe60SDimitry Andric if (Reg == FramePtr) 1722754fe60SDimitry Andric FramePtrSpillFI = FI; 1732754fe60SDimitry Andric if (STI.isTargetDarwin()) { 1742754fe60SDimitry Andric AFI->addGPRCalleeSavedArea2Frame(FI); 1752754fe60SDimitry Andric GPRCS2Size += 4; 1762754fe60SDimitry Andric } else { 1772754fe60SDimitry Andric AFI->addGPRCalleeSavedArea1Frame(FI); 1782754fe60SDimitry Andric GPRCS1Size += 4; 1792754fe60SDimitry Andric } 1802754fe60SDimitry Andric break; 1812754fe60SDimitry Andric default: 1822754fe60SDimitry Andric AFI->addDPRCalleeSavedAreaFrame(FI); 1832754fe60SDimitry Andric DPRCSSize += 8; 1842754fe60SDimitry Andric } 1852754fe60SDimitry Andric } 1862754fe60SDimitry Andric 1872754fe60SDimitry Andric // Move past area 1. 1882754fe60SDimitry Andric if (GPRCS1Size > 0) MBBI++; 1892754fe60SDimitry Andric 1902754fe60SDimitry Andric // Set FP to point to the stack slot that contains the previous FP. 1912754fe60SDimitry Andric // For Darwin, FP is R7, which has now been stored in spill area 1. 1922754fe60SDimitry Andric // Otherwise, if this is not Darwin, all the callee-saved registers go 1932754fe60SDimitry Andric // into spill area 1, including the FP in R11. In either case, it is 1942754fe60SDimitry Andric // now safe to emit this assignment. 1952754fe60SDimitry Andric bool HasFP = hasFP(MF); 1962754fe60SDimitry Andric if (HasFP) { 1972754fe60SDimitry Andric unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : ARM::t2ADDri; 1982754fe60SDimitry Andric MachineInstrBuilder MIB = 1992754fe60SDimitry Andric BuildMI(MBB, MBBI, dl, TII.get(ADDriOpc), FramePtr) 2003b0f4066SDimitry Andric .addFrameIndex(FramePtrSpillFI).addImm(0) 2013b0f4066SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 2022754fe60SDimitry Andric AddDefaultCC(AddDefaultPred(MIB)); 2032754fe60SDimitry Andric } 2042754fe60SDimitry Andric 2052754fe60SDimitry Andric // Move past area 2. 2062754fe60SDimitry Andric if (GPRCS2Size > 0) MBBI++; 2072754fe60SDimitry Andric 2082754fe60SDimitry Andric // Determine starting offsets of spill areas. 2092754fe60SDimitry Andric unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize); 2102754fe60SDimitry Andric unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize; 2112754fe60SDimitry Andric unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size; 2122754fe60SDimitry Andric if (HasFP) 2132754fe60SDimitry Andric AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + 2142754fe60SDimitry Andric NumBytes); 2152754fe60SDimitry Andric AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset); 2162754fe60SDimitry Andric AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset); 2172754fe60SDimitry Andric AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset); 2182754fe60SDimitry Andric 2192754fe60SDimitry Andric // Move past area 3. 220dd6029ffSDimitry Andric if (DPRCSSize > 0) { 221dd6029ffSDimitry Andric MBBI++; 222dd6029ffSDimitry Andric // Since vpush register list cannot have gaps, there may be multiple vpush 223dd6029ffSDimitry Andric // instructions in the prologue. 224dd6029ffSDimitry Andric while (MBBI->getOpcode() == ARM::VSTMDDB_UPD) 225dd6029ffSDimitry Andric MBBI++; 226dd6029ffSDimitry Andric } 2272754fe60SDimitry Andric 2282754fe60SDimitry Andric NumBytes = DPRCSOffset; 2292754fe60SDimitry Andric if (NumBytes) { 2302754fe60SDimitry Andric // Adjust SP after all the callee-save spills. 2313b0f4066SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes, 2323b0f4066SDimitry Andric MachineInstr::FrameSetup); 2332754fe60SDimitry Andric if (HasFP && isARM) 2342754fe60SDimitry Andric // Restore from fp only in ARM mode: e.g. sub sp, r7, #24 2352754fe60SDimitry Andric // Note it's not safe to do this in Thumb2 mode because it would have 2362754fe60SDimitry Andric // taken two instructions: 2372754fe60SDimitry Andric // mov sp, r7 2382754fe60SDimitry Andric // sub sp, #24 2392754fe60SDimitry Andric // If an interrupt is taken between the two instructions, then sp is in 2402754fe60SDimitry Andric // an inconsistent state (pointing to the middle of callee-saved area). 2412754fe60SDimitry Andric // The interrupt handler can end up clobbering the registers. 2422754fe60SDimitry Andric AFI->setShouldRestoreSPFromFP(true); 2432754fe60SDimitry Andric } 2442754fe60SDimitry Andric 2452754fe60SDimitry Andric if (STI.isTargetELF() && hasFP(MF)) 2462754fe60SDimitry Andric MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() - 2472754fe60SDimitry Andric AFI->getFramePtrSpillOffset()); 2482754fe60SDimitry Andric 2492754fe60SDimitry Andric AFI->setGPRCalleeSavedArea1Size(GPRCS1Size); 2502754fe60SDimitry Andric AFI->setGPRCalleeSavedArea2Size(GPRCS2Size); 2512754fe60SDimitry Andric AFI->setDPRCalleeSavedAreaSize(DPRCSSize); 2522754fe60SDimitry Andric 2532754fe60SDimitry Andric // If we need dynamic stack realignment, do it here. Be paranoid and make 2542754fe60SDimitry Andric // sure if we also have VLAs, we have a base pointer for frame access. 2552754fe60SDimitry Andric if (RegInfo->needsStackRealignment(MF)) { 2562754fe60SDimitry Andric unsigned MaxAlign = MFI->getMaxAlignment(); 2572754fe60SDimitry Andric assert (!AFI->isThumb1OnlyFunction()); 2582754fe60SDimitry Andric if (!AFI->isThumbFunction()) { 2592754fe60SDimitry Andric // Emit bic sp, sp, MaxAlign 2602754fe60SDimitry Andric AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, 2612754fe60SDimitry Andric TII.get(ARM::BICri), ARM::SP) 2622754fe60SDimitry Andric .addReg(ARM::SP, RegState::Kill) 2632754fe60SDimitry Andric .addImm(MaxAlign-1))); 2642754fe60SDimitry Andric } else { 2652754fe60SDimitry Andric // We cannot use sp as source/dest register here, thus we're emitting the 2662754fe60SDimitry Andric // following sequence: 2672754fe60SDimitry Andric // mov r4, sp 2682754fe60SDimitry Andric // bic r4, r4, MaxAlign 2692754fe60SDimitry Andric // mov sp, r4 2702754fe60SDimitry Andric // FIXME: It will be better just to find spare register here. 2712754fe60SDimitry Andric BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2tgpr), ARM::R4) 2722754fe60SDimitry Andric .addReg(ARM::SP, RegState::Kill); 2732754fe60SDimitry Andric AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, 2742754fe60SDimitry Andric TII.get(ARM::t2BICri), ARM::R4) 2752754fe60SDimitry Andric .addReg(ARM::R4, RegState::Kill) 2762754fe60SDimitry Andric .addImm(MaxAlign-1))); 2772754fe60SDimitry Andric BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVtgpr2gpr), ARM::SP) 2782754fe60SDimitry Andric .addReg(ARM::R4, RegState::Kill); 2792754fe60SDimitry Andric } 2802754fe60SDimitry Andric 2812754fe60SDimitry Andric AFI->setShouldRestoreSPFromFP(true); 2822754fe60SDimitry Andric } 2832754fe60SDimitry Andric 2842754fe60SDimitry Andric // If we need a base pointer, set it up here. It's whatever the value 2852754fe60SDimitry Andric // of the stack pointer is at this point. Any variable size objects 2862754fe60SDimitry Andric // will be allocated after this, so we can still use the base pointer 2872754fe60SDimitry Andric // to reference locals. 2883b0f4066SDimitry Andric // FIXME: Clarify FrameSetup flags here. 2892754fe60SDimitry Andric if (RegInfo->hasBasePointer(MF)) { 2902754fe60SDimitry Andric if (isARM) 2912754fe60SDimitry Andric BuildMI(MBB, MBBI, dl, 2922754fe60SDimitry Andric TII.get(ARM::MOVr), RegInfo->getBaseRegister()) 2932754fe60SDimitry Andric .addReg(ARM::SP) 2942754fe60SDimitry Andric .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); 2952754fe60SDimitry Andric else 2962754fe60SDimitry Andric BuildMI(MBB, MBBI, dl, 2972754fe60SDimitry Andric TII.get(ARM::tMOVgpr2gpr), RegInfo->getBaseRegister()) 2982754fe60SDimitry Andric .addReg(ARM::SP); 2992754fe60SDimitry Andric } 3002754fe60SDimitry Andric 3012754fe60SDimitry Andric // If the frame has variable sized objects then the epilogue must restore 3022754fe60SDimitry Andric // the sp from fp. We can assume there's an FP here since hasFP already 3032754fe60SDimitry Andric // checks for hasVarSizedObjects. 3042754fe60SDimitry Andric if (MFI->hasVarSizedObjects()) 3052754fe60SDimitry Andric AFI->setShouldRestoreSPFromFP(true); 3062754fe60SDimitry Andric } 3072754fe60SDimitry Andric 3082754fe60SDimitry Andric void ARMFrameLowering::emitEpilogue(MachineFunction &MF, 3092754fe60SDimitry Andric MachineBasicBlock &MBB) const { 3102754fe60SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 3112754fe60SDimitry Andric assert(MBBI->getDesc().isReturn() && 3122754fe60SDimitry Andric "Can only insert epilog into returning blocks"); 3132754fe60SDimitry Andric unsigned RetOpcode = MBBI->getOpcode(); 3142754fe60SDimitry Andric DebugLoc dl = MBBI->getDebugLoc(); 3152754fe60SDimitry Andric MachineFrameInfo *MFI = MF.getFrameInfo(); 3162754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3172754fe60SDimitry Andric const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo(); 3182754fe60SDimitry Andric const ARMBaseInstrInfo &TII = 3192754fe60SDimitry Andric *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo()); 3202754fe60SDimitry Andric assert(!AFI->isThumb1OnlyFunction() && 3212754fe60SDimitry Andric "This emitEpilogue does not support Thumb1!"); 3222754fe60SDimitry Andric bool isARM = !AFI->isThumbFunction(); 3232754fe60SDimitry Andric 3242754fe60SDimitry Andric unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize(); 3252754fe60SDimitry Andric int NumBytes = (int)MFI->getStackSize(); 3262754fe60SDimitry Andric unsigned FramePtr = RegInfo->getFrameRegister(MF); 3272754fe60SDimitry Andric 3282754fe60SDimitry Andric if (!AFI->hasStackFrame()) { 3292754fe60SDimitry Andric if (NumBytes != 0) 3302754fe60SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes); 3312754fe60SDimitry Andric } else { 3322754fe60SDimitry Andric // Unwind MBBI to point to first LDR / VLDRD. 3332754fe60SDimitry Andric const unsigned *CSRegs = RegInfo->getCalleeSavedRegs(); 3342754fe60SDimitry Andric if (MBBI != MBB.begin()) { 3352754fe60SDimitry Andric do 3362754fe60SDimitry Andric --MBBI; 3372754fe60SDimitry Andric while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs)); 3382754fe60SDimitry Andric if (!isCSRestore(MBBI, TII, CSRegs)) 3392754fe60SDimitry Andric ++MBBI; 3402754fe60SDimitry Andric } 3412754fe60SDimitry Andric 3422754fe60SDimitry Andric // Move SP to start of FP callee save spill area. 3432754fe60SDimitry Andric NumBytes -= (AFI->getGPRCalleeSavedArea1Size() + 3442754fe60SDimitry Andric AFI->getGPRCalleeSavedArea2Size() + 3452754fe60SDimitry Andric AFI->getDPRCalleeSavedAreaSize()); 3462754fe60SDimitry Andric 3472754fe60SDimitry Andric // Reset SP based on frame pointer only if the stack frame extends beyond 3482754fe60SDimitry Andric // frame pointer stack slot or target is ELF and the function has FP. 3492754fe60SDimitry Andric if (AFI->shouldRestoreSPFromFP()) { 3502754fe60SDimitry Andric NumBytes = AFI->getFramePtrSpillOffset() - NumBytes; 3512754fe60SDimitry Andric if (NumBytes) { 3522754fe60SDimitry Andric if (isARM) 3532754fe60SDimitry Andric emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes, 3542754fe60SDimitry Andric ARMCC::AL, 0, TII); 3552754fe60SDimitry Andric else { 3562754fe60SDimitry Andric // It's not possible to restore SP from FP in a single instruction. 3572754fe60SDimitry Andric // For Darwin, this looks like: 3582754fe60SDimitry Andric // mov sp, r7 3592754fe60SDimitry Andric // sub sp, #24 3602754fe60SDimitry Andric // This is bad, if an interrupt is taken after the mov, sp is in an 3612754fe60SDimitry Andric // inconsistent state. 3622754fe60SDimitry Andric // Use the first callee-saved register as a scratch register. 3632754fe60SDimitry Andric assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) && 3642754fe60SDimitry Andric "No scratch register to restore SP from FP!"); 3652754fe60SDimitry Andric emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes, 3662754fe60SDimitry Andric ARMCC::AL, 0, TII); 3672754fe60SDimitry Andric BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2gpr), ARM::SP) 3682754fe60SDimitry Andric .addReg(ARM::R4); 3692754fe60SDimitry Andric } 3702754fe60SDimitry Andric } else { 3712754fe60SDimitry Andric // Thumb2 or ARM. 3722754fe60SDimitry Andric if (isARM) 3732754fe60SDimitry Andric BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP) 3742754fe60SDimitry Andric .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); 3752754fe60SDimitry Andric else 3762754fe60SDimitry Andric BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2gpr), ARM::SP) 3772754fe60SDimitry Andric .addReg(FramePtr); 3782754fe60SDimitry Andric } 3792754fe60SDimitry Andric } else if (NumBytes) 3802754fe60SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes); 3812754fe60SDimitry Andric 3822754fe60SDimitry Andric // Increment past our save areas. 383dd6029ffSDimitry Andric if (AFI->getDPRCalleeSavedAreaSize()) { 384dd6029ffSDimitry Andric MBBI++; 385dd6029ffSDimitry Andric // Since vpop register list cannot have gaps, there may be multiple vpop 386dd6029ffSDimitry Andric // instructions in the epilogue. 387dd6029ffSDimitry Andric while (MBBI->getOpcode() == ARM::VLDMDIA_UPD) 388dd6029ffSDimitry Andric MBBI++; 389dd6029ffSDimitry Andric } 3902754fe60SDimitry Andric if (AFI->getGPRCalleeSavedArea2Size()) MBBI++; 3912754fe60SDimitry Andric if (AFI->getGPRCalleeSavedArea1Size()) MBBI++; 3922754fe60SDimitry Andric } 3932754fe60SDimitry Andric 3942754fe60SDimitry Andric if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNdiND || 3952754fe60SDimitry Andric RetOpcode == ARM::TCRETURNri || RetOpcode == ARM::TCRETURNriND) { 3962754fe60SDimitry Andric // Tail call return: adjust the stack pointer and jump to callee. 3972754fe60SDimitry Andric MBBI = MBB.getLastNonDebugInstr(); 3982754fe60SDimitry Andric MachineOperand &JumpTarget = MBBI->getOperand(0); 3992754fe60SDimitry Andric 4002754fe60SDimitry Andric // Jump to label or value in register. 4012754fe60SDimitry Andric if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNdiND) { 4022754fe60SDimitry Andric unsigned TCOpcode = (RetOpcode == ARM::TCRETURNdi) 4033b0f4066SDimitry Andric ? (STI.isThumb() ? ARM::tTAILJMPd : ARM::TAILJMPd) 4043b0f4066SDimitry Andric : (STI.isThumb() ? ARM::tTAILJMPdND : ARM::TAILJMPdND); 4052754fe60SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(TCOpcode)); 4062754fe60SDimitry Andric if (JumpTarget.isGlobal()) 4072754fe60SDimitry Andric MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(), 4082754fe60SDimitry Andric JumpTarget.getTargetFlags()); 4092754fe60SDimitry Andric else { 4102754fe60SDimitry Andric assert(JumpTarget.isSymbol()); 4112754fe60SDimitry Andric MIB.addExternalSymbol(JumpTarget.getSymbolName(), 4122754fe60SDimitry Andric JumpTarget.getTargetFlags()); 4132754fe60SDimitry Andric } 4142754fe60SDimitry Andric } else if (RetOpcode == ARM::TCRETURNri) { 4153b0f4066SDimitry Andric BuildMI(MBB, MBBI, dl, 4163b0f4066SDimitry Andric TII.get(STI.isThumb() ? ARM::tTAILJMPr : ARM::TAILJMPr)). 4172754fe60SDimitry Andric addReg(JumpTarget.getReg(), RegState::Kill); 4182754fe60SDimitry Andric } else if (RetOpcode == ARM::TCRETURNriND) { 4193b0f4066SDimitry Andric BuildMI(MBB, MBBI, dl, 4203b0f4066SDimitry Andric TII.get(STI.isThumb() ? ARM::tTAILJMPrND : ARM::TAILJMPrND)). 4212754fe60SDimitry Andric addReg(JumpTarget.getReg(), RegState::Kill); 4222754fe60SDimitry Andric } 4232754fe60SDimitry Andric 4242754fe60SDimitry Andric MachineInstr *NewMI = prior(MBBI); 4252754fe60SDimitry Andric for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i) 4262754fe60SDimitry Andric NewMI->addOperand(MBBI->getOperand(i)); 4272754fe60SDimitry Andric 4282754fe60SDimitry Andric // Delete the pseudo instruction TCRETURN. 4292754fe60SDimitry Andric MBB.erase(MBBI); 4302754fe60SDimitry Andric } 4312754fe60SDimitry Andric 4322754fe60SDimitry Andric if (VARegSaveSize) 4332754fe60SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, VARegSaveSize); 4342754fe60SDimitry Andric } 4352754fe60SDimitry Andric 4362754fe60SDimitry Andric /// getFrameIndexReference - Provide a base+offset reference to an FI slot for 4372754fe60SDimitry Andric /// debug info. It's the same as what we use for resolving the code-gen 4382754fe60SDimitry Andric /// references for now. FIXME: This can go wrong when references are 4392754fe60SDimitry Andric /// SP-relative and simple call frames aren't used. 4402754fe60SDimitry Andric int 4412754fe60SDimitry Andric ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI, 4422754fe60SDimitry Andric unsigned &FrameReg) const { 4432754fe60SDimitry Andric return ResolveFrameIndexReference(MF, FI, FrameReg, 0); 4442754fe60SDimitry Andric } 4452754fe60SDimitry Andric 4462754fe60SDimitry Andric int 4472754fe60SDimitry Andric ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF, 4483b0f4066SDimitry Andric int FI, unsigned &FrameReg, 4492754fe60SDimitry Andric int SPAdj) const { 4502754fe60SDimitry Andric const MachineFrameInfo *MFI = MF.getFrameInfo(); 4512754fe60SDimitry Andric const ARMBaseRegisterInfo *RegInfo = 4522754fe60SDimitry Andric static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo()); 4532754fe60SDimitry Andric const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 4542754fe60SDimitry Andric int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize(); 4552754fe60SDimitry Andric int FPOffset = Offset - AFI->getFramePtrSpillOffset(); 4562754fe60SDimitry Andric bool isFixed = MFI->isFixedObjectIndex(FI); 4572754fe60SDimitry Andric 4582754fe60SDimitry Andric FrameReg = ARM::SP; 4592754fe60SDimitry Andric Offset += SPAdj; 4602754fe60SDimitry Andric if (AFI->isGPRCalleeSavedArea1Frame(FI)) 4612754fe60SDimitry Andric return Offset - AFI->getGPRCalleeSavedArea1Offset(); 4622754fe60SDimitry Andric else if (AFI->isGPRCalleeSavedArea2Frame(FI)) 4632754fe60SDimitry Andric return Offset - AFI->getGPRCalleeSavedArea2Offset(); 4642754fe60SDimitry Andric else if (AFI->isDPRCalleeSavedAreaFrame(FI)) 4652754fe60SDimitry Andric return Offset - AFI->getDPRCalleeSavedAreaOffset(); 4662754fe60SDimitry Andric 4672754fe60SDimitry Andric // When dynamically realigning the stack, use the frame pointer for 4682754fe60SDimitry Andric // parameters, and the stack/base pointer for locals. 4692754fe60SDimitry Andric if (RegInfo->needsStackRealignment(MF)) { 4702754fe60SDimitry Andric assert (hasFP(MF) && "dynamic stack realignment without a FP!"); 4712754fe60SDimitry Andric if (isFixed) { 4722754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 4732754fe60SDimitry Andric Offset = FPOffset; 4742754fe60SDimitry Andric } else if (MFI->hasVarSizedObjects()) { 4752754fe60SDimitry Andric assert(RegInfo->hasBasePointer(MF) && 4762754fe60SDimitry Andric "VLAs and dynamic stack alignment, but missing base pointer!"); 4772754fe60SDimitry Andric FrameReg = RegInfo->getBaseRegister(); 4782754fe60SDimitry Andric } 4792754fe60SDimitry Andric return Offset; 4802754fe60SDimitry Andric } 4812754fe60SDimitry Andric 4822754fe60SDimitry Andric // If there is a frame pointer, use it when we can. 4832754fe60SDimitry Andric if (hasFP(MF) && AFI->hasStackFrame()) { 4842754fe60SDimitry Andric // Use frame pointer to reference fixed objects. Use it for locals if 4852754fe60SDimitry Andric // there are VLAs (and thus the SP isn't reliable as a base). 4862754fe60SDimitry Andric if (isFixed || (MFI->hasVarSizedObjects() && 4872754fe60SDimitry Andric !RegInfo->hasBasePointer(MF))) { 4882754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 4892754fe60SDimitry Andric return FPOffset; 4902754fe60SDimitry Andric } else if (MFI->hasVarSizedObjects()) { 4912754fe60SDimitry Andric assert(RegInfo->hasBasePointer(MF) && "missing base pointer!"); 4923b0f4066SDimitry Andric if (AFI->isThumb2Function()) { 4932754fe60SDimitry Andric // Try to use the frame pointer if we can, else use the base pointer 4942754fe60SDimitry Andric // since it's available. This is handy for the emergency spill slot, in 4952754fe60SDimitry Andric // particular. 4962754fe60SDimitry Andric if (FPOffset >= -255 && FPOffset < 0) { 4972754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 4982754fe60SDimitry Andric return FPOffset; 4992754fe60SDimitry Andric } 5003b0f4066SDimitry Andric } 5012754fe60SDimitry Andric } else if (AFI->isThumb2Function()) { 5023b0f4066SDimitry Andric // Use add <rd>, sp, #<imm8> 5033b0f4066SDimitry Andric // ldr <rd>, [sp, #<imm8>] 5043b0f4066SDimitry Andric // if at all possible to save space. 5053b0f4066SDimitry Andric if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020) 5063b0f4066SDimitry Andric return Offset; 5072754fe60SDimitry Andric // In Thumb2 mode, the negative offset is very limited. Try to avoid 5083b0f4066SDimitry Andric // out of range references. ldr <rt>,[<rn>, #-<imm8>] 5092754fe60SDimitry Andric if (FPOffset >= -255 && FPOffset < 0) { 5102754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 5112754fe60SDimitry Andric return FPOffset; 5122754fe60SDimitry Andric } 5132754fe60SDimitry Andric } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) { 5142754fe60SDimitry Andric // Otherwise, use SP or FP, whichever is closer to the stack slot. 5152754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 5162754fe60SDimitry Andric return FPOffset; 5172754fe60SDimitry Andric } 5182754fe60SDimitry Andric } 5192754fe60SDimitry Andric // Use the base pointer if we have one. 5202754fe60SDimitry Andric if (RegInfo->hasBasePointer(MF)) 5212754fe60SDimitry Andric FrameReg = RegInfo->getBaseRegister(); 5222754fe60SDimitry Andric return Offset; 5232754fe60SDimitry Andric } 5242754fe60SDimitry Andric 5252754fe60SDimitry Andric int ARMFrameLowering::getFrameIndexOffset(const MachineFunction &MF, 5262754fe60SDimitry Andric int FI) const { 5272754fe60SDimitry Andric unsigned FrameReg; 5282754fe60SDimitry Andric return getFrameIndexReference(MF, FI, FrameReg); 5292754fe60SDimitry Andric } 5302754fe60SDimitry Andric 5312754fe60SDimitry Andric void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB, 5322754fe60SDimitry Andric MachineBasicBlock::iterator MI, 5332754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 5342754fe60SDimitry Andric unsigned StmOpc, unsigned StrOpc, 5352754fe60SDimitry Andric bool NoGap, 5363b0f4066SDimitry Andric bool(*Func)(unsigned, bool), 5373b0f4066SDimitry Andric unsigned MIFlags) const { 5382754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 5392754fe60SDimitry Andric const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); 5402754fe60SDimitry Andric 5412754fe60SDimitry Andric DebugLoc DL; 5422754fe60SDimitry Andric if (MI != MBB.end()) DL = MI->getDebugLoc(); 5432754fe60SDimitry Andric 5442754fe60SDimitry Andric SmallVector<std::pair<unsigned,bool>, 4> Regs; 5452754fe60SDimitry Andric unsigned i = CSI.size(); 5462754fe60SDimitry Andric while (i != 0) { 5472754fe60SDimitry Andric unsigned LastReg = 0; 5482754fe60SDimitry Andric for (; i != 0; --i) { 5492754fe60SDimitry Andric unsigned Reg = CSI[i-1].getReg(); 5502754fe60SDimitry Andric if (!(Func)(Reg, STI.isTargetDarwin())) continue; 5512754fe60SDimitry Andric 5522754fe60SDimitry Andric // Add the callee-saved register as live-in unless it's LR and 5532754fe60SDimitry Andric // @llvm.returnaddress is called. If LR is returned for 5542754fe60SDimitry Andric // @llvm.returnaddress then it's already added to the function and 5552754fe60SDimitry Andric // entry block live-in sets. 5562754fe60SDimitry Andric bool isKill = true; 5572754fe60SDimitry Andric if (Reg == ARM::LR) { 5582754fe60SDimitry Andric if (MF.getFrameInfo()->isReturnAddressTaken() && 5592754fe60SDimitry Andric MF.getRegInfo().isLiveIn(Reg)) 5602754fe60SDimitry Andric isKill = false; 5612754fe60SDimitry Andric } 5622754fe60SDimitry Andric 5632754fe60SDimitry Andric if (isKill) 5642754fe60SDimitry Andric MBB.addLiveIn(Reg); 5652754fe60SDimitry Andric 5662754fe60SDimitry Andric // If NoGap is true, push consecutive registers and then leave the rest 5672754fe60SDimitry Andric // for other instructions. e.g. 5682754fe60SDimitry Andric // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11} 5692754fe60SDimitry Andric if (NoGap && LastReg && LastReg != Reg-1) 5702754fe60SDimitry Andric break; 5712754fe60SDimitry Andric LastReg = Reg; 5722754fe60SDimitry Andric Regs.push_back(std::make_pair(Reg, isKill)); 5732754fe60SDimitry Andric } 5742754fe60SDimitry Andric 5752754fe60SDimitry Andric if (Regs.empty()) 5762754fe60SDimitry Andric continue; 5772754fe60SDimitry Andric if (Regs.size() > 1 || StrOpc== 0) { 5782754fe60SDimitry Andric MachineInstrBuilder MIB = 5792754fe60SDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP) 5803b0f4066SDimitry Andric .addReg(ARM::SP).setMIFlags(MIFlags)); 5812754fe60SDimitry Andric for (unsigned i = 0, e = Regs.size(); i < e; ++i) 5822754fe60SDimitry Andric MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second)); 5832754fe60SDimitry Andric } else if (Regs.size() == 1) { 5842754fe60SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc), 5852754fe60SDimitry Andric ARM::SP) 5862754fe60SDimitry Andric .addReg(Regs[0].first, getKillRegState(Regs[0].second)) 5873b0f4066SDimitry Andric .addReg(ARM::SP).setMIFlags(MIFlags); 5882754fe60SDimitry Andric // ARM mode needs an extra reg0 here due to addrmode2. Will go away once 5892754fe60SDimitry Andric // that refactoring is complete (eventually). 5902754fe60SDimitry Andric if (StrOpc == ARM::STR_PRE) { 5912754fe60SDimitry Andric MIB.addReg(0); 5922754fe60SDimitry Andric MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::sub, 4, ARM_AM::no_shift)); 5932754fe60SDimitry Andric } else 5942754fe60SDimitry Andric MIB.addImm(-4); 5952754fe60SDimitry Andric AddDefaultPred(MIB); 5962754fe60SDimitry Andric } 5972754fe60SDimitry Andric Regs.clear(); 5982754fe60SDimitry Andric } 5992754fe60SDimitry Andric } 6002754fe60SDimitry Andric 6012754fe60SDimitry Andric void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB, 6022754fe60SDimitry Andric MachineBasicBlock::iterator MI, 6032754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 6042754fe60SDimitry Andric unsigned LdmOpc, unsigned LdrOpc, 6052754fe60SDimitry Andric bool isVarArg, bool NoGap, 6062754fe60SDimitry Andric bool(*Func)(unsigned, bool)) const { 6072754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 6082754fe60SDimitry Andric const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); 6092754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 6102754fe60SDimitry Andric DebugLoc DL = MI->getDebugLoc(); 6112754fe60SDimitry Andric unsigned RetOpcode = MI->getOpcode(); 6122754fe60SDimitry Andric bool isTailCall = (RetOpcode == ARM::TCRETURNdi || 6132754fe60SDimitry Andric RetOpcode == ARM::TCRETURNdiND || 6142754fe60SDimitry Andric RetOpcode == ARM::TCRETURNri || 6152754fe60SDimitry Andric RetOpcode == ARM::TCRETURNriND); 6162754fe60SDimitry Andric 6172754fe60SDimitry Andric SmallVector<unsigned, 4> Regs; 6182754fe60SDimitry Andric unsigned i = CSI.size(); 6192754fe60SDimitry Andric while (i != 0) { 6202754fe60SDimitry Andric unsigned LastReg = 0; 6212754fe60SDimitry Andric bool DeleteRet = false; 6222754fe60SDimitry Andric for (; i != 0; --i) { 6232754fe60SDimitry Andric unsigned Reg = CSI[i-1].getReg(); 6242754fe60SDimitry Andric if (!(Func)(Reg, STI.isTargetDarwin())) continue; 6252754fe60SDimitry Andric 6262754fe60SDimitry Andric if (Reg == ARM::LR && !isTailCall && !isVarArg && STI.hasV5TOps()) { 6272754fe60SDimitry Andric Reg = ARM::PC; 6282754fe60SDimitry Andric LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET; 6292754fe60SDimitry Andric // Fold the return instruction into the LDM. 6302754fe60SDimitry Andric DeleteRet = true; 6312754fe60SDimitry Andric } 6322754fe60SDimitry Andric 6332754fe60SDimitry Andric // If NoGap is true, pop consecutive registers and then leave the rest 6342754fe60SDimitry Andric // for other instructions. e.g. 6352754fe60SDimitry Andric // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11} 6362754fe60SDimitry Andric if (NoGap && LastReg && LastReg != Reg-1) 6372754fe60SDimitry Andric break; 6382754fe60SDimitry Andric 6392754fe60SDimitry Andric LastReg = Reg; 6402754fe60SDimitry Andric Regs.push_back(Reg); 6412754fe60SDimitry Andric } 6422754fe60SDimitry Andric 6432754fe60SDimitry Andric if (Regs.empty()) 6442754fe60SDimitry Andric continue; 6452754fe60SDimitry Andric if (Regs.size() > 1 || LdrOpc == 0) { 6462754fe60SDimitry Andric MachineInstrBuilder MIB = 6472754fe60SDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP) 6482754fe60SDimitry Andric .addReg(ARM::SP)); 6492754fe60SDimitry Andric for (unsigned i = 0, e = Regs.size(); i < e; ++i) 6502754fe60SDimitry Andric MIB.addReg(Regs[i], getDefRegState(true)); 6512754fe60SDimitry Andric if (DeleteRet) 6522754fe60SDimitry Andric MI->eraseFromParent(); 6532754fe60SDimitry Andric MI = MIB; 6542754fe60SDimitry Andric } else if (Regs.size() == 1) { 6552754fe60SDimitry Andric // If we adjusted the reg to PC from LR above, switch it back here. We 6562754fe60SDimitry Andric // only do that for LDM. 6572754fe60SDimitry Andric if (Regs[0] == ARM::PC) 6582754fe60SDimitry Andric Regs[0] = ARM::LR; 6592754fe60SDimitry Andric MachineInstrBuilder MIB = 6602754fe60SDimitry Andric BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0]) 6612754fe60SDimitry Andric .addReg(ARM::SP, RegState::Define) 6622754fe60SDimitry Andric .addReg(ARM::SP); 6632754fe60SDimitry Andric // ARM mode needs an extra reg0 here due to addrmode2. Will go away once 6642754fe60SDimitry Andric // that refactoring is complete (eventually). 6652754fe60SDimitry Andric if (LdrOpc == ARM::LDR_POST) { 6662754fe60SDimitry Andric MIB.addReg(0); 6672754fe60SDimitry Andric MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift)); 6682754fe60SDimitry Andric } else 6692754fe60SDimitry Andric MIB.addImm(4); 6702754fe60SDimitry Andric AddDefaultPred(MIB); 6712754fe60SDimitry Andric } 6722754fe60SDimitry Andric Regs.clear(); 6732754fe60SDimitry Andric } 6742754fe60SDimitry Andric } 6752754fe60SDimitry Andric 6762754fe60SDimitry Andric bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB, 6772754fe60SDimitry Andric MachineBasicBlock::iterator MI, 6782754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 6792754fe60SDimitry Andric const TargetRegisterInfo *TRI) const { 6802754fe60SDimitry Andric if (CSI.empty()) 6812754fe60SDimitry Andric return false; 6822754fe60SDimitry Andric 6832754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 6842754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 6852754fe60SDimitry Andric 6862754fe60SDimitry Andric unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD; 6872754fe60SDimitry Andric unsigned PushOneOpc = AFI->isThumbFunction() ? ARM::t2STR_PRE : ARM::STR_PRE; 6882754fe60SDimitry Andric unsigned FltOpc = ARM::VSTMDDB_UPD; 6893b0f4066SDimitry Andric emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 6903b0f4066SDimitry Andric MachineInstr::FrameSetup); 6913b0f4066SDimitry Andric emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 6923b0f4066SDimitry Andric MachineInstr::FrameSetup); 6933b0f4066SDimitry Andric emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register, 6943b0f4066SDimitry Andric MachineInstr::FrameSetup); 6952754fe60SDimitry Andric 6962754fe60SDimitry Andric return true; 6972754fe60SDimitry Andric } 6982754fe60SDimitry Andric 6992754fe60SDimitry Andric bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, 7002754fe60SDimitry Andric MachineBasicBlock::iterator MI, 7012754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 7022754fe60SDimitry Andric const TargetRegisterInfo *TRI) const { 7032754fe60SDimitry Andric if (CSI.empty()) 7042754fe60SDimitry Andric return false; 7052754fe60SDimitry Andric 7062754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 7072754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 7082754fe60SDimitry Andric bool isVarArg = AFI->getVarArgsRegSaveSize() > 0; 7092754fe60SDimitry Andric 7102754fe60SDimitry Andric unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD; 7112754fe60SDimitry Andric unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST : ARM::LDR_POST; 7122754fe60SDimitry Andric unsigned FltOpc = ARM::VLDMDIA_UPD; 7132754fe60SDimitry Andric emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register); 7142754fe60SDimitry Andric emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false, 7152754fe60SDimitry Andric &isARMArea2Register); 7162754fe60SDimitry Andric emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false, 7172754fe60SDimitry Andric &isARMArea1Register); 7182754fe60SDimitry Andric 7192754fe60SDimitry Andric return true; 7202754fe60SDimitry Andric } 7212754fe60SDimitry Andric 7222754fe60SDimitry Andric // FIXME: Make generic? 7232754fe60SDimitry Andric static unsigned GetFunctionSizeInBytes(const MachineFunction &MF, 7242754fe60SDimitry Andric const ARMBaseInstrInfo &TII) { 7252754fe60SDimitry Andric unsigned FnSize = 0; 7262754fe60SDimitry Andric for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end(); 7272754fe60SDimitry Andric MBBI != E; ++MBBI) { 7282754fe60SDimitry Andric const MachineBasicBlock &MBB = *MBBI; 7292754fe60SDimitry Andric for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end(); 7302754fe60SDimitry Andric I != E; ++I) 7312754fe60SDimitry Andric FnSize += TII.GetInstSizeInBytes(I); 7322754fe60SDimitry Andric } 7332754fe60SDimitry Andric return FnSize; 7342754fe60SDimitry Andric } 7352754fe60SDimitry Andric 7362754fe60SDimitry Andric /// estimateStackSize - Estimate and return the size of the frame. 7372754fe60SDimitry Andric /// FIXME: Make generic? 7382754fe60SDimitry Andric static unsigned estimateStackSize(MachineFunction &MF) { 7392754fe60SDimitry Andric const MachineFrameInfo *FFI = MF.getFrameInfo(); 7402754fe60SDimitry Andric int Offset = 0; 7412754fe60SDimitry Andric for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) { 7422754fe60SDimitry Andric int FixedOff = -FFI->getObjectOffset(i); 7432754fe60SDimitry Andric if (FixedOff > Offset) Offset = FixedOff; 7442754fe60SDimitry Andric } 7452754fe60SDimitry Andric for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) { 7462754fe60SDimitry Andric if (FFI->isDeadObjectIndex(i)) 7472754fe60SDimitry Andric continue; 7482754fe60SDimitry Andric Offset += FFI->getObjectSize(i); 7492754fe60SDimitry Andric unsigned Align = FFI->getObjectAlignment(i); 7502754fe60SDimitry Andric // Adjust to alignment boundary 7512754fe60SDimitry Andric Offset = (Offset+Align-1)/Align*Align; 7522754fe60SDimitry Andric } 7532754fe60SDimitry Andric return (unsigned)Offset; 7542754fe60SDimitry Andric } 7552754fe60SDimitry Andric 7562754fe60SDimitry Andric /// estimateRSStackSizeLimit - Look at each instruction that references stack 7572754fe60SDimitry Andric /// frames and return the stack size limit beyond which some of these 7582754fe60SDimitry Andric /// instructions will require a scratch register during their expansion later. 7592754fe60SDimitry Andric // FIXME: Move to TII? 7602754fe60SDimitry Andric static unsigned estimateRSStackSizeLimit(MachineFunction &MF, 7612754fe60SDimitry Andric const TargetFrameLowering *TFI) { 7622754fe60SDimitry Andric const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 7632754fe60SDimitry Andric unsigned Limit = (1 << 12) - 1; 7642754fe60SDimitry Andric for (MachineFunction::iterator BB = MF.begin(),E = MF.end(); BB != E; ++BB) { 7652754fe60SDimitry Andric for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); 7662754fe60SDimitry Andric I != E; ++I) { 7672754fe60SDimitry Andric for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { 7682754fe60SDimitry Andric if (!I->getOperand(i).isFI()) continue; 7692754fe60SDimitry Andric 7702754fe60SDimitry Andric // When using ADDri to get the address of a stack object, 255 is the 7712754fe60SDimitry Andric // largest offset guaranteed to fit in the immediate offset. 7722754fe60SDimitry Andric if (I->getOpcode() == ARM::ADDri) { 7732754fe60SDimitry Andric Limit = std::min(Limit, (1U << 8) - 1); 7742754fe60SDimitry Andric break; 7752754fe60SDimitry Andric } 7762754fe60SDimitry Andric 7772754fe60SDimitry Andric // Otherwise check the addressing mode. 7782754fe60SDimitry Andric switch (I->getDesc().TSFlags & ARMII::AddrModeMask) { 7792754fe60SDimitry Andric case ARMII::AddrMode3: 7802754fe60SDimitry Andric case ARMII::AddrModeT2_i8: 7812754fe60SDimitry Andric Limit = std::min(Limit, (1U << 8) - 1); 7822754fe60SDimitry Andric break; 7832754fe60SDimitry Andric case ARMII::AddrMode5: 7842754fe60SDimitry Andric case ARMII::AddrModeT2_i8s4: 7852754fe60SDimitry Andric Limit = std::min(Limit, ((1U << 8) - 1) * 4); 7862754fe60SDimitry Andric break; 7872754fe60SDimitry Andric case ARMII::AddrModeT2_i12: 7882754fe60SDimitry Andric // i12 supports only positive offset so these will be converted to 7892754fe60SDimitry Andric // i8 opcodes. See llvm::rewriteT2FrameIndex. 7902754fe60SDimitry Andric if (TFI->hasFP(MF) && AFI->hasStackFrame()) 7912754fe60SDimitry Andric Limit = std::min(Limit, (1U << 8) - 1); 7922754fe60SDimitry Andric break; 7932754fe60SDimitry Andric case ARMII::AddrMode4: 7942754fe60SDimitry Andric case ARMII::AddrMode6: 7952754fe60SDimitry Andric // Addressing modes 4 & 6 (load/store) instructions can't encode an 7962754fe60SDimitry Andric // immediate offset for stack references. 7972754fe60SDimitry Andric return 0; 7982754fe60SDimitry Andric default: 7992754fe60SDimitry Andric break; 8002754fe60SDimitry Andric } 8012754fe60SDimitry Andric break; // At most one FI per instruction 8022754fe60SDimitry Andric } 8032754fe60SDimitry Andric } 8042754fe60SDimitry Andric } 8052754fe60SDimitry Andric 8062754fe60SDimitry Andric return Limit; 8072754fe60SDimitry Andric } 8082754fe60SDimitry Andric 8092754fe60SDimitry Andric void 8102754fe60SDimitry Andric ARMFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF, 8112754fe60SDimitry Andric RegScavenger *RS) const { 8122754fe60SDimitry Andric // This tells PEI to spill the FP as if it is any other callee-save register 8132754fe60SDimitry Andric // to take advantage the eliminateFrameIndex machinery. This also ensures it 8142754fe60SDimitry Andric // is spilled in the order specified by getCalleeSavedRegs() to make it easier 8152754fe60SDimitry Andric // to combine multiple loads / stores. 8162754fe60SDimitry Andric bool CanEliminateFrame = true; 8172754fe60SDimitry Andric bool CS1Spilled = false; 8182754fe60SDimitry Andric bool LRSpilled = false; 8192754fe60SDimitry Andric unsigned NumGPRSpills = 0; 8202754fe60SDimitry Andric SmallVector<unsigned, 4> UnspilledCS1GPRs; 8212754fe60SDimitry Andric SmallVector<unsigned, 4> UnspilledCS2GPRs; 8222754fe60SDimitry Andric const ARMBaseRegisterInfo *RegInfo = 8232754fe60SDimitry Andric static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo()); 8242754fe60SDimitry Andric const ARMBaseInstrInfo &TII = 8252754fe60SDimitry Andric *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo()); 8262754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 8272754fe60SDimitry Andric MachineFrameInfo *MFI = MF.getFrameInfo(); 8282754fe60SDimitry Andric unsigned FramePtr = RegInfo->getFrameRegister(MF); 8292754fe60SDimitry Andric 8302754fe60SDimitry Andric // Spill R4 if Thumb2 function requires stack realignment - it will be used as 8312754fe60SDimitry Andric // scratch register. Also spill R4 if Thumb2 function has varsized objects, 8322754fe60SDimitry Andric // since it's not always possible to restore sp from fp in a single 8332754fe60SDimitry Andric // instruction. 8342754fe60SDimitry Andric // FIXME: It will be better just to find spare register here. 8352754fe60SDimitry Andric if (AFI->isThumb2Function() && 8362754fe60SDimitry Andric (MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(MF))) 8372754fe60SDimitry Andric MF.getRegInfo().setPhysRegUsed(ARM::R4); 8382754fe60SDimitry Andric 8392754fe60SDimitry Andric if (AFI->isThumb1OnlyFunction()) { 8402754fe60SDimitry Andric // Spill LR if Thumb1 function uses variable length argument lists. 8412754fe60SDimitry Andric if (AFI->getVarArgsRegSaveSize() > 0) 8422754fe60SDimitry Andric MF.getRegInfo().setPhysRegUsed(ARM::LR); 8432754fe60SDimitry Andric 8442754fe60SDimitry Andric // Spill R4 if Thumb1 epilogue has to restore SP from FP since 8452754fe60SDimitry Andric // FIXME: It will be better just to find spare register here. 8462754fe60SDimitry Andric if (MFI->hasVarSizedObjects()) 8472754fe60SDimitry Andric MF.getRegInfo().setPhysRegUsed(ARM::R4); 8482754fe60SDimitry Andric } 8492754fe60SDimitry Andric 8502754fe60SDimitry Andric // Spill the BasePtr if it's used. 8512754fe60SDimitry Andric if (RegInfo->hasBasePointer(MF)) 8522754fe60SDimitry Andric MF.getRegInfo().setPhysRegUsed(RegInfo->getBaseRegister()); 8532754fe60SDimitry Andric 8542754fe60SDimitry Andric // Don't spill FP if the frame can be eliminated. This is determined 8552754fe60SDimitry Andric // by scanning the callee-save registers to see if any is used. 8562754fe60SDimitry Andric const unsigned *CSRegs = RegInfo->getCalleeSavedRegs(); 8572754fe60SDimitry Andric for (unsigned i = 0; CSRegs[i]; ++i) { 8582754fe60SDimitry Andric unsigned Reg = CSRegs[i]; 8592754fe60SDimitry Andric bool Spilled = false; 8602754fe60SDimitry Andric if (MF.getRegInfo().isPhysRegUsed(Reg)) { 8612754fe60SDimitry Andric Spilled = true; 8622754fe60SDimitry Andric CanEliminateFrame = false; 8632754fe60SDimitry Andric } else { 8642754fe60SDimitry Andric // Check alias registers too. 8652754fe60SDimitry Andric for (const unsigned *Aliases = 8662754fe60SDimitry Andric RegInfo->getAliasSet(Reg); *Aliases; ++Aliases) { 8672754fe60SDimitry Andric if (MF.getRegInfo().isPhysRegUsed(*Aliases)) { 8682754fe60SDimitry Andric Spilled = true; 8692754fe60SDimitry Andric CanEliminateFrame = false; 8702754fe60SDimitry Andric } 8712754fe60SDimitry Andric } 8722754fe60SDimitry Andric } 8732754fe60SDimitry Andric 8742754fe60SDimitry Andric if (!ARM::GPRRegisterClass->contains(Reg)) 8752754fe60SDimitry Andric continue; 8762754fe60SDimitry Andric 8772754fe60SDimitry Andric if (Spilled) { 8782754fe60SDimitry Andric NumGPRSpills++; 8792754fe60SDimitry Andric 8802754fe60SDimitry Andric if (!STI.isTargetDarwin()) { 8812754fe60SDimitry Andric if (Reg == ARM::LR) 8822754fe60SDimitry Andric LRSpilled = true; 8832754fe60SDimitry Andric CS1Spilled = true; 8842754fe60SDimitry Andric continue; 8852754fe60SDimitry Andric } 8862754fe60SDimitry Andric 8872754fe60SDimitry Andric // Keep track if LR and any of R4, R5, R6, and R7 is spilled. 8882754fe60SDimitry Andric switch (Reg) { 8892754fe60SDimitry Andric case ARM::LR: 8902754fe60SDimitry Andric LRSpilled = true; 8912754fe60SDimitry Andric // Fallthrough 8922754fe60SDimitry Andric case ARM::R4: case ARM::R5: 8932754fe60SDimitry Andric case ARM::R6: case ARM::R7: 8942754fe60SDimitry Andric CS1Spilled = true; 8952754fe60SDimitry Andric break; 8962754fe60SDimitry Andric default: 8972754fe60SDimitry Andric break; 8982754fe60SDimitry Andric } 8992754fe60SDimitry Andric } else { 9002754fe60SDimitry Andric if (!STI.isTargetDarwin()) { 9012754fe60SDimitry Andric UnspilledCS1GPRs.push_back(Reg); 9022754fe60SDimitry Andric continue; 9032754fe60SDimitry Andric } 9042754fe60SDimitry Andric 9052754fe60SDimitry Andric switch (Reg) { 9062754fe60SDimitry Andric case ARM::R4: case ARM::R5: 9072754fe60SDimitry Andric case ARM::R6: case ARM::R7: 9082754fe60SDimitry Andric case ARM::LR: 9092754fe60SDimitry Andric UnspilledCS1GPRs.push_back(Reg); 9102754fe60SDimitry Andric break; 9112754fe60SDimitry Andric default: 9122754fe60SDimitry Andric UnspilledCS2GPRs.push_back(Reg); 9132754fe60SDimitry Andric break; 9142754fe60SDimitry Andric } 9152754fe60SDimitry Andric } 9162754fe60SDimitry Andric } 9172754fe60SDimitry Andric 9182754fe60SDimitry Andric bool ForceLRSpill = false; 9192754fe60SDimitry Andric if (!LRSpilled && AFI->isThumb1OnlyFunction()) { 9202754fe60SDimitry Andric unsigned FnSize = GetFunctionSizeInBytes(MF, TII); 9212754fe60SDimitry Andric // Force LR to be spilled if the Thumb function size is > 2048. This enables 9222754fe60SDimitry Andric // use of BL to implement far jump. If it turns out that it's not needed 9232754fe60SDimitry Andric // then the branch fix up path will undo it. 9242754fe60SDimitry Andric if (FnSize >= (1 << 11)) { 9252754fe60SDimitry Andric CanEliminateFrame = false; 9262754fe60SDimitry Andric ForceLRSpill = true; 9272754fe60SDimitry Andric } 9282754fe60SDimitry Andric } 9292754fe60SDimitry Andric 9302754fe60SDimitry Andric // If any of the stack slot references may be out of range of an immediate 9312754fe60SDimitry Andric // offset, make sure a register (or a spill slot) is available for the 9322754fe60SDimitry Andric // register scavenger. Note that if we're indexing off the frame pointer, the 9332754fe60SDimitry Andric // effective stack size is 4 bytes larger since the FP points to the stack 9342754fe60SDimitry Andric // slot of the previous FP. Also, if we have variable sized objects in the 9352754fe60SDimitry Andric // function, stack slot references will often be negative, and some of 9362754fe60SDimitry Andric // our instructions are positive-offset only, so conservatively consider 9372754fe60SDimitry Andric // that case to want a spill slot (or register) as well. Similarly, if 9382754fe60SDimitry Andric // the function adjusts the stack pointer during execution and the 9392754fe60SDimitry Andric // adjustments aren't already part of our stack size estimate, our offset 9402754fe60SDimitry Andric // calculations may be off, so be conservative. 9412754fe60SDimitry Andric // FIXME: We could add logic to be more precise about negative offsets 9422754fe60SDimitry Andric // and which instructions will need a scratch register for them. Is it 9432754fe60SDimitry Andric // worth the effort and added fragility? 9442754fe60SDimitry Andric bool BigStack = 9452754fe60SDimitry Andric (RS && 9462754fe60SDimitry Andric (estimateStackSize(MF) + ((hasFP(MF) && AFI->hasStackFrame()) ? 4:0) >= 9472754fe60SDimitry Andric estimateRSStackSizeLimit(MF, this))) 9482754fe60SDimitry Andric || MFI->hasVarSizedObjects() 9492754fe60SDimitry Andric || (MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF)); 9502754fe60SDimitry Andric 9512754fe60SDimitry Andric bool ExtraCSSpill = false; 9522754fe60SDimitry Andric if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) { 9532754fe60SDimitry Andric AFI->setHasStackFrame(true); 9542754fe60SDimitry Andric 9552754fe60SDimitry Andric // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled. 9562754fe60SDimitry Andric // Spill LR as well so we can fold BX_RET to the registers restore (LDM). 9572754fe60SDimitry Andric if (!LRSpilled && CS1Spilled) { 9582754fe60SDimitry Andric MF.getRegInfo().setPhysRegUsed(ARM::LR); 9592754fe60SDimitry Andric NumGPRSpills++; 9602754fe60SDimitry Andric UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(), 9612754fe60SDimitry Andric UnspilledCS1GPRs.end(), (unsigned)ARM::LR)); 9622754fe60SDimitry Andric ForceLRSpill = false; 9632754fe60SDimitry Andric ExtraCSSpill = true; 9642754fe60SDimitry Andric } 9652754fe60SDimitry Andric 9662754fe60SDimitry Andric if (hasFP(MF)) { 9672754fe60SDimitry Andric MF.getRegInfo().setPhysRegUsed(FramePtr); 9682754fe60SDimitry Andric NumGPRSpills++; 9692754fe60SDimitry Andric } 9702754fe60SDimitry Andric 9712754fe60SDimitry Andric // If stack and double are 8-byte aligned and we are spilling an odd number 9722754fe60SDimitry Andric // of GPRs, spill one extra callee save GPR so we won't have to pad between 9732754fe60SDimitry Andric // the integer and double callee save areas. 9742754fe60SDimitry Andric unsigned TargetAlign = getStackAlignment(); 9752754fe60SDimitry Andric if (TargetAlign == 8 && (NumGPRSpills & 1)) { 9762754fe60SDimitry Andric if (CS1Spilled && !UnspilledCS1GPRs.empty()) { 9772754fe60SDimitry Andric for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) { 9782754fe60SDimitry Andric unsigned Reg = UnspilledCS1GPRs[i]; 9792754fe60SDimitry Andric // Don't spill high register if the function is thumb1 9802754fe60SDimitry Andric if (!AFI->isThumb1OnlyFunction() || 9812754fe60SDimitry Andric isARMLowRegister(Reg) || Reg == ARM::LR) { 9822754fe60SDimitry Andric MF.getRegInfo().setPhysRegUsed(Reg); 9832754fe60SDimitry Andric if (!RegInfo->isReservedReg(MF, Reg)) 9842754fe60SDimitry Andric ExtraCSSpill = true; 9852754fe60SDimitry Andric break; 9862754fe60SDimitry Andric } 9872754fe60SDimitry Andric } 9882754fe60SDimitry Andric } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) { 9892754fe60SDimitry Andric unsigned Reg = UnspilledCS2GPRs.front(); 9902754fe60SDimitry Andric MF.getRegInfo().setPhysRegUsed(Reg); 9912754fe60SDimitry Andric if (!RegInfo->isReservedReg(MF, Reg)) 9922754fe60SDimitry Andric ExtraCSSpill = true; 9932754fe60SDimitry Andric } 9942754fe60SDimitry Andric } 9952754fe60SDimitry Andric 9962754fe60SDimitry Andric // Estimate if we might need to scavenge a register at some point in order 9972754fe60SDimitry Andric // to materialize a stack offset. If so, either spill one additional 9982754fe60SDimitry Andric // callee-saved register or reserve a special spill slot to facilitate 9992754fe60SDimitry Andric // register scavenging. Thumb1 needs a spill slot for stack pointer 10002754fe60SDimitry Andric // adjustments also, even when the frame itself is small. 10012754fe60SDimitry Andric if (BigStack && !ExtraCSSpill) { 10022754fe60SDimitry Andric // If any non-reserved CS register isn't spilled, just spill one or two 10032754fe60SDimitry Andric // extra. That should take care of it! 10042754fe60SDimitry Andric unsigned NumExtras = TargetAlign / 4; 10052754fe60SDimitry Andric SmallVector<unsigned, 2> Extras; 10062754fe60SDimitry Andric while (NumExtras && !UnspilledCS1GPRs.empty()) { 10072754fe60SDimitry Andric unsigned Reg = UnspilledCS1GPRs.back(); 10082754fe60SDimitry Andric UnspilledCS1GPRs.pop_back(); 10092754fe60SDimitry Andric if (!RegInfo->isReservedReg(MF, Reg) && 10102754fe60SDimitry Andric (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) || 10112754fe60SDimitry Andric Reg == ARM::LR)) { 10122754fe60SDimitry Andric Extras.push_back(Reg); 10132754fe60SDimitry Andric NumExtras--; 10142754fe60SDimitry Andric } 10152754fe60SDimitry Andric } 10162754fe60SDimitry Andric // For non-Thumb1 functions, also check for hi-reg CS registers 10172754fe60SDimitry Andric if (!AFI->isThumb1OnlyFunction()) { 10182754fe60SDimitry Andric while (NumExtras && !UnspilledCS2GPRs.empty()) { 10192754fe60SDimitry Andric unsigned Reg = UnspilledCS2GPRs.back(); 10202754fe60SDimitry Andric UnspilledCS2GPRs.pop_back(); 10212754fe60SDimitry Andric if (!RegInfo->isReservedReg(MF, Reg)) { 10222754fe60SDimitry Andric Extras.push_back(Reg); 10232754fe60SDimitry Andric NumExtras--; 10242754fe60SDimitry Andric } 10252754fe60SDimitry Andric } 10262754fe60SDimitry Andric } 10272754fe60SDimitry Andric if (Extras.size() && NumExtras == 0) { 10282754fe60SDimitry Andric for (unsigned i = 0, e = Extras.size(); i != e; ++i) { 10292754fe60SDimitry Andric MF.getRegInfo().setPhysRegUsed(Extras[i]); 10302754fe60SDimitry Andric } 10312754fe60SDimitry Andric } else if (!AFI->isThumb1OnlyFunction()) { 10322754fe60SDimitry Andric // note: Thumb1 functions spill to R12, not the stack. Reserve a slot 10332754fe60SDimitry Andric // closest to SP or frame pointer. 10342754fe60SDimitry Andric const TargetRegisterClass *RC = ARM::GPRRegisterClass; 10352754fe60SDimitry Andric RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(), 10362754fe60SDimitry Andric RC->getAlignment(), 10372754fe60SDimitry Andric false)); 10382754fe60SDimitry Andric } 10392754fe60SDimitry Andric } 10402754fe60SDimitry Andric } 10412754fe60SDimitry Andric 10422754fe60SDimitry Andric if (ForceLRSpill) { 10432754fe60SDimitry Andric MF.getRegInfo().setPhysRegUsed(ARM::LR); 10442754fe60SDimitry Andric AFI->setLRIsSpilledForFarJump(true); 10452754fe60SDimitry Andric } 10462754fe60SDimitry Andric } 1047