ARMFrameLowering.cpp revision 8a8d479214745c82ef00f08d4e4f1c173b5f9ce2
1//=======- ARMFrameLowering.cpp - ARM Frame Information --------*- C++ -*-====//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the ARM implementation of TargetFrameLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ARMFrameLowering.h"
15#include "ARMBaseInstrInfo.h"
16#include "ARMBaseRegisterInfo.h"
17#include "ARMMachineFunctionInfo.h"
18#include "MCTargetDesc/ARMAddressingModes.h"
19#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
23#include "llvm/CodeGen/RegisterScavenging.h"
24#include "llvm/Target/TargetOptions.h"
25
26using namespace llvm;
27
28/// hasFP - Return true if the specified function should have a dedicated frame
29/// pointer register.  This is true if the function has variable sized allocas
30/// or if frame pointer elimination is disabled.
31bool ARMFrameLowering::hasFP(const MachineFunction &MF) const {
32  const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
33
34  // Mac OS X requires FP not to be clobbered for backtracing purpose.
35  if (STI.isTargetDarwin())
36    return true;
37
38  const MachineFrameInfo *MFI = MF.getFrameInfo();
39  // Always eliminate non-leaf frame pointers.
40  return ((MF.getTarget().Options.DisableFramePointerElim(MF) &&
41           MFI->hasCalls()) ||
42          RegInfo->needsStackRealignment(MF) ||
43          MFI->hasVarSizedObjects() ||
44          MFI->isFrameAddressTaken());
45}
46
47/// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
48/// not required, we reserve argument space for call sites in the function
49/// immediately on entry to the current function.  This eliminates the need for
50/// add/sub sp brackets around call sites.  Returns true if the call frame is
51/// included as part of the stack frame.
52bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
53  const MachineFrameInfo *FFI = MF.getFrameInfo();
54  unsigned CFSize = FFI->getMaxCallFrameSize();
55  // It's not always a good idea to include the call frame as part of the
56  // stack frame. ARM (especially Thumb) has small immediate offset to
57  // address the stack frame. So a large call frame can cause poor codegen
58  // and may even makes it impossible to scavenge a register.
59  if (CFSize >= ((1 << 12) - 1) / 2)  // Half of imm12
60    return false;
61
62  return !MF.getFrameInfo()->hasVarSizedObjects();
63}
64
65/// canSimplifyCallFramePseudos - If there is a reserved call frame, the
66/// call frame pseudos can be simplified.  Unlike most targets, having a FP
67/// is not sufficient here since we still may reference some objects via SP
68/// even when FP is available in Thumb2 mode.
69bool
70ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const {
71  return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects();
72}
73
74static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
75  for (unsigned i = 0; CSRegs[i]; ++i)
76    if (Reg == CSRegs[i])
77      return true;
78  return false;
79}
80
81static bool isCSRestore(MachineInstr *MI,
82                        const ARMBaseInstrInfo &TII,
83                        const unsigned *CSRegs) {
84  // Integer spill area is handled with "pop".
85  if (MI->getOpcode() == ARM::LDMIA_RET ||
86      MI->getOpcode() == ARM::t2LDMIA_RET ||
87      MI->getOpcode() == ARM::LDMIA_UPD ||
88      MI->getOpcode() == ARM::t2LDMIA_UPD ||
89      MI->getOpcode() == ARM::VLDMDIA_UPD) {
90    // The first two operands are predicates. The last two are
91    // imp-def and imp-use of SP. Check everything in between.
92    for (int i = 5, e = MI->getNumOperands(); i != e; ++i)
93      if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
94        return false;
95    return true;
96  }
97  if ((MI->getOpcode() == ARM::LDR_POST_IMM ||
98       MI->getOpcode() == ARM::LDR_POST_REG ||
99       MI->getOpcode() == ARM::t2LDR_POST) &&
100      isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs) &&
101      MI->getOperand(1).getReg() == ARM::SP)
102    return true;
103
104  return false;
105}
106
107static void
108emitSPUpdate(bool isARM,
109             MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
110             DebugLoc dl, const ARMBaseInstrInfo &TII,
111             int NumBytes, unsigned MIFlags = MachineInstr::NoFlags) {
112  if (isARM)
113    emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
114                            ARMCC::AL, 0, TII, MIFlags);
115  else
116    emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
117                           ARMCC::AL, 0, TII, MIFlags);
118}
119
120void ARMFrameLowering::emitPrologue(MachineFunction &MF) const {
121  MachineBasicBlock &MBB = MF.front();
122  MachineBasicBlock::iterator MBBI = MBB.begin();
123  MachineFrameInfo  *MFI = MF.getFrameInfo();
124  ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
125  const ARMBaseRegisterInfo *RegInfo =
126    static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
127  const ARMBaseInstrInfo &TII =
128    *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
129  assert(!AFI->isThumb1OnlyFunction() &&
130         "This emitPrologue does not support Thumb1!");
131  bool isARM = !AFI->isThumbFunction();
132  unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
133  unsigned NumBytes = MFI->getStackSize();
134  const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
135  DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
136  unsigned FramePtr = RegInfo->getFrameRegister(MF);
137
138  // Determine the sizes of each callee-save spill areas and record which frame
139  // belongs to which callee-save spill areas.
140  unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
141  int FramePtrSpillFI = 0;
142
143  // Allocate the vararg register save area. This is not counted in NumBytes.
144  if (VARegSaveSize)
145    emitSPUpdate(isARM, MBB, MBBI, dl, TII, -VARegSaveSize,
146                 MachineInstr::FrameSetup);
147
148  if (!AFI->hasStackFrame()) {
149    if (NumBytes != 0)
150      emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
151                   MachineInstr::FrameSetup);
152    return;
153  }
154
155  for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
156    unsigned Reg = CSI[i].getReg();
157    int FI = CSI[i].getFrameIdx();
158    switch (Reg) {
159    case ARM::R4:
160    case ARM::R5:
161    case ARM::R6:
162    case ARM::R7:
163    case ARM::LR:
164      if (Reg == FramePtr)
165        FramePtrSpillFI = FI;
166      AFI->addGPRCalleeSavedArea1Frame(FI);
167      GPRCS1Size += 4;
168      break;
169    case ARM::R8:
170    case ARM::R9:
171    case ARM::R10:
172    case ARM::R11:
173      if (Reg == FramePtr)
174        FramePtrSpillFI = FI;
175      if (STI.isTargetDarwin()) {
176        AFI->addGPRCalleeSavedArea2Frame(FI);
177        GPRCS2Size += 4;
178      } else {
179        AFI->addGPRCalleeSavedArea1Frame(FI);
180        GPRCS1Size += 4;
181      }
182      break;
183    default:
184      AFI->addDPRCalleeSavedAreaFrame(FI);
185      DPRCSSize += 8;
186    }
187  }
188
189  // Move past area 1.
190  if (GPRCS1Size > 0) MBBI++;
191
192  // Set FP to point to the stack slot that contains the previous FP.
193  // For Darwin, FP is R7, which has now been stored in spill area 1.
194  // Otherwise, if this is not Darwin, all the callee-saved registers go
195  // into spill area 1, including the FP in R11.  In either case, it is
196  // now safe to emit this assignment.
197  bool HasFP = hasFP(MF);
198  if (HasFP) {
199    unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : ARM::t2ADDri;
200    MachineInstrBuilder MIB =
201      BuildMI(MBB, MBBI, dl, TII.get(ADDriOpc), FramePtr)
202      .addFrameIndex(FramePtrSpillFI).addImm(0)
203      .setMIFlag(MachineInstr::FrameSetup);
204    AddDefaultCC(AddDefaultPred(MIB));
205  }
206
207  // Move past area 2.
208  if (GPRCS2Size > 0) MBBI++;
209
210  // Determine starting offsets of spill areas.
211  unsigned DPRCSOffset  = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
212  unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
213  unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
214  if (HasFP)
215    AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) +
216                                NumBytes);
217  AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
218  AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
219  AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
220
221  // Move past area 3.
222  if (DPRCSSize > 0) {
223    MBBI++;
224    // Since vpush register list cannot have gaps, there may be multiple vpush
225    // instructions in the prologue.
226    while (MBBI->getOpcode() == ARM::VSTMDDB_UPD)
227      MBBI++;
228  }
229
230  NumBytes = DPRCSOffset;
231  if (NumBytes) {
232    // Adjust SP after all the callee-save spills.
233    emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
234                 MachineInstr::FrameSetup);
235    if (HasFP && isARM)
236      // Restore from fp only in ARM mode: e.g. sub sp, r7, #24
237      // Note it's not safe to do this in Thumb2 mode because it would have
238      // taken two instructions:
239      // mov sp, r7
240      // sub sp, #24
241      // If an interrupt is taken between the two instructions, then sp is in
242      // an inconsistent state (pointing to the middle of callee-saved area).
243      // The interrupt handler can end up clobbering the registers.
244      AFI->setShouldRestoreSPFromFP(true);
245  }
246
247  if (STI.isTargetELF() && hasFP(MF))
248    MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
249                             AFI->getFramePtrSpillOffset());
250
251  AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
252  AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
253  AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
254
255  // If we need dynamic stack realignment, do it here. Be paranoid and make
256  // sure if we also have VLAs, we have a base pointer for frame access.
257  if (RegInfo->needsStackRealignment(MF)) {
258    unsigned MaxAlign = MFI->getMaxAlignment();
259    assert (!AFI->isThumb1OnlyFunction());
260    if (!AFI->isThumbFunction()) {
261      // Emit bic sp, sp, MaxAlign
262      AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
263                                          TII.get(ARM::BICri), ARM::SP)
264                                  .addReg(ARM::SP, RegState::Kill)
265                                  .addImm(MaxAlign-1)));
266    } else {
267      // We cannot use sp as source/dest register here, thus we're emitting the
268      // following sequence:
269      // mov r4, sp
270      // bic r4, r4, MaxAlign
271      // mov sp, r4
272      // FIXME: It will be better just to find spare register here.
273      AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4)
274        .addReg(ARM::SP, RegState::Kill));
275      AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
276                                          TII.get(ARM::t2BICri), ARM::R4)
277                                  .addReg(ARM::R4, RegState::Kill)
278                                  .addImm(MaxAlign-1)));
279      AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
280        .addReg(ARM::R4, RegState::Kill));
281    }
282
283    AFI->setShouldRestoreSPFromFP(true);
284  }
285
286  // If we need a base pointer, set it up here. It's whatever the value
287  // of the stack pointer is at this point. Any variable size objects
288  // will be allocated after this, so we can still use the base pointer
289  // to reference locals.
290  // FIXME: Clarify FrameSetup flags here.
291  if (RegInfo->hasBasePointer(MF)) {
292    if (isARM)
293      BuildMI(MBB, MBBI, dl,
294              TII.get(ARM::MOVr), RegInfo->getBaseRegister())
295        .addReg(ARM::SP)
296        .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
297    else
298      AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
299                             RegInfo->getBaseRegister())
300        .addReg(ARM::SP));
301  }
302
303  // If the frame has variable sized objects then the epilogue must restore
304  // the sp from fp. We can assume there's an FP here since hasFP already
305  // checks for hasVarSizedObjects.
306  if (MFI->hasVarSizedObjects())
307    AFI->setShouldRestoreSPFromFP(true);
308}
309
310void ARMFrameLowering::emitEpilogue(MachineFunction &MF,
311                                    MachineBasicBlock &MBB) const {
312  MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
313  assert(MBBI->getDesc().isReturn() &&
314         "Can only insert epilog into returning blocks");
315  unsigned RetOpcode = MBBI->getOpcode();
316  DebugLoc dl = MBBI->getDebugLoc();
317  MachineFrameInfo *MFI = MF.getFrameInfo();
318  ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
319  const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
320  const ARMBaseInstrInfo &TII =
321    *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
322  assert(!AFI->isThumb1OnlyFunction() &&
323         "This emitEpilogue does not support Thumb1!");
324  bool isARM = !AFI->isThumbFunction();
325
326  unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
327  int NumBytes = (int)MFI->getStackSize();
328  unsigned FramePtr = RegInfo->getFrameRegister(MF);
329
330  if (!AFI->hasStackFrame()) {
331    if (NumBytes != 0)
332      emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
333  } else {
334    // Unwind MBBI to point to first LDR / VLDRD.
335    const unsigned *CSRegs = RegInfo->getCalleeSavedRegs();
336    if (MBBI != MBB.begin()) {
337      do
338        --MBBI;
339      while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs));
340      if (!isCSRestore(MBBI, TII, CSRegs))
341        ++MBBI;
342    }
343
344    // Move SP to start of FP callee save spill area.
345    NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
346                 AFI->getGPRCalleeSavedArea2Size() +
347                 AFI->getDPRCalleeSavedAreaSize());
348
349    // Reset SP based on frame pointer only if the stack frame extends beyond
350    // frame pointer stack slot or target is ELF and the function has FP.
351    if (AFI->shouldRestoreSPFromFP()) {
352      NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
353      if (NumBytes) {
354        if (isARM)
355          emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
356                                  ARMCC::AL, 0, TII);
357        else {
358          // It's not possible to restore SP from FP in a single instruction.
359          // For Darwin, this looks like:
360          // mov sp, r7
361          // sub sp, #24
362          // This is bad, if an interrupt is taken after the mov, sp is in an
363          // inconsistent state.
364          // Use the first callee-saved register as a scratch register.
365          assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) &&
366                 "No scratch register to restore SP from FP!");
367          emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
368                                 ARMCC::AL, 0, TII);
369          AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
370                                 ARM::SP)
371            .addReg(ARM::R4));
372        }
373      } else {
374        // Thumb2 or ARM.
375        if (isARM)
376          BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
377            .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
378        else
379          AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
380                                 ARM::SP)
381            .addReg(FramePtr));
382      }
383    } else if (NumBytes)
384      emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
385
386    // Increment past our save areas.
387    if (AFI->getDPRCalleeSavedAreaSize()) {
388      MBBI++;
389      // Since vpop register list cannot have gaps, there may be multiple vpop
390      // instructions in the epilogue.
391      while (MBBI->getOpcode() == ARM::VLDMDIA_UPD)
392        MBBI++;
393    }
394    if (AFI->getGPRCalleeSavedArea2Size()) MBBI++;
395    if (AFI->getGPRCalleeSavedArea1Size()) MBBI++;
396  }
397
398  if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNdiND ||
399      RetOpcode == ARM::TCRETURNri || RetOpcode == ARM::TCRETURNriND) {
400    // Tail call return: adjust the stack pointer and jump to callee.
401    MBBI = MBB.getLastNonDebugInstr();
402    MachineOperand &JumpTarget = MBBI->getOperand(0);
403
404    // Jump to label or value in register.
405    if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNdiND) {
406      unsigned TCOpcode = (RetOpcode == ARM::TCRETURNdi)
407        ? (STI.isThumb() ? ARM::tTAILJMPd : ARM::TAILJMPd)
408        : (STI.isThumb() ? ARM::tTAILJMPdND : ARM::TAILJMPdND);
409      MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(TCOpcode));
410      if (JumpTarget.isGlobal())
411        MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
412                             JumpTarget.getTargetFlags());
413      else {
414        assert(JumpTarget.isSymbol());
415        MIB.addExternalSymbol(JumpTarget.getSymbolName(),
416                              JumpTarget.getTargetFlags());
417      }
418
419      // Add the default predicate in Thumb mode.
420      if (STI.isThumb()) MIB.addImm(ARMCC::AL).addReg(0);
421    } else if (RetOpcode == ARM::TCRETURNri) {
422      BuildMI(MBB, MBBI, dl,
423              TII.get(STI.isThumb() ? ARM::tTAILJMPr : ARM::TAILJMPr)).
424        addReg(JumpTarget.getReg(), RegState::Kill);
425    } else if (RetOpcode == ARM::TCRETURNriND) {
426      BuildMI(MBB, MBBI, dl,
427              TII.get(STI.isThumb() ? ARM::tTAILJMPrND : ARM::TAILJMPrND)).
428        addReg(JumpTarget.getReg(), RegState::Kill);
429    }
430
431    MachineInstr *NewMI = prior(MBBI);
432    for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i)
433      NewMI->addOperand(MBBI->getOperand(i));
434
435    // Delete the pseudo instruction TCRETURN.
436    MBB.erase(MBBI);
437    MBBI = NewMI;
438  }
439
440  if (VARegSaveSize)
441    emitSPUpdate(isARM, MBB, MBBI, dl, TII, VARegSaveSize);
442}
443
444/// getFrameIndexReference - Provide a base+offset reference to an FI slot for
445/// debug info.  It's the same as what we use for resolving the code-gen
446/// references for now.  FIXME: This can go wrong when references are
447/// SP-relative and simple call frames aren't used.
448int
449ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
450                                         unsigned &FrameReg) const {
451  return ResolveFrameIndexReference(MF, FI, FrameReg, 0);
452}
453
454int
455ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF,
456                                             int FI, unsigned &FrameReg,
457                                             int SPAdj) const {
458  const MachineFrameInfo *MFI = MF.getFrameInfo();
459  const ARMBaseRegisterInfo *RegInfo =
460    static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
461  const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
462  int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize();
463  int FPOffset = Offset - AFI->getFramePtrSpillOffset();
464  bool isFixed = MFI->isFixedObjectIndex(FI);
465
466  FrameReg = ARM::SP;
467  Offset += SPAdj;
468  if (AFI->isGPRCalleeSavedArea1Frame(FI))
469    return Offset - AFI->getGPRCalleeSavedArea1Offset();
470  else if (AFI->isGPRCalleeSavedArea2Frame(FI))
471    return Offset - AFI->getGPRCalleeSavedArea2Offset();
472  else if (AFI->isDPRCalleeSavedAreaFrame(FI))
473    return Offset - AFI->getDPRCalleeSavedAreaOffset();
474
475  // When dynamically realigning the stack, use the frame pointer for
476  // parameters, and the stack/base pointer for locals.
477  if (RegInfo->needsStackRealignment(MF)) {
478    assert (hasFP(MF) && "dynamic stack realignment without a FP!");
479    if (isFixed) {
480      FrameReg = RegInfo->getFrameRegister(MF);
481      Offset = FPOffset;
482    } else if (MFI->hasVarSizedObjects()) {
483      assert(RegInfo->hasBasePointer(MF) &&
484             "VLAs and dynamic stack alignment, but missing base pointer!");
485      FrameReg = RegInfo->getBaseRegister();
486    }
487    return Offset;
488  }
489
490  // If there is a frame pointer, use it when we can.
491  if (hasFP(MF) && AFI->hasStackFrame()) {
492    // Use frame pointer to reference fixed objects. Use it for locals if
493    // there are VLAs (and thus the SP isn't reliable as a base).
494    if (isFixed || (MFI->hasVarSizedObjects() &&
495                    !RegInfo->hasBasePointer(MF))) {
496      FrameReg = RegInfo->getFrameRegister(MF);
497      return FPOffset;
498    } else if (MFI->hasVarSizedObjects()) {
499      assert(RegInfo->hasBasePointer(MF) && "missing base pointer!");
500      if (AFI->isThumb2Function()) {
501        // Try to use the frame pointer if we can, else use the base pointer
502        // since it's available. This is handy for the emergency spill slot, in
503        // particular.
504        if (FPOffset >= -255 && FPOffset < 0) {
505          FrameReg = RegInfo->getFrameRegister(MF);
506          return FPOffset;
507        }
508      }
509    } else if (AFI->isThumb2Function()) {
510      // Use  add <rd>, sp, #<imm8>
511      //      ldr <rd>, [sp, #<imm8>]
512      // if at all possible to save space.
513      if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020)
514        return Offset;
515      // In Thumb2 mode, the negative offset is very limited. Try to avoid
516      // out of range references. ldr <rt>,[<rn>, #-<imm8>]
517      if (FPOffset >= -255 && FPOffset < 0) {
518        FrameReg = RegInfo->getFrameRegister(MF);
519        return FPOffset;
520      }
521    } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) {
522      // Otherwise, use SP or FP, whichever is closer to the stack slot.
523      FrameReg = RegInfo->getFrameRegister(MF);
524      return FPOffset;
525    }
526  }
527  // Use the base pointer if we have one.
528  if (RegInfo->hasBasePointer(MF))
529    FrameReg = RegInfo->getBaseRegister();
530  return Offset;
531}
532
533int ARMFrameLowering::getFrameIndexOffset(const MachineFunction &MF,
534                                          int FI) const {
535  unsigned FrameReg;
536  return getFrameIndexReference(MF, FI, FrameReg);
537}
538
539void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB,
540                                    MachineBasicBlock::iterator MI,
541                                    const std::vector<CalleeSavedInfo> &CSI,
542                                    unsigned StmOpc, unsigned StrOpc,
543                                    bool NoGap,
544                                    bool(*Func)(unsigned, bool),
545                                    unsigned MIFlags) const {
546  MachineFunction &MF = *MBB.getParent();
547  const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
548
549  DebugLoc DL;
550  if (MI != MBB.end()) DL = MI->getDebugLoc();
551
552  SmallVector<std::pair<unsigned,bool>, 4> Regs;
553  unsigned i = CSI.size();
554  while (i != 0) {
555    unsigned LastReg = 0;
556    for (; i != 0; --i) {
557      unsigned Reg = CSI[i-1].getReg();
558      if (!(Func)(Reg, STI.isTargetDarwin())) continue;
559
560      // Add the callee-saved register as live-in unless it's LR and
561      // @llvm.returnaddress is called. If LR is returned for
562      // @llvm.returnaddress then it's already added to the function and
563      // entry block live-in sets.
564      bool isKill = true;
565      if (Reg == ARM::LR) {
566        if (MF.getFrameInfo()->isReturnAddressTaken() &&
567            MF.getRegInfo().isLiveIn(Reg))
568          isKill = false;
569      }
570
571      if (isKill)
572        MBB.addLiveIn(Reg);
573
574      // If NoGap is true, push consecutive registers and then leave the rest
575      // for other instructions. e.g.
576      // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11}
577      if (NoGap && LastReg && LastReg != Reg-1)
578        break;
579      LastReg = Reg;
580      Regs.push_back(std::make_pair(Reg, isKill));
581    }
582
583    if (Regs.empty())
584      continue;
585    if (Regs.size() > 1 || StrOpc== 0) {
586      MachineInstrBuilder MIB =
587        AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP)
588                       .addReg(ARM::SP).setMIFlags(MIFlags));
589      for (unsigned i = 0, e = Regs.size(); i < e; ++i)
590        MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second));
591    } else if (Regs.size() == 1) {
592      MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc),
593                                        ARM::SP)
594        .addReg(Regs[0].first, getKillRegState(Regs[0].second))
595        .addReg(ARM::SP).setMIFlags(MIFlags)
596        .addImm(-4);
597      AddDefaultPred(MIB);
598    }
599    Regs.clear();
600  }
601}
602
603void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB,
604                                   MachineBasicBlock::iterator MI,
605                                   const std::vector<CalleeSavedInfo> &CSI,
606                                   unsigned LdmOpc, unsigned LdrOpc,
607                                   bool isVarArg, bool NoGap,
608                                   bool(*Func)(unsigned, bool)) const {
609  MachineFunction &MF = *MBB.getParent();
610  const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
611  ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
612  DebugLoc DL = MI->getDebugLoc();
613  unsigned RetOpcode = MI->getOpcode();
614  bool isTailCall = (RetOpcode == ARM::TCRETURNdi ||
615                     RetOpcode == ARM::TCRETURNdiND ||
616                     RetOpcode == ARM::TCRETURNri ||
617                     RetOpcode == ARM::TCRETURNriND);
618
619  SmallVector<unsigned, 4> Regs;
620  unsigned i = CSI.size();
621  while (i != 0) {
622    unsigned LastReg = 0;
623    bool DeleteRet = false;
624    for (; i != 0; --i) {
625      unsigned Reg = CSI[i-1].getReg();
626      if (!(Func)(Reg, STI.isTargetDarwin())) continue;
627
628      if (Reg == ARM::LR && !isTailCall && !isVarArg && STI.hasV5TOps()) {
629        Reg = ARM::PC;
630        LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET;
631        // Fold the return instruction into the LDM.
632        DeleteRet = true;
633      }
634
635      // If NoGap is true, pop consecutive registers and then leave the rest
636      // for other instructions. e.g.
637      // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11}
638      if (NoGap && LastReg && LastReg != Reg-1)
639        break;
640
641      LastReg = Reg;
642      Regs.push_back(Reg);
643    }
644
645    if (Regs.empty())
646      continue;
647    if (Regs.size() > 1 || LdrOpc == 0) {
648      MachineInstrBuilder MIB =
649        AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP)
650                       .addReg(ARM::SP));
651      for (unsigned i = 0, e = Regs.size(); i < e; ++i)
652        MIB.addReg(Regs[i], getDefRegState(true));
653      if (DeleteRet) {
654        MIB->copyImplicitOps(&*MI);
655        MI->eraseFromParent();
656      }
657      MI = MIB;
658    } else if (Regs.size() == 1) {
659      // If we adjusted the reg to PC from LR above, switch it back here. We
660      // only do that for LDM.
661      if (Regs[0] == ARM::PC)
662        Regs[0] = ARM::LR;
663      MachineInstrBuilder MIB =
664        BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0])
665          .addReg(ARM::SP, RegState::Define)
666          .addReg(ARM::SP);
667      // ARM mode needs an extra reg0 here due to addrmode2. Will go away once
668      // that refactoring is complete (eventually).
669      if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) {
670        MIB.addReg(0);
671        MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift));
672      } else
673        MIB.addImm(4);
674      AddDefaultPred(MIB);
675    }
676    Regs.clear();
677  }
678}
679
680bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
681                                        MachineBasicBlock::iterator MI,
682                                        const std::vector<CalleeSavedInfo> &CSI,
683                                        const TargetRegisterInfo *TRI) const {
684  if (CSI.empty())
685    return false;
686
687  MachineFunction &MF = *MBB.getParent();
688  ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
689
690  unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD;
691  unsigned PushOneOpc = AFI->isThumbFunction() ?
692    ARM::t2STR_PRE : ARM::STR_PRE_IMM;
693  unsigned FltOpc = ARM::VSTMDDB_UPD;
694  emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register,
695               MachineInstr::FrameSetup);
696  emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register,
697               MachineInstr::FrameSetup);
698  emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register,
699               MachineInstr::FrameSetup);
700
701  return true;
702}
703
704bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
705                                        MachineBasicBlock::iterator MI,
706                                        const std::vector<CalleeSavedInfo> &CSI,
707                                        const TargetRegisterInfo *TRI) const {
708  if (CSI.empty())
709    return false;
710
711  MachineFunction &MF = *MBB.getParent();
712  ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
713  bool isVarArg = AFI->getVarArgsRegSaveSize() > 0;
714
715  unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
716  unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM;
717  unsigned FltOpc = ARM::VLDMDIA_UPD;
718  emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register);
719  emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
720              &isARMArea2Register);
721  emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
722              &isARMArea1Register);
723
724  return true;
725}
726
727// FIXME: Make generic?
728static unsigned GetFunctionSizeInBytes(const MachineFunction &MF,
729                                       const ARMBaseInstrInfo &TII) {
730  unsigned FnSize = 0;
731  for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end();
732       MBBI != E; ++MBBI) {
733    const MachineBasicBlock &MBB = *MBBI;
734    for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end();
735         I != E; ++I)
736      FnSize += TII.GetInstSizeInBytes(I);
737  }
738  return FnSize;
739}
740
741/// estimateStackSize - Estimate and return the size of the frame.
742/// FIXME: Make generic?
743static unsigned estimateStackSize(MachineFunction &MF) {
744  const MachineFrameInfo *MFI = MF.getFrameInfo();
745  const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
746  const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
747  unsigned MaxAlign = MFI->getMaxAlignment();
748  int Offset = 0;
749
750  // This code is very, very similar to PEI::calculateFrameObjectOffsets().
751  // It really should be refactored to share code. Until then, changes
752  // should keep in mind that there's tight coupling between the two.
753
754  for (int i = MFI->getObjectIndexBegin(); i != 0; ++i) {
755    int FixedOff = -MFI->getObjectOffset(i);
756    if (FixedOff > Offset) Offset = FixedOff;
757  }
758  for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
759    if (MFI->isDeadObjectIndex(i))
760      continue;
761    Offset += MFI->getObjectSize(i);
762    unsigned Align = MFI->getObjectAlignment(i);
763    // Adjust to alignment boundary
764    Offset = (Offset+Align-1)/Align*Align;
765
766    MaxAlign = std::max(Align, MaxAlign);
767  }
768
769  if (MFI->adjustsStack() && TFI->hasReservedCallFrame(MF))
770    Offset += MFI->getMaxCallFrameSize();
771
772  // Round up the size to a multiple of the alignment.  If the function has
773  // any calls or alloca's, align to the target's StackAlignment value to
774  // ensure that the callee's frame or the alloca data is suitably aligned;
775  // otherwise, for leaf functions, align to the TransientStackAlignment
776  // value.
777  unsigned StackAlign;
778  if (MFI->adjustsStack() || MFI->hasVarSizedObjects() ||
779      (RegInfo->needsStackRealignment(MF) && MFI->getObjectIndexEnd() != 0))
780    StackAlign = TFI->getStackAlignment();
781  else
782    StackAlign = TFI->getTransientStackAlignment();
783
784  // If the frame pointer is eliminated, all frame offsets will be relative to
785  // SP not FP. Align to MaxAlign so this works.
786  StackAlign = std::max(StackAlign, MaxAlign);
787  unsigned AlignMask = StackAlign - 1;
788  Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
789
790  return (unsigned)Offset;
791}
792
793/// estimateRSStackSizeLimit - Look at each instruction that references stack
794/// frames and return the stack size limit beyond which some of these
795/// instructions will require a scratch register during their expansion later.
796// FIXME: Move to TII?
797static unsigned estimateRSStackSizeLimit(MachineFunction &MF,
798                                         const TargetFrameLowering *TFI) {
799  const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
800  unsigned Limit = (1 << 12) - 1;
801  for (MachineFunction::iterator BB = MF.begin(),E = MF.end(); BB != E; ++BB) {
802    for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
803         I != E; ++I) {
804      for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
805        if (!I->getOperand(i).isFI()) continue;
806
807        // When using ADDri to get the address of a stack object, 255 is the
808        // largest offset guaranteed to fit in the immediate offset.
809        if (I->getOpcode() == ARM::ADDri) {
810          Limit = std::min(Limit, (1U << 8) - 1);
811          break;
812        }
813
814        // Otherwise check the addressing mode.
815        switch (I->getDesc().TSFlags & ARMII::AddrModeMask) {
816        case ARMII::AddrMode3:
817        case ARMII::AddrModeT2_i8:
818          Limit = std::min(Limit, (1U << 8) - 1);
819          break;
820        case ARMII::AddrMode5:
821        case ARMII::AddrModeT2_i8s4:
822          Limit = std::min(Limit, ((1U << 8) - 1) * 4);
823          break;
824        case ARMII::AddrModeT2_i12:
825          // i12 supports only positive offset so these will be converted to
826          // i8 opcodes. See llvm::rewriteT2FrameIndex.
827          if (TFI->hasFP(MF) && AFI->hasStackFrame())
828            Limit = std::min(Limit, (1U << 8) - 1);
829          break;
830        case ARMII::AddrMode4:
831        case ARMII::AddrMode6:
832          // Addressing modes 4 & 6 (load/store) instructions can't encode an
833          // immediate offset for stack references.
834          return 0;
835        default:
836          break;
837        }
838        break; // At most one FI per instruction
839      }
840    }
841  }
842
843  return Limit;
844}
845
846void
847ARMFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
848                                                       RegScavenger *RS) const {
849  // This tells PEI to spill the FP as if it is any other callee-save register
850  // to take advantage the eliminateFrameIndex machinery. This also ensures it
851  // is spilled in the order specified by getCalleeSavedRegs() to make it easier
852  // to combine multiple loads / stores.
853  bool CanEliminateFrame = true;
854  bool CS1Spilled = false;
855  bool LRSpilled = false;
856  unsigned NumGPRSpills = 0;
857  SmallVector<unsigned, 4> UnspilledCS1GPRs;
858  SmallVector<unsigned, 4> UnspilledCS2GPRs;
859  const ARMBaseRegisterInfo *RegInfo =
860    static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
861  const ARMBaseInstrInfo &TII =
862    *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
863  ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
864  MachineFrameInfo *MFI = MF.getFrameInfo();
865  unsigned FramePtr = RegInfo->getFrameRegister(MF);
866
867  // Spill R4 if Thumb2 function requires stack realignment - it will be used as
868  // scratch register. Also spill R4 if Thumb2 function has varsized objects,
869  // since it's not always possible to restore sp from fp in a single
870  // instruction.
871  // FIXME: It will be better just to find spare register here.
872  if (AFI->isThumb2Function() &&
873      (MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(MF)))
874    MF.getRegInfo().setPhysRegUsed(ARM::R4);
875
876  if (AFI->isThumb1OnlyFunction()) {
877    // Spill LR if Thumb1 function uses variable length argument lists.
878    if (AFI->getVarArgsRegSaveSize() > 0)
879      MF.getRegInfo().setPhysRegUsed(ARM::LR);
880
881    // Spill R4 if Thumb1 epilogue has to restore SP from FP. We don't know
882    // for sure what the stack size will be, but for this, an estimate is good
883    // enough. If there anything changes it, it'll be a spill, which implies
884    // we've used all the registers and so R4 is already used, so not marking
885    // it here will be OK.
886    // FIXME: It will be better just to find spare register here.
887    unsigned StackSize = estimateStackSize(MF);
888    if (MFI->hasVarSizedObjects() || StackSize > 508)
889      MF.getRegInfo().setPhysRegUsed(ARM::R4);
890  }
891
892  // Spill the BasePtr if it's used.
893  if (RegInfo->hasBasePointer(MF))
894    MF.getRegInfo().setPhysRegUsed(RegInfo->getBaseRegister());
895
896  // Don't spill FP if the frame can be eliminated. This is determined
897  // by scanning the callee-save registers to see if any is used.
898  const unsigned *CSRegs = RegInfo->getCalleeSavedRegs();
899  for (unsigned i = 0; CSRegs[i]; ++i) {
900    unsigned Reg = CSRegs[i];
901    bool Spilled = false;
902    if (MF.getRegInfo().isPhysRegUsed(Reg)) {
903      Spilled = true;
904      CanEliminateFrame = false;
905    } else {
906      // Check alias registers too.
907      for (const unsigned *Aliases =
908             RegInfo->getAliasSet(Reg); *Aliases; ++Aliases) {
909        if (MF.getRegInfo().isPhysRegUsed(*Aliases)) {
910          Spilled = true;
911          CanEliminateFrame = false;
912        }
913      }
914    }
915
916    if (!ARM::GPRRegisterClass->contains(Reg))
917      continue;
918
919    if (Spilled) {
920      NumGPRSpills++;
921
922      if (!STI.isTargetDarwin()) {
923        if (Reg == ARM::LR)
924          LRSpilled = true;
925        CS1Spilled = true;
926        continue;
927      }
928
929      // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
930      switch (Reg) {
931      case ARM::LR:
932        LRSpilled = true;
933        // Fallthrough
934      case ARM::R4: case ARM::R5:
935      case ARM::R6: case ARM::R7:
936        CS1Spilled = true;
937        break;
938      default:
939        break;
940      }
941    } else {
942      if (!STI.isTargetDarwin()) {
943        UnspilledCS1GPRs.push_back(Reg);
944        continue;
945      }
946
947      switch (Reg) {
948      case ARM::R4: case ARM::R5:
949      case ARM::R6: case ARM::R7:
950      case ARM::LR:
951        UnspilledCS1GPRs.push_back(Reg);
952        break;
953      default:
954        UnspilledCS2GPRs.push_back(Reg);
955        break;
956      }
957    }
958  }
959
960  bool ForceLRSpill = false;
961  if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
962    unsigned FnSize = GetFunctionSizeInBytes(MF, TII);
963    // Force LR to be spilled if the Thumb function size is > 2048. This enables
964    // use of BL to implement far jump. If it turns out that it's not needed
965    // then the branch fix up path will undo it.
966    if (FnSize >= (1 << 11)) {
967      CanEliminateFrame = false;
968      ForceLRSpill = true;
969    }
970  }
971
972  // If any of the stack slot references may be out of range of an immediate
973  // offset, make sure a register (or a spill slot) is available for the
974  // register scavenger. Note that if we're indexing off the frame pointer, the
975  // effective stack size is 4 bytes larger since the FP points to the stack
976  // slot of the previous FP. Also, if we have variable sized objects in the
977  // function, stack slot references will often be negative, and some of
978  // our instructions are positive-offset only, so conservatively consider
979  // that case to want a spill slot (or register) as well. Similarly, if
980  // the function adjusts the stack pointer during execution and the
981  // adjustments aren't already part of our stack size estimate, our offset
982  // calculations may be off, so be conservative.
983  // FIXME: We could add logic to be more precise about negative offsets
984  //        and which instructions will need a scratch register for them. Is it
985  //        worth the effort and added fragility?
986  bool BigStack =
987    (RS &&
988     (estimateStackSize(MF) + ((hasFP(MF) && AFI->hasStackFrame()) ? 4:0) >=
989      estimateRSStackSizeLimit(MF, this)))
990    || MFI->hasVarSizedObjects()
991    || (MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF));
992
993  bool ExtraCSSpill = false;
994  if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) {
995    AFI->setHasStackFrame(true);
996
997    // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
998    // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
999    if (!LRSpilled && CS1Spilled) {
1000      MF.getRegInfo().setPhysRegUsed(ARM::LR);
1001      NumGPRSpills++;
1002      UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(),
1003                                    UnspilledCS1GPRs.end(), (unsigned)ARM::LR));
1004      ForceLRSpill = false;
1005      ExtraCSSpill = true;
1006    }
1007
1008    if (hasFP(MF)) {
1009      MF.getRegInfo().setPhysRegUsed(FramePtr);
1010      NumGPRSpills++;
1011    }
1012
1013    // If stack and double are 8-byte aligned and we are spilling an odd number
1014    // of GPRs, spill one extra callee save GPR so we won't have to pad between
1015    // the integer and double callee save areas.
1016    unsigned TargetAlign = getStackAlignment();
1017    if (TargetAlign == 8 && (NumGPRSpills & 1)) {
1018      if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
1019        for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
1020          unsigned Reg = UnspilledCS1GPRs[i];
1021          // Don't spill high register if the function is thumb1
1022          if (!AFI->isThumb1OnlyFunction() ||
1023              isARMLowRegister(Reg) || Reg == ARM::LR) {
1024            MF.getRegInfo().setPhysRegUsed(Reg);
1025            if (!RegInfo->isReservedReg(MF, Reg))
1026              ExtraCSSpill = true;
1027            break;
1028          }
1029        }
1030      } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) {
1031        unsigned Reg = UnspilledCS2GPRs.front();
1032        MF.getRegInfo().setPhysRegUsed(Reg);
1033        if (!RegInfo->isReservedReg(MF, Reg))
1034          ExtraCSSpill = true;
1035      }
1036    }
1037
1038    // Estimate if we might need to scavenge a register at some point in order
1039    // to materialize a stack offset. If so, either spill one additional
1040    // callee-saved register or reserve a special spill slot to facilitate
1041    // register scavenging. Thumb1 needs a spill slot for stack pointer
1042    // adjustments also, even when the frame itself is small.
1043    if (BigStack && !ExtraCSSpill) {
1044      // If any non-reserved CS register isn't spilled, just spill one or two
1045      // extra. That should take care of it!
1046      unsigned NumExtras = TargetAlign / 4;
1047      SmallVector<unsigned, 2> Extras;
1048      while (NumExtras && !UnspilledCS1GPRs.empty()) {
1049        unsigned Reg = UnspilledCS1GPRs.back();
1050        UnspilledCS1GPRs.pop_back();
1051        if (!RegInfo->isReservedReg(MF, Reg) &&
1052            (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) ||
1053             Reg == ARM::LR)) {
1054          Extras.push_back(Reg);
1055          NumExtras--;
1056        }
1057      }
1058      // For non-Thumb1 functions, also check for hi-reg CS registers
1059      if (!AFI->isThumb1OnlyFunction()) {
1060        while (NumExtras && !UnspilledCS2GPRs.empty()) {
1061          unsigned Reg = UnspilledCS2GPRs.back();
1062          UnspilledCS2GPRs.pop_back();
1063          if (!RegInfo->isReservedReg(MF, Reg)) {
1064            Extras.push_back(Reg);
1065            NumExtras--;
1066          }
1067        }
1068      }
1069      if (Extras.size() && NumExtras == 0) {
1070        for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
1071          MF.getRegInfo().setPhysRegUsed(Extras[i]);
1072        }
1073      } else if (!AFI->isThumb1OnlyFunction()) {
1074        // note: Thumb1 functions spill to R12, not the stack.  Reserve a slot
1075        // closest to SP or frame pointer.
1076        const TargetRegisterClass *RC = ARM::GPRRegisterClass;
1077        RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
1078                                                           RC->getAlignment(),
1079                                                           false));
1080      }
1081    }
1082  }
1083
1084  if (ForceLRSpill) {
1085    MF.getRegInfo().setPhysRegUsed(ARM::LR);
1086    AFI->setLRIsSpilledForFarJump(true);
1087  }
1088}
1089