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"));
47 unsigned ARMBaseRegisterInfo::getRegisterNumbering(unsigned RegEnum,
55 llvm_unreachable("Unknown ARM register!");
56 case R0: case D0: case Q0: return 0;
57 case R1: case D1: case Q1: return 1;
58 case R2: case D2: case Q2: return 2;
59 case R3: case D3: case Q3: return 3;
60 case R4: case D4: case Q4: return 4;
61 case R5: case D5: case Q5: return 5;
62 case R6: case D6: case Q6: return 6;
63 case R7: case D7: case Q7: return 7;
64 case R8: case D8: case Q8: return 8;
65 case R9: case D9: case Q9: return 9;
66 case R10: case D10: case Q10: return 10;
67 case R11: case D11: case Q11: return 11;
68 case R12: case D12: case Q12: return 12;
69 case SP: case D13: case Q13: return 13;
70 case LR: case D14: case Q14: return 14;
71 case PC: case D15: case Q15: return 15;
90 case S0: case S1: case S2: case S3:
91 case S4: case S5: case S6: case S7:
92 case S8: case S9: case S10: case S11:
93 case S12: case S13: case S14: case S15:
94 case S16: case S17: case S18: case S19:
95 case S20: case S21: case S22: case S23:
96 case S24: case S25: case S26: case S27:
97 case S28: case S29: case S30: case S31: {
101 default: return 0; // Avoid compile time warning.
139 ARMBaseRegisterInfo::ARMBaseRegisterInfo(const ARMBaseInstrInfo &tii,
140 const ARMSubtarget &sti)
141 : ARMGenRegisterInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
143 FramePtr((STI.isTargetDarwin() || STI.isThumb()) ? ARM::R7 : ARM::R11) {
147 ARMBaseRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
148 static const unsigned CalleeSavedRegs[] = {
149 ARM::LR, ARM::R11, ARM::R10, ARM::R9, ARM::R8,
150 ARM::R7, ARM::R6, ARM::R5, ARM::R4,
152 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
153 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
157 static const unsigned DarwinCalleeSavedRegs[] = {
158 // Darwin ABI deviates from ARM standard ABI. R9 is not a callee-saved
160 ARM::LR, ARM::R7, ARM::R6, ARM::R5, ARM::R4,
161 ARM::R11, ARM::R10, ARM::R8,
163 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
164 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
167 return STI.isTargetDarwin() ? DarwinCalleeSavedRegs : CalleeSavedRegs;
170 const TargetRegisterClass* const *
171 ARMBaseRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
172 static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
173 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
174 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
175 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
177 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
178 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
182 static const TargetRegisterClass * const ThumbCalleeSavedRegClasses[] = {
183 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
184 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::tGPRRegClass,
185 &ARM::tGPRRegClass,&ARM::tGPRRegClass,&ARM::tGPRRegClass,
187 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
188 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
192 static const TargetRegisterClass * const DarwinCalleeSavedRegClasses[] = {
193 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
194 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
195 &ARM::GPRRegClass, &ARM::GPRRegClass,
197 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
198 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
202 static const TargetRegisterClass * const DarwinThumbCalleeSavedRegClasses[] ={
203 &ARM::GPRRegClass, &ARM::tGPRRegClass, &ARM::tGPRRegClass,
204 &ARM::tGPRRegClass, &ARM::tGPRRegClass, &ARM::GPRRegClass,
205 &ARM::GPRRegClass, &ARM::GPRRegClass,
207 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
208 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
212 if (STI.isThumb1Only()) {
213 return STI.isTargetDarwin()
214 ? DarwinThumbCalleeSavedRegClasses : ThumbCalleeSavedRegClasses;
216 return STI.isTargetDarwin()
217 ? DarwinCalleeSavedRegClasses : CalleeSavedRegClasses;
220 BitVector ARMBaseRegisterInfo::
221 getReservedRegs(const MachineFunction &MF) const {
222 // FIXME: avoid re-calculating this everytime.
223 BitVector Reserved(getNumRegs());
224 Reserved.set(ARM::SP);
225 Reserved.set(ARM::PC);
226 if (STI.isTargetDarwin() || hasFP(MF))
227 Reserved.set(FramePtr);
228 // Some targets reserve R9.
229 if (STI.isR9Reserved())
230 Reserved.set(ARM::R9);
234 bool ARMBaseRegisterInfo::isReservedReg(const MachineFunction &MF,
235 unsigned Reg) const {
243 if (FramePtr == Reg && (STI.isTargetDarwin() || hasFP(MF)))
247 return STI.isR9Reserved();
253 const TargetRegisterClass *
254 ARMBaseRegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A,
255 const TargetRegisterClass *B,
256 unsigned SubIdx) const {
264 if (A->getSize() == 8) {
265 if (B == &ARM::SPR_8RegClass)
266 return &ARM::DPR_8RegClass;
267 assert(B == &ARM::SPRRegClass && "Expecting SPR register class!");
268 if (A == &ARM::DPR_8RegClass)
270 return &ARM::DPR_VFP2RegClass;
273 assert(A->getSize() == 16 && "Expecting a Q register class!");
274 if (B == &ARM::SPR_8RegClass)
275 return &ARM::QPR_8RegClass;
276 return &ARM::QPR_VFP2RegClass;
280 if (B == &ARM::DPR_VFP2RegClass)
281 return &ARM::QPR_VFP2RegClass;
282 if (B == &ARM::DPR_8RegClass)
283 return &ARM::QPR_8RegClass;
289 const TargetRegisterClass *
290 ARMBaseRegisterInfo::getPointerRegClass(unsigned Kind) const {
291 return ARM::GPRRegisterClass;
294 /// getAllocationOrder - Returns the register allocation order for a specified
295 /// register class in the form of a pair of TargetRegisterClass iterators.
296 std::pair<TargetRegisterClass::iterator,TargetRegisterClass::iterator>
297 ARMBaseRegisterInfo::getAllocationOrder(const TargetRegisterClass *RC,
298 unsigned HintType, unsigned HintReg,
299 const MachineFunction &MF) const {
300 // Alternative register allocation orders when favoring even / odd registers
301 // of register pairs.
303 // No FP, R9 is available.
304 static const unsigned GPREven1[] = {
305 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8, ARM::R10,
306 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7,
309 static const unsigned GPROdd1[] = {
310 ARM::R1, ARM::R3, ARM::R5, ARM::R7, ARM::R9, ARM::R11,
311 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6,
315 // FP is R7, R9 is available.
316 static const unsigned GPREven2[] = {
317 ARM::R0, ARM::R2, ARM::R4, ARM::R8, ARM::R10,
318 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6,
321 static const unsigned GPROdd2[] = {
322 ARM::R1, ARM::R3, ARM::R5, ARM::R9, ARM::R11,
323 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6,
327 // FP is R11, R9 is available.
328 static const unsigned GPREven3[] = {
329 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8,
330 ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7,
333 static const unsigned GPROdd3[] = {
334 ARM::R1, ARM::R3, ARM::R5, ARM::R6, ARM::R9,
335 ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R7,
339 // No FP, R9 is not available.
340 static const unsigned GPREven4[] = {
341 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R10,
342 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8,
345 static const unsigned GPROdd4[] = {
346 ARM::R1, ARM::R3, ARM::R5, ARM::R7, ARM::R11,
347 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8,
351 // FP is R7, R9 is not available.
352 static const unsigned GPREven5[] = {
353 ARM::R0, ARM::R2, ARM::R4, ARM::R10,
354 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6, ARM::R8,
357 static const unsigned GPROdd5[] = {
358 ARM::R1, ARM::R3, ARM::R5, ARM::R11,
359 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8,
363 // FP is R11, R9 is not available.
364 static const unsigned GPREven6[] = {
365 ARM::R0, ARM::R2, ARM::R4, ARM::R6,
366 ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8
368 static const unsigned GPROdd6[] = {
369 ARM::R1, ARM::R3, ARM::R5, ARM::R7,
370 ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8
374 if (HintType == ARMRI::RegPairEven) {
375 if (isPhysicalRegister(HintReg) && getRegisterPairEven(HintReg, MF) == 0)
376 // It's no longer possible to fulfill this hint. Return the default
378 return std::make_pair(RC->allocation_order_begin(MF),
379 RC->allocation_order_end(MF));
381 if (!STI.isTargetDarwin() && !hasFP(MF)) {
382 if (!STI.isR9Reserved())
383 return std::make_pair(GPREven1,
384 GPREven1 + (sizeof(GPREven1)/sizeof(unsigned)));
386 return std::make_pair(GPREven4,
387 GPREven4 + (sizeof(GPREven4)/sizeof(unsigned)));
388 } else if (FramePtr == ARM::R7) {
389 if (!STI.isR9Reserved())
390 return std::make_pair(GPREven2,
391 GPREven2 + (sizeof(GPREven2)/sizeof(unsigned)));
393 return std::make_pair(GPREven5,
394 GPREven5 + (sizeof(GPREven5)/sizeof(unsigned)));
395 } else { // FramePtr == ARM::R11
396 if (!STI.isR9Reserved())
397 return std::make_pair(GPREven3,
398 GPREven3 + (sizeof(GPREven3)/sizeof(unsigned)));
400 return std::make_pair(GPREven6,
401 GPREven6 + (sizeof(GPREven6)/sizeof(unsigned)));
403 } else if (HintType == ARMRI::RegPairOdd) {
404 if (isPhysicalRegister(HintReg) && getRegisterPairOdd(HintReg, MF) == 0)
405 // It's no longer possible to fulfill this hint. Return the default
407 return std::make_pair(RC->allocation_order_begin(MF),
408 RC->allocation_order_end(MF));
410 if (!STI.isTargetDarwin() && !hasFP(MF)) {
411 if (!STI.isR9Reserved())
412 return std::make_pair(GPROdd1,
413 GPROdd1 + (sizeof(GPROdd1)/sizeof(unsigned)));
415 return std::make_pair(GPROdd4,
416 GPROdd4 + (sizeof(GPROdd4)/sizeof(unsigned)));
417 } else if (FramePtr == ARM::R7) {
418 if (!STI.isR9Reserved())
419 return std::make_pair(GPROdd2,
420 GPROdd2 + (sizeof(GPROdd2)/sizeof(unsigned)));
422 return std::make_pair(GPROdd5,
423 GPROdd5 + (sizeof(GPROdd5)/sizeof(unsigned)));
424 } else { // FramePtr == ARM::R11
425 if (!STI.isR9Reserved())
426 return std::make_pair(GPROdd3,
427 GPROdd3 + (sizeof(GPROdd3)/sizeof(unsigned)));
429 return std::make_pair(GPROdd6,
430 GPROdd6 + (sizeof(GPROdd6)/sizeof(unsigned)));
433 return std::make_pair(RC->allocation_order_begin(MF),
434 RC->allocation_order_end(MF));
437 /// ResolveRegAllocHint - Resolves the specified register allocation hint
438 /// to a physical register. Returns the physical register if it is successful.
440 ARMBaseRegisterInfo::ResolveRegAllocHint(unsigned Type, unsigned Reg,
441 const MachineFunction &MF) const {
442 if (Reg == 0 || !isPhysicalRegister(Reg))
446 else if (Type == (unsigned)ARMRI::RegPairOdd)
448 return getRegisterPairOdd(Reg, MF);
449 else if (Type == (unsigned)ARMRI::RegPairEven)
451 return getRegisterPairEven(Reg, MF);
456 ARMBaseRegisterInfo::UpdateRegAllocHint(unsigned Reg, unsigned NewReg,
457 MachineFunction &MF) const {
458 MachineRegisterInfo *MRI = &MF.getRegInfo();
459 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(Reg);
460 if ((Hint.first == (unsigned)ARMRI::RegPairOdd ||
461 Hint.first == (unsigned)ARMRI::RegPairEven) &&
462 Hint.second && TargetRegisterInfo::isVirtualRegister(Hint.second)) {
463 // If 'Reg' is one of the even / odd register pair and it's now changed
464 // (e.g. coalesced) into a different register. The other register of the
465 // pair allocation hint must be updated to reflect the relationship
467 unsigned OtherReg = Hint.second;
468 Hint = MRI->getRegAllocationHint(OtherReg);
469 if (Hint.second == Reg)
470 // Make sure the pair has not already divorced.
471 MRI->setRegAllocationHint(OtherReg, Hint.first, NewReg);
475 /// hasFP - Return true if the specified function should have a dedicated frame
476 /// pointer register. This is true if the function has variable sized allocas
477 /// or if frame pointer elimination is disabled.
479 bool ARMBaseRegisterInfo::hasFP(const MachineFunction &MF) const {
480 const MachineFrameInfo *MFI = MF.getFrameInfo();
481 return (NoFramePointerElim ||
482 needsStackRealignment(MF) ||
483 MFI->hasVarSizedObjects() ||
484 MFI->isFrameAddressTaken());
487 bool ARMBaseRegisterInfo::
488 needsStackRealignment(const MachineFunction &MF) const {
489 const MachineFrameInfo *MFI = MF.getFrameInfo();
490 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
491 unsigned StackAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
492 return (RealignStack &&
493 !AFI->isThumb1OnlyFunction() &&
494 (MFI->getMaxAlignment() > StackAlign) &&
495 !MFI->hasVarSizedObjects());
498 bool ARMBaseRegisterInfo::
499 cannotEliminateFrame(const MachineFunction &MF) const {
500 const MachineFrameInfo *MFI = MF.getFrameInfo();
501 if (NoFramePointerElim && MFI->hasCalls())
503 return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken()
504 || needsStackRealignment(MF);
507 /// estimateStackSize - Estimate and return the size of the frame.
508 static unsigned estimateStackSize(MachineFunction &MF, MachineFrameInfo *MFI) {
509 const MachineFrameInfo *FFI = MF.getFrameInfo();
511 for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
512 int FixedOff = -FFI->getObjectOffset(i);
513 if (FixedOff > Offset) Offset = FixedOff;
515 for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
516 if (FFI->isDeadObjectIndex(i))
518 Offset += FFI->getObjectSize(i);
519 unsigned Align = FFI->getObjectAlignment(i);
520 // Adjust to alignment boundary
521 Offset = (Offset+Align-1)/Align*Align;
523 return (unsigned)Offset;
526 /// estimateRSStackSizeLimit - Look at each instruction that references stack
527 /// frames and return the stack size limit beyond which some of these
528 /// instructions will require a scratch register during their expansion later.
530 ARMBaseRegisterInfo::estimateRSStackSizeLimit(MachineFunction &MF) const {
531 unsigned Limit = (1 << 12) - 1;
532 for (MachineFunction::iterator BB = MF.begin(),E = MF.end(); BB != E; ++BB) {
533 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
535 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
536 if (!I->getOperand(i).isFI()) continue;
538 const TargetInstrDesc &Desc = TII.get(I->getOpcode());
539 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
540 if (AddrMode == ARMII::AddrMode3 ||
541 AddrMode == ARMII::AddrModeT2_i8)
544 if (AddrMode == ARMII::AddrMode5 ||
545 AddrMode == ARMII::AddrModeT2_i8s4)
546 Limit = std::min(Limit, ((1U << 8) - 1) * 4);
548 if (AddrMode == ARMII::AddrModeT2_i12 && hasFP(MF))
549 // When the stack offset is negative, we will end up using
550 // the i8 instructions instead.
553 if (AddrMode == ARMII::AddrMode6)
555 break; // At most one FI per instruction
564 ARMBaseRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
565 RegScavenger *RS) const {
566 // This tells PEI to spill the FP as if it is any other callee-save register
567 // to take advantage the eliminateFrameIndex machinery. This also ensures it
568 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
569 // to combine multiple loads / stores.
570 bool CanEliminateFrame = true;
571 bool CS1Spilled = false;
572 bool LRSpilled = false;
573 unsigned NumGPRSpills = 0;
574 SmallVector<unsigned, 4> UnspilledCS1GPRs;
575 SmallVector<unsigned, 4> UnspilledCS2GPRs;
576 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
579 // Calculate and set max stack object alignment early, so we can decide
580 // whether we will need stack realignment (and thus FP).
582 MachineFrameInfo *MFI = MF.getFrameInfo();
583 MFI->calculateMaxStackAlignment();
586 // Spill R4 if Thumb2 function requires stack realignment - it will be used as
588 // FIXME: It will be better just to find spare register here.
589 if (needsStackRealignment(MF) &&
590 AFI->isThumb2Function())
591 MF.getRegInfo().setPhysRegUsed(ARM::R4);
593 // Don't spill FP if the frame can be eliminated. This is determined
594 // by scanning the callee-save registers to see if any is used.
595 const unsigned *CSRegs = getCalleeSavedRegs();
596 const TargetRegisterClass* const *CSRegClasses = getCalleeSavedRegClasses();
597 for (unsigned i = 0; CSRegs[i]; ++i) {
598 unsigned Reg = CSRegs[i];
599 bool Spilled = false;
600 if (MF.getRegInfo().isPhysRegUsed(Reg)) {
601 AFI->setCSRegisterIsSpilled(Reg);
603 CanEliminateFrame = false;
605 // Check alias registers too.
606 for (const unsigned *Aliases = getAliasSet(Reg); *Aliases; ++Aliases) {
607 if (MF.getRegInfo().isPhysRegUsed(*Aliases)) {
609 CanEliminateFrame = false;
614 if (CSRegClasses[i] == ARM::GPRRegisterClass ||
615 CSRegClasses[i] == ARM::tGPRRegisterClass) {
619 if (!STI.isTargetDarwin()) {
626 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
641 if (!STI.isTargetDarwin()) {
642 UnspilledCS1GPRs.push_back(Reg);
652 UnspilledCS1GPRs.push_back(Reg);
655 UnspilledCS2GPRs.push_back(Reg);
662 bool ForceLRSpill = false;
663 if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
664 unsigned FnSize = TII.GetFunctionSizeInBytes(MF);
665 // Force LR to be spilled if the Thumb function size is > 2048. This enables
666 // use of BL to implement far jump. If it turns out that it's not needed
667 // then the branch fix up path will undo it.
668 if (FnSize >= (1 << 11)) {
669 CanEliminateFrame = false;
674 bool ExtraCSSpill = false;
675 if (!CanEliminateFrame || cannotEliminateFrame(MF)) {
676 AFI->setHasStackFrame(true);
678 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
679 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
680 if (!LRSpilled && CS1Spilled) {
681 MF.getRegInfo().setPhysRegUsed(ARM::LR);
682 AFI->setCSRegisterIsSpilled(ARM::LR);
684 UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(),
685 UnspilledCS1GPRs.end(), (unsigned)ARM::LR));
686 ForceLRSpill = false;
690 // Darwin ABI requires FP to point to the stack slot that contains the
692 if (STI.isTargetDarwin() || hasFP(MF)) {
693 MF.getRegInfo().setPhysRegUsed(FramePtr);
697 // If stack and double are 8-byte aligned and we are spilling an odd number
698 // of GPRs. Spill one extra callee save GPR so we won't have to pad between
699 // the integer and double callee save areas.
700 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
701 if (TargetAlign == 8 && (NumGPRSpills & 1)) {
702 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
703 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
704 unsigned Reg = UnspilledCS1GPRs[i];
705 // Don't spill high register if the function is thumb1
706 if (!AFI->isThumb1OnlyFunction() ||
707 isARMLowRegister(Reg) || Reg == ARM::LR) {
708 MF.getRegInfo().setPhysRegUsed(Reg);
709 AFI->setCSRegisterIsSpilled(Reg);
710 if (!isReservedReg(MF, Reg))
715 } else if (!UnspilledCS2GPRs.empty() &&
716 !AFI->isThumb1OnlyFunction()) {
717 unsigned Reg = UnspilledCS2GPRs.front();
718 MF.getRegInfo().setPhysRegUsed(Reg);
719 AFI->setCSRegisterIsSpilled(Reg);
720 if (!isReservedReg(MF, Reg))
725 // Estimate if we might need to scavenge a register at some point in order
726 // to materialize a stack offset. If so, either spill one additional
727 // callee-saved register or reserve a special spill slot to facilitate
728 // register scavenging. Thumb1 needs a spill slot for stack pointer
729 // adjustments also, even when the frame itself is small.
730 if (RS && !ExtraCSSpill) {
731 MachineFrameInfo *MFI = MF.getFrameInfo();
732 // If any of the stack slot references may be out of range of an
733 // immediate offset, make sure a register (or a spill slot) is
734 // available for the register scavenger. Note that if we're indexing
735 // off the frame pointer, the effective stack size is 4 bytes larger
736 // since the FP points to the stack slot of the previous FP.
737 if (estimateStackSize(MF, MFI) + (hasFP(MF) ? 4 : 0)
738 >= estimateRSStackSizeLimit(MF)) {
739 // If any non-reserved CS register isn't spilled, just spill one or two
740 // extra. That should take care of it!
741 unsigned NumExtras = TargetAlign / 4;
742 SmallVector<unsigned, 2> Extras;
743 while (NumExtras && !UnspilledCS1GPRs.empty()) {
744 unsigned Reg = UnspilledCS1GPRs.back();
745 UnspilledCS1GPRs.pop_back();
746 if (!isReservedReg(MF, Reg)) {
747 Extras.push_back(Reg);
751 // For non-Thumb1 functions, also check for hi-reg CS registers
752 if (!AFI->isThumb1OnlyFunction()) {
753 while (NumExtras && !UnspilledCS2GPRs.empty()) {
754 unsigned Reg = UnspilledCS2GPRs.back();
755 UnspilledCS2GPRs.pop_back();
756 if (!isReservedReg(MF, Reg)) {
757 Extras.push_back(Reg);
762 if (Extras.size() && NumExtras == 0) {
763 for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
764 MF.getRegInfo().setPhysRegUsed(Extras[i]);
765 AFI->setCSRegisterIsSpilled(Extras[i]);
767 } else if (!AFI->isThumb1OnlyFunction()) {
768 // note: Thumb1 functions spill to R12, not the stack.
769 // Reserve a slot closest to SP or frame pointer.
770 const TargetRegisterClass *RC = ARM::GPRRegisterClass;
771 RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
780 MF.getRegInfo().setPhysRegUsed(ARM::LR);
781 AFI->setCSRegisterIsSpilled(ARM::LR);
782 AFI->setLRIsSpilledForFarJump(true);
786 unsigned ARMBaseRegisterInfo::getRARegister() const {
791 ARMBaseRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
792 if (STI.isTargetDarwin() || hasFP(MF))
798 ARMBaseRegisterInfo::getFrameIndexReference(MachineFunction &MF, int FI,
799 unsigned &FrameReg) const {
800 const MachineFrameInfo *MFI = MF.getFrameInfo();
801 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
802 int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize();
803 bool isFixed = MFI->isFixedObjectIndex(FI);
806 if (AFI->isGPRCalleeSavedArea1Frame(FI))
807 Offset -= AFI->getGPRCalleeSavedArea1Offset();
808 else if (AFI->isGPRCalleeSavedArea2Frame(FI))
809 Offset -= AFI->getGPRCalleeSavedArea2Offset();
810 else if (AFI->isDPRCalleeSavedAreaFrame(FI))
811 Offset -= AFI->getDPRCalleeSavedAreaOffset();
812 else if (needsStackRealignment(MF)) {
813 // When dynamically realigning the stack, use the frame pointer for
814 // parameters, and the stack pointer for locals.
815 assert (hasFP(MF) && "dynamic stack realignment without a FP!");
817 FrameReg = getFrameRegister(MF);
818 Offset -= AFI->getFramePtrSpillOffset();
820 } else if (hasFP(MF) && AFI->hasStackFrame()) {
821 if (isFixed || MFI->hasVarSizedObjects()) {
822 // Use frame pointer to reference fixed objects unless this is a
823 // frameless function.
824 FrameReg = getFrameRegister(MF);
825 Offset -= AFI->getFramePtrSpillOffset();
826 } else if (AFI->isThumb2Function()) {
827 // In Thumb2 mode, the negative offset is very limited.
828 int FPOffset = Offset - AFI->getFramePtrSpillOffset();
829 if (FPOffset >= -255 && FPOffset < 0) {
830 FrameReg = getFrameRegister(MF);
840 ARMBaseRegisterInfo::getFrameIndexOffset(MachineFunction &MF, int FI) const {
842 return getFrameIndexReference(MF, FI, FrameReg);
845 unsigned ARMBaseRegisterInfo::getEHExceptionRegister() const {
846 llvm_unreachable("What is the exception register");
850 unsigned ARMBaseRegisterInfo::getEHHandlerRegister() const {
851 llvm_unreachable("What is the exception handler register");
855 int ARMBaseRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
856 return ARMGenRegisterInfo::getDwarfRegNumFull(RegNum, 0);
859 unsigned ARMBaseRegisterInfo::getRegisterPairEven(unsigned Reg,
860 const MachineFunction &MF) const {
863 // Return 0 if either register of the pair is a special register.
872 return isReservedReg(MF, ARM::R7) ? 0 : ARM::R6;
874 return isReservedReg(MF, ARM::R9) ? 0 :ARM::R8;
876 return isReservedReg(MF, ARM::R11) ? 0 : ARM::R10;
948 unsigned ARMBaseRegisterInfo::getRegisterPairOdd(unsigned Reg,
949 const MachineFunction &MF) const {
952 // Return 0 if either register of the pair is a special register.
961 return isReservedReg(MF, ARM::R7) ? 0 : ARM::R7;
963 return isReservedReg(MF, ARM::R9) ? 0 :ARM::R9;
965 return isReservedReg(MF, ARM::R11) ? 0 : ARM::R11;
1037 /// emitLoadConstPool - Emits a load from constpool to materialize the
1038 /// specified immediate.
1039 void ARMBaseRegisterInfo::
1040 emitLoadConstPool(MachineBasicBlock &MBB,
1041 MachineBasicBlock::iterator &MBBI,
1043 unsigned DestReg, unsigned SubIdx, int Val,
1044 ARMCC::CondCodes Pred,
1045 unsigned PredReg) const {
1046 MachineFunction &MF = *MBB.getParent();
1047 MachineConstantPool *ConstantPool = MF.getConstantPool();
1049 ConstantInt::get(Type::getInt32Ty(MF.getFunction()->getContext()), Val);
1050 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
1052 BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
1053 .addReg(DestReg, getDefRegState(true), SubIdx)
1054 .addConstantPoolIndex(Idx)
1055 .addReg(0).addImm(0).addImm(Pred).addReg(PredReg);
1058 bool ARMBaseRegisterInfo::
1059 requiresRegisterScavenging(const MachineFunction &MF) const {
1063 bool ARMBaseRegisterInfo::
1064 requiresFrameIndexScavenging(const MachineFunction &MF) const {
1068 // hasReservedCallFrame - Under normal circumstances, when a frame pointer is
1069 // not required, we reserve argument space for call sites in the function
1070 // immediately on entry to the current function. This eliminates the need for
1071 // add/sub sp brackets around call sites. Returns true if the call frame is
1072 // included as part of the stack frame.
1073 bool ARMBaseRegisterInfo::
1074 hasReservedCallFrame(MachineFunction &MF) const {
1075 const MachineFrameInfo *FFI = MF.getFrameInfo();
1076 unsigned CFSize = FFI->getMaxCallFrameSize();
1077 // It's not always a good idea to include the call frame as part of the
1078 // stack frame. ARM (especially Thumb) has small immediate offset to
1079 // address the stack frame. So a large call frame can cause poor codegen
1080 // and may even makes it impossible to scavenge a register.
1081 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
1084 return !MF.getFrameInfo()->hasVarSizedObjects();
1088 emitSPUpdate(bool isARM,
1089 MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
1090 DebugLoc dl, const ARMBaseInstrInfo &TII,
1092 ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
1094 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
1095 Pred, PredReg, TII);
1097 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
1098 Pred, PredReg, TII);
1102 void ARMBaseRegisterInfo::
1103 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1104 MachineBasicBlock::iterator I) const {
1105 if (!hasReservedCallFrame(MF)) {
1106 // If we have alloca, convert as follows:
1107 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
1108 // ADJCALLSTACKUP -> add, sp, sp, amount
1109 MachineInstr *Old = I;
1110 DebugLoc dl = Old->getDebugLoc();
1111 unsigned Amount = Old->getOperand(0).getImm();
1113 // We need to keep the stack aligned properly. To do this, we round the
1114 // amount of space needed for the outgoing arguments up to the next
1115 // alignment boundary.
1116 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1117 Amount = (Amount+Align-1)/Align*Align;
1119 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1120 assert(!AFI->isThumb1OnlyFunction() &&
1121 "This eliminateCallFramePseudoInstr does not suppor Thumb1!");
1122 bool isARM = !AFI->isThumbFunction();
1124 // Replace the pseudo instruction with a new instruction...
1125 unsigned Opc = Old->getOpcode();
1126 ARMCC::CondCodes Pred = (ARMCC::CondCodes)Old->getOperand(1).getImm();
1127 // FIXME: Thumb2 version of ADJCALLSTACKUP and ADJCALLSTACKDOWN?
1128 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
1129 // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
1130 unsigned PredReg = Old->getOperand(2).getReg();
1131 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, Pred, PredReg);
1133 // Note: PredReg is operand 3 for ADJCALLSTACKUP.
1134 unsigned PredReg = Old->getOperand(3).getReg();
1135 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
1136 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, Pred, PredReg);
1144 ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
1145 int SPAdj, int *Value,
1146 RegScavenger *RS) const {
1148 MachineInstr &MI = *II;
1149 MachineBasicBlock &MBB = *MI.getParent();
1150 MachineFunction &MF = *MBB.getParent();
1151 const MachineFrameInfo *MFI = MF.getFrameInfo();
1152 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1153 assert(!AFI->isThumb1OnlyFunction() &&
1154 "This eliminateFrameIndex does not support Thumb1!");
1156 while (!MI.getOperand(i).isFI()) {
1158 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
1161 int FrameIndex = MI.getOperand(i).getIndex();
1162 int Offset = MFI->getObjectOffset(FrameIndex) + MFI->getStackSize() + SPAdj;
1165 Offset = getFrameIndexReference(MF, FrameIndex, FrameReg);
1166 if (FrameReg != ARM::SP)
1169 // Modify MI as necessary to handle as much of 'Offset' as possible
1171 if (!AFI->isThumbFunction())
1172 Done = rewriteARMFrameIndex(MI, i, FrameReg, Offset, TII);
1174 assert(AFI->isThumb2Function());
1175 Done = rewriteT2FrameIndex(MI, i, FrameReg, Offset, TII);
1180 // If we get here, the immediate doesn't fit into the instruction. We folded
1181 // as much as possible above, handle the rest, providing a register that is
1184 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode4 ||
1185 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode6) &&
1186 "This code isn't needed if offset already handled!");
1188 unsigned ScratchReg = 0;
1189 int PIdx = MI.findFirstPredOperandIdx();
1190 ARMCC::CondCodes Pred = (PIdx == -1)
1191 ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
1192 unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg();
1194 // Must be addrmode4/6.
1195 MI.getOperand(i).ChangeToRegister(FrameReg, false, false, false);
1197 ScratchReg = MF.getRegInfo().createVirtualRegister(ARM::GPRRegisterClass);
1198 if (Value) *Value = Offset;
1199 if (!AFI->isThumbFunction())
1200 emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1201 Offset, Pred, PredReg, TII);
1203 assert(AFI->isThumb2Function());
1204 emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1205 Offset, Pred, PredReg, TII);
1207 MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true);
1208 if (!ReuseFrameIndexVals)
1214 /// Move iterator past the next bunch of callee save load / store ops for
1215 /// the particular spill area (1: integer area 1, 2: integer area 2,
1216 /// 3: fp area, 0: don't care).
1217 static void movePastCSLoadStoreOps(MachineBasicBlock &MBB,
1218 MachineBasicBlock::iterator &MBBI,
1219 int Opc1, int Opc2, unsigned Area,
1220 const ARMSubtarget &STI) {
1221 while (MBBI != MBB.end() &&
1222 ((MBBI->getOpcode() == Opc1) || (MBBI->getOpcode() == Opc2)) &&
1223 MBBI->getOperand(1).isFI()) {
1226 unsigned Category = 0;
1227 switch (MBBI->getOperand(0).getReg()) {
1228 case ARM::R4: case ARM::R5: case ARM::R6: case ARM::R7:
1232 case ARM::R8: case ARM::R9: case ARM::R10: case ARM::R11:
1233 Category = STI.isTargetDarwin() ? 2 : 1;
1235 case ARM::D8: case ARM::D9: case ARM::D10: case ARM::D11:
1236 case ARM::D12: case ARM::D13: case ARM::D14: case ARM::D15:
1243 if (Done || Category != Area)
1251 void ARMBaseRegisterInfo::
1252 emitPrologue(MachineFunction &MF) const {
1253 MachineBasicBlock &MBB = MF.front();
1254 MachineBasicBlock::iterator MBBI = MBB.begin();
1255 MachineFrameInfo *MFI = MF.getFrameInfo();
1256 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1257 assert(!AFI->isThumb1OnlyFunction() &&
1258 "This emitPrologue does not suppor Thumb1!");
1259 bool isARM = !AFI->isThumbFunction();
1260 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1261 unsigned NumBytes = MFI->getStackSize();
1262 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
1263 DebugLoc dl = (MBBI != MBB.end() ?
1264 MBBI->getDebugLoc() : DebugLoc::getUnknownLoc());
1266 // Determine the sizes of each callee-save spill areas and record which frame
1267 // belongs to which callee-save spill areas.
1268 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
1269 int FramePtrSpillFI = 0;
1271 // Allocate the vararg register save area. This is not counted in NumBytes.
1273 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -VARegSaveSize);
1275 if (!AFI->hasStackFrame()) {
1277 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes);
1281 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1282 unsigned Reg = CSI[i].getReg();
1283 int FI = CSI[i].getFrameIdx();
1290 if (Reg == FramePtr)
1291 FramePtrSpillFI = FI;
1292 AFI->addGPRCalleeSavedArea1Frame(FI);
1299 if (Reg == FramePtr)
1300 FramePtrSpillFI = FI;
1301 if (STI.isTargetDarwin()) {
1302 AFI->addGPRCalleeSavedArea2Frame(FI);
1305 AFI->addGPRCalleeSavedArea1Frame(FI);
1310 AFI->addDPRCalleeSavedAreaFrame(FI);
1315 // Build the new SUBri to adjust SP for integer callee-save spill area 1.
1316 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -GPRCS1Size);
1317 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, ARM::t2STRi12, 1, STI);
1319 // Set FP to point to the stack slot that contains the previous FP.
1320 // For Darwin, FP is R7, which has now been stored in spill area 1.
1321 // Otherwise, if this is not Darwin, all the callee-saved registers go
1322 // into spill area 1, including the FP in R11. In either case, it is
1323 // now safe to emit this assignment.
1324 if (STI.isTargetDarwin() || hasFP(MF)) {
1325 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : ARM::t2ADDri;
1326 MachineInstrBuilder MIB =
1327 BuildMI(MBB, MBBI, dl, TII.get(ADDriOpc), FramePtr)
1328 .addFrameIndex(FramePtrSpillFI).addImm(0);
1329 AddDefaultCC(AddDefaultPred(MIB));
1332 // Build the new SUBri to adjust SP for integer callee-save spill area 2.
1333 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -GPRCS2Size);
1335 // Build the new SUBri to adjust SP for FP callee-save spill area.
1336 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, ARM::t2STRi12, 2, STI);
1337 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRCSSize);
1339 // Determine starting offsets of spill areas.
1340 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
1341 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
1342 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
1343 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + NumBytes);
1344 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
1345 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
1346 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
1348 movePastCSLoadStoreOps(MBB, MBBI, ARM::VSTRD, 0, 3, STI);
1349 NumBytes = DPRCSOffset;
1351 // Adjust SP after all the callee-save spills.
1352 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes);
1355 if (STI.isTargetELF() && hasFP(MF)) {
1356 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
1357 AFI->getFramePtrSpillOffset());
1360 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
1361 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
1362 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
1364 // If we need dynamic stack realignment, do it here.
1365 if (needsStackRealignment(MF)) {
1366 unsigned MaxAlign = MFI->getMaxAlignment();
1367 assert (!AFI->isThumb1OnlyFunction());
1368 if (!AFI->isThumbFunction()) {
1369 // Emit bic sp, sp, MaxAlign
1370 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
1371 TII.get(ARM::BICri), ARM::SP)
1372 .addReg(ARM::SP, RegState::Kill)
1373 .addImm(MaxAlign-1)));
1375 // We cannot use sp as source/dest register here, thus we're emitting the
1376 // following sequence:
1378 // bic r4, r4, MaxAlign
1380 // FIXME: It will be better just to find spare register here.
1381 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2tgpr), ARM::R4)
1382 .addReg(ARM::SP, RegState::Kill);
1383 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
1384 TII.get(ARM::t2BICri), ARM::R4)
1385 .addReg(ARM::R4, RegState::Kill)
1386 .addImm(MaxAlign-1)));
1387 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVtgpr2gpr), ARM::SP)
1388 .addReg(ARM::R4, RegState::Kill);
1393 static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
1394 for (unsigned i = 0; CSRegs[i]; ++i)
1395 if (Reg == CSRegs[i])
1400 static bool isCSRestore(MachineInstr *MI,
1401 const ARMBaseInstrInfo &TII,
1402 const unsigned *CSRegs) {
1403 return ((MI->getOpcode() == (int)ARM::VLDRD ||
1404 MI->getOpcode() == (int)ARM::LDR ||
1405 MI->getOpcode() == (int)ARM::t2LDRi12) &&
1406 MI->getOperand(1).isFI() &&
1407 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs));
1410 void ARMBaseRegisterInfo::
1411 emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const {
1412 MachineBasicBlock::iterator MBBI = prior(MBB.end());
1413 assert(MBBI->getDesc().isReturn() &&
1414 "Can only insert epilog into returning blocks");
1415 DebugLoc dl = MBBI->getDebugLoc();
1416 MachineFrameInfo *MFI = MF.getFrameInfo();
1417 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1418 assert(!AFI->isThumb1OnlyFunction() &&
1419 "This emitEpilogue does not suppor Thumb1!");
1420 bool isARM = !AFI->isThumbFunction();
1422 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1423 int NumBytes = (int)MFI->getStackSize();
1425 if (!AFI->hasStackFrame()) {
1427 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
1429 // Unwind MBBI to point to first LDR / VLDRD.
1430 const unsigned *CSRegs = getCalleeSavedRegs();
1431 if (MBBI != MBB.begin()) {
1434 while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs));
1435 if (!isCSRestore(MBBI, TII, CSRegs))
1439 // Move SP to start of FP callee save spill area.
1440 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
1441 AFI->getGPRCalleeSavedArea2Size() +
1442 AFI->getDPRCalleeSavedAreaSize());
1444 // Darwin ABI requires FP to point to the stack slot that contains the
1446 bool HasFP = hasFP(MF);
1447 if ((STI.isTargetDarwin() && NumBytes) || HasFP) {
1448 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
1449 // Reset SP based on frame pointer only if the stack frame extends beyond
1450 // frame pointer stack slot or target is ELF and the function has FP.
1452 AFI->getGPRCalleeSavedArea2Size() ||
1453 AFI->getDPRCalleeSavedAreaSize() ||
1454 AFI->getDPRCalleeSavedAreaOffset()) {
1457 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
1460 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
1465 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
1467 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
1469 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2gpr), ARM::SP)
1473 } else if (NumBytes)
1474 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
1476 // Move SP to start of integer callee save spill area 2.
1477 movePastCSLoadStoreOps(MBB, MBBI, ARM::VLDRD, 0, 3, STI);
1478 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedAreaSize());
1480 // Move SP to start of integer callee save spill area 1.
1481 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, ARM::t2LDRi12, 2, STI);
1482 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getGPRCalleeSavedArea2Size());
1484 // Move SP to SP upon entry to the function.
1485 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, ARM::t2LDRi12, 1, STI);
1486 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getGPRCalleeSavedArea1Size());
1490 emitSPUpdate(isARM, MBB, MBBI, dl, TII, VARegSaveSize);
1493 #include "ARMGenRegisterInfo.inc"