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 "ARMBaseInstrInfo.h" 162754fe60SDimitry Andric #include "ARMBaseRegisterInfo.h" 172754fe60SDimitry Andric #include "ARMMachineFunctionInfo.h" 18f8254f43SDimitry Andric #include "llvm/CallingConv.h" 19f8254f43SDimitry Andric #include "llvm/Function.h" 206122f3e6SDimitry Andric #include "MCTargetDesc/ARMAddressingModes.h" 212754fe60SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 222754fe60SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 232754fe60SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 242754fe60SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 252754fe60SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h" 262754fe60SDimitry Andric #include "llvm/Target/TargetOptions.h" 272754fe60SDimitry Andric 282754fe60SDimitry Andric using namespace llvm; 292754fe60SDimitry Andric 302754fe60SDimitry Andric /// hasFP - Return true if the specified function should have a dedicated frame 312754fe60SDimitry Andric /// pointer register. This is true if the function has variable sized allocas 322754fe60SDimitry Andric /// or if frame pointer elimination is disabled. 332754fe60SDimitry Andric bool ARMFrameLowering::hasFP(const MachineFunction &MF) const { 342754fe60SDimitry Andric const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo(); 352754fe60SDimitry Andric 362754fe60SDimitry Andric // Mac OS X requires FP not to be clobbered for backtracing purpose. 372754fe60SDimitry Andric if (STI.isTargetDarwin()) 382754fe60SDimitry Andric return true; 392754fe60SDimitry Andric 402754fe60SDimitry Andric const MachineFrameInfo *MFI = MF.getFrameInfo(); 412754fe60SDimitry Andric // Always eliminate non-leaf frame pointers. 422754fe60SDimitry Andric return ((DisableFramePointerElim(MF) && MFI->hasCalls()) || 432754fe60SDimitry Andric RegInfo->needsStackRealignment(MF) || 442754fe60SDimitry Andric MFI->hasVarSizedObjects() || 452754fe60SDimitry Andric MFI->isFrameAddressTaken()); 462754fe60SDimitry Andric } 472754fe60SDimitry Andric 482754fe60SDimitry Andric /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is 492754fe60SDimitry Andric /// not required, we reserve argument space for call sites in the function 502754fe60SDimitry Andric /// immediately on entry to the current function. This eliminates the need for 512754fe60SDimitry Andric /// add/sub sp brackets around call sites. Returns true if the call frame is 522754fe60SDimitry Andric /// included as part of the stack frame. 532754fe60SDimitry Andric bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { 542754fe60SDimitry Andric const MachineFrameInfo *FFI = MF.getFrameInfo(); 552754fe60SDimitry Andric unsigned CFSize = FFI->getMaxCallFrameSize(); 562754fe60SDimitry Andric // It's not always a good idea to include the call frame as part of the 572754fe60SDimitry Andric // stack frame. ARM (especially Thumb) has small immediate offset to 582754fe60SDimitry Andric // address the stack frame. So a large call frame can cause poor codegen 592754fe60SDimitry Andric // and may even makes it impossible to scavenge a register. 602754fe60SDimitry Andric if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12 612754fe60SDimitry Andric return false; 622754fe60SDimitry Andric 632754fe60SDimitry Andric return !MF.getFrameInfo()->hasVarSizedObjects(); 642754fe60SDimitry Andric } 652754fe60SDimitry Andric 662754fe60SDimitry Andric /// canSimplifyCallFramePseudos - If there is a reserved call frame, the 672754fe60SDimitry Andric /// call frame pseudos can be simplified. Unlike most targets, having a FP 682754fe60SDimitry Andric /// is not sufficient here since we still may reference some objects via SP 692754fe60SDimitry Andric /// even when FP is available in Thumb2 mode. 702754fe60SDimitry Andric bool 712754fe60SDimitry Andric ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const { 722754fe60SDimitry Andric return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects(); 732754fe60SDimitry Andric } 742754fe60SDimitry Andric 752754fe60SDimitry Andric static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) { 762754fe60SDimitry Andric for (unsigned i = 0; CSRegs[i]; ++i) 772754fe60SDimitry Andric if (Reg == CSRegs[i]) 782754fe60SDimitry Andric return true; 792754fe60SDimitry Andric return false; 802754fe60SDimitry Andric } 812754fe60SDimitry Andric 822754fe60SDimitry Andric static bool isCSRestore(MachineInstr *MI, 832754fe60SDimitry Andric const ARMBaseInstrInfo &TII, 842754fe60SDimitry Andric const unsigned *CSRegs) { 852754fe60SDimitry Andric // Integer spill area is handled with "pop". 862754fe60SDimitry Andric if (MI->getOpcode() == ARM::LDMIA_RET || 872754fe60SDimitry Andric MI->getOpcode() == ARM::t2LDMIA_RET || 882754fe60SDimitry Andric MI->getOpcode() == ARM::LDMIA_UPD || 892754fe60SDimitry Andric MI->getOpcode() == ARM::t2LDMIA_UPD || 902754fe60SDimitry Andric MI->getOpcode() == ARM::VLDMDIA_UPD) { 912754fe60SDimitry Andric // The first two operands are predicates. The last two are 922754fe60SDimitry Andric // imp-def and imp-use of SP. Check everything in between. 932754fe60SDimitry Andric for (int i = 5, e = MI->getNumOperands(); i != e; ++i) 942754fe60SDimitry Andric if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs)) 952754fe60SDimitry Andric return false; 962754fe60SDimitry Andric return true; 972754fe60SDimitry Andric } 986122f3e6SDimitry Andric if ((MI->getOpcode() == ARM::LDR_POST_IMM || 996122f3e6SDimitry Andric MI->getOpcode() == ARM::LDR_POST_REG || 1002754fe60SDimitry Andric MI->getOpcode() == ARM::t2LDR_POST) && 1012754fe60SDimitry Andric isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs) && 1022754fe60SDimitry Andric MI->getOperand(1).getReg() == ARM::SP) 1032754fe60SDimitry Andric return true; 1042754fe60SDimitry Andric 1052754fe60SDimitry Andric return false; 1062754fe60SDimitry Andric } 1072754fe60SDimitry Andric 1082754fe60SDimitry Andric static void 1092754fe60SDimitry Andric emitSPUpdate(bool isARM, 1102754fe60SDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, 1112754fe60SDimitry Andric DebugLoc dl, const ARMBaseInstrInfo &TII, 1123b0f4066SDimitry Andric int NumBytes, unsigned MIFlags = MachineInstr::NoFlags) { 1132754fe60SDimitry Andric if (isARM) 1142754fe60SDimitry Andric emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, 1153b0f4066SDimitry Andric ARMCC::AL, 0, TII, MIFlags); 1162754fe60SDimitry Andric else 1172754fe60SDimitry Andric emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, 1183b0f4066SDimitry Andric ARMCC::AL, 0, TII, MIFlags); 1192754fe60SDimitry Andric } 1202754fe60SDimitry Andric 1212754fe60SDimitry Andric void ARMFrameLowering::emitPrologue(MachineFunction &MF) const { 1222754fe60SDimitry Andric MachineBasicBlock &MBB = MF.front(); 1232754fe60SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.begin(); 1242754fe60SDimitry Andric MachineFrameInfo *MFI = MF.getFrameInfo(); 1252754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1262754fe60SDimitry Andric const ARMBaseRegisterInfo *RegInfo = 1272754fe60SDimitry Andric static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo()); 1282754fe60SDimitry Andric const ARMBaseInstrInfo &TII = 1292754fe60SDimitry Andric *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo()); 1302754fe60SDimitry Andric assert(!AFI->isThumb1OnlyFunction() && 1312754fe60SDimitry Andric "This emitPrologue does not support Thumb1!"); 1322754fe60SDimitry Andric bool isARM = !AFI->isThumbFunction(); 1332754fe60SDimitry Andric unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize(); 1342754fe60SDimitry Andric unsigned NumBytes = MFI->getStackSize(); 1352754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); 1362754fe60SDimitry Andric DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 1372754fe60SDimitry Andric unsigned FramePtr = RegInfo->getFrameRegister(MF); 1382754fe60SDimitry Andric 1392754fe60SDimitry Andric // Determine the sizes of each callee-save spill areas and record which frame 1402754fe60SDimitry Andric // belongs to which callee-save spill areas. 1412754fe60SDimitry Andric unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0; 1422754fe60SDimitry Andric int FramePtrSpillFI = 0; 1432754fe60SDimitry Andric 144f8254f43SDimitry Andric // All calls are tail calls in GHC calling conv, and functions have no prologue/epilogue. 145f8254f43SDimitry Andric if (MF.getFunction()->getCallingConv() == CallingConv::GHC) 146f8254f43SDimitry Andric return; 147f8254f43SDimitry Andric 1482754fe60SDimitry Andric // Allocate the vararg register save area. This is not counted in NumBytes. 1492754fe60SDimitry Andric if (VARegSaveSize) 1503b0f4066SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, -VARegSaveSize, 1513b0f4066SDimitry Andric MachineInstr::FrameSetup); 1522754fe60SDimitry Andric 1532754fe60SDimitry Andric if (!AFI->hasStackFrame()) { 1542754fe60SDimitry Andric if (NumBytes != 0) 1553b0f4066SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes, 1563b0f4066SDimitry Andric MachineInstr::FrameSetup); 1572754fe60SDimitry Andric return; 1582754fe60SDimitry Andric } 1592754fe60SDimitry Andric 1602754fe60SDimitry Andric for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 1612754fe60SDimitry Andric unsigned Reg = CSI[i].getReg(); 1622754fe60SDimitry Andric int FI = CSI[i].getFrameIdx(); 1632754fe60SDimitry Andric switch (Reg) { 1642754fe60SDimitry Andric case ARM::R4: 1652754fe60SDimitry Andric case ARM::R5: 1662754fe60SDimitry Andric case ARM::R6: 1672754fe60SDimitry Andric case ARM::R7: 1682754fe60SDimitry Andric case ARM::LR: 1692754fe60SDimitry Andric if (Reg == FramePtr) 1702754fe60SDimitry Andric FramePtrSpillFI = FI; 1712754fe60SDimitry Andric AFI->addGPRCalleeSavedArea1Frame(FI); 1722754fe60SDimitry Andric GPRCS1Size += 4; 1732754fe60SDimitry Andric break; 1742754fe60SDimitry Andric case ARM::R8: 1752754fe60SDimitry Andric case ARM::R9: 1762754fe60SDimitry Andric case ARM::R10: 1772754fe60SDimitry Andric case ARM::R11: 1782754fe60SDimitry Andric if (Reg == FramePtr) 1792754fe60SDimitry Andric FramePtrSpillFI = FI; 1802754fe60SDimitry Andric if (STI.isTargetDarwin()) { 1812754fe60SDimitry Andric AFI->addGPRCalleeSavedArea2Frame(FI); 1822754fe60SDimitry Andric GPRCS2Size += 4; 1832754fe60SDimitry Andric } else { 1842754fe60SDimitry Andric AFI->addGPRCalleeSavedArea1Frame(FI); 1852754fe60SDimitry Andric GPRCS1Size += 4; 1862754fe60SDimitry Andric } 1872754fe60SDimitry Andric break; 1882754fe60SDimitry Andric default: 1892754fe60SDimitry Andric AFI->addDPRCalleeSavedAreaFrame(FI); 1902754fe60SDimitry Andric DPRCSSize += 8; 1912754fe60SDimitry Andric } 1922754fe60SDimitry Andric } 1932754fe60SDimitry Andric 1942754fe60SDimitry Andric // Move past area 1. 1952754fe60SDimitry Andric if (GPRCS1Size > 0) MBBI++; 1962754fe60SDimitry Andric 1972754fe60SDimitry Andric // Set FP to point to the stack slot that contains the previous FP. 1982754fe60SDimitry Andric // For Darwin, FP is R7, which has now been stored in spill area 1. 1992754fe60SDimitry Andric // Otherwise, if this is not Darwin, all the callee-saved registers go 2002754fe60SDimitry Andric // into spill area 1, including the FP in R11. In either case, it is 2012754fe60SDimitry Andric // now safe to emit this assignment. 2022754fe60SDimitry Andric bool HasFP = hasFP(MF); 2032754fe60SDimitry Andric if (HasFP) { 2042754fe60SDimitry Andric unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : ARM::t2ADDri; 2052754fe60SDimitry Andric MachineInstrBuilder MIB = 2062754fe60SDimitry Andric BuildMI(MBB, MBBI, dl, TII.get(ADDriOpc), FramePtr) 2073b0f4066SDimitry Andric .addFrameIndex(FramePtrSpillFI).addImm(0) 2083b0f4066SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 2092754fe60SDimitry Andric AddDefaultCC(AddDefaultPred(MIB)); 2102754fe60SDimitry Andric } 2112754fe60SDimitry Andric 2122754fe60SDimitry Andric // Move past area 2. 2132754fe60SDimitry Andric if (GPRCS2Size > 0) MBBI++; 2142754fe60SDimitry Andric 2152754fe60SDimitry Andric // Determine starting offsets of spill areas. 2162754fe60SDimitry Andric unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize); 2172754fe60SDimitry Andric unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize; 2182754fe60SDimitry Andric unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size; 2192754fe60SDimitry Andric if (HasFP) 2202754fe60SDimitry Andric AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + 2212754fe60SDimitry Andric NumBytes); 2222754fe60SDimitry Andric AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset); 2232754fe60SDimitry Andric AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset); 2242754fe60SDimitry Andric AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset); 2252754fe60SDimitry Andric 2262754fe60SDimitry Andric // Move past area 3. 227dd6029ffSDimitry Andric if (DPRCSSize > 0) { 228dd6029ffSDimitry Andric MBBI++; 229dd6029ffSDimitry Andric // Since vpush register list cannot have gaps, there may be multiple vpush 230dd6029ffSDimitry Andric // instructions in the prologue. 231dd6029ffSDimitry Andric while (MBBI->getOpcode() == ARM::VSTMDDB_UPD) 232dd6029ffSDimitry Andric MBBI++; 233dd6029ffSDimitry Andric } 2342754fe60SDimitry Andric 2352754fe60SDimitry Andric NumBytes = DPRCSOffset; 2362754fe60SDimitry Andric if (NumBytes) { 2372754fe60SDimitry Andric // Adjust SP after all the callee-save spills. 2383b0f4066SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes, 2393b0f4066SDimitry Andric MachineInstr::FrameSetup); 2402754fe60SDimitry Andric if (HasFP && isARM) 2412754fe60SDimitry Andric // Restore from fp only in ARM mode: e.g. sub sp, r7, #24 2422754fe60SDimitry Andric // Note it's not safe to do this in Thumb2 mode because it would have 2432754fe60SDimitry Andric // taken two instructions: 2442754fe60SDimitry Andric // mov sp, r7 2452754fe60SDimitry Andric // sub sp, #24 2462754fe60SDimitry Andric // If an interrupt is taken between the two instructions, then sp is in 2472754fe60SDimitry Andric // an inconsistent state (pointing to the middle of callee-saved area). 2482754fe60SDimitry Andric // The interrupt handler can end up clobbering the registers. 2492754fe60SDimitry Andric AFI->setShouldRestoreSPFromFP(true); 2502754fe60SDimitry Andric } 2512754fe60SDimitry Andric 2522754fe60SDimitry Andric if (STI.isTargetELF() && hasFP(MF)) 2532754fe60SDimitry Andric MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() - 2542754fe60SDimitry Andric AFI->getFramePtrSpillOffset()); 2552754fe60SDimitry Andric 2562754fe60SDimitry Andric AFI->setGPRCalleeSavedArea1Size(GPRCS1Size); 2572754fe60SDimitry Andric AFI->setGPRCalleeSavedArea2Size(GPRCS2Size); 2582754fe60SDimitry Andric AFI->setDPRCalleeSavedAreaSize(DPRCSSize); 2592754fe60SDimitry Andric 2602754fe60SDimitry Andric // If we need dynamic stack realignment, do it here. Be paranoid and make 2612754fe60SDimitry Andric // sure if we also have VLAs, we have a base pointer for frame access. 2622754fe60SDimitry Andric if (RegInfo->needsStackRealignment(MF)) { 2632754fe60SDimitry Andric unsigned MaxAlign = MFI->getMaxAlignment(); 2642754fe60SDimitry Andric assert (!AFI->isThumb1OnlyFunction()); 2652754fe60SDimitry Andric if (!AFI->isThumbFunction()) { 2662754fe60SDimitry Andric // Emit bic sp, sp, MaxAlign 2672754fe60SDimitry Andric AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, 2682754fe60SDimitry Andric TII.get(ARM::BICri), ARM::SP) 2692754fe60SDimitry Andric .addReg(ARM::SP, RegState::Kill) 2702754fe60SDimitry Andric .addImm(MaxAlign-1))); 2712754fe60SDimitry Andric } else { 2722754fe60SDimitry Andric // We cannot use sp as source/dest register here, thus we're emitting the 2732754fe60SDimitry Andric // following sequence: 2742754fe60SDimitry Andric // mov r4, sp 2752754fe60SDimitry Andric // bic r4, r4, MaxAlign 2762754fe60SDimitry Andric // mov sp, r4 2772754fe60SDimitry Andric // FIXME: It will be better just to find spare register here. 27817a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4) 27917a519f9SDimitry Andric .addReg(ARM::SP, RegState::Kill)); 2802754fe60SDimitry Andric AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, 2812754fe60SDimitry Andric TII.get(ARM::t2BICri), ARM::R4) 2822754fe60SDimitry Andric .addReg(ARM::R4, RegState::Kill) 2832754fe60SDimitry Andric .addImm(MaxAlign-1))); 28417a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP) 28517a519f9SDimitry Andric .addReg(ARM::R4, RegState::Kill)); 2862754fe60SDimitry Andric } 2872754fe60SDimitry Andric 2882754fe60SDimitry Andric AFI->setShouldRestoreSPFromFP(true); 2892754fe60SDimitry Andric } 2902754fe60SDimitry Andric 2912754fe60SDimitry Andric // If we need a base pointer, set it up here. It's whatever the value 2922754fe60SDimitry Andric // of the stack pointer is at this point. Any variable size objects 2932754fe60SDimitry Andric // will be allocated after this, so we can still use the base pointer 2942754fe60SDimitry Andric // to reference locals. 2953b0f4066SDimitry Andric // FIXME: Clarify FrameSetup flags here. 2962754fe60SDimitry Andric if (RegInfo->hasBasePointer(MF)) { 2972754fe60SDimitry Andric if (isARM) 2982754fe60SDimitry Andric BuildMI(MBB, MBBI, dl, 2992754fe60SDimitry Andric TII.get(ARM::MOVr), RegInfo->getBaseRegister()) 3002754fe60SDimitry Andric .addReg(ARM::SP) 3012754fe60SDimitry Andric .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); 3022754fe60SDimitry Andric else 30317a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), 30417a519f9SDimitry Andric RegInfo->getBaseRegister()) 30517a519f9SDimitry Andric .addReg(ARM::SP)); 3062754fe60SDimitry Andric } 3072754fe60SDimitry Andric 3082754fe60SDimitry Andric // If the frame has variable sized objects then the epilogue must restore 3092754fe60SDimitry Andric // the sp from fp. We can assume there's an FP here since hasFP already 3102754fe60SDimitry Andric // checks for hasVarSizedObjects. 3112754fe60SDimitry Andric if (MFI->hasVarSizedObjects()) 3122754fe60SDimitry Andric AFI->setShouldRestoreSPFromFP(true); 3132754fe60SDimitry Andric } 3142754fe60SDimitry Andric 3152754fe60SDimitry Andric void ARMFrameLowering::emitEpilogue(MachineFunction &MF, 3162754fe60SDimitry Andric MachineBasicBlock &MBB) const { 3172754fe60SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 3182754fe60SDimitry Andric assert(MBBI->getDesc().isReturn() && 3192754fe60SDimitry Andric "Can only insert epilog into returning blocks"); 3202754fe60SDimitry Andric unsigned RetOpcode = MBBI->getOpcode(); 3212754fe60SDimitry Andric DebugLoc dl = MBBI->getDebugLoc(); 3222754fe60SDimitry Andric MachineFrameInfo *MFI = MF.getFrameInfo(); 3232754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3242754fe60SDimitry Andric const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo(); 3252754fe60SDimitry Andric const ARMBaseInstrInfo &TII = 3262754fe60SDimitry Andric *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo()); 3272754fe60SDimitry Andric assert(!AFI->isThumb1OnlyFunction() && 3282754fe60SDimitry Andric "This emitEpilogue does not support Thumb1!"); 3292754fe60SDimitry Andric bool isARM = !AFI->isThumbFunction(); 3302754fe60SDimitry Andric 3312754fe60SDimitry Andric unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize(); 3322754fe60SDimitry Andric int NumBytes = (int)MFI->getStackSize(); 3332754fe60SDimitry Andric unsigned FramePtr = RegInfo->getFrameRegister(MF); 3342754fe60SDimitry Andric 335f8254f43SDimitry Andric // All calls are tail calls in GHC calling conv, and functions have no prologue/epilogue. 336f8254f43SDimitry Andric if (MF.getFunction()->getCallingConv() == CallingConv::GHC) 337f8254f43SDimitry Andric return; 338f8254f43SDimitry Andric 3392754fe60SDimitry Andric if (!AFI->hasStackFrame()) { 3402754fe60SDimitry Andric if (NumBytes != 0) 3412754fe60SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes); 3422754fe60SDimitry Andric } else { 3432754fe60SDimitry Andric // Unwind MBBI to point to first LDR / VLDRD. 3442754fe60SDimitry Andric const unsigned *CSRegs = RegInfo->getCalleeSavedRegs(); 3452754fe60SDimitry Andric if (MBBI != MBB.begin()) { 3462754fe60SDimitry Andric do 3472754fe60SDimitry Andric --MBBI; 3482754fe60SDimitry Andric while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs)); 3492754fe60SDimitry Andric if (!isCSRestore(MBBI, TII, CSRegs)) 3502754fe60SDimitry Andric ++MBBI; 3512754fe60SDimitry Andric } 3522754fe60SDimitry Andric 3532754fe60SDimitry Andric // Move SP to start of FP callee save spill area. 3542754fe60SDimitry Andric NumBytes -= (AFI->getGPRCalleeSavedArea1Size() + 3552754fe60SDimitry Andric AFI->getGPRCalleeSavedArea2Size() + 3562754fe60SDimitry Andric AFI->getDPRCalleeSavedAreaSize()); 3572754fe60SDimitry Andric 3582754fe60SDimitry Andric // Reset SP based on frame pointer only if the stack frame extends beyond 3592754fe60SDimitry Andric // frame pointer stack slot or target is ELF and the function has FP. 3602754fe60SDimitry Andric if (AFI->shouldRestoreSPFromFP()) { 3612754fe60SDimitry Andric NumBytes = AFI->getFramePtrSpillOffset() - NumBytes; 3622754fe60SDimitry Andric if (NumBytes) { 3632754fe60SDimitry Andric if (isARM) 3642754fe60SDimitry Andric emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes, 3652754fe60SDimitry Andric ARMCC::AL, 0, TII); 3662754fe60SDimitry Andric else { 3672754fe60SDimitry Andric // It's not possible to restore SP from FP in a single instruction. 3682754fe60SDimitry Andric // For Darwin, this looks like: 3692754fe60SDimitry Andric // mov sp, r7 3702754fe60SDimitry Andric // sub sp, #24 3712754fe60SDimitry Andric // This is bad, if an interrupt is taken after the mov, sp is in an 3722754fe60SDimitry Andric // inconsistent state. 3732754fe60SDimitry Andric // Use the first callee-saved register as a scratch register. 3742754fe60SDimitry Andric assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) && 3752754fe60SDimitry Andric "No scratch register to restore SP from FP!"); 3762754fe60SDimitry Andric emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes, 3772754fe60SDimitry Andric ARMCC::AL, 0, TII); 37817a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), 37917a519f9SDimitry Andric ARM::SP) 38017a519f9SDimitry Andric .addReg(ARM::R4)); 3812754fe60SDimitry Andric } 3822754fe60SDimitry Andric } else { 3832754fe60SDimitry Andric // Thumb2 or ARM. 3842754fe60SDimitry Andric if (isARM) 3852754fe60SDimitry Andric BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP) 3862754fe60SDimitry Andric .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); 3872754fe60SDimitry Andric else 38817a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), 38917a519f9SDimitry Andric ARM::SP) 39017a519f9SDimitry Andric .addReg(FramePtr)); 3912754fe60SDimitry Andric } 3922754fe60SDimitry Andric } else if (NumBytes) 3932754fe60SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes); 3942754fe60SDimitry Andric 3952754fe60SDimitry Andric // Increment past our save areas. 396dd6029ffSDimitry Andric if (AFI->getDPRCalleeSavedAreaSize()) { 397dd6029ffSDimitry Andric MBBI++; 398dd6029ffSDimitry Andric // Since vpop register list cannot have gaps, there may be multiple vpop 399dd6029ffSDimitry Andric // instructions in the epilogue. 400dd6029ffSDimitry Andric while (MBBI->getOpcode() == ARM::VLDMDIA_UPD) 401dd6029ffSDimitry Andric MBBI++; 402dd6029ffSDimitry Andric } 4032754fe60SDimitry Andric if (AFI->getGPRCalleeSavedArea2Size()) MBBI++; 4042754fe60SDimitry Andric if (AFI->getGPRCalleeSavedArea1Size()) MBBI++; 4052754fe60SDimitry Andric } 4062754fe60SDimitry Andric 4072754fe60SDimitry Andric if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNdiND || 4082754fe60SDimitry Andric RetOpcode == ARM::TCRETURNri || RetOpcode == ARM::TCRETURNriND) { 4092754fe60SDimitry Andric // Tail call return: adjust the stack pointer and jump to callee. 4102754fe60SDimitry Andric MBBI = MBB.getLastNonDebugInstr(); 4112754fe60SDimitry Andric MachineOperand &JumpTarget = MBBI->getOperand(0); 4122754fe60SDimitry Andric 4132754fe60SDimitry Andric // Jump to label or value in register. 4142754fe60SDimitry Andric if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNdiND) { 4152754fe60SDimitry Andric unsigned TCOpcode = (RetOpcode == ARM::TCRETURNdi) 4163b0f4066SDimitry Andric ? (STI.isThumb() ? ARM::tTAILJMPd : ARM::TAILJMPd) 4173b0f4066SDimitry Andric : (STI.isThumb() ? ARM::tTAILJMPdND : ARM::TAILJMPdND); 4182754fe60SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(TCOpcode)); 4192754fe60SDimitry Andric if (JumpTarget.isGlobal()) 4202754fe60SDimitry Andric MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(), 4212754fe60SDimitry Andric JumpTarget.getTargetFlags()); 4222754fe60SDimitry Andric else { 4232754fe60SDimitry Andric assert(JumpTarget.isSymbol()); 4242754fe60SDimitry Andric MIB.addExternalSymbol(JumpTarget.getSymbolName(), 4252754fe60SDimitry Andric JumpTarget.getTargetFlags()); 4262754fe60SDimitry Andric } 4276122f3e6SDimitry Andric 4286122f3e6SDimitry Andric // Add the default predicate in Thumb mode. 4296122f3e6SDimitry Andric if (STI.isThumb()) MIB.addImm(ARMCC::AL).addReg(0); 4302754fe60SDimitry Andric } else if (RetOpcode == ARM::TCRETURNri) { 4313b0f4066SDimitry Andric BuildMI(MBB, MBBI, dl, 4323b0f4066SDimitry Andric TII.get(STI.isThumb() ? ARM::tTAILJMPr : ARM::TAILJMPr)). 4332754fe60SDimitry Andric addReg(JumpTarget.getReg(), RegState::Kill); 4342754fe60SDimitry Andric } else if (RetOpcode == ARM::TCRETURNriND) { 4353b0f4066SDimitry Andric BuildMI(MBB, MBBI, dl, 4363b0f4066SDimitry Andric TII.get(STI.isThumb() ? ARM::tTAILJMPrND : ARM::TAILJMPrND)). 4372754fe60SDimitry Andric addReg(JumpTarget.getReg(), RegState::Kill); 4382754fe60SDimitry Andric } 4392754fe60SDimitry Andric 4402754fe60SDimitry Andric MachineInstr *NewMI = prior(MBBI); 4412754fe60SDimitry Andric for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i) 4422754fe60SDimitry Andric NewMI->addOperand(MBBI->getOperand(i)); 4432754fe60SDimitry Andric 4442754fe60SDimitry Andric // Delete the pseudo instruction TCRETURN. 4452754fe60SDimitry Andric MBB.erase(MBBI); 44617a519f9SDimitry Andric MBBI = NewMI; 4472754fe60SDimitry Andric } 4482754fe60SDimitry Andric 4492754fe60SDimitry Andric if (VARegSaveSize) 4502754fe60SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, VARegSaveSize); 4512754fe60SDimitry Andric } 4522754fe60SDimitry Andric 4532754fe60SDimitry Andric /// getFrameIndexReference - Provide a base+offset reference to an FI slot for 4542754fe60SDimitry Andric /// debug info. It's the same as what we use for resolving the code-gen 4552754fe60SDimitry Andric /// references for now. FIXME: This can go wrong when references are 4562754fe60SDimitry Andric /// SP-relative and simple call frames aren't used. 4572754fe60SDimitry Andric int 4582754fe60SDimitry Andric ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI, 4592754fe60SDimitry Andric unsigned &FrameReg) const { 4602754fe60SDimitry Andric return ResolveFrameIndexReference(MF, FI, FrameReg, 0); 4612754fe60SDimitry Andric } 4622754fe60SDimitry Andric 4632754fe60SDimitry Andric int 4642754fe60SDimitry Andric ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF, 4653b0f4066SDimitry Andric int FI, unsigned &FrameReg, 4662754fe60SDimitry Andric int SPAdj) const { 4672754fe60SDimitry Andric const MachineFrameInfo *MFI = MF.getFrameInfo(); 4682754fe60SDimitry Andric const ARMBaseRegisterInfo *RegInfo = 4692754fe60SDimitry Andric static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo()); 4702754fe60SDimitry Andric const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 4712754fe60SDimitry Andric int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize(); 4722754fe60SDimitry Andric int FPOffset = Offset - AFI->getFramePtrSpillOffset(); 4732754fe60SDimitry Andric bool isFixed = MFI->isFixedObjectIndex(FI); 4742754fe60SDimitry Andric 4752754fe60SDimitry Andric FrameReg = ARM::SP; 4762754fe60SDimitry Andric Offset += SPAdj; 4772754fe60SDimitry Andric if (AFI->isGPRCalleeSavedArea1Frame(FI)) 4782754fe60SDimitry Andric return Offset - AFI->getGPRCalleeSavedArea1Offset(); 4792754fe60SDimitry Andric else if (AFI->isGPRCalleeSavedArea2Frame(FI)) 4802754fe60SDimitry Andric return Offset - AFI->getGPRCalleeSavedArea2Offset(); 4812754fe60SDimitry Andric else if (AFI->isDPRCalleeSavedAreaFrame(FI)) 4822754fe60SDimitry Andric return Offset - AFI->getDPRCalleeSavedAreaOffset(); 4832754fe60SDimitry Andric 4842754fe60SDimitry Andric // When dynamically realigning the stack, use the frame pointer for 4852754fe60SDimitry Andric // parameters, and the stack/base pointer for locals. 4862754fe60SDimitry Andric if (RegInfo->needsStackRealignment(MF)) { 4872754fe60SDimitry Andric assert (hasFP(MF) && "dynamic stack realignment without a FP!"); 4882754fe60SDimitry Andric if (isFixed) { 4892754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 4902754fe60SDimitry Andric Offset = FPOffset; 4912754fe60SDimitry Andric } else if (MFI->hasVarSizedObjects()) { 4922754fe60SDimitry Andric assert(RegInfo->hasBasePointer(MF) && 4932754fe60SDimitry Andric "VLAs and dynamic stack alignment, but missing base pointer!"); 4942754fe60SDimitry Andric FrameReg = RegInfo->getBaseRegister(); 4952754fe60SDimitry Andric } 4962754fe60SDimitry Andric return Offset; 4972754fe60SDimitry Andric } 4982754fe60SDimitry Andric 4992754fe60SDimitry Andric // If there is a frame pointer, use it when we can. 5002754fe60SDimitry Andric if (hasFP(MF) && AFI->hasStackFrame()) { 5012754fe60SDimitry Andric // Use frame pointer to reference fixed objects. Use it for locals if 5022754fe60SDimitry Andric // there are VLAs (and thus the SP isn't reliable as a base). 5032754fe60SDimitry Andric if (isFixed || (MFI->hasVarSizedObjects() && 5042754fe60SDimitry Andric !RegInfo->hasBasePointer(MF))) { 5052754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 5062754fe60SDimitry Andric return FPOffset; 5072754fe60SDimitry Andric } else if (MFI->hasVarSizedObjects()) { 5082754fe60SDimitry Andric assert(RegInfo->hasBasePointer(MF) && "missing base pointer!"); 5093b0f4066SDimitry Andric if (AFI->isThumb2Function()) { 5102754fe60SDimitry Andric // Try to use the frame pointer if we can, else use the base pointer 5112754fe60SDimitry Andric // since it's available. This is handy for the emergency spill slot, in 5122754fe60SDimitry Andric // particular. 5132754fe60SDimitry Andric if (FPOffset >= -255 && FPOffset < 0) { 5142754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 5152754fe60SDimitry Andric return FPOffset; 5162754fe60SDimitry Andric } 5173b0f4066SDimitry Andric } 5182754fe60SDimitry Andric } else if (AFI->isThumb2Function()) { 5193b0f4066SDimitry Andric // Use add <rd>, sp, #<imm8> 5203b0f4066SDimitry Andric // ldr <rd>, [sp, #<imm8>] 5213b0f4066SDimitry Andric // if at all possible to save space. 5223b0f4066SDimitry Andric if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020) 5233b0f4066SDimitry Andric return Offset; 5242754fe60SDimitry Andric // In Thumb2 mode, the negative offset is very limited. Try to avoid 5253b0f4066SDimitry Andric // out of range references. ldr <rt>,[<rn>, #-<imm8>] 5262754fe60SDimitry Andric if (FPOffset >= -255 && FPOffset < 0) { 5272754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 5282754fe60SDimitry Andric return FPOffset; 5292754fe60SDimitry Andric } 5302754fe60SDimitry Andric } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) { 5312754fe60SDimitry Andric // Otherwise, use SP or FP, whichever is closer to the stack slot. 5322754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 5332754fe60SDimitry Andric return FPOffset; 5342754fe60SDimitry Andric } 5352754fe60SDimitry Andric } 5362754fe60SDimitry Andric // Use the base pointer if we have one. 5372754fe60SDimitry Andric if (RegInfo->hasBasePointer(MF)) 5382754fe60SDimitry Andric FrameReg = RegInfo->getBaseRegister(); 5392754fe60SDimitry Andric return Offset; 5402754fe60SDimitry Andric } 5412754fe60SDimitry Andric 5422754fe60SDimitry Andric int ARMFrameLowering::getFrameIndexOffset(const MachineFunction &MF, 5432754fe60SDimitry Andric int FI) const { 5442754fe60SDimitry Andric unsigned FrameReg; 5452754fe60SDimitry Andric return getFrameIndexReference(MF, FI, FrameReg); 5462754fe60SDimitry Andric } 5472754fe60SDimitry Andric 5482754fe60SDimitry Andric void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB, 5492754fe60SDimitry Andric MachineBasicBlock::iterator MI, 5502754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 5512754fe60SDimitry Andric unsigned StmOpc, unsigned StrOpc, 5522754fe60SDimitry Andric bool NoGap, 5533b0f4066SDimitry Andric bool(*Func)(unsigned, bool), 5543b0f4066SDimitry Andric unsigned MIFlags) const { 5552754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 5562754fe60SDimitry Andric const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); 5572754fe60SDimitry Andric 5582754fe60SDimitry Andric DebugLoc DL; 5592754fe60SDimitry Andric if (MI != MBB.end()) DL = MI->getDebugLoc(); 5602754fe60SDimitry Andric 5612754fe60SDimitry Andric SmallVector<std::pair<unsigned,bool>, 4> Regs; 5622754fe60SDimitry Andric unsigned i = CSI.size(); 5632754fe60SDimitry Andric while (i != 0) { 5642754fe60SDimitry Andric unsigned LastReg = 0; 5652754fe60SDimitry Andric for (; i != 0; --i) { 5662754fe60SDimitry Andric unsigned Reg = CSI[i-1].getReg(); 5672754fe60SDimitry Andric if (!(Func)(Reg, STI.isTargetDarwin())) continue; 5682754fe60SDimitry Andric 5692754fe60SDimitry Andric // Add the callee-saved register as live-in unless it's LR and 5702754fe60SDimitry Andric // @llvm.returnaddress is called. If LR is returned for 5712754fe60SDimitry Andric // @llvm.returnaddress then it's already added to the function and 5722754fe60SDimitry Andric // entry block live-in sets. 5732754fe60SDimitry Andric bool isKill = true; 5742754fe60SDimitry Andric if (Reg == ARM::LR) { 5752754fe60SDimitry Andric if (MF.getFrameInfo()->isReturnAddressTaken() && 5762754fe60SDimitry Andric MF.getRegInfo().isLiveIn(Reg)) 5772754fe60SDimitry Andric isKill = false; 5782754fe60SDimitry Andric } 5792754fe60SDimitry Andric 5802754fe60SDimitry Andric if (isKill) 5812754fe60SDimitry Andric MBB.addLiveIn(Reg); 5822754fe60SDimitry Andric 5832754fe60SDimitry Andric // If NoGap is true, push consecutive registers and then leave the rest 5842754fe60SDimitry Andric // for other instructions. e.g. 5852754fe60SDimitry Andric // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11} 5862754fe60SDimitry Andric if (NoGap && LastReg && LastReg != Reg-1) 5872754fe60SDimitry Andric break; 5882754fe60SDimitry Andric LastReg = Reg; 5892754fe60SDimitry Andric Regs.push_back(std::make_pair(Reg, isKill)); 5902754fe60SDimitry Andric } 5912754fe60SDimitry Andric 5922754fe60SDimitry Andric if (Regs.empty()) 5932754fe60SDimitry Andric continue; 5942754fe60SDimitry Andric if (Regs.size() > 1 || StrOpc== 0) { 5952754fe60SDimitry Andric MachineInstrBuilder MIB = 5962754fe60SDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP) 5973b0f4066SDimitry Andric .addReg(ARM::SP).setMIFlags(MIFlags)); 5982754fe60SDimitry Andric for (unsigned i = 0, e = Regs.size(); i < e; ++i) 5992754fe60SDimitry Andric MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second)); 6002754fe60SDimitry Andric } else if (Regs.size() == 1) { 6012754fe60SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc), 6022754fe60SDimitry Andric ARM::SP) 6032754fe60SDimitry Andric .addReg(Regs[0].first, getKillRegState(Regs[0].second)) 6046122f3e6SDimitry Andric .addReg(ARM::SP).setMIFlags(MIFlags) 6056122f3e6SDimitry Andric .addImm(-4); 6062754fe60SDimitry Andric AddDefaultPred(MIB); 6072754fe60SDimitry Andric } 6082754fe60SDimitry Andric Regs.clear(); 6092754fe60SDimitry Andric } 6102754fe60SDimitry Andric } 6112754fe60SDimitry Andric 6122754fe60SDimitry Andric void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB, 6132754fe60SDimitry Andric MachineBasicBlock::iterator MI, 6142754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 6152754fe60SDimitry Andric unsigned LdmOpc, unsigned LdrOpc, 6162754fe60SDimitry Andric bool isVarArg, bool NoGap, 6172754fe60SDimitry Andric bool(*Func)(unsigned, bool)) const { 6182754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 6192754fe60SDimitry Andric const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); 6202754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 6212754fe60SDimitry Andric DebugLoc DL = MI->getDebugLoc(); 6222754fe60SDimitry Andric unsigned RetOpcode = MI->getOpcode(); 6232754fe60SDimitry Andric bool isTailCall = (RetOpcode == ARM::TCRETURNdi || 6242754fe60SDimitry Andric RetOpcode == ARM::TCRETURNdiND || 6252754fe60SDimitry Andric RetOpcode == ARM::TCRETURNri || 6262754fe60SDimitry Andric RetOpcode == ARM::TCRETURNriND); 6272754fe60SDimitry Andric 6282754fe60SDimitry Andric SmallVector<unsigned, 4> Regs; 6292754fe60SDimitry Andric unsigned i = CSI.size(); 6302754fe60SDimitry Andric while (i != 0) { 6312754fe60SDimitry Andric unsigned LastReg = 0; 6322754fe60SDimitry Andric bool DeleteRet = false; 6332754fe60SDimitry Andric for (; i != 0; --i) { 6342754fe60SDimitry Andric unsigned Reg = CSI[i-1].getReg(); 6352754fe60SDimitry Andric if (!(Func)(Reg, STI.isTargetDarwin())) continue; 6362754fe60SDimitry Andric 6372754fe60SDimitry Andric if (Reg == ARM::LR && !isTailCall && !isVarArg && STI.hasV5TOps()) { 6382754fe60SDimitry Andric Reg = ARM::PC; 6392754fe60SDimitry Andric LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET; 6402754fe60SDimitry Andric // Fold the return instruction into the LDM. 6412754fe60SDimitry Andric DeleteRet = true; 6422754fe60SDimitry Andric } 6432754fe60SDimitry Andric 6442754fe60SDimitry Andric // If NoGap is true, pop consecutive registers and then leave the rest 6452754fe60SDimitry Andric // for other instructions. e.g. 6462754fe60SDimitry Andric // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11} 6472754fe60SDimitry Andric if (NoGap && LastReg && LastReg != Reg-1) 6482754fe60SDimitry Andric break; 6492754fe60SDimitry Andric 6502754fe60SDimitry Andric LastReg = Reg; 6512754fe60SDimitry Andric Regs.push_back(Reg); 6522754fe60SDimitry Andric } 6532754fe60SDimitry Andric 6542754fe60SDimitry Andric if (Regs.empty()) 6552754fe60SDimitry Andric continue; 6562754fe60SDimitry Andric if (Regs.size() > 1 || LdrOpc == 0) { 6572754fe60SDimitry Andric MachineInstrBuilder MIB = 6582754fe60SDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP) 6592754fe60SDimitry Andric .addReg(ARM::SP)); 6602754fe60SDimitry Andric for (unsigned i = 0, e = Regs.size(); i < e; ++i) 6612754fe60SDimitry Andric MIB.addReg(Regs[i], getDefRegState(true)); 6626122f3e6SDimitry Andric if (DeleteRet) { 6636122f3e6SDimitry Andric MIB->copyImplicitOps(&*MI); 6642754fe60SDimitry Andric MI->eraseFromParent(); 6656122f3e6SDimitry Andric } 6662754fe60SDimitry Andric MI = MIB; 6672754fe60SDimitry Andric } else if (Regs.size() == 1) { 6682754fe60SDimitry Andric // If we adjusted the reg to PC from LR above, switch it back here. We 6692754fe60SDimitry Andric // only do that for LDM. 6702754fe60SDimitry Andric if (Regs[0] == ARM::PC) 6712754fe60SDimitry Andric Regs[0] = ARM::LR; 6722754fe60SDimitry Andric MachineInstrBuilder MIB = 6732754fe60SDimitry Andric BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0]) 6742754fe60SDimitry Andric .addReg(ARM::SP, RegState::Define) 6752754fe60SDimitry Andric .addReg(ARM::SP); 6762754fe60SDimitry Andric // ARM mode needs an extra reg0 here due to addrmode2. Will go away once 6772754fe60SDimitry Andric // that refactoring is complete (eventually). 6786122f3e6SDimitry Andric if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) { 6792754fe60SDimitry Andric MIB.addReg(0); 6802754fe60SDimitry Andric MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift)); 6812754fe60SDimitry Andric } else 6822754fe60SDimitry Andric MIB.addImm(4); 6832754fe60SDimitry Andric AddDefaultPred(MIB); 6842754fe60SDimitry Andric } 6852754fe60SDimitry Andric Regs.clear(); 6862754fe60SDimitry Andric } 6872754fe60SDimitry Andric } 6882754fe60SDimitry Andric 6892754fe60SDimitry Andric bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB, 6902754fe60SDimitry Andric MachineBasicBlock::iterator MI, 6912754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 6922754fe60SDimitry Andric const TargetRegisterInfo *TRI) const { 6932754fe60SDimitry Andric if (CSI.empty()) 6942754fe60SDimitry Andric return false; 6952754fe60SDimitry Andric 6962754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 6972754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 6982754fe60SDimitry Andric 6992754fe60SDimitry Andric unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD; 7006122f3e6SDimitry Andric unsigned PushOneOpc = AFI->isThumbFunction() ? 7016122f3e6SDimitry Andric ARM::t2STR_PRE : ARM::STR_PRE_IMM; 7022754fe60SDimitry Andric unsigned FltOpc = ARM::VSTMDDB_UPD; 7033b0f4066SDimitry Andric emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 7043b0f4066SDimitry Andric MachineInstr::FrameSetup); 7053b0f4066SDimitry Andric emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 7063b0f4066SDimitry Andric MachineInstr::FrameSetup); 7073b0f4066SDimitry Andric emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register, 7083b0f4066SDimitry Andric MachineInstr::FrameSetup); 7092754fe60SDimitry Andric 7102754fe60SDimitry Andric return true; 7112754fe60SDimitry Andric } 7122754fe60SDimitry Andric 7132754fe60SDimitry Andric bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, 7142754fe60SDimitry Andric MachineBasicBlock::iterator MI, 7152754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 7162754fe60SDimitry Andric const TargetRegisterInfo *TRI) const { 7172754fe60SDimitry Andric if (CSI.empty()) 7182754fe60SDimitry Andric return false; 7192754fe60SDimitry Andric 7202754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 7212754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 7222754fe60SDimitry Andric bool isVarArg = AFI->getVarArgsRegSaveSize() > 0; 7232754fe60SDimitry Andric 7242754fe60SDimitry Andric unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD; 7256122f3e6SDimitry Andric unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM; 7262754fe60SDimitry Andric unsigned FltOpc = ARM::VLDMDIA_UPD; 7272754fe60SDimitry Andric emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register); 7282754fe60SDimitry Andric emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false, 7292754fe60SDimitry Andric &isARMArea2Register); 7302754fe60SDimitry Andric emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false, 7312754fe60SDimitry Andric &isARMArea1Register); 7322754fe60SDimitry Andric 7332754fe60SDimitry Andric return true; 7342754fe60SDimitry Andric } 7352754fe60SDimitry Andric 7362754fe60SDimitry Andric // FIXME: Make generic? 7372754fe60SDimitry Andric static unsigned GetFunctionSizeInBytes(const MachineFunction &MF, 7382754fe60SDimitry Andric const ARMBaseInstrInfo &TII) { 7392754fe60SDimitry Andric unsigned FnSize = 0; 7402754fe60SDimitry Andric for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end(); 7412754fe60SDimitry Andric MBBI != E; ++MBBI) { 7422754fe60SDimitry Andric const MachineBasicBlock &MBB = *MBBI; 7432754fe60SDimitry Andric for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end(); 7442754fe60SDimitry Andric I != E; ++I) 7452754fe60SDimitry Andric FnSize += TII.GetInstSizeInBytes(I); 7462754fe60SDimitry Andric } 7472754fe60SDimitry Andric return FnSize; 7482754fe60SDimitry Andric } 7492754fe60SDimitry Andric 7502754fe60SDimitry Andric /// estimateStackSize - Estimate and return the size of the frame. 7512754fe60SDimitry Andric /// FIXME: Make generic? 7522754fe60SDimitry Andric static unsigned estimateStackSize(MachineFunction &MF) { 75317a519f9SDimitry Andric const MachineFrameInfo *MFI = MF.getFrameInfo(); 75417a519f9SDimitry Andric const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering(); 75517a519f9SDimitry Andric const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo(); 75617a519f9SDimitry Andric unsigned MaxAlign = MFI->getMaxAlignment(); 7572754fe60SDimitry Andric int Offset = 0; 75817a519f9SDimitry Andric 75917a519f9SDimitry Andric // This code is very, very similar to PEI::calculateFrameObjectOffsets(). 76017a519f9SDimitry Andric // It really should be refactored to share code. Until then, changes 76117a519f9SDimitry Andric // should keep in mind that there's tight coupling between the two. 76217a519f9SDimitry Andric 76317a519f9SDimitry Andric for (int i = MFI->getObjectIndexBegin(); i != 0; ++i) { 76417a519f9SDimitry Andric int FixedOff = -MFI->getObjectOffset(i); 7652754fe60SDimitry Andric if (FixedOff > Offset) Offset = FixedOff; 7662754fe60SDimitry Andric } 76717a519f9SDimitry Andric for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) { 76817a519f9SDimitry Andric if (MFI->isDeadObjectIndex(i)) 7692754fe60SDimitry Andric continue; 77017a519f9SDimitry Andric Offset += MFI->getObjectSize(i); 77117a519f9SDimitry Andric unsigned Align = MFI->getObjectAlignment(i); 7722754fe60SDimitry Andric // Adjust to alignment boundary 7732754fe60SDimitry Andric Offset = (Offset+Align-1)/Align*Align; 77417a519f9SDimitry Andric 77517a519f9SDimitry Andric MaxAlign = std::max(Align, MaxAlign); 7762754fe60SDimitry Andric } 77717a519f9SDimitry Andric 77817a519f9SDimitry Andric if (MFI->adjustsStack() && TFI->hasReservedCallFrame(MF)) 77917a519f9SDimitry Andric Offset += MFI->getMaxCallFrameSize(); 78017a519f9SDimitry Andric 78117a519f9SDimitry Andric // Round up the size to a multiple of the alignment. If the function has 78217a519f9SDimitry Andric // any calls or alloca's, align to the target's StackAlignment value to 78317a519f9SDimitry Andric // ensure that the callee's frame or the alloca data is suitably aligned; 78417a519f9SDimitry Andric // otherwise, for leaf functions, align to the TransientStackAlignment 78517a519f9SDimitry Andric // value. 78617a519f9SDimitry Andric unsigned StackAlign; 78717a519f9SDimitry Andric if (MFI->adjustsStack() || MFI->hasVarSizedObjects() || 78817a519f9SDimitry Andric (RegInfo->needsStackRealignment(MF) && MFI->getObjectIndexEnd() != 0)) 78917a519f9SDimitry Andric StackAlign = TFI->getStackAlignment(); 79017a519f9SDimitry Andric else 79117a519f9SDimitry Andric StackAlign = TFI->getTransientStackAlignment(); 79217a519f9SDimitry Andric 79317a519f9SDimitry Andric // If the frame pointer is eliminated, all frame offsets will be relative to 79417a519f9SDimitry Andric // SP not FP. Align to MaxAlign so this works. 79517a519f9SDimitry Andric StackAlign = std::max(StackAlign, MaxAlign); 79617a519f9SDimitry Andric unsigned AlignMask = StackAlign - 1; 79717a519f9SDimitry Andric Offset = (Offset + AlignMask) & ~uint64_t(AlignMask); 79817a519f9SDimitry Andric 7992754fe60SDimitry Andric return (unsigned)Offset; 8002754fe60SDimitry Andric } 8012754fe60SDimitry Andric 8022754fe60SDimitry Andric /// estimateRSStackSizeLimit - Look at each instruction that references stack 8032754fe60SDimitry Andric /// frames and return the stack size limit beyond which some of these 8042754fe60SDimitry Andric /// instructions will require a scratch register during their expansion later. 8052754fe60SDimitry Andric // FIXME: Move to TII? 8062754fe60SDimitry Andric static unsigned estimateRSStackSizeLimit(MachineFunction &MF, 8072754fe60SDimitry Andric const TargetFrameLowering *TFI) { 8082754fe60SDimitry Andric const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 8092754fe60SDimitry Andric unsigned Limit = (1 << 12) - 1; 8102754fe60SDimitry Andric for (MachineFunction::iterator BB = MF.begin(),E = MF.end(); BB != E; ++BB) { 8112754fe60SDimitry Andric for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); 8122754fe60SDimitry Andric I != E; ++I) { 8132754fe60SDimitry Andric for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { 8142754fe60SDimitry Andric if (!I->getOperand(i).isFI()) continue; 8152754fe60SDimitry Andric 8162754fe60SDimitry Andric // When using ADDri to get the address of a stack object, 255 is the 8172754fe60SDimitry Andric // largest offset guaranteed to fit in the immediate offset. 8182754fe60SDimitry Andric if (I->getOpcode() == ARM::ADDri) { 8192754fe60SDimitry Andric Limit = std::min(Limit, (1U << 8) - 1); 8202754fe60SDimitry Andric break; 8212754fe60SDimitry Andric } 8222754fe60SDimitry Andric 8232754fe60SDimitry Andric // Otherwise check the addressing mode. 8242754fe60SDimitry Andric switch (I->getDesc().TSFlags & ARMII::AddrModeMask) { 8252754fe60SDimitry Andric case ARMII::AddrMode3: 8262754fe60SDimitry Andric case ARMII::AddrModeT2_i8: 8272754fe60SDimitry Andric Limit = std::min(Limit, (1U << 8) - 1); 8282754fe60SDimitry Andric break; 8292754fe60SDimitry Andric case ARMII::AddrMode5: 8302754fe60SDimitry Andric case ARMII::AddrModeT2_i8s4: 8312754fe60SDimitry Andric Limit = std::min(Limit, ((1U << 8) - 1) * 4); 8322754fe60SDimitry Andric break; 8332754fe60SDimitry Andric case ARMII::AddrModeT2_i12: 8342754fe60SDimitry Andric // i12 supports only positive offset so these will be converted to 8352754fe60SDimitry Andric // i8 opcodes. See llvm::rewriteT2FrameIndex. 8362754fe60SDimitry Andric if (TFI->hasFP(MF) && AFI->hasStackFrame()) 8372754fe60SDimitry Andric Limit = std::min(Limit, (1U << 8) - 1); 8382754fe60SDimitry Andric break; 8392754fe60SDimitry Andric case ARMII::AddrMode4: 8402754fe60SDimitry Andric case ARMII::AddrMode6: 8412754fe60SDimitry Andric // Addressing modes 4 & 6 (load/store) instructions can't encode an 8422754fe60SDimitry Andric // immediate offset for stack references. 8432754fe60SDimitry Andric return 0; 8442754fe60SDimitry Andric default: 8452754fe60SDimitry Andric break; 8462754fe60SDimitry Andric } 8472754fe60SDimitry Andric break; // At most one FI per instruction 8482754fe60SDimitry Andric } 8492754fe60SDimitry Andric } 8502754fe60SDimitry Andric } 8512754fe60SDimitry Andric 8522754fe60SDimitry Andric return Limit; 8532754fe60SDimitry Andric } 8542754fe60SDimitry Andric 8552754fe60SDimitry Andric void 8562754fe60SDimitry Andric ARMFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF, 8572754fe60SDimitry Andric RegScavenger *RS) const { 8582754fe60SDimitry Andric // This tells PEI to spill the FP as if it is any other callee-save register 8592754fe60SDimitry Andric // to take advantage the eliminateFrameIndex machinery. This also ensures it 8602754fe60SDimitry Andric // is spilled in the order specified by getCalleeSavedRegs() to make it easier 8612754fe60SDimitry Andric // to combine multiple loads / stores. 8622754fe60SDimitry Andric bool CanEliminateFrame = true; 8632754fe60SDimitry Andric bool CS1Spilled = false; 8642754fe60SDimitry Andric bool LRSpilled = false; 8652754fe60SDimitry Andric unsigned NumGPRSpills = 0; 8662754fe60SDimitry Andric SmallVector<unsigned, 4> UnspilledCS1GPRs; 8672754fe60SDimitry Andric SmallVector<unsigned, 4> UnspilledCS2GPRs; 8682754fe60SDimitry Andric const ARMBaseRegisterInfo *RegInfo = 8692754fe60SDimitry Andric static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo()); 8702754fe60SDimitry Andric const ARMBaseInstrInfo &TII = 8712754fe60SDimitry Andric *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo()); 8722754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 8732754fe60SDimitry Andric MachineFrameInfo *MFI = MF.getFrameInfo(); 8742754fe60SDimitry Andric unsigned FramePtr = RegInfo->getFrameRegister(MF); 8752754fe60SDimitry Andric 8762754fe60SDimitry Andric // Spill R4 if Thumb2 function requires stack realignment - it will be used as 8772754fe60SDimitry Andric // scratch register. Also spill R4 if Thumb2 function has varsized objects, 8782754fe60SDimitry Andric // since it's not always possible to restore sp from fp in a single 8792754fe60SDimitry Andric // instruction. 8802754fe60SDimitry Andric // FIXME: It will be better just to find spare register here. 8812754fe60SDimitry Andric if (AFI->isThumb2Function() && 8822754fe60SDimitry Andric (MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(MF))) 8832754fe60SDimitry Andric MF.getRegInfo().setPhysRegUsed(ARM::R4); 8842754fe60SDimitry Andric 8852754fe60SDimitry Andric if (AFI->isThumb1OnlyFunction()) { 8862754fe60SDimitry Andric // Spill LR if Thumb1 function uses variable length argument lists. 8872754fe60SDimitry Andric if (AFI->getVarArgsRegSaveSize() > 0) 8882754fe60SDimitry Andric MF.getRegInfo().setPhysRegUsed(ARM::LR); 8892754fe60SDimitry Andric 89017a519f9SDimitry Andric // Spill R4 if Thumb1 epilogue has to restore SP from FP. We don't know 89117a519f9SDimitry Andric // for sure what the stack size will be, but for this, an estimate is good 89217a519f9SDimitry Andric // enough. If there anything changes it, it'll be a spill, which implies 89317a519f9SDimitry Andric // we've used all the registers and so R4 is already used, so not marking 89417a519f9SDimitry Andric // it here will be OK. 8952754fe60SDimitry Andric // FIXME: It will be better just to find spare register here. 89617a519f9SDimitry Andric unsigned StackSize = estimateStackSize(MF); 89717a519f9SDimitry Andric if (MFI->hasVarSizedObjects() || StackSize > 508) 8982754fe60SDimitry Andric MF.getRegInfo().setPhysRegUsed(ARM::R4); 8992754fe60SDimitry Andric } 9002754fe60SDimitry Andric 9012754fe60SDimitry Andric // Spill the BasePtr if it's used. 9022754fe60SDimitry Andric if (RegInfo->hasBasePointer(MF)) 9032754fe60SDimitry Andric MF.getRegInfo().setPhysRegUsed(RegInfo->getBaseRegister()); 9042754fe60SDimitry Andric 9052754fe60SDimitry Andric // Don't spill FP if the frame can be eliminated. This is determined 9062754fe60SDimitry Andric // by scanning the callee-save registers to see if any is used. 9072754fe60SDimitry Andric const unsigned *CSRegs = RegInfo->getCalleeSavedRegs(); 9082754fe60SDimitry Andric for (unsigned i = 0; CSRegs[i]; ++i) { 9092754fe60SDimitry Andric unsigned Reg = CSRegs[i]; 9102754fe60SDimitry Andric bool Spilled = false; 9112754fe60SDimitry Andric if (MF.getRegInfo().isPhysRegUsed(Reg)) { 9122754fe60SDimitry Andric Spilled = true; 9132754fe60SDimitry Andric CanEliminateFrame = false; 9142754fe60SDimitry Andric } else { 9152754fe60SDimitry Andric // Check alias registers too. 9162754fe60SDimitry Andric for (const unsigned *Aliases = 9172754fe60SDimitry Andric RegInfo->getAliasSet(Reg); *Aliases; ++Aliases) { 9182754fe60SDimitry Andric if (MF.getRegInfo().isPhysRegUsed(*Aliases)) { 9192754fe60SDimitry Andric Spilled = true; 9202754fe60SDimitry Andric CanEliminateFrame = false; 9212754fe60SDimitry Andric } 9222754fe60SDimitry Andric } 9232754fe60SDimitry Andric } 9242754fe60SDimitry Andric 9252754fe60SDimitry Andric if (!ARM::GPRRegisterClass->contains(Reg)) 9262754fe60SDimitry Andric continue; 9272754fe60SDimitry Andric 9282754fe60SDimitry Andric if (Spilled) { 9292754fe60SDimitry Andric NumGPRSpills++; 9302754fe60SDimitry Andric 9312754fe60SDimitry Andric if (!STI.isTargetDarwin()) { 9322754fe60SDimitry Andric if (Reg == ARM::LR) 9332754fe60SDimitry Andric LRSpilled = true; 9342754fe60SDimitry Andric CS1Spilled = true; 9352754fe60SDimitry Andric continue; 9362754fe60SDimitry Andric } 9372754fe60SDimitry Andric 9382754fe60SDimitry Andric // Keep track if LR and any of R4, R5, R6, and R7 is spilled. 9392754fe60SDimitry Andric switch (Reg) { 9402754fe60SDimitry Andric case ARM::LR: 9412754fe60SDimitry Andric LRSpilled = true; 9422754fe60SDimitry Andric // Fallthrough 9432754fe60SDimitry Andric case ARM::R4: case ARM::R5: 9442754fe60SDimitry Andric case ARM::R6: case ARM::R7: 9452754fe60SDimitry Andric CS1Spilled = true; 9462754fe60SDimitry Andric break; 9472754fe60SDimitry Andric default: 9482754fe60SDimitry Andric break; 9492754fe60SDimitry Andric } 9502754fe60SDimitry Andric } else { 9512754fe60SDimitry Andric if (!STI.isTargetDarwin()) { 9522754fe60SDimitry Andric UnspilledCS1GPRs.push_back(Reg); 9532754fe60SDimitry Andric continue; 9542754fe60SDimitry Andric } 9552754fe60SDimitry Andric 9562754fe60SDimitry Andric switch (Reg) { 9572754fe60SDimitry Andric case ARM::R4: case ARM::R5: 9582754fe60SDimitry Andric case ARM::R6: case ARM::R7: 9592754fe60SDimitry Andric case ARM::LR: 9602754fe60SDimitry Andric UnspilledCS1GPRs.push_back(Reg); 9612754fe60SDimitry Andric break; 9622754fe60SDimitry Andric default: 9632754fe60SDimitry Andric UnspilledCS2GPRs.push_back(Reg); 9642754fe60SDimitry Andric break; 9652754fe60SDimitry Andric } 9662754fe60SDimitry Andric } 9672754fe60SDimitry Andric } 9682754fe60SDimitry Andric 9692754fe60SDimitry Andric bool ForceLRSpill = false; 9702754fe60SDimitry Andric if (!LRSpilled && AFI->isThumb1OnlyFunction()) { 9712754fe60SDimitry Andric unsigned FnSize = GetFunctionSizeInBytes(MF, TII); 9722754fe60SDimitry Andric // Force LR to be spilled if the Thumb function size is > 2048. This enables 9732754fe60SDimitry Andric // use of BL to implement far jump. If it turns out that it's not needed 9742754fe60SDimitry Andric // then the branch fix up path will undo it. 9752754fe60SDimitry Andric if (FnSize >= (1 << 11)) { 9762754fe60SDimitry Andric CanEliminateFrame = false; 9772754fe60SDimitry Andric ForceLRSpill = true; 9782754fe60SDimitry Andric } 9792754fe60SDimitry Andric } 9802754fe60SDimitry Andric 9812754fe60SDimitry Andric // If any of the stack slot references may be out of range of an immediate 9822754fe60SDimitry Andric // offset, make sure a register (or a spill slot) is available for the 9832754fe60SDimitry Andric // register scavenger. Note that if we're indexing off the frame pointer, the 9842754fe60SDimitry Andric // effective stack size is 4 bytes larger since the FP points to the stack 9852754fe60SDimitry Andric // slot of the previous FP. Also, if we have variable sized objects in the 9862754fe60SDimitry Andric // function, stack slot references will often be negative, and some of 9872754fe60SDimitry Andric // our instructions are positive-offset only, so conservatively consider 9882754fe60SDimitry Andric // that case to want a spill slot (or register) as well. Similarly, if 9892754fe60SDimitry Andric // the function adjusts the stack pointer during execution and the 9902754fe60SDimitry Andric // adjustments aren't already part of our stack size estimate, our offset 9912754fe60SDimitry Andric // calculations may be off, so be conservative. 9922754fe60SDimitry Andric // FIXME: We could add logic to be more precise about negative offsets 9932754fe60SDimitry Andric // and which instructions will need a scratch register for them. Is it 9942754fe60SDimitry Andric // worth the effort and added fragility? 9952754fe60SDimitry Andric bool BigStack = 9962754fe60SDimitry Andric (RS && 9972754fe60SDimitry Andric (estimateStackSize(MF) + ((hasFP(MF) && AFI->hasStackFrame()) ? 4:0) >= 9982754fe60SDimitry Andric estimateRSStackSizeLimit(MF, this))) 9992754fe60SDimitry Andric || MFI->hasVarSizedObjects() 10002754fe60SDimitry Andric || (MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF)); 10012754fe60SDimitry Andric 10022754fe60SDimitry Andric bool ExtraCSSpill = false; 10032754fe60SDimitry Andric if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) { 10042754fe60SDimitry Andric AFI->setHasStackFrame(true); 10052754fe60SDimitry Andric 10062754fe60SDimitry Andric // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled. 10072754fe60SDimitry Andric // Spill LR as well so we can fold BX_RET to the registers restore (LDM). 10082754fe60SDimitry Andric if (!LRSpilled && CS1Spilled) { 10092754fe60SDimitry Andric MF.getRegInfo().setPhysRegUsed(ARM::LR); 10102754fe60SDimitry Andric NumGPRSpills++; 10112754fe60SDimitry Andric UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(), 10122754fe60SDimitry Andric UnspilledCS1GPRs.end(), (unsigned)ARM::LR)); 10132754fe60SDimitry Andric ForceLRSpill = false; 10142754fe60SDimitry Andric ExtraCSSpill = true; 10152754fe60SDimitry Andric } 10162754fe60SDimitry Andric 10172754fe60SDimitry Andric if (hasFP(MF)) { 10182754fe60SDimitry Andric MF.getRegInfo().setPhysRegUsed(FramePtr); 10192754fe60SDimitry Andric NumGPRSpills++; 10202754fe60SDimitry Andric } 10212754fe60SDimitry Andric 10222754fe60SDimitry Andric // If stack and double are 8-byte aligned and we are spilling an odd number 10232754fe60SDimitry Andric // of GPRs, spill one extra callee save GPR so we won't have to pad between 10242754fe60SDimitry Andric // the integer and double callee save areas. 10252754fe60SDimitry Andric unsigned TargetAlign = getStackAlignment(); 10262754fe60SDimitry Andric if (TargetAlign == 8 && (NumGPRSpills & 1)) { 10272754fe60SDimitry Andric if (CS1Spilled && !UnspilledCS1GPRs.empty()) { 10282754fe60SDimitry Andric for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) { 10292754fe60SDimitry Andric unsigned Reg = UnspilledCS1GPRs[i]; 10302754fe60SDimitry Andric // Don't spill high register if the function is thumb1 10312754fe60SDimitry Andric if (!AFI->isThumb1OnlyFunction() || 10322754fe60SDimitry Andric isARMLowRegister(Reg) || Reg == ARM::LR) { 10332754fe60SDimitry Andric MF.getRegInfo().setPhysRegUsed(Reg); 10342754fe60SDimitry Andric if (!RegInfo->isReservedReg(MF, Reg)) 10352754fe60SDimitry Andric ExtraCSSpill = true; 10362754fe60SDimitry Andric break; 10372754fe60SDimitry Andric } 10382754fe60SDimitry Andric } 10392754fe60SDimitry Andric } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) { 10402754fe60SDimitry Andric unsigned Reg = UnspilledCS2GPRs.front(); 10412754fe60SDimitry Andric MF.getRegInfo().setPhysRegUsed(Reg); 10422754fe60SDimitry Andric if (!RegInfo->isReservedReg(MF, Reg)) 10432754fe60SDimitry Andric ExtraCSSpill = true; 10442754fe60SDimitry Andric } 10452754fe60SDimitry Andric } 10462754fe60SDimitry Andric 10472754fe60SDimitry Andric // Estimate if we might need to scavenge a register at some point in order 10482754fe60SDimitry Andric // to materialize a stack offset. If so, either spill one additional 10492754fe60SDimitry Andric // callee-saved register or reserve a special spill slot to facilitate 10502754fe60SDimitry Andric // register scavenging. Thumb1 needs a spill slot for stack pointer 10512754fe60SDimitry Andric // adjustments also, even when the frame itself is small. 10522754fe60SDimitry Andric if (BigStack && !ExtraCSSpill) { 10532754fe60SDimitry Andric // If any non-reserved CS register isn't spilled, just spill one or two 10542754fe60SDimitry Andric // extra. That should take care of it! 10552754fe60SDimitry Andric unsigned NumExtras = TargetAlign / 4; 10562754fe60SDimitry Andric SmallVector<unsigned, 2> Extras; 10572754fe60SDimitry Andric while (NumExtras && !UnspilledCS1GPRs.empty()) { 10582754fe60SDimitry Andric unsigned Reg = UnspilledCS1GPRs.back(); 10592754fe60SDimitry Andric UnspilledCS1GPRs.pop_back(); 10602754fe60SDimitry Andric if (!RegInfo->isReservedReg(MF, Reg) && 10612754fe60SDimitry Andric (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) || 10622754fe60SDimitry Andric Reg == ARM::LR)) { 10632754fe60SDimitry Andric Extras.push_back(Reg); 10642754fe60SDimitry Andric NumExtras--; 10652754fe60SDimitry Andric } 10662754fe60SDimitry Andric } 10672754fe60SDimitry Andric // For non-Thumb1 functions, also check for hi-reg CS registers 10682754fe60SDimitry Andric if (!AFI->isThumb1OnlyFunction()) { 10692754fe60SDimitry Andric while (NumExtras && !UnspilledCS2GPRs.empty()) { 10702754fe60SDimitry Andric unsigned Reg = UnspilledCS2GPRs.back(); 10712754fe60SDimitry Andric UnspilledCS2GPRs.pop_back(); 10722754fe60SDimitry Andric if (!RegInfo->isReservedReg(MF, Reg)) { 10732754fe60SDimitry Andric Extras.push_back(Reg); 10742754fe60SDimitry Andric NumExtras--; 10752754fe60SDimitry Andric } 10762754fe60SDimitry Andric } 10772754fe60SDimitry Andric } 10782754fe60SDimitry Andric if (Extras.size() && NumExtras == 0) { 10792754fe60SDimitry Andric for (unsigned i = 0, e = Extras.size(); i != e; ++i) { 10802754fe60SDimitry Andric MF.getRegInfo().setPhysRegUsed(Extras[i]); 10812754fe60SDimitry Andric } 10822754fe60SDimitry Andric } else if (!AFI->isThumb1OnlyFunction()) { 10832754fe60SDimitry Andric // note: Thumb1 functions spill to R12, not the stack. Reserve a slot 10842754fe60SDimitry Andric // closest to SP or frame pointer. 10852754fe60SDimitry Andric const TargetRegisterClass *RC = ARM::GPRRegisterClass; 10862754fe60SDimitry Andric RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(), 10872754fe60SDimitry Andric RC->getAlignment(), 10882754fe60SDimitry Andric false)); 10892754fe60SDimitry Andric } 10902754fe60SDimitry Andric } 10912754fe60SDimitry Andric } 10922754fe60SDimitry Andric 10932754fe60SDimitry Andric if (ForceLRSpill) { 10942754fe60SDimitry Andric MF.getRegInfo().setPhysRegUsed(ARM::LR); 10952754fe60SDimitry Andric AFI->setLRIsSpilledForFarJump(true); 10962754fe60SDimitry Andric } 10972754fe60SDimitry Andric } 1098