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