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(false),
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 // FIXME: For now, force at least 128-bit alignment. This will push the
480 // nightly tester harder for making sure things work correctly. When
481 // we're ready to enable this for real, this goes back to starting at zero.
482 unsigned MaxAlign = 16;
483 // unsigned MaxAlign = 0;
485 for (int i = FFI->getObjectIndexBegin(),
486 e = FFI->getObjectIndexEnd(); i != e; ++i) {
487 if (FFI->isDeadObjectIndex(i))
490 unsigned Align = FFI->getObjectAlignment(i);
491 MaxAlign = std::max(MaxAlign, Align);
497 /// hasFP - Return true if the specified function should have a dedicated frame
498 /// pointer register. This is true if the function has variable sized allocas
499 /// or if frame pointer elimination is disabled.
501 bool ARMBaseRegisterInfo::hasFP(const MachineFunction &MF) const {
502 const MachineFrameInfo *MFI = MF.getFrameInfo();
503 return (NoFramePointerElim ||
504 needsStackRealignment(MF) ||
505 MFI->hasVarSizedObjects() ||
506 MFI->isFrameAddressTaken());
509 bool ARMBaseRegisterInfo::
510 needsStackRealignment(const MachineFunction &MF) const {
511 // Only do this for ARM if explicitly enabled
512 // FIXME: Once it's passing all the tests, enable by default
513 if (!ARMDynamicStackAlign)
516 // FIXME: To force more brutal testing, realign whether we need to or not.
517 // Change this to be more selective when we turn it on for real, of course.
518 const MachineFrameInfo *MFI = MF.getFrameInfo();
519 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
520 // unsigned StackAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
521 return (RealignStack &&
522 !AFI->isThumb1OnlyFunction() &&
523 // (MFI->getMaxAlignment() > StackAlign) &&
524 !MFI->hasVarSizedObjects());
527 bool ARMBaseRegisterInfo::cannotEliminateFrame(const MachineFunction &MF) const {
528 const MachineFrameInfo *MFI = MF.getFrameInfo();
529 if (NoFramePointerElim && MFI->hasCalls())
531 return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken()
532 || needsStackRealignment(MF);
535 /// estimateStackSize - Estimate and return the size of the frame.
536 static unsigned estimateStackSize(MachineFunction &MF, MachineFrameInfo *MFI) {
537 const MachineFrameInfo *FFI = MF.getFrameInfo();
539 for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
540 int FixedOff = -FFI->getObjectOffset(i);
541 if (FixedOff > Offset) Offset = FixedOff;
543 for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
544 if (FFI->isDeadObjectIndex(i))
546 Offset += FFI->getObjectSize(i);
547 unsigned Align = FFI->getObjectAlignment(i);
548 // Adjust to alignment boundary
549 Offset = (Offset+Align-1)/Align*Align;
551 return (unsigned)Offset;
554 /// estimateRSStackSizeLimit - Look at each instruction that references stack
555 /// frames and return the stack size limit beyond which some of these
556 /// instructions will require scratch register during their expansion later.
558 ARMBaseRegisterInfo::estimateRSStackSizeLimit(MachineFunction &MF) const {
559 unsigned Limit = (1 << 12) - 1;
560 for (MachineFunction::iterator BB = MF.begin(),E = MF.end(); BB != E; ++BB) {
561 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
563 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
564 if (!I->getOperand(i).isFI()) continue;
566 const TargetInstrDesc &Desc = TII.get(I->getOpcode());
567 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
568 if (AddrMode == ARMII::AddrMode3 ||
569 AddrMode == ARMII::AddrModeT2_i8)
572 if (AddrMode == ARMII::AddrMode5 ||
573 AddrMode == ARMII::AddrModeT2_i8s4)
574 Limit = std::min(Limit, ((1U << 8) - 1) * 4);
576 if (AddrMode == ARMII::AddrModeT2_i12 && hasFP(MF))
577 // When the stack offset is negative, we will end up using
578 // the i8 instructions instead.
580 break; // At most one FI per instruction
589 ARMBaseRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
590 RegScavenger *RS) const {
591 // This tells PEI to spill the FP as if it is any other callee-save register
592 // to take advantage the eliminateFrameIndex machinery. This also ensures it
593 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
594 // to combine multiple loads / stores.
595 bool CanEliminateFrame = true;
596 bool CS1Spilled = false;
597 bool LRSpilled = false;
598 unsigned NumGPRSpills = 0;
599 SmallVector<unsigned, 4> UnspilledCS1GPRs;
600 SmallVector<unsigned, 4> UnspilledCS2GPRs;
601 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
603 MachineFrameInfo *MFI = MF.getFrameInfo();
605 // Calculate and set max stack object alignment early, so we can decide
606 // whether we will need stack realignment (and thus FP).
607 if (ARMDynamicStackAlign) {
608 unsigned MaxAlign = std::max(MFI->getMaxAlignment(),
609 calculateMaxStackAlignment(MFI));
610 MFI->setMaxAlignment(MaxAlign);
613 // Don't spill FP if the frame can be eliminated. This is determined
614 // by scanning the callee-save registers to see if any is used.
615 const unsigned *CSRegs = getCalleeSavedRegs();
616 const TargetRegisterClass* const *CSRegClasses = getCalleeSavedRegClasses();
617 for (unsigned i = 0; CSRegs[i]; ++i) {
618 unsigned Reg = CSRegs[i];
619 bool Spilled = false;
620 if (MF.getRegInfo().isPhysRegUsed(Reg)) {
621 AFI->setCSRegisterIsSpilled(Reg);
623 CanEliminateFrame = false;
625 // Check alias registers too.
626 for (const unsigned *Aliases = getAliasSet(Reg); *Aliases; ++Aliases) {
627 if (MF.getRegInfo().isPhysRegUsed(*Aliases)) {
629 CanEliminateFrame = false;
634 if (CSRegClasses[i] == ARM::GPRRegisterClass ||
635 CSRegClasses[i] == ARM::tGPRRegisterClass) {
639 if (!STI.isTargetDarwin()) {
646 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
661 if (!STI.isTargetDarwin()) {
662 UnspilledCS1GPRs.push_back(Reg);
672 UnspilledCS1GPRs.push_back(Reg);
675 UnspilledCS2GPRs.push_back(Reg);
682 bool ForceLRSpill = false;
683 if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
684 unsigned FnSize = TII.GetFunctionSizeInBytes(MF);
685 // Force LR to be spilled if the Thumb function size is > 2048. This enables
686 // use of BL to implement far jump. If it turns out that it's not needed
687 // then the branch fix up path will undo it.
688 if (FnSize >= (1 << 11)) {
689 CanEliminateFrame = false;
694 bool ExtraCSSpill = false;
695 if (!CanEliminateFrame || cannotEliminateFrame(MF)) {
696 AFI->setHasStackFrame(true);
698 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
699 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
700 if (!LRSpilled && CS1Spilled) {
701 MF.getRegInfo().setPhysRegUsed(ARM::LR);
702 AFI->setCSRegisterIsSpilled(ARM::LR);
704 UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(),
705 UnspilledCS1GPRs.end(), (unsigned)ARM::LR));
706 ForceLRSpill = false;
710 // Darwin ABI requires FP to point to the stack slot that contains the
712 if (STI.isTargetDarwin() || hasFP(MF)) {
713 MF.getRegInfo().setPhysRegUsed(FramePtr);
717 // If stack and double are 8-byte aligned and we are spilling an odd number
718 // of GPRs. Spill one extra callee save GPR so we won't have to pad between
719 // the integer and double callee save areas.
720 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
721 if (TargetAlign == 8 && (NumGPRSpills & 1)) {
722 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
723 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
724 unsigned Reg = UnspilledCS1GPRs[i];
725 // Don't spill high register if the function is thumb1
726 if (!AFI->isThumb1OnlyFunction() ||
727 isARMLowRegister(Reg) || Reg == ARM::LR) {
728 MF.getRegInfo().setPhysRegUsed(Reg);
729 AFI->setCSRegisterIsSpilled(Reg);
730 if (!isReservedReg(MF, Reg))
735 } else if (!UnspilledCS2GPRs.empty() &&
736 !AFI->isThumb1OnlyFunction()) {
737 unsigned Reg = UnspilledCS2GPRs.front();
738 MF.getRegInfo().setPhysRegUsed(Reg);
739 AFI->setCSRegisterIsSpilled(Reg);
740 if (!isReservedReg(MF, Reg))
745 // Estimate if we might need to scavenge a register at some point in order
746 // to materialize a stack offset. If so, either spill one additional
747 // callee-saved register or reserve a special spill slot to facilitate
748 // register scavenging. Thumb1 needs a spill slot for stack pointer
749 // adjustments also, even when the frame itself is small.
750 if (RS && !ExtraCSSpill) {
751 MachineFrameInfo *MFI = MF.getFrameInfo();
752 // If any of the stack slot references may be out of range of an
753 // immediate offset, make sure a register (or a spill slot) is
754 // available for the register scavenger. Note that if we're indexing
755 // off the frame pointer, the effective stack size is 4 bytes larger
756 // since the FP points to the stack slot of the previous FP.
757 if (estimateStackSize(MF, MFI) + (hasFP(MF) ? 4 : 0)
758 >= estimateRSStackSizeLimit(MF)) {
759 // If any non-reserved CS register isn't spilled, just spill one or two
760 // extra. That should take care of it!
761 unsigned NumExtras = TargetAlign / 4;
762 SmallVector<unsigned, 2> Extras;
763 while (NumExtras && !UnspilledCS1GPRs.empty()) {
764 unsigned Reg = UnspilledCS1GPRs.back();
765 UnspilledCS1GPRs.pop_back();
766 if (!isReservedReg(MF, Reg)) {
767 Extras.push_back(Reg);
771 // For non-Thumb1 functions, also check for hi-reg CS registers
772 if (!AFI->isThumb1OnlyFunction()) {
773 while (NumExtras && !UnspilledCS2GPRs.empty()) {
774 unsigned Reg = UnspilledCS2GPRs.back();
775 UnspilledCS2GPRs.pop_back();
776 if (!isReservedReg(MF, Reg)) {
777 Extras.push_back(Reg);
782 if (Extras.size() && NumExtras == 0) {
783 for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
784 MF.getRegInfo().setPhysRegUsed(Extras[i]);
785 AFI->setCSRegisterIsSpilled(Extras[i]);
787 } else if (!AFI->isThumb1OnlyFunction()) {
788 // note: Thumb1 functions spill to R12, not the stack.
789 // Reserve a slot closest to SP or frame pointer.
790 const TargetRegisterClass *RC = ARM::GPRRegisterClass;
791 RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
792 RC->getAlignment()));
799 MF.getRegInfo().setPhysRegUsed(ARM::LR);
800 AFI->setCSRegisterIsSpilled(ARM::LR);
801 AFI->setLRIsSpilledForFarJump(true);
805 unsigned ARMBaseRegisterInfo::getRARegister() const {
809 unsigned ARMBaseRegisterInfo::getFrameRegister(MachineFunction &MF) const {
810 if (STI.isTargetDarwin() || hasFP(MF))
815 unsigned ARMBaseRegisterInfo::getEHExceptionRegister() const {
816 llvm_unreachable("What is the exception register");
820 unsigned ARMBaseRegisterInfo::getEHHandlerRegister() const {
821 llvm_unreachable("What is the exception handler register");
825 int ARMBaseRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
826 return ARMGenRegisterInfo::getDwarfRegNumFull(RegNum, 0);
829 unsigned ARMBaseRegisterInfo::getRegisterPairEven(unsigned Reg,
830 const MachineFunction &MF) const {
833 // Return 0 if either register of the pair is a special register.
842 return isReservedReg(MF, ARM::R7) ? 0 : ARM::R6;
844 return isReservedReg(MF, ARM::R9) ? 0 :ARM::R8;
846 return isReservedReg(MF, ARM::R11) ? 0 : ARM::R10;
918 unsigned ARMBaseRegisterInfo::getRegisterPairOdd(unsigned Reg,
919 const MachineFunction &MF) const {
922 // Return 0 if either register of the pair is a special register.
931 return isReservedReg(MF, ARM::R7) ? 0 : ARM::R7;
933 return isReservedReg(MF, ARM::R9) ? 0 :ARM::R9;
935 return isReservedReg(MF, ARM::R11) ? 0 : ARM::R11;
1007 /// emitLoadConstPool - Emits a load from constpool to materialize the
1008 /// specified immediate.
1009 void ARMBaseRegisterInfo::
1010 emitLoadConstPool(MachineBasicBlock &MBB,
1011 MachineBasicBlock::iterator &MBBI,
1013 unsigned DestReg, unsigned SubIdx, int Val,
1014 ARMCC::CondCodes Pred,
1015 unsigned PredReg) const {
1016 MachineFunction &MF = *MBB.getParent();
1017 MachineConstantPool *ConstantPool = MF.getConstantPool();
1019 ConstantInt::get(Type::getInt32Ty(MF.getFunction()->getContext()), Val);
1020 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
1022 BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
1023 .addReg(DestReg, getDefRegState(true), SubIdx)
1024 .addConstantPoolIndex(Idx)
1025 .addReg(0).addImm(0).addImm(Pred).addReg(PredReg);
1028 bool ARMBaseRegisterInfo::
1029 requiresRegisterScavenging(const MachineFunction &MF) const {
1033 bool ARMBaseRegisterInfo::
1034 requiresFrameIndexScavenging(const MachineFunction &MF) const {
1038 // hasReservedCallFrame - Under normal circumstances, when a frame pointer is
1039 // not required, we reserve argument space for call sites in the function
1040 // immediately on entry to the current function. This eliminates the need for
1041 // add/sub sp brackets around call sites. Returns true if the call frame is
1042 // included as part of the stack frame.
1043 bool ARMBaseRegisterInfo::
1044 hasReservedCallFrame(MachineFunction &MF) const {
1045 const MachineFrameInfo *FFI = MF.getFrameInfo();
1046 unsigned CFSize = FFI->getMaxCallFrameSize();
1047 // It's not always a good idea to include the call frame as part of the
1048 // stack frame. ARM (especially Thumb) has small immediate offset to
1049 // address the stack frame. So a large call frame can cause poor codegen
1050 // and may even makes it impossible to scavenge a register.
1051 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
1054 return !MF.getFrameInfo()->hasVarSizedObjects();
1058 emitSPUpdate(bool isARM,
1059 MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
1060 DebugLoc dl, const ARMBaseInstrInfo &TII,
1062 ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
1064 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
1065 Pred, PredReg, TII);
1067 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
1068 Pred, PredReg, TII);
1072 void ARMBaseRegisterInfo::
1073 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1074 MachineBasicBlock::iterator I) const {
1075 if (!hasReservedCallFrame(MF)) {
1076 // If we have alloca, convert as follows:
1077 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
1078 // ADJCALLSTACKUP -> add, sp, sp, amount
1079 MachineInstr *Old = I;
1080 DebugLoc dl = Old->getDebugLoc();
1081 unsigned Amount = Old->getOperand(0).getImm();
1083 // We need to keep the stack aligned properly. To do this, we round the
1084 // amount of space needed for the outgoing arguments up to the next
1085 // alignment boundary.
1086 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1087 Amount = (Amount+Align-1)/Align*Align;
1089 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1090 assert(!AFI->isThumb1OnlyFunction() &&
1091 "This eliminateCallFramePseudoInstr does not suppor Thumb1!");
1092 bool isARM = !AFI->isThumbFunction();
1094 // Replace the pseudo instruction with a new instruction...
1095 unsigned Opc = Old->getOpcode();
1096 ARMCC::CondCodes Pred = (ARMCC::CondCodes)Old->getOperand(1).getImm();
1097 // FIXME: Thumb2 version of ADJCALLSTACKUP and ADJCALLSTACKDOWN?
1098 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
1099 // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
1100 unsigned PredReg = Old->getOperand(2).getReg();
1101 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, Pred, PredReg);
1103 // Note: PredReg is operand 3 for ADJCALLSTACKUP.
1104 unsigned PredReg = Old->getOperand(3).getReg();
1105 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
1106 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, Pred, PredReg);
1114 ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
1115 int SPAdj, int *Value,
1116 RegScavenger *RS) const {
1118 MachineInstr &MI = *II;
1119 MachineBasicBlock &MBB = *MI.getParent();
1120 MachineFunction &MF = *MBB.getParent();
1121 const MachineFrameInfo *MFI = MF.getFrameInfo();
1122 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1123 assert(!AFI->isThumb1OnlyFunction() &&
1124 "This eliminateFrameIndex does not support Thumb1!");
1126 while (!MI.getOperand(i).isFI()) {
1128 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
1131 unsigned FrameReg = ARM::SP;
1132 int FrameIndex = MI.getOperand(i).getIndex();
1133 int Offset = MFI->getObjectOffset(FrameIndex) + MFI->getStackSize() + SPAdj;
1134 bool isFixed = MFI->isFixedObjectIndex(FrameIndex);
1136 // When doing dynamic stack realignment, all of these need to change(?)
1137 if (AFI->isGPRCalleeSavedArea1Frame(FrameIndex))
1138 Offset -= AFI->getGPRCalleeSavedArea1Offset();
1139 else if (AFI->isGPRCalleeSavedArea2Frame(FrameIndex))
1140 Offset -= AFI->getGPRCalleeSavedArea2Offset();
1141 else if (AFI->isDPRCalleeSavedAreaFrame(FrameIndex))
1142 Offset -= AFI->getDPRCalleeSavedAreaOffset();
1143 else if (needsStackRealignment(MF)) {
1144 // When dynamically realigning the stack, use the frame pointer for
1145 // parameters, and the stack pointer for locals.
1146 assert (hasFP(MF) && "dynamic stack realignment without a FP!");
1148 FrameReg = getFrameRegister(MF);
1149 Offset -= AFI->getFramePtrSpillOffset();
1150 // When referencing from the frame pointer, stack pointer adjustments
1154 } else if (hasFP(MF) && AFI->hasStackFrame()) {
1155 assert(SPAdj == 0 && "Unexpected stack offset!");
1156 if (isFixed || MFI->hasVarSizedObjects()) {
1157 // Use frame pointer to reference fixed objects unless this is a
1158 // frameless function.
1159 FrameReg = getFrameRegister(MF);
1160 Offset -= AFI->getFramePtrSpillOffset();
1161 } else if (AFI->isThumb2Function()) {
1162 // In Thumb2 mode, the negative offset is very limited.
1163 int FPOffset = Offset - AFI->getFramePtrSpillOffset();
1164 if (FPOffset >= -255 && FPOffset < 0) {
1165 FrameReg = getFrameRegister(MF);
1171 // Modify MI as necessary to handle as much of 'Offset' as possible
1173 if (!AFI->isThumbFunction())
1174 Done = rewriteARMFrameIndex(MI, i, FrameReg, Offset, TII);
1176 assert(AFI->isThumb2Function());
1177 Done = rewriteT2FrameIndex(MI, i, FrameReg, Offset, TII);
1182 // If we get here, the immediate doesn't fit into the instruction. We folded
1183 // as much as possible above, handle the rest, providing a register that is
1186 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode4) &&
1187 "This code isn't needed if offset already handled!");
1189 unsigned ScratchReg = 0;
1190 int PIdx = MI.findFirstPredOperandIdx();
1191 ARMCC::CondCodes Pred = (PIdx == -1)
1192 ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
1193 unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg();
1195 // Must be addrmode4.
1196 MI.getOperand(i).ChangeToRegister(FrameReg, false, false, false);
1198 ScratchReg = MF.getRegInfo().createVirtualRegister(ARM::GPRRegisterClass);
1199 if (Value) *Value = Offset;
1200 if (!AFI->isThumbFunction())
1201 emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1202 Offset, Pred, PredReg, TII);
1204 assert(AFI->isThumb2Function());
1205 emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1206 Offset, Pred, PredReg, TII);
1208 MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true);
1209 if (!ReuseFrameIndexVals)
1215 /// Move iterator past the next bunch of callee save load / store ops for
1216 /// the particular spill area (1: integer area 1, 2: integer area 2,
1217 /// 3: fp area, 0: don't care).
1218 static void movePastCSLoadStoreOps(MachineBasicBlock &MBB,
1219 MachineBasicBlock::iterator &MBBI,
1220 int Opc1, int Opc2, unsigned Area,
1221 const ARMSubtarget &STI) {
1222 while (MBBI != MBB.end() &&
1223 ((MBBI->getOpcode() == Opc1) || (MBBI->getOpcode() == Opc2)) &&
1224 MBBI->getOperand(1).isFI()) {
1227 unsigned Category = 0;
1228 switch (MBBI->getOperand(0).getReg()) {
1229 case ARM::R4: case ARM::R5: case ARM::R6: case ARM::R7:
1233 case ARM::R8: case ARM::R9: case ARM::R10: case ARM::R11:
1234 Category = STI.isTargetDarwin() ? 2 : 1;
1236 case ARM::D8: case ARM::D9: case ARM::D10: case ARM::D11:
1237 case ARM::D12: case ARM::D13: case ARM::D14: case ARM::D15:
1244 if (Done || Category != Area)
1252 void ARMBaseRegisterInfo::
1253 emitPrologue(MachineFunction &MF) const {
1254 MachineBasicBlock &MBB = MF.front();
1255 MachineBasicBlock::iterator MBBI = MBB.begin();
1256 MachineFrameInfo *MFI = MF.getFrameInfo();
1257 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1258 assert(!AFI->isThumb1OnlyFunction() &&
1259 "This emitPrologue does not suppor Thumb1!");
1260 bool isARM = !AFI->isThumbFunction();
1261 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1262 unsigned NumBytes = MFI->getStackSize();
1263 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
1264 DebugLoc dl = (MBBI != MBB.end() ?
1265 MBBI->getDebugLoc() : DebugLoc::getUnknownLoc());
1267 // Determine the sizes of each callee-save spill areas and record which frame
1268 // belongs to which callee-save spill areas.
1269 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
1270 int FramePtrSpillFI = 0;
1272 // Allocate the vararg register save area. This is not counted in NumBytes.
1274 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -VARegSaveSize);
1276 if (!AFI->hasStackFrame()) {
1278 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes);
1282 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1283 unsigned Reg = CSI[i].getReg();
1284 int FI = CSI[i].getFrameIdx();
1291 if (Reg == FramePtr)
1292 FramePtrSpillFI = FI;
1293 AFI->addGPRCalleeSavedArea1Frame(FI);
1300 if (Reg == FramePtr)
1301 FramePtrSpillFI = FI;
1302 if (STI.isTargetDarwin()) {
1303 AFI->addGPRCalleeSavedArea2Frame(FI);
1306 AFI->addGPRCalleeSavedArea1Frame(FI);
1311 AFI->addDPRCalleeSavedAreaFrame(FI);
1316 // Build the new SUBri to adjust SP for integer callee-save spill area 1.
1317 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -GPRCS1Size);
1318 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, ARM::t2STRi12, 1, STI);
1320 // Set FP to point to the stack slot that contains the previous FP.
1321 // For Darwin, FP is R7, which has now been stored in spill area 1.
1322 // Otherwise, if this is not Darwin, all the callee-saved registers go
1323 // into spill area 1, including the FP in R11. In either case, it is
1324 // now safe to emit this assignment.
1325 if (STI.isTargetDarwin() || hasFP(MF)) {
1326 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : ARM::t2ADDri;
1327 MachineInstrBuilder MIB =
1328 BuildMI(MBB, MBBI, dl, TII.get(ADDriOpc), FramePtr)
1329 .addFrameIndex(FramePtrSpillFI).addImm(0);
1330 AddDefaultCC(AddDefaultPred(MIB));
1333 // Build the new SUBri to adjust SP for integer callee-save spill area 2.
1334 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -GPRCS2Size);
1336 // Build the new SUBri to adjust SP for FP callee-save spill area.
1337 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, ARM::t2STRi12, 2, STI);
1338 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRCSSize);
1340 // Determine starting offsets of spill areas.
1341 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
1342 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
1343 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
1344 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + NumBytes);
1345 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
1346 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
1347 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
1349 movePastCSLoadStoreOps(MBB, MBBI, ARM::FSTD, 0, 3, STI);
1350 NumBytes = DPRCSOffset;
1352 // Adjust SP after all the callee-save spills.
1353 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes);
1356 if (STI.isTargetELF() && hasFP(MF)) {
1357 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
1358 AFI->getFramePtrSpillOffset());
1361 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
1362 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
1363 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
1365 // If we need dynamic stack realignment, do it here.
1366 if (needsStackRealignment(MF)) {
1368 unsigned MaxAlign = MFI->getMaxAlignment();
1369 assert (!AFI->isThumb1OnlyFunction());
1370 Opc = AFI->isThumbFunction() ? ARM::t2BICri : ARM::BICri;
1372 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(Opc), ARM::SP)
1373 .addReg(ARM::SP, RegState::Kill)
1374 .addImm(MaxAlign-1)));
1378 static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
1379 for (unsigned i = 0; CSRegs[i]; ++i)
1380 if (Reg == CSRegs[i])
1385 static bool isCSRestore(MachineInstr *MI,
1386 const ARMBaseInstrInfo &TII,
1387 const unsigned *CSRegs) {
1388 return ((MI->getOpcode() == (int)ARM::FLDD ||
1389 MI->getOpcode() == (int)ARM::LDR ||
1390 MI->getOpcode() == (int)ARM::t2LDRi12) &&
1391 MI->getOperand(1).isFI() &&
1392 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs));
1395 void ARMBaseRegisterInfo::
1396 emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const {
1397 MachineBasicBlock::iterator MBBI = prior(MBB.end());
1398 assert(MBBI->getDesc().isReturn() &&
1399 "Can only insert epilog into returning blocks");
1400 DebugLoc dl = MBBI->getDebugLoc();
1401 MachineFrameInfo *MFI = MF.getFrameInfo();
1402 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1403 assert(!AFI->isThumb1OnlyFunction() &&
1404 "This emitEpilogue does not suppor Thumb1!");
1405 bool isARM = !AFI->isThumbFunction();
1407 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1408 int NumBytes = (int)MFI->getStackSize();
1410 if (!AFI->hasStackFrame()) {
1412 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
1414 // Unwind MBBI to point to first LDR / FLDD.
1415 const unsigned *CSRegs = getCalleeSavedRegs();
1416 if (MBBI != MBB.begin()) {
1419 while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs));
1420 if (!isCSRestore(MBBI, TII, CSRegs))
1424 // Move SP to start of FP callee save spill area.
1425 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
1426 AFI->getGPRCalleeSavedArea2Size() +
1427 AFI->getDPRCalleeSavedAreaSize());
1429 // Darwin ABI requires FP to point to the stack slot that contains the
1431 bool HasFP = hasFP(MF);
1432 if ((STI.isTargetDarwin() && NumBytes) || HasFP) {
1433 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
1434 // Reset SP based on frame pointer only if the stack frame extends beyond
1435 // frame pointer stack slot or target is ELF and the function has FP.
1437 AFI->getGPRCalleeSavedArea2Size() ||
1438 AFI->getDPRCalleeSavedAreaSize() ||
1439 AFI->getDPRCalleeSavedAreaOffset()) {
1442 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
1445 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
1450 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
1452 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
1454 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2gpr), ARM::SP)
1458 } else if (NumBytes)
1459 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
1461 // Move SP to start of integer callee save spill area 2.
1462 movePastCSLoadStoreOps(MBB, MBBI, ARM::FLDD, 0, 3, STI);
1463 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedAreaSize());
1465 // Move SP to start of integer callee save spill area 1.
1466 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, ARM::t2LDRi12, 2, STI);
1467 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getGPRCalleeSavedArea2Size());
1469 // Move SP to SP upon entry to the function.
1470 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, ARM::t2LDRi12, 1, STI);
1471 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getGPRCalleeSavedArea1Size());
1475 emitSPUpdate(isARM, MBB, MBBI, dl, TII, VARegSaveSize);
1478 #include "ARMGenRegisterInfo.inc"