1 //===- ARMBaseRegisterInfo.cpp - ARM Register Information -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the base ARM implementation of TargetRegisterInfo class.
12 //===----------------------------------------------------------------------===//
15 #include "ARMAddressingModes.h"
16 #include "ARMBaseInstrInfo.h"
17 #include "ARMBaseRegisterInfo.h"
18 #include "ARMInstrInfo.h"
19 #include "ARMMachineFunctionInfo.h"
20 #include "ARMSubtarget.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Function.h"
24 #include "llvm/LLVMContext.h"
25 #include "llvm/CodeGen/MachineConstantPool.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineInstrBuilder.h"
29 #include "llvm/CodeGen/MachineLocation.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/CodeGen/RegisterScavenging.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Target/TargetFrameInfo.h"
36 #include "llvm/Target/TargetMachine.h"
37 #include "llvm/Target/TargetOptions.h"
38 #include "llvm/ADT/BitVector.h"
39 #include "llvm/ADT/SmallVector.h"
40 #include "llvm/Support/CommandLine.h"
44 ReuseFrameIndexVals("arm-reuse-frame-index-vals", cl::Hidden, cl::init(true),
45 cl::desc("Reuse repeated frame index values"));
48 ARMDynamicStackAlign("arm-dynamic-stack-alignment", cl::Hidden, cl::init(true),
49 cl::desc("Dynamically re-align the stack as needed"));
51 unsigned ARMBaseRegisterInfo::getRegisterNumbering(unsigned RegEnum,
59 llvm_unreachable("Unknown ARM register!");
60 case R0: case D0: case Q0: return 0;
61 case R1: case D1: case Q1: return 1;
62 case R2: case D2: case Q2: return 2;
63 case R3: case D3: case Q3: return 3;
64 case R4: case D4: case Q4: return 4;
65 case R5: case D5: case Q5: return 5;
66 case R6: case D6: case Q6: return 6;
67 case R7: case D7: case Q7: return 7;
68 case R8: case D8: case Q8: return 8;
69 case R9: case D9: case Q9: return 9;
70 case R10: case D10: case Q10: return 10;
71 case R11: case D11: case Q11: return 11;
72 case R12: case D12: case Q12: return 12;
73 case SP: case D13: case Q13: return 13;
74 case LR: case D14: case Q14: return 14;
75 case PC: case D15: case Q15: return 15;
94 case S0: case S1: case S2: case S3:
95 case S4: case S5: case S6: case S7:
96 case S8: case S9: case S10: case S11:
97 case S12: case S13: case S14: case S15:
98 case S16: case S17: case S18: case S19:
99 case S20: case S21: case S22: case S23:
100 case S24: case S25: case S26: case S27:
101 case S28: case S29: case S30: case S31: {
105 default: return 0; // Avoid compile time warning.
143 ARMBaseRegisterInfo::ARMBaseRegisterInfo(const ARMBaseInstrInfo &tii,
144 const ARMSubtarget &sti)
145 : ARMGenRegisterInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
147 FramePtr((STI.isTargetDarwin() || STI.isThumb()) ? ARM::R7 : ARM::R11) {
151 ARMBaseRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
152 static const unsigned CalleeSavedRegs[] = {
153 ARM::LR, ARM::R11, ARM::R10, ARM::R9, ARM::R8,
154 ARM::R7, ARM::R6, ARM::R5, ARM::R4,
156 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
157 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
161 static const unsigned DarwinCalleeSavedRegs[] = {
162 // Darwin ABI deviates from ARM standard ABI. R9 is not a callee-saved
164 ARM::LR, ARM::R7, ARM::R6, ARM::R5, ARM::R4,
165 ARM::R11, ARM::R10, ARM::R8,
167 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
168 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
171 return STI.isTargetDarwin() ? DarwinCalleeSavedRegs : CalleeSavedRegs;
174 const TargetRegisterClass* const *
175 ARMBaseRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
176 static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
177 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
178 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
179 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
181 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
182 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
186 static const TargetRegisterClass * const ThumbCalleeSavedRegClasses[] = {
187 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
188 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::tGPRRegClass,
189 &ARM::tGPRRegClass,&ARM::tGPRRegClass,&ARM::tGPRRegClass,
191 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
192 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
196 static const TargetRegisterClass * const DarwinCalleeSavedRegClasses[] = {
197 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
198 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
199 &ARM::GPRRegClass, &ARM::GPRRegClass,
201 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
202 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
206 static const TargetRegisterClass * const DarwinThumbCalleeSavedRegClasses[] ={
207 &ARM::GPRRegClass, &ARM::tGPRRegClass, &ARM::tGPRRegClass,
208 &ARM::tGPRRegClass, &ARM::tGPRRegClass, &ARM::GPRRegClass,
209 &ARM::GPRRegClass, &ARM::GPRRegClass,
211 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
212 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
216 if (STI.isThumb1Only()) {
217 return STI.isTargetDarwin()
218 ? DarwinThumbCalleeSavedRegClasses : ThumbCalleeSavedRegClasses;
220 return STI.isTargetDarwin()
221 ? DarwinCalleeSavedRegClasses : CalleeSavedRegClasses;
224 BitVector ARMBaseRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
225 // FIXME: avoid re-calculating this everytime.
226 BitVector Reserved(getNumRegs());
227 Reserved.set(ARM::SP);
228 Reserved.set(ARM::PC);
229 if (STI.isTargetDarwin() || hasFP(MF))
230 Reserved.set(FramePtr);
231 // Some targets reserve R9.
232 if (STI.isR9Reserved())
233 Reserved.set(ARM::R9);
237 bool ARMBaseRegisterInfo::isReservedReg(const MachineFunction &MF,
238 unsigned Reg) const {
246 if (FramePtr == Reg && (STI.isTargetDarwin() || hasFP(MF)))
250 return STI.isR9Reserved();
256 const TargetRegisterClass *
257 ARMBaseRegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A,
258 const TargetRegisterClass *B,
259 unsigned SubIdx) const {
267 if (A->getSize() == 8) {
268 if (B == &ARM::SPR_8RegClass)
269 return &ARM::DPR_8RegClass;
270 assert(B == &ARM::SPRRegClass && "Expecting SPR register class!");
271 if (A == &ARM::DPR_8RegClass)
273 return &ARM::DPR_VFP2RegClass;
276 assert(A->getSize() == 16 && "Expecting a Q register class!");
277 if (B == &ARM::SPR_8RegClass)
278 return &ARM::QPR_8RegClass;
279 return &ARM::QPR_VFP2RegClass;
283 if (B == &ARM::DPR_VFP2RegClass)
284 return &ARM::QPR_VFP2RegClass;
285 if (B == &ARM::DPR_8RegClass)
286 return &ARM::QPR_8RegClass;
292 const TargetRegisterClass *
293 ARMBaseRegisterInfo::getPointerRegClass(unsigned Kind) const {
294 return ARM::GPRRegisterClass;
297 /// getAllocationOrder - Returns the register allocation order for a specified
298 /// register class in the form of a pair of TargetRegisterClass iterators.
299 std::pair<TargetRegisterClass::iterator,TargetRegisterClass::iterator>
300 ARMBaseRegisterInfo::getAllocationOrder(const TargetRegisterClass *RC,
301 unsigned HintType, unsigned HintReg,
302 const MachineFunction &MF) const {
303 // Alternative register allocation orders when favoring even / odd registers
304 // of register pairs.
306 // No FP, R9 is available.
307 static const unsigned GPREven1[] = {
308 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8, ARM::R10,
309 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7,
312 static const unsigned GPROdd1[] = {
313 ARM::R1, ARM::R3, ARM::R5, ARM::R7, ARM::R9, ARM::R11,
314 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6,
318 // FP is R7, R9 is available.
319 static const unsigned GPREven2[] = {
320 ARM::R0, ARM::R2, ARM::R4, ARM::R8, ARM::R10,
321 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6,
324 static const unsigned GPROdd2[] = {
325 ARM::R1, ARM::R3, ARM::R5, ARM::R9, ARM::R11,
326 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6,
330 // FP is R11, R9 is available.
331 static const unsigned GPREven3[] = {
332 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8,
333 ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7,
336 static const unsigned GPROdd3[] = {
337 ARM::R1, ARM::R3, ARM::R5, ARM::R6, ARM::R9,
338 ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R7,
342 // No FP, R9 is not available.
343 static const unsigned GPREven4[] = {
344 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R10,
345 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8,
348 static const unsigned GPROdd4[] = {
349 ARM::R1, ARM::R3, ARM::R5, ARM::R7, ARM::R11,
350 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8,
354 // FP is R7, R9 is not available.
355 static const unsigned GPREven5[] = {
356 ARM::R0, ARM::R2, ARM::R4, ARM::R10,
357 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6, ARM::R8,
360 static const unsigned GPROdd5[] = {
361 ARM::R1, ARM::R3, ARM::R5, ARM::R11,
362 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8,
366 // FP is R11, R9 is not available.
367 static const unsigned GPREven6[] = {
368 ARM::R0, ARM::R2, ARM::R4, ARM::R6,
369 ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8
371 static const unsigned GPROdd6[] = {
372 ARM::R1, ARM::R3, ARM::R5, ARM::R7,
373 ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8
377 if (HintType == ARMRI::RegPairEven) {
378 if (isPhysicalRegister(HintReg) && getRegisterPairEven(HintReg, MF) == 0)
379 // It's no longer possible to fulfill this hint. Return the default
381 return std::make_pair(RC->allocation_order_begin(MF),
382 RC->allocation_order_end(MF));
384 if (!STI.isTargetDarwin() && !hasFP(MF)) {
385 if (!STI.isR9Reserved())
386 return std::make_pair(GPREven1,
387 GPREven1 + (sizeof(GPREven1)/sizeof(unsigned)));
389 return std::make_pair(GPREven4,
390 GPREven4 + (sizeof(GPREven4)/sizeof(unsigned)));
391 } else if (FramePtr == ARM::R7) {
392 if (!STI.isR9Reserved())
393 return std::make_pair(GPREven2,
394 GPREven2 + (sizeof(GPREven2)/sizeof(unsigned)));
396 return std::make_pair(GPREven5,
397 GPREven5 + (sizeof(GPREven5)/sizeof(unsigned)));
398 } else { // FramePtr == ARM::R11
399 if (!STI.isR9Reserved())
400 return std::make_pair(GPREven3,
401 GPREven3 + (sizeof(GPREven3)/sizeof(unsigned)));
403 return std::make_pair(GPREven6,
404 GPREven6 + (sizeof(GPREven6)/sizeof(unsigned)));
406 } else if (HintType == ARMRI::RegPairOdd) {
407 if (isPhysicalRegister(HintReg) && getRegisterPairOdd(HintReg, MF) == 0)
408 // It's no longer possible to fulfill this hint. Return the default
410 return std::make_pair(RC->allocation_order_begin(MF),
411 RC->allocation_order_end(MF));
413 if (!STI.isTargetDarwin() && !hasFP(MF)) {
414 if (!STI.isR9Reserved())
415 return std::make_pair(GPROdd1,
416 GPROdd1 + (sizeof(GPROdd1)/sizeof(unsigned)));
418 return std::make_pair(GPROdd4,
419 GPROdd4 + (sizeof(GPROdd4)/sizeof(unsigned)));
420 } else if (FramePtr == ARM::R7) {
421 if (!STI.isR9Reserved())
422 return std::make_pair(GPROdd2,
423 GPROdd2 + (sizeof(GPROdd2)/sizeof(unsigned)));
425 return std::make_pair(GPROdd5,
426 GPROdd5 + (sizeof(GPROdd5)/sizeof(unsigned)));
427 } else { // FramePtr == ARM::R11
428 if (!STI.isR9Reserved())
429 return std::make_pair(GPROdd3,
430 GPROdd3 + (sizeof(GPROdd3)/sizeof(unsigned)));
432 return std::make_pair(GPROdd6,
433 GPROdd6 + (sizeof(GPROdd6)/sizeof(unsigned)));
436 return std::make_pair(RC->allocation_order_begin(MF),
437 RC->allocation_order_end(MF));
440 /// ResolveRegAllocHint - Resolves the specified register allocation hint
441 /// to a physical register. Returns the physical register if it is successful.
443 ARMBaseRegisterInfo::ResolveRegAllocHint(unsigned Type, unsigned Reg,
444 const MachineFunction &MF) const {
445 if (Reg == 0 || !isPhysicalRegister(Reg))
449 else if (Type == (unsigned)ARMRI::RegPairOdd)
451 return getRegisterPairOdd(Reg, MF);
452 else if (Type == (unsigned)ARMRI::RegPairEven)
454 return getRegisterPairEven(Reg, MF);
459 ARMBaseRegisterInfo::UpdateRegAllocHint(unsigned Reg, unsigned NewReg,
460 MachineFunction &MF) const {
461 MachineRegisterInfo *MRI = &MF.getRegInfo();
462 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(Reg);
463 if ((Hint.first == (unsigned)ARMRI::RegPairOdd ||
464 Hint.first == (unsigned)ARMRI::RegPairEven) &&
465 Hint.second && TargetRegisterInfo::isVirtualRegister(Hint.second)) {
466 // If 'Reg' is one of the even / odd register pair and it's now changed
467 // (e.g. coalesced) into a different register. The other register of the
468 // pair allocation hint must be updated to reflect the relationship
470 unsigned OtherReg = Hint.second;
471 Hint = MRI->getRegAllocationHint(OtherReg);
472 if (Hint.second == Reg)
473 // Make sure the pair has not already divorced.
474 MRI->setRegAllocationHint(OtherReg, Hint.first, NewReg);
478 static unsigned calculateMaxStackAlignment(const MachineFrameInfo *FFI) {
479 unsigned MaxAlign = 0;
481 for (int i = FFI->getObjectIndexBegin(),
482 e = FFI->getObjectIndexEnd(); i != e; ++i) {
483 if (FFI->isDeadObjectIndex(i))
486 unsigned Align = FFI->getObjectAlignment(i);
487 MaxAlign = std::max(MaxAlign, Align);
493 /// hasFP - Return true if the specified function should have a dedicated frame
494 /// pointer register. This is true if the function has variable sized allocas
495 /// or if frame pointer elimination is disabled.
497 bool ARMBaseRegisterInfo::hasFP(const MachineFunction &MF) const {
498 const MachineFrameInfo *MFI = MF.getFrameInfo();
499 return (NoFramePointerElim ||
500 needsStackRealignment(MF) ||
501 MFI->hasVarSizedObjects() ||
502 MFI->isFrameAddressTaken());
505 bool ARMBaseRegisterInfo::
506 needsStackRealignment(const MachineFunction &MF) const {
507 if (!ARMDynamicStackAlign)
510 const MachineFrameInfo *MFI = MF.getFrameInfo();
511 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
512 unsigned StackAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
513 return (RealignStack &&
514 !AFI->isThumb1OnlyFunction() &&
515 (MFI->getMaxAlignment() > StackAlign) &&
516 !MFI->hasVarSizedObjects());
519 bool ARMBaseRegisterInfo::cannotEliminateFrame(const MachineFunction &MF) const {
520 const MachineFrameInfo *MFI = MF.getFrameInfo();
521 if (NoFramePointerElim && MFI->hasCalls())
523 return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken()
524 || needsStackRealignment(MF);
527 /// estimateStackSize - Estimate and return the size of the frame.
528 static unsigned estimateStackSize(MachineFunction &MF, MachineFrameInfo *MFI) {
529 const MachineFrameInfo *FFI = MF.getFrameInfo();
531 for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
532 int FixedOff = -FFI->getObjectOffset(i);
533 if (FixedOff > Offset) Offset = FixedOff;
535 for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
536 if (FFI->isDeadObjectIndex(i))
538 Offset += FFI->getObjectSize(i);
539 unsigned Align = FFI->getObjectAlignment(i);
540 // Adjust to alignment boundary
541 Offset = (Offset+Align-1)/Align*Align;
543 return (unsigned)Offset;
546 /// estimateRSStackSizeLimit - Look at each instruction that references stack
547 /// frames and return the stack size limit beyond which some of these
548 /// instructions will require scratch register during their expansion later.
550 ARMBaseRegisterInfo::estimateRSStackSizeLimit(MachineFunction &MF) const {
551 unsigned Limit = (1 << 12) - 1;
552 for (MachineFunction::iterator BB = MF.begin(),E = MF.end(); BB != E; ++BB) {
553 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
555 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
556 if (!I->getOperand(i).isFI()) continue;
558 const TargetInstrDesc &Desc = TII.get(I->getOpcode());
559 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
560 if (AddrMode == ARMII::AddrMode3 ||
561 AddrMode == ARMII::AddrModeT2_i8)
564 if (AddrMode == ARMII::AddrMode5 ||
565 AddrMode == ARMII::AddrModeT2_i8s4)
566 Limit = std::min(Limit, ((1U << 8) - 1) * 4);
568 if (AddrMode == ARMII::AddrModeT2_i12 && hasFP(MF))
569 // When the stack offset is negative, we will end up using
570 // the i8 instructions instead.
572 break; // At most one FI per instruction
581 ARMBaseRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
582 RegScavenger *RS) const {
583 // This tells PEI to spill the FP as if it is any other callee-save register
584 // to take advantage the eliminateFrameIndex machinery. This also ensures it
585 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
586 // to combine multiple loads / stores.
587 bool CanEliminateFrame = true;
588 bool CS1Spilled = false;
589 bool LRSpilled = false;
590 unsigned NumGPRSpills = 0;
591 SmallVector<unsigned, 4> UnspilledCS1GPRs;
592 SmallVector<unsigned, 4> UnspilledCS2GPRs;
593 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
595 MachineFrameInfo *MFI = MF.getFrameInfo();
597 // Calculate and set max stack object alignment early, so we can decide
598 // whether we will need stack realignment (and thus FP).
599 if (ARMDynamicStackAlign) {
600 unsigned MaxAlign = std::max(MFI->getMaxAlignment(),
601 calculateMaxStackAlignment(MFI));
602 MFI->setMaxAlignment(MaxAlign);
605 // Don't spill FP if the frame can be eliminated. This is determined
606 // by scanning the callee-save registers to see if any is used.
607 const unsigned *CSRegs = getCalleeSavedRegs();
608 const TargetRegisterClass* const *CSRegClasses = getCalleeSavedRegClasses();
609 for (unsigned i = 0; CSRegs[i]; ++i) {
610 unsigned Reg = CSRegs[i];
611 bool Spilled = false;
612 if (MF.getRegInfo().isPhysRegUsed(Reg)) {
613 AFI->setCSRegisterIsSpilled(Reg);
615 CanEliminateFrame = false;
617 // Check alias registers too.
618 for (const unsigned *Aliases = getAliasSet(Reg); *Aliases; ++Aliases) {
619 if (MF.getRegInfo().isPhysRegUsed(*Aliases)) {
621 CanEliminateFrame = false;
626 if (CSRegClasses[i] == ARM::GPRRegisterClass ||
627 CSRegClasses[i] == ARM::tGPRRegisterClass) {
631 if (!STI.isTargetDarwin()) {
638 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
653 if (!STI.isTargetDarwin()) {
654 UnspilledCS1GPRs.push_back(Reg);
664 UnspilledCS1GPRs.push_back(Reg);
667 UnspilledCS2GPRs.push_back(Reg);
674 bool ForceLRSpill = false;
675 if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
676 unsigned FnSize = TII.GetFunctionSizeInBytes(MF);
677 // Force LR to be spilled if the Thumb function size is > 2048. This enables
678 // use of BL to implement far jump. If it turns out that it's not needed
679 // then the branch fix up path will undo it.
680 if (FnSize >= (1 << 11)) {
681 CanEliminateFrame = false;
686 bool ExtraCSSpill = false;
687 if (!CanEliminateFrame || cannotEliminateFrame(MF)) {
688 AFI->setHasStackFrame(true);
690 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
691 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
692 if (!LRSpilled && CS1Spilled) {
693 MF.getRegInfo().setPhysRegUsed(ARM::LR);
694 AFI->setCSRegisterIsSpilled(ARM::LR);
696 UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(),
697 UnspilledCS1GPRs.end(), (unsigned)ARM::LR));
698 ForceLRSpill = false;
702 // Darwin ABI requires FP to point to the stack slot that contains the
704 if (STI.isTargetDarwin() || hasFP(MF)) {
705 MF.getRegInfo().setPhysRegUsed(FramePtr);
709 // If stack and double are 8-byte aligned and we are spilling an odd number
710 // of GPRs. Spill one extra callee save GPR so we won't have to pad between
711 // the integer and double callee save areas.
712 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
713 if (TargetAlign == 8 && (NumGPRSpills & 1)) {
714 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
715 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
716 unsigned Reg = UnspilledCS1GPRs[i];
717 // Don't spill high register if the function is thumb1
718 if (!AFI->isThumb1OnlyFunction() ||
719 isARMLowRegister(Reg) || Reg == ARM::LR) {
720 MF.getRegInfo().setPhysRegUsed(Reg);
721 AFI->setCSRegisterIsSpilled(Reg);
722 if (!isReservedReg(MF, Reg))
727 } else if (!UnspilledCS2GPRs.empty() &&
728 !AFI->isThumb1OnlyFunction()) {
729 unsigned Reg = UnspilledCS2GPRs.front();
730 MF.getRegInfo().setPhysRegUsed(Reg);
731 AFI->setCSRegisterIsSpilled(Reg);
732 if (!isReservedReg(MF, Reg))
737 // Estimate if we might need to scavenge a register at some point in order
738 // to materialize a stack offset. If so, either spill one additional
739 // callee-saved register or reserve a special spill slot to facilitate
740 // register scavenging. Thumb1 needs a spill slot for stack pointer
741 // adjustments also, even when the frame itself is small.
742 if (RS && !ExtraCSSpill) {
743 MachineFrameInfo *MFI = MF.getFrameInfo();
744 // If any of the stack slot references may be out of range of an
745 // immediate offset, make sure a register (or a spill slot) is
746 // available for the register scavenger. Note that if we're indexing
747 // off the frame pointer, the effective stack size is 4 bytes larger
748 // since the FP points to the stack slot of the previous FP.
749 if (estimateStackSize(MF, MFI) + (hasFP(MF) ? 4 : 0)
750 >= estimateRSStackSizeLimit(MF)) {
751 // If any non-reserved CS register isn't spilled, just spill one or two
752 // extra. That should take care of it!
753 unsigned NumExtras = TargetAlign / 4;
754 SmallVector<unsigned, 2> Extras;
755 while (NumExtras && !UnspilledCS1GPRs.empty()) {
756 unsigned Reg = UnspilledCS1GPRs.back();
757 UnspilledCS1GPRs.pop_back();
758 if (!isReservedReg(MF, Reg)) {
759 Extras.push_back(Reg);
763 // For non-Thumb1 functions, also check for hi-reg CS registers
764 if (!AFI->isThumb1OnlyFunction()) {
765 while (NumExtras && !UnspilledCS2GPRs.empty()) {
766 unsigned Reg = UnspilledCS2GPRs.back();
767 UnspilledCS2GPRs.pop_back();
768 if (!isReservedReg(MF, Reg)) {
769 Extras.push_back(Reg);
774 if (Extras.size() && NumExtras == 0) {
775 for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
776 MF.getRegInfo().setPhysRegUsed(Extras[i]);
777 AFI->setCSRegisterIsSpilled(Extras[i]);
779 } else if (!AFI->isThumb1OnlyFunction()) {
780 // note: Thumb1 functions spill to R12, not the stack.
781 // Reserve a slot closest to SP or frame pointer.
782 const TargetRegisterClass *RC = ARM::GPRRegisterClass;
783 RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
784 RC->getAlignment()));
791 MF.getRegInfo().setPhysRegUsed(ARM::LR);
792 AFI->setCSRegisterIsSpilled(ARM::LR);
793 AFI->setLRIsSpilledForFarJump(true);
797 unsigned ARMBaseRegisterInfo::getRARegister() const {
801 unsigned ARMBaseRegisterInfo::getFrameRegister(MachineFunction &MF) const {
802 if (STI.isTargetDarwin() || hasFP(MF))
807 unsigned ARMBaseRegisterInfo::getEHExceptionRegister() const {
808 llvm_unreachable("What is the exception register");
812 unsigned ARMBaseRegisterInfo::getEHHandlerRegister() const {
813 llvm_unreachable("What is the exception handler register");
817 int ARMBaseRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
818 return ARMGenRegisterInfo::getDwarfRegNumFull(RegNum, 0);
821 unsigned ARMBaseRegisterInfo::getRegisterPairEven(unsigned Reg,
822 const MachineFunction &MF) const {
825 // Return 0 if either register of the pair is a special register.
834 return isReservedReg(MF, ARM::R7) ? 0 : ARM::R6;
836 return isReservedReg(MF, ARM::R9) ? 0 :ARM::R8;
838 return isReservedReg(MF, ARM::R11) ? 0 : ARM::R10;
910 unsigned ARMBaseRegisterInfo::getRegisterPairOdd(unsigned Reg,
911 const MachineFunction &MF) const {
914 // Return 0 if either register of the pair is a special register.
923 return isReservedReg(MF, ARM::R7) ? 0 : ARM::R7;
925 return isReservedReg(MF, ARM::R9) ? 0 :ARM::R9;
927 return isReservedReg(MF, ARM::R11) ? 0 : ARM::R11;
999 /// emitLoadConstPool - Emits a load from constpool to materialize the
1000 /// specified immediate.
1001 void ARMBaseRegisterInfo::
1002 emitLoadConstPool(MachineBasicBlock &MBB,
1003 MachineBasicBlock::iterator &MBBI,
1005 unsigned DestReg, unsigned SubIdx, int Val,
1006 ARMCC::CondCodes Pred,
1007 unsigned PredReg) const {
1008 MachineFunction &MF = *MBB.getParent();
1009 MachineConstantPool *ConstantPool = MF.getConstantPool();
1011 ConstantInt::get(Type::getInt32Ty(MF.getFunction()->getContext()), Val);
1012 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
1014 BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
1015 .addReg(DestReg, getDefRegState(true), SubIdx)
1016 .addConstantPoolIndex(Idx)
1017 .addReg(0).addImm(0).addImm(Pred).addReg(PredReg);
1020 bool ARMBaseRegisterInfo::
1021 requiresRegisterScavenging(const MachineFunction &MF) const {
1025 bool ARMBaseRegisterInfo::
1026 requiresFrameIndexScavenging(const MachineFunction &MF) const {
1030 // hasReservedCallFrame - Under normal circumstances, when a frame pointer is
1031 // not required, we reserve argument space for call sites in the function
1032 // immediately on entry to the current function. This eliminates the need for
1033 // add/sub sp brackets around call sites. Returns true if the call frame is
1034 // included as part of the stack frame.
1035 bool ARMBaseRegisterInfo::
1036 hasReservedCallFrame(MachineFunction &MF) const {
1037 const MachineFrameInfo *FFI = MF.getFrameInfo();
1038 unsigned CFSize = FFI->getMaxCallFrameSize();
1039 // It's not always a good idea to include the call frame as part of the
1040 // stack frame. ARM (especially Thumb) has small immediate offset to
1041 // address the stack frame. So a large call frame can cause poor codegen
1042 // and may even makes it impossible to scavenge a register.
1043 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
1046 return !MF.getFrameInfo()->hasVarSizedObjects();
1050 emitSPUpdate(bool isARM,
1051 MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
1052 DebugLoc dl, const ARMBaseInstrInfo &TII,
1054 ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
1056 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
1057 Pred, PredReg, TII);
1059 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
1060 Pred, PredReg, TII);
1064 void ARMBaseRegisterInfo::
1065 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1066 MachineBasicBlock::iterator I) const {
1067 if (!hasReservedCallFrame(MF)) {
1068 // If we have alloca, convert as follows:
1069 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
1070 // ADJCALLSTACKUP -> add, sp, sp, amount
1071 MachineInstr *Old = I;
1072 DebugLoc dl = Old->getDebugLoc();
1073 unsigned Amount = Old->getOperand(0).getImm();
1075 // We need to keep the stack aligned properly. To do this, we round the
1076 // amount of space needed for the outgoing arguments up to the next
1077 // alignment boundary.
1078 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1079 Amount = (Amount+Align-1)/Align*Align;
1081 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1082 assert(!AFI->isThumb1OnlyFunction() &&
1083 "This eliminateCallFramePseudoInstr does not suppor Thumb1!");
1084 bool isARM = !AFI->isThumbFunction();
1086 // Replace the pseudo instruction with a new instruction...
1087 unsigned Opc = Old->getOpcode();
1088 ARMCC::CondCodes Pred = (ARMCC::CondCodes)Old->getOperand(1).getImm();
1089 // FIXME: Thumb2 version of ADJCALLSTACKUP and ADJCALLSTACKDOWN?
1090 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
1091 // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
1092 unsigned PredReg = Old->getOperand(2).getReg();
1093 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, Pred, PredReg);
1095 // Note: PredReg is operand 3 for ADJCALLSTACKUP.
1096 unsigned PredReg = Old->getOperand(3).getReg();
1097 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
1098 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, Pred, PredReg);
1106 ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
1107 int SPAdj, int *Value,
1108 RegScavenger *RS) const {
1110 MachineInstr &MI = *II;
1111 MachineBasicBlock &MBB = *MI.getParent();
1112 MachineFunction &MF = *MBB.getParent();
1113 const MachineFrameInfo *MFI = MF.getFrameInfo();
1114 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1115 assert(!AFI->isThumb1OnlyFunction() &&
1116 "This eliminateFrameIndex does not support Thumb1!");
1118 while (!MI.getOperand(i).isFI()) {
1120 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
1123 unsigned FrameReg = ARM::SP;
1124 int FrameIndex = MI.getOperand(i).getIndex();
1125 int Offset = MFI->getObjectOffset(FrameIndex) + MFI->getStackSize() + SPAdj;
1126 bool isFixed = MFI->isFixedObjectIndex(FrameIndex);
1128 // When doing dynamic stack realignment, all of these need to change(?)
1129 if (AFI->isGPRCalleeSavedArea1Frame(FrameIndex))
1130 Offset -= AFI->getGPRCalleeSavedArea1Offset();
1131 else if (AFI->isGPRCalleeSavedArea2Frame(FrameIndex))
1132 Offset -= AFI->getGPRCalleeSavedArea2Offset();
1133 else if (AFI->isDPRCalleeSavedAreaFrame(FrameIndex))
1134 Offset -= AFI->getDPRCalleeSavedAreaOffset();
1135 else if (needsStackRealignment(MF)) {
1136 // When dynamically realigning the stack, use the frame pointer for
1137 // parameters, and the stack pointer for locals.
1138 assert (hasFP(MF) && "dynamic stack realignment without a FP!");
1140 FrameReg = getFrameRegister(MF);
1141 Offset -= AFI->getFramePtrSpillOffset();
1142 // When referencing from the frame pointer, stack pointer adjustments
1146 } else if (hasFP(MF) && AFI->hasStackFrame()) {
1147 assert(SPAdj == 0 && "Unexpected stack offset!");
1148 if (isFixed || MFI->hasVarSizedObjects()) {
1149 // Use frame pointer to reference fixed objects unless this is a
1150 // frameless function.
1151 FrameReg = getFrameRegister(MF);
1152 Offset -= AFI->getFramePtrSpillOffset();
1153 } else if (AFI->isThumb2Function()) {
1154 // In Thumb2 mode, the negative offset is very limited.
1155 int FPOffset = Offset - AFI->getFramePtrSpillOffset();
1156 if (FPOffset >= -255 && FPOffset < 0) {
1157 FrameReg = getFrameRegister(MF);
1163 // Modify MI as necessary to handle as much of 'Offset' as possible
1165 if (!AFI->isThumbFunction())
1166 Done = rewriteARMFrameIndex(MI, i, FrameReg, Offset, TII);
1168 assert(AFI->isThumb2Function());
1169 Done = rewriteT2FrameIndex(MI, i, FrameReg, Offset, TII);
1174 // If we get here, the immediate doesn't fit into the instruction. We folded
1175 // as much as possible above, handle the rest, providing a register that is
1178 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode4) &&
1179 "This code isn't needed if offset already handled!");
1181 unsigned ScratchReg = 0;
1182 int PIdx = MI.findFirstPredOperandIdx();
1183 ARMCC::CondCodes Pred = (PIdx == -1)
1184 ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
1185 unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg();
1187 // Must be addrmode4.
1188 MI.getOperand(i).ChangeToRegister(FrameReg, false, false, false);
1190 ScratchReg = MF.getRegInfo().createVirtualRegister(ARM::GPRRegisterClass);
1191 if (Value) *Value = Offset;
1192 if (!AFI->isThumbFunction())
1193 emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1194 Offset, Pred, PredReg, TII);
1196 assert(AFI->isThumb2Function());
1197 emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1198 Offset, Pred, PredReg, TII);
1200 MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true);
1201 if (!ReuseFrameIndexVals)
1207 /// Move iterator past the next bunch of callee save load / store ops for
1208 /// the particular spill area (1: integer area 1, 2: integer area 2,
1209 /// 3: fp area, 0: don't care).
1210 static void movePastCSLoadStoreOps(MachineBasicBlock &MBB,
1211 MachineBasicBlock::iterator &MBBI,
1212 int Opc1, int Opc2, unsigned Area,
1213 const ARMSubtarget &STI) {
1214 while (MBBI != MBB.end() &&
1215 ((MBBI->getOpcode() == Opc1) || (MBBI->getOpcode() == Opc2)) &&
1216 MBBI->getOperand(1).isFI()) {
1219 unsigned Category = 0;
1220 switch (MBBI->getOperand(0).getReg()) {
1221 case ARM::R4: case ARM::R5: case ARM::R6: case ARM::R7:
1225 case ARM::R8: case ARM::R9: case ARM::R10: case ARM::R11:
1226 Category = STI.isTargetDarwin() ? 2 : 1;
1228 case ARM::D8: case ARM::D9: case ARM::D10: case ARM::D11:
1229 case ARM::D12: case ARM::D13: case ARM::D14: case ARM::D15:
1236 if (Done || Category != Area)
1244 void ARMBaseRegisterInfo::
1245 emitPrologue(MachineFunction &MF) const {
1246 MachineBasicBlock &MBB = MF.front();
1247 MachineBasicBlock::iterator MBBI = MBB.begin();
1248 MachineFrameInfo *MFI = MF.getFrameInfo();
1249 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1250 assert(!AFI->isThumb1OnlyFunction() &&
1251 "This emitPrologue does not suppor Thumb1!");
1252 bool isARM = !AFI->isThumbFunction();
1253 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1254 unsigned NumBytes = MFI->getStackSize();
1255 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
1256 DebugLoc dl = (MBBI != MBB.end() ?
1257 MBBI->getDebugLoc() : DebugLoc::getUnknownLoc());
1259 // Determine the sizes of each callee-save spill areas and record which frame
1260 // belongs to which callee-save spill areas.
1261 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
1262 int FramePtrSpillFI = 0;
1264 // Allocate the vararg register save area. This is not counted in NumBytes.
1266 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -VARegSaveSize);
1268 if (!AFI->hasStackFrame()) {
1270 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes);
1274 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1275 unsigned Reg = CSI[i].getReg();
1276 int FI = CSI[i].getFrameIdx();
1283 if (Reg == FramePtr)
1284 FramePtrSpillFI = FI;
1285 AFI->addGPRCalleeSavedArea1Frame(FI);
1292 if (Reg == FramePtr)
1293 FramePtrSpillFI = FI;
1294 if (STI.isTargetDarwin()) {
1295 AFI->addGPRCalleeSavedArea2Frame(FI);
1298 AFI->addGPRCalleeSavedArea1Frame(FI);
1303 AFI->addDPRCalleeSavedAreaFrame(FI);
1308 // Build the new SUBri to adjust SP for integer callee-save spill area 1.
1309 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -GPRCS1Size);
1310 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, ARM::t2STRi12, 1, STI);
1312 // Set FP to point to the stack slot that contains the previous FP.
1313 // For Darwin, FP is R7, which has now been stored in spill area 1.
1314 // Otherwise, if this is not Darwin, all the callee-saved registers go
1315 // into spill area 1, including the FP in R11. In either case, it is
1316 // now safe to emit this assignment.
1317 if (STI.isTargetDarwin() || hasFP(MF)) {
1318 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : ARM::t2ADDri;
1319 MachineInstrBuilder MIB =
1320 BuildMI(MBB, MBBI, dl, TII.get(ADDriOpc), FramePtr)
1321 .addFrameIndex(FramePtrSpillFI).addImm(0);
1322 AddDefaultCC(AddDefaultPred(MIB));
1325 // Build the new SUBri to adjust SP for integer callee-save spill area 2.
1326 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -GPRCS2Size);
1328 // Build the new SUBri to adjust SP for FP callee-save spill area.
1329 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, ARM::t2STRi12, 2, STI);
1330 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRCSSize);
1332 // Determine starting offsets of spill areas.
1333 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
1334 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
1335 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
1336 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + NumBytes);
1337 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
1338 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
1339 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
1341 movePastCSLoadStoreOps(MBB, MBBI, ARM::VSTRD, 0, 3, STI);
1342 NumBytes = DPRCSOffset;
1344 // Adjust SP after all the callee-save spills.
1345 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes);
1348 if (STI.isTargetELF() && hasFP(MF)) {
1349 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
1350 AFI->getFramePtrSpillOffset());
1353 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
1354 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
1355 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
1357 // If we need dynamic stack realignment, do it here.
1358 if (needsStackRealignment(MF)) {
1360 unsigned MaxAlign = MFI->getMaxAlignment();
1361 assert (!AFI->isThumb1OnlyFunction());
1362 Opc = AFI->isThumbFunction() ? ARM::t2BICri : ARM::BICri;
1364 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(Opc), ARM::SP)
1365 .addReg(ARM::SP, RegState::Kill)
1366 .addImm(MaxAlign-1)));
1370 static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
1371 for (unsigned i = 0; CSRegs[i]; ++i)
1372 if (Reg == CSRegs[i])
1377 static bool isCSRestore(MachineInstr *MI,
1378 const ARMBaseInstrInfo &TII,
1379 const unsigned *CSRegs) {
1380 return ((MI->getOpcode() == (int)ARM::VLDRD ||
1381 MI->getOpcode() == (int)ARM::LDR ||
1382 MI->getOpcode() == (int)ARM::t2LDRi12) &&
1383 MI->getOperand(1).isFI() &&
1384 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs));
1387 void ARMBaseRegisterInfo::
1388 emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const {
1389 MachineBasicBlock::iterator MBBI = prior(MBB.end());
1390 assert(MBBI->getDesc().isReturn() &&
1391 "Can only insert epilog into returning blocks");
1392 DebugLoc dl = MBBI->getDebugLoc();
1393 MachineFrameInfo *MFI = MF.getFrameInfo();
1394 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1395 assert(!AFI->isThumb1OnlyFunction() &&
1396 "This emitEpilogue does not suppor Thumb1!");
1397 bool isARM = !AFI->isThumbFunction();
1399 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1400 int NumBytes = (int)MFI->getStackSize();
1402 if (!AFI->hasStackFrame()) {
1404 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
1406 // Unwind MBBI to point to first LDR / VLDRD.
1407 const unsigned *CSRegs = getCalleeSavedRegs();
1408 if (MBBI != MBB.begin()) {
1411 while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs));
1412 if (!isCSRestore(MBBI, TII, CSRegs))
1416 // Move SP to start of FP callee save spill area.
1417 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
1418 AFI->getGPRCalleeSavedArea2Size() +
1419 AFI->getDPRCalleeSavedAreaSize());
1421 // Darwin ABI requires FP to point to the stack slot that contains the
1423 bool HasFP = hasFP(MF);
1424 if ((STI.isTargetDarwin() && NumBytes) || HasFP) {
1425 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
1426 // Reset SP based on frame pointer only if the stack frame extends beyond
1427 // frame pointer stack slot or target is ELF and the function has FP.
1429 AFI->getGPRCalleeSavedArea2Size() ||
1430 AFI->getDPRCalleeSavedAreaSize() ||
1431 AFI->getDPRCalleeSavedAreaOffset()) {
1434 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
1437 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
1442 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
1444 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
1446 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2gpr), ARM::SP)
1450 } else if (NumBytes)
1451 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
1453 // Move SP to start of integer callee save spill area 2.
1454 movePastCSLoadStoreOps(MBB, MBBI, ARM::VLDRD, 0, 3, STI);
1455 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedAreaSize());
1457 // Move SP to start of integer callee save spill area 1.
1458 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, ARM::t2LDRi12, 2, STI);
1459 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getGPRCalleeSavedArea2Size());
1461 // Move SP to SP upon entry to the function.
1462 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, ARM::t2LDRi12, 1, STI);
1463 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getGPRCalleeSavedArea1Size());
1467 emitSPUpdate(isARM, MBB, MBBI, dl, TII, VARegSaveSize);
1470 #include "ARMGenRegisterInfo.inc"