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